branch: elpa/gptel
commit 2e9adaed4dd236e0779604d99c0f0b9dff4193d1
Author: Henrik Ahlgren <pa...@seestieto.com>
Commit: GitHub <nore...@github.com>

    gptel: Restoring state should not flag the buffer modified (#711)
    
    * gptel.el (gptel--restore-props): Save `buffer-modified-p' before
    restoring properties and revert it to its original state.  This
    prevents a visited file from being marked as modified until the user
    actually makes changes.  This is particularly useful when multiple
    files are accessed automatically, such as from the `org-agenda-files'
    location (otherwise when exiting Emacs the user is asked about each
    file if it needs to be saved).
    
    * gptel-org.el (gptel-org--restore-state): Likewise for Org.
---
 gptel-org.el | 46 ++++++++++++++++++++++++----------------------
 gptel.el     | 44 +++++++++++++++++++++++---------------------
 2 files changed, 47 insertions(+), 43 deletions(-)

diff --git a/gptel-org.el b/gptel-org.el
index f5f9256a02..3aedd90869 100644
--- a/gptel-org.el
+++ b/gptel-org.el
@@ -451,28 +451,30 @@ 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
-    (widen)
-    (condition-case status
-        (progn
-          (when-let* ((bounds (org-entry-get (point-min) "GPTEL_BOUNDS")))
-            (gptel--restore-props (read bounds)))
-          (pcase-let ((`(,system ,backend ,model ,temperature ,tokens ,num)
-                       (gptel-org--entry-properties (point-min))))
-            (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))))
-      (:success (message "gptel chat restored."))
-      (error (message "Could not restore gptel state, sorry! Error: %s" 
status)))))
+    (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 ((`(,system ,backend ,model ,temperature ,tokens ,num)
+                         (gptel-org--entry-properties (point-min))))
+              (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))))
+        (:success (message "gptel chat restored."))
+        (error (message "Could not restore gptel state, sorry! Error: %s" 
status)))
+      (set-buffer-modified-p modified))))
 
 (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 8b530060c6..ff9c0cf2d1 100644
--- a/gptel.el
+++ b/gptel.el
@@ -1226,27 +1226,29 @@ 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."
-  (if (symbolp (caar bounds-alist))
-      (mapc
-       (lambda (bounds)
-         (let* ((prop (pop bounds)))
-           (mapc
-            (lambda (bound)
-              (let ((prop-has-val (> (length bound) 2)))
-                (add-text-properties
-                 (pop bound) (pop bound)
-                 (if (eq prop 'response)
-                     '(gptel response front-sticky (gptel))
-                   (list 'gptel
-                         (if prop-has-val
-                             (cons prop (pop bound))
-                           prop))))))
-            bounds)))
-       bounds-alist)
-    (mapc (lambda (bound)
-            (add-text-properties
-             (car bound) (cdr bound) '(gptel response front-sticky (gptel))))
-          bounds-alist)))
+  (let ((modified (buffer-modified-p)))
+    (if (symbolp (caar bounds-alist))
+        (mapc
+         (lambda (bounds)
+           (let* ((prop (pop bounds)))
+             (mapc
+              (lambda (bound)
+                (let ((prop-has-val (> (length bound) 2)))
+                  (add-text-properties
+                   (pop bound) (pop bound)
+                   (if (eq prop 'response)
+                       '(gptel response front-sticky (gptel))
+                     (list 'gptel
+                           (if prop-has-val
+                               (cons prop (pop bound))
+                             prop))))))
+              bounds)))
+         bounds-alist)
+      (mapc (lambda (bound)
+              (add-text-properties
+               (car bound) (cdr bound) '(gptel response front-sticky (gptel))))
+            bounds-alist))
+    (set-buffer-modified-p modified)))
 
 (defun gptel--restore-state ()
   "Restore gptel state when turning on `gptel-mode'."

Reply via email to