stuhlmueller / gpt.el

A simple Emacs package for calling instruction-following language models
MIT License
68 stars 10 forks source link

Features: use markdown-mode & named buffers #15

Closed AnselmC closed 3 weeks ago

AnselmC commented 4 weeks ago

Hey, thanks for this package! I've been using it more extensively recently and was wondering what your opinion of these two features is and whether or not I should create a PR.

  1. Derive from markdown mode if available since the streamed output is formatted as markdown. Something like

    (define-derived-mode gpt-mode markdown-mode "GPT"
    "A mode for displaying the output of GPT commands."
    (setq-local word-wrap t)
    (setq markdown-fontify-code-blocks-natively t)
    ;; Combine markdown-mode's keywords with custom keywords
    (setq font-lock-defaults
        (list (append markdown-mode-font-lock-keywords gpt-font-lock-keywords)))
    (add-to-invisibility-spec 'gpt-prefix))

    (I haven't yet figured out how to make it only derive from markdown mode conditionally, i.e. when the user has it installed) But this results in very nicely parseable output:

    image
  2. Use named buffer I find it useful to have multiple ongoing 'conversation' threads with gpt. However, the temporary buffers don't allow me to search for them in the buffer list and I often accidentally close them. I've been using something like the following to have named buffers that I can search and switch to. What do you think about this?

    
    (defvar gpt-buffer-counter 0
    "Counter to ensure unique buffer names for GPT output buffers.")

(defun gpt-create-output-buffer (command n) "Create or get a named buffer to capture the output of the GPT process. The buffer name is derived from the first N characters of COMMAND. Use the `gpt-mode' for the output buffer." (let ((buffer-name (concat "gpt" "[" (number-to-string gpt-buffer-counter) "]:" (substring command 0 (min n (length command))) "..." "*")) (output-buffer (get-buffer-create buffer-name))) (setq gpt-buffer-counter (1+ gpt-buffer-counter)) ; Increment the counter (with-current-buffer output-buffer (gpt-mode)) output-buffer))

stuhlmueller commented 4 weeks ago

These are great ideas! Please make a PR.

(Could even generate the buffer name using a language model but maybe that's too fancy.)

AnselmC commented 3 weeks ago

Cool, I did that. Good idea around generating buffer name, I'll create a separate PR for that though