@@ -225,19 +225,25 @@ necessary."
225
225
(org-ql-view--display :buffer buffer :header header :strings strings))))
226
226
227
227
;;;### autoload
228
- (defun org-ql-search-block (query )
229
- " Insert items for QUERY into current buffer.
230
- QUERY should be an `org-ql' query form. Intended to be used as a
231
- user-defined function in `org-agenda-custom-commands' . QUERY
232
- corresponds to the `match' item in the custom command form.
228
+ (defun org-ql-search-block (args )
229
+ " Insert items for ARGS into current buffer.
230
+ Intended to be used as a user-defined function in
231
+ `org-agenda-custom-commands' . ARGS corresponds to the `match'
232
+ item in the custom command form. It should be a list of
233
+ arguments which may be applied to `org-ql-select' , which see, but
234
+ not including its BUFFERS-FILES argument (which is supplied
235
+ through the Agenda). An additional `:header' keyword argument
236
+ may be supplied as a string, like that supplied to
237
+ `org-ql-view--display' .
233
238
234
239
Like other agenda block commands, it searches files returned by
235
240
function `org-agenda-files' . Inserts a newline after the block.
236
241
237
242
If `org-ql-block-header' is non-nil, it is used as the header
238
243
string for the block, otherwise a the header is formed
239
244
automatically from the query."
240
- (let (narrow-p old-beg old-end)
245
+ (pcase-let ((`(, query . ,(map :header :sort )) args)
246
+ (narrow-p) (old-beg) (old-end))
241
247
(when-let* ((from (pcase org-agenda-restrict
242
248
('nil (org-agenda-files nil 'ifmode ))
243
249
(_ (prog1 org-agenda-restrict
@@ -248,7 +254,7 @@ automatically from the query."
248
254
(narrow-to-region org-agenda-restrict-begin org-agenda-restrict-end))))))
249
255
(items (org-ql-select from query
250
256
:action 'element-with-markers
251
- :narrow narrow-p)))
257
+ :narrow narrow-p :sort sort )))
252
258
(when narrow-p
253
259
; ; Restore buffer's previous restrictions.
254
260
(with-current-buffer from
@@ -258,16 +264,25 @@ automatically from the query."
258
264
; ; FIXME: `org-agenda--insert-overriding-header' is from an Org version newer than
259
265
; ; I'm using. Should probably declare it as a minimum Org version after upgrading.
260
266
; ; (org-agenda--insert-overriding-header (or org-ql-block-header (org-ql-agenda--header-line-format from query)))
261
- (insert (org-add-props (or org-ql-block-header (org-ql-view--header-line-format
262
- :buffers-files from :query query))
267
+ ; ; FIXME: Should we really use `org-ql-block-header' AND `header' , or just one of them?
268
+ (insert (org-add-props (or org-ql-block-header header
269
+ (org-ql-view--header-line-format
270
+ :buffers-files from :query query))
263
271
nil 'face 'org-agenda-structure ) " \n " )
264
272
; ; Calling `org-agenda-finalize' should be unnecessary, because in a "series" agenda,
265
273
; ; `org-agenda-multi' is bound non-nil, in which case `org-agenda-finalize' does nothing.
266
274
; ; But we do call `org-agenda-finalize-entries' , which allows `org-super-agenda' to work.
267
- (->> items
268
- (-map #'org-ql-view--format-element )
269
- org-agenda-finalize-entries
270
- insert)
275
+ ; ; However, `org-agenda-finalize-entries' sorts entries with `org-entries-lessp' , which
276
+ ; ; overrides the sorting `org-ql' has already done, so we rebind `org-entries-lessp' to
277
+ ; ; prevent it from affecting sort order. (Ideally we would let `org-entries-lessp'
278
+ ; ; handle sorting, but that's not possible, because we can't add the `type' text property
279
+ ; ; it uses to sort entries, because the design of org-ql and org-agenda is fundamentally
280
+ ; ; different. So we have to do the sorting ourselves.)
281
+ (cl-letf (((symbol-function 'org-entries-lessp ) #'ignore ))
282
+ (->> items
283
+ (-map #'org-ql-view--format-element )
284
+ org-agenda-finalize-entries
285
+ insert))
271
286
(insert " \n " ))))
272
287
273
288
;;;### autoload
0 commit comments