alphapapa / org-ql

A searching tool for Org-mode, including custom query languages, commands, saved searches and agenda-like views, etc.
GNU General Public License v3.0
1.35k stars 104 forks source link

I can't seem to get heading-regexp to work with "starts-with" (^) #340

Open talwrii opened 1 year ago

talwrii commented 1 year ago

This is call gives me results

(org-ql-search (buffer-file-name) '(heading-regexp  "Emacs"))

as does this

(org-ql-search (buffer-file-name) '(heading-regexp  "Emacs$"))

and this

(org-ql-search (buffer-file-name) '(heading-regexp  "Emacs$"))

but all of the following give me no results:

(org-ql-search (buffer-file-name) '(heading-regexp  "^Emacs$"))
(org-ql-search (buffer-file-name) '(heading-regexp  "^.*Emacs$"))
talwrii commented 1 year ago

Having a look at the code, this line seems to be the problem. Stuff is added to the beginning of the regexp so that "^" is no longer at the beginning.

https://github.com/alphapapa/org-ql/blob/4c1a4b169f54d37ce541902c0ae5043759ef9d9b/org-ql.el#L1490

alphapapa commented 1 year ago

Hello,

Thanks for pointing this out. Yes, it appears that the regexp optimization for this predicate interferes with these regexp metacharacters. Ideally they should work "naturally," according to the user's expectations, but they currently don't.

This can probably fixed, but it will require careful design and testing, and maybe also documentation of any limitations that remain.

In the meantime, if you require this functionality, you can disable the use of regexp "preambles" by binding org-ql-use-preamble to nil around your search code, which will allow the regexp to be tested only by the predicate's body form: https://github.com/alphapapa/org-ql/blob/4c1a4b169f54d37ce541902c0ae5043759ef9d9b/org-ql.el#L1502-L1504

talwrii commented 1 year ago

Ah awesome! That seems like a useful workaround. This was already a bit of a hack to search relative to the current a given node (often the current one). As I've looked at the code a little it doesn't seem like it would be that difficult implement this as a predicate so I might have a go doing that.