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

From: Juri Linkov <[email-masked]>
Date: Sun, 03 Jul 2022 19:36:04 +0300

> Juri Linkov <[email-masked]> writes:
>
>> Actually, I can imagine when someone might want to keep a transient map
>> active with KEEP-PRED, but still deactivate it after a timeout.  So
>> a separate TIMEOUT looks cleaner.
>
> Yup, makes sense.
>
>> Anyway, repeat-mode has separate options for repeat-exit-timeout
>> and constructs a message, so I could copy the same code from repeat-mode
>> to set-transient-map.
>
> Sounds good.

Maybe something like this.  I'm still unsure about providing a prefix
message without keys, because when the message arg is a string
then better to display it as is.  Otherwise when it is `t', then
the message will be constructed to "Repeat with <keys...>".
I.e. the remaining question is that you already raised whether
typically all keys should be mentioned in the message.

OTOH, there are messages like "Use \\`+',\\`-',\\`0' for further adjustment"
have not only a prefix, but also a suffix.

diff --git a/lisp/face-remap.el b/lisp/face-remap.el
index 7037bc58cb..951285fbf2 100644
--- a/lisp/face-remap.el
+++ b/lisp/face-remap.el
@@ -408,9 +408,6 @@ 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)))
@@ -418,10 +415,9 @@ text-scale-adjust
              (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
+       (substitute-command-keys
+        "Use \\`+',\\`-',\\`0' for further adjustment")))))
 
 (defvar-local text-scale--pinch-start-scale 0
   "The text scale at the start of a pinch sequence.")
@@ -515,15 +511,16 @@ 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
+       (substitute-command-keys
+        "Use \\`+',\\`-',\\`0' for further adjustment"))))))
 
 
 ;; ----------------------------------------------------------------
diff --git a/lisp/indent.el b/lisp/indent.el
index d6dee94016..6eaf9f04ae 100644
--- a/lisp/indent.el
+++ b/lisp/indent.el
@@ -270,11 +270,10 @@ 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
+       (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]."))
     (save-excursion
       (goto-char end)
       (setq end (point-marker))
diff --git a/lisp/subr.el b/lisp/subr.el
index 4e4eac32d9..338248bbae 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,33 @@ 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
+            (if (stringp message) message
+              (let (keys)
+                (map-keymap (lambda (key cmd) (and cmd (push key keys))) map)
+                (format-message "Repeat with %s"
+                                (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 +6089,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.