From 617e0021f5ec37bb0421bf434dbfd33d9911bb56 Mon Sep 17 00:00:00 2001 From: Daniel Mendler Date: Wed, 13 Oct 2021 13:13:45 +0200 Subject: [PATCH] (fix)completions: fix highlighting of formatted/truncated strings (#1895) * (fix)org-roam-unlinked-references-section: Use truncate-string-ellipsis The variable can be set to a unicode ellipsis. * (fix)org-roam-node-read--format-entry: Fix highlighting for truncation Fix #1801. The truncated part of the string is made invisible. Matching on the whole string remains possible. * (fix)org-roam-format-template: Preserve string properties like format If the variable value carries properties itself, these properties take precedence. Display templates can be properties with this change. (setq org-roam-node-display-template (concat (propertize "${title:*}" 'face 'font-lock-keyword-face) " " (propertize "${tags:10}" 'face 'font-lock-constant-face))) --- org-roam-mode.el | 2 +- org-roam-node.el | 20 ++++++++++++++------ org-roam-utils.el | 4 +++- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/org-roam-mode.el b/org-roam-mode.el index 1c79b9b..08664ee 100644 --- a/org-roam-mode.el +++ b/org-roam-mode.el @@ -684,7 +684,7 @@ References from FILE are excluded." (oset section row row) (oset section col col) (insert (propertize (format "%s:%s:%s" - (truncate-string-to-width (file-name-base f) 15 nil nil "...") + (truncate-string-to-width (file-name-base f) 15 nil nil t) row col) 'font-lock-face 'org-roam-dim) " " (org-roam-fontify-like-in-org-mode diff --git a/org-roam-node.el b/org-roam-node.el index d2ea018..4670caa 100644 --- a/org-roam-node.el +++ b/org-roam-node.el @@ -536,12 +536,20 @@ Uses `org-roam-node-display-template' to format the entry." ;; empty string results in an empty string and misalignment for candidates that ;; don't have some field. This uses the actual display string, made of spaces ;; when the field-value is "" so that we actually take up space. - (if (or (not field-width) (equal field-value "")) - field-value - ;; Remove properties from the full candidate string, otherwise the display - ;; formatting with pre-propertized field-values gets messed up. - (let ((display-string (truncate-string-to-width field-value field-width 0 ?\s))) - (propertize (substring-no-properties field-value) 'display display-string)))))))) + (unless (or (not field-width) (equal field-value "")) + (let* ((truncated (truncate-string-to-width field-value field-width 0 ?\s)) + (tlen (length truncated)) + (len (length field-value))) + (if (< tlen len) + ;; Make the truncated part of the string invisible. If strings + ;; are pre-propertized with display or invisible properties, the + ;; formatting may get messed up. Ideally, truncated strings are + ;; not preformatted with these properties. Face properties are + ;; allowed without restriction. + (put-text-property tlen len 'invisible t field-value) + ;; If the string wasn't truncated, but padded, use this string instead. + (setq field-value truncated)))) + field-value))))) (defun org-roam-node-read--process-display-format (format) "Pre-calculate minimal widths needed by the FORMAT string." diff --git a/org-roam-utils.el b/org-roam-utils.el index 7233d2b..925ec3c 100644 --- a/org-roam-utils.el +++ b/org-roam-utils.el @@ -127,7 +127,9 @@ value (possibly nil). Adapted from `s-format'." (let ((v (progn (set-match-data saved-match-data) (funcall replacer var default-val)))) - (if v (format "%s" v) (signal 'org-roam-format-resolve md))) + (if v + (format (apply #'propertize "%s" (text-properties-at 0 var)) v) + (signal 'org-roam-format-resolve md))) (set-match-data replacer-match-data)))) (if (functionp template) (funcall template)