branch: elpa/gptel commit 65846e7ebdf08483d63386ba53c631a0fcc0ee8a Author: Karthik Chikmagalur <karthikchikmaga...@gmail.com> Commit: Karthik Chikmagalur <karthikchikmaga...@gmail.com>
gptel: Linting and formatting, use when-let* * gptel.el (gptel--url-get-response, gptel--handle-error, gptel--fsm-transition, gptel--restore-state, gptel-auto-scroll, gptel--get-api-key, gptel-api-key-from-auth-source): Use `when-let*' and `if-let*' everywhere. `when-let' and `if-let' have been obsoleted in Emacs 30+. * gptel-transient.el (gptel--read-crowdsourced-prompt, gptel--transient-read-number): Ditto. * gptel-rewrite.el (gptel--rewrite-merge, gptel--rewrite-ediff, gptel--rewrite-diff): Ditto. * gptel-org.el (gptel-org--restore-state): Ditto. * gptel-openai.el (gptel-make-openai, gptel--wrap-user-prompt): Ditto. * gptel-openai-extras.el (gptel-make-perplexity, gptel-make-privategpt): Ditto. * gptel-kagi.el (gptel--parse-buffer): Ditto. * gptel-gemini.el (gptel--wrap-user-prompt): Ditto. * gptel-curl.el (gptel-curl--stream-filter, gptel-curl--get-args): Ditto. * gptel-context.el (gptel-context-confirm, gptel-context--buffer-setup, gptel-context-remove): Ditto. * gptel-anthropic.el (gptel-make-anthropic, gptel--wrap-user-prompt): Ditto. --- gptel-anthropic.el | 8 ++++---- gptel-context.el | 28 ++++++++++++++-------------- gptel-curl.el | 14 +++++++------- gptel-gemini.el | 4 ++-- gptel-kagi.el | 4 ++-- gptel-openai-extras.el | 12 ++++++------ gptel-openai.el | 6 +++--- gptel-org.el | 10 +++++----- gptel-rewrite.el | 6 +++--- gptel-transient.el | 8 ++++---- gptel.el | 38 +++++++++++++++++++------------------- 11 files changed, 69 insertions(+), 69 deletions(-) diff --git a/gptel-anthropic.el b/gptel-anthropic.el index bc610929f3..3565893ad5 100644 --- a/gptel-anthropic.el +++ b/gptel-anthropic.el @@ -358,7 +358,7 @@ If INJECT-MEDIA is non-nil wrap it with base64-encoded media files in the context." (if inject-media ;; Wrap the first user prompt with included media files/contexts - (when-let ((media-list (gptel-context--collect-media))) + (when-let* ((media-list (gptel-context--collect-media))) (cl-callf (lambda (current) (vconcat (gptel--anthropic-parse-multipart media-list) @@ -371,13 +371,13 @@ files in the context." (cl-callf (lambda (current) (cl-etypecase current (string (gptel-context--wrap current)) - (vector (if-let ((wrapped (gptel-context--wrap nil))) + (vector (if-let* ((wrapped (gptel-context--wrap nil))) (vconcat `((:type "text" :text ,wrapped)) current) current)))) (plist-get (car (last prompts)) :content)))) -;; (if-let ((context-string (gptel-context--string gptel-context--alist))) +;; (if-let* ((context-string (gptel-context--string gptel-context--alist))) ;; (cl-callf (lambda (previous) ;; (cl-typecase previous ;; (string (concat context-string previous)) @@ -462,7 +462,7 @@ comparison table: (cl-defun gptel-make-anthropic (name &key curl-args stream key request-params (header - (lambda () (when-let (key (gptel--get-api-key)) + (lambda () (when-let* ((key (gptel--get-api-key))) `(("x-api-key" . ,key) ("anthropic-version" . "2023-06-01") ("anthropic-beta" . "pdfs-2024-09-25") diff --git a/gptel-context.el b/gptel-context.el index 24a2bbea22..bae1748cfc 100644 --- a/gptel-context.el +++ b/gptel-context.el @@ -248,12 +248,12 @@ If CONTEXT is a directory, recursively removes all files in it." (setf (alist-get context gptel-context--alist nil 'remove #'equal) nil) (message "File \"%s\" removed from context." context))) ((region-active-p) - (when-let ((contexts (gptel-context--in-region (current-buffer) - (region-beginning) - (region-end)))) + (when-let* ((contexts (gptel-context--in-region (current-buffer) + (region-beginning) + (region-end)))) (cl-loop for ctx in contexts do (delete-overlay ctx)))) (t - (when-let ((ctx (gptel-context--at-point))) + (when-let* ((ctx (gptel-context--at-point))) (delete-overlay ctx))))) (defun gptel-context-remove-all (&optional verbose) @@ -481,14 +481,14 @@ context overlays, see `gptel-context--alist'." (insert (propertize (format "In file %s:\n\n" (file-name-nondirectory buf)) 'face 'bold)) (setq beg (point)) - (if-let ((mime (plist-get ovs :mime))) + (if-let* ((mime (plist-get ovs :mime))) ;; BUF is a binary file - (if-let (((string-match-p (image-file-name-regexp) buf)) + (if-let* (((string-match-p (image-file-name-regexp) buf)) (img (create-image buf))) (insert-image img "*") ; Can be displayed (insert buf " " (propertize "(No preview for binary file)" - 'face '(:inherit shadow :slant italic)))) + 'face '(:inherit shadow :slant italic)))) (insert-file-contents buf)) (goto-char (point-max)) (insert "\n") @@ -605,13 +605,13 @@ If non-nil, indicates backward movement.") "Confirm pending operations and return to gptel's menu." (interactive) ;; Delete all the context overlays that have been marked for deletion. - (when-let ((deletion-marks - (delq nil (mapcar - (lambda (ov) - (and - (overlay-get ov 'gptel-context-deletion-mark) - (overlay-get ov 'gptel-context))) - (overlays-in (point-min) (point-max)))))) + (when-let* ((deletion-marks + (delq nil (mapcar + (lambda (ov) + (and + (overlay-get ov 'gptel-context-deletion-mark) + (overlay-get ov 'gptel-context))) + (overlays-in (point-min) (point-max)))))) (mapc #'gptel-context-remove deletion-marks) (gptel-context--collect) ;Update contexts and revert buffer (#482) (revert-buffer)) diff --git a/gptel-curl.el b/gptel-curl.el index 77eeca3842..34d86fb273 100644 --- a/gptel-curl.el +++ b/gptel-curl.el @@ -61,7 +61,7 @@ REQUEST-DATA is the data to send, TOKEN is a unique identifier." (data-json (encode-coding-string (gptel--json-encode data) 'utf-8)) (headers (append '(("Content-Type" . "application/json")) - (when-let ((header (gptel-backend-header gptel-backend))) + (when-let* ((header (gptel-backend-header gptel-backend))) (if (functionp header) (funcall header) header))))) (when gptel-log-level @@ -285,14 +285,14 @@ See `gptel--url-get-response' for details." (plist-put proc-info :status (string-trim http-msg)) (gptel--fsm-transition fsm)))) - (when-let ((http-msg (plist-get proc-info :status)) - (http-status (plist-get proc-info :http-status))) + (when-let* ((http-msg (plist-get proc-info :status)) + (http-status (plist-get proc-info :http-status))) ;; Find data chunk(s) and run callback ;; FIXME Handle the case where HTTP 100 is followed by HTTP (not 200) BUG #194 - (when-let (((member http-status '("200" "100"))) - (response ;; (funcall (plist-get proc-info :parser) nil proc-info) - (gptel-curl--parse-stream (plist-get proc-info :backend) proc-info)) - ((not (equal response "")))) + (when-let* (((member http-status '("200" "100"))) + (response ;; (funcall (plist-get proc-info :parser) nil proc-info) + (gptel-curl--parse-stream (plist-get proc-info :backend) proc-info)) + ((not (equal response "")))) (funcall (or (plist-get proc-info :callback) #'gptel-curl--stream-insert-response) response proc-info)))))) diff --git a/gptel-gemini.el b/gptel-gemini.el index b562217c87..4018b3f021 100644 --- a/gptel-gemini.el +++ b/gptel-gemini.el @@ -295,14 +295,14 @@ If INJECT-MEDIA is non-nil wrap it with base64-encoded media files in the context." (if inject-media ;; Wrap the first user prompt with included media files/contexts - (when-let ((media-list (gptel-context--collect-media))) + (when-let* ((media-list (gptel-context--collect-media))) (cl-callf (lambda (current) (vconcat (gptel--gemini-parse-multipart media-list) current)) (plist-get (car prompts) :parts))) ;; Wrap the last user prompt with included text contexts (cl-callf (lambda (current) - (if-let ((wrapped (gptel-context--wrap nil))) + (if-let* ((wrapped (gptel-context--wrap nil))) (vconcat `((:text ,wrapped)) current) current)) (plist-get (car (last prompts)) :parts)))) diff --git a/gptel-kagi.el b/gptel-kagi.el index 48e6f404db..2f5c163206 100644 --- a/gptel-kagi.el +++ b/gptel-kagi.el @@ -112,8 +112,8 @@ ;; If the entire contents of the prompt looks like a url, send the url ;; Else send the text of the region (setq prompts - (if-let (((prop-match-p prop)) - (engine (substring model 10))) + (if-let* (((prop-match-p prop)) + (engine (substring model 10))) ;; It's a region of text (list :text prompts) "")))) diff --git a/gptel-openai-extras.el b/gptel-openai-extras.el index f9ef2cad6d..885e4dd75f 100644 --- a/gptel-openai-extras.el +++ b/gptel-openai-extras.el @@ -65,7 +65,7 @@ (while (re-search-forward "^data:" nil t) (save-match-data (if (looking-at " *\\[DONE\\]") - (when-let ((sources-string (plist-get info :sources))) + (when-let* ((sources-string (plist-get info :sources))) (push sources-string content-strs)) (let ((response (gptel--json-read))) (unless (or (plist-get info :sources) @@ -111,8 +111,8 @@ (cl-defun gptel-make-privategpt (name &key curl-args stream key request-params (header - (lambda () (when-let (key (gptel--get-api-key)) - `(("Authorization" . ,(concat "Bearer " key)))))) + (lambda () (when-let* ((key (gptel--get-api-key))) + `(("Authorization" . ,(concat "Bearer " key)))))) (host "localhost:8001") (protocol "http") (models '(private-gpt)) @@ -171,7 +171,7 @@ for." (prog1 backend (setf (alist-get name gptel--known-backends nil nil #'equal) - backend)))) + backend)))) ;;; Perplexity @@ -190,7 +190,7 @@ for." (cl-defmethod gptel--parse-response ((_backend gptel-perplexity) response _info) "Parse Perplexity response RESPONSE." (let ((response-string (map-nested-elt response '(:choices 0 :message :content))) - (citations-string (when-let ((citations (map-elt response :citations))) + (citations-string (when-let* ((citations (map-elt response :citations))) (gptel--perplexity-parse-citations citations)))) (concat response-string citations-string))) @@ -220,7 +220,7 @@ the response." (cl-defun gptel-make-perplexity (name &key curl-args stream key (header - (lambda () (when-let (key (gptel--get-api-key)) + (lambda () (when-let* ((key (gptel--get-api-key))) `(("Authorization" . ,(concat "Bearer " key)))))) (host "api.perplexity.ai") (protocol "https") diff --git a/gptel-openai.el b/gptel-openai.el index a5db964198..c584359605 100644 --- a/gptel-openai.el +++ b/gptel-openai.el @@ -397,7 +397,7 @@ If INJECT-MEDIA is non-nil wrap it with base64-encoded media files in the context." (if inject-media ;; Wrap the first user prompt with included media files/contexts - (when-let ((media-list (gptel-context--collect-media))) + (when-let* ((media-list (gptel-context--collect-media))) (cl-callf (lambda (current) (vconcat (gptel--openai-parse-multipart media-list) @@ -410,7 +410,7 @@ files in the context." (cl-callf (lambda (current) (cl-etypecase current (string (gptel-context--wrap current)) - (vector (if-let ((wrapped (gptel-context--wrap nil))) + (vector (if-let* ((wrapped (gptel-context--wrap nil))) (vconcat `((:type "text" :text ,wrapped)) current) current)))) @@ -420,7 +420,7 @@ files in the context." (cl-defun gptel-make-openai (name &key curl-args models stream key request-params (header - (lambda () (when-let (key (gptel--get-api-key)) + (lambda () (when-let* ((key (gptel--get-api-key))) `(("Authorization" . ,(concat "Bearer " key)))))) (host "api.openai.com") (protocol "https") diff --git a/gptel-org.el b/gptel-org.el index a7bbaada68..4cbc30b3d0 100644 --- a/gptel-org.el +++ b/gptel-org.el @@ -268,16 +268,16 @@ for inclusion into the user prompt for the gptel request." (when (file-readable-p path) ;; Collect text up to this image, and ;; Collect this image - (when-let ((text (string-trim (buffer-substring-no-properties - from-pt (gptel-org--element-begin link))))) + (when-let* ((text (string-trim (buffer-substring-no-properties + from-pt (gptel-org--element-begin link))))) (unless (string-empty-p text) (push (list :text text) parts))) (push (list :media path :mime mime) parts) (setq from-pt (point)))) ((member type '("http" "https" "ftp")) ;; Collect text up to this image, and ;; Collect this image url - (when-let ((text (string-trim (buffer-substring-no-properties - from-pt (gptel-org--element-begin link))))) + (when-let* ((text (string-trim (buffer-substring-no-properties + from-pt (gptel-org--element-begin link))))) (unless (string-empty-p text) (push (list :text text) parts))) (push (list :url raw-link :mime mime) parts) (setq from-pt (point)))))) @@ -354,7 +354,7 @@ ARGS are the original function call arguments." (widen) (condition-case status (progn - (when-let ((bounds (org-entry-get (point-min) "GPTEL_BOUNDS"))) + (when-let* ((bounds (org-entry-get (point-min) "GPTEL_BOUNDS"))) (mapc (pcase-lambda (`(,beg . ,end)) (add-text-properties beg end '(gptel response front-sticky (gptel)))) diff --git a/gptel-rewrite.el b/gptel-rewrite.el index 00b8a7a67e..5c8fe83e6d 100644 --- a/gptel-rewrite.el +++ b/gptel-rewrite.el @@ -292,7 +292,7 @@ BUF is the buffer to modify, defaults to the overlay buffer." (require 'diff) (let* ((newbuf (gptel--rewrite-prepare-buffer ovs)) (diff-buf (diff-no-select - (if-let ((buf-file (buffer-file-name ov-buf))) + (if-let* ((buf-file (buffer-file-name ov-buf))) (expand-file-name buf-file) ov-buf) newbuf switches))) (with-current-buffer diff-buf @@ -309,7 +309,7 @@ BUF is the buffer to modify, defaults to the overlay buffer." (hideshow (lambda (&optional restore) (dolist (ov (ensure-list ovs)) - (when-let ((overlay-buffer ov)) + (when-let* ((overlay-buffer ov)) (let ((disp (overlay-get ov 'display)) (stored (overlay-get ov 'gptel--ediff))) (overlay-put ov 'display (and restore stored)) @@ -333,7 +333,7 @@ BUF is the buffer to modify, defaults to the overlay buffer." (let ((changed)) (dolist (ov (ensure-list ovs)) (save-excursion - (when-let (new-str (overlay-get ov 'gptel-rewrite)) + (when-let* ((new-str (overlay-get ov 'gptel-rewrite))) ;; Insert merge (goto-char (overlay-start ov)) (unless (bolp) (insert "\n")) diff --git a/gptel-transient.el b/gptel-transient.el index eba2eda53b..d2c686932c 100644 --- a/gptel-transient.el +++ b/gptel-transient.el @@ -174,14 +174,14 @@ will toggle its visibility state." prefix max-width nil nil 'ellipsis)))))))) -(defun gptel--transient-read-number (prompt initial-input history) +(defun gptel--transient-read-number (prompt _initial-input history) "Read a numeric value from the minibuffer. -PROMPT, INITIAL-INPUT and HISTORY are as in the transient reader +PROMPT, _INITIAL-INPUT and HISTORY are as in the transient reader documention. Return nil if user does not provide a number, for default." ;; Workaround for buggy transient behaviour when dealing with ;; non-string values. See: https://github.com/magit/transient/issues/172 - (when-let ((val (symbol-value history))) + (when-let* ((val (symbol-value history))) (when (not (stringp (car val))) (setcar val (number-to-string (car val))))) (let* ((minibuffer-default-prompt-format "") @@ -1425,7 +1425,7 @@ This uses the prompts in the variable cands)))) (complete-with-action action gptel--crowdsourced-prompts str pred))) nil t))) - (when-let ((prompt (gethash choice gptel--crowdsourced-prompts))) + (when-let* ((prompt (gethash choice gptel--crowdsourced-prompts))) (gptel--set-with-scope 'gptel--system-message prompt gptel--set-buffer-locally) (gptel--edit-directive 'gptel--system-message))) diff --git a/gptel.el b/gptel.el index c9c8ea086f..9525475125 100644 --- a/gptel.el +++ b/gptel.el @@ -783,13 +783,13 @@ or "Lookup api key in the auth source. By default, the LLM host for the active backend is used as HOST, and \"apikey\" as USER." - (if-let ((secret - (plist-get - (car (auth-source-search - :host (or host (gptel-backend-host gptel-backend)) - :user (or user "apikey") - :require '(:secret))) - :secret))) + (if-let* ((secret + (plist-get + (car (auth-source-search + :host (or host (gptel-backend-host gptel-backend)) + :user (or user "apikey") + :require '(:secret))) + :secret))) (if (functionp secret) (encode-coding-string (funcall secret) 'utf-8) secret) @@ -798,11 +798,11 @@ and \"apikey\" as USER." ;; FIXME Should we utf-8 encode the api-key here? (defun gptel--get-api-key (&optional key) "Get api key from KEY, or from `gptel-api-key'." - (when-let ((key-sym (or key (gptel-backend-key gptel-backend)))) + (when-let* ((key-sym (or key (gptel-backend-key gptel-backend)))) (cl-typecase key-sym (function (string-trim-right (funcall key-sym) "[\n\r]+")) (string (string-trim-right key-sym "[\n\r]+")) - (symbol (if-let ((val (symbol-value key-sym))) + (symbol (if-let* ((val (symbol-value key-sym))) (gptel--get-api-key val) (error "`gptel-api-key' is not valid"))) (t (error "`gptel-api-key' is not valid"))))) @@ -851,9 +851,9 @@ Later plists in the sequence take precedence over earlier ones." "Scroll window if LLM response continues below viewport. Note: This will move the cursor." - (when-let ((win (get-buffer-window (current-buffer) 'visible)) - ((not (pos-visible-in-window-p (point) win))) - (scroll-error-top-bottom t)) + (when-let* ((win (get-buffer-window (current-buffer) 'visible)) + ((not (pos-visible-in-window-p (point) win))) + (scroll-error-top-bottom t)) (condition-case nil (with-selected-window win (scroll-up-command)) @@ -1132,9 +1132,9 @@ Valid JSON unless NO-JSON is t." gptel--bounds) (message "gptel chat restored.")) (when gptel--backend-name - (if-let ((backend (alist-get - gptel--backend-name gptel--known-backends - nil nil #'equal))) + (if-let* ((backend (alist-get + gptel--backend-name gptel--known-backends + nil nil #'equal))) (setq-local gptel-backend backend) (message (substitute-command-keys @@ -1705,7 +1705,7 @@ automatically from MACHINE's transition table." (push (gptel-fsm-state machine) (plist-get (gptel-fsm-info machine) :history)) (setf (gptel-fsm-state machine) new-state) - (when-let ((handlers (alist-get new-state (gptel-fsm-handlers machine)))) + (when-let* ((handlers (alist-get new-state (gptel-fsm-handlers machine)))) (mapc (lambda (h) (funcall h machine)) handlers))) (defun gptel--fsm-next (machine) @@ -1900,9 +1900,9 @@ Run post-response hooks." (buffer-local-value 'gptel-backend gptel-buffer)))) (if (stringp error-data) (message "%s error: (%s) %s" backend-name http-msg (string-trim error-data)) - (when-let ((error-type (plist-get error-data :type))) + (when-let* ((error-type (plist-get error-data :type))) (setq http-msg (concat "(" http-msg ") " (string-trim error-type)))) - (when-let ((error-msg (plist-get error-data :message))) + (when-let* ((error-msg (plist-get error-data :message))) (message "%s error: (%s) %s" backend-name http-msg (string-trim error-msg)))) (with-current-buffer gptel-buffer (when gptel-mode @@ -2518,7 +2518,7 @@ the response is inserted into the current buffer after point." (url-request-method "POST") (url-request-extra-headers (append '(("Content-Type" . "application/json")) - (when-let ((header (gptel-backend-header gptel-backend))) + (when-let* ((header (gptel-backend-header gptel-backend))) (if (functionp header) (funcall header) header)))) (info (gptel-fsm-info fsm))