reddit / emacs

Searching org-roam notes with property information

sigsegv0xb · 2022-08-30 21:15
I just learned I could do this, and thought if it was useful for me someone else might benefit from hearing it, especially since I can't find it mentioned in any other posts about org-roam. Some people are happy just searching their notes by titles, some also like to search tags by adding them into `org-roam-node-display-template`, and others like to do full text search with `ripgrep`. These worked OK for me, but they each had their own disadvantages, so I was looking for something different. Let's say I have a note called "Python Linting and Formatting". In the tags I might have something like `:python:linters`, in other words, very generic tags, which is how I like it. But I want to be able to search for specific tool names as well (i.e. `black`, `flake8`, etc.) without cluttering up the title or the tags. I don't want to use `ripgrep` because I don't want every note with the word `black` in the text to show up. Here's my solution. In the node properties, define a new property. You can name it whatever you want, but I've chosen to call mine `KEYWORDS`: :PROPERTIES: :KEYWORDS: :pep8:black:yapf:flake8: :ID: A8C66CA8-6103-4F5F-A78D-C9A00173869A :END: #+title: Python Formatting and Linting #+filetags: :python:linters: In your config, define a method with the name, `org-roam-node-<property_name>`: (cl-defmethod org-roam-node-keywords ((node org-roam-node)) "Return the currently set category for the NODE." (cdr (assoc-string "KEYWORDS" (org-roam-node-properties node)))) Now you can use `${keywords}` in `org-roam-node-display-template`, which makes these keywords searchable: (setq org-roam-node-display-template (concat "${title:*} " (propertize "${tags:30}" 'face 'org-tag) (propertize "${keywords:30}" 'face 'org-tag))) Running `org-roam-node-find`, you can now see the title, tags, and keywords in the results. Even if there's not enough room for the keywords to be displayed, they should still be searchable. There might be a way to even hide the keywords but still keep them searchable, but I don't really need that feature so I haven't looked into it. Again, this isn't for everyone, but if you've been looking for something like this I hope this helps.
Difficult-Flower2617 · 2022-08-31 01:47
Thanks for your great efforts. I actually need such kinds of function. Is your function different from alias of original function? Or almost the same but customized alias? Also ur function acts filtering in third party plug-in such as org-roam-ui? I appreciate your guides.
sigsegv0xb · 2022-08-31 03:57
I'm not sure if I fully understand your question, but there is no "original function" for `org-roam-node-keywords`, we're defining it for the first time ourselves. We have to define it as specified in the doc for `org-roam-node-display-template`: Patterns of form "${field-name:length}" are interpolated based on the current node. Each "field-name" is replaced with the return value of each corresponding accessor function for ‘org-roam-node’, e.g. "${title}" will be interpolated by the result of ‘org-roam-node-title’. You can also define custom accessors using ‘cl-defmethod’. For example, you can define: (cl-defmethod org-roam-node-my-title ((node org-roam-node)) (concat "My " (org-roam-node-title node))) and then reference it here or in the capture templates as "${my-title}". If I'm misunderstanding your question, please let me know. I also don't use org-roam-ui, so I can't answer that question.
Difficult-Flower2617 · 2022-08-31 04:20
Thank you for your time and kind guide. I will try!!
thecashewtrader · 2022-09-06 05:00
I wonder if it's worth it to use something like counsel-ripgrep for this.
sigsegv0xb · 2022-09-07 15:55
I mentioned in the post why I don't want to use ripgrep for this.
thecashewtrader · 2022-09-13 08:44
You could search for :black: Still kinda hacky though