2019-12-16 04:50:34 +01:00
|
|
|
;;; term-switcher.el --- Easily switch between open vterms
|
2019-12-15 23:53:58 +01:00
|
|
|
;;
|
|
|
|
;; Copyright (C) 2019 Google Inc.
|
|
|
|
;;
|
|
|
|
;; Author: Vincent Ambo <tazjin@google.com>
|
2019-12-16 04:50:34 +01:00
|
|
|
;; Version: 1.1
|
2019-12-16 04:57:03 +01:00
|
|
|
;; Package-Requires: (dash ivy s vterm)
|
2019-12-15 23:53:58 +01:00
|
|
|
;;
|
|
|
|
;;; Commentary:
|
|
|
|
;;
|
|
|
|
;; This package adds a function that lets users quickly switch between
|
2019-12-16 04:50:34 +01:00
|
|
|
;; different open vterms via ivy.
|
2019-12-15 23:53:58 +01:00
|
|
|
|
|
|
|
(require 'dash)
|
|
|
|
(require 'ivy)
|
|
|
|
(require 's)
|
2019-12-16 04:57:03 +01:00
|
|
|
(require 'vterm)
|
2019-12-15 23:53:58 +01:00
|
|
|
|
|
|
|
(defgroup term-switcher nil
|
2019-12-16 04:50:34 +01:00
|
|
|
"Customization options `term-switcher'.")
|
2019-12-15 23:53:58 +01:00
|
|
|
|
2019-12-16 04:50:34 +01:00
|
|
|
(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<'."
|
2019-12-15 23:53:58 +01:00
|
|
|
:type '(string)
|
|
|
|
:group 'term-switcher)
|
|
|
|
|
2019-12-16 04:50:34 +01:00
|
|
|
(defun ts/open-or-create-vterm (buffer-name)
|
|
|
|
"Switch to the buffer with BUFFER-NAME or create a new vterm
|
|
|
|
buffer."
|
2020-09-05 13:40:26 +02:00
|
|
|
(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))))
|
2019-12-15 23:53:58 +01:00
|
|
|
|
2019-12-16 04:50:34 +01:00
|
|
|
(defun ts/is-vterm-buffer (buffer)
|
|
|
|
"Determine whether BUFFER runs a vterm."
|
|
|
|
(equal 'vterm-mode (buffer-local-value 'major-mode buffer)))
|
2019-12-15 23:53:58 +01:00
|
|
|
|
|
|
|
(defun ts/switch-to-terminal ()
|
2019-12-16 04:50:34 +01:00
|
|
|
"Switch to an existing vterm buffer or create a new one."
|
|
|
|
|
2019-12-15 23:53:58 +01:00
|
|
|
(interactive)
|
|
|
|
(let ((terms (-map #'buffer-name
|
2019-12-16 04:50:34 +01:00
|
|
|
(-filter #'ts/is-vterm-buffer (buffer-list)))))
|
2019-12-15 23:53:58 +01:00
|
|
|
(if terms
|
2019-12-16 04:50:34 +01:00
|
|
|
(ivy-read "Switch to vterm: "
|
|
|
|
(cons "New vterm" terms)
|
2019-12-15 23:53:58 +01:00
|
|
|
:caller 'ts/switch-to-terminal
|
|
|
|
:preselect (s-concat "^" term-switcher-buffer-prefix)
|
|
|
|
:require-match t
|
2019-12-16 04:50:34 +01:00
|
|
|
:action #'ts/open-or-create-vterm)
|
|
|
|
(vterm))))
|
2019-12-15 23:53:58 +01:00
|
|
|
|
|
|
|
(provide 'term-switcher)
|