blob: 7e5a259524c918415e9b5b3a8e509bc86b299063 (
plain)
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
|
;;; vterm.el --- This package implements a terminal via libvterm
;;; Commentary:
;;
;; This Emacs module implements a bridge to libvterm to display a terminal in a
;; Emacs buffer.
;;; Code:
(defun vterm-module-compile ()
"This function compiles the vterm-module."
(interactive)
(let ((buffer (get-buffer-create " *Install vterm"))
(default-directory (file-name-directory (locate-library "vterm"))))
(make-process
:name "build-vterm-module"
:buffer buffer
:command '("sh" "-c" "mkdir -p build; cd build; cmake -DCMAKE_BUILD_TYPE=RelWithDebInfo ..; make")
:stderr buffer
:sentinel (lambda (process status)
(if (equal status "finished\n")
(progn (let ((buffer (process-buffer process)))
(bury-buffer buffer)
(when-let ((window (get-buffer-window buffer)))
(delete-window window)))
(message "Sucessfully compiled the emacs-libvterm module."))
(message "Compilation of libvterm has failed."))))
(pop-to-buffer buffer)))
(when (boundp 'vterm-install)
(vterm-module-compile))
(require 'vterm-module)
(require 'subr-x)
(require 'cl-lib)
(require 'color)
(defcustom vterm-shell (getenv "SHELL")
"The shell that gets run in the vterm."
:type 'string
:group 'vterm)
(defcustom vterm-max-scrollback 1000
"Maximum 'scrollback' value."
:type 'number
:group 'vterm)
(defcustom vterm-keymap-exceptions '("C-x" "C-u" "C-g" "C-h" "M-x" "M-o" "C-v" "M-v")
"Exceptions for vterm-keymap.
If you use a keybinding with a prefix-key that prefix-key cannot
be send to the terminal."
:type '(repeat string)
:group 'vterm)
(defface vterm
'((t :inherit default))
"Default face to use in Term mode."
:group 'vterm)
(defface vterm-color-black
'((t :foreground "black" :background "black"))
"Face used to render black color code."
:group 'vterm)
(defface vterm-color-red
'((t :foreground "red3" :background "red3"))
"Face used to render red color code."
:group 'vterm)
(defface vterm-color-green
'((t :foreground "green3" :background "green3"))
"Face used to render green color code."
:group 'vterm)
(defface vterm-color-yellow
'((t :foreground "yellow3" :background "yellow3"))
"Face used to render yellow color code."
:group 'vterm)
(defface vterm-color-blue
'((t :foreground "blue2" :background "blue2"))
"Face used to render blue color code."
:group 'vterm)
(defface vterm-color-magenta
'((t :foreground "magenta3" :background "magenta3"))
"Face used to render magenta color code."
:group 'vterm)
(defface vterm-color-cyan
'((t :foreground "cyan3" :background "cyan3"))
"Face used to render cyan color code."
:group 'vterm)
(defface vterm-color-white
'((t :foreground "white" :background "white"))
"Face used to render white color code."
:group 'vterm)
(defvar vterm--term nil
"Pointer to Term.")
(make-variable-buffer-local 'vterm--term)
(defvar vterm--process nil
"Shell process of current term.")
(make-variable-buffer-local 'vterm--process)
(define-derived-mode vterm-mode fundamental-mode "VTerm"
"Mayor mode for vterm buffer."
(buffer-disable-undo)
(setq vterm--term (vterm--new (window-body-height)
(window-body-width)
vterm-max-scrollback))
(setq buffer-read-only t)
(setq-local scroll-conservatively 101)
(setq-local scroll-margin 0)
(add-hook 'window-size-change-functions #'vterm--window-size-change t t)
(let ((process-environment (append '("TERM=xterm") process-environment)))
(setq vterm--process (make-process
:name "vterm"
:buffer (current-buffer)
:command `("/bin/sh" "-c" ,(format "stty -nl sane iutf8 rows %d columns %d >/dev/null && exec %s" (window-body-height) (window-body-width) vterm-shell))
:coding 'no-conversion
:connection-type 'pty
:filter #'vterm--filter
:sentinel #'ignore))))
;; Keybindings
(define-key vterm-mode-map [t] #'vterm--self-insert)
(define-key vterm-mode-map [mouse-1] nil)
(define-key vterm-mode-map [mouse-2] nil)
(define-key vterm-mode-map [mouse-3] nil)
(define-key vterm-mode-map [mouse-4] #'ignore)
(define-key vterm-mode-map [mouse-5] #'ignore)
(dolist (prefix '("M-" "C-"))
(dolist (char (cl-loop for char from ?a to ?z
collect char))
(let ((key (concat prefix (char-to-string char))))
(unless (cl-member key vterm-keymap-exceptions)
(define-key vterm-mode-map (kbd key) #'vterm--self-insert)))))
(dolist (exception vterm-keymap-exceptions)
(define-key vterm-mode-map (kbd exception) nil))
(defun vterm--self-insert ()
"Sends invoking key to libvterm."
(interactive)
(when vterm--term
(let* ((modifiers (event-modifiers last-input-event))
(shift (memq 'shift modifiers))
(meta (memq 'meta modifiers))
(ctrl (memq 'control modifiers)))
(when-let ((key (key-description (vector (event-basic-type last-input-event))))
(inhibit-redisplay t)
(inhibit-read-only t))
(when (equal modifiers '(shift))
(setq key (upcase key)))
(vterm--update vterm--term key shift meta ctrl)))))
(defun vterm ()
"Create a new vterm."
(interactive)
(let ((buffer (generate-new-buffer "vterm")))
(with-current-buffer buffer
(vterm-mode))
(switch-to-buffer buffer)))
(defun vterm-other-window ()
"Create a new vterm."
(interactive)
(let ((buffer (generate-new-buffer "vterm")))
(with-current-buffer buffer
(vterm-mode))
(pop-to-buffer buffer)))
(defun vterm--flush-output (output)
"Sends the virtual terminal's OUTPUT to the shell."
(process-send-string vterm--process output))
(defun vterm--filter (process input)
"I/O Event. Feeds PROCESS's INPUT to the virtual terminal.
Then triggers a redraw from the module."
(with-current-buffer (process-buffer process)
(vterm--write-input vterm--term input)
(let ((inhibit-read-only t)
(inhibit-redisplay t))
(vterm--update vterm--term))))
(defun vterm--window-size-change (frame)
"Callback triggered by a size change of the FRAME.
Feeds the size change to the virtual terminal."
(dolist (window (window-list frame))
(with-current-buffer (window-buffer window)
(when (and (processp vterm--process)
(process-live-p vterm--process))
(let ((height (window-body-height window))
(width (window-body-width window))
(inhibit-read-only t))
(set-process-window-size vterm--process height width)
(vterm--set-size vterm--term height width))))))
(defun vterm--face-color-hex (face attr)
"Return the color of the FACE's ATTR as a hex string."
(if (< emacs-major-version 26)
(apply #'color-rgb-to-hex (color-name-to-rgb (face-attribute face attr nil 'default)))
(apply #'color-rgb-to-hex (append (color-name-to-rgb (face-attribute face attr nil 'default)) '(2)))))
(defun vterm--delete-lines (line-num count &optional delete-whole-line)
"Delete lines from line-num. If option ‘kill-whole-line’ is non-nil,
then this command kills the whole line including its terminating newline"
(save-excursion
(when (vterm--goto-line line-num)
(delete-region (point) (point-at-eol))
(when delete-whole-line
(when (looking-at "\n")
(delete-char 1))
(when (and (eobp) (looking-back "\n"))
(delete-char -1))
)
(cl-loop repeat (1- count) do
(when (or delete-whole-line (forward-line 1))
(delete-region (point) (point-at-eol))
(when delete-whole-line
(when (looking-at "\n")
(delete-char 1))
(when (and (eobp) (looking-back "\n"))
(delete-char -1)))
)))))
(defun vterm--goto-line(n)
"If move succ return t"
(goto-char (point-min))
(let ((succ (eq 0 (forward-line (1- n)))))
succ))
(defun vterm--buffer-line-num()
"Return the maximum line number."
(line-number-at-pos (point-max)))
(provide 'vterm)
;;; vterm.el ends here
|