unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Federico Tedin <federicotedin@gmail.com>
To: 38317@debbugs.gnu.org
Cc: larsi@gnus.org
Subject: bug#38317: Buffer-local variables don't work as history for read-from-minibuffer
Date: Thu, 28 Nov 2019 13:05:40 +0100	[thread overview]
Message-ID: <874kyow6i3.fsf@gmail.com> (raw)
In-Reply-To: <87d0ddvxnk.fsf@gmail.com> (Federico Tedin's message of "Wed, 27 Nov 2019 22:04:31 +0100")

[-- Attachment #1: Type: text/plain, Size: 97 bytes --]

I'm now attaching a new patch with feedback corrections from everyone in
the thread (threads?).


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: patch --]
[-- Type: text/x-diff, Size: 7252 bytes --]

From 481f85d70463207c5ca5c43c287f1edcc6ed288e Mon Sep 17 00:00:00 2001
From: Federico Tedin <federicotedin@gmail.com>
Date: Thu, 28 Nov 2019 12:59:52 +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).
(read-from-minibuffer): Extend documentation to mention that the
result of using the command will be added to the history list by
default.
* doc/lispref/minibuf.texi (Minibuffer History): Mention the
possibility of using a buffer-local variable as history.
* etc/NEWS: Announce changes.
---
 doc/lispref/minibuf.texi |  3 ++-
 etc/NEWS                 |  6 ++++++
 lisp/simple.el           | 18 +++++++++++++-----
 src/minibuf.c            | 20 ++++++++++++++++++--
 4 files changed, 39 insertions(+), 8 deletions(-)

diff --git a/doc/lispref/minibuf.texi b/doc/lispref/minibuf.texi
index 4a218fe737..dde30ce67f 100644
--- a/doc/lispref/minibuf.texi
+++ b/doc/lispref/minibuf.texi
@@ -547,7 +547,8 @@ Minibuffer History
   If you don't specify @var{history}, then the default history list
 @code{minibuffer-history} is used.  For other standard history lists,
 see below.  You can also create your own history list variable; just
-initialize it to @code{nil} before the first use.
+initialize it to @code{nil} before the first use.  If the variable is
+buffer local, then each buffer will have its own input history list.
 
   Both @code{read-from-minibuffer} and @code{completing-read} add new
 elements to the history list automatically, and provide commands to
diff --git a/etc/NEWS b/etc/NEWS
index cb73e46358..bc088129f8 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -522,6 +522,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.
+
 \f
 * Editing Changes in Emacs 27.1
 
diff --git a/lisp/simple.el b/lisp/simple.el
index 2aac557154..ef36ff9f8a 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-value))
 	  (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-value ()
+  "Return the value of the minibuffer input history list.
+If `minibuffer-history-variable' points to a buffer-local variable and
+the minibuffer is active, return the buffer-local value for the buffer
+that was current when the minibuffer was activated."
+  (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-value))
+                    (length (minibuffer-history-value))
                   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-value)))))
     (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-value)))
     (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..bdae01dbc5 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)
@@ -879,6 +892,9 @@ Fifth arg HIST, if non-nil, specifies a history list and optionally
   starting from 1 at the beginning of the list.  If HIST is the symbol
   `t', history is not recorded.
 
+  If `history-add-new-input' is non-nil (the default), the result will
+  be added to the history list using `add-to-history'.
+
 Sixth arg DEFAULT-VALUE, if non-nil, should be a string, which is used
   as the default to `read' if READ is non-nil and the user enters
   empty input.  But if READ is nil, this function does _not_ return
-- 
2.17.1


  reply	other threads:[~2019-11-28 12:05 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-11-21 21:30 bug#38317: Buffer-local variables don't work as history for read-from-minibuffer Federico Tedin
2019-11-22 13:33 ` Lars Ingebrigtsen
2019-11-22 15:23   ` Michael Heerdegen
2019-11-22 16:24   ` martin rudalics
2019-11-23 11:54     ` Lars Ingebrigtsen
2019-11-23 20:36       ` Federico Tedin
2019-11-23 23:12         ` Federico Tedin
2019-11-26 21:54           ` Federico Tedin
2019-11-27  8:14             ` Robert Pluim
2019-11-27 15:24               ` Drew Adams
2019-11-27 15:28                 ` Robert Pluim
2019-11-27 11:53             ` Lars Ingebrigtsen
2019-11-27 18:50               ` Federico Tedin
2019-11-27 19:25                 ` Eli Zaretskii
2019-11-27 21:04                   ` Federico Tedin
2019-11-28 12:05                     ` Federico Tedin [this message]
2019-12-05  9:31                       ` Lars Ingebrigtsen
2019-11-29  5:22                     ` Richard Stallman
2019-11-29 10:21                       ` Eli Zaretskii
2019-11-29 17:02                         ` Federico Tedin
2019-12-01  5:59                       ` Richard Stallman
2019-11-27 21:19             ` Juri Linkov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=874kyow6i3.fsf@gmail.com \
    --to=federicotedin@gmail.com \
    --cc=38317@debbugs.gnu.org \
    --cc=larsi@gnus.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).