emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

master 9904376c797: Support calling 'project-current' with custom prompt


From: Dmitry Gutov
Subject: master 9904376c797: Support calling 'project-current' with custom prompt
Date: Mon, 7 Oct 2024 19:06:33 -0400 (EDT)

branch: master
commit 9904376c797665de47ff760bcf8c2fe33d7ae625
Author: Dmitry Gutov <dmitry@gutov.dev>
Commit: Dmitry Gutov <dmitry@gutov.dev>

    Support calling 'project-current' with custom prompt
    
    * lisp/progmodes/project.el (project-current): Treat being passed
    a string in the MAYBE-CURRENT argument specially (bug#70833).
    (project-prompt-project-dir, project-prompt-project-name):
    Handle it.
    
    * etc/NEWS: Mention that change.
---
 etc/NEWS                  |  6 ++++++
 lisp/progmodes/project.el | 28 ++++++++++++++++++++--------
 2 files changed, 26 insertions(+), 8 deletions(-)

diff --git a/etc/NEWS b/etc/NEWS
index b532d7c6555..b5104145878 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -124,6 +124,12 @@ This hook allows you to control which tab-bar tabs are 
auto-resized.
 *** New command 'project-root-find-file'.
 It is equivalent to running ‘project-any-command’ with ‘find-file’.
 
+---
+*** The MAYBE-PROMPT argument of 'project-current' can be a string.
+When such value is used, the 'project-prompt' values are called with it
+as the first argument.  This is a way for the callers to indicate, for
+example, the reason or the context why the project is asked for.
+
 ** Registers
 
 *** New functions 'buffer-to-register' and 'file-to-register'.
diff --git a/lisp/progmodes/project.el b/lisp/progmodes/project.el
index 599a350e5ce..b2f18da8925 100644
--- a/lisp/progmodes/project.el
+++ b/lisp/progmodes/project.el
@@ -218,7 +218,8 @@ else prompt the user for the project to use.  To prompt for 
a
 project, call the function specified by `project-prompter', which
 returns the directory in which to look for the project.  If no
 project is found in that directory, return a \"transient\"
-project instance.
+project instance.  When MAYBE-PROMPT is a string, it's passed to the
+prompter function as an argument.
 
 The \"transient\" project instance is a special kind of value
 which denotes a project rooted in that directory and includes all
@@ -235,7 +236,9 @@ of the project instance object."
      (pr)
      ((unless project-current-directory-override
         maybe-prompt)
-      (setq directory (funcall project-prompter)
+      (setq directory (if (stringp maybe-prompt)
+                          (funcall project-prompter maybe-prompt)
+                        (funcall project-prompter))
             pr (project--find-in-directory directory))))
     (when maybe-prompt
       (if pr
@@ -1880,11 +1883,12 @@ the project list."
 
 (defvar project--dir-history)
 
-(defun project-prompt-project-dir ()
+(defun project-prompt-project-dir (&optional prompt)
   "Prompt the user for a directory that is one of the known project roots.
 The project is chosen among projects known from the project list,
 see `project-list-file'.
-It's also possible to enter an arbitrary directory not in the list."
+It's also possible to enter an arbitrary directory not in the list.
+When PROMPT is non-nil, use it as the prompt string."
   (project--ensure-read-project-list)
   (let* ((dir-choice "... (choose a dir)")
          (choices
@@ -1898,18 +1902,23 @@ It's also possible to enter an arbitrary directory not 
in the list."
       ;; If the user simply pressed RET, do this again until they don't.
       (setq pr-dir
             (let (history-add-new-input)
-              (completing-read "Select project: " choices nil t nil 
'project--dir-history))))
+              (completing-read (if prompt
+                                   ;; TODO: Use `format-prompt' (Emacs 28.1+)
+                                   (format "%s: " (substitute-command-keys 
prompt))
+                                 "Select project: ")
+                               choices nil t nil 'project--dir-history))))
     (if (equal pr-dir dir-choice)
         (read-directory-name "Select directory: " default-directory nil t)
       pr-dir)))
 
 (defvar project--name-history)
 
-(defun project-prompt-project-name ()
+(defun project-prompt-project-name (&optional prompt)
   "Prompt the user for a project, by name, that is one of the known project 
roots.
 The project is chosen among projects known from the project list,
 see `project-list-file'.
-It's also possible to enter an arbitrary directory not in the list."
+It's also possible to enter an arbitrary directory not in the list.
+When PROMPT is non-nil, use it as the prompt string."
   (let* ((dir-choice "... (choose a dir)")
          project--name-history
          (choices
@@ -1933,7 +1942,10 @@ It's also possible to enter an arbitrary directory not 
in the list."
       ;; If the user simply pressed RET, do this again until they don't.
       (setq pr-name
             (let (history-add-new-input)
-              (completing-read "Select project: " table nil t nil 
'project--name-history))))
+              (completing-read (if prompt
+                                   (format "%s: " prompt)
+                                 "Select project: ")
+                               table nil t nil 'project--name-history))))
     (if (equal pr-name dir-choice)
         (read-directory-name "Select directory: " default-directory nil t)
       (let ((proj (assoc pr-name choices)))



reply via email to

[Prev in Thread] Current Thread [Next in Thread]