Subject: Re: bug#63648: 29.0.90; project.el: with switch-use-entire-map, switch-project errors on non-project commands

From: Dmitry Gutov <[email-masked]>
Date: Fri, 20 Oct 2023 22:25:06 +0300

On 20/10/2023 09:44, Juri Linkov wrote:
>> 'C-x d' will result in a warning echo, though, instead of just using
>> 'd'. If you really prefer that, try experimenting with the below addition,
>> but I'm wary of edge cases, since we want to keep supporting sub-maps like
>> in bug#47620. It also might feel a little too "magical" now that there are
>> more details printed (it also doesn't handle extremes like 'C-c C-x C-c'
>> still).
>>
>> @@ -2003,7 +2003,9 @@ project--switch-project-command
>>                                          (help-key-description choice
>>                                          nil)))))
>>           (setq choice (read-key-sequence (concat "Choose: " prompt)))
>>           (when (setq command (lookup-key commands-map choice))
>> -          (when (numberp command) (setq command nil))
>> +          (when (numberp command)
>> +            (setq command
>> +                  (lookup-key commands-map (substring choice command))))
> Wow, it works nicely for 'C-x p p C-x d'.
> But strange it fails for 'C-x p p C-x v d'
> with the same error:
> 
>    (wrong-type-argument commandp 1)

That's because (lookup-key ... "vd") also returns 1.

I haven't been able to find a solution that works like we would expect. 
The most trivial would be to loop cutting off invalid prefixes, but then 
we end up with 'd', not 'v'. That's probably not what you want.

Ideally, 'read-key-sequence' would stop at the user pressing 'v' and 
return "^Xv", then the rest would work out okay. But I haven't managed 
to have it do that, even when using overriding-terminal-local-map and 
temporarily altering the global map. My experimental patch is below, you 
can try tweaking it.

And overall I'm not sure it's a constructive approach because you might 
have been going for 'C-x v d' (where the 'v' translation is correct), 
but you might have been going for 'C-x v D' or 'C-x v v' instead, very 
different commands. It might be better to report unknown key sequence 
and let the user make an explicit choice, like it works now. Depends on 
whether you notice the key sequence echoing while doing that input.

diff --git a/lisp/progmodes/project.el b/lisp/progmodes/project.el
index fda1081eb62..09d7e1025ca 100644
--- a/lisp/progmodes/project.el
+++ b/lisp/progmodes/project.el
@@ -1983,7 +1983,6 @@ project--switch-project-command
             project-switch-commands))
           (commands-map
            (let ((temp-map (make-sparse-keymap)))
-            (set-keymap-parent temp-map project-prefix-map)
              (dolist (row commands-menu temp-map)
                (when-let ((cmd (nth 0 row))
                           (keychar (nth 2 row)))
@@ -1991,7 +1990,15 @@ project--switch-project-command
           command
           choice)
      (while (not command)
-      (let* ((overriding-local-map commands-map)
+      (let* ((map
+              (let ((map (make-sparse-keymap)))
+                (set-keymap-parent map (if project-switch-use-entire-map
+                                           project-prefix-map
+                                         commands-map))
+                (define-key map "\e\e\e" 'keyboard-escape-quit)
+                (define-key map "\C-g" 'keyboard-quit)
+                map))
+             (overriding-terminal-local-map map)
               (prompt (if project-switch-use-entire-map
                           (project--keymap-prompt)
                         (project--menu-prompt))))
@@ -2001,16 +2008,20 @@ project--switch-project-command
                                         (propertize "Unrecognized input"
                                                     'face 'warning)
                                         (help-key-description choice 
nil)))))
-        (setq choice (read-key-sequence (concat "Choose: " prompt)))
-        (when (setq command (lookup-key commands-map choice))
-          (when (numberp command) (setq command nil))
+        (unwind-protect
+            (progn
+              (use-global-map map)
+              (setq choice (read-key-sequence (concat "Choose: " 
prompt) nil t nil t)))
+          (use-global-map global-map))
+        (when (setq command (lookup-key map choice))
+          (while (numberp command)
+            (setq choice (substring choice command))
+            (setq command (lookup-key map choice)))
+          (when (memq command '(keyboard-quit keyboard-escape-quit))
+            (call-interactively command))
            (unless (or project-switch-use-entire-map
                        (assq command commands-menu))
-            (setq command nil)))
-        (let ((global-command (lookup-key (current-global-map) choice)))
-          (when (memq global-command
-                      '(keyboard-quit keyboard-escape-quit))
-            (call-interactively global-command)))))
+            (setq command nil)))))
      (message nil)
      command))