Subject: 30.0.50; [PATCH] Add bookmark handler for M-x shell
From: Protesilaos Stavrou <[email-masked]>
To: [email-masked]
Date: Thu, 03 Aug 2023 17:41:27 +0300
List-Id: <debbugs-submit.debbugs.gnu.org>

Dear maintainers,

I noticed that M-x shell does not have a bookmark handler like M-x
eshell does.  What do you think about the attached patch?

All the best,
Protesilaos (or simply "Prot")

-- 
Protesilaos Stavrou
https://protesilaos.com
From 69744f953b95dc2f3c9c3039324e121a6d664570 Mon Sep 17 00:00:00 2001
Message-ID: <[email-masked]>
From: Protesilaos Stavrou <[email-masked]>
Date: Thu, 3 Aug 2023 17:35:10 +0300
Subject: [PATCH] Add bookmark handler for M-x shell

* etc/NEWS: Anounce the new feature.
* lisp/shell.el (shell-mode): Add buffer-local 'bookmark-make-record-function'.
(bookmark-prop-get, shell-bookmark-name, shell-bookmark-make-record)
(shell-bookmark-jump): Add bookmark handler.
---
 etc/NEWS      |  6 ++++++
 lisp/shell.el | 26 ++++++++++++++++++++++++++
 2 files changed, 32 insertions(+)

diff --git a/etc/NEWS b/etc/NEWS
index 7b521f3e6fe..da5d9b96002 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -286,6 +286,12 @@ When this user option is non-nil, 'shell-get-old-input' ('C-RET')
 includes multiple shell "\" continuation lines from command output.
 Default is nil.
 
+---
+*** Bookmark handler for 'shell' buffers
+Now the 'bookmark-set' command will record 'shell' buffers.  This
+means that 'bookmark-jump' will create the 'shell' buffer in the
+directory it was in.
+
 ** Prog Mode
 
 +++
diff --git a/lisp/shell.el b/lisp/shell.el
index 0a24b4ea4c2..bdf8eb17fbd 100644
--- a/lisp/shell.el
+++ b/lisp/shell.el
@@ -676,6 +676,7 @@ (define-derived-mode shell-mode comint-mode "Shell"
 
   (setq comint-prompt-regexp shell-prompt-pattern)
   (shell-completion-vars)
+  (setq-local bookmark-make-record-function #'shell-bookmark-make-record)
   (setq-local paragraph-separate "\\'")
   (setq-local paragraph-start comint-prompt-regexp)
   (setq-local font-lock-defaults '(shell-font-lock-keywords t))
@@ -1812,6 +1813,31 @@ (defun shell-highlight-undef-mode-restart ()
   (when shell-highlight-undef-mode
     (shell-highlight-undef-mode 1)))
 
+;;; Bookmark support
+
+;; Adapted from esh-mode.el
+(declare-function bookmark-prop-get "bookmark" (bookmark prop))
+
+(defun shell-bookmark-name ()
+  (format "shell-%s"
+          (file-name-nondirectory
+           (directory-file-name
+            (file-name-directory default-directory)))))
+
+(defun shell-bookmark-make-record ()
+  "Create a bookmark for the current Shell buffer."
+  `(,(shell-bookmark-name)
+    (location . ,default-directory)
+    (handler . shell-bookmark-jump)))
+
+;;;###autoload
+(defun shell-bookmark-jump (bookmark)
+  "Default bookmark handler for Shell buffers."
+  (let ((default-directory (bookmark-prop-get bookmark 'location)))
+    (shell)))
+
+(put 'shell-bookmark-jump 'bookmark-handler-type "Shell")
+
 (provide 'shell)
 
 ;;; shell.el ends here
-- 
2.41.0