Simplify reactive text replacement to directly reset properties with new values
Co-authored-by: Kinneyzhang <38454496+Kinneyzhang@users.noreply.github.com>
This commit is contained in:
parent
7d7979c54c
commit
6b96d8575f
47
tp.el
47
tp.el
@ -1093,20 +1093,12 @@ 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)))))))))))
|
||||||
|
|
||||||
(defvar tp-preserve-external-properties '(keymap mouse-face cursor pointer help-echo)
|
|
||||||
"List of text properties to preserve during reactive text updates.
|
|
||||||
These properties are typically set by external libraries (like twidget's event
|
|
||||||
system) and should not be overwritten when tp.el updates reactive text.
|
|
||||||
Properties in this list will be preserved from the buffer during updates,
|
|
||||||
while other properties will be replaced with the new values.")
|
|
||||||
|
|
||||||
(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.
|
||||||
+Properties listed in `tp-preserve-external-properties' that were applied
|
The new properties completely reset/replace the old 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
|
||||||
@ -1114,29 +1106,18 @@ PROPS are the properties to apply to the new text.
|
|||||||
(while match
|
(while match
|
||||||
(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)))
|
||||||
;; Preserve only specific external properties
|
(if (equal old-text (substring-no-properties new-text))
|
||||||
(existing-props (text-properties-at m-start))
|
;; Text content is the same, but properties may differ
|
||||||
(preserved-props nil))
|
;; Use set-text-properties to reset with new properties
|
||||||
;; Extract only the explicitly listed external properties to preserve
|
(set-text-properties m-start m-end merged-props)
|
||||||
(dolist (prop-name tp-preserve-external-properties)
|
;; Text content is different - delete old text and insert new
|
||||||
(let ((val (plist-get existing-props prop-name)))
|
(delete-region m-start m-end)
|
||||||
(when (and val (not (plist-member merged-props prop-name)))
|
(goto-char m-start)
|
||||||
(setq preserved-props
|
(insert (substring-no-properties new-text))
|
||||||
(plist-put preserved-props prop-name val)))))
|
;; Apply new properties
|
||||||
;; Combine merged-props with preserved external props
|
(let ((new-end (+ m-start (length new-text))))
|
||||||
(let ((final-props (append merged-props preserved-props)))
|
(set-text-properties m-start new-end merged-props))))
|
||||||
(if (equal old-text (substring-no-properties new-text))
|
|
||||||
;; Text content is the same, but properties may differ
|
|
||||||
;; Use set-text-properties to replace with final properties
|
|
||||||
(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