Search for `src` finds only first occurrence
Using org-ql-query, I want to find which sections contain source blocks in a specified language.
(org-ql-query
:select '(list (substring-no-properties (org-get-heading t t))
(src :lang "ttl"))
... etc.
However, only the first source block is considered, i.e., the result is t only if a ttl block is the first source block under the heading.
Maybe the problem is in org-ql-defpred, which seems to return once a source block with non-matching language is encountered. Gives up too soon?
The following replacement for :body in (org-ql-defpred src ... seems to help. The lang string is pasted into the search string org-babel-src-block-regexp before searching.
:body
(catch 'return
(let ((my-org-babel-src-block-regexp
(if lang
;; insert lang in the search string
(and (string-match "\\\\([^\\]+\\\\)" org-babel-src-block-regexp 5)
(replace-match (concat "\\\\(" lang "\\\\)") nil nil org-babel-src-block-regexp))
org-babel-src-block-regexp)))
(save-excursion
(save-match-data
(when (re-search-forward my-org-babel-src-block-regexp (org-entry-end-position) t)
;; (when lang
;; (unless (string= lang (match-string 2))
;; (throw 'return nil)))
(if regexps
(let ((contents-beg (progn
(goto-char (match-beginning 0))
(forward-line 1)
(point)))
(contents-end (progn
(goto-char (match-end 0))
(point-at-bol))))
(cl-loop for re in regexps
do (goto-char contents-beg)
always (re-search-forward re contents-end t)))
;; No regexps to check: return non-nil.
t))))))
This change still leaves something to be desired. If you want to combine a search for a language with regexps in the source block, it will return nil if the regexps aren't present in the first block to match the language.
@johanwk Thanks for reporting this. The fix should be fairly simple, and I'll try to get to it soon.
Great!
I'm very impressed with how efficient and smart org-ql really is. Wish I had discovered it sooner.
Finally fixed. Thanks for your patience.