branch: elpa/gptel
commit 273c0f93958c1ffa85e396717b504903eda36bce
Author: Karthik Chikmagalur <[email protected]>
Commit: Karthik Chikmagalur <[email protected]>
gptel: Avoid text-property stickiness when restoring state
* gptel.el (gptel--restore-props): Wrap in
`with-silent-modifications' to avoid undesirable interactions with
buffer-modification hooks when restoring gptel state.
Specifically, `gptel--inherit-stickiness' can mess up the gptel
text properties as restored from file-local variables or Org
properties. (#1246)
* gptel-org.el (gptel-org--restore-state): Don't control the
buffer-modified flag here. The only buffer modification happens
in `gptel--restore-props', which is handled separately. All other
changes in restoring gptel state involve setting buffer-local
variables, which don't modify the buffer.
* test: Add test for restoring state.
---
gptel-org.el | 66 +++++++++++++++++++++++++++++-------------------------------
gptel.el | 7 ++++---
test | 2 +-
3 files changed, 37 insertions(+), 38 deletions(-)
diff --git a/gptel-org.el b/gptel-org.el
index 4270b59a49..d5a5e94991 100644
--- a/gptel-org.el
+++ b/gptel-org.el
@@ -554,40 +554,38 @@ ARGS are the original function call arguments."
(defun gptel-org--restore-state ()
"Restore gptel state for Org buffers when turning on `gptel-mode'."
(save-restriction
- (let ((modified (buffer-modified-p)))
- (widen)
- (condition-case status
- (progn
- (when-let* ((bounds (org-entry-get (point-min) "GPTEL_BOUNDS")))
- (gptel--restore-props (read bounds)))
- (pcase-let ((`(,preset ,system ,backend ,model ,temperature
,tokens ,num ,tools)
- (gptel-org--entry-properties (point-min))))
- (when preset
- (if (gptel-get-preset preset)
- (progn (gptel--apply-preset
- preset (lambda (sym val) (set (make-local-variable
sym) val)))
- (setq gptel--preset preset))
- (display-warning
- '(gptel presets)
- (format "Could not activate gptel preset `%s' in buffer
\"%s\""
- preset (buffer-name)))))
- (when system (setq-local gptel--system-message system))
- (if backend (setq-local gptel-backend backend)
- (message
- (substitute-command-keys
- (concat
- "Could not activate gptel backend \"%s\"! "
- "Switch backends with \\[universal-argument] \\[gptel-send]"
- " before using gptel."))
- backend))
- (when model (setq-local gptel-model model))
- (when temperature (setq-local gptel-temperature temperature))
- (when tokens (setq-local gptel-max-tokens tokens))
- (when num (setq-local gptel--num-messages-to-send num))
- (when tools (setq-local gptel-tools tools))))
- (:success (message "gptel chat restored."))
- (error (message "Could not restore gptel state, sorry! Error: %s"
status)))
- (set-buffer-modified-p modified))))
+ (widen)
+ (condition-case status
+ (progn
+ (when-let* ((bounds (org-entry-get (point-min) "GPTEL_BOUNDS")))
+ (gptel--restore-props (read bounds)))
+ (pcase-let ((`(,preset ,system ,backend ,model ,temperature ,tokens
,num ,tools)
+ (gptel-org--entry-properties (point-min))))
+ (when preset
+ (if (gptel-get-preset preset)
+ (progn (gptel--apply-preset
+ preset (lambda (sym val) (set (make-local-variable
sym) val)))
+ (setq gptel--preset preset))
+ (display-warning
+ '(gptel presets)
+ (format "Could not activate gptel preset `%s' in buffer
\"%s\""
+ preset (buffer-name)))))
+ (when system (setq-local gptel--system-message system))
+ (if backend (setq-local gptel-backend backend)
+ (message
+ (substitute-command-keys
+ (concat
+ "Could not activate gptel backend \"%s\"! "
+ "Switch backends with \\[universal-argument] \\[gptel-send]"
+ " before using gptel."))
+ backend))
+ (when model (setq-local gptel-model model))
+ (when temperature (setq-local gptel-temperature temperature))
+ (when tokens (setq-local gptel-max-tokens tokens))
+ (when num (setq-local gptel--num-messages-to-send num))
+ (when tools (setq-local gptel-tools tools))))
+ (:success (message "gptel chat restored."))
+ (error (message "Could not restore gptel state, sorry! Error: %s"
status)))))
(defun gptel-org-set-properties (pt &optional msg)
"Store the active gptel configuration under the current heading.
diff --git a/gptel.el b/gptel.el
index 4555652fa2..3c5b142e49 100644
--- a/gptel.el
+++ b/gptel.el
@@ -600,7 +600,9 @@ the gptel property is set to just PROP.
The legacy structure, a list of (BEG . END) is also supported and will be
applied before being re-persisted in the new structure."
- (let ((modified (buffer-modified-p)))
+ ;; Run silently to avoid `gptel--inherit-stickiness' and other hooks that
+ ;; might modify the gptel text property.
+ (with-silent-modifications
(if (symbolp (caar bounds-alist))
(mapc
(lambda (bounds)
@@ -621,8 +623,7 @@ applied before being re-persisted in the new structure."
(mapc (lambda (bound)
(add-text-properties
(car bound) (cdr bound) '(gptel response front-sticky (gptel))))
- bounds-alist))
- (set-buffer-modified-p modified)))
+ bounds-alist))))
(defun gptel--restore-state ()
"Restore gptel state when turning on `gptel-mode'."
diff --git a/test b/test
index 318c9dda9e..db2230cb4b 160000
--- a/test
+++ b/test
@@ -1 +1 @@
-Subproject commit 318c9dda9e6dbb3e61311cf4de23f124c4d2960a
+Subproject commit db2230cb4b1c9a3a3dec175a4018bae75097e012