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

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

[elpa] externals/consult 94224d0f91: Bring back consult-project-root-fun


From: ELPA Syncer
Subject: [elpa] externals/consult 94224d0f91: Bring back consult-project-root-function
Date: Tue, 15 Feb 2022 14:57:20 -0500 (EST)

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

    Bring back consult-project-root-function
---
 CHANGELOG.org    |   8 +--
 README.org       |  33 ++++++++---
 consult-imenu.el |   8 +--
 consult.el       | 177 +++++++++++++++++++++----------------------------------
 4 files changed, 98 insertions(+), 128 deletions(-)

diff --git a/CHANGELOG.org b/CHANGELOG.org
index 0e431802ef..50997881a2 100644
--- a/CHANGELOG.org
+++ b/CHANGELOG.org
@@ -4,12 +4,8 @@
 
 * Development
 
-- Add =consult--source-project-root=, a =consult-buffer= source for root 
directories
-  of known projects.
-- Introduce generalized =consult-project-function=, deprecate
-  =consult-project-root-function=. The =consult-project-function= is used to
-  retrieve the project directories and is preconfigured with a default 
function,
-  which uses project.el.
+- Preconfigure =consult-project-root-function= with a default function based
+  on project.el.
 - Add =consult-project-buffer=, a variant of =consult-buffer= restricted to the
   current project.
 
diff --git a/README.org b/README.org
index a4e98eb5ee..4cfbaed1c9 100644
--- a/README.org
+++ b/README.org
@@ -256,10 +256,10 @@ their descriptions.
    =filter-string= is passed to the /fast/ Emacs filtering to further narrow 
down
    the list of matches. This is particularly useful if you are using an 
advanced
    completion style like orderless. =consult-grep= supports preview. If the
