Subject: [PATCH] Modify comint-read-input-ring to skip uninteresting text

From: Brian Leung <[email-masked]>
Date: Sat, 2 Jan 2021 04:56:54 +0100 (CET)

I made the attached changes to handle zsh's history on Emacs' master branch.

This is a follow-up to Matthew Bauer's commit f7421039fb15123dbbda047e23504dd1771183b5 which sought to support zsh's extended_history. Matthew, would you confirm that these patches are OK?

Thanks,
Brian

-- 
Sent with https://mailfence.com
Secure and private email

>From 258a325eb90cc516231714fb329a0ad876ea537f Mon Sep 17 00:00:00 2001
From: Brian Leung <[email-masked]>
Date: Fri, 1 Jan 2021 18:56:41 -0800
Subject: [PATCH 1/4] shell-mode: use "~/.zsh_history" as a history file

* lisp/shell.el (shell-mode): Add "~/.zsh_history".
---
 lisp/shell.el | 1 +
 1 file changed, 1 insertion(+)

diff --git a/lisp/shell.el b/lisp/shell.el
index c179dd24d3..0f866158fe 100644
--- a/lisp/shell.el
+++ b/lisp/shell.el
@@ -603,6 +603,7 @@ shell-mode
 	     (or hfile
 		 (cond ((string-equal shell "bash") "~/.bash_history")
 		       ((string-equal shell "ksh") "~/.sh_history")
+		       ((string-equal shell "zsh") "~/.zsh_history")
 		       (t "~/.history")))))
       (if (or (equal comint-input-ring-file-name "")
 	      (equal (file-truename comint-input-ring-file-name)
-- 
2.29.2

>From 5a6418f693bd71c3dc0cb398209b769c45e59f30 Mon Sep 17 00:00:00 2001
From: Brian Leung <[email-masked]>
Date: Fri, 1 Jan 2021 18:58:26 -0800
Subject: [PATCH 2/4] comint-read-input-ring: Use comint buffer's
 ring-file-prefix

* lisp/comint.el (comint-read-input-ring): Bind
`comint-input-ring-file-prefix' in anticipation of a buffer switch.
---
 lisp/comint.el | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/lisp/comint.el b/lisp/comint.el
index 2e683a7572..f92b7b4a7b 100644
--- a/lisp/comint.el
+++ b/lisp/comint.el
@@ -979,6 +979,7 @@ comint-read-input-ring
 		(ring (make-ring ring-size))
                 ;; Use possibly buffer-local values of these variables.
                 (ring-separator comint-input-ring-separator)
+                (ring-file-prefix comint-input-ring-file-prefix)
                 (history-ignore comint-input-history-ignore)
                 (ignoredups comint-input-ignoredups))
 	   (with-temp-buffer
@@ -993,17 +994,17 @@ comint-read-input-ring
                  (setq start
                        (if (re-search-backward ring-separator nil t)
                            (progn
-                             (when (and comint-input-ring-file-prefix
+                             (when (and ring-file-prefix
                                         (looking-at
-                                         comint-input-ring-file-prefix))
+                                         ring-file-prefix))
                                ;; Skip zsh extended_history stamps
                                (goto-char (match-end 0)))
                              (match-end 0))
                          (progn
                            (goto-char (point-min))
-                           (when (and comint-input-ring-file-prefix
+                           (when (and ring-file-prefix
                                       (looking-at
-                                       comint-input-ring-file-prefix))
+                                       ring-file-prefix))
                              (goto-char (match-end 0)))
                            (point))))
                  (setq history (buffer-substring start end))
-- 
2.29.2

>From 601431c612b1585cbf32f18d4de6287b83cfbdf2 Mon Sep 17 00:00:00 2001
From: Brian Leung <[email-masked]>
Date: Fri, 1 Jan 2021 19:04:27 -0800
Subject: [PATCH 3/4] comint-read-input-ring: skip over the
 comint-input-ring-separator

* lisp/comint.el (comint-read-input-ring): Skip the separator.

Because re-search-backward moves point to the beginning of the match,
and since we don't want the separator appearing in the output, we skip
over it.

This is required to properly detect instances of the value that zsh
uses for `comint-input-ring-file-prefix'; if the
`comint-input-ring-file-prefix' is ':potato', the subsequent
invocation `looking-at' sees '\n:potato' for all entries after the one
at the very beginning of the history file.
---
 lisp/comint.el | 1 +
 1 file changed, 1 insertion(+)

diff --git a/lisp/comint.el b/lisp/comint.el
index f92b7b4a7b..36fcbb9465 100644
--- a/lisp/comint.el
+++ b/lisp/comint.el
@@ -994,6 +994,7 @@ comint-read-input-ring
                  (setq start
                        (if (re-search-backward ring-separator nil t)
                            (progn
+                             (goto-char (match-end 0))
                              (when (and ring-file-prefix
                                         (looking-at
                                          ring-file-prefix))
-- 
2.29.2

>From 125025c407e6995dd9239a51997082cb898acf3b Mon Sep 17 00:00:00 2001
From: Brian Leung <[email-masked]>
Date: Fri, 1 Jan 2021 19:41:26 -0800
Subject: [PATCH 4/4] comint-read-input-ring: Tidy

* lisp/comint.el (comint-read-input-ring): Simplify.
---
 lisp/comint.el | 25 ++++++++-----------------
 1 file changed, 8 insertions(+), 17 deletions(-)

diff --git a/lisp/comint.el b/lisp/comint.el
index 36fcbb9465..3476fd146c 100644
--- a/lisp/comint.el
+++ b/lisp/comint.el
@@ -991,23 +991,14 @@ comint-read-input-ring
                (while (and (< count comint-input-ring-size)
                            (re-search-backward ring-separator nil t)
                            (setq end (match-beginning 0)))
-                 (setq start
-                       (if (re-search-backward ring-separator nil t)
-                           (progn
-                             (goto-char (match-end 0))
-                             (when (and ring-file-prefix
-                                        (looking-at
-                                         ring-file-prefix))
-                               ;; Skip zsh extended_history stamps
-                               (goto-char (match-end 0)))
-                             (match-end 0))
-                         (progn
-                           (goto-char (point-min))
-                           (when (and ring-file-prefix
-                                      (looking-at
-                                       ring-file-prefix))
-                             (goto-char (match-end 0)))
-                           (point))))
+                 (goto-char (if (re-search-backward ring-separator nil t)
+                                (match-end 0)
+                              (point-min)))
+                 (when (and ring-file-prefix
+                            (looking-at ring-file-prefix))
+                   ;; Skip zsh extended_history stamps
+                   (goto-char (match-end 0)))
+                 (setq start (point))
                  (setq history (buffer-substring start end))
                  (goto-char start)
                  (when (and (not (string-match history-ignore history))
-- 
2.29.2