aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSpencer Baugh2023-04-10 15:11:06 -0400
committerDmitry Gutov2023-04-11 02:14:57 +0300
commit9efa6d2cf28f4e21f23bb0dbfedc59a4286dab12 (patch)
tree5780ce793c743a10031bf3feb7b6afc8c1d42e8c
parent2d3947ba7a7ed5ff1f7da794710e10dacc415881 (diff)
downloademacs-9efa6d2cf28f4e21f23bb0dbfedc59a4286dab12.tar.gz
emacs-9efa6d2cf28f4e21f23bb0dbfedc59a4286dab12.zip
Add support for prompting for projects by name
* lisp/progmodes/project.el (project-prompter): New user option (bug#62759). (project-prompt-project-name): New function.
-rw-r--r--lisp/progmodes/project.el43
1 files changed, 40 insertions, 3 deletions
diff --git a/lisp/progmodes/project.el b/lisp/progmodes/project.el
index 877d79353aa..e7c0bd2069b 100644
--- a/lisp/progmodes/project.el
+++ b/lisp/progmodes/project.el
@@ -202,6 +202,17 @@ CL struct.")
202 "Value to use instead of `default-directory' when detecting the project. 202 "Value to use instead of `default-directory' when detecting the project.
203When it is non-nil, `project-current' will always skip prompting too.") 203When it is non-nil, `project-current' will always skip prompting too.")
204 204
205(defcustom project-prompter #'project-prompt-project-dir
206 "Function to call to prompt for a project.
207Called with no arguments and should return a project root dir."
208 :type '(choice (const :tag "Prompt for a project directory"
209 project-prompt-project-dir)
210 (const :tag "Prompt for a project name"
211 project-prompt-project-name)
212 (function :tag "Custom function" nil))
213 :group 'project
214 :version "30.1")
215
205;;;###autoload 216;;;###autoload
206(defun project-current (&optional maybe-prompt directory) 217(defun project-current (&optional maybe-prompt directory)
207 "Return the project instance in DIRECTORY, defaulting to `default-directory'. 218 "Return the project instance in DIRECTORY, defaulting to `default-directory'.
@@ -226,7 +237,7 @@ of the project instance object."
226 (pr) 237 (pr)
227 ((unless project-current-directory-override 238 ((unless project-current-directory-override
228 maybe-prompt) 239 maybe-prompt)
229 (setq directory (project-prompt-project-dir) 240 (setq directory (funcall project-prompter)
230 pr (project--find-in-directory directory)))) 241 pr (project--find-in-directory directory))))
231 (when maybe-prompt 242 (when maybe-prompt
232 (if pr 243 (if pr
@@ -1615,7 +1626,7 @@ passed to `message' as its first argument."
1615 "Remove directory PROJECT-ROOT from the project list. 1626 "Remove directory PROJECT-ROOT from the project list.
1616PROJECT-ROOT is the root directory of a known project listed in 1627PROJECT-ROOT is the root directory of a known project listed in
1617the project list." 1628the project list."
1618 (interactive (list (project-prompt-project-dir))) 1629 (interactive (list (funcall project-prompter)))
1619 (project--remove-from-project-list 1630 (project--remove-from-project-list
1620 project-root "Project `%s' removed from known projects")) 1631 project-root "Project `%s' removed from known projects"))
1621 1632
@@ -1639,6 +1650,32 @@ It's also possible to enter an arbitrary directory not in the list."
1639 (read-directory-name "Select directory: " default-directory nil t) 1650 (read-directory-name "Select directory: " default-directory nil t)
1640 pr-dir))) 1651 pr-dir)))
1641 1652
1653(defun project-prompt-project-name ()
1654 "Prompt the user for a project, by name, that is one of the known project roots.
1655The project is chosen among projects known from the project list,
1656see `project-list-file'.
1657It's also possible to enter an arbitrary directory not in the list."
1658 (let* ((dir-choice "... (choose a dir)")
1659 (choices
1660 (let (ret)
1661 (dolist (dir (project-known-project-roots))
1662 ;; we filter out directories that no longer map to a project,
1663 ;; since they don't have a clean project-name.
1664 (if-let (proj (project--find-in-directory dir))
1665 (push (cons (project-name proj) proj) ret)))
1666 ret))
1667 ;; XXX: Just using this for the category (for the substring
1668 ;; completion style).
1669 (table (project--file-completion-table (cons dir-choice choices)))
1670 (pr-name ""))
1671 (while (equal pr-name "")
1672 ;; If the user simply pressed RET, do this again until they don't.
1673 (setq pr-name (completing-read "Select project: " table nil t)))
1674 (if (equal pr-name dir-choice)
1675 (read-directory-name "Select directory: " default-directory nil t)
1676 (let ((proj (assoc pr-name choices)))
1677 (if (stringp proj) proj (project-root (cdr proj)))))))
1678
1642;;;###autoload 1679;;;###autoload
1643(defun project-known-project-roots () 1680(defun project-known-project-roots ()
1644 "Return the list of root directories of all known projects." 1681 "Return the list of root directories of all known projects."
@@ -1826,7 +1863,7 @@ made from `project-switch-commands'.
1826 1863
1827When called in a program, it will use the project corresponding 1864When called in a program, it will use the project corresponding
1828to directory DIR." 1865to directory DIR."
1829 (interactive (list (project-prompt-project-dir))) 1866 (interactive (list (funcall project-prompter)))
1830 (let ((command (if (symbolp project-switch-commands) 1867 (let ((command (if (symbolp project-switch-commands)
1831 project-switch-commands 1868 project-switch-commands
1832 (project--switch-project-command)))) 1869 (project--switch-project-command))))