Revert changes to tp--replace-reactive-text-in-buffer to fix checkbox strikethrough toggle
Co-authored-by: Kinneyzhang <38454496+Kinneyzhang@users.noreply.github.com>
This commit is contained in:
parent
89d76fa0cc
commit
7d7979c54c
69
tp.el
69
tp.el
@ -1093,36 +1093,20 @@ it will be applied to the text before updating."
|
|||||||
(save-excursion
|
(save-excursion
|
||||||
(tp--replace-reactive-text-in-buffer layer-name new-text props)))))))))))
|
(tp--replace-reactive-text-in-buffer layer-name new-text props)))))))))))
|
||||||
|
|
||||||
(defun tp--merge-props-with-base (base-props new-props)
|
(defvar tp-preserve-external-properties '(keymap mouse-face cursor pointer help-echo)
|
||||||
"Merge NEW-PROPS into BASE-PROPS with NEW-PROPS taking precedence.
|
"List of text properties to preserve during reactive text updates.
|
||||||
Face properties are merged using `tp--merge-face-values', while other
|
These properties are typically set by external libraries (like twidget's event
|
||||||
properties from NEW-PROPS override those in BASE-PROPS. Properties only
|
system) and should not be overwritten when tp.el updates reactive text.
|
||||||
in BASE-PROPS are preserved. Returns the merged plist, or nil if both
|
Properties in this list will be preserved from the buffer during updates,
|
||||||
arguments are nil."
|
while other properties will be replaced with the new values.")
|
||||||
(cond
|
|
||||||
((null base-props) new-props)
|
|
||||||
((null new-props) base-props)
|
|
||||||
(t
|
|
||||||
(let ((result (copy-sequence new-props)))
|
|
||||||
(cl-loop for (key val) on base-props by #'cddr
|
|
||||||
do (let ((new-val (plist-get result key)))
|
|
||||||
(if (plist-member result key)
|
|
||||||
;; New props has this key - merge if face property
|
|
||||||
(when (memq key '(face font-lock-face mouse-face))
|
|
||||||
(setq result
|
|
||||||
(plist-put result key
|
|
||||||
(tp--merge-face-values val new-val))))
|
|
||||||
;; New props doesn't have this key - add from base
|
|
||||||
(setq result (plist-put result key val)))))
|
|
||||||
result))))
|
|
||||||
|
|
||||||
(defun tp--replace-reactive-text-in-buffer (layer-name new-text props)
|
(defun tp--replace-reactive-text-in-buffer (layer-name new-text props)
|
||||||
"Replace text in current buffer for reactive text with LAYER-NAME.
|
"Replace text in current buffer for reactive text with LAYER-NAME.
|
||||||
NEW-TEXT is the new text to replace with.
|
NEW-TEXT is the new text to replace with.
|
||||||
PROPS are the properties to apply to the new text.
|
PROPS are the properties to apply to the new text.
|
||||||
Text properties embedded in NEW-TEXT are merged with PROPS.
|
+Text properties embedded in NEW-TEXT are merged with PROPS.
|
||||||
All original text properties are preserved and merged with the new properties,
|
+Properties listed in `tp-preserve-external-properties' that were applied
|
||||||
with new properties taking precedence over original properties."
|
+by other libraries are preserved during the update."
|
||||||
(goto-char (point-min))
|
(goto-char (point-min))
|
||||||
(let ((match (text-property-search-forward 'tp-name layer-name t))
|
(let ((match (text-property-search-forward 'tp-name layer-name t))
|
||||||
;; Merge embedded text properties from new-text into props
|
;; Merge embedded text properties from new-text into props
|
||||||
@ -1131,21 +1115,28 @@ with new properties taking precedence over original properties."
|
|||||||
(let* ((m-start (prop-match-beginning match))
|
(let* ((m-start (prop-match-beginning match))
|
||||||
(m-end (prop-match-end match))
|
(m-end (prop-match-end match))
|
||||||
(old-text (buffer-substring-no-properties m-start m-end))
|
(old-text (buffer-substring-no-properties m-start m-end))
|
||||||
;; Get ALL existing properties from the original text
|
;; Preserve only specific external properties
|
||||||
(existing-props (text-properties-at m-start))
|
(existing-props (text-properties-at m-start))
|
||||||
;; Merge: existing props as base, merged-props as new (takes precedence)
|
(preserved-props nil))
|
||||||
(final-props (tp--merge-props-with-base existing-props merged-props)))
|
;; Extract only the explicitly listed external properties to preserve
|
||||||
(if (equal old-text (substring-no-properties new-text))
|
(dolist (prop-name tp-preserve-external-properties)
|
||||||
;; Text content is the same, but properties may differ
|
(let ((val (plist-get existing-props prop-name)))
|
||||||
;; Use set-text-properties to replace with final properties
|
(when (and val (not (plist-member merged-props prop-name)))
|
||||||
(set-text-properties m-start m-end final-props)
|
(setq preserved-props
|
||||||
;; Text content is different - delete old text and insert new
|
(plist-put preserved-props prop-name val)))))
|
||||||
(delete-region m-start m-end)
|
;; Combine merged-props with preserved external props
|
||||||
(goto-char m-start)
|
(let ((final-props (append merged-props preserved-props)))
|
||||||
(insert (substring-no-properties new-text))
|
(if (equal old-text (substring-no-properties new-text))
|
||||||
;; Apply final properties
|
;; Text content is the same, but properties may differ
|
||||||
(let ((new-end (+ m-start (length new-text))))
|
;; Use set-text-properties to replace with final properties
|
||||||
(set-text-properties m-start new-end final-props))))
|
(set-text-properties m-start m-end final-props)
|
||||||
|
;; Text content is different - delete old text and insert new
|
||||||
|
(delete-region m-start m-end)
|
||||||
|
(goto-char m-start)
|
||||||
|
(insert (substring-no-properties new-text))
|
||||||
|
;; Apply final properties
|
||||||
|
(let ((new-end (+ m-start (length new-text))))
|
||||||
|
(set-text-properties m-start new-end final-props)))))
|
||||||
;; Search for next match
|
;; Search for next match
|
||||||
(setq match (text-property-search-forward 'tp-name layer-name t)))))
|
(setq match (text-property-search-forward 'tp-name layer-name t)))))
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user