Change: Respect narrowing in org-ql-find

This allows searching within a narrowed buffer with org-ql-find.
Using "C-u" searches the widened buffer, while "C-u C-u" prompts to
select the buffers to search (which previously worked with just
"C-u").
This commit is contained in:
Adam Porter 2024-08-26 07:14:31 -05:00
parent b7d4856f92
commit ba49d0e3c7
2 changed files with 29 additions and 16 deletions

View file

@ -134,7 +134,7 @@ value, or nil."
;;;###autoload ;;;###autoload
(cl-defun org-ql-completing-read (cl-defun org-ql-completing-read
(buffers-files &key query-prefix query-filter (buffers-files &key query-prefix query-filter narrowp
(action #'org-ql-completing-read-action) (action #'org-ql-completing-read-action)
;; FIXME: Unused argument. ;; FIXME: Unused argument.
;; (annotate #'org-ql-completing-read-snippet) ;; (annotate #'org-ql-completing-read-snippet)
@ -145,6 +145,8 @@ value, or nil."
"Return marker at entry in BUFFERS-FILES selected with `org-ql'. "Return marker at entry in BUFFERS-FILES selected with `org-ql'.
PROMPT is shown to the user. PROMPT is shown to the user.
NARROWP is passed to `org-ql-select', which see.
QUERY-PREFIX may be a string to prepend to the query entered by QUERY-PREFIX may be a string to prepend to the query entered by
the user (e.g. use \"heading:\" to only search headings, easily the user (e.g. use \"heading:\" to only search headings, easily
creating a custom command that saves the user from having to type creating a custom command that saves the user from having to type
@ -323,6 +325,7 @@ single predicate)."
bow (or ,@query-tokens) (0+ (not space)) bow (or ,@query-tokens) (0+ (not space))
(optional (repeat 1 3 (0+ space) (repeat 1 15 (not space)))))))) (optional (repeat 1 3 (0+ space) (repeat 1 15 (not space))))))))
(org-ql-select buffers-files (org-ql--query-string-to-sexp input) (org-ql-select buffers-files (org-ql--query-string-to-sexp input)
:narrow narrowp
:action #'action)))) :action #'action))))
(unless (listp buffers-files) (unless (listp buffers-files)
;; Since we map across this argument, we ensure it's a list. ;; Since we map across this argument, we ensure it's a list.

View file

@ -54,14 +54,18 @@ See function `display-buffer'."
;;;; Commands ;;;; Commands
;;;###autoload ;;;###autoload
(cl-defun org-ql-find (buffers-files &key query-prefix query-filter (cl-defun org-ql-find (buffers-files &key query-prefix query-filter widen
(prompt "Find entry: ")) (prompt "Find entry: "))
"Go to an Org entry in BUFFERS-FILES selected by searching entries with `org-ql'. "Go to an Org entry in BUFFERS-FILES selected by searching entries with `org-ql'.
Interactively, search the buffers and files relevant to the Interactively, search the buffers and files relevant to the
current buffer (i.e. in `org-agenda-mode', the value of current buffer (i.e. in `org-agenda-mode', the value of
`org-ql-view-buffers-files' or `org-agenda-contributing-files'; `org-ql-view-buffers-files' or `org-agenda-contributing-files';
in `org-mode', that buffer). With universal prefix, select in `org-mode', that buffer).
multiple buffers to search with completion and PROMPT.
With one or more universal prefix arguments, WIDEN buffers before
searching (otherwise, respect any narrowing). With two universal
prefix arguments, select multiple buffers to search with
completion and PROMPT.
QUERY-PREFIX may be a string to prepend to the query (e.g. use QUERY-PREFIX may be a string to prepend to the query (e.g. use
\"heading:\" to only search headings, easily creating a custom \"heading:\" to only search headings, easily creating a custom
@ -72,11 +76,17 @@ types is filtered before execution (e.g. it could replace spaces
with commas to turn multiple tokens, which would normally be with commas to turn multiple tokens, which would normally be
treated as multiple predicates, into multiple arguments to a treated as multiple predicates, into multiple arguments to a
single predicate)." single predicate)."
(interactive (list (org-ql-find--buffers))) (interactive (list (org-ql-find--buffers
(let ((marker (org-ql-completing-read buffers-files :read-buffer-p (equal '(16) current-prefix-arg))
:widen current-prefix-arg))
(let ((marker (save-restriction
(when (and widen (equal (current-buffer) buffers-files))
(widen))
(org-ql-completing-read buffers-files
:narrowp (not widen)
:query-prefix query-prefix :query-prefix query-prefix
:query-filter query-filter :query-filter query-filter
:prompt prompt))) :prompt prompt))))
(set-buffer (or (buffer-base-buffer (marker-buffer marker)) (set-buffer (or (buffer-base-buffer (marker-buffer marker))
(marker-buffer marker))) (marker-buffer marker)))
(pop-to-buffer (current-buffer) org-ql-find-display-buffer-action) (pop-to-buffer (current-buffer) org-ql-find-display-buffer-action)
@ -195,15 +205,15 @@ multiple buffers to search with completion and PROMPT."
;;;; Functions ;;;; Functions
(defun org-ql-find--buffers () (cl-defun org-ql-find--buffers (&key read-buffer-p)
"Return list of buffers to search in. "Return buffer or list of buffers to search in.
In a mode derived from `org-agenda-mode', return the value of In a mode derived from `org-agenda-mode', return the value of
`org-ql-view-buffers-files' or `org-agenda-contributing-files'. `org-ql-view-buffers-files' or `org-agenda-contributing-files'.
In a mode derived from `org-mode', return the current buffer. In a mode derived from `org-mode', return the current buffer. If
When `current-prefix-arg', read a list of buffers in `org-mode' READ-BUFFER-P, read a list of buffers in `org-mode' with
with completion. To be used in `org-ql-find' commands' completion. To be used in `org-ql-find' commands' interactive
interactive forms." forms."
(if current-prefix-arg (if read-buffer-p
(mapcar #'get-buffer (mapcar #'get-buffer
(completing-read-multiple (completing-read-multiple
"Buffers: " "Buffers: "