emacs-elpa-diffs
[Top][All Lists]
Advanced

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

[elpa] externals/consult aa93dca652 1/2: consult-project-buffer: Prompt


From: ELPA Syncer
Subject: [elpa] externals/consult aa93dca652 1/2: consult-project-buffer: Prompt if there is no current project
Date: Tue, 15 Feb 2022 20:57:25 -0500 (EST)

branch: externals/consult
commit aa93dca652daa48c243100e1d3c8367c27e2eced
Author: Daniel Mendler <mail@daniel-mendler.de>
Commit: Daniel Mendler <mail@daniel-mendler.de>

    consult-project-buffer: Prompt if there is no current project
---
 consult.el | 59 +++++++++++++++++++++++++++++++++++++++++++----------------
 1 file changed, 43 insertions(+), 16 deletions(-)

diff --git a/consult.el b/consult.el
index fa0643f28a..8e5c34eecf 100644
--- a/consult.el
+++ b/consult.el
@@ -843,16 +843,24 @@ Otherwise the `default-directory' is returned."
       (t (format "%s (%s): " prompt (consult--abbreviate-directory dir))))
      edir)))
 
-(defun consult--project-root-default-function ()
-  "Return project root directory."
-  (when-let (proj (project-current))
+(defun consult--project-root-default-function (&optional maybe-prompt)
+  "Return project root directory.
+When no project is found and MAYBE-PROMPT is non-nil ask the user."
+  (when-let (proj (project-current maybe-prompt))
     (cond
      ((fboundp 'project-root) (project-root proj))
      ((fboundp 'project-roots) (car (project-roots proj))))))
 
-(defun consult--project-root ()
-  "Return project root as absolute path."
-  (when-let (root (and consult-project-root-function (funcall 
consult-project-root-function)))
+(defun consult--project-root (&optional maybe-prompt)
+  "Return project root as absolute path.
+When no project is found and MAYBE-PROMPT is non-nil ask the user."
+  (when-let (root (and consult-project-root-function
+                       (if maybe-prompt
+                           (condition-case nil
+                               (funcall consult-project-root-function t)
+                             (wrong-number-of-arguments
+                              (funcall consult-project-root-function)))
+                         (funcall consult-project-root-function))))
     (expand-file-name root)))
 
 (defun consult--project-name (dir)
@@ -4061,17 +4069,18 @@ If NORECORD is non-nil, do not record the buffer switch 
in the buffer list."
   "Recent file candidate source for `consult-buffer'.")
 
 ;;;###autoload
-(defun consult-buffer ()
+(defun consult-buffer (&optional sources)
   "Enhanced `switch-to-buffer' command with support for virtual buffers.
 
-The command supports recent files, bookmarks, views and project files as 
virtual
-buffers. Buffers are previewed. Furthermore narrowing to buffers (b), files 
(f),
-bookmarks (m) and project files (p) is supported via the corresponding keys. In
-order to determine the project-specific files and buffers, the
-`consult-project-root-function' is used. See `consult-buffer-sources' and
-`consult--multi' for the configuration of the virtual buffer sources."
+The command supports recent files, bookmarks, views and project files as
+virtual buffers. Buffers are previewed. Narrowing to buffers (b), files (f),
+bookmarks (m) and project files (p) is supported via the corresponding
+keys. In order to determine the project-specific files and buffers, the
+`consult-project-root-function' is used. The virtual buffer SOURCES
+default to `consult-buffer-sources'. See `consult--multi' for the
+configuration of the virtual buffer sources."
   (interactive)
-  (when-let (buffer (consult--multi consult-buffer-sources
+  (when-let (buffer (consult--multi (or sources consult-buffer-sources)
                                     :require-match
                                     (confirm-nonexistent-file-or-buffer)
                                     :prompt "Switch to: "
@@ -4088,13 +4097,31 @@ order to determine the project-specific files and 
buffers, the
        `(:hidden nil :narrow ?b ,@consult--source-project-buffer)
        `(:hidden nil :narrow ?f ,@consult--source-project-recent-file)))
 
+(defmacro consult--with-project (&rest body)
+  "Ensure that BODY is executed with a project root."
+  ;; We have to work quite hard here to ensure that the project root is
+  ;; only overriden at the current recursion level. When entering a
+  ;; recursive minibuffer session, we should be able to still switch the
+  ;; project. But who does that? Working on the first level on project A
+  ;; and on the second level on project B and on the third level on project C?
+  ;; You mustn't be afraid to dream a little bigger, darling.
+  `(let ((consult-project-root-function
+          (let ((root (or (consult--project-root t) (user-error "No project 
found")))
+                (depth (recursion-depth))
+                (orig consult-project-root-function))
+            (lambda (&rest args)
+              (if (= depth (recursion-depth))
+                  root
+                (apply orig args))))))
+     ,@body))
+
 ;;;###autoload
 (defun consult-project-buffer ()
   "Enhanced `project-switch-to-buffer' command with support for virtual 
buffers.
 See `consult-buffer' for more details."
   (interactive)
-  (let ((consult-buffer-sources consult-project-buffer-sources))
-    (consult-buffer)))
+  (consult--with-project
+    (consult-buffer consult-project-buffer-sources)))
 
 ;;;###autoload
 (defun consult-buffer-other-window ()



reply via email to

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