Subject: Re: bug#38317: Buffer-local variables don't work as history for read-from-minibuffer

From: Federico Tedin <[email-masked]>
Date: Tue, 26 Nov 2019 22:54:29 +0100

> I'm attaching a draft patch in case anyone wants to provide some
> feedback. Thanks!

I'm now attaching a final version of my patch.

I've tested the following commands with it, using `read-from-minibuffer'
with a buffer-local HIST:

- previous-history-element (M-p)
- next-history-element (M-n)
- previous-matching-history-element (M-r)
- next-matching-history-element (M-s)
- isearch-forward (C-s)
- isearch-backward (C-r)

They've all worked correctly and used only the buffer's local history
value.

Thanks!

>From 496fbc75e02e016d50d3e1a21154e4da844a1140 Mon Sep 17 00:00:00 2001
From: Federico Tedin <[email-masked]>
Date: Tue, 26 Nov 2019 22:39:34 +0100
Subject: [PATCH 1/1] Make HIST arg of read-from-minibuffer work with
 buffer-local vars

* lisp/simple.el (minibuffer-history-values): New function, should be
used to access the minibuffer input history variable when the
minibuffer might be active. If the variable is buffer-local, the
previous buffer's value will be used.
(goto-history-element): Use the new function to access the minibuffer
history.
(minibuffer-history-isearch-wrap): Use the new function to access the
minibuffer history.
* src/minibuf.c (read_minibuf): Switch to previous buffer temporarily
before updating history list (Bug#38317).
* etc/NEWS: Announce changes.
---
 etc/NEWS       |  6 ++++++
 lisp/simple.el | 18 +++++++++++++-----
 src/minibuf.c  | 17 +++++++++++++++--
 3 files changed, 34 insertions(+), 7 deletions(-)

diff --git a/etc/NEWS b/etc/NEWS
index d3331daf17..9dbf36ae40 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -518,6 +518,12 @@ Note that this key binding will not work on MS-Windows systems if
 key binding with an upper case letter - if you can type it, you can
 bind it.
 
+---
+** 'read-from-minibuffer' now works with buffer-local history variables
+The HIST argument of 'read-from-minibuffer' now works correctly with
+buffer-local variables. This means that different buffers can have
+their own separated input history list if desired.
+
 
 * Editing Changes in Emacs 27.1
 
diff --git a/lisp/simple.el b/lisp/simple.el
index 2aac557154..0b1d7b73ab 100644
--- a/lisp/simple.el
+++ b/lisp/simple.el
@@ -2041,7 +2041,7 @@ previous-matching-history-element
 	     (null minibuffer-text-before-history))
 	(setq minibuffer-text-before-history
 	      (minibuffer-contents-no-properties)))
-    (let ((history (symbol-value minibuffer-history-variable))
+    (let ((history (minibuffer-history-values))
 	  (case-fold-search
 	   (if (isearch-no-upper-case-p regexp t) ; assume isearch.el is dumped
 	       ;; On some systems, ignore case for file names.
@@ -2141,6 +2141,14 @@ minibuffer-default-add-completions
 	(append def all)
       (cons def (delete def all)))))
 
+(defun minibuffer-history-values ()
+  "Return the minibuffer input history values.
+If `minibuffer-history-variable' points to a buffer-local variable and
+the minibuffer is active, return the buffer-local value for the buffer
+selected in the window returned by `minibuffer-selected-window'."
+  (buffer-local-value minibuffer-history-variable
+                      (window-buffer (minibuffer-selected-window))))
+
 (defun goto-history-element (nabs)
   "Puts element of the minibuffer history in the minibuffer.
 The argument NABS specifies the absolute history position in
@@ -2169,8 +2177,8 @@ goto-history-element
 	(user-error (if minibuffer-default
                         "End of defaults; no next item"
                       "End of history; no default available")))
-    (if (> nabs (if (listp (symbol-value minibuffer-history-variable))
-                    (length (symbol-value minibuffer-history-variable))
+    (if (> nabs (if (listp (minibuffer-history-values))
+                    (length (minibuffer-history-values))
                   0))
 	(user-error "Beginning of history; no preceding item"))
     (unless (memq last-command '(next-history-element
@@ -2192,7 +2200,7 @@ goto-history-element
 	   (setq minibuffer-returned-to-present t)
 	   (setq minibuffer-text-before-history nil))
 	  (t (setq elt (nth (1- minibuffer-history-position)
-			    (symbol-value minibuffer-history-variable)))))
+			    (minibuffer-history-values)))))
     (insert
      (if (and (eq minibuffer-history-sexp-flag (minibuffer-depth))
 	      (not minibuffer-returned-to-present))
@@ -2445,7 +2453,7 @@ minibuffer-history-isearch-wrap
   ;; beginning/end of the history, wrap the search to the first/last
   ;; minibuffer history element.
   (if isearch-forward
-      (goto-history-element (length (symbol-value minibuffer-history-variable)))
+      (goto-history-element (length (minibuffer-history-values)))
     (goto-history-element 0))
   (setq isearch-success t)
   (goto-char (if isearch-forward (minibuffer-prompt-end) (point-max))))
diff --git a/src/minibuf.c b/src/minibuf.c
index 1e87c5044a..2fbfb31b99 100644
--- a/src/minibuf.c
+++ b/src/minibuf.c
@@ -353,7 +353,7 @@ read_minibuf (Lisp_Object map, Lisp_Object initial, Lisp_Object prompt,
 	      Lisp_Object histvar, Lisp_Object histpos, Lisp_Object defalt,
 	      bool allow_props, bool inherit_input_method)
 {
-  Lisp_Object val;
+  Lisp_Object val, previous_buffer = Fcurrent_buffer ();
   ptrdiff_t count = SPECPDL_INDEX ();
   Lisp_Object mini_frame, ambient_dir, minibuffer, input_method;
   Lisp_Object enable_multibyte;
@@ -698,7 +698,20 @@ read_minibuf (Lisp_Object map, Lisp_Object initial, Lisp_Object prompt,
 
   /* Add the value to the appropriate history list, if any.  */
   if (! (NILP (Vhistory_add_new_input) || NILP (histstring)))
-    call2 (intern ("add-to-history"), Vminibuffer_history_variable, histstring);
+    {
+      ptrdiff_t count2 = SPECPDL_INDEX ();
+
+      /* If possible, switch back to the previous buffer first, in
+	 case the history variable is buffer-local.  */
+      if (BUFFER_LIVE_P (XBUFFER (previous_buffer)))
+	{
+	  record_unwind_current_buffer ();
+	  Fset_buffer (previous_buffer);
+	}
+
+      call2 (intern ("add-to-history"), Vminibuffer_history_variable, histstring);
+      unbind_to (count2, Qnil);
+    }
 
   /* If Lisp form desired instead of string, parse it.  */
   if (expflag)
-- 
2.17.1