@@ -252,13 +252,28 @@ returns nil or non-nil."
252252 , action )))
253253 (_ (user-error " Invalid action form: %s" action))))
254254 (org-ql--today (ts-now))
255- (items (->> buffers
256- (--map (with-current-buffer it
257- (unless (derived-mode-p 'org-mode )
258- (user-error " Not an Org buffer: %s" (buffer-name )))
259- (org-ql--select-cached :query query :preamble preamble :preamble-case-fold preamble-case-fold
260- :predicate predicate :action action :narrow narrow)))
261- (-flatten-n 1 ))))
255+ (items (let (orig-fns)
256+ (unwind-protect
257+ (progn
258+ (--each org-ql-predicates
259+ ; ; Set predicate functions.
260+ (-let (((&plist :name :fn ) (cdr it)))
261+ ; ; Save original function.
262+ (push (list :name name :fn (symbol-function name)) orig-fns)
263+ ; ; Temporarily set new function definition.
264+ (fset name fn)))
265+ ; ; Run query on buffers.
266+ (->> buffers
267+ (--map (with-current-buffer it
268+ (unless (derived-mode-p 'org-mode )
269+ (user-error " Not an Org buffer: %s" (buffer-name )))
270+ (org-ql--select-cached :query query :preamble preamble :preamble-case-fold preamble-case-fold
271+ :predicate predicate :action action :narrow narrow)))
272+ (-flatten-n 1 )))
273+ (--each orig-fns
274+ ; ; Restore original function mappings.
275+ (-let (((&plist :name :fn ) it))
276+ (fset name fn)))))))
262277 ; ; Sort items
263278 (pcase sort
264279 (`nil items)
@@ -347,50 +362,32 @@ If NARROW is non-nil, buffer will not be widened."
347362 ; ; can't be used, so we do it manually (this is same as the equivalent `flet' expansion).
348363 ; ; Mappings are stored in the variable because it allows predicates to be defined with a
349364 ; ; macro, which allows documentation to be easily generated for them.
350-
351- ; ; MAYBE: Lift the `flet' -equivalent out of this function so it isn't done for each buffer.
352- (let (orig-fns)
353- (--each org-ql-predicates
354- ; ; Save original function mappings.
355- (let* ((it (cdr it))
356- (name (plist-get it :name )))
357- (push (list :name name :fn (symbol-function name)) orig-fns)))
358- (unwind-protect
359- (progn
360- (--each org-ql-predicates
361- ; ; Set predicate functions.
362- (let ((it (cdr it)))
363- (fset (plist-get it :name ) (plist-get it :fn ))))
364- ; ; Run query.
365- (save-excursion
366- (save-restriction
367- (unless narrow
368- (widen ))
369- (goto-char (point-min ))
370- (when (org-before-first-heading-p )
371- (outline-next-heading ))
372- (if (not (org-at-heading-p ))
373- (progn
374- ; ; No headings in buffer: return nil.
375- (unless (string-prefix-p " " (buffer-name ))
376- ; ; Not a special, hidden buffer: show message, because if a user accidentally
377- ; ; searches a buffer without headings, he might be confused.
378- (message " org-ql: No headings in buffer: %s " (current-buffer )))
379- nil )
380- ; ; Find matching entries.
381- ; ; TODO: Bind `case-fold-search' around the preamble loop.
382- (cond (preamble (cl-loop while (let ((case-fold-search preamble-case-fold))
383- (re-search-forward preamble nil t ))
384- do (outline-back-to-heading 'invisible-ok )
385- when (funcall predicate)
386- collect (funcall action)
387- do (outline-next-heading )))
388- (t (cl-loop when (funcall predicate)
389- collect (funcall action)
390- while (outline-next-heading ))))))))
391- (--each orig-fns
392- ; ; Restore original function mappings.
393- (fset (plist-get it :name ) (plist-get it :fn ))))))
365+ (save-excursion
366+ (save-restriction
367+ (unless narrow
368+ (widen ))
369+ (goto-char (point-min ))
370+ (when (org-before-first-heading-p )
371+ (outline-next-heading ))
372+ (if (not (org-at-heading-p ))
373+ (progn
374+ ; ; No headings in buffer: return nil.
375+ (unless (string-prefix-p " " (buffer-name ))
376+ ; ; Not a special, hidden buffer: show message, because if a user accidentally
377+ ; ; searches a buffer without headings, he might be confused.
378+ (message " org-ql: No headings in buffer: %s " (current-buffer )))
379+ nil )
380+ ; ; Find matching entries.
381+ ; ; TODO: Bind `case-fold-search' around the preamble loop.
382+ (cond (preamble (cl-loop while (let ((case-fold-search preamble-case-fold))
383+ (re-search-forward preamble nil t ))
384+ do (outline-back-to-heading 'invisible-ok )
385+ when (funcall predicate)
386+ collect (funcall action)
387+ do (outline-next-heading )))
388+ (t (cl-loop when (funcall predicate)
389+ collect (funcall action)
390+ while (outline-next-heading ))))))))
394391
395392; ;;;; Helpers
396393
0 commit comments