tvl-depot/tools/emacs-pkgs/term-switcher/term-switcher.el
Vincent Ambo 11430f4a4b fix(term-switcher.el): Explicitly fail if buffers are missing
Since upgrading to Emacs 27 I have observed a strange behaviour where
this terminal switcher sometimes fails to select a valid buffer, in
which case it falls through to the case that just opens a new buffer
instead.

This is kind of annoying and to aid in debugging this change makes the
creation of new buffers explicit and fails if no matching buffer is
found.

Note that this is likely not a fix for the issue itself, but it will
help debug what is going on.

Change-Id: I906869aba7d25156aaf92c090b169ce02785b85e
Reviewed-on: https://cl.tvl.fyi/c/depot/+/1930
Tested-by: BuildkiteCI
Reviewed-by: tazjin <mail@tazj.in>
2020-09-05 11:45:33 +00:00

57 lines
1.7 KiB
EmacsLisp

;;; term-switcher.el --- Easily switch between open vterms
;;
;; Copyright (C) 2019 Google Inc.
;;
;; Author: Vincent Ambo <tazjin@google.com>
;; 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-name)
"Switch to the buffer with BUFFER-NAME or create a new vterm
buffer."
(if (equal "New vterm" buffer-name)
(vterm)
(if-let ((buffer (get-buffer buffer-name)))
(switch-to-buffer buffer)
(error "Could not find vterm buffer: %s" buffer-name))))
(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 #'buffer-name
(-filter #'ts/is-vterm-buffer (buffer-list)))))
(if terms
(ivy-read "Switch to vterm: "
(cons "New vterm" terms)
:caller 'ts/switch-to-terminal
:preselect (s-concat "^" term-switcher-buffer-prefix)
:require-match t
:action #'ts/open-or-create-vterm)
(vterm))))
(provide 'term-switcher)