From a4d4eaf9fcb83b98277959083028aa2a20489a1c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Mattias=20Engdeg=C3=A5rd?= <mattiase@acm.org>
Date: Mon, 24 Jan 2022 16:49:08 +0100
Subject: [PATCH] Cleaner `what-cursor-position` format

Add a new `C-x =` output format, under the control of the new
`what-cursor-format` user option, that uses more modern formatting
and omits noise syntax and rarely used information.  It always
includes the character name.

* lisp/simple.el (what-cursor-format): New.
(what-cursor-show-names): Explain relation to `what-cursor-format`.
(what-cursor-position--default,
what-cursor-position--traditional): New functions.
(what-cursor-position): Reduce to calling the right back-end function.
* etc/NEWS: Announce.
---
 etc/NEWS       |  6 ++++
 lisp/simple.el | 78 ++++++++++++++++++++++++++++++++++++++------------
 2 files changed, 65 insertions(+), 19 deletions(-)

diff --git a/etc/NEWS b/etc/NEWS
index abef1019ac..68fb9110f1 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -183,6 +183,12 @@ wheel reports.  Unlike 'pixel-scroll-mode', this mode scrolls the
 display pixel-by-pixel, as opposed to only animating line-by-line
 scrolls.
 
+** New user option 'what-cursor-format'
+It governs the output format of 'what-cursor-position', normally
+bound to 'C-x ='.  When 'traditional', the output is identical
+to that of Emacs 28; when 'default', a simpler, briefer format
+that always includes the character name is used.
+
 ** Terminal Emacs
 
 ---
diff --git a/lisp/simple.el b/lisp/simple.el
index 801a3c992c..2cc90426e2 100644
--- a/lisp/simple.el
+++ b/lisp/simple.el
@@ -1564,27 +1564,57 @@ count-lines
                  (1- (line-number-at-pos))
                (line-number-at-pos)))))))
 
+(defcustom what-cursor-format 'default
+  "What to show in the echo area in `what-cursor-position'.
+The `default' format gives the hex code and name of the character
+after point, the cursor position, buffer size, and column number.
+
+The `traditional' format also displays the code in decimal and octal,
+and only includes the name if `what-cursor-show-name' is non-nil.
+For a non-ASCII multibyte character, it also gives the encoding in the
+buffer's selected coding system if the coding system encodes the
+character safely.  If the character is encoded into one byte, that
+code is shown in hex.  If the character is encoded into more than one
+byte, just \"...\" is shown."
+  :type '(choice (const :tag "Default" default)
+                 (const :tag "Traditional" traditional))
+  :version "29.1"
+  :group 'editing-basics)
+
 (defcustom what-cursor-show-names nil
-  "Whether to show character names in `what-cursor-position'."
+  "Whether to show character names in `what-cursor-position'.
+It only applies when `what-cursor-format' is `traditional'."
   :type 'boolean
   :version "27.1"
   :group 'editing-basics)
 
-(defun what-cursor-position (&optional detail)
-  "Print info on cursor position (on screen and within buffer).
-Also describe the character after point, and give its character
-code in octal, decimal and hex.  If `what-cursor-show-names' is
-non-nil, additionally show the name of the character.
-
-For a non-ASCII multibyte character, also give its encoding in the
-buffer's selected coding system if the coding system encodes the
-character safely.  If the character is encoded into one byte, that
-code is shown in hex.  If the character is encoded into more than one
-byte, just \"...\" is shown.
-
-In addition, with prefix argument, show details about that character
-in *Help* buffer.  See also the command `describe-char'."
-  (interactive "P")
+(defun what-cursor-position--default ()
+  "Default echo area output of `what-cursor-position'."
+  (let* ((char (char-after))
+         (char-info
+          (cond
+           ((null char) "")
+           ((<= #x3fff80 char #x3fffff)
+            (format "#x%02X (raw byte) " (logand char #xff)))
+           (t
+            (format "U+%04X %s, "
+                    char
+                    (or (get-char-code-property char 'name)
+                        (get-char-code-property char 'old-name)
+                        "(undefined)")))))
+         (beg (point-min))
+         (end (point-max))
+         (narrow (if (and (= beg 1) (= (1- end) (buffer-size)))
+                     ""
+                   (format " (narrowed to %d-%d)" beg end))))
+    (message "%spoint=%d/%d%s col=%d"
+             char-info
+             (point) (buffer-size)
+             narrow
+             (current-column))))
+
+(defun what-cursor-position--traditional ()
+  "Traditional echo area output of `what-cursor-position'."
   (let* ((char (following-char))
          (char-name (and what-cursor-show-names
                          (or (get-char-code-property char 'name)
@@ -1671,9 +1701,6 @@ what-cursor-position
 				  "..."
 				(encoded-string-description encoded coding)))
 		    (format "(%d, #o%o, #x%x%s)" char char char char-name-fmt)))))
-	(if detail
-	    ;; We show the detailed information about CHAR.
-	    (describe-char (point)))
 	(if (or (/= beg 1) (/= end (1+ total)))
 	    (message "Char: %s%s %s point=%d of %d (%d%%) <%d-%d> column=%d%s"
 		     (if (< char 256)
@@ -1688,6 +1715,19 @@ what-cursor-position
 			 (buffer-substring-no-properties (point) (1+ (point))))
 		     (single-key-description char))
 		   bidi-fixer encoding-msg pos total percent col hscroll))))))
+
+(defun what-cursor-position (&optional detail)
+  "Print info on cursor position (on screen and within buffer).
+Also describe the character after point according to `what-cursor-format'.
+
+In addition, with prefix argument, show details about that character
+in *Help* buffer.  See also the command `describe-char'."
+  (interactive "P")
+  (when detail
+    (describe-char (point)))
+  (cond
+   ((eq what-cursor-format 'default)     (what-cursor-position--default))
+   ((eq what-cursor-format 'traditional) (what-cursor-position--traditional))))
 
 ;; Initialize read-expression-map.  It is defined at C level.
 (defvar read-expression-map
-- 
2.32.0 (Apple Git-132)

