Subject: bug#70577: [PATCH] New command other-project-prefix

From: Juri Linkov <[email-masked]>
Date: Wed, 22 May 2024 09:12:19 +0300

>> This works until I moved project-prompter to the beginning,
>> then 'C-x 5 p p' doesn't work with this:
>>    (letrec ((root (funcall project-prompter))
>>             (depth (minibuffer-depth))
>>             (echofun (lambda () "[switch-project]"))
>>             (around-fun
>> Maybe there is a better place?
>
> Last I checked, the project-prompter can change the value of this-command.
>
> Perhaps you can try a let-binding for this-command around the call to
> project-prompter, so that it's restored at the end.

This helps: (letrec ((root (let ((this-command this-command)) (funcall project-prompter)))

> Something to also be concerned about is having any of the display-buffer
> modifications, or other-project advices, get applied to one of the commands
> inside the project-prompter UI (if it's implemented using a sequence of
> commands). Perhaps I haven't triggered this case mostly by luck so far. How
> to guard against that? Maybe a dynamic variable of some sort. But then it'd
> also have to be checked uniformly in all such functions (hooks that prefix
> commands install).

This also helps:

diff --git a/lisp/window.el b/lisp/window.el
index 4147d7e6ebb..a4577d509b8 100644
--- a/lisp/window.el
+++ b/lisp/window.el
@@ -9252,7 +9279,7 @@ display-buffer-override-next-command
                        ;; after the first display-buffer action (bug#39722).
                        (funcall clearfun)
                        new-window))))
-         (command this-command)
+         (command this-original-command)
          (echofun (when echo (lambda () echo)))
          (exitfun
           (lambda ()
@@ -9274,7 +9301,8 @@ display-buffer-override-next-command
 		     (> (minibuffer-depth) minibuffer-depth)
 		     ;; But don't remove immediately after
 		     ;; adding the hook by the same command below.
-		     (eq this-command command))
+		     (eq this-original-command command)
+		     (get command 'prefix-command))
               (funcall exitfun))))
     ;; Call post-function after the next command finishes (bug#49057).
     (add-hook 'post-command-hook postfun)

But not sure about the latter, it might break some cases.