Add: Adjust search parameters in view buffers

This commit is contained in:
Adam Porter 2019-10-07 18:20:29 -05:00
parent 7fa0d83a54
commit 553c22c3ac
4 changed files with 128 additions and 90 deletions

View file

@ -105,7 +105,7 @@ Read ~QUERY~ and search with ~org-ql~. Interactively, prompt for these variable
~SORT~: One or a list of ~org-ql~ sorting functions, like ~date~ or ~priority~. ~SORT~: One or a list of ~org-ql~ sorting functions, like ~date~ or ~priority~.
*Bindings:* Keys bound in results buffer. *Bindings:* Keys bound in results buffer.
+ =g=: Refresh results. + =g=: Refresh results. With prefix, prompt to adjust search parameters.
+ =C-x C-s=: Save query to variable ~org-ql-views~ (accessible with command ~org-ql-view~). + =C-x C-s=: Save query to variable ~org-ql-views~ (accessible with command ~org-ql-view~).
*Note:* The view buffer is currently put in ~org-agenda-mode~, which means that /some/ Org Agenda commands work, such as jumping to entries and changing item priorities (without necessarily updating the view). This feature is experimental and not guaranteed to work correctly with all commands. (It works to the extent it does because the appropriate text properties are placed on each item, imitating an Agenda buffer.) *Note:* The view buffer is currently put in ~org-agenda-mode~, which means that /some/ Org Agenda commands work, such as jumping to entries and changing item priorities (without necessarily updating the view). This feature is experimental and not guaranteed to work correctly with all commands. (It works to the extent it does because the appropriate text properties are placed on each item, imitating an Agenda buffer.)
@ -376,6 +376,7 @@ Expands into a call to ~org-ql-select~ with the same arguments. For convenience
+ Predicates =outline-path= (alias =olp=) and =outline-path-segment= (alias =olps=). + Predicates =outline-path= (alias =olp=) and =outline-path-segment= (alias =olps=).
+ Info manual. + Info manual.
+ Command ~org-ql-search~ can search files in ~org-directory~; customization options are available in the ~org-ql-search~ group. + Command ~org-ql-search~ can search files in ~org-directory~; customization options are available in the ~org-ql-search~ group.
+ Command ~org-ql-view-refresh~ can be called with a prefix argument to adjust search parameters.
+ Function ~helm-org-ql-source~, which returns a Helm source that searches given buffers/files with ~helm-org-ql~. It can be used for custom Helm commands that search certain files. + Function ~helm-org-ql-source~, which returns a Helm source that searches given buffers/files with ~helm-org-ql~. It can be used for custom Helm commands that search certain files.
*Internal* *Internal*

View file

