Function: gptel--modify-value

gptel--modify-value is a natively compiled function defined in gptel.el.

Signature

(gptel--modify-value ORIGINAL NEW-SPEC)

Documentation

Combine ORIGINAL with NEW-SPEC and return the new result.

This function is non-destructive, ORIGINAL is not modified.

NEW-SPEC is either a declarative action spec (plist) of the form
 (:key val ...), or a simple value. Recognized spec keys are :append,
:prepend, :eval, :function and :merge. If NEW-SPEC does not have this
form it is returned as is.

- :append and :prepend will append/prepend val (a list or string) to ORIGINAL.
  Actions on strings are idempotent, they will only be appended/prepended once.
- :eval will evaluate val and return the result, and
- :function will call val with ORIGINAL as its argument, and return the result.
- :merge will treat ORIGINAL and NEW-SPEC as plists and return a merged plist,
  with NEW-SPEC taking precedence.

Source Code

;; Defined in /nix/store/q3r7g81pvghw7a523kjk2k2zih3pfk8z-emacs-packages-deps/share/emacs/site-lisp/elpa/gptel-20261002.545/gptel.el
;;; Utility functions
(defun gptel--modify-value (original new-spec)
  "Combine ORIGINAL with NEW-SPEC and return the new result.

This function is non-destructive, ORIGINAL is not modified.

NEW-SPEC is either a declarative action spec (plist) of the form
 (:key val ...), or a simple value.  Recognized spec keys are :append,
:prepend, :eval, :function and :merge.  If NEW-SPEC does not have this
form it is returned as is.

- :append and :prepend will append/prepend val (a list or string) to ORIGINAL.
  Actions on strings are idempotent, they will only be appended/prepended once.
- :eval will evaluate val and return the result, and
- :function will call val with ORIGINAL as its argument, and return the result.
- :merge will treat ORIGINAL and NEW-SPEC as plists and return a merged plist,
  with NEW-SPEC taking precedence."
  (if (not (and (consp new-spec) (keywordp (car new-spec))))
      new-spec
    (let ((current original) (tail new-spec))
      (while tail
        (let ((key (pop tail)) (form (pop tail)))
          (setq current
                (pcase key
                  (:append (if (stringp form)
                               (if (string-suffix-p form current t)
                                   current (concat current form))
                             (append current form)))
                  (:prepend (if (stringp form)
                                (if (string-prefix-p form current t)
                                    current (concat form current))
                              (append form current)))
                  (:eval (eval form t))
                  (:function (funcall form current))
                  (:merge (gptel--merge-plists (copy-sequence current) form))
                  (_ new-spec)))))
      current)))