Skip to content

Commit b3601cf

Browse files
committed
Add: (org-ql-block) Respect Org Agenda restrictions
Fixes #84. Thanks to Ihor Radchenko (@yantar92) for reporting.
1 parent 0a7e4f3 commit b3601cf

File tree

3 files changed

+47
-30
lines changed

3 files changed

+47
-30
lines changed

README.org

+1
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,7 @@ Expands into a call to ~org-ql-select~ with the same arguments. For convenience
398398
+ Info manual.
399399
+ 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.
400400
+ Display a message when views are refreshed. (Thanks to [[https://github.com/xeijin][xeijin]].)
401+
+ Respect Org Agenda restriction in =org-ql-block=. (Thanks to [[https://github.com/yantar92][Ihor Radchenko]] for reporting.)
401402

402403
*Fixed*
403404
+ 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]].)

org-ql-search.el

+32-18
Original file line numberDiff line numberDiff line change
@@ -231,24 +231,38 @@ function `org-agenda-files'. Inserts a newline after the block.
231231
If `org-ql-block-header' is non-nil, it is used as the header
232232
string for the block, otherwise a the header is formed
233233
automatically from the query."
234-
(when-let* ((from (org-agenda-files nil 'ifmode))
235-
(items (org-ql-select from query
236-
:action 'element-with-markers)))
237-
;; Not sure if calling the prepare function is necessary, but let's follow the pattern.
238-
(org-agenda-prepare)
239-
;; FIXME: `org-agenda--insert-overriding-header' is from an Org version newer than
240-
;; I'm using. Should probably declare it as a minimum Org version after upgrading.
241-
;; (org-agenda--insert-overriding-header (or org-ql-block-header (org-ql-agenda--header-line-format from query)))
242-
(insert (org-add-props (or org-ql-block-header (org-ql-view--header-line-format from query))
243-
nil 'face 'org-agenda-structure) "\n")
244-
;; Calling `org-agenda-finalize' should be unnecessary, because in a "series" agenda,
245-
;; `org-agenda-multi' is bound non-nil, in which case `org-agenda-finalize' does nothing.
246-
;; But we do call `org-agenda-finalize-entries', which allows `org-super-agenda' to work.
247-
(->> items
248-
(-map #'org-ql-view--format-element)
249-
org-agenda-finalize-entries
250-
insert)
251-
(insert "\n")))
234+
(let (narrow-p old-beg old-end)
235+
(when-let* ((from (pcase org-agenda-overriding-restriction
236+
('nil (org-agenda-files nil 'ifmode))
237+
('file (get 'org-agenda-files 'org-restrict))
238+
('subtree (prog1 org-agenda-restrict
239+
(with-current-buffer org-agenda-restrict
240+
;; Narrow the buffer; remember to widen it later.
241+
(setf old-beg (point-min) old-end (point-max)
242+
narrow-p t)
243+
(narrow-to-region org-agenda-restrict-begin org-agenda-restrict-end))))))
244+
(items (org-ql-select from query
245+
:action 'element-with-markers
246+
:narrow narrow-p)))
247+
(when narrow-p
248+
;; Restore buffer's previous restrictions.
249+
(with-current-buffer from
250+
(narrow-to-region old-beg old-end)))
251+
;; Not sure if calling the prepare function is necessary, but let's follow the pattern.
252+
(org-agenda-prepare)
253+
;; FIXME: `org-agenda--insert-overriding-header' is from an Org version newer than
254+
;; I'm using. Should probably declare it as a minimum Org version after upgrading.
255+
;; (org-agenda--insert-overriding-header (or org-ql-block-header (org-ql-agenda--header-line-format from query)))
256+
(insert (org-add-props (or org-ql-block-header (org-ql-view--header-line-format from query))
257+
nil 'face 'org-agenda-structure) "\n")
258+
;; Calling `org-agenda-finalize' should be unnecessary, because in a "series" agenda,
259+
;; `org-agenda-multi' is bound non-nil, in which case `org-agenda-finalize' does nothing.
260+
;; But we do call `org-agenda-finalize-entries', which allows `org-super-agenda' to work.
261+
(->> items
262+
(-map #'org-ql-view--format-element)
263+
org-agenda-finalize-entries
264+
insert)
265+
(insert "\n"))))
252266

253267
;;;###autoload
254268
(defalias 'org-ql-block 'org-ql-search-block)

org-ql.info

+14-12
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,8 @@ File: README.info, Node: 04-pre, Next: 032, Up: Changelog
723723
for custom Helm commands that search certain files.
724724
• Display a message when views are refreshed. (Thanks to xeijin
725725
(https://github.com/xeijin).)
726+
• Respect Org Agenda restriction in org-ql-block. (Thanks to Ihor
727+
Radchenko (https://github.com/yantar92) for reporting.)
726728

727729
*Fixed*
728730
• Inherit file tags when org-tag-inheritance is enabled. (Fixes #55
@@ -1028,18 +1030,18 @@ Node: Agenda-like views18025
10281030
Node: Listing / acting-on results19430
10291031
Node: Changelog24032
10301032
Node: 04-pre24569
1031-
Node: 03226689
1032-
Node: 03127070
1033-
Node: 0327265
1034-
Node: 02330238
1035-
Node: 02230464
1036-
Node: 02130730
1037-
Node: 0230927
1038-
Node: 0134960
1039-
Node: Notes35059
1040-
Node: Comparison with Org Agenda searches35221
1041-
Node: org-sidebar36092
1042-
Node: License36371
1033+
Node: 03226822
1034+
Node: 03127203
1035+
Node: 0327398
1036+
Node: 02330371
1037+
Node: 02230597
1038+
Node: 02130863
1039+
Node: 0231060
1040+
Node: 0135093
1041+
Node: Notes35192
1042+
Node: Comparison with Org Agenda searches35354
1043+
Node: org-sidebar36225
1044+
Node: License36504
10431045

10441046
End Tag Table
10451047

0 commit comments

Comments
 (0)