Reimplement toggle-vterm

The previous implementation was too complicated, and slightly buggy. This one
seems far more robust. It relies on the ordering given by `buffer-list`.
This commit is contained in:
larstvei 2023-07-29 21:27:36 -07:00
parent 0f675bce1c
commit 470b6419b1

View File

@ -1565,40 +1565,28 @@
(use-package vterm (use-package vterm
:defer t :defer t
:preface :preface
(let ((last-vterm "")) (defvar vterms nil)
(defun toggle-vterm ()
(interactive)
(cond ((string-match-p "^\\vterm<[1-9][0-9]*>$" (buffer-name))
(goto-non-vterm-buffer))
((get-buffer last-vterm) (switch-to-buffer last-vterm))
(t (vterm (setq last-vterm "vterm<1>")))))
(defun goto-non-vterm-buffer () (defun toggle-vterm (&optional n)
(let* ((r "^\\vterm<[1-9][0-9]*>$") (interactive)
(vterm-buffer-p (lambda (b) (string-match-p r (buffer-name b)))) (setq vterms (seq-filter 'buffer-live-p vterms))
(non-vterms (cl-remove-if vterm-buffer-p (buffer-list)))) (cond ((numberp n) (push (vterm n) vterms))
(when non-vterms ((null vterms) (push (vterm 1) vterms))
(switch-to-buffer (car non-vterms))))) ((seq-contains-p vterms (current-buffer))
(switch-to-buffer (car (seq-difference (buffer-list) vterms))))
(defun switch-vterm (n) (t (switch-to-buffer (car (seq-intersection (buffer-list) vterms))))))
(let ((buffer-name (format "vterm<%d>" n)))
(setq last-vterm buffer-name)
(cond ((get-buffer buffer-name)
(switch-to-buffer buffer-name))
(t (vterm buffer-name)
(rename-buffer buffer-name))))))
:bind (:map custom-bindings-map :bind (:map custom-bindings-map
("C-z" . toggle-vterm) ("C-z" . toggle-vterm)
("M-1" . (lambda () (interactive) (switch-vterm 1))) ("M-1" . (lambda () (interactive) (toggle-vterm 1)))
("M-2" . (lambda () (interactive) (switch-vterm 2))) ("M-2" . (lambda () (interactive) (toggle-vterm 2)))
("M-3" . (lambda () (interactive) (switch-vterm 3))) ("M-3" . (lambda () (interactive) (toggle-vterm 3)))
("M-4" . (lambda () (interactive) (switch-vterm 4))) ("M-4" . (lambda () (interactive) (toggle-vterm 4)))
("M-5" . (lambda () (interactive) (switch-vterm 5))) ("M-5" . (lambda () (interactive) (toggle-vterm 5)))
("M-6" . (lambda () (interactive) (switch-vterm 6))) ("M-6" . (lambda () (interactive) (toggle-vterm 6)))
("M-7" . (lambda () (interactive) (switch-vterm 7))) ("M-7" . (lambda () (interactive) (toggle-vterm 7)))
("M-8" . (lambda () (interactive) (switch-vterm 8))) ("M-8" . (lambda () (interactive) (toggle-vterm 8)))
("M-9" . (lambda () (interactive) (switch-vterm 9)))) ("M-9" . (lambda () (interactive) (toggle-vterm 9))))
:config :config
;; Don't query about killing vterm buffers, just kill it ;; Don't query about killing vterm buffers, just kill it