Subject: Re: bug#21634: text-scale-adjust suggestion

From: Juri Linkov <[email-masked]>
Date: Mon, 04 Jul 2022 20:45:47 +0300

>> OTOH, there are messages like "Use \\`+',\\`-',\\`0' for further adjustment"
>> have not only a prefix, but also a suffix.
>
> Perhaps the MESSAGE should be a format-spec string?  So the caller could
> say "Use %k for further adjustment" and have the keymap description
> inserted at the %k.  That should be plenty flexible.

format-spec is a nice helper.  Here are the changes after
the value of %k is automatically generated from the keys:

  emoji-zoom-increase:
  OLD: Zoom with + and -
  NEW: Zoom with +, -

  indent-rigidly:
  OLD: Indent region with <left>, <right>, S-<left>, or S-<right>.
  NEW: Indent region with TAB, <left>, <right>, S-<left>, S-<right>

  text-scale-adjust:
  OLD: Use +,-,0 for further adjustment
  NEW: Use +, =, -, 0, C-+, C-=, C--, C-0 for further adjustment

  global-text-scale-adjust:
  OLD: Use +,-,0 for further adjustment
  NEW: Use +, =, -, 0, ESC for further adjustment

ESC is because map-keymap handles only top-level keys
but C-M-+ is [ESC C-+].

diff --git a/lisp/international/emoji.el b/lisp/international/emoji.el
index 27b725b0aa..a129656d2b 100644
--- a/lisp/international/emoji.el
+++ b/lisp/international/emoji.el
@@ -709,10 +709,7 @@ emoji-zoom-increase
   "Increase the size of the character under point.
 FACTOR is the multiplication factor for the size."
   (interactive)
-  (message
-   (substitute-command-keys
-    "Zoom with \\<emoji-zoom-map>\\[emoji-zoom-increase] and \\[emoji-zoom-decrease]"))
-  (set-transient-map emoji-zoom-map t)
+  (set-transient-map emoji-zoom-map t nil "Zoom with %k")
   (let* ((factor (or factor 1.1))
          (old (get-text-property (point) 'face))
          (height (or (and (consp old)
diff --git a/lisp/face-remap.el b/lisp/face-remap.el
index 467ccbc299..fd49c81ab3 100644
--- a/lisp/face-remap.el
+++ b/lisp/face-remap.el
@@ -408,20 +408,15 @@ text-scale-adjust
               (?0 0)
               (_ inc))))
       (text-scale-increase step)
-      ;; (unless (zerop step)
-      (message (substitute-command-keys
-                "Use \\`+',\\`-',\\`0' for further adjustment"))
       (set-transient-map
        (let ((map (make-sparse-keymap)))
          (dolist (mods '(() (control)))
-           (dolist (key '(?- ?+ ?= ?0)) ;; = is often unshifted +.
+           (dolist (key '(?+ ?= ?- ?0)) ;; = is often unshifted +.
              (define-key map (vector (append mods (list key)))
                (lambda () (interactive) (text-scale-adjust (abs inc))))))
          map)
-       nil
-       ;; Clear the prompt after exiting.
-       (lambda ()
-         (message ""))))))
+       nil nil
+       "Use %k for further adjustment"))))
 
 (defvar-local text-scale--pinch-start-scale 0
   "The text scale at the start of a pinch sequence.")
@@ -515,15 +510,15 @@ global-text-scale-adjust
                (not global-text-scale-adjust-resizes-frames)))
           (set-face-attribute 'default nil :height new)))
       (when (characterp key)
-        (message (substitute-command-keys
-                  "Use \\`+',\\`-',\\`0' for further adjustment"))
         (set-transient-map
          (let ((map (make-sparse-keymap)))
            (dolist (mod '(() (control meta)))
              (dolist (key '(?+ ?= ?- ?0))
                (define-key map (vector (append mod (list key)))
                  'global-text-scale-adjust)))
-           map))))))
+           map)
+       nil nil
+       "Use %k for further adjustment")))))
 
 
 ;; ----------------------------------------------------------------
