karthink / gptel

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

gptel-send when called on region in prog-mode buffers should output response to other buffer #446

Closed nordlow closed 1 week ago

nordlow commented 3 weeks ago

M-x gptel-send when mark is active in a code buffer should adhere to the Emacs DWIM principle, meaning it should output the response into another buffer instead of the current behavior that outputs typically non-working code or English text the resulting in a syntactically incorrect code modification.

The solution could branch on the whether the current major-mode inherits from prog-mode and output the result in another buffer or directly in the minibuffer.

karthink commented 3 weeks ago

the current behavior that outputs typically non-working code or English text the resulting in a syntactically incorrect code modification.

Perhaps this is the problem. You can ask it to output code and only code in the system message, and it will generally do so. One of the default gptel-directives includes this, and you can set it for the buffer. That said, you'll probably want to write a system message with more instructions.

Emacs DWIM principle, meaning it should output the response into another buffer

I'm not seeing what's DWIM about this behavior. "What I Mean" can be anything here -- In a prog-mode buffer, I might want

All of these (and more) seem like equally valid "DWIM" behaviors to me.

You can do the first and last using gptel's transient menu. To wrap text in a code comment in the prog-mode buffer you can use the hook gptel-post-response-functions. To rewrite/refactor the code you can use the "in-place output" option in the transient menu, or use the refactor interface (also in the transient menu).

nordlow commented 3 weeks ago

I guess it all comes down to what instructions (system message) you feed it.

I guess it would be very useful to have gtpel insert code in the same language as that major-mode represents as real code and anything else as comments.

To me this is the most obvious generic DWIM behavior. IMO , a use never wants his prog-mode buffer to contain non-code content unless it's in a comment.

karthink commented 3 weeks ago

I guess it would be very useful to have gtpel insert code in the same language as that major-mode represents as real code and anything else as comments.

It's rarely enough to specify that the response has to be code in language FOO. Typically you are looking to provide many more task-specific instructions. You can craft a system message with the requisite details and save it as an element of gptel-directives. When using gptel you can switch the system message to this preset and send your request.

The "add directive" d option in the transient menu can be used to add a one-time instruction on top of the more general system message.