jkitchin / org-ref

org-mode modules for citations, cross-references, bibliographies in org-mode and useful bibtex tools to go with it.
GNU General Public License v3.0
1.36k stars 243 forks source link

Combine `link-hint-open-link` and `org-ref-jump-to-visible-key` #1053

Open randomwangran opened 1 year ago

randomwangran commented 1 year ago

org-ref-jump-to-visible-key can identify multiple candidates, but it does not contain a regular link in Org-mode. link-hint-open-link on the other hand, can identify a normal link but is not very good at finding multiple citation keys. We need a strong merge.

Following code snippet can merge two types of link, but I don't feel this is the best way to achieve this.

(defun wr/pos-getter (arg)
  (plist-get arg :pos))

(defun wr/org-ref-jump-to-visible-key ()
  "Jump to a visible key with avy."
  (interactive)
  (avy-with avy-goto-key
    (avy-process
     (apply #'append
            (list
             (append (mapcar #'wr/pos-getter (link-hint--get-links))
                     (car (save-excursion
                            (org-element-map (org-element-parse-buffer) 'link
                              (lambda (c)
                                (when (assoc (org-element-property :type c) org-ref-cite-types)
                                  (goto-char (org-element-property :begin c))
                                  (let* ((path (org-element-property :path c))
                                         (data (org-ref-parse-cite-path path))
                                         (references (plist-get data :references)))
                                    (append (list (org-element-property :begin c))
                                            (cl-loop for ref in references collect
                                                     (progn
                                                       (search-forward (plist-get ref :key))
                                                       (match-beginning 0))))))))))))))
    (avy--style-fn avy-style))
  (org-open-at-point))

Also please see: https://github.com/noctuid/link-hint.el/issues/209

jkitchin commented 1 year ago

I am not sure what you are trying to accomplish here. Is it one function that jumps to links or cite keys?

if it is, and your function works, what is the issue?

randomwangran commented 1 year ago

Is it one function that jumps to links or cite keys?

Yes, it is. The original function only works for citation links. This is what it is supposed to be. There is no issue with this function.

https://github.com/jkitchin/org-ref/blob/29223fb65a7c4b5451542b3ace03aed2a924e339/org-ref-citation-links.el#L1165-L1184

However, at the user's level, this is not an optimal function. The primary part of the jumping function is to jump a proper point in a buffer regardless it is a citation link or a normal Org-mode link.

So, I merged them together. It works well so far, but I don't think the way I make it is a robust way. I am writing this issue for users who might have the same issue. I am also open to suggestions to make this snippet a better one.

jkitchin commented 1 year ago

It seems fine to me, you just merge two lists of locations for avy.