diff --git a/lisp/progmodes/project.el b/lisp/progmodes/project.el index 52fe4df9080..e41a23fe13d 100644 --- a/lisp/progmodes/project.el +++ b/lisp/progmodes/project.el @@ -967,6 +967,45 @@ project-other-tab-command (when (bound-and-true-p tab-prefix-map) (define-key tab-prefix-map "p" #'project-other-tab-command)) +;;;###autoload +(defun other-project-prefix () + "\"Switch\" to another project before running an Emacs command. +The next command you invoke will prompt for the project in which to run +the command." + (interactive) + (prefix-command-preserve-state) + (letrec ((depth (minibuffer-depth)) + (echofun (lambda () "[switch-project]")) + (around-fun + (lambda (command &rest _args) + (interactive) + (advice-remove this-command around-fun) + (if (or (eq this-command 'other-project-prefix) + (eq last-command-event help-char)) + (call-interactively command) + (let* ((this-command-saved this-command) + (root (funcall project-prompter))) + (if (or (string-prefix-p "project-" + (symbol-name this-command-saved)) + (get this-command-saved 'project-aware)) + (let ((project-current-directory-override root)) + (call-interactively command)) + (let ((default-directory root)) + (call-interactively command))))))) + (prefun + (lambda () + (unless (> (minibuffer-depth) depth) + (remove-hook 'pre-command-hook prefun) + (remove-hook 'prefix-command-echo-keystrokes-functions echofun) + (when (and this-command (symbolp this-command)) + (advice-add this-command :around around-fun)))))) + (add-hook 'pre-command-hook prefun) + (add-hook 'prefix-command-echo-keystrokes-functions echofun) + (set-transient-map project-prefix-map) + (message (concat "Type " (project--keymap-prompt) " or any global key")))) + +;; (define-key project-prefix-map (kbd "P") #'other-project-prefix) + (declare-function grep-read-files "grep") (declare-function xref--find-ignores-arguments "xref")