2019-12-14 14:21:12 +01:00
|
|
|
;; Utilities for X11 terminal buffers.
|
2018-05-09 00:41:48 +02:00
|
|
|
|
2019-12-14 14:21:12 +01:00
|
|
|
(defvar x11-terminal-program "gnome-terminal"
|
|
|
|
"Which X11 terminal application to use.")
|
|
|
|
|
|
|
|
(defvar x11-terminal-buffer-prefix "Term"
|
|
|
|
"String prefix for X11 terminal buffer names.")
|
|
|
|
|
|
|
|
(defun open-or-create-terminal-buffer (buffer-name)
|
|
|
|
"Switch to the buffer with BUFFER-NAME or create a new buffer
|
|
|
|
running the configured X11 terminal."
|
2018-05-09 00:41:48 +02:00
|
|
|
(let ((buffer (get-buffer buffer-name)))
|
|
|
|
(if (not buffer)
|
2019-12-14 14:21:12 +01:00
|
|
|
(run-external-command x11-terminal-program)
|
2018-05-09 00:41:48 +02:00
|
|
|
(switch-to-buffer buffer))))
|
|
|
|
|
2019-12-14 14:21:12 +01:00
|
|
|
(defun is-terminal-buffer (buffer)
|
|
|
|
"Determine whether BUFFER runs an X11 terminal."
|
2018-10-31 10:29:49 +01:00
|
|
|
(and (equal 'exwm-mode (buffer-local-value 'major-mode buffer))
|
2019-12-14 14:21:12 +01:00
|
|
|
(s-starts-with? x11-terminal-buffer-prefix (buffer-name buffer))))
|
2018-10-31 10:29:49 +01:00
|
|
|
|
2019-12-14 14:21:12 +01:00
|
|
|
(defun counsel-switch-to-terminal ()
|
|
|
|
"Switch to an X11 terminal buffer, or create a new one."
|
2018-05-09 00:41:48 +02:00
|
|
|
(interactive)
|
2018-10-31 10:29:49 +01:00
|
|
|
(let ((terms (-map #'buffer-name
|
2019-12-14 14:21:12 +01:00
|
|
|
(-filter #'is-terminal-buffer (buffer-list)))))
|
2018-05-09 00:41:48 +02:00
|
|
|
(if terms
|
2019-12-14 14:21:12 +01:00
|
|
|
(ivy-read "Switch to terminal buffer: "
|
2018-05-09 00:41:48 +02:00
|
|
|
(cons "New terminal" terms)
|
2019-12-14 14:21:12 +01:00
|
|
|
:caller 'counsel-switch-to-terminal
|
|
|
|
:preselect (s-concat "^" x11-terminal-buffer-prefix)
|
2018-05-09 00:41:48 +02:00
|
|
|
:require-match t
|
2019-12-14 14:21:12 +01:00
|
|
|
:action #'open-or-create-terminal-buffer)
|
|
|
|
(run-external-command x11-terminal-program))))
|
2018-05-09 00:41:48 +02:00
|
|
|
|
|
|
|
(provide 'term-setup)
|