Skip to content

Commit 772116a

Browse files
committed
WIP
See #3.
1 parent 9cce826 commit 772116a

File tree

1 file changed

+27
-15
lines changed

1 file changed

+27
-15
lines changed

org-ql.el

+27-15
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
;;;; Macros
1919

2020
(cl-defmacro org-ql (buffers-or-files pred-body &key (action-fn '#'identity)
21-
sort narrow markers)
21+
sort narrow markers match-next-fn)
2222
"Find entries in BUFFERS-OR-FILES that match PRED-BODY, and return the results of running ACTION-FN on each matching entry.
2323
2424
ACTION-FN should take a single argument, which will be the result
@@ -65,7 +65,8 @@ buffer."
6565
(list 'quote sort))
6666
(_
6767
;; Other expression to evaluate
68-
sort))))
68+
sort))
69+
:match-next-fn ,match-next-fn))
6970

7071
(defmacro org-ql--fmap (fns &rest body)
7172
(declare (indent defun) (debug (listp body)))
@@ -76,7 +77,7 @@ buffer."
7677

7778
;;;; Functions
7879

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)
8081
"FIXME: Add docstring."
8182
;; MAYBE: Set :narrow t for buffers and nil for files.
8283
(declare (indent defun))
@@ -95,7 +96,8 @@ buffer."
9596
(find-file-noselect it)
9697
(user-error "Can't open file: %s" it))))
9798
(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)))
99101
buffers-or-files))))
100102
(cl-typecase sort
101103
(list (org-ql--sort-by items sort))
@@ -120,7 +122,7 @@ Or, when possible, fix the problem."
120122
(org-ql--sanity-check-form (cdr elem)))
121123
else do (check elem))))
122124

123-
(cl-defun org-ql--filter-buffer (&key pred narrow)
125+
(cl-defun org-ql--filter-buffer (&key pred narrow match-next-fn)
124126
"Return positions of matching headings in current buffer.
125127
Headings should return non-nil for any ANY-PREDS and nil for all
126128
NONE-PREDS. If NARROW is non-nil, buffer will not be widened
@@ -140,16 +142,26 @@ first."
140142
(regexp #'org-ql--regexp-p)
141143
(level #'org-ql--level-p)
142144
(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))
153165

154166
;;;;; Predicates
155167

0 commit comments

Comments
 (0)