From b3601cf0aec26d4fffc2454ecc72b557dce10082 Mon Sep 17 00:00:00 2001 From: Adam Porter Date: Sat, 18 Jan 2020 22:20:52 -0600 Subject: [PATCH] Add: (org-ql-block) Respect Org Agenda restrictions Fixes #84. Thanks to Ihor Radchenko (@yantar92) for reporting. --- README.org | 1 + org-ql-search.el | 50 +++++++++++++++++++++++++++++++----------------- org-ql.info | 26 +++++++++++++------------ 3 files changed, 47 insertions(+), 30 deletions(-) diff --git a/README.org b/README.org index abb5489..aa482e0 100644 --- a/README.org +++ b/README.org @@ -398,6 +398,7 @@ Expands into a call to ~org-ql-select~ with the same arguments. For convenience + Info manual. + 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. + Display a message when views are refreshed. (Thanks to [[https://github.com/xeijin][xeijin]].) ++ Respect Org Agenda restriction in =org-ql-block=. (Thanks to [[https://github.com/yantar92][Ihor Radchenko]] for reporting.) *Fixed* + Inherit file tags when =org-tag-inheritance= is enabled. (Fixes [[https://github.com/alphapapa/org-ql/issues/55][#55]]. Thanks to [[https://github.com/mskorzhinskiy][Mikhail Skorzhinskiy]].) diff --git a/org-ql-search.el b/org-ql-search.el index f79b254..fab0e95 100644 --- a/org-ql-search.el +++ b/org-ql-search.el @@ -231,24 +231,38 @@ function `org-agenda-files'. Inserts a newline after the block. If `org-ql-block-header' is non-nil, it is used as the header string for the block, otherwise a the header is formed automatically from the query." - (when-let* ((from (org-agenda-files nil 'ifmode)) - (items (org-ql-select from query - :action 'element-with-markers))) - ;; Not sure if calling the prepare function is necessary, but let's follow the pattern. - (org-agenda-prepare) - ;; FIXME: `org-agenda--insert-overriding-header' is from an Org version newer than - ;; I'm using. Should probably declare it as a minimum Org version after upgrading. - ;; (org-agenda--insert-overriding-header (or org-ql-block-header (org-ql-agenda--header-line-format from query))) - (insert (org-add-props (or org-ql-block-header (org-ql-view--header-line-format from query)) - nil 'face 'org-agenda-structure) "\n") - ;; Calling `org-agenda-finalize' should be unnecessary, because in a "series" agenda, - ;; `org-agenda-multi' is bound non-nil, in which case `org-agenda-finalize' does nothing. - ;; But we do call `org-agenda-finalize-entries', which allows `org-super-agenda' to work. - (->> items - (-map #'org-ql-view--format-element) - org-agenda-finalize-entries - insert) - (insert "\n"))) + (let (narrow-p old-beg old-end) + (when-let* ((from (pcase org-agenda-overriding-restriction + ('nil (org-agenda-files nil 'ifmode)) + ('file (get 'org-agenda-files 'org-restrict)) + ('subtree (prog1 org-agenda-restrict + (with-current-buffer org-agenda-restrict + ;; Narrow the buffer; remember to widen it later. + (setf old-beg (point-min) old-end (point-max) + narrow-p t) + (narrow-to-region org-agenda-restrict-begin org-agenda-restrict-end)))))) + (items (org-ql-select from query + :action 'element-with-markers + :narrow narrow-p))) + (when narrow-p + ;; Restore buffer's previous restrictions. + (with-current-buffer from + (narrow-to-region old-beg old-end))) + ;; Not sure if calling the prepare function is necessary, but let's follow the pattern. + (org-agenda-prepare) + ;; FIXME: `org-agenda--insert-overriding-header' is from an Org version newer than + ;; I'm using. Should probably declare it as a minimum Org version after upgrading. + ;; (org-agenda--insert-overriding-header (or org-ql-block-header (org-ql-agenda--header-line-format from query))) + (insert (org-add-props (or org-ql-block-header (org-ql-view--header-line-format from query)) + nil 'face 'org-agenda-structure) "\n") + ;; Calling `org-agenda-finalize' should be unnecessary, because in a "series" agenda, + ;; `org-agenda-multi' is bound non-nil, in which case `org-agenda-finalize' does nothing. + ;; But we do call `org-agenda-finalize-entries', which allows `org-super-agenda' to work. + (->> items + (-map #'org-ql-view--format-element) + org-agenda-finalize-entries + insert) + (insert "\n")))) ;;;###autoload (defalias 'org-ql-block 'org-ql-search-block) diff --git a/org-ql.info b/org-ql.info index 3254983..689d1e2 100644 --- a/org-ql.info +++ b/org-ql.info @@ -723,6 +723,8 @@ File: README.info, Node: 04-pre, Next: 032, Up: Changelog for custom Helm commands that search certain files. • Display a message when views are refreshed. (Thanks to xeijin (https://github.com/xeijin).) + • Respect Org Agenda restriction in org-ql-block. (Thanks to Ihor + Radchenko (https://github.com/yantar92) for reporting.) *Fixed* • Inherit file tags when org-tag-inheritance is enabled. (Fixes #55 @@ -1028,18 +1030,18 @@ Node: Agenda-like views18025 Node: Listing / acting-on results19430 Node: Changelog24032 Node: 04-pre24569 -Node: 03226689 -Node: 03127070 -Node: 0327265 -Node: 02330238 -Node: 02230464 -Node: 02130730 -Node: 0230927 -Node: 0134960 -Node: Notes35059 -Node: Comparison with Org Agenda searches35221 -Node: org-sidebar36092 -Node: License36371 +Node: 03226822 +Node: 03127203 +Node: 0327398 +Node: 02330371 +Node: 02230597 +Node: 02130863 +Node: 0231060 +Node: 0135093 +Node: Notes35192 +Node: Comparison with Org Agenda searches35354 +Node: org-sidebar36225 +Node: License36504  End Tag Table