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: Mon, 23 Oct 2023 20:24:08 +0300

On 23/10/2023 09:58, Juri Linkov wrote:
>>> 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.
> 
> Now can't type 'C-x p p C-x v d' completely because
> 'C-x p p C-x v' opens vc-dir after typing 'v'.

Huh. That actually works for me now, after I ran 'make' and restarted.

But I thought you wanted this? 'C-x v d' ends with vc-dir, so if 'C-x p 
p C-x v' opens vc-dir, then isn't this what you expected?

Anyway, the resulting UI is fairly counter-intuitive.

>> 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.
> 
> Maybe adding a new option 'project-switch-use-global-map' would help?

Try the attached, it's another iteration on the latest patch 
(prefix-command-echo-keystrokes-functions doesn't seem to work reliably 
in this context). Try it together with

   (setq project-switch-commands 'project-any-command)

Also see the "Variation" in the comment (it allows for short bindings).

diff --git a/lisp/progmodes/project.el b/lisp/progmodes/project.el
index fda1081eb62..6fffa92d71f 100644
--- a/lisp/progmodes/project.el
+++ b/lisp/progmodes/project.el
@@ -847,6 +847,7 @@ project-prefix-map
     (define-key map "G" 'project-or-external-find-regexp)
     (define-key map "r" 'project-query-replace-regexp)
     (define-key map "x" 'project-execute-extended-command)
+    (define-key map "o" 'project-any-command)
     (define-key map "\C-b" 'project-list-buffers)
     map)
   "Keymap for project commands.")
@@ -1813,6 +1814,37 @@ project-execute-extended-command
   (let ((default-directory (project-root (project-current t))))
     (call-interactively #'execute-extended-command)))
 
+;;;###autoload
+(defun project-any-command ()
+  "Run the next command in the current project.
+If the command is in `project-prefix-map', it gets passed that
+info with `project-current-directory-override'.  Otherwise,
+`default-directory' is temporarily set to the current project's
+root."
+  (interactive)
+  (let* ((pr (project-current t))
+         ;; Variation: could be a separate command, or an option.
+         ;; (command (let ((overriding-local-map project-prefix-map))
+         ;;            (key-binding (read-key-sequence
+         ;;                          (format "[execute in %s]:" (project-root pr)))
+         ;;                         t)))
+         (command (key-binding (read-key-sequence
+                                (format "[execute in %s]:" (project-root pr)))
+                               t))
+         (root (project-root pr))
+         found)
+    (when command
+      ;; We could also check the command name against "\\`project-",
+      ;; and/or (get command 'project-command).
+      (map-keymap
+       (lambda (_evt cmd) (if (eq cmd command) (setq found t)))
+       project-prefix-map)
+      (if found
+          (let ((project-current-directory-override root))
+            (call-interactively command))
+        (let ((default-directory root))
+          (call-interactively command))))))
+
 (defun project-remember-projects-under (dir &optional recursive)
   "Index all projects below a directory DIR.
 If RECURSIVE is non-nil, recurse into all subdirectories to find
@@ -1891,7 +1923,8 @@ project-switch-commands
     (project-find-regexp "Find regexp")
     (project-find-dir "Find directory")
     (project-vc-dir "VC-Dir")
-    (project-eshell "Eshell"))
+    (project-eshell "Eshell")
+    (project-any-command "Other"))
   "Alist mapping commands to descriptions.
 Used by `project-switch-project' to construct a dispatch menu of
 commands available upon \"switching\" to another project.