toshism/org-super-links

Attempting to create a link to another file instead creates a link to a random heading in the current file?

Stewmath opened this issue · 3 comments

It's just as the title says... suppose I have a file named "Test1.org":

* Heading A

* Heading B

And a file named "Test2.org":

* Heading C

* Heading D

When I use "org-super-links-link" under "Heading B" in Test1.org and attempt to create a link to "Heading C", this happens instead:

Test1.org:

* Heading A
:PROPERTIES:
:ID:       662f9f39-b22a-4203-8ec1-2b87bcd53342
:END:
:BACKLINKS:
[2024-05-13 Mon 23:41] <- [[id:d798e570-13b4-4d46-906a-22d44c6ecc60][Heading B]]
:END:

* Heading B
:PROPERTIES:
:ID:       d798e570-13b4-4d46-906a-22d44c6ecc60
:END:
[[id:662f9f39-b22a-4203-8ec1-2b87bcd53342][Heading A]]

As you can see it simply linked to Heading A instead. It does seem to work when you link headings within the current file, but for anything outside the current file, this happens instead.

I should also mention that I'm using org-super-links with doom emacs:

(package! org-super-links :recipe
  (:host github
   :repo "toshism/org-super-links"
   :branch "develop"
   :files ("*.el")))

OK, the problem is that my org directory is a symlink. The org-super-links-get-location function contains the following line for getting the buffer to be linked to:

				 (get-file-buffer (car (cdr target)))))))

According to the documentation for get-file-buffer:

The buffer's buffer-file-name must match exactly the expansion of FILENAME.
If there is no such live buffer, return nil.
See also find-buffer-visiting.

It was returning nil because the "canonical" version of the file is somewhere else. By returning nil, it was always operating in the current buffer.

Changing "get-file-buffer" to "find-buffer-visiting" in the implementation of the org-super-links-get-location function fixes the problem. In my case I've simply made my own version of the function:

(defun my-org-super-links-get-location ()
  "My fixed version of org-super-links-get-location. Works with symlinks."
  (let ((target (org-refile-get-location "Super Link")))
    (org-super-links--insert-link (set-marker (make-marker) (car (cdddr target))
                                              (find-buffer-visiting (car (cdr target)))))))
(setq org-super-links-search-function #'my-org-super-links-get-location)

Will leave this open in case the fix can get merged in.

thanks for the investigation. i'll get a fix in for this.