The terminal switcher uses ivy to select buffers from a list of buffer *names*, however this can cause weird situations if, for example, two `vterm` sessions are in the same folder and buffer name uniquification is active. This commit implements a corrected solution, which constructs an association list of buffer names to their actual buffer object, and retrieves the buffer object from that list after the user has made their selection. This way, changes in buffer names during terminal selection do not lead to confusing results. Change-Id: I3ab3d6b715b32606cf771dabc31d9d4507c8b856 Reviewed-on: https://cl.tvl.fyi/c/depot/+/9145 Tested-by: BuildkiteCI Reviewed-by: tazjin <tazjin@tvl.su>
60 lines
1.8 KiB
EmacsLisp
60 lines
1.8 KiB
EmacsLisp
;;; term-switcher.el --- Easily switch between open vterms
|
|
;;
|
|
;; Copyright (C) 2019-2020 Google Inc.
|
|
;; Copyright (C) 2021-2023 The TVL Authors
|
|
;;
|
|
;; Author: Vincent Ambo <tazjin@tvl.su>
|
|
;; Version: 1.1
|
|
;; Package-Requires: (dash ivy s vterm)
|
|
;;
|
|
;;; Commentary:
|
|
;;
|
|
;; This package adds a function that lets users quickly switch between
|
|
;; different open vterms via ivy.
|
|
|
|
(require 'dash)
|
|
(require 'ivy)
|
|
(require 's)
|
|
(require 'vterm)
|
|
|
|
(defgroup term-switcher nil
|
|
"Customization options `term-switcher'.")
|
|
|
|
(defcustom term-switcher-buffer-prefix "vterm<"
|
|
"String prefix for vterm terminal buffers. For example, if you
|
|
set your titles to match `vterm<...>' a useful prefix might be
|
|
`vterm<'."
|
|
:type '(string)
|
|
:group 'term-switcher)
|
|
|
|
(defun ts/open-or-create-vterm (buffer)
|
|
"Switch to the terminal in BUFFER, or create a new one if buffer is nil."
|
|
(if buffer
|
|
(switch-to-buffer buffer)
|
|
;; Don't open semi-broken vterms over tramp.
|
|
(if (file-remote-p default-directory)
|
|
(let ((default-directory "~"))
|
|
(vterm))
|
|
(vterm))))
|
|
|
|
(defun ts/is-vterm-buffer (buffer)
|
|
"Determine whether BUFFER runs a vterm."
|
|
(equal 'vterm-mode (buffer-local-value 'major-mode buffer)))
|
|
|
|
(defun ts/switch-to-terminal ()
|
|
"Switch to an existing vterm buffer or create a new one."
|
|
|
|
(interactive)
|
|
(let ((terms (-map (lambda (b) (cons (buffer-name b) b))
|
|
(-filter #'ts/is-vterm-buffer (buffer-list)))))
|
|
(if terms
|
|
(ivy-read "Switch to vterm: "
|
|
(cons "New vterm" (-map #'car terms))
|
|
:caller 'ts/switch-to-terminal
|
|
:preselect (s-concat "^" term-switcher-buffer-prefix)
|
|
:require-match t
|
|
:action (lambda (match)
|
|
(ts/open-or-create-vterm (cdr (assoc match terms)))))
|
|
(vterm))))
|
|
|
|
(provide 'term-switcher)
|