Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure org-roam-node-from-title-or-alias won't mess up with the match-data #1766

Merged
merged 1 commit into from
Aug 16, 2021
Merged

Conversation

Konubinix
Copy link
Contributor

It lies in between org-in-regexp and replace-match. In some situations, like
when the link looks like roam:a s'b, it changes the match-data.

Motivation for this change

Get file file test.txt
It is an elisp file, but github won't allow the extension .el...
It

  • installs org-roam,
  • sets up a roam directory
  • adds two files,
    • one with title "a s'b"
    • one with title "other" in it the content [[roam:a s'b]]
  • run org-roam-link-replace-all

This simulates the behavior when you create a note and use the completion mecanism to add a link to another note.

Run this with emacs -Q --load test.txt, you will get the following error

org-roam-link-replace-at-point: Args out of range: 0, 0

Try removing the "'" character, so that the first file has the title "a sb" and the roam link is now [[roam:a sb]]. Now the test works.

Digging a little bit, I found out that, when the title is "a s'b"

  • org-roam-link-replace-at-point calls org-roam-node-from-title-or-alias
  • org-roam-node-from-title-or-alias calls org-roam-db-query
  • org-roam-db-query calls emacsql and org-roam-db
  • both emacsql and org-roam-db change the match-data

I did not look further. May be some other place of the code changes the match-data.

But I can tell that nesting org-roam-link-replace-at-point inside save-match-data fixes the issue.

Moreover, I guess it is a good practice to ensure that the code between anything that sets the match data (org-in-regexp in this example) and anything that relies on this match-data (replace-match in this example) does not change the match-data.

…tch-data

It lies in between org-in-regexp and replace-match. In some situations, like
when the link looks like roam:a s'b, it changes the match-data.
@@ -628,7 +628,7 @@ Assumes that the cursor was put where the link is."
(goto-char (org-element-property :begin link))
(when (and (org-in-regexp org-link-any-re 1)
(string-equal type "roam")
(setq node (org-roam-node-from-title-or-alias path)))
(setq node (save-match-data (org-roam-node-from-title-or-alias path))))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case, I'll move the save-match-data into org-roam-node-from-title-or-alias.

Copy link
Contributor Author

@Konubinix Konubinix Aug 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't mind, but IIUC, the idiomatic way of doing it in emacs appears
to put save-match-data in the function that needs the match data to be
saved.

At least, this it what I understand by reading at the documentation of
save-match-data:

so ‘save-match-data’ should normally be used to save *your* match data
rather than your caller’s match data.

@jethrokuan jethrokuan merged commit c51ce08 into org-roam:master Aug 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants