branch: elpa/gptel
commit 4210b6664f2f464800c5be782cb7bf21c61f56db
Author: Karthik Chikmagalur <karthikchikmaga...@gmail.com>
Commit: Karthik Chikmagalur <karthikchikmaga...@gmail.com>

    gptel: Add gptel-with-preset
    
    * gptel.el (gptel--preset-syms, gptel-with-preset): Add a
    `gptel-with-preset' macro.  This can be used to run code with a
    gptel preset applied.
    
    `gptel-with-preset' works by let-binding all the symbols that the
    preset sets to nil, then calling `gptel--apply-preset' within the
    binding.  These values are cleared when the let clause is exited.
---
 gptel.el | 40 ++++++++++++++++++++++++++++++++++++++++
 test     |  2 +-
 2 files changed, 41 insertions(+), 1 deletion(-)

diff --git a/gptel.el b/gptel.el
index a47e8d056d..4b4879328c 100644
--- a/gptel.el
+++ b/gptel.el
@@ -3428,6 +3428,46 @@ example) apply the preset buffer-locally."
                    (car preset) key)))))
    (cdr preset)))
 
+(defun gptel--preset-syms (preset)
+  "Return a list of gptel variables (symbols) set by PRESET.
+
+PRESET is a spec (plist) of keys and values."
+  (let* ((index preset)
+         syms key val)
+    (while index
+      (setq key (pop index) val (pop index))
+      (pcase key
+        (:description)
+        (:parents
+         (mapc (lambda (parent-preset)
+                 (nconc syms (gptel--preset-syms
+                              (gptel-get-preset parent-preset))))
+               (ensure-list val)))
+        (:system (push 'gptel--system-message syms))
+        (_ (if-let* ((var (or (intern-soft
+                               (concat "gptel-" (substring (symbol-name key) 
1)))
+                              (intern-soft
+                               (concat "gptel--" (substring (symbol-name key) 
1))))))
+               (push var syms)
+             (display-warning
+              '(gptel presets)
+              (format "gptel preset \"%s\": setting for %s not found, 
ignoring."
+                      (car preset) key))))))
+    (cl-delete-duplicates syms)))
+
+(defmacro gptel-with-preset (name &rest body)
+  "Run BODY with gptel preset NAME applied.
+
+This macro can be used to create `gptel-request' command with settings
+from a gptel preset applied.  NAME is the preset name, typically a
+symbol."
+  (declare (indent 1))
+  `(cl-progv (gptel--preset-syms
+              (gptel-get-preset ,(if (symbolp name) `',name name)))
+       nil
+     (gptel--apply-preset ,(if (symbolp name) `',name name))
+     ,@body))
+
 
 ;;; Response tweaking commands
 
diff --git a/test b/test
index 14371c92b0..29d9330ad0 160000
--- a/test
+++ b/test
@@ -1 +1 @@
-Subproject commit 14371c92b0a7671f4b6b7679a1ac0eba3ded478f
+Subproject commit 29d9330ad0d1a7e56a25c60333a3e73c21de4af5

Reply via email to