Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore gptel-mode in chat buffers #491

Closed
metachip opened this issue Dec 2, 2024 · 5 comments
Closed

Restore gptel-mode in chat buffers #491

metachip opened this issue Dec 2, 2024 · 5 comments
Labels
enhancement New feature or request

Comments

@metachip
Copy link

metachip commented Dec 2, 2024

When chat buffer is saved to a file, gptel prepends a properties drawer to capture the metadata for the chat. When the chat buffer is reopened, the mode will be org mode or markdown or whatever, based on the file suffix (presuming one save it that way). However, when opening a saved chat buffer, I generally want it to automatically be opened in gptel-mode.

If a mode line does not yet exist in the buffer, why not add one just before the PROPERTIES drawer when saving the chat buffer as follows:

# -*- eval: (gptel-mode 1) -*-

This would ensure that the newly reopened chat buffer would be automatically opened in gptel-mode. This has another benefit as the content of the saved chat shows the purpose of the file which would otherwise only known by way of the file suffix.

@metachip metachip added the enhancement New feature or request label Dec 2, 2024
@karthink
Copy link
Owner

karthink commented Dec 2, 2024

I do this in my personal configuration:

(defun my/gptel-mode-auto ()
  "Ensure that this file opens with `gptel-mode' enabled."
  (save-excursion
    (modify-file-local-variable-prop-line
     'eval nil 'delete)
    (add-file-local-variable-prop-line
     'eval '(and (fboundp 'gptel-mode) (gptel-mode 1)))))

(add-hook 'gptel-save-state-hook #'my/gptel-mode-auto)

I haven't had any requests from users to be able to turn on gptel-mode automatically, so I haven't bothered.

EDIT: the previous version I posted wasn't robust.

@metachip
Copy link
Author

metachip commented Dec 20, 2024

For reasons unknown gptel no longer prepends a properties drawer when a chat buffer is saved. Any idea what might have changed. The hook is also not run any more. I don't think I've changed anything? If I open a buffer that has the properties drawer, it is read and the variables are used. If I remove the hook as you've defined it. the properties drawer is again run. I'm running gptel at commit 0ce1628.

@karthink
Copy link
Owner

karthink commented Dec 20, 2024 via email

@metachip
Copy link
Author

metachip commented Dec 20, 2024

It appears your original version was failing if a property line did not already exist at the point of saving.

Error: (void-function modify-file-local-variable-prop-line)

I instrumented the function as

(defun my/gptel-mode-auto ()
  "Ensure that this file opens with `gptel-mode' enabled."
  (message "Running my/gptel-mode-auto in buffer %s" (buffer-name))
  (condition-case err
      (save-excursion
        (modify-file-local-variable-prop-line
         'eval nil 'delete)
        (add-file-local-variable-prop-line
         'eval '(and (fboundp 'gptel-mode) (gptel-mode 1))))
    (error (message "%S" err))))

which reported:

Running my/gptel-mode-auto in buffer muse-sonnet.org
The -*- line not found

I modified the function as:

(defun my/gptel-mode-auto ()
  "Ensure that this file opens with `gptel-mode' enabled."
  (save-excursion
    (let ((enable-local-variables t))  ; Ensure we can modify local variables
      (if (and (save-excursion
                 (goto-char (point-min))
                 (looking-at ".*-\\*-")))  ; If there's a -*- line
        ;; First remove any existing eval, then add the new one
        (modify-file-local-variable-prop-line
          'eval nil 'delete))
      ;; Always add our eval
      (add-file-local-variable-prop-line
        'eval '(and (fboundp 'gptel-mode) (gptel-mode 1))))))

This appears to work. It first enables local variable modification in the off chance that it's disabled, checks if there's a property line, and removes the eval variable if it exists, and then replaces it. This leaves any other properties in the property line unchanged. This is what I need as I use the property line to give the buffers a title unrelated to the file name.

Hope this helps.

@karthink
Copy link
Owner

karthink commented Jan 2, 2025

This appears to work. It first enables local variable modification in the off chance that it's disabled, checks if there's a property line, and removes the eval variable if it exists, and then replaces it. This leaves any other properties in the property line unchanged. This is what I need as I use the property line to give the buffers a title unrelated to the file name.

Thank you. Could you add this to the wiki? I'm not inclined to include this in gptel yet, but in the meantime the wiki is a better place for it than this thread.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants