From 1b3a0abd36ab00a1994fa32bf12bf5894c2daeee Mon Sep 17 00:00:00 2001 From: Jethro Kuan Date: Sun, 17 Jan 2021 02:52:39 +0800 Subject: [PATCH] (feat): add org-roam-buffer-preview-function (#1388) 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) --- org-roam-buffer.el | 23 +++++++++++++++++++++-- org-roam.el | 11 +---------- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/org-roam-buffer.el b/org-roam-buffer.el index 148a7f7..f8bb8aa 100644 --- a/org-roam-buffer.el +++ b/org-roam-buffer.el @@ -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) @@ -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)))" @@ -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 @@ -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 @@ -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" diff --git a/org-roam.el b/org-roam.el index 059e5fb..87bf47b 100644 --- a/org-roam.el +++ b/org-roam.el @@ -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)))