(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)))
This commit is contained in:
Daniel Mendler
2021-10-13 13:13:45 +02:00
committed by GitHub
parent f80515ab5f
commit 617e0021f5
3 changed files with 18 additions and 8 deletions

View File

@ -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

View File

@ -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."

View File

@ -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)