Subject: Re: bug#45199: 28.0.50; [PATCH] Make goto-char offer the number at point as default

From: Daniel Martín <[email-masked]>
Date: Sun, 13 Dec 2020 19:01:24 +0100

Lars Ingebrigtsen <[email-masked]> writes:
>
> Yeah, I'd still rather have a helper function -- putting several lines
> of Lisp code in a string in C isn't optimal.

OK, done.

>From 62dffb016c1b95923e654f516f52780aa2eb966d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Daniel=20Mart=C3=ADn?= <[email-masked]>
Date: Sat, 12 Dec 2020 17:10:05 +0100
Subject: [PATCH] Make goto-char offer the number at point as default

* lisp/subr.el (read-natnum-interactive): New function to read natural
numbers for interactive functions.
* src/editfns.c (Fgoto_char): Call read-natnum-interactive from the
interactive definition of goto-char to offer the number at point as
default.  Also expand the docstring to document this new interactive
behavior.
* doc/emacs/basic.texi (Moving Point): Expand the Emacs manual to
document this new behavior.
* etc/NEWS: And announce it.
---
 doc/emacs/basic.texi | 5 ++++-
 etc/NEWS             | 4 ++++
 lisp/subr.el         | 9 +++++++++
 src/editfns.c        | 9 +++++++--
 4 files changed, 24 insertions(+), 3 deletions(-)

diff --git a/doc/emacs/basic.texi b/doc/emacs/basic.texi
index cd1ffbebd7..77c8054746 100644
--- a/doc/emacs/basic.texi
+++ b/doc/emacs/basic.texi
@@ -310,7 +310,10 @@ Moving Point
 @kindex M-g c
 @findex goto-char
 Read a number @var{n} and move point to buffer position @var{n}.
-Position 1 is the beginning of the buffer.
+Position 1 is the beginning of the buffer.  If point is on or just
+after a number in the buffer, that is the default for @var{n}.  Just
+type @key{RET} in the minibuffer to use it.  You can also specify
+@var{n} by giving @kbd{M-g c} a numeric prefix argument.
 
 @item M-g M-g
 @itemx M-g g
diff --git a/etc/NEWS b/etc/NEWS
index 514209516d..13ff0bb171 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -257,6 +257,10 @@ When 'widen-automatically' is non-nil, 'goto-line' widens the narrowed
 buffer to be able to move point to the inaccessible portion.
 'goto-line-relative' is bound to 'C-x n g'.
 
++++
+** When called interactively, 'goto-char' now offers the number at
+   point as default.
+
 +++
 ** When 'suggest-key-bindings' is non-nil, the completion list of 'M-x'
 shows equivalent key bindings for all commands that have them.
diff --git a/lisp/subr.el b/lisp/subr.el
index ed235ee1f7..5e4f7dec7d 100644
--- a/lisp/subr.el
+++ b/lisp/subr.el
@@ -2820,6 +2820,15 @@ read-char-from-minibuffer
     (message "%s%s" prompt (char-to-string char))
     char))
 
+(defun read-natnum-interactive (prompt)
+  "Get a natural number argument, optionally prompting with PROMPT.
+If there is a natural number at point, use it as default."
+  (if (and current-prefix-arg (not (consp current-prefix-arg)))
+      (list (prefix-numeric-value current-prefix-arg))
+    (let* ((number (number-at-point))
+           (default (and (natnump number) number)))
+      (list (read-number prompt default)))))
+
 
 ;; Behind display-popup-menus-p test.
 (declare-function x-popup-dialog "menu.c" (position contents &optional header))
diff --git a/src/editfns.c b/src/editfns.c
index 4104edd77f..4723660b43 100644
--- a/src/editfns.c
+++ b/src/editfns.c
@@ -188,11 +188,16 @@ DEFUN ("point-marker", Fpoint_marker, Spoint_marker, 0, 0, 0,
   return build_marker (current_buffer, PT, PT_BYTE);
 }
 
-DEFUN ("goto-char", Fgoto_char, Sgoto_char, 1, 1, "NGoto char: ",
+DEFUN ("goto-char", Fgoto_char, Sgoto_char, 1, 1,
+         "(read-natnum-interactive \"Go to char: \")",
        doc: /* Set point to POSITION, a number or marker.
 Beginning of buffer is position (point-min), end is (point-max).
 
-The return value is POSITION.  */)
+The return value is POSITION.
+
+If called interactively, a numeric prefix argument specifies
+POSITION; without a numeric prefix argument, read POSITION from the
+minibuffer.  */)
   (register Lisp_Object position)
 {
   if (MARKERP (position))
-- 
2.28.0