karthink / gptel

A simple LLM client for Emacs
GNU General Public License v3.0
1.04k stars 113 forks source link

Writing an org-mode gptel-mode buffer to disk jumps buffer to top #199

Open JosephFerano opened 5 months ago

JosephFerano commented 5 months ago

Hello, thank you for the great package.

I think this has to do with the fact that gptel is writing to the properties section at the top

:GPTEL_MODEL: gpt-3.5-turbo
:GPTEL_BACKEND: ChatGPT
:GPTEL_BOUNDS: ((14613 . 16183) (16184 . 16843) (16912 . 18574) (19036 . 22079) (22402 . 23183) (23184 . 24707) (24831 . 25642) (25643 . 25795) (25796 . 25864) (25887 . 26517) (26847 . 28245) (28262 . 28541) (28679 . 29796) (30080 . 30942) (30964 . 31351) (31481 . 33220) (33607 . 34392) (34393 . 34880) (34881 . 35049) (35071 . 35695) (35785 . 36704))
:END:

It's noticeable for me because I'm using evil-mode and I have it bound such that when leaving insert mode, I autosave. So while I'm writing my prompts and going back and forth between normal/insert, I keep jumping back to the top. I tried this with emacs -Q and I get the same behavior. Could a with-excursion fix this?

karthink commented 5 months ago

@JosephFerano there is already a save-excursion around this process in the code. org-with-wide-buffer expands to save-excursion + save-restriction.

I'm not able to reproduce this in my regular Emacs, but can reproduce it with emacs -Q, which is really strange.

  1. I'm not sure why the save-excursion is not being respected.
  2. I'm on the development branch of Org and emacs -Q is using Org 9.6, perhaps Org changed something in the meantime.

I'll try to find the issue. If you have any ideas please let me know.

karthink commented 5 months ago

Shortly after I typed out the previous comment, I tested it some more, and I can no longer reproduce the bug with emacs -Q. I haven't changed the code at all.

Very strange.

EDIT: I was only able to reproduce it once in 10+ tries with emacs -Q (starting a new session each time), and not reproduce it at all with my Emacs config.

JosephFerano commented 5 months ago

@karthink Thank you for taking a look at this, if there's anything I can do on my end to help with the repro let me know. Here are my emacs and org versions if it helps (I compiled emacs myself) GNU Emacs 29.1.90 (build 1, x86_64-pc-linux-gnu, GTK+ Version 3.24.38, cairo version 1.17.8) of 2023-11-05 Org mode version 9.6.10 (release_9.6.10 @ /usr/local/share/emacs/29.1.90/lisp/org/)

P.S. I also have a small issue with org src blocks where the highlighting isn't being activated when I get an answer back. I have to manipulate the text for the src block in order for org-mode to highlight it. Is this something I should open a separate issue for?

karthink commented 5 months ago

P.S. I also have a small issue with org src blocks where the highlighting isn't being activated when I get an answer back. I have to manipulate the text for the src block in order for org-mode to highlight it. Is this something I should open a separate issue for?

I can't reproduce this either (with my config or emacs -Q). Could be something specific to your jit-lock , font-lock or Org settings. You should be able to force fontification with

(add-hook 'gptel-post-response-functions #'font-lock-ensure)

If that doesn't work, please create a separate issue.

james-ru commented 5 months ago

I am also experiencing the exact same thing happening on a Doom Emacs install with a clean config that only sets up gpt.el. Issue occurs each time I open an org file with gptel-mode, and it doesn't happen at all when a Markdown file is used instead.

GNU Emacs 29.1.50 (build 1, x86_64-pc-linux-gnu, GTK+ Version 3.24.33, cairo version 1.16.0) of 2023-08-06 Org mode version 9.6.9 (9.6.9-??-806abc5 @ /home/james/.emacs.d/.local/straight/build-29.1.50/org/)

karthink commented 4 months ago

I'm still trying to figure out what's causing this, as I can't seem to reproduce it.

If it's happening in your Org buffer(s) right now, could you do the following?

  1. Try saving the file and confirm that the cursor jumps to the top.
  2. Run M-x find-function gptel--save-state, and manually evaluate this function with C-M-x or equivalent.
  3. Try modifying and saving the file again.

I'm interested to know if that changes anything. I suspect a byte-compilation issue.

JosephFerano commented 4 months ago

Going through those exact steps fixes the issue. I am able to save the buffer and it doesn't jump. It is odd indeed. What do you suggest as a temporary workaround?

karthink commented 3 months ago

@JosephFerano I've made some changes to the save-state feature in the feature-org branch. (It also has some new org-specific gptel features that you may find useful.)

if you have the time, could you try switching to this branch and testing if the problem still persists?