From 62c1b677e74b1c9e7d06637c1d4f7efd72690750 Mon Sep 17 00:00:00 2001 From: Bohan Li Date: Wed, 27 Jan 2021 15:37:55 -0800 Subject: Support bookmarks in vterm. This patch allows saving bookmarks in vterm mode. It saves the current buffer name, as well as the current directory. When asked to restore a bookmark, it will create a new vterm buffer if needed, and navigate to the previous directory if the custom variable vterm-bookmark-check-dir is set to non-nil. This can be quite convenient for people who constantly have several vterm buffers with different names/dirs for different needs. --- vterm.el | 45 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/vterm.el b/vterm.el index b4c0c78..b4dc734 100644 --- a/vterm.el +++ b/vterm.el @@ -701,7 +701,50 @@ Exceptions are defined by `vterm-keymap-exceptions'." #'vterm--window-adjust-process-window-size) ;; Support to compilation-shell-minor-mode ;; Is this necessary? See vterm--compilation-setup - (setq next-error-function 'vterm-next-error-function)) + (setq next-error-function 'vterm-next-error-function) + (setq-local bookmark-make-record-function 'vterm--bookmark-make-record)) + +(defun vterm--bookmark-make-record () + "Create a vterm bookmark. + +Notes down the current directory and buffer name." + `(nil + (handler . vterm--bookmark-handler) + (thisdir . ,default-directory) + (buf-name . ,(buffer-name)) + (defaults . nil))) + +(defcustom vterm-bookmark-check-dir t + "When set to non-nil, also restore directory when restoring a vterm bookmark.") + +;;;###autoload +(defun vterm--bookmark-handler (bmk) + "Handler to restore a vterm bookmark BMK. + +If a vterm buffer of the same name does not exist, the function will create a +new vterm buffer of the name. It also checks the current directory and sets +it to the bookmarked directory if needed." + (let* ((thisdir (bookmark-prop-get bmk 'thisdir)) + (buf-name (bookmark-prop-get bmk 'buf-name)) + (buf (get-buffer buf-name)) + (thismode (and buf (with-current-buffer buf major-mode)))) + ;; create if no such vterm buffer exists + (when (or (not buf) (not (eq thismode 'vterm-mode))) + (setq buf (generate-new-buffer buf-name)) + (with-current-buffer buf + (when vterm-bookmark-check-dir + (setq default-directory thisdir)) + (vterm-mode))) + ;; check the current directory + (with-current-buffer buf + (when (and vterm-bookmark-check-dir + (not (string-equal default-directory thisdir))) + (when vterm-copy-mode + (vterm-copy-mode-done)) + (vterm-insert (concat "cd " thisdir)) + (vterm-send-return))) + ;; set to this vterm buf + (set-buffer buf))) (defun vterm--compilation-setup () "Function to enable the option `compilation-shell-minor-mode' for vterm. -- cgit v1.0