Skip to content

Commit

Permalink
(feat): add org-roam-buffer-preview-function (#1388)
Browse files Browse the repository at this point in the history
Instead of storing preview content in the database, now provide a
function to fetch these on the fly. This paves the way for future
improvements (e.g. showing more lines)
  • Loading branch information
jethrokuan committed Jan 16, 2021
1 parent 06e5814 commit 1b3a0ab
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
23 changes: 21 additions & 2 deletions org-roam-buffer.el
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
(require 's)
(require 'f)
(require 'ol)
(require 'org-element)
(require 'org-roam-macs)

(defvar org-roam-directory)
(defvar org-link-frame-setup)
Expand Down Expand Up @@ -98,6 +100,13 @@ Has an effect if and only if `org-roam-buffer-position' is `top' or `bottom'."
:type 'hook
:group 'org-roam)

(defcustom org-roam-buffer-preview-function #'org-roam-buffer--preview
"Function to obtain preview contents for a given link.
The function takes in two arguments, the FILE containing the
link, and the POINT of the link."
:type 'function
:group 'org-roam)

(defcustom org-roam-buffer-window-parameters nil
"Additional window parameters for the `org-roam-buffer' side window.
For example: (setq org-roam-buffer-window-parameters '((no-other-window . t)))"
Expand All @@ -124,6 +133,16 @@ For example: (setq org-roam-buffer-window-parameters '((no-other-window . t)))"
'font-lock-face
'org-document-title)))

(defun org-roam-buffer--preview (file point)
"Get preview content for FILE at POINT."
(org-roam--with-temp-buffer file
(goto-char point)
(let ((elem (org-element-at-point)))
(or (org-element-property :raw-value elem)
(when-let ((begin (org-element-property :begin elem))
(end (org-element-property :end elem)))
(string-trim (buffer-substring-no-properties begin end)))))))

(defun org-roam-buffer--pluralize (string number)
"Conditionally pluralize STRING if NUMBER is above 1."
(let ((l (pcase number
Expand Down Expand Up @@ -171,7 +190,7 @@ ORIG-PATH is the path where the CONTENT originated."
"file")))
(dolist (backlink bls)
(pcase-let ((`(,file-from _ ,props) backlink))
(insert (if-let ((content (plist-get props :content)))
(insert (if-let ((content (funcall org-roam-buffer-preview-function file-from (plist-get props :point))))
(propertize (org-roam-buffer-expand-links content file-from)
'help-echo "mouse-1: visit backlinked note"
'file-from file-from
Expand Down Expand Up @@ -208,7 +227,7 @@ ORIG-PATH is the path where the CONTENT originated."
(org-roam-buffer-expand-links file-from))
"Top")
"\n"
(if-let ((content (plist-get prop :content)))
(if-let ((content (funcall org-roam-buffer-preview-function file-from (plist-get prop :point))))
(propertize
(s-trim (s-replace "\n" " " (org-roam-buffer-expand-links content file-from)))
'help-echo "mouse-1: visit backlinked note"
Expand Down
11 changes: 1 addition & 10 deletions org-roam.el
Original file line number Diff line number Diff line change
Expand Up @@ -602,17 +602,8 @@ it as FILE-PATH."
(goto-char (org-element-property :begin link))
(let* ((type (org-roam--collate-types (org-element-property :type link)))
(path (org-element-property :path link))
(element (org-element-at-point))
(begin (or (org-element-property :contents-begin element)
(org-element-property :begin element)))
(end (or (org-element-property :contents-end element)
(org-element-property :end element)))
(content (or (org-element-property :raw-value element)
(when (and begin end)
(string-trim (buffer-substring-no-properties begin end)))))
(properties (list :outline (org-roam--get-outline-path)
:content content
:point begin))
:point (point)))
(names (pcase type
("id"
(when-let ((file-path (org-roam-id-get-file path)))
Expand Down

0 comments on commit 1b3a0ab

Please sign in to comment.