gptel: Declare compat as explicit dependency

* gptel.el (gptel--always, gptel--button-buttonize): Currently
gptel depends on the Compat library transitively via transient.el.
Declare it as an explicit dependency so we can get rid of special
case definitions and simplify.  This also enables us to use Emacs
28 and 29 conveniences freely in the code.
This commit is contained in:
Karthik Chikmagalur 2023-12-29 13:06:25 -08:00
parent 85bd47cb4c
commit 1e31f550de

View file

@ -4,7 +4,7 @@
;; Author: Karthik Chikmagalur ;; Author: Karthik Chikmagalur
;; Version: 0.5.5 ;; Version: 0.5.5
;; Package-Requires: ((emacs "27.1") (transient "0.4.0")) ;; Package-Requires: ((emacs "27.1") (transient "0.4.0") (compat "29.1.4.1"))
;; Keywords: convenience ;; Keywords: convenience
;; URL: https://github.com/karthink/gptel ;; URL: https://github.com/karthink/gptel
@ -112,7 +112,7 @@
(eval-when-compile (eval-when-compile
(require 'subr-x) (require 'subr-x)
(require 'cl-lib)) (require 'cl-lib))
(require 'compat)
(require 'url) (require 'url)
(require 'json) (require 'json)
(require 'map) (require 'map)
@ -298,29 +298,30 @@ transient menu interface provided by `gptel-menu'."
:group 'gptel :group 'gptel
:type 'file) :type 'file)
;; FIXME This is convoluted, but it's not worth adding the `compat' dependency ;; NOTE now testing compat.
;; This is convoluted, but it's not worth adding the `compat' dependency
;; just for a couple of helper functions either. ;; just for a couple of helper functions either.
(cl-macrolet ;; (cl-macrolet
((gptel--compat ;; ((gptel--compat
() (if (version< "28.1" emacs-version) ;; () (if (version< "28.1" emacs-version)
(macroexp-progn ;; (macroexp-progn
`((defalias 'gptel--button-buttonize #'button-buttonize) ;; `((defalias 'gptel--button-buttonize #'button-buttonize)
(defalias 'gptel--always #'always))) ;; (defalias 'gptel--always #'always)))
(macroexp-progn ;; (macroexp-progn
`((defun gptel--always (&rest _) ;; `((defun gptel--always (&rest _)
"Always return t." t) ;; "Always return t." t)
(defun gptel--button-buttonize (string callback) ;; (defun gptel--button-buttonize (string callback)
"Make STRING into a button and return it. ;; "Make STRING into a button and return it.
When clicked, CALLBACK will be called." ;; When clicked, CALLBACK will be called."
(propertize string ;; (propertize string
'face 'button ;; 'face 'button
'button t ;; 'button t
'follow-link t ;; 'follow-link t
'category t ;; 'category t
'button-data nil ;; 'button-data nil
'keymap button-map ;; 'keymap button-map
'action callback))))))) ;; 'action callback)))))))
(gptel--compat)) ;; (gptel--compat))
;; Model and interaction parameters ;; Model and interaction parameters
(defcustom gptel-directives (defcustom gptel-directives
@ -337,11 +338,11 @@ Each entry in this alist maps a symbol naming the directive to
the string that is sent. To set the directive for a chat session the string that is sent. To set the directive for a chat session
interactively call `gptel-send' with a prefix argument." interactively call `gptel-send' with a prefix argument."
:group 'gptel :group 'gptel
:safe #'gptel--always :safe #'always
:type '(alist :key-type symbol :value-type string)) :type '(alist :key-type symbol :value-type string))
(defvar-local gptel--system-message (alist-get 'default gptel-directives)) (defvar-local gptel--system-message (alist-get 'default gptel-directives))
(put 'gptel--system-message 'safe-local-variable #'gptel--always) (put 'gptel--system-message 'safe-local-variable #'always)
(defcustom gptel-max-tokens nil (defcustom gptel-max-tokens nil
"Max tokens per response. "Max tokens per response.
@ -357,7 +358,7 @@ If left unset, ChatGPT will target about 40% of the total token
count of the conversation so far in each message, so messages count of the conversation so far in each message, so messages
will get progressively longer!" will get progressively longer!"
:local t :local t
:safe #'gptel--always :safe #'always
:group 'gptel :group 'gptel
:type '(choice (integer :tag "Specify Token count") :type '(choice (integer :tag "Specify Token count")
(const :tag "Default" nil))) (const :tag "Default" nil)))
@ -374,7 +375,7 @@ The current options for ChatGPT are
To set the model for a chat session interactively call To set the model for a chat session interactively call
`gptel-send' with a prefix argument." `gptel-send' with a prefix argument."
:local t :local t
:safe #'gptel--always :safe #'always
:group 'gptel :group 'gptel
:type '(choice :type '(choice
(const :tag "GPT 3.5 turbo" "gpt-3.5-turbo") (const :tag "GPT 3.5 turbo" "gpt-3.5-turbo")
@ -391,7 +392,7 @@ of the response, with 2.0 being the most random.
To set the temperature for a chat session interactively call To set the temperature for a chat session interactively call
`gptel-send' with a prefix argument." `gptel-send' with a prefix argument."
:local t :local t
:safe #'gptel--always :safe #'always
:group 'gptel :group 'gptel
:type 'number) :type 'number)
@ -415,10 +416,10 @@ with differing settings.")
(defvar-local gptel-backend gptel--openai) (defvar-local gptel-backend gptel--openai)
(defvar-local gptel--bounds nil) (defvar-local gptel--bounds nil)
(put 'gptel--bounds 'safe-local-variable #'gptel--always) (put 'gptel--bounds 'safe-local-variable #'always)
(defvar-local gptel--num-messages-to-send nil) (defvar-local gptel--num-messages-to-send nil)
(put 'gptel--num-messages-to-send 'safe-local-variable #'gptel--always) (put 'gptel--num-messages-to-send 'safe-local-variable #'always)
(defvar gptel--debug nil (defvar gptel--debug nil
"Enable printing debug messages. "Enable printing debug messages.
@ -498,7 +499,7 @@ Note: This will move the cursor."
Note: Changing this variable does not affect gptel\\='s behavior Note: Changing this variable does not affect gptel\\='s behavior
in any way.") in any way.")
(put 'gptel--backend-name 'safe-local-variable #'gptel--always) (put 'gptel--backend-name 'safe-local-variable #'always)
(defun gptel--restore-backend (name) (defun gptel--restore-backend (name)
"Activate gptel backend with NAME in current buffer. "Activate gptel backend with NAME in current buffer.
@ -641,20 +642,20 @@ file."
(propertize (propertize
" " 'display `(space :align-to ,(max 1 (- (window-width) (+ 2 l1 l2))))) " " 'display `(space :align-to ,(max 1 (- (window-width) (+ 2 l1 l2)))))
(propertize (propertize
(gptel--button-buttonize num-exchanges (buttonize num-exchanges
(lambda (&rest _) (gptel-menu))) (lambda (&rest _) (gptel-menu)))
'mouse-face 'highlight 'mouse-face 'highlight
'help-echo 'help-echo
"Number of past exchanges to include with each request") "Number of past exchanges to include with each request")
" " " "
(propertize (propertize
(gptel--button-buttonize (concat "[" gptel-model "]") (buttonize (concat "[" gptel-model "]")
(lambda (&rest _) (gptel-menu))) (lambda (&rest _) (gptel-menu)))
'mouse-face 'highlight 'mouse-face 'highlight
'help-echo "GPT model in use")))))) 'help-echo "GPT model in use"))))))
(setq mode-line-process (setq mode-line-process
'(:eval (concat " " '(:eval (concat " "
(gptel--button-buttonize gptel-model (buttonize gptel-model
(lambda (&rest _) (gptel-menu)))))))) (lambda (&rest _) (gptel-menu))))))))
(if gptel-use-header-line (if gptel-use-header-line
(setq header-line-format gptel--old-header-line (setq header-line-format gptel--old-header-line
@ -672,7 +673,7 @@ file."
(setq mode-line-process (propertize msg 'face face)) (setq mode-line-process (propertize msg 'face face))
(setq mode-line-process (setq mode-line-process
'(:eval (concat " " '(:eval (concat " "
(gptel--button-buttonize gptel-model (buttonize gptel-model
(lambda (&rest _) (gptel-menu)))))) (lambda (&rest _) (gptel-menu))))))
(message (propertize msg 'face face)))) (message (propertize msg 'face face))))
(force-mode-line-update))) (force-mode-line-update)))