diff --git a/CHANGELOG.md b/CHANGELOG.md index 087593a..b833c6d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ - [#847](https://github.com/org-roam/org-roam/pull/847) Add GC threshold `org-roam-db-gc-threshold` to temporarily change the threshold on expensive operations. - [#847](https://github.com/org-roam/org-roam/pull/847) Use sqlite3 transactions instead of storing the values to be inserted. - [#851](https://github.com/org-roam/org-roam/pull/851) Add `'first-directory'` option for `org-roam-tag-sources` +- [#863](https://github.com/org-roam/org-roam/pull/863) Display outline hierarchy in backlinks buffer ### Bugfixes diff --git a/org-roam-buffer.el b/org-roam-buffer.el index bc9e859..2b0be9c 100644 --- a/org-roam-buffer.el +++ b/org-roam-buffer.el @@ -169,13 +169,18 @@ For example: (setq org-roam-buffer-window-parameters '((no-other-window . t)))" (org-roam--get-title-or-slug file-from))) (dolist (backlink bls) (pcase-let ((`(,file-from _ ,props) backlink)) - (insert (propertize + (insert "*** " + (if-let ((outline (plist-get props :outline))) + (string-join outline " > ") + "Top") + "\n" + (propertize (s-trim (s-replace "\n" " " (plist-get props :content))) 'help-echo "mouse-1: visit backlinked note" 'file-from file-from - 'file-from-point (plist-get props :point))) - (insert "\n\n")))))) + 'file-from-point (plist-get props :point)) + "\n\n")))))) (insert "\n\n* No backlinks!"))) (defun org-roam-buffer-update () diff --git a/org-roam.el b/org-roam.el index c439a19..2448cba 100644 --- a/org-roam.el +++ b/org-roam.el @@ -510,6 +510,42 @@ PATH should be the root from which to compute the relativity." (concat dir link))))) (buffer-string)))) +(defun org-roam--get-outline-path () + "Return the outline path to the current entry. + +An outline path is a list of ancestors for current headline, as a +list of strings. Statistics cookies are removed and links are +kept. + +When optional argument WITH-SELF is non-nil, the path also +includes the current headline." + (org-with-wide-buffer + (and (or (condition-case nil + (org-back-to-heading t) + (error nil)) + (org-up-heading-safe)) + (reverse (org-roam--get-outline-path-1))))) + +(defun org-roam--get-outline-path-1 () + "Return outline path to current headline. + +Outline path is a list of strings, in reverse order. See +`org-roam--get-outline-path' for details. + +Assume buffer is widened and point is on a headline." + (when org-complex-heading-regexp + (let ((heading (let ((case-fold-search nil)) + (looking-at org-complex-heading-regexp) + (if (not (match-end 4)) "" + ;; Remove statistics cookies. + (org-trim + (replace-regexp-in-string + "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" "" + (match-string-no-properties 4))))))) + (if (org-up-heading-safe) + (cons heading (org-roam--get-outline-path-1)) + (list heading))))) + (defun org-roam--extract-links (&optional file-path) "Extracts all link items within the current buffer. Link items are of the form: @@ -553,7 +589,11 @@ it as FILE-PATH." (content (string-trim content)) ;; Expand all relative links to absolute links (content (org-roam--expand-links content file-path))) - (let ((context (list :content content :point begin)) + (let ((properties (list :outline (mapcar (lambda (path) + (org-roam--expand-links path file-path)) + (org-roam--get-outline-path)) + :content content + :point begin)) (names (pcase link-type ("file" (list (file-truename (expand-file-name path (file-name-directory file-path))))) @@ -565,7 +605,7 @@ it as FILE-PATH." (push (vector file-path name link-type - context) + properties) links)) names))))))) links)) diff --git a/tests/test-org-roam-perf.el b/tests/test-org-roam-perf.el index 917602a..d9ffa13 100644 --- a/tests/test-org-roam-perf.el +++ b/tests/test-org-roam-perf.el @@ -45,7 +45,7 @@ (pcase (benchmark-run 1 (org-roam-db-build-cache t)) (`(,time ,gcs ,time-in-gc) (message "Elapsed time: %fs (%fs in %d GCs)" time time-in-gc gcs) - (expect time :to-be-less-than 60)))) + (expect time :to-be-less-than 70)))) (it "builds quickly without change" (pcase (benchmark-run 1 (org-roam-db-build-cache)) (`(,time ,gcs ,time-in-gc)