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

org-ref cite links are not action in ROAM_KEY context #718

Closed
brabalan opened this issue Apr 10, 2020 · 15 comments
Closed

org-ref cite links are not action in ROAM_KEY context #718

brabalan opened this issue Apr 10, 2020 · 15 comments

Comments

@brabalan
Copy link
Contributor

Org Roam has a nice feature where you can add a key at the beginning of your file, and citations to that key will be tracked (see https://org-roam.readthedocs.io/en/master/anatomy/#file-refs for a description).

Unfortunately, these links are not active: I cannot use them to access the org-ref menu (that lets me open the corresponding menu).

Here is what the beginning of my file looks like:

#+TITLE: Higher-Order Minimal Functional Graphs
#+ROAM_KEY: cite:JonesRosendahl1994

The org-ref link is not highlighted and I cannot act on it.

Is it possible to make a link in this setting active? (The link works fine in the text in the body of the file.)

@jkitchin
Copy link
Owner

This seems like an org-mode limitation. I cannot exactly reproduce the problem, for me they are highlighted, but I get an error clicking on them. The problem is related to how they are fontified. I don't think org-ref can fix that.

@brabalan
Copy link
Contributor Author

Thank you, I'll ask on the org mailing list.

@brabalan
Copy link
Contributor Author

I have investigated further, and I think org does the right thing (C-c C-o opens the link, and in my modded emacs the org-ref menu). This is an issue with doom emacs.

@brabalan
Copy link
Contributor Author

For information, this is the reported bug: doomemacs/doomemacs#2880

@brabalan
Copy link
Contributor Author

brabalan commented Apr 13, 2020

I am reopening this issue as I found further problems, and they seem to be with org-ref proper.

When in a keyword context, org-ref cannot get the link using the usual way (org-ref-get-bibtex-key-under-cursor, https://github.com/jkitchin/org-ref/blob/master/org-ref-core.el#L2045), because the parsed structure looks like this:

(keyword
 (:key "FOO"
  :value "http://alan.petitepomme.net/"
  :begin 1
  :end 38
  :post-blank 1
  :post-affiliated 1
  :parent nil))

This won't change on the orgmode side of things. Nicolas Goaziou suggested on the mailing list to use org-element-parse-secondary-string in this case (https://lists.gnu.org/archive/html/emacs-orgmode/2020-04/msg00215.html). Is this something you could use with org-ref?

@brabalan brabalan reopened this Apr 13, 2020
@jkitchin
Copy link
Owner

It might be, but I won't have time to look into this for a while. If you want to try it and make a pull request, it might happen faster. I have generally tried not to fight orgmode like this too much like this though, as it makes it harder to debug in the future.

@tshu-w
Copy link
Contributor

tshu-w commented Jul 22, 2020

@brabalan I made some minor changes to make org-ref-get-bibtex-key-under-cursor work.

(link-string
            (progn (org-in-regexp org-link-any-re)
                   (cadr (split-string
                          (match-string-no-properties 0) ":"))))

For more details:

  (defun +org-ref-get-bibtex-key-under-cursor ()
    "Return key under the cursor in org-mode.
We search forward from point to get a comma, or the end of the link,
and then backwards to get a comma, or the beginning of the link. that
delimits the keyword we clicked on. We also strip the text
properties."
    (let* ((object (org-element-context))
           (link-string
            (progn (org-in-regexp org-link-any-re)
                   (cadr (split-string
                          (match-string-no-properties 0) ":")))))
      ;; you may click on the part before the citations. here we make
      ;; sure to move to the beginning so you get the first citation.
      (let ((cp (point)))
        (goto-char (org-element-property :begin object))
        (search-forward link-string (org-element-property :end object))
        (goto-char (match-beginning 0))
        ;; check if we clicked before the path and move as needed.
        (unless (< cp (point))
          (goto-char cp)))

      (if (not (org-element-property :contents-begin object))
          ;; this means no description in the link
          (progn
            ;; we need the link path start and end
            (let (link-string-beginning link-string-end)
              (save-excursion
                (goto-char (org-element-property :begin object))
                (search-forward link-string nil nil 1)
                (setq link-string-beginning (match-beginning 0))
                (setq link-string-end (match-end 0)))

              (let (key-beginning key-end)
                ;; The key is the text between commas, or the link boundaries
                (save-excursion
                  (if (search-forward "," link-string-end t 1)
                      (setq key-end (- (match-end 0) 1)) ; we found a match
                    (setq key-end link-string-end))) ; no comma found so take the end
                ;; and backward to previous comma from point which defines the start character
                (save-excursion
                  (if (search-backward "," link-string-beginning 1 1)
                      (setq key-beginning (+ (match-beginning 0) 1)) ; we found a match
                    (setq key-beginning link-string-beginning))) ; no match found
                ;; save the key we clicked on.
                (let ((bibtex-key
                       (org-ref-strip-string
                        (buffer-substring key-beginning key-end))))
                  (set-text-properties 0 (length bibtex-key) nil bibtex-key)
                  bibtex-key))))

        ;; link with description and multiple keys
        (if (and (org-element-property :contents-begin object)
	               (string-match "," link-string)
	               (equal (org-element-type object) 'link))
	          ;; point is not on the link description
	          (if (not (>= (point) (org-element-property :contents-begin object)))
	              (let (link-string-beginning link-string-end)
		              (save-excursion
		                (goto-char (org-element-property :begin object))
		                (search-forward link-string nil t 1)
		                (setq link-string-beginning (match-beginning 0))
		                (setq link-string-end (match-end 0)))

		              (let (key-beginning key-end)
		                ;; The key is the text between commas, or the link boundaries
		                (save-excursion
		                  (if (search-forward "," link-string-end t 1)
			                    (setq key-end (- (match-end 0) 1)) ; we found a match
		                    (setq key-end link-string-end))) ; no comma found so take the end
		                ;; and backward to previous comma from point which defines the start character

		                (save-excursion
		                  (if (search-backward "," link-string-beginning 1 1)
			                    (setq key-beginning (+ (match-beginning 0) 1)) ; we found a match
		                    (setq key-beginning link-string-beginning))) ; no match found
		                ;; save the key we clicked on.
		                (let ((bibtex-key
			                     (org-ref-strip-string
			                      (buffer-substring key-beginning key-end))))
		                  (set-text-properties 0 (length bibtex-key) nil bibtex-key)
		                  bibtex-key)))
	            ;; point is on the link description, assume we want the
	            ;; last key
	            (let ((last-key (replace-regexp-in-string "[a-zA-Z0-9_-]*," "" link-string)))
	              last-key))
	        ;; link with description. assume only one key
	        link-string))))

  (advice-add 'org-ref-get-bibtex-key-under-cursor :override '+org-ref-get-bibtex-key-under-cursor)

@brabalan
Copy link
Contributor Author

This is great, thanks. @jkitchin any chance it could be included?

@tshu-w
Copy link
Contributor

tshu-w commented Aug 10, 2020

@brabalan If this works on your side, I would try to make a pull request.

@jkitchin
Copy link
Owner

I am testing this as a replacement now, if it works out for me, I will integrate the function above in this week.

jkitchin pushed a commit that referenced this issue Aug 10, 2020
this enables cite links inside keywords. Before this did not work because I used
org-parsing, which doesn't work on a keyword which parses as a keyword. This
solution was proposed by @tshu-w. It works for getting the key under the cursor,
and should work for this use.

It doesn't work for me yet, but for independent reasons I think. I am unable to
open a cite link from a string due to a fontification error. So far I have not
been able to figure why, somewhere the cite link fontification goes past the end
of the buffer.
@jkitchin
Copy link
Owner

I have pushed this as a replacement. Hoepfully it works for you. It fixes the issue of key under cursor in keywords I think. I still can't open the links though because of another fontification issue that I sort of believe is a bug in my emacs setup that is specific to cite links.

@tshu-w
Copy link
Contributor

tshu-w commented Aug 11, 2020

@jkitchin maybe the following will be better since we don't need a regular search if the type of org-element-context is link.

(link-string (if (eq (org-element-type object) 'link)
                            (org-element-property :path object)
                          (org-in-regexp org-link-any-re)
                          (cadr (split-string
                                 (match-string-no-properties 0) ":"))))

@brabalan
Copy link
Contributor Author

This is partially working for me: I can open the link (using the RET key). I do not get the information displayed in the minibar when the cursor is on the link, but it is a very minor hindrance.

@jkitchin
Copy link
Owner

The minibar message is related to the same issue, org-ref-link-message uses the same org-parsing machinery that made clicking not work. It is harder to fix this issue, because this function also supports a bunch of other link types, not just cite links. Fixing that would be more work than I have time for now.

Luckily for me, the tooltip from the mouse at least works.

@brabalan
Copy link
Contributor Author

Mouse tooltip works for me too, so I think this is fine at the moment. Thanks!

jkitchin added a commit that referenced this issue Aug 21, 2020
This combines a suggestion in issue #718 that is more reliable to get the link
string when we are on an org-link, and some better logic to get the link string
that works with keys containing colons.
myshevchuk added a commit to myshevchuk/org-ref that referenced this issue Oct 22, 2020
- handle cases when the link is not `org-element`'s primary object
see jkitchin#718
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

No branches or pull requests

3 participants