Skip to content

Commit

Permalink
consult--buffer-query: Add generalized directory argument
Browse files Browse the repository at this point in the history
  • Loading branch information
minad committed Jul 26, 2021
1 parent 07af37b commit eddc6d3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
2 changes: 1 addition & 1 deletion consult-imenu.el
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ same major mode as the current buffer are used. See also
(interactive)
(consult-imenu--select
(consult-imenu--all-items
(or (consult--buffer-query :project t
(or (consult--buffer-query :directory 'project
:mode major-mode
:sort 'alpha)
(list (current-buffer))))))
Expand Down
27 changes: 14 additions & 13 deletions consult.el
Original file line number Diff line number Diff line change
Expand Up @@ -3536,9 +3536,9 @@ The command supports previewing the currently selected theme."
nil)))
(nconc (nreverse hidden) buffers (list (current-buffer)))))

(cl-defun consult--buffer-query (&key sort (filter t) project mode as)
(cl-defun consult--buffer-query (&key sort (filter t) directory mode as)
"Buffer query function.
PROJECT can be set to t to consider only project-specific buffers.
DIRECTORY can either be project or a path.
SORT can be visibility, alpha or nil.
FILTER can be t, hidden or nil.
MODE can be a mode or a list of modes to restrict the returned buffers.
Expand All @@ -3547,11 +3547,14 @@ AS is a conversion function."
;; allocations. It is the backbone of most `consult-buffer' source. The
;; function supports filtering by various criteria which are used throughout
;; Consult.
(when-let (root (or (not project) (consult--project-root)))
(when-let (root (pcase-exhaustive directory
('project (consult--project-root))
('nil t)
((pred stringp) (expand-file-name directory))))
(let ((buffers (buffer-list)))
(when sort
(setq buffers (funcall (intern (format "consult--buffer-sort-%s" sort)) buffers)))
(when (or filter mode project as)
(when (or filter mode as (stringp root))
(let ((mode (if (listp mode) mode (list mode)))
(re (consult--regexp-filter consult-buffer-filter)))
(consult--keep! buffers
Expand All @@ -3563,14 +3566,12 @@ AS is a conversion function."
('nil t)
('hidden (string-match-p re (buffer-name it)))
('t (not (string-match-p re (buffer-name it)))))
(pcase-exhaustive project
('nil t)
('t
(when-let (dir (buffer-local-value 'default-directory it))
(string-prefix-p root
(if (and (/= 0 (length dir)) (eq (aref dir 0) ?/))
dir
(expand-file-name dir))))))
(or (not (stringp root))
(when-let (dir (buffer-local-value 'default-directory it))
(string-prefix-p root
(if (and (/= 0 (length dir)) (eq (aref dir 0) ?/))
dir
(expand-file-name dir)))))
(if as (funcall as it) it)))))
buffers)))

Expand Down Expand Up @@ -3620,7 +3621,7 @@ If NORECORD is non-nil, do not record the buffer switch in the buffer list."
:items
,(lambda ()
(consult--buffer-query :sort 'visibility
:project t
:directory 'project
:as #'buffer-name)))
"Project buffer candidate source for `consult-buffer'.")

Expand Down

0 comments on commit eddc6d3

Please sign in to comment.