org-ql icon indicating copy to clipboard operation
org-ql copied to clipboard

Search for `src` finds only first occurrence

Open johanwk opened this issue 3 years ago • 4 comments

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?

johanwk avatar Sep 14 '22 11:09 johanwk

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))))))

johanwk avatar Sep 14 '22 14:09 johanwk

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 avatar Sep 14 '22 14:09 johanwk

@johanwk Thanks for reporting this. The fix should be fairly simple, and I'll try to get to it soon.

alphapapa avatar Sep 15 '22 21:09 alphapapa

Great!

I'm very impressed with how efficient and smart org-ql really is. Wish I had discovered it sooner.

johanwk avatar Sep 20 '22 09:09 johanwk

Finally fixed. Thanks for your patience.

alphapapa avatar Mar 09 '23 07:03 alphapapa