Skip to content

Commit

Permalink
Make denote-link-return-backlinks optionally return full data of match
Browse files Browse the repository at this point in the history
The idea is for us to then use this information in 'denote-find-backlink'
(or related) to jump to the position of the match, rather than simply
visit the corresponding file.

This is a preliminary response to what johkneisl requested in issue
471: <#471>.
  • Loading branch information
protesilaos committed Nov 4, 2024
1 parent d42be4e commit 4a2079a
Showing 1 changed file with 26 additions and 3 deletions.
29 changes: 26 additions & 3 deletions denote.el
Original file line number Diff line number Diff line change
Expand Up @@ -2064,6 +2064,16 @@ See `denote--retrieve-locations-in-xrefs'."
(denote--retrieve-group-in-xrefs identifier))
#'string-collate-lessp))

(defun denote--retrieve-file-and-location-in-xrefs (identifier)
"Return matches for IDENTIFIER as a list of (file line column)."
(when-let* ((locations (denote--retrieve-location-in-xrefs identifier)))
(mapcar
(lambda (element)
(list (xref-file-location-file element)
(xref-file-location-line element)
(xref-file-location-column element)))
locations)))

;;;; New note

;;;;; Common helpers for new notes
Expand Down Expand Up @@ -4303,12 +4313,25 @@ Also see `denote-link-return-backlinks'."
(or (denote-link-return-links)
(user-error "No links found"))))))

(defun denote-link-return-backlinks (&optional file)
"Return list of backlinks in current or optional FILE.
(defun denote-link-return-backlinks (&optional file full-data)
"Return list of paths to backlinks in current or optional FILE.
With optional FULL-DATA return backlinks as (file line column).
Also see `denote-link-return-links'."
(when-let* ((current-file (or file (buffer-file-name)))
(id (denote-retrieve-filename-identifier-with-error current-file)))
(delete current-file (denote--retrieve-files-in-xrefs id))))
(if full-data
(seq-remove (lambda (element)
(equal (car element) current-file))
;; We concat the denote: to make sure we are only
;; matching links. Otherwise, we will get plain
;; identifiers, which could be shown due to the
;; output of some dynamic block. Those are not
;; "backlinks" per se. When we do not use
;; FULL-DATA, this is not necessary because we
;; only get unique matches to files.
(denote--retrieve-file-and-location-in-xrefs (concat "denote:" id)))
(delete current-file (denote--retrieve-files-in-xrefs id)))))

;; TODO 2024-09-04: Instead of using `denote-link-return-backlinks' we
;; should have a function that does not try to find all backlinks but
Expand Down

0 comments on commit 4a2079a

Please sign in to comment.