-   =consult-project-function= is [[#use-package-example][configured]] and 
returns a non-nil directory,
-   =consult-grep= searches the current project directory. Otherwise the
-   =default-directory= is searched. If =consult-grep= is invoked with prefix
-   argument =C-u M-s g=, you can specify the directory manually.
+   =consult-project-root-function= is [[#use-package-example][configured]] and 
returns non-nil, =consult-grep=
+   searches the current project directory. Otherwise the =default-directory= is
+   searched. If =consult-grep= is invoked with prefix argument =C-u M-s g=, 
you can
+   specify the directory manually.
  - =consult-find=, =consult-locate=: Find file by
    matching the path against a regexp. Like for =consult-grep,= either the 
project
    root or the current directory is the root directory for the search. The 
input
@@ -447,8 +447,8 @@ their descriptions.
    (consult-customize
     consult-ripgrep consult-git-grep consult-grep
     consult-bookmark consult-recent-file consult-xref
-    consult--source-bookmark consult--source-project-root
     consult--source-recent-file consult--source-project-recent-file
+    consult--source-bookmark
     :preview-key (kbd "M-."))
  #+end_src
 
@@ -636,7 +636,9 @@ their descriptions.
 
   #+begin_src emacs-lisp
     (consult-customize
-     consult--source-recent-file consult--source-project-recent-file 
consult--source-bookmark
+     consult--source-recent-file
+     consult--source-project-recent-file
+     consult--source-bookmark
      :preview-key (kbd "M-."))
   #+end_src
 
@@ -876,8 +878,8 @@ configuration examples.
       consult-theme
       :preview-key '(:debounce 0.2 any)
       consult-ripgrep consult-git-grep consult-grep
-      consult-bookmark consult-recent-file consult-xref
-      consult--source-recent-file consult--source-project-recent-file 
consult--source-bookmark
+      consult-bookmark consult-recent-file consult-xref 
consult--source-bookmark
+      consult--source-recent-file consult--source-project-recent-file
       :preview-key (kbd "M-."))
 
      ;; Optionally configure the narrowing key.
@@ -887,6 +889,19 @@ configuration examples.
      ;; Optionally make narrowing help available in the minibuffer.
      ;; You may want to use `embark-prefix-help-command' or which-key instead.
      ;; (define-key consult-narrow-map (vconcat consult-narrow-key "?") 
#'consult-narrow-help)
+
+     ;; By default `consult-project-root-function' uses `project-root' from 
project.el.
+     ;; Optionally configure a different project root function.
+     ;; There are multiple reasonable alternatives to chose from.
+     ;;;; 1. project.el (the default)
+     ;; (setq consult-project-root-function 
#'consult--project-root-default-function)
+     ;;;; 2. projectile.el (projectile-project-root)
+     ;; (autoload 'projectile-project-root "projectile")
+     ;; (setq consult-project-root-function #'projectile-project-root)
+     ;;;; 3. vc.el (vc-root-dir)
+     ;; (setq consult-project-root-function #'vc-root-dir)
+     ;;;; 4. locate-dominating-file
+     ;; (setq consult-project-root-function (lambda () (locate-dominating-file 
"." ".git")))
    )
  #+end_src
 
@@ -936,7 +951,7 @@ configuration examples.
  | consult-preview-max-size         | Files larger than this size are not 
previewed         |
  | consult-preview-raw-size         | Files larger than this size are 
previewed in raw form |
  | consult-project-buffer-sources   | List of virtual project buffer sources   
             |
- | consult-project-function         | Function which returns project 
directories            |
+ | consult-project-root-function    | Function which returns current project 
root           |
  | consult-recent-file-filter       | Filter for =consult-recent-file=         
               |
  | consult-register-narrow          | Narrowing configuration for 
=consult-register=          |
  | consult-ripgrep-args             | Command line arguments for ripgrep       
             |
diff --git a/consult-imenu.el b/consult-imenu.el
index 4269030d7c..2ce241342f 100644
--- a/consult-imenu.el
+++ b/consult-imenu.el
@@ -216,10 +216,10 @@ See also `consult-imenu-multi'."
   "Select item from the imenus of all buffers from the same project.
 
 In order to determine the buffers belonging to the same project, the
-`consult-project-function' is used. Only the buffers with the same major
-mode as the current buffer are used. See also `consult-imenu' for more
-details. In order to search a subset of buffers, QUERY can be set to a
-plist according to `consult--buffer-query'."
+`consult-project-root-function' is used. Only the buffers with the
+same major mode as the current buffer are used. See also
+`consult-imenu' for more details. In order to search a subset of buffers,
+QUERY can be set to a plist according to `consult--buffer-query'."
   (interactive "P")
   (unless (keywordp (car-safe query))
     (setq query (list :sort 'alpha :mode major-mode
diff --git a/consult.el b/consult.el
index 0adf8443e4..fa0643f28a 100644
--- a/consult.el
+++ b/consult.el
@@ -78,20 +78,11 @@ The key must be either a string or a vector.
 This is the key representation accepted by `define-key'."
   :type '(choice key-sequence (const nil)))
 
-(defvar consult-project-root-function nil)
-(make-obsolete-variable
- 'consult-project-root-function
- "The function has been generalized to the `consult-project-function'"
- "0.15")
-
-(defcustom consult-project-function #'consult--default-project-function
-  "Function which takes a single symbol argument and returns project 
directories.
-
-The argument can be:
-- current-project: Return the current project directory.
-  The root directory is used by `consult-buffer' and `consult-grep'.
-- known-projects: Return the directories of known projects.
-  The list of known projects is used by `consult--source-project-root'."
+(defcustom consult-project-root-function
+  #'consult--project-root-default-function
+  "Function which returns project root directory.
+
+The root directory is used by `consult-buffer' and `consult-grep'."
   :type '(choice function (const nil)))
 
 (defcustom consult-async-refresh-delay 0.2
@@ -213,8 +204,7 @@ character, the *Completions* buffer and a few log buffers."
     consult--source-recent-file
     consult--source-bookmark
     consult--source-project-buffer
-    consult--source-project-recent-file
-    consult--source-project-root)
+    consult--source-project-recent-file)
   "Sources used by `consult-buffer'.
 See also `consult-project-buffer-sources'.
 See `consult--multi' for a description of the source data structure."
@@ -829,7 +819,7 @@ only the last two path components are shown.
 
 If DIR is a string, it is returned.
 If DIR is a true value, the user is asked.
-Then the `consult-project-function' is tried.
+Then the `consult-project-root-function' is tried.
 Otherwise the `default-directory' is returned."
   (let* ((dir
           (cond
@@ -853,37 +843,18 @@ Otherwise the `default-directory' is returned."
       (t (format "%s (%s): " prompt (consult--abbreviate-directory dir))))
      edir)))
 
-(defun consult--default-project-function (what)
-  "Return project directories depending on WHAT.
-See `consult-project-function' for WHAT."
-  (pcase-exhaustive what
-    ('current-project
-     (when-let (proj (project-current))
-       (cond
-        ((fboundp 'project-root) (project-root proj))
-        ((fboundp 'project-roots) (car (project-roots proj))))))
-    ('known-projects
-     (and (fboundp 'project-known-project-roots)
-          (project-known-project-roots)))))
+(defun consult--project-root-default-function ()
+  "Return project root directory."
+  (when-let (proj (project-current))
+    (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-function
-                       (funcall consult-project-function 'current-project)))
+  (when-let (root (and consult-project-root-function (funcall 
consult-project-root-function)))
     (expand-file-name root)))
 
-(defun consult--known-projects ()
-  "Return list of known project directories."
-  (let ((root (consult--project-root))
-        (dirs (sort
-               (mapcar #'abbreviate-file-name
-                       (funcall consult-project-function 'known-projects))
-               #'string<)))
-    (when root
-      (setq root (abbreviate-file-name root)
-            dirs (cons root (delete root dirs))))
-    dirs))
-
 (defun consult--project-name (dir)
   "Return the project name for DIR."
   (if (string-match "/\\([^/]+\\)/\\'" dir)
@@ -4000,6 +3971,49 @@ If NORECORD is non-nil, do not record the buffer switch 
in the buffer list."
     :state    ,#'consult--bookmark-state)
   "Bookmark candidate source for `consult-buffer'.")
 
+(defvar consult--source-project-buffer
+  `(:name     "Project Buffer"
+    :narrow   (?p . "Project")
+    :hidden   t
+    :category buffer
+    :face     consult-buffer
+    :history  buffer-name-history
+    :state    ,#'consult--buffer-state
+    :enabled  ,(lambda () consult-project-root-function)
+    :items
+    ,(lambda ()
+       (consult--buffer-query :sort 'visibility
+                              :directory 'project
+                              :as #'buffer-name)))
+  "Project buffer candidate source for `consult-buffer'.")
+
+(defvar consult--source-project-recent-file
+  `(:name     "Project File"
+    :narrow   (?p . "Project")
+    :hidden   t
+    :category file
+    :face     consult-file
+    :history  file-name-history
+    :state    ,#'consult--file-state
+    :enabled  ,(lambda () (and consult-project-root-function
+                               recentf-mode))
+    :items
+    ,(lambda ()
+      (when-let (root (consult--project-root))
+        (let ((len (length root))
+              (inv-root (propertize root 'invisible t))
+              (ht (consult--buffer-file-hash))
+              (filter (consult--regexp-filter consult-recent-file-filter)))
+          (mapcar (lambda (x)
+                    (concat inv-root (substring x len)))
+                  (seq-filter (lambda (x)
+                                (and (not (gethash x ht))
+                                     (string-prefix-p root x)
+                                     (not (and consult-recent-file-filter
+                                               (string-match-p filter x)))))
+                              recentf-list))))))
+  "Project file candidate source for `consult-buffer'.")
+
 (defvar consult--source-hidden-buffer
   `(:name     "Hidden Buffer"
     :narrow   32
@@ -4046,67 +4060,6 @@ If NORECORD is non-nil, do not record the buffer switch 
in the buffer list."
                              recentf-list)))))
   "Recent file candidate source for `consult-buffer'.")
 
-(defvar consult--source-project-buffer
-  `(:name     "Project Buffer"
-    :narrow   (?p . "Project")
-    :hidden   t
-    :category buffer
-    :face     consult-buffer
-    :history  buffer-name-history
-    :state    ,#'consult--buffer-state
-    :enabled  ,(lambda () consult-project-function)
-    :items
-    ,(lambda ()
-       (consult--buffer-query :sort 'visibility
-                              :directory 'project
-                              :as #'buffer-name)))
-  "Project buffer candidate source for `consult-buffer'.")
-
-(defvar consult--source-project-root
-  `(:name     "Project Root"
-    :narrow   (?p . "Project")
-    :hidden   t
-    :category file
-    :face     consult-file
-    :history  file-name-history
-    :state    ,#'consult--file-state
-    :enabled  ,(lambda () consult-project-function)
-    :items    ,#'consult--known-projects)
-  "Known project root directory source for `consult-buffer'.")
-
-(defvar consult--source-project-recent-file
-  `(:name     "Project File"
-    :narrow   (?p . "Project")
-    :hidden   t
-    :category file
-    :face     consult-file
-    :history  file-name-history
-    :state    ,#'consult--file-state
-    :enabled  ,(lambda () (and consult-project-function recentf-mode))
-    :items
-    ,(lambda ()
-      (when-let (root (consult--project-root))
-        (let ((len (length root))
-              (inv-root (propertize root 'invisible t))
-              (ht (consult--buffer-file-hash))
-              (filter (consult--regexp-filter consult-recent-file-filter)))
-          (mapcar (lambda (x)
-                    (concat inv-root (substring x len)))
-                  (seq-filter (lambda (x)
-                                (and (not (gethash x ht))
-                                     (string-prefix-p root x)
-                                     (not (and consult-recent-file-filter
-                                               (string-match-p filter x)))))
-                              recentf-list))))))
-  "Project file candidate source for `consult-buffer'.")
-
-;; Populate `consult-project-buffer-sources'.
-(setq consult-project-buffer-sources
-      (list
-       `(:hidden nil :narrow ?b ,@consult--source-project-buffer)
-       `(:hidden nil :narrow ?f ,@consult--source-project-recent-file)
-       `(:hidden nil :narrow ?r ,@consult--source-project-root)))
-
 ;;;###autoload
 (defun consult-buffer ()
   "Enhanced `switch-to-buffer' command with support for virtual buffers.
@@ -4115,7 +4068,7 @@ 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-unction' is used. See `consult-buffer-sources' and
+`consult-project-root-function' is used. See `consult-buffer-sources' and
 `consult--multi' for the configuration of the virtual buffer sources."
   (interactive)
   (when-let (buffer (consult--multi consult-buffer-sources
@@ -4129,6 +4082,12 @@ order to determine the project-specific files and 
buffers, the
     (unless (cdr buffer)
       (consult--buffer-action (car buffer)))))
 
+;; Populate `consult-project-buffer-sources'.
+(setq consult-project-buffer-sources
+      (list
+       `(:hidden nil :narrow ?b ,@consult--source-project-buffer)
+       `(:hidden nil :narrow ?f ,@consult--source-project-recent-file)))
+
 ;;;###autoload
 (defun consult-project-buffer ()
   "Enhanced `project-switch-to-buffer' command with support for virtual 
buffers.
@@ -4374,10 +4333,10 @@ Here we give a few example inputs:
 #word -- -C3        : Search for word, include 3 lines as context
 #first#second       : Search for first, quick filter for second.
 
-The symbol at point is added to the future history. If `consult-grep' is
-called interactively with a prefix argument, the user can specify the
-directory to search in. By default the project directory is used if
-`consult-project-function' is defined and returns a non-nil directory.
+The symbol at point is added to the future history. If `consult-grep'
+is called interactively with a prefix argument, the user can specify
+the directory to search in. By default the project directory is used
+if `consult-project-root-function' is defined and returns non-nil.
 Otherwise the `default-directory' is searched."
   (interactive "P")
   (consult--grep "Grep" #'consult--grep-builder dir initial))



reply via email to

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