Add command to format code with gptel

This commit is contained in:
Jeremy Dormitzer 2024-08-22 16:02:54 -04:00
parent af5e7ab1e0
commit 59ab43f820

View File

@ -5,6 +5,7 @@
(use-package gptel (use-package gptel
:commands (gptel-request) :commands (gptel-request)
:autoload (gptel--strip-mode-suffix)
:init :init
;; Annoyingly, gptel fails to require gptel-context, so we have to do it manually ;; Annoyingly, gptel fails to require gptel-context, so we have to do it manually
(autoload 'gptel-context-add "gptel-context") (autoload 'gptel-context-add "gptel-context")
@ -81,6 +82,27 @@
(when options-plist (when options-plist
(plist-put prompts-plist :options options-plist)) (plist-put prompts-plist :options options-plist))
prompts-plist)) prompts-plist))
(defun gptel-format ()
"Format the current region or buffer with gptel."
(interactive)
(let* ((subject (if (derived-mode-p 'prog-mode)
(format "%s code" (gptel--strip-mode-suffix major-mode))
"prose"))
(output-format (if (derived-mode-p 'prog-mode)
"code"
"text"))
start
end)
(if (use-region-p)
(setq start (region-beginning)
end (region-end))
(setq start (point-min)
end (point-max)))
(goto-char start)
(push-mark end t t)
(setq mark-active t)
(setq gptel--rewrite-message (format "Format this %s according to industry standards. Generate only %s, no explanation, no code fences." subject output-format))
(gptel-rewrite-menu)))
(add-to-list 'gptel-directives '(shell-command . "You are a command line helper. Generate shell commands that do what is requested, without any additional description or explanation. Reply in plain text with no Markdown or other syntax. Reply with the command only.")) (add-to-list 'gptel-directives '(shell-command . "You are a command line helper. Generate shell commands that do what is requested, without any additional description or explanation. Reply in plain text with no Markdown or other syntax. Reply with the command only."))
(add-to-list 'gptel-directives '(org-mode . "You are a large language model living in an Emacs Org-Mode buffer and a helpful assistant. You may evaluate Emacs Lisp, Python, and shell-script code when necessary by outputting an Org-Mode source block. You don't need to ask for confirmation before evaluating code. The user will execute the source block and display the results in the buffer. Respond concisely. (add-to-list 'gptel-directives '(org-mode . "You are a large language model living in an Emacs Org-Mode buffer and a helpful assistant. You may evaluate Emacs Lisp, Python, and shell-script code when necessary by outputting an Org-Mode source block. You don't need to ask for confirmation before evaluating code. The user will execute the source block and display the results in the buffer. Respond concisely.
@ -257,9 +279,10 @@ Assistant: Write a short story about a dragon who discovers a hidden talent that
"s" #'gptel-send "s" #'gptel-send
"m" #'gptel-menu "m" #'gptel-menu
"b" #'gptel-chat-with-buffer "b" #'gptel-chat-with-buffer
"f" #'gptel-format
"B" #'gptel-select-backend "B" #'gptel-select-backend
"a" #'gptel-context-add "a" #'gptel-context-add
"f" #'gptel-context-add-file "F" #'gptel-context-add-file
"k" #'gptel-abort)) "k" #'gptel-abort))
(use-package gptel-quick (use-package gptel-quick