18
18
; ;;; Macros
19
19
20
20
(cl-defmacro org-ql (buffers-or-files pred-body &key (action-fn '#'identity )
21
- sort narrow markers )
21
+ sort narrow markers match-next-fn )
22
22
" Find entries in BUFFERS-OR-FILES that match PRED-BODY, and return the results of running ACTION-FN on each matching entry.
23
23
24
24
ACTION-FN should take a single argument, which will be the result
@@ -65,7 +65,8 @@ buffer."
65
65
(list 'quote sort))
66
66
(_
67
67
; ; Other expression to evaluate
68
- sort))))
68
+ sort))
69
+ :match-next-fn , match-next-fn ))
69
70
70
71
(defmacro org-ql--fmap (fns &rest body )
71
72
(declare (indent defun ) (debug (listp body)))
@@ -76,7 +77,7 @@ buffer."
76
77
77
78
; ;;; Functions
78
79
79
- (cl-defun org-ql--query (buffers-or-files pred &key (action-fn #'identity ) narrow sort )
80
+ (cl-defun org-ql--query (buffers-or-files pred &key (action-fn #'identity ) narrow sort match-next-fn )
80
81
" FIXME: Add docstring."
81
82
; ; MAYBE: Set :narrow t for buffers and nil for files.
82
83
(declare (indent defun ))
@@ -95,7 +96,8 @@ buffer."
95
96
(find-file-noselect it)
96
97
(user-error " Can't open file: %s" it))))
97
98
(mapcar action-fn
98
- (org-ql--filter-buffer :pred pred :narrow narrow)))
99
+ (org-ql--filter-buffer :pred pred :narrow narrow
100
+ :match-next-fn match-next-fn)))
99
101
buffers-or-files))))
100
102
(cl-typecase sort
101
103
(list (org-ql--sort-by items sort))
@@ -120,7 +122,7 @@ Or, when possible, fix the problem."
120
122
(org-ql--sanity-check-form (cdr elem)))
121
123
else do (check elem))))
122
124
123
- (cl-defun org-ql--filter-buffer (&key pred narrow )
125
+ (cl-defun org-ql--filter-buffer (&key pred narrow match-next-fn )
124
126
" Return positions of matching headings in current buffer.
125
127
Headings should return non-nil for any ANY-PREDS and nil for all
126
128
NONE-PREDS. If NARROW is non-nil, buffer will not be widened
@@ -140,16 +142,26 @@ first."
140
142
(regexp #'org-ql--regexp-p )
141
143
(level #'org-ql--level-p )
142
144
(org-back-to-heading #'outline-back-to-heading ))
143
- (save-excursion
144
- (save-restriction
145
- (unless narrow
146
- (widen ))
147
- (goto-char (point-min ))
148
- (when (org-before-first-heading-p )
149
- (outline-next-heading ))
150
- (cl-loop when (funcall pred)
151
- collect (org-element-headline-parser (line-end-position ))
152
- while (outline-next-heading ))))))
145
+ (let ((match-next-fn (or match-next-fn #'outline-next-heading )))
146
+ (save-excursion
147
+ (save-restriction
148
+ (unless narrow
149
+ (widen ))
150
+ (goto-char (point-min ))
151
+ (when (org-before-first-heading-p )
152
+ (outline-next-heading ))
153
+ (cl-loop if (funcall pred)
154
+ collect (org-element-headline-parser (line-end-position ))
155
+ and do (funcall match-next-fn)
156
+ else do (outline-next-heading )
157
+ until (eobp )))))))
158
+
159
+ (defun org-ql--outline-next-heading-same-level ()
160
+ " FIXME"
161
+ (cl-loop with level = (org-outline-level )
162
+ while (outline-next-heading )
163
+ when (= level (org-outline-level ))
164
+ return t ))
153
165
154
166
; ;;;; Predicates
155
167
0 commit comments