-
Notifications
You must be signed in to change notification settings - Fork 6
/
eaf-terminal.el
211 lines (179 loc) · 6.1 KB
/
eaf-terminal.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
;;; eaf-terminal.el --- Terminal plugins
;; Filename: eaf-terminal.el
;; Description: Terminal plugins
;; Author: Andy Stewart <[email protected]>
;; Maintainer: Andy Stewart <[email protected]>
;; Copyright (C) 2021, Andy Stewart, all rights reserved.
;; Created: 2021-07-20 22:46:47
;; Version: 0.1
;; Last-Updated: Sat Jul 31 11:29:58 2021 (-0400)
;; By: Mingde (Matthew) Zeng
;; URL: http://www.emacswiki.org/emacs/download/eaf-terminal.el
;; Keywords:
;; Compatibility: GNU Emacs 28.0.50
;;
;; Features that might be required by this library:
;;
;;
;;
;;; This file is NOT part of GNU Emacs
;;; License
;;
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth
;; Floor, Boston, MA 02110-1301, USA.
;;; Commentary:
;;
;; Terminal plugins
;;
;;; Installation:
;;
;; Put eaf-terminal.el to your load-path.
;; The load-path is usually ~/elisp/.
;; It's set in your ~/.emacs like this:
;; (add-to-list 'load-path (expand-file-name "~/elisp"))
;;
;; And the following to your ~/.emacs startup file.
;;
;; (require 'eaf-terminal)
;;
;; No need more.
;;; Customize:
;;
;;
;;
;; All of the above can customize by:
;; M-x customize-group RET eaf-terminal RET
;;
;;; Change log:
;;
;; 2021/07/20
;; * First released.
;;
;;; Acknowledgements:
;;
;;
;;
;;; TODO
;;
;;
;;
;;; Require
;;; Code:
(defcustom eaf-terminal-dark-mode "follow"
""
:type 'string)
(defcustom eaf-terminal-font-size 13
""
:type 'integer)
(defcustom eaf-terminal-font-family ""
""
:type 'string)
(defcustom eaf-terminal-keybinding
'(("M-j" . "scroll_up")
("M-k" . "scroll_down")
("s-J" . "scroll_up")
("s-K" . "scroll_down")
("C-v" . "scroll_up_page")
("M-v" . "scroll_down_page")
("M-<" . "scroll_to_begin")
("M->" . "scroll_to_bottom")
("C-z k" . "scroll_to_begin")
("C-z j" . "scroll_to_bottom")
("C--" . "zoom_out")
("C-=" . "zoom_in")
("C-0" . "zoom_reset")
("C-S-c" . "copy_text")
("C-S-v" . "yank_text")
("C-s" . "search_text_forward")
("M-s" . "search_text_backward")
("C-a" . "eaf-send-key-sequence")
("C-e" . "eaf-send-key-sequence")
("C-f" . "eaf-send-key-sequence")
("C-b" . "eaf-send-key-sequence")
("C-d" . "eaf-send-key-sequence")
("C-n" . "eaf-send-key-sequence")
("C-p" . "eaf-send-key-sequence")
("C-r" . "eaf-send-key-sequence")
("C-y" . "eaf-send-key-sequence")
("C-k" . "eaf-send-key-sequence")
("C-o" . "eaf-send-key-sequence")
("C-u" . "eaf-send-key-sequence")
("C-l" . "eaf-send-key-sequence")
("C-w" . "eaf-send-key-sequence")
("M-f" . "eaf-send-key-sequence")
("M-b" . "eaf-send-key-sequence")
("M-d" . "eaf-send-key-sequence")
("C-c C-c" . "eaf-send-second-key-sequence")
("C-c C-x" . "eaf-send-second-key-sequence")
("<f12>" . "open_devtools")
("M-w" . "copy_text")
("C-y" . "yank_text")
("C-S-a" . "select_all")
("C-S-l" . "clear_selection")
("C-M-l" . "clear")
("M-DEL" . "eaf-send-alt-backspace-sequence")
("M-<backspace>" . "eaf-send-alt-backspace-sequence")
("<escape>" . "eaf-send-escape-key"))
"The keybinding of EAF Terminal."
:type 'cons)
(add-to-list 'eaf-app-binding-alist '("terminal" . eaf-terminal-keybinding))
(setq eaf-terminal-module-path (concat (file-name-directory load-file-name) "buffer.py"))
(add-to-list 'eaf-app-module-path-alist '("terminal" . eaf-terminal-module-path))
(defun eaf-send-second-key-sequence ()
"Send second part of key sequence to terminal."
(interactive)
(eaf-call-async "send_key_sequence"
eaf--buffer-id
(nth 1 (split-string (key-description (this-command-keys-vector))))))
(defun eaf-ipython-command ()
(if (eaf--called-from-wsl-on-windows-p)
"ipython.exe"
"ipython"))
(defun eaf-open-ipython ()
"Open ipython in terminal."
(interactive)
(if (executable-find (eaf-ipython-command))
(eaf-terminal-run-command-in-dir
(eaf-ipython-command)
(eaf--non-remote-default-directory))
(message "[EAF/terminal] Please install ipython first.")))
(defun eaf-terminal-get-row-number ()
(/ (nth 3 (blink-search-get-window-allocation (get-buffer-window blink-search-candidate-buffer))) (line-pixel-height)))
(defun eaf-terminal-run-command-in-dir (command dir &optional always-new)
"Run COMMAND in terminal in directory DIR.
If ALWAYS-NEW is non-nil, always open a new terminal for the dedicated DIR."
(let* ((args (make-hash-table :test 'equal))
(expand-dir (expand-file-name dir)))
(puthash "command" command args)
(puthash "directory"
(if (eaf--called-from-wsl-on-windows-p)
(eaf--translate-wsl-url-to-windows expand-dir)
expand-dir)
args)
(eaf-open dir "terminal" (json-encode-hash-table args) always-new)))
(defun eaf--generate-terminal-command ()
(if (or (eaf--called-from-wsl-on-windows-p)
(eq system-type 'windows-nt))
"powershell.exe"
(getenv "SHELL")))
;;;###autoload
(defun eaf-open-terminal ()
"Open EAF Terminal, a powerful GUI terminal emulator in Emacs.
The initial directory is `default-directory'. However, it opens `$HOME'
when `default-directory' is part of a remote process.
If a buffer of EAF Terminal in `default-directory' exists, switch to the buffer.
To override and open a new terminal regardless, call interactively with prefix arg."
(interactive)
(eaf-terminal-run-command-in-dir (eaf--generate-terminal-command) (eaf--non-remote-default-directory) t))
(provide 'eaf-terminal)
;;; eaf-terminal.el ends here