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

From: Daniel Martín <[email-masked]>
Date: Sat, 12 Dec 2020 17:40:23 +0100

My use case (there may be many) comes from working with PDFs at a low
level.  If you open a PDF file in fundamental-mode, towards the end of
the file you'll see text that resembles the following:

xref
0 26
0000000000 65535 f 
0000006459 00000 n 

This is a table where the first column contains file offsets that point
to where the PDF objects are defined.  I wanted to place the point on
the "0000006459", do M-g c, and easily go to file offset 6459 to see the
definition of the PDF object number 1 ("1 0 obj").  goto-char is useful
here because PDF files are open as unibyte in Emacs.

If you also find the behavior generally useful, feel free to install it
for Emacs 28.

Thanks.

>From 92e0a5c1b7482c4399f86eee9d4d41732457d081 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

* src/editfns.c (Fgoto_char): Expand the interactive definition of
goto-char to offer the number at point as default.  Note that only
numbers that make sense as character positions will be offered.  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 ++++
 src/editfns.c        | 20 ++++++++++++++++++--
 3 files changed, 26 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/src/editfns.c b/src/editfns.c
index 4104edd77f..cd6ed90ee5 100644
--- a/src/editfns.c
+++ b/src/editfns.c
@@ -188,11 +188,27 @@ 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,
+         "(if (and current-prefix-arg (not (consp current-prefix-arg)))\
+              (list (prefix-numeric-value current-prefix-arg))\
+            (let* ((default\
+	             (save-excursion\
+	               (skip-chars-backward \"0-9\")\
+	               (if (looking-at-p \"[0-9]\")\
+	                   (string-to-number\
+	                   (buffer-substring-no-properties\
+	                   (point)\
+	                   (progn (skip-chars-forward \"0-9\")\
+		                  (point))))))))\
+              (list (read-number \"Goto char: \" default))))",
        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