Subject: bug#38282: 26.3; goto-line should not share input history with other commands

From: Federico Tedin <[email-masked]>
Date: Thu, 21 Nov 2019 23:06:14 +0100

> It seems like `read-from-minibuffer' doesn't like having its HIST
> parameter be buffer-local (but I may have misunderstood the problem).
> I'm going to look into it more on depth these days.

Here's my first attempt at implementing this - I managed to get around
the `read-from-minibuffer' problem with some help from the Emacs
StackOverflow (but created a separate bug report for it:
https://debbugs.gnu.org/cgi/bugreport.cgi?bug=38317). I'm attaching a
small patch.

>From 3994fa53d0cf210e7f109dc2059ee78a96215547 Mon Sep 17 00:00:00 2001
From: Federico Tedin <[email-masked]>
Date: Thu, 21 Nov 2019 23:01:23 +0100
Subject: [PATCH 1/1] Make input history buffer-local for 'goto-line'

* lisp/simple.el (goto-line-history): New buffer-local variable which
holds the input history for 'goto-line'.
(goto-line): Use the new variable as input history (Bug#38282).
* etc/NEWS: Announce changes.
---
 etc/NEWS       |  5 +++++
 lisp/simple.el | 19 +++++++++++++++++--
 2 files changed, 22 insertions(+), 2 deletions(-)

diff --git a/etc/NEWS b/etc/NEWS
index 7a51106add..0bbb40dfa2 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -589,6 +589,11 @@ If the region is active, the command joins all the lines in the
 region.  When there's no active region, the command works on the
 current and the previous or the next line, as before.
 
+---
+** 'goto-line' now keeps a separate input history for each buffer.
+Additionally, the line number input history will be kept separate from
+the input history for other commands.
+
 
 * Changes in Specialized Modes and Packages in Emacs 27.1
 
diff --git a/lisp/simple.el b/lisp/simple.el
index c61ccd511c..dd8e8a291a 100644
--- a/lisp/simple.el
+++ b/lisp/simple.el
@@ -1212,6 +1212,11 @@ mark-whole-buffer
 
 ;; Counting lines, one way or another.
 
+(defvar goto-line-history nil
+  "Input history for `goto-line'.")
+
+(make-variable-buffer-local 'goto-line-history)
+
 (defun goto-line (line &optional buffer)
   "Go to LINE, counting from line 1 at beginning of buffer.
 If called interactively, a numeric prefix argument specifies
@@ -1253,7 +1258,14 @@ goto-line
 	    (buffer-prompt
 	     (if buffer
 		 (concat " in " (buffer-name buffer))
-	       "")))
+	       ""))
+            ;; It would be better to use `goto-line-history' as a HIST
+            ;; parameter to `read-from-minibuffer' (through
+            ;; `read-number'), but using buffer-local variables
+            ;; doesn't work for that purpose. (Bug#38317)
+            (minibuffer-history
+             (buffer-local-value 'goto-line-history (or buffer
+                                                        (current-buffer)))))
        ;; Read the argument, offering that number (if any) as default.
        (list (read-number (format "Goto line%s: " buffer-prompt)
                           (list default (line-number-at-pos)))
@@ -1271,7 +1283,10 @@ goto-line
     (goto-char (point-min))
     (if (eq selective-display t)
 	(re-search-forward "[\n\C-m]" nil 'end (1- line))
-      (forward-line (1- line)))))
+      (forward-line (1- line))))
+  ;; Save the line number in the input history list.
+  (with-current-buffer (or buffer (current-buffer))
+    (add-to-history 'goto-line-history (int-to-string line))))
 
 (defun count-words-region (start end &optional arg)
   "Count the number of words in the region.
-- 
2.17.1