Switch from ansi-term to vterm

This commit is contained in:
Wojciech Kozlowski 2019-08-07 12:10:37 +02:00
parent bbdd30fc3d
commit c5df3d3f4f

View File

@ -20,7 +20,7 @@
(defvar emodule/terminal-packages
'(tramp-term)
'(vterm)
)
@ -29,89 +29,6 @@
(defun emodule/terminal-init ()
"Initialise the `terminal' module."
;; --------------------------------------------------------------------------
;; `term' - the terminal reports itself as a "eterm-color". Therefore the
;; `.bashrc' or `.zshrc' scripts can know if they're inside Emacs by testing
;; [ "$TERM" == "eterm-color" ].
;; --------------------------------------------------------------------------
(use-package term
:config
(defun x-term-setup ()
(interactive)
(define-key term-raw-map (kbd "C-y") 'term-send-raw)
(define-key term-raw-map (kbd "C-p") 'term-send-raw)
(define-key term-raw-map (kbd "C-n") 'term-send-raw)
(define-key term-raw-map (kbd "C-s") 'term-send-raw)
(define-key term-raw-map (kbd "C-r") 'term-send-raw)
(define-key term-raw-map (kbd "M-o") 'other-window)
(define-key term-raw-map (kbd "M-w") 'kill-ring-save)
(define-key term-raw-map (kbd "M-y") 'helm-show-kill-ring)
(define-key term-raw-map (kbd "M-d") (lambda () (interactive) (term-send-raw-string "\ed")))
(define-key term-raw-map (kbd "<C-backspace>") (lambda () (interactive) (term-send-raw-string "\e\C-?")))
(define-key term-raw-map (kbd "M-p") (lambda () (interactive) (term-send-raw-string "\ep")))
(define-key term-raw-map (kbd "M-n") (lambda () (interactive) (term-send-raw-string "\en")))
(define-key term-raw-map (kbd "C-S-v") 'term-paste)
(define-key term-raw-map (kbd "C-h") nil) ; unbind C-h
(define-key term-raw-map (kbd "M-x") nil) ; unbind M-x
(define-key term-raw-map (kbd "C-1") 'delete-other-windows)
(define-key term-raw-map (kbd "C-2") 'split-window-below)
(define-key term-raw-map (kbd "C-3") 'split-window-right)
(define-key term-raw-map (kbd "C-0") 'delete-window))
(add-hook 'term-mode-hook 'x-term-setup t)
(setq term-buffer-maximum-size 0)
(defun ansi-term-pop (term-cmd)
"Launch terminal in (preferably) other window."
(let ((ansi-buf nil)
(cur-buf (current-buffer)))
(setq ansi-buf (ansi-term term-cmd))
(switch-to-buffer cur-buf)
(switch-to-buffer-other-window ansi-buf)))
(defun ansi-term-recycle (term-cmd)
"Kill current buffer and start an *ansi-term* in it."
(kill-buffer (current-buffer))
(ansi-term term-cmd))
(defun first-matching-buffer (regex)
"Find first buffer whose name matches REGEXP."
(car (remove-if-not
(apply-partially 'string-match-p regex)
(mapcar 'buffer-name (buffer-list)))))
(defun visit-ansi-term ()
"Open or switch to active ansi-term.
If current buffer is a term:
If it is running
Open a new ansi-term in a new window
If it is not running
Recycle (kill buffer, restart term)
If current buffer is not a term:
If a *ansi-term*<x> buffer exists
Switch to that ansi-term in other window
Recycle if necessary
If it does not exist
Open a new ansi-term in a new window"
(interactive)
(let ((is-term (string= "term-mode" major-mode))
(is-running (term-check-proc (buffer-name)))
(term-cmd "/bin/zsh")
(anon-term (first-matching-buffer "^*ansi-term*")))
(if is-term
(if is-running
(ansi-term-pop term-cmd)
(ansi-term-recycle term-cmd))
(if anon-term
(progn
(switch-to-buffer-other-window anon-term)
(unless (term-check-proc (buffer-name))
(ansi-term-recycle term-cmd)))
(ansi-term-pop term-cmd)))))
(global-set-key (kbd "C-x '") 'visit-ansi-term))
;; --------------------------------------------------------------------------
;; `eshell'
;; --------------------------------------------------------------------------
@ -181,11 +98,58 @@
"tmux"))))
;; --------------------------------------------------------------------------
;; `tramp-term'
;; `vterm'
;; --------------------------------------------------------------------------
(use-package tramp-term
:defer t)
(use-package vterm
:init
(setq vterm-shell "/bin/zsh"
vterm-max-scrollback 10000)
(defun x-vterm-setup ()
(define-key vterm-mode-map
[remap whole-line-or-region-yank] 'vterm-yank))
(defun x-vterm-recycle ()
"Kill current buffer and start a vterm in it."
(let ((working-directory default-directory))
(kill-buffer (current-buffer))
(let ((default-directory working-directory))
(vterm))))
(defun visit-vterm ()
"Open or switch to active vterm.
If current buffer is a vterm:
If it is running
Open a new vterm in a new window
If it is not running
Recycle (kill buffer, restart vterm)
If current buffer is not a vterm:
If a buffer in vterm-mode exists
Switch to that buffer in other window
Recycle if necessary
If it does not exist
Open a new vterm in a new window"
(interactive)
(if (string= "vterm-mode" major-mode)
(if (term-check-proc (buffer-name))
(vterm-other-window)
(x-vterm-recycle))
(let ((anon-term (seq-find (lambda (buffer)
(with-current-buffer buffer
(string= "vterm-mode" major-mode)))
(buffer-list))))
(if anon-term
(progn
(switch-to-buffer-other-window anon-term)
(unless (term-check-proc (buffer-name))
(x-vterm-recycle)))
(vterm-other-window)))))
:hook
(vterm-mode . x-vterm-setup)
:bind
(("C-x '" . visit-vterm)))
)