-
Notifications
You must be signed in to change notification settings - Fork 309
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP] add libvterm support #1130
Changes from all commits
61efb78
e5f3a06
6d64227
790b6c7
19ae959
b74c01b
fcbfed5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,234 @@ | ||
(require 'subr-x) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I assume this file comes from the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's wait how much code still comes from the module when we merge this. |
||
(require 'cl-lib) | ||
(require 'color) | ||
|
||
(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) | ||
|
||
(defcustom vterm-shell (getenv "SHELL") | ||
"The shell that gets run in the vterm." | ||
:type 'string | ||
:group 'vterm) | ||
|
||
(defcustom vterm-display-method 'switch-to-buffer | ||
"Default display method." | ||
:type '(choice (const :tag "Display buffer." 'switch-to-buffer) | ||
(const :tag "Pop to buffer." 'pop-to-buffer)) | ||
:group 'vterm) | ||
|
||
(defcustom vterm-max-scrollback 1000 | ||
"Maximum 'scrollback' value." | ||
:type 'number | ||
:group 'vterm) | ||
|
||
(defcustom vterm-keymap-exceptions '("C-c" "C-x" "C-u" "C-g" "C-h" "M-x" "M-o" "C-v" "M-v" "C-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." | ||
:type '(repeat string) | ||
:group 'vterm) | ||
|
||
(defcustom vterm-set-title-hook nil | ||
"Shell set title hook. | ||
|
||
those functions are called one by one, with 1 arguments. | ||
`vterm-set-title-hook' should be a symbol, a hook variable. | ||
The value of HOOK may be nil, a function, or a list of functions. | ||
for example | ||
(defun vterm--rename-buffer-as-title (title) | ||
(rename-buffer (format \"vterm %s\" title))) | ||
(add-hook 'vterm-set-title-hook 'vterm--rename-buffer-as-title) | ||
|
||
see http://tldp.org/HOWTO/Xterm-Title-4.html about how to set terminal title | ||
for different shell. " | ||
:type 'hook | ||
:group 'vterm) | ||
|
||
(defvar vterm--term nil | ||
"Pointer to Term.") | ||
(make-variable-buffer-local 'vterm--term) | ||
|
||
(defvar vterm-buffer-name "*vterm*" | ||
"Buffer name for vterm buffers.") | ||
|
||
(defvar vterm-mode-map | ||
(let ((map (make-sparse-keymap))) | ||
(define-key map [tab] #'vterm--self-insert) | ||
(define-key map [backspace] #'vterm--self-insert) | ||
(define-key map [M-backspace] #'vterm--self-insert) | ||
(define-key map [return] #'vterm--self-insert) | ||
(define-key map [left] #'vterm--self-insert) | ||
(define-key map [right] #'vterm--self-insert) | ||
(define-key map [up] #'vterm--self-insert) | ||
(define-key map [down] #'vterm--self-insert) | ||
(define-key map [home] #'vterm--self-insert) | ||
(define-key map [end] #'vterm--self-insert) | ||
(define-key map [escape] #'vterm--self-insert) | ||
(define-key map [remap self-insert-command] #'vterm--self-insert) | ||
(define-key map [remap yank] #'vterm-yank) | ||
(define-key map (kbd "C-c C-y") #'vterm--self-insert) | ||
(define-key map (kbd "C-c C-c") #'vterm-send-ctrl-c) | ||
map) | ||
"Keymap for `vterm-mode'.") | ||
|
||
;; Function keys and most of C- and M- bindings | ||
(mapcar (lambda (key) | ||
(define-key vterm-mode-map (kbd key) #'vterm--self-insert)) | ||
(append (cl-loop for number from 1 to 12 | ||
collect (format "<f%i>" number)) | ||
(cl-loop for prefix in '("C-" "M-") | ||
append (cl-loop for char from ?a to ?z | ||
for key = (format "%s%c" prefix char) | ||
unless (member key vterm-keymap-exceptions) | ||
collect key)))) | ||
|
||
(define-derived-mode vterm-mode fundamental-mode "VTerm" | ||
"Major mode for vterm buffer." | ||
(buffer-disable-undo) | ||
(setq vterm--term (vterm-new (window-body-height) | ||
(window-body-width) | ||
(vterm-make-process) | ||
;; 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-resize-window t t)) | ||
|
||
(defun vterm-make-process () | ||
(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 #'vterm-sentinel)))) | ||
|
||
(defun vterm-filter (process output) | ||
"I/O Event. Feeds PROCESS's OUTPUT to the virtual terminal. | ||
|
||
Then triggers a redraw from the module." | ||
(let ((inhibit-redisplay t) | ||
(inhibit-read-only t)) | ||
(with-current-buffer (process-buffer process) | ||
(vterm-write-input vterm--term output) | ||
(vterm-update vterm--term)))) | ||
|
||
(defun vterm-sentinel (process string) | ||
(let ((buf (process-buffer proc))) | ||
(when (buffer-live-p buf) | ||
(kill-buffer buf)))) | ||
|
||
(defun vterm-resize-window (frame) | ||
(let (bufs) | ||
(dolist (win (window-list frame)) | ||
(let ((buf (window-buffer win))) | ||
(with-current-buffer buf | ||
(when (and (eq major-mode 'vterm-mode) | ||
(not (member buf bufs))) | ||
(with-selected-window win | ||
(push buf bufs) | ||
(let ((height (window-body-height)) | ||
(width (window-body-width)) | ||
(inhibit-read-only t)) | ||
(set-process-window-size (get-buffer-process buf) height width) | ||
(vterm-set-size vterm--term height width))))))))) | ||
|
||
(defun vterm--self-insert () | ||
"Sends invoking key to libvterm." | ||
(interactive) | ||
(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))))) | ||
(vterm-send-key key shift meta ctrl)))) | ||
|
||
(defun vterm-send-key (key &optional shift meta ctrl) | ||
"Sends KEY to libvterm with optional modifiers SHIFT, META and CTRL." | ||
(let ((inhibit-redisplay t) | ||
(inhibit-read-only t)) | ||
(when (and shift (not meta) (not ctrl)) | ||
(setq key (upcase key))) | ||
(vterm-update vterm--term key shift meta ctrl))) | ||
|
||
(defun vterm--face-color-hex (face attr) | ||
"Return the color of the FACE's ATTR as a hex string." | ||
(apply #'color-rgb-to-hex (append (color-name-to-rgb (face-attribute face attr nil 'default)) '(2)))) | ||
|
||
(defun vterm-yank () | ||
"Implementation of `yank' (paste) in vterm." | ||
(interactive) | ||
(vterm-send-string (current-kill 0))) | ||
|
||
(defun vterm-send-string (string) | ||
"Send the string STRING to vterm." | ||
(when vterm--term | ||
(dolist (char (string-to-list string)) | ||
(vterm-update vterm--term (char-to-string char) nil nil nil)))) | ||
|
||
;;;###autoload | ||
(defun vterm (&optional arg) | ||
"Display vterminal. If called with prefix arg open new terminal." | ||
(interactive "P") | ||
(let ((buffer (if arg | ||
(generate-new-buffer vterm-buffer-name) | ||
(get-buffer-create vterm-buffer-name)))) | ||
(when (or arg (not (get-buffer-process buffer))) | ||
(with-current-buffer buffer | ||
(vterm-mode))) | ||
(funcall vterm-display-method buffer))) | ||
|
||
(provide 'vterm) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, you need this... it should says
[Define if libvterm is available.]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will update this together with the make stuff.