@ -139,28 +139,39 @@ display the results. By default, the value of
`org-ql-view-buffer' is used, and a new buffer is created if `org-ql-view-buffer' is used, and a new buffer is created if
necessary." necessary."
(declare (indent defun)) (declare (indent defun))
(interactive (list (pcase-exhaustive (completing-read "Buffers/Files: " (interactive (list (if (and org-ql-view-buffers-files
(list 'buffer 'agenda 'directory 'all) (bufferp org-ql-view-buffers-files))
nil t) ;; Buffers can't be input by name, so if the default value is a buffer, just use it.
((or "" "buffer") (current-buffer)) ;; TODO: Find a way to fix this.
("agenda" (org-agenda-files)) org-ql-view-buffers-files
("all" (--select (equal (buffer-local-value 'major-mode it) 'org-mode) (pcase-exhaustive (completing-read "Buffers/Files: "
(buffer-list))) (list 'buffer 'agenda 'directory 'all)
("directory" (org-ql-search-directories-files)) nil nil (when org-ql-view-buffers-files
((and form (guard (rx bos "("))) (-flatten (eval (read form)))) (prin1-to-string (cons 'list org-ql-view-buffers-files))))
(else (s-split (rx (1+ space)) else))) ((or "" "buffer") (current-buffer))
(read-string "Query: ") ("agenda" (org-agenda-files))
:narrow (eq current-prefix-arg '(4)) ("all" (--select (equal (buffer-local-value 'major-mode it) 'org-mode)
(buffer-list)))
("directory" (org-ql-search-directories-files))
((and form (guard (rx bos "("))) (-flatten (eval (read form))))
(else (s-split (rx (1+ space)) else))))
(read-string "Query: " (when org-ql-view-query
(format "%S" org-ql-view-query)))
:narrow (or org-ql-view-narrow (eq current-prefix-arg '(4)))
:super-groups (when (bound-and-true-p org-super-agenda-auto-selector-keywords) :super-groups (when (bound-and-true-p org-super-agenda-auto-selector-keywords)
(pcase (completing-read "Group by: " (let ((keywords (cl-loop for type in org-super-agenda-auto-selector-keywords
(append (list "Don't group" collect (substring (symbol-name type) 6))))
"Global super-groups") (pcase (completing-read "Group by: "
(cl-loop for type in org-super-agenda-auto-selector-keywords (append (list "Don't group"
collect (substring (symbol-name type) 6))) "Global super-groups")
nil t) keywords)
("Global super-groups" org-super-agenda-groups) nil nil (when org-ql-view-super-groups
((or "" "Don't group") nil) (format "%S" org-ql-view-super-groups)))
(property (list (list (intern (concat ":auto-" property))))))) ("Global super-groups" org-super-agenda-groups)
((or "" "Don't group") nil)
((and keyword (guard (member keyword keywords)))
(list (list (intern (concat ":auto-" keyword)))))
(else (read else)))))
:sort (pcase (completing-read "Sort by: " :sort (pcase (completing-read "Sort by: "
(list "Don't sort" (list "Don't sort"
"date" "date"
@ -168,35 +179,42 @@ necessary."
"priority" "priority"
"scheduled" "scheduled"
"todo") "todo")
nil t) nil t (when org-ql-view-sort
(prin1-to-string org-ql-view-sort)))
((or "" "Don't sort") nil) ((or "" "Don't sort") nil)
(sort (intern sort))))) (sort (intern sort)))))
(let* ((query (cl-etypecase query ;; NOTE: Using `with-temp-buffer' is a hack to work around the fact that `make-local-variable'
(string (if (string-match-p (rx bos (1+ alpha) ":") query) ;; does not work reliably from inside a `let' form when the target buffer is current on entry
;; to or exit from the `let', even though `make-local-variable' is actually done in
;; `org-ql-view--display'. So we do all this within a temp buffer, which works around it.
(with-temp-buffer
(let* ((query (cl-etypecase query
(string (if (or (string-prefix-p "(" query)
(string-prefix-p "\"" query))
;; Read sexp query.
(read query)
;; Parse non-sexp query into sexp query. ;; Parse non-sexp query into sexp query.
(org-ql--plain-query query) (org-ql--plain-query query)))
;; Read sexp query. (list query)))
(read query))) (results (org-ql-select buffers-files query
(list query))) :action 'element-with-markers
(results (org-ql-select buffers-files query :narrow narrow
:action 'element-with-markers :sort sort))
:narrow narrow (strings (-map #'org-ql-view--format-element results))
:sort sort)) (buffer (or buffer (format "%s %s*" org-ql-view-buffer-name-prefix (or title query))))
(strings (-map #'org-ql-view--format-element results)) (header (org-ql-view--header-line-format buffers-files query title))
(buffer (or buffer (format "%s %s*" org-ql-view-buffer-name-prefix (or title query)))) ;; Bind variables for `org-ql-view--display' to set.
(header (org-ql-view--header-line-format buffers-files query title)) (org-ql-view-buffers-files buffers-files)
;; Bind variables for `org-ql-view--display' to set. (org-ql-view-query query)
(org-ql-view-buffers-files buffers-files) (org-ql-view-sort sort)
(org-ql-view-query query) (org-ql-view-narrow narrow)
(org-ql-view-sort sort) (org-ql-view-super-groups super-groups)
(org-ql-view-narrow narrow) (org-ql-view-title title))
(org-ql-view-super-groups super-groups) (when super-groups
(org-ql-view-title title)) (let ((org-super-agenda-groups super-groups))
(when super-groups (setf strings (org-super-agenda--group-items strings))))
(let ((org-super-agenda-groups super-groups)) (org-ql-view--display :buffer buffer :header header
(setf strings (org-super-agenda--group-items strings)))) :string (s-join "\n" strings)))))
(org-ql-view--display :buffer buffer :header header
:string (s-join "\n" strings))))
(defun org-ql-search-block (query) (defun org-ql-search-block (query)
"Insert items for QUERY into current buffer. "Insert items for QUERY into current buffer.

View file

@ -266,18 +266,27 @@ TYPE may be `ts', `ts-active', `ts-inactive', `clocked', or
'face '(:weight bold :inherit highlight)) 'face '(:weight bold :inherit highlight))
(org-ql-view key)))) (org-ql-view key))))
(defun org-ql-view-refresh () (defun org-ql-view-refresh (&optional prompt)
"Refresh current `org-ql-search' buffer." "Refresh current `org-ql-search' buffer.
(interactive) If PROMPT is non-nil (interactively, with prefix), prompt to
(let ((current-line (buffer-substring-no-properties (line-beginning-position) (line-end-position))) update search arguments."
(old-pos (point))) (interactive "P")
(org-ql-search org-ql-view-buffers-files (let* ((current-line (buffer-substring-no-properties (line-beginning-position) (line-end-position)))
org-ql-view-query (old-pos (point))
:sort org-ql-view-sort (defaults (list org-ql-view-buffers-files
:narrow org-ql-view-narrow org-ql-view-query
:super-groups org-ql-view-super-groups :sort org-ql-view-sort
:title org-ql-view-title :narrow org-ql-view-narrow
:buffer (current-buffer)) :super-groups org-ql-view-super-groups
:title org-ql-view-title))
(org-ql-view-buffer (current-buffer)))
(if prompt
(call-interactively #'org-ql-search)
(apply #'org-ql-search defaults))
;; Now in the results buffer.
(rename-buffer (format "%s %s*"
org-ql-view-buffer-name-prefix
(or org-ql-view-title org-ql-view-query)))
(goto-char (point-min)) (goto-char (point-min))
(or (when (search-forward current-line nil t) (or (when (search-forward current-line nil t)
(beginning-of-line)) (beginning-of-line))
@ -338,17 +347,24 @@ subsequent refreshing of the buffer: `org-ql-view-buffers-files',
`org-ql-view-query', `org-ql-view-sort', `org-ql-view-narrow', `org-ql-view-query', `org-ql-view-sort', `org-ql-view-narrow',
`org-ql-view-super-groups', `org-ql-title.'" `org-ql-view-super-groups', `org-ql-title.'"
(declare (indent defun)) (declare (indent defun))
(let* ((buffer (cl-etypecase buffer (let* ((vars (list 'org-ql-view-buffers-files 'org-ql-view-query
'org-ql-view-sort 'org-ql-view-narrow
'org-ql-view-super-groups 'org-ql-view-title))
;; Save the values of variables which are set buffer-locally in the
;; results buffer, which we want to override and set buffer-locally again.
(vals (cl-loop for symbol in vars
collect (cons symbol (symbol-value symbol))))
(buffer (cl-etypecase buffer
(string (org-ql-view--buffer buffer)) (string (org-ql-view--buffer buffer))
(null (org-ql-view--buffer buffer)) (null (org-ql-view--buffer buffer))
(buffer buffer)))) (buffer buffer))))
(with-current-buffer buffer (with-current-buffer buffer
(use-local-map org-ql-view-map) (use-local-map org-ql-view-map)
;; Prepare buffer, saving data for refreshing. ;; Prepare buffer, saving data for refreshing.
(cl-loop for symbol in (list 'org-ql-view-buffers-files 'org-ql-view-query (cl-loop for symbol in vars
'org-ql-view-sort 'org-ql-view-narrow do (progn
'org-ql-view-super-groups 'org-ql-view-title) (kill-local-variable symbol)
do (set (make-local-variable symbol) (symbol-value symbol))) (set (make-local-variable symbol) (alist-get symbol vals nil nil #'equal))))
(setf header-line-format header) (setf header-line-format header)
;; Clear buffer, insert entries, etc. ;; Clear buffer, insert entries, etc.
(let ((inhibit-read-only t)) (let ((inhibit-read-only t))

View file

@ -216,7 +216,8 @@ Interactively, with prefix, leave narrowed.
priority. priority.
*Bindings:* Keys bound in results buffer. *Bindings:* Keys bound in results buffer.
• g: Refresh results. • g: Refresh results. With prefix, prompt to adjust search
parameters.
• C-x C-s: Save query to variable org-ql-views (accessible with • C-x C-s: Save query to variable org-ql-views (accessible with
command org-ql-view). command org-ql-view).
@ -692,6 +693,8 @@ File: README.info, Node: 04-pre, Next: 03, Up: Changelog
• Info manual. • Info manual.
• Command org-ql-search can search files in org-directory; • Command org-ql-search can search files in org-directory;
customization options are available in the org-ql-search group. customization options are available in the org-ql-search group.
• Command org-ql-view-refresh can be called with a prefix argument
to adjust search parameters.
• Function helm-org-ql-source, which returns a Helm source that • Function helm-org-ql-source, which returns a Helm source that
searches given buffers/files with helm-org-ql. It can be used searches given buffers/files with helm-org-ql. It can be used
for custom Helm commands that search certain files. for custom Helm commands that search certain files.
@ -950,30 +953,30 @@ Node: Quelpa2241
Node: Usage2684 Node: Usage2684
Node: Commands3594 Node: Commands3594
Node: org-ql-search3823 Node: org-ql-search3823
Node: helm-org-ql5416 Node: helm-org-ql5471
Node: org-ql-view5828 Node: org-ql-view5883
Node: org-ql-view-sidebar6026 Node: org-ql-view-sidebar6081
Node: org-ql-view-recent-items6382 Node: org-ql-view-recent-items6437
Node: org-ql-sparse-tree6866 Node: org-ql-sparse-tree6921
Node: Queries7666 Node: Queries7721
Node: Non-sexp query syntax8508 Node: Non-sexp query syntax8563
Node: Predicates10008 Node: Predicates10063
Node: Date/time predicates14865 Node: Date/time predicates14920
Node: Functions / Macros17500 Node: Functions / Macros17555
Node: Agenda-like views17687 Node: Agenda-like views17742
Node: Listing / acting-on results19092 Node: Listing / acting-on results19147
Node: Changelog23694 Node: Changelog23749
Node: 04-pre24201 Node: 04-pre24256
Node: 0324862 Node: 0325029
Node: 02327838 Node: 02328005
Node: 02228064 Node: 02228231
Node: 02128330 Node: 02128497
Node: 0228527 Node: 0228694
Node: 0132560 Node: 0132727
Node: Notes32659 Node: Notes32826
Node: Comparison with Org Agenda searches32821 Node: Comparison with Org Agenda searches32988
Node: org-sidebar33692 Node: org-sidebar33859
Node: License33971 Node: License34138
 
End Tag Table End Tag Table