Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 24 additions & 22 deletions gptel-org.el
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
44 changes: 23 additions & 21 deletions gptel.el
Original file line number Diff line number Diff line change
Expand Up @@ -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'."
Expand Down