rksm / org-ai

Emacs as your personal AI assistant. Use LLMs such as ChatGPT or LLaMA for text generation or DALL-E and Stable Diffusion for image generation. Also supports speech input / output.
GNU General Public License v3.0
697 stars 56 forks source link

Maintain org indentation #18

Open mjhoy opened 1 year ago

mjhoy commented 1 year ago

AI chat within begin_ai blocks doesn't seem to maintain org-indent-mode in a way that makes my OCD go crazy 😂. Looking at the code, I don't totally understand why this is... it seems to just use insert and when I evaluate (insert "\n\n") locally, for instance, I can't reproduce the behavior.

rksm commented 1 year ago

Yeah seeing this as well. When I disable/enable org-indent-mode it seems to get fixed. It might be related to the org-element-cache issue? https://github.com/rksm/org-ai/issues/16. Hmm. If an org-mode maintainer is seeing this, any idea would be appreciated!

shamiv commented 1 year ago

I am definitely not the org-mode expert/maintainer you're looking for, but running this inside the block after the output has been inserted fixes it for me. I was looking for a way to have it run after the text insertion is complete, but I didn't manage yet (there is no hook for that?)

(EDIT: "revert-buffer" is probably easier.)

  (defun org-ai-dirty-indent-fix ()
    (interactive)
    (save-excursion
    (evil-org->
     (re-search-backward "^\\#\\+begin_ai" nil t)
     (re-search-forward "^\\#\\+end_ai" nil t) 2)
    (evil-undo 1)))
LemonBreezes commented 1 year ago

I personally just save and use C-x x g, revert-buffer-quick if I notice the indentation is off.

shamiv commented 1 year ago

Is there a way run revert-buffer-quick after org-ai is done? Some hook that runs functions after the response has been inserted?

rksm commented 1 year ago

You can do something like

(add-hook 'org-ai-after-chat-insertion-hook
          (lambda (type text) ; type will be 'text, 'role or 'end
            (when (eq type 'end)
              (save-buffer)
              (revert-buffer-quick))))
tavisrudd commented 1 year ago

I've been using the following hook to solve this issue:

(defun dss/-org-ai-after-chat-insertion-hook (type _text)
  (when (and (eq type 'end) (eq major-mode 'org-mode) (memq 'org-indent-mode minor-mode-list))
    (org-indent-indent-buffer)))
(add-hook 'org-ai-after-chat-insertion-hook #'dss/-org-ai-after-chat-insertion-hook)