diff --git a/lisp/indent.el b/lisp/indent.el
index d6dee94016..f52b729051 100644
--- a/lisp/indent.el
+++ b/lisp/indent.el
@@ -270,11 +270,8 @@ indent-rigidly
 indentation by specifying a large negative ARG."
   (interactive "r\nP\np")
   (if (and (not arg) interactive)
-      (progn
-        (message
-	 (substitute-command-keys
-	  "Indent region with \\<indent-rigidly-map>\\[indent-rigidly-left], \\[indent-rigidly-right], \\[indent-rigidly-left-to-tab-stop], or \\[indent-rigidly-right-to-tab-stop]."))
-        (set-transient-map indent-rigidly-map t #'deactivate-mark))
+      (set-transient-map indent-rigidly-map t #'deactivate-mark
+                         "Indent region with %k")
     (save-excursion
       (goto-char end)
       (setq end (point-marker))
diff --git a/lisp/subr.el b/lisp/subr.el
index 2f9d37ffd6..efb920ec5a 100644
--- a/lisp/subr.el
+++ b/lisp/subr.el
@@ -6013,7 +6013,15 @@ internal-pop-keymap
 (define-obsolete-function-alias
   'set-temporary-overlay-map #'set-transient-map "24.4")
 
-(defun set-transient-map (map &optional keep-pred on-exit)
+(defvar set-transient-map-timeout 3
+  "Break the repetition chain of keys after specified timeout.
+When a number, exit the previous `set-transient-map' after idle time
+of the specified number of seconds.")
+
+(defvar set-transient-map-timer nil
+  "Timer to exit `set-transient-map' after `set-transient-map-timeout'.")
+
+(defun set-transient-map (map &optional keep-pred on-exit message timeout)
   "Set MAP as a temporary keymap taking precedence over other keymaps.
 Normally, MAP is used only once, to look up the very next key.
 However, if the optional argument KEEP-PRED is t, MAP stays
@@ -6030,18 +6038,34 @@ set-transient-map
 
 This returns an \"exit function\", which can be called with no argument
 to deactivate this transient map, regardless of KEEP-PRED."
-  (let* ((clearfun (make-symbol "clear-transient-map"))
+  (let* ((timeout (or set-transient-map-timeout timeout))
+         (message
+          (when message
+            (let (keys)
+              (map-keymap (lambda (key cmd) (and cmd (push key keys))) map)
+              (format-spec (if (stringp message) message
+                             "Repeat with %k")
+                           `((?k . ,(mapconcat
+                                     (lambda (key)
+                                       (substitute-command-keys
+                                        (format "\\`%s'"
+                                                (key-description (vector key)))))
+                                     keys ", ")))))))
+         (clearfun (make-symbol "clear-transient-map"))
          (exitfun
           (lambda ()
             (internal-pop-keymap map 'overriding-terminal-local-map)
             (remove-hook 'pre-command-hook clearfun)
+            ;; Clear the prompt after exiting.
+            (when message (message ""))
+            (when set-transient-map-timer (cancel-timer set-transient-map-timer))
             (when on-exit (funcall on-exit)))))
     ;; Don't use letrec, because equal (in add/remove-hook) could get trapped
     ;; in a cycle. (bug#46326)
     (fset clearfun
           (lambda ()
             (with-demoted-errors "set-transient-map PCH: %S"
-              (unless (cond
+              (if (cond
                        ((null keep-pred) nil)
                        ((and (not (eq map (cadr overriding-terminal-local-map)))
                              (memq map (cddr overriding-terminal-local-map)))
@@ -6066,9 +6090,14 @@ set-transient-map
                           ;; nil and so is `mc`.
                           (and mc (eq this-command mc))))
                        (t (funcall keep-pred)))
+                  (when message (message "%s" message))
                 (funcall exitfun)))))
     (add-hook 'pre-command-hook clearfun)
     (internal-push-keymap map 'overriding-terminal-local-map)
+    (when timeout
+      (when set-transient-map-timer (cancel-timer set-transient-map-timer))
+      (setq set-transient-map-timer (run-with-idle-timer timeout nil exitfun)))
+    (when message (message "%s" message))
     exitfun))
 
 ;;;; Progress reporters.