From 25bc3acce35313fab1e2a105f026a5549cddb041 Mon Sep 17 00:00:00 2001 From: Jethro Kuan Date: Tue, 9 Nov 2021 22:32:40 +0800 Subject: [PATCH] (fix)buffer: change preview-content logic (#1955) Some commit in Org 9.5 changed the AST structure, so we our previous preview content logic was borked and started showing the full contents of the file. To be forward compatible we change the content preview logic, and extract everything within the section. Fixes #1934. --- org-roam-mode.el | 24 +++++++++--------------- org-roam-utils.el | 18 +++++++++++------- 2 files changed, 20 insertions(+), 22 deletions(-) diff --git a/org-roam-mode.el b/org-roam-mode.el index 08664ee..4ba6a4d 100644 --- a/org-roam-mode.el +++ b/org-roam-mode.el @@ -397,23 +397,17 @@ In interactive calls OTHER-WINDOW is set with (when (org-invisible-p) (org-show-context)) buf)) -(defun org-roam-preview-get-contents (file point) - "Get preview content for FILE at POINT." +(defun org-roam-preview-get-contents (file pt) + "Get preview content for FILE at PT." (save-excursion (org-roam-with-temp-buffer file - (goto-char point) - (let ((elem (org-element-at-point))) - ;; We want the parent element always - (while (org-element-property :parent elem) - (setq elem (org-element-property :parent elem))) - (pcase (car elem) - ('headline ; show subtree - (org-roam-preview-get-entry-text (point-marker) most-positive-fixnum)) - (_ - (let ((begin (org-element-property :begin elem)) - (end (org-element-property :end elem))) - (or (string-trim (buffer-substring-no-properties begin end)) - (org-element-property :raw-value elem))))))))) + (org-with-wide-buffer + (goto-char pt) + (let ((beg (progn (org-roam-end-of-meta-data t) + (point))) + (end (progn (org-next-visible-heading 1) + (point)))) + (string-trim (buffer-substring-no-properties beg end))))))) (defun org-roam-preview-get-entry-text (marker n-lines &optional indent) "Extract entry text from MARKER, at most N-LINES lines. diff --git a/org-roam-utils.el b/org-roam-utils.el index 9351bd3..67c0cf4 100644 --- a/org-roam-utils.el +++ b/org-roam-utils.el @@ -243,12 +243,13 @@ If BOUND, scan up to BOUND bytes of the buffer." (defun org-roam-end-of-meta-data (&optional full) "Like `org-end-of-meta-data', but supports file-level metadata. -When optional argument FULL is t, also skip planning information, -clocking lines and any kind of drawer. - When FULL is non-nil but not t, skip planning information, -properties, clocking lines and logbook drawers." +properties, clocking lines and logbook drawers. + +When optional argument FULL is t, skip everything above, and also +skip keywords." (org-back-to-heading-or-point-min t) + (when (org-at-heading-p) (forward-line)) ;; Skip planning information. (when (looking-at-p org-planning-line-re) (forward-line)) ;; Skip property drawer. @@ -268,11 +269,14 @@ properties, clocking lines and logbook drawers." (if (re-search-forward "^[ \t]*:END:[ \t]*$" end t) (forward-line) (throw 'exit t))) - ;; When FULL is t, skip regular drawer too. - ((and (eq full t) (looking-at-p org-drawer-regexp)) + ((looking-at-p org-drawer-regexp) (if (re-search-forward "^[ \t]*:END:[ \t]*$" end t) (forward-line) (throw 'exit t))) + ;; When FULL is t, skip keywords too. + ((and (eq full t) + (looking-at-p org-keyword-regexp)) + (forward-line)) (t (throw 'exit t)))))))) (defun org-roam-set-keyword (key value) @@ -284,7 +288,7 @@ If the property is already set, it's value is replaced." (if (string-blank-p value) (kill-whole-line) (replace-match (concat " " value) 'fixedcase nil nil 1)) - (org-roam-end-of-meta-data) + (org-roam-end-of-meta-data 'drawers) (if (save-excursion (end-of-line) (eobp)) (progn (end-of-line)