diff --git a/tp-tests.el b/tp-tests.el index 11008fe..26cd768 100644 --- a/tp-tests.el +++ b/tp-tests.el @@ -3066,7 +3066,10 @@ the source text should be used and the reactive variable should be updated." (should (equal (tp-at 0 'tp-text result) "2"))) ;; Now set the variable to a different value and test again (setq tp-test-text-var "18") - ;; Redefine layer to reset resolved props + ;; Redefine layer to reset resolved props to the new variable value. + ;; This is necessary because the layer definition caches the resolved + ;; tp-text value, and we want to test the behavior when the variable + ;; already has a non-nil value at layer application time. (tp-define-layer 'test-init-text-layer :props '(face bold tp-text $tp-test-text-var)) (let ((result (tp-set "2" 'test-init-text-layer))) diff --git a/tp.el b/tp.el index 114b2b9..5eebd3e 100644 --- a/tp.el +++ b/tp.el @@ -668,13 +668,16 @@ NEW-OBJECT is the new string object (only different for strings with tp-text)." ;; This ensures the reactive variable and buffer text stay in sync (when-let ((layer-name (plist-get props 'tp-name))) (when-let ((reactive-var (tp--find-tp-text-reactive-var layer-name))) - ;; Update the reactive variable with the current text (buffer-local) + ;; Update the reactive variable with the current text + ;; Note: Using global `set` here because the layer definition is global. + ;; When the variable is changed, the reactive watcher will update all + ;; buffers that have this layer applied. (set reactive-var current-text) ;; Also update the layer definition so future accesses see the new value (let ((layer-props (cdr (assoc layer-name tp-layer-alist)))) (when layer-props - (setf (cdr (assoc layer-name tp-layer-alist)) - (plist-put layer-props 'tp-text current-text)))))) + (tp--set-layer-props layer-name + (plist-put layer-props 'tp-text current-text)))))) (list (plist-put props 'tp-text current-text) end object))) ;; tp-text has a string value - replace the text in the region ((stringp tp-text-val)