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

org-ql link query does not find links whose description contains brackets #283

Open exot opened 2 years ago

exot commented 2 years ago

Hey,

first of all: many thanks for org-ql! I am enjoying the way it let's me make queries on my org files and dynamically extract reports on complex tasks I have to take care of. That really takes my workflow to a new level :)

One of my main use cases of org-ql is to find items linking back to the current item (thus implementing some kind of "backlink listings"). I am mostly using the link query for this, but recently I discovered that this query did not find items I had linked back to the item at point. Going through the code I found that org-ql--link-regexp contains the following definition (among others):

      ((no-desc
        (match) (rx-to-string `(seq (or bol (1+ blank))
                                    "[[" (0+ (not (any "]"))) (regexp ,match) (0+ (not (any "]")))
                                    "]]")))
       (match-both
        (description target)
        (rx-to-string `(seq (or bol (1+ blank))
                            "[[" (0+ (not (any "]"))) (regexp ,target) (0+ (not (any "]")))
                            "][" (0+ (not (any "]"))) (regexp ,description) (0+ (not (any "]")))
                            "]]")))
       ;; Note that these actually allow empty descriptions
       ;; or targets, depending on what they are matching.
       (match-desc
        (match) (rx-to-string `(seq (or bol (1+ blank))
                                    "[[" (0+ (not (any "]")))
                                    "][" (0+ (not (any "]"))) (regexp ,match) (0+ (not (any "]")))
                                    "]]")))
       (match-target
        (match) (rx-to-string `(seq (or bol (1+ blank))
                                    "[[" (0+ (not (any "]"))) (regexp ,match) (0+ (not (any "]")))
                                    "][" (0+ (not (any "]")))
                                    "]]"))))

Those regular expressions all exclude brackets in the description part (when they consider the description part at all). I think this may lead to certain links not being considered when their descriptions contain either inactive timestamps or counters (i.e. "[2/3]"), both of which I am making heavy use of. I have tried to come up with some fix for this, but couldn't find a real solution (I repeatedly ran into errors like org-ql--select: Stack overflow in regexp matcher when replacing (0+ (not (any "]"))) with things like (regexp ".*")). Currently, I am have locally patched this function to ignore the description part altogether, as I am only searching for "id:…" targets.

Is my understanding correct that the regular expressions should allow brackets in the description part, or am I missing something?

Thanks again!

  Daniel

alphapapa commented 2 years ago

Hi Daniel,

Thanks for the kind words. I'm glad to hear that org-ql is useful to you.

Thanks also for reporting this problem. I thought that Org did not allow brackets in link descriptions, and AFAIK it didn't in the past (they were converted to { }). But I just tested on Org 9.5.2, and rather than being converted to braces, they are escaped in the URI part, and left as-is in the description part, like:

[[*Heading \[argh\]][Heading [argh]​]]

It looks like the setting of org-link-bracket-re in org-link-make-regexps shows how to do this properly now. (I would just use those variables, but Org has changed such variables out from under me enough times that I try not to depend on them anymore.)

So I think it should be a simple fix. I'll try to get to it soon.

alphapapa commented 1 year ago

Hi Daniel,

Thanks for your patience. This should finally be fixed now. Please let me know if it works for you.

Also, reading this again:

I am enjoying the way it let's me make queries on my org files and dynamically extract reports on complex tasks I have to take care of. That really takes my workflow to a new level :)

If you have time, would you mind sharing a few examples of how you're using org-ql like that? It could help me understand how the package is used by serious users, and it might be helpful as an example for other users.

exot commented 1 year ago

Hey,

thank you for taking care of this issue! If will try to check your changes as soon as possible and will come back to you :-)

As to some examples of how I am using org-ql: I am happy to share what I do with org-ql, but it might be a bit complicated to explain because for me org-ql is usually part of some larger workflow. I'll try to summarize my main applications:

As you can see, I am a big fan of dynamic org-ql blocks as well as org-ql's simplicity when used in elisp code :-)

If it's helpful, I could try to explain some of the approaches in more detail, but it's hard for me to tell whether anything I wrote remotely makes any sense to anybody who's not accustomed to my workflow …

Best,

  Daniel

alphapapa commented 1 year ago

Hi Daniel,

That's a very impressive system you have. Thanks for sharing and describing it. I'd love to read a blog post describing it in more detail. Of course, there's never enough time to do everything, is there. :)

From looking at that, I think that you must be one of Org QL's most advanced users, so please do let me know about any ideas you may have in the future about how it could make your work easier.

Also, you might be interested in this: https://github.com/alphapapa/org-ql/issues/331

I also have some WIP branches for improving dynamic blocks. One of them is especially interesting, I think, as it allows a dynamic block to contain, rather than the usual table, an Org plain list of entries formatted using the same function that formats them for org-ql-view buffers, grouped with taxy as shown in #331. Since it's a plain list, the sublists can be folded, and they can be exported, etc.

alphapapa commented 1 year ago

I'm going to reopen this issue to remind me to follow up on some of the examples you shared.

exot commented 1 year ago

Hi Adam,

I finally found the time to test you fix and report back that everything works fine! (Indeed, testing was quick, but replying here took me far too long, sorry for that …)

That's a very impressive system you have. Thanks for sharing and describing it. I'd love to read a blog post describing it in more detail. Of course, there's never enough time to do everything, is there. :)

Thank you for your feedback! Indeed, I am currently trying – or rather struggling – to describe some of my workflows, mainly for myself to keep an overview over what I am actually doing 😉 But it turns out that what is easy to use is sometimes quite complicated to describe, at least for me, so I am afraid this will take even some more time …

From looking at that, I think that you must be one of Org QL's most advanced users, so please do let me know about any ideas you may have in the future about how it could make your work easier.

Thanks again! 😃 Scrolling through the documentation of Org QL, I still have the feeling though that I am not leveraging even a fraction of the full potential of Org QL, as there are so many things I haven't tried out yet. However, when I ever find that there is something I would like to do with Org QL but cannot find how to do it, I will come back to you 😁

Also, you might be interested in this: #331

I also have some WIP branches for improving dynamic blocks. One of them is especially interesting, I think, as it allows a dynamic block to contain, rather than the usual table, an Org plain list of entries formatted using the same function that formats them for org-ql-view buffers, grouped with taxy as shown in #331. Since it's a plain list, the sublists can be folded, and they can be exported, etc.

These are indeed fantastic pointers, I will check those out, thank you! While looking at #331, I also learned about your with-emacs.sh repository, which will also be a big help for me. Your repositories are a huge treasure chest!

Please keep up this great work!

  Daniel

alphapapa commented 1 year ago

Hi Daniel,

Thanks for the update, and for the kind words. I'm glad to hear that they're useful to you.