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

Move point to the end of inserted text when inserting into a buffer #269

Closed
jwr opened this issue Mar 29, 2024 · 3 comments
Closed

Move point to the end of inserted text when inserting into a buffer #269

jwr opened this issue Mar 29, 2024 · 3 comments

Comments

@jwr
Copy link

jwr commented Mar 29, 2024

I would suggest that gptel move the point to the end of the inserted text when generating into a buffer. Right now, if I'm running gptel several times, each reply gets inserted before the previous one. I expected to be able to generate several replies into a buffer, one after another, separated by a newline.

I tried (add-hook 'gptel-post-response-functions 'gptel-end-of-response), but that only seems to work for the gptel buffer, not for other text buffers (I am testing with *scratch* in Markdown mode).

karthink added a commit that referenced this issue Mar 29, 2024
* gptel.el (gptel--insert-response): Moving point in a buffer does
not move window-point in a window displaying that buffer if the
window is not selected (#269).  So select the gptel response
window explicitly (if possible) when running
`gptel-post-response-functions`.

NOTE: Instead of selecting the window before running the hook, We
could run `set-window-point` in the window after running it.
Since the hook can have any kind of behavior (smooth-scrolling for
instance) we want it to play out interactively.

* gptel-curl.el (gptel--curl-stream-cleanup): Ditto.
@karthink
Copy link
Owner

I tried (add-hook 'gptel-post-response-functions 'gptel-end-of-response), but that only seems to work for the gptel buffer, not for other text buffers (I am testing with scratch in Markdown mode).

This was a bug, thanks for reporting it! It's not that it was only running in gptel chat buffers -- the problem is that moving point like this:

(with-current-buffer gptel-buffer
  (goto-char (somewhere)))

does not move the point in a window showing that buffer unless the window is selected. Each window has its own point in the buffer and it only moves if the buffer is selected -- a fact I have to remind myself of every couple of years. Fixed by selecting the window (if possible) before running the hook.

I would suggest that gptel move the point to the end of the inserted text when generating into a buffer.

I will not do this by default. It breaks the async behavior, Emacs or the user can be doing any number of other things in the buffer. The hook method is the recommended way to do this.

@jwr
Copy link
Author

jwr commented Mar 30, 2024

Thank you for fixing this 🙏 — I can now implement the behavior I need using gptel-post-response-functions and it works well.

Incidentally, I think this variable needs a docstring fix. The docstring says "This hook is called in the buffer from which the prompt was sent", it probably should say "This hook is called in the buffer to which the response was sent"?

Also, not sure if that is intentional or not, but the gptel-post-response-functions does not follow the -hook naming convention, so one can't use it in the :hooks section of use-package. Not a big problem, obviously.

karthink added a commit that referenced this issue Mar 30, 2024
* gptel.el (gptel-post-response-functions): Adjust docstring for
this hook to mention that the hook runs in the response buffer,
not the prompt buffer. (#269)
@karthink
Copy link
Owner

Incidentally, I think this variable needs a docstring fix. The docstring says "This hook is called in the buffer from which the prompt was sent", it probably should say "This hook is called in the buffer to which the response was sent"?

Fixed, thanks for catching that.

Also, not sure if that is intentional or not, but the gptel-post-response-functions does not follow the -hook naming convention

I am following the guidelines for naming hooks as laid out in the Elisp manual (info "(elisp) Hooks"):

If the hook variable’s name does not end with ‘-hook’, that indicates it is probably an “abnormal hook”. These differ from normal hooks in two ways: they can be called with one or more arguments, and their return values can be used in some way. The hook’s documentation says how the functions are called and how their return values are used. Any functions added to an abnormal hook must follow the hook’s calling convention. By convention, abnormal hook names end in ‘-functions’.

The problem is use-package's bad defaults. You can change it by adjusting use-package-hook-name-suffix.

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

No branches or pull requests

2 participants