forked from akermu/emacs-libvterm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vterm.el
1472 lines (1247 loc) · 52.6 KB
/
vterm.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
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; vterm.el --- Fully-featured terminal emulator -*- lexical-binding: t; -*-
;; Copyright (C) 2017-2020 by Lukas Fürmetz & Contributors
;;
;; Author: Lukas Fürmetz <fuermetz@mailbox.org>
;; Version: 0.0.1
;; URL: https://github.com/akermu/emacs-libvterm
;; Keywords: terminals
;; Package-Requires: ((emacs "25.1"))
;; This file is not part of GNU Emacs.
;; This file 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 2, or (at your option)
;; any later version.
;; This file 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 GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;;
;; Emacs-libvterm (vterm) is fully-fledged terminal emulator based on an
;; external library (libvterm) loaded as a dynamic module. As a result of using
;; compiled code (instead of elisp), emacs-libvterm is fully capable, fast, and
;; it can seamlessly handle large outputs.
;;; Installation
;; Emacs-libvterm requires support for loading modules. You can check if your
;; Emacs supports modules by inspecting the variable module-file-suffix. If it
;; nil, than, you need to recompile Emacs or obtain a copy of Emacs with this
;; option enabled.
;; Emacs-libvterm requires CMake and libvterm. If libvterm is not available,
;; emacs-libvterm will downloaded and compiled. In this case, libtool is
;; needed.
;; The reccomended way to install emacs-libvterm is from MELPA.
;;; Usage
;; To open a terminal, simply use the command M-x vterm.
;;; Tips and tricks
;; Adding some shell-side configuration enables a large set of additional
;; features, including, directory tracking, prompt recognition, message passing.
;;; Code:
(require 'term/xterm)
(unless module-file-suffix
(error "VTerm needs module support. Please compile Emacs with
the --with-modules option!"))
;;; Compilation of the module
(defcustom vterm-module-cmake-args ""
"Arguments given to CMake to compile vterm-module.
Currently, vterm defines the following flags (in addition to the
ones already available in CMake):
`USE_SYSTEM_LIBVTERM'. Set it to `yes' to use the version of
libvterm installed on your system.
This string is given verbatim to CMake, so it has to have the
correct syntax. An example of meaningful value for this variable
is `-DUSE_SYSTEM_LIBVTERM=yes'."
:type 'string
:group 'vterm)
(defcustom vterm-always-compile-module nil
"If not nil, if `vterm-module' is not found, compile it without asking.
When `vterm-always-compile-module' is nil, vterm will ask for
confirmation before compiling."
:type 'boolean
:group 'vterm)
(defvar vterm-install-buffer-name " *Install vterm* "
"Name of the buffer used for compiling vterm-module.")
(defun vterm-module--cmake-is-available ()
"Return t if cmake is available.
CMake is needed to build vterm, here we check that we can find
the executable."
(unless (executable-find "cmake")
(error "Vterm needs CMake to be compiled. Please, install CMake"))
t)
;;;###autoload
(defun vterm-module-compile ()
"Compile vterm-module."
(interactive)
(when (vterm-module--cmake-is-available)
(let* ((vterm-directory
(shell-quote-argument
;; NOTE: This is a workaround to fix an issue with how the Emacs
;; feature/native-comp branch changes the result of
;; `(locate-library "vterm")'. See emacs-devel thread
;; https://lists.gnu.org/archive/html/emacs-devel/2020-07/msg00306.html
;; for a discussion.
(file-name-directory (locate-library "vterm.el" t))))
(make-commands
(concat
"cd " vterm-directory "; \
mkdir -p build; \
cd build; \
cmake "
vterm-module-cmake-args
" ..; \
make; \
cd -"))
(buffer (get-buffer-create vterm-install-buffer-name)))
(pop-to-buffer buffer)
(compilation-mode)
(if (zerop (let ((inhibit-read-only t))
(call-process "sh" nil buffer t "-c" make-commands)))
(message "Compilation of `emacs-libvterm' module succeeded")
(error "Compilation of `emacs-libvterm' module failed!")))))
;; If the vterm-module is not compiled yet, compile it
(unless (require 'vterm-module nil t)
(if (or vterm-always-compile-module
(y-or-n-p "Vterm needs `vterm-module' to work. Compile it now? "))
(progn
(vterm-module-compile)
(require 'vterm-module))
(error "Vterm will not work until `vterm-module' is compiled!")))
;;; Dependencies
;; Generate this list with:
;; awk -F\" '/bind_function*/ {print "(declare-function", $2, "\"vterm-module\")"}' vterm-module.c
(declare-function vterm--new "vterm-module")
(declare-function vterm--update "vterm-module")
(declare-function vterm--redraw "vterm-module")
(declare-function vterm--write-input "vterm-module")
(declare-function vterm--set-size "vterm-module")
(declare-function vterm--set-pty-name "vterm-module")
(declare-function vterm--get-pwd-raw "vterm-module")
(declare-function vterm--reset-point "vterm-module")
(declare-function vterm--get-icrnl "vterm-module")
(require 'subr-x)
(require 'find-func)
(require 'cl-lib)
(require 'term)
(require 'color)
(require 'compile)
(require 'face-remap)
;;; Options
(defcustom vterm-shell shell-file-name
"The shell that gets run in the vterm."
:type 'string
:group 'vterm)
(defcustom vterm-max-scrollback 1000
"Maximum 'scrollback' value.
The maximum allowed is 100000. This value can modified by
changing the SB_MAX variable in vterm-module.h and recompiling
the module."
:type 'number
:group 'vterm)
(defcustom vterm-min-window-width 80
"Minimum window width."
:type 'number
:group 'vterm)
(defcustom vterm-kill-buffer-on-exit t
"If not nil vterm buffers are killed when the attached process is terminated.
If `vterm-kill-buffer-on-exit' is set to t, when the process
associated to a vterm buffer quits, the buffer is killed. When
nil, the buffer will still be available as if it were in
`fundamental-mode'."
:type 'boolean
:group 'vterm)
(define-obsolete-variable-alias 'vterm-clear-scrollback
'vterm-clear-scrollback-when-clearing "0.0.1")
(define-obsolete-variable-alias 'vterm-use-vterm-prompt
'vterm-use-vterm-prompt-detection-method "0.0.1")
(defcustom vterm-clear-scrollback-when-clearing nil
"If not nil `vterm-clear' clears both screen and scrollback.
The scrollback is everything that is not current visible on
screen in vterm buffers.
If `vterm-clear-scrollback-when-clearing' is nil, `vterm-clear'
clears only the screen, so the scrollback is accessible moving
the point up."
:type 'number
:group 'vterm)
(defcustom vterm-keymap-exceptions
'("C-c" "C-x" "C-u" "C-g" "C-h" "C-l" "M-x" "M-o" "C-v" "M-v" "C-y" "M-y")
"Exceptions for `vterm-keymap'.
If you use a keybinding with a prefix-key, add that prefix-key to
this list. Note that after doing so that prefix-key cannot be sent
to the terminal anymore.
The mapping is done by the macro `vterm-define-key', and the
function `vterm--exclude-keys' removes the keybindings defined in
`vterm-keymap-exceptions'."
:type '(repeat string)
:set (lambda (sym val)
(set sym val)
(when (and (fboundp 'vterm--exclude-keys)
(boundp 'vterm-mode-map))
(vterm--exclude-keys vterm-mode-map val)))
:group 'vterm)
(defcustom vterm-exit-functions nil
"List of functions called when a vterm process exits.
Each function is called with two arguments: the vterm buffer of
the process if any, and a string describing the event passed from
the sentinel.
This hook applies only to new vterms, created after setting this
value with `add-hook'.
Note that this hook will not work if another package like
`shell-pop' sets its own sentinel to the `vterm' process."
:type 'hook
:group 'vterm)
(make-obsolete-variable 'vterm-set-title-functions
"This variable was substituted by `vterm-buffer-name-string'."
"0.0.1")
(defcustom vterm-buffer-name-string nil
"Format string for the title of vterm buffers.
If `vterm-buffer-name-string' is nil, vterm will not set the
title of its buffers. If not nil, `vterm-buffer-name-string' has
to be a format control string (see `format') containing one
instance of %s which will be substituted with the string TITLE.
The argument TITLE is provided by the shell. This requires shell
side configuration.
For example, if `vterm-buffer-name-string' is set to \"vterm %s\",
and the shell properly configured to set TITLE=$(pwd), than vterm
buffers will be named \"vterm\" followed by the current path.
See URL http://tldp.org/HOWTO/Xterm-Title-4.html for additional
information on the how to configure the shell."
:type 'string
:group 'vterm)
(defcustom vterm-term-environment-variable "xterm-256color"
"TERM value for terminal."
:type 'string
:group 'vterm)
(defcustom vterm-environment nil
"List of extra environment variables to the vterm shell processes only.
demo: '(\"env1=v1\" \"env2=v2\")"
:type '(repeat string)
:group 'vterm)
(defcustom vterm-enable-manipulate-selection-data-by-osc52 nil
"Support OSC 52 MANIPULATE SELECTION DATA.
Support copy text to emacs kill ring and system clipboard by using OSC 52.
For example: send base64 encoded 'foo' to kill ring: echo -en '\e]52;c;Zm9v\a',
tmux can share its copy buffer to terminals by supporting osc52(like iterm2 xterm),
you can enable this feature for tmux by :
set -g set-clipboard on #osc 52 copy paste share with iterm
set -ga terminal-overrides ',xterm*:XT:Ms=\E]52;%p1%s;%p2%s\007'
set -ga terminal-overrides ',screen*:XT:Ms=\E]52;%p1%s;%p2%s\007'
The clipboard querying/clearing functionality offered by OSC 52 is not implemented here,
And for security reason, this feature is disabled by default."
:type 'boolean
:group 'vterm)
;; TODO: Improve doc string, it should not point to the readme but it should
;; be self-contained.
(defcustom vterm-eval-cmds '(("find-file" find-file)
("message" message)
("vterm-clear-scrollback" vterm-clear-scrollback))
"Whitelisted Emacs functions that can be executed from vterm.
You can execute Emacs functions directly from vterm buffers. To do this,
you have to escape the name of the function and its arguments with \e]51;E.
See Message passing in README.
The function you want to execute has to be in `vterm-eval-cmds'.
`vterm-eval-cmds' has to be a list of pairs of the format:
\(NAME-OF-COMMAND-IN-SHELL EMACS-FUNCTION)
The need for an explicit map is to avoid arbitrary code execution."
:type '(alist :key-type string)
:group 'vterm)
(defcustom vterm-disable-underline nil
"When not-nil, underline text properties are ignored.
This means that vterm will render underlined text as if it was not
underlined."
:type 'boolean
:group 'vterm)
(defcustom vterm-disable-inverse-video nil
"When not-nil, inverse video text properties are ignored.
This means that vterm will render reversed video text as if it was not
such."
:type 'boolean
:group 'vterm)
(define-obsolete-variable-alias 'vterm-disable-bold-font
'vterm-disable-bold "0.0.1")
(defcustom vterm-disable-bold-font nil
"When not-nil, bold text properties are ignored.
This means that vterm will render bold with the default face weight."
:type 'boolean
:group 'vterm)
(defcustom vterm-ignore-blink-cursor t
"When t, vterm will ignore request from application to turn on or off cursor blink.
If nil, cursor in any window may begin to blink or not blink because `blink-cursor-mode`
is a global minor mode in Emacs, you can use `M-x blink-cursor-mode` to toggle."
:type 'boolean
:group 'vterm)
(defcustom vterm-copy-exclude-prompt t
"When not-nil, the prompt is not included by `vterm-copy-mode-done'."
:type 'boolean
:group 'vterm)
(defcustom vterm-use-vterm-prompt-detection-method t
"When not-nil, the prompt is detected through the shell.
Vterm needs to know where the shell prompt is to enable all the
available features. There are two supported ways to do this.
First, the shell can inform vterm on the location of the prompt.
This requires shell-side configuration: the escape code 51;A is
used to set the current directory and prompt location. This
detection method is the most-reliable. To use it, you have
to change your shell prompt to print 51;A.
The second method is using a regular expression. This method does
not require any shell-side configuration. See
`term-prompt-regexp', for more information."
:type 'boolean
:group 'vterm)
;;; Faces
(defface vterm-color-black
`((t :inherit term-color-black))
"Face used to render black color code.
The foreground color is used as ANSI color 0 and the background
color is used as ANSI color 8."
:group 'vterm)
(defface vterm-color-red
`((t :inherit term-color-red))
"Face used to render red color code.
The foreground color is used as ANSI color 1 and the background
color is used as ANSI color 9."
:group 'vterm)
(defface vterm-color-green
`((t :inherit term-color-green))
"Face used to render green color code.
The foreground color is used as ANSI color 2 and the background
color is used as ANSI color 10."
:group 'vterm)
(defface vterm-color-yellow
`((t :inherit term-color-yellow))
"Face used to render yellow color code.
The foreground color is used as ANSI color 3 and the background
color is used as ANSI color 11."
:group 'vterm)
(defface vterm-color-blue
`((t :inherit term-color-blue))
"Face used to render blue color code.
The foreground color is used as ANSI color 4 and the background
color is used as ANSI color 12."
:group 'vterm)
(defface vterm-color-magenta
`((t :inherit term-color-magenta))
"Face used to render magenta color code.
The foreground color is used as ansi color 5 and the background
color is used as ansi color 13."
:group 'vterm)
(defface vterm-color-cyan
`((t :inherit term-color-cyan))
"Face used to render cyan color code.
The foreground color is used as ansi color 6 and the background
color is used as ansi color 14."
:group 'vterm)
(defface vterm-color-white
`((t :inherit term-color-white))
"Face used to render white color code.
The foreground color is used as ansi color 7 and the background
color is used as ansi color 15."
:group 'vterm)
(defface vterm-color-underline
`((t :inherit default))
"Face used to render cells with underline attribute.
Only foreground is used."
:group 'vterm)
(defface vterm-color-inverse-video
`((t :inherit default))
"Face used to render cells with inverse video attribute.
Only background is used."
:group 'vterm)
;;; Variables
(defvar vterm-color-palette
[vterm-color-black
vterm-color-red
vterm-color-green
vterm-color-yellow
vterm-color-blue
vterm-color-magenta
vterm-color-cyan
vterm-color-white]
"Color palette for the foreground and background.")
(defvar-local vterm--term nil
"Pointer to Term.")
(defvar-local vterm--process nil
"Shell process of current term.")
(defvar-local vterm--redraw-timer nil)
(defvar-local vterm--redraw-immididately nil)
(defvar-local vterm--linenum-remapping nil)
(defvar-local vterm--prompt-tracking-enabled-p nil)
(defvar-local vterm--insert-function (symbol-function #'insert))
(defvar-local vterm--delete-char-function (symbol-function #'delete-char))
(defvar-local vterm--delete-region-function (symbol-function #'delete-region))
(defvar vterm-timer-delay 0.1
"Delay for refreshing the buffer after receiving updates from libvterm.
A larger delary improves performance when receiving large bursts
of data. If nil, never delay. The units are seconds.")
;;; Keybindings
;; We have many functions defined by vterm-define-key. Later, we will bind some
;; of the functions. If the following is not evaluated during compilation, the compiler
;; will complain that some functions are not defined (eg, vterm-send-C-c)
(eval-and-compile
(defmacro vterm-define-key (key)
"Define a command that sends KEY with modifiers C and M to vterm."
(declare (indent defun)
(doc-string 3))
`(defun ,(intern (format "vterm-send-%s" key))()
,(format "Sends %s to the libvterm." key)
(interactive)
(vterm-send-key ,(char-to-string (get-byte (1- (length key)) key))
,(let ((case-fold-search nil))
(or (string-match-p "[A-Z]$" key)
(string-match-p "S-" key)))
,(string-match-p "M-" key)
,(string-match-p "C-" key))))
(mapc (lambda (key)
(eval `(vterm-define-key ,key)))
(cl-loop for prefix in '("M-")
append (cl-loop for char from ?A to ?Z
for key = (format "%s%c" prefix char)
collect key)))
(mapc (lambda (key)
(eval `(vterm-define-key ,key)))
(cl-loop for prefix in '("C-" "M-" "C-S-")
append (cl-loop for char from ?a to ?z
for key = (format "%s%c" prefix char)
collect key))))
;; Function keys and most of C- and M- bindings
(defun vterm--exclude-keys (map exceptions)
"Remove EXCEPTIONS from the keys bound by `vterm-define-keys'.
Exceptions are defined by `vterm-keymap-exceptions'."
(mapc (lambda (key)
(define-key map (kbd key) nil))
exceptions)
(mapc (lambda (key)
(define-key map (kbd key) #'vterm--self-insert))
(cl-loop for number from 1 to 12
for key = (format "<f%i>" number)
unless (member key exceptions)
collect key))
(mapc (lambda (key)
(define-key map (kbd key)
(intern (format "vterm-send-%s" key))))
(cl-loop for prefix in '("M-")
append (cl-loop for char from ?A to ?Z
for key = (format "%s%c" prefix char)
unless (member key exceptions)
collect key)))
(mapc (lambda (key)
(define-key map (kbd key)
(intern (format "vterm-send-%s" key))))
(cl-loop for prefix in '("C-" "M-" "C-S-" )
append (cl-loop for char from ?a to ?z
for key = (format "%s%c" prefix char)
unless (member key exceptions)
collect key))))
(defun vterm-xterm-paste (event)
"Handle xterm paste EVENT in vterm."
(interactive "e")
(with-temp-buffer
(xterm-paste event)
(kill-new (buffer-string)))
(vterm-yank))
(defvar vterm-mode-map
(let ((map (make-sparse-keymap)))
(vterm--exclude-keys map vterm-keymap-exceptions)
(define-key map (kbd "C-]") #'vterm--self-insert)
(define-key map (kbd "M-<") #'vterm--self-insert)
(define-key map (kbd "M->") #'vterm--self-insert)
(define-key map [tab] #'vterm-send-tab)
(define-key map (kbd "TAB") #'vterm-send-tab)
(define-key map [backtab] #'vterm--self-insert)
(define-key map [backspace] #'vterm-send-backspace)
(define-key map (kbd "DEL") #'vterm-send-backspace)
(define-key map [delete] #'vterm-send-delete)
(define-key map [M-backspace] #'vterm-send-meta-backspace)
(define-key map (kbd "M-DEL") #'vterm-send-meta-backspace)
(define-key map [C-backspace] #'vterm-send-meta-backspace)
(define-key map [return] #'vterm-send-return)
(define-key map (kbd "RET") #'vterm-send-return)
(define-key map [C-left] #'vterm-send-M-b)
(define-key map [M-left] #'vterm-send-M-b)
(define-key map [C-right] #'vterm-send-M-f)
(define-key map [M-right] #'vterm-send-M-f)
(define-key map [C-up] #'vterm-send-up)
(define-key map [C-down] #'vterm-send-down)
(define-key map [left] #'vterm-send-left)
(define-key map [right] #'vterm-send-right)
(define-key map [up] #'vterm-send-up)
(define-key map [down] #'vterm-send-down)
(define-key map [prior] #'vterm-send-prior)
(define-key map [S-prior] #'scroll-down-command)
(define-key map [next] #'vterm-send-next)
(define-key map [S-next] #'scroll-up-command)
(define-key map [home] #'vterm--self-insert)
(define-key map [end] #'vterm--self-insert)
(define-key map [C-home] #'vterm--self-insert)
(define-key map [C-end] #'vterm--self-insert)
(define-key map [escape] #'vterm--self-insert)
(define-key map [remap yank] #'vterm-yank)
(define-key map [remap xterm-paste] #'vterm-xterm-paste)
(define-key map [remap yank-pop] #'vterm-yank-pop)
(define-key map [remap mouse-yank-primary] #'vterm-yank-primary)
(define-key map (kbd "C-SPC") #'vterm--self-insert)
(define-key map (kbd "S-SPC") #'vterm-send-space)
(define-key map (kbd "C-_") #'vterm--self-insert)
(define-key map (kbd "C-/") #'vterm-undo)
(define-key map (kbd "M-.") #'vterm-send-meta-dot)
(define-key map (kbd "M-,") #'vterm-send-meta-comma)
(define-key map (kbd "C-c C-y") #'vterm--self-insert)
(define-key map (kbd "C-c C-c") #'vterm-send-C-c)
(define-key map (kbd "C-c C-l") #'vterm-clear-scrollback)
(define-key map (kbd "C-l") #'vterm-clear)
(define-key map (kbd "C-\\") #'vterm-send-ctrl-slash)
(define-key map (kbd "C-c C-g") #'vterm-send-C-g)
(define-key map (kbd "C-c C-u") #'vterm-send-C-u)
(define-key map [remap self-insert-command] #'vterm--self-insert)
(define-key map (kbd "C-c C-r") #'vterm-reset-cursor-point)
(define-key map (kbd "C-c C-n") #'vterm-next-prompt)
(define-key map (kbd "C-c C-p") #'vterm-previous-prompt)
(define-key map (kbd "C-c C-t") #'vterm-copy-mode)
map))
(defvar vterm-copy-mode-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "C-c C-t") #'vterm-copy-mode)
(define-key map [return] #'vterm-copy-mode-done)
(define-key map (kbd "RET") #'vterm-copy-mode-done)
(define-key map (kbd "C-c C-r") #'vterm-reset-cursor-point)
(define-key map (kbd "C-a") #'vterm-beginning-of-line)
(define-key map (kbd "C-e") #'vterm-end-of-line)
(define-key map (kbd "C-c C-n") #'vterm-next-prompt)
(define-key map (kbd "C-c C-p") #'vterm-previous-prompt)
map))
;;; Mode
(define-derived-mode vterm-mode fundamental-mode "VTerm"
"Major mode for vterm buffer."
(buffer-disable-undo)
(and (boundp 'display-line-numbers)
(let ((font-height (expt text-scale-mode-step text-scale-mode-amount)))
(setq vterm--linenum-remapping
(face-remap-add-relative 'line-number :height font-height))))
(let ((process-environment (append vterm-environment
`(,(concat "TERM="
vterm-term-environment-variable)
,(concat "EMACS_VTERM_PATH="
(file-name-directory (find-library-name "vterm")))
"INSIDE_EMACS=vterm"
"LINES"
"COLUMNS")
process-environment))
(process-adaptive-read-buffering nil)
(width (max (- (window-body-width) (vterm--get-margin-width))
vterm-min-window-width)))
(setq vterm--term (vterm--new (window-body-height)
width vterm-max-scrollback
vterm-disable-bold-font
vterm-disable-underline
vterm-disable-inverse-video
vterm-ignore-blink-cursor))
(setq buffer-read-only t)
(setq-local scroll-conservatively 101)
(setq-local scroll-margin 0)
(setq-local hscroll-margin 0)
(setq-local hscroll-step 1)
(setq-local truncate-lines t)
;; Disable all automatic fontification
(setq-local font-lock-defaults '(nil t))
(add-function :filter-return
(local 'filter-buffer-substring-function)
#'vterm--filter-buffer-substring)
(setq vterm--process
(make-process
:name "vterm"
:buffer (current-buffer)
:command
`("/bin/sh" "-c"
,(format
"stty -nl sane %s erase ^? rows %d columns %d >/dev/null && exec %s"
;; Some stty implementations (i.e. that of *BSD) do not
;; support the iutf8 option. to handle that, we run some
;; heuristics to work out if the system supports that
;; option and set the arg string accordingly. This is a
;; gross hack but FreeBSD doesn't seem to want to fix it.
;;
;; See: https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=220009
(if (eq system-type 'berkeley-unix) "" "iutf8")
(window-body-height)
width vterm-shell))
:coding 'no-conversion
:connection-type 'pty
:filter #'vterm--filter
;; The sentinel is needed if there are exit functions or if
;; vterm-kill-buffer-on-exit is set to t. In this latter case,
;; vterm--sentinel will kill the buffer
:sentinel (when (or vterm-exit-functions
vterm-kill-buffer-on-exit)
#'vterm--sentinel))))
;; Change major-mode is not allowed
;; Vterm interfaces with an underlying process. Changing the major
;; mode can break this, leading to segmentation faults.
(add-hook 'change-major-mode-hook
(lambda () (interactive)
(user-error "You cannot change major mode in vterm buffers")) nil t)
(vterm--set-pty-name vterm--term (process-tty-name vterm--process))
(process-put vterm--process 'adjust-window-size-function
#'vterm--window-adjust-process-window-size)
;; Support to compilation-shell-minor-mode
;; Is this necessary? See vterm--compilation-setup
(setq next-error-function 'vterm-next-error-function))
(defun vterm--compilation-setup ()
"Function to enable the option `compilation-shell-minor-mode' for vterm.
`'compilation-shell-minor-mode' would change the value of local
variable `next-error-function', so we should call this function in
`compilation-shell-minor-mode-hook'."
(when (eq major-mode 'vterm-mode)
(setq next-error-function 'vterm-next-error-function)))
(add-hook 'compilation-shell-minor-mode-hook #'vterm--compilation-setup)
;;;###autoload
(defun vterm-next-error-function (n &optional reset)
"Advance to the next error message and visit the file where the error was.
This is the value of `next-error-function' in Compilation
buffers. Prefix arg N says how many error messages to move
forwards (or backwards, if negative).
Optional argument RESET clears all the errors."
(interactive "p")
(let* ((pt (point))
(default-directory default-directory)
(pwd (vterm--get-pwd)))
(when pwd
(setq default-directory pwd))
(goto-char pt)
(compilation-next-error-function n reset)))
;;; Copy Mode
(define-minor-mode vterm-copy-mode
"Toggle `vterm-copy-mode'.
When `vterm-copy-mode' is enabled, the terminal will not display
additional output received from the underlying process and will
behave similarly to buffer in `fundamental-mode'. This mode is
typically used to copy text from vterm buffers.
A conventient way to exit `vterm-copy-mode' is with
`vterm-copy-mode-done', which copies the selected text and exit
`vterm-copy-mode'."
:group 'vterm
:lighter " VTermCopy"
:keymap vterm-copy-mode-map
(if (equal major-mode 'vterm-mode)
(if vterm-copy-mode
(progn ;enable vterm-copy-mode
(use-local-map nil)
(vterm-send-stop))
(vterm-reset-cursor-point)
(use-local-map vterm-mode-map)
(vterm-send-start))
(user-error "You cannot enable vterm-copy-mode outside vterm buffers")))
(defun vterm-copy-mode-done (arg)
"Save the active region or line to the kill ring and exit `vterm-copy-mode'.
If a region is defined then that region is killed, with no region then
current line is killed from start to end.
The option `vterm-copy-exclude-prompt' controls if the prompt
should be included in a line copy. Using the universal prefix ARG
will invert `vterm-copy-exclude-prompt' for that call."
(interactive "P")
(unless vterm-copy-mode
(user-error "This command is effective only in vterm-copy-mode"))
(unless (use-region-p)
(goto-char (vterm--get-beginning-of-line))
;; Are we excluding the prompt?
(if (or (and vterm-copy-exclude-prompt (not arg))
(and (not vterm-copy-exclude-prompt) arg))
(goto-char (max (or (vterm--get-prompt-point) 0)
(vterm--get-beginning-of-line))))
(set-mark (point))
(goto-char (vterm--get-end-of-line)))
(kill-ring-save (region-beginning) (region-end))
(vterm-copy-mode -1))
;;; Commands
(defun vterm--self-insert ()
"Send 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))
(raw-key (event-basic-type last-input-event))
(ev-key (if input-method-function
(let ((inhibit-read-only t))
(funcall input-method-function raw-key))
(vector raw-key))))
(when-let ((key (key-description ev-key)))
(vterm-send-key key shift meta ctrl)))))
(defun vterm-send-key (key &optional shift meta ctrl)
"Send KEY to libvterm with optional modifiers SHIFT, META and CTRL."
(deactivate-mark)
(when vterm--term
(let ((inhibit-redisplay t)
(inhibit-read-only t))
(when (and (not (symbolp last-input-event)) shift (not meta) (not ctrl))
(setq key (upcase key)))
(vterm--update vterm--term key shift meta ctrl)
(setq vterm--redraw-immididately t)
(accept-process-output vterm--process vterm-timer-delay nil t))))
(defun vterm-send (key)
"Send KEY to libvterm. KEY can be anything `kbd' understands."
(let* ((event (listify-key-sequence (kbd key)))
(modifiers (event-modifiers event))
(base (event-basic-type event)))
(vterm-send-key (char-to-string base)
(memq 'shift modifiers)
(memq 'meta modifiers)
(memq 'control modifiers))))
(defun vterm-send-start ()
"Output from the system is started when the system receives START."
(interactive)
(vterm-send-key "<start>"))
(defun vterm-send-stop ()
"Output from the system is stopped when the system receives STOP."
(interactive)
(vterm-send-key "<stop>"))
(defun vterm-send-return ()
"Send `C-m' to the libvterm."
(interactive)
(deactivate-mark)
(when vterm--term
(if (vterm--get-icrnl vterm--term)
(process-send-string vterm--process "\C-j")
(process-send-string vterm--process "\C-m"))))
(defun vterm-send-tab ()
"Send `<tab>' to the libvterm."
(interactive)
(vterm-send-key "<tab>"))
(defun vterm-send-space ()
"Send `<space>' to the libvterm."
(interactive)
(vterm-send-key " "))
(defun vterm-send-backspace ()
"Send `<backspace>' to the libvterm."
(interactive)
(vterm-send-key "<backspace>"))
(defun vterm-send-delete ()
"Send `<delete>' to the libvterm."
(interactive)
(vterm-send-key "<delete>"))
(defun vterm-send-meta-backspace ()
"Send `M-<backspace>' to the libvterm."
(interactive)
(vterm-send-key "<backspace>" nil t))
(defun vterm-send-up ()
"Send `<up>' to the libvterm."
(interactive)
(vterm-send-key "<up>"))
(defun vterm-send-down ()
"Send `<down>' to the libvterm."
(interactive)
(vterm-send-key "<down>"))
(defun vterm-send-left ()
"Send `<left>' to the libvterm."
(interactive)
(vterm-send-key "<left>"))
(defun vterm-send-right ()
"Send `<right>' to the libvterm."
(interactive)
(vterm-send-key "<right>"))
(defun vterm-send-prior ()
"Send `<prior>' to the libvterm."
(interactive)
(vterm-send-key "<prior>"))
(defun vterm-send-next ()
"Send `<next>' to the libvterm."
(interactive)
(vterm-send-key "<next>"))
(defun vterm-send-meta-dot ()
"Send `M-.' to the libvterm."
(interactive)
(vterm-send-key "." nil t))
(defun vterm-send-meta-comma ()
"Send `M-,' to the libvterm."
(interactive)
(vterm-send-key "," nil t))
(defun vterm-send-ctrl-slash ()
"Send `C-\' to the libvterm."
(interactive)
(vterm-send-key "\\" nil nil t))
(defun vterm-send-escape ()
"Send `<escape>' to the libvterm."
(interactive)
(vterm-send-key "<escape>"))
(defun vterm-clear-scrollback ()
"Send `<clear-scrollback>' to the libvterm."
(interactive)
(vterm-send-key "<clear_scrollback>"))
(defun vterm-clear (&optional arg)
"Send `<clear>' to the libvterm.
`vterm-clear-scrollback' determines whether
`vterm-clear' should also clear the scrollback or not.
This behavior can be altered by calling `vterm-clear' with a
prefix argument ARG or with \\[universal-argument]."
(interactive "P")
(if (or
(and vterm-clear-scrollback-when-clearing (not arg))
(and arg (not vterm-clear-scrollback-when-clearing)))
(vterm-clear-scrollback))
(vterm-send-C-l))
(defun vterm-undo ()
"Send `C-_' to the libvterm."
(interactive)
(vterm-send-key "_" nil nil t))
(defun vterm-yank (&optional arg)
"Yank (paste) text in vterm.
Argument ARG is passed to `yank'."
(interactive "P")
(deactivate-mark)
(vterm-goto-char (point))
(let ((inhibit-read-only t))
(cl-letf (((symbol-function 'insert-for-yank) #'vterm-insert))
(yank arg))))
(defun vterm-yank-primary ()
"Yank text from the primary selection in vterm."
(interactive)
(vterm-goto-char (point))
(let ((inhibit-read-only t)
(primary (gui-get-primary-selection)))
(cl-letf (((symbol-function 'insert-for-yank) #'vterm-insert))
(insert-for-yank primary))))
(defun vterm-yank-pop (&optional arg)
"Replaced text just yanked with the next entry in the kill ring.
Argument ARG is passed to `yank'"
(interactive "p")
(vterm-goto-char (point))
(let ((inhibit-read-only t)
(yank-undo-function #'(lambda (_start _end) (vterm-undo))))
(cl-letf (((symbol-function 'insert-for-yank) #'vterm-insert))
(yank-pop arg))))
(defun vterm-send-string (string &optional paste-p)
"Send the string STRING to vterm.
Optional argument PASTE-P paste-p."
(when vterm--term
(when paste-p
(vterm--update vterm--term "<start_paste>" ))
(dolist (char (string-to-list string))
(vterm--update vterm--term (char-to-string char)))
(when paste-p
(vterm--update vterm--term "<end_paste>")))
(setq vterm--redraw-immididately t)
(accept-process-output vterm--process vterm-timer-delay nil t))
(defun vterm-insert (&rest contents)
"Insert the arguments, either strings or characters, at point.
Provide similar behavior as `insert' for vterm."
(when vterm--term
(vterm--update vterm--term "<start_paste>")
(dolist (c contents)
(if (characterp c)
(vterm--update vterm--term (char-to-string c))
(dolist (char (string-to-list c))
(vterm--update vterm--term (char-to-string char)))))
(vterm--update vterm--term "<end_paste>")
(setq vterm--redraw-immididately t)