unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#62759: [PATCH] add support for prompting for projects by name
@ 2023-04-10 19:14 Spencer Baugh
  2023-04-10 23:20 ` Dmitry Gutov
  0 siblings, 1 reply; 3+ messages in thread
From: Spencer Baugh @ 2023-04-10 19:14 UTC (permalink / raw)
  To: 62759

[-- Attachment #1: Type: text/plain, Size: 651 bytes --]

Tags: patch


This add a new function project-prompt-project-name, and a
project-prompter customize variable which can be used to switch between
that function and the existing project-prompt-project-dir.

In GNU Emacs 29.0.60 (build 3, x86_64-pc-linux-gnu, X toolkit, cairo
 version 1.15.12, Xaw scroll bars) of 2023-03-13 built on
 igm-qws-u22796a
Repository revision: e759905d2e0828eac4c8164b09113b40f6899656
Repository branch: emacs-29
Windowing system distributor 'The X.Org Foundation', version 11.0.12011000
System Description: CentOS Linux 7 (Core)

Configured using:
 'configure --with-x-toolkit=lucid --with-modules
 --with-gif=ifavailable'


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-add-support-for-prompting-for-projects-by-name.patch --]
[-- Type: text/patch, Size: 4070 bytes --]

From e7a03ab0e74bddadf8fa349587ab60cc7f63f6c2 Mon Sep 17 00:00:00 2001
From: Spencer Baugh <sbaugh@janestreet.com>
Date: Mon, 10 Apr 2023 15:11:06 -0400
Subject: [PATCH] add support for prompting for projects by name

---
 lisp/progmodes/project.el | 43 ++++++++++++++++++++++++++++++++++++---
 1 file 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 @@ project-current-directory-override
   "Value to use instead of `default-directory' when detecting the project.
 When it is non-nil, `project-current' will always skip prompting too.")
 
+(defcustom project-prompter #'project-prompt-project-dir
+  "Function to call to prompt for a project.
+Called with no arguments and should return a project root dir."
+  :type '(choice (const :tag "Prompt for a project directory"
+                        project-prompt-project-dir)
+                 (const :tag "Prompt for a project name"
+                        project-prompt-project-name)
+                 (function :tag "Custom function" nil))
+  :group 'project
+  :version "30.1")
+
 ;;;###autoload
 (defun project-current (&optional maybe-prompt directory)
   "Return the project instance in DIRECTORY, defaulting to `default-directory'.
@@ -226,7 +237,7 @@ project-current
      (pr)
      ((unless project-current-directory-override
         maybe-prompt)
-      (setq directory (project-prompt-project-dir)
+      (setq directory (funcall project-prompter)
             pr (project--find-in-directory directory))))
     (when maybe-prompt
       (if pr
@@ -1615,7 +1626,7 @@ project-forget-project
   "Remove directory PROJECT-ROOT from the project list.
 PROJECT-ROOT is the root directory of a known project listed in
 the project list."
-  (interactive (list (project-prompt-project-dir)))
+  (interactive (list (funcall project-prompter)))
   (project--remove-from-project-list
    project-root "Project `%s' removed from known projects"))
 
@@ -1639,6 +1650,32 @@ project-prompt-project-dir
         (read-directory-name "Select directory: " default-directory nil t)
       pr-dir)))
 
+(defun project-prompt-project-name ()
+  "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."
+  (let* ((dir-choice "... (choose a dir)")
+         (choices
+          (let (ret)
+            (dolist (dir (project-known-project-roots))
+              ;; we filter out directories that no longer map to a project,
+              ;; since they don't have a clean project-name.
+              (if-let (proj (project--find-in-directory dir))
+                  (push (cons (project-name proj) proj) ret)))
+            ret))
+         ;; XXX: Just using this for the category (for the substring
+         ;; completion style).
+         (table (project--file-completion-table (cons dir-choice choices)))
+         (pr-name ""))
+    (while (equal pr-name "")
+      ;; If the user simply pressed RET, do this again until they don't.
+      (setq pr-name (completing-read "Select project: " table nil t)))
+    (if (equal pr-name dir-choice)
+        (read-directory-name "Select directory: " default-directory nil t)
+      (let ((proj (assoc pr-name choices)))
+        (if (stringp proj) proj (project-root (cdr proj)))))))
+
 ;;;###autoload
 (defun project-known-project-roots ()
   "Return the list of root directories of all known projects."
@@ -1826,7 +1863,7 @@ project-switch-project
 
 When called in a program, it will use the project corresponding
 to directory DIR."
-  (interactive (list (project-prompt-project-dir)))
+  (interactive (list (funcall project-prompter)))
   (let ((command (if (symbolp project-switch-commands)
                      project-switch-commands
                    (project--switch-project-command))))
-- 
2.30.2


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* bug#62759: [PATCH] add support for prompting for projects by name
  2023-04-10 19:14 bug#62759: [PATCH] add support for prompting for projects by name Spencer Baugh
@ 2023-04-10 23:20 ` Dmitry Gutov
  2023-04-11  0:08   ` Spencer Baugh
  0 siblings, 1 reply; 3+ messages in thread
From: Dmitry Gutov @ 2023-04-10 23:20 UTC (permalink / raw)
  To: Spencer Baugh, 62759-done

Hi! Thanks for the patch. Pushed to master.

Next time please also add the full common message, though.

Also note that the idea behind the current default (and also why it 
doesn't return a project instance: just the dir) was to avoid visiting 
all the directories -- because some might be unavailable at this exact 
moment (e.g. remote, over Tramp, and currently disconnected), and some 
simply slow.

It shouldn't hurt to have this as an alternative behavior, though.





^ permalink raw reply	[flat|nested] 3+ messages in thread

* bug#62759: [PATCH] add support for prompting for projects by name
  2023-04-10 23:20 ` Dmitry Gutov
@ 2023-04-11  0:08   ` Spencer Baugh
  0 siblings, 0 replies; 3+ messages in thread
From: Spencer Baugh @ 2023-04-11  0:08 UTC (permalink / raw)
  To: Dmitry Gutov; +Cc: 62759-done

[-- Attachment #1: Type: text/plain, Size: 916 bytes --]

On Mon, Apr 10, 2023, 19:20 Dmitry Gutov <dmitry@gutov.dev> wrote:

> Hi! Thanks for the patch. Pushed to master.
>
> Next time please also add the full common message, though.
>

Sorry, will do. I was expecting to need to iterate through some review
cycles :)

Also note that the idea behind the current default (and also why it
> doesn't return a project instance: just the dir) was to avoid visiting
> all the directories -- because some might be unavailable at this exact
> moment (e.g. remote, over Tramp, and currently disconnected), and some
> simply slow.


Yes, I figured that was the reason. I should also mention that I was
thinking about automatically pruning zombie projects (my backend yields
lots of those) but instead I just had project-prompt-project-name skip
zombies, for exactly this reason (that it avoids excess FS access).

It shouldn't hurt to have this as an alternative behavior, though.
>

[-- Attachment #2: Type: text/html, Size: 1720 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-04-11  0:08 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-04-10 19:14 bug#62759: [PATCH] add support for prompting for projects by name Spencer Baugh
2023-04-10 23:20 ` Dmitry Gutov
2023-04-11  0:08   ` Spencer Baugh

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).