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
701 stars 56 forks source link

Auto-indentation and alignment are not supported within #+begin_ai. #47

Open iT-Boyer opened 1 year ago

iT-Boyer commented 1 year ago

Indentation issue in org-ai: After executing a dialogue, newly added lines by [AI] are not aligned with the content above. For example:

    #+begin_ai
    [SYS]: You are a helpful assistant.

    [ME]: hello world!
    #+end_ai

layout of newly added content after executing a request:

    #+begin_ai
    [SYS]: You are a helpful assistant.

    [ME]: hello world!

[AI]: Hello! How can I assist you today?

[ME]: 
     #+end_ai
rksm commented 1 year ago

The same is true of org babel src blocks I believe. Not sure if this is something that org-ai can fix, it seems like an issue with org-mode / org-indent-mode

One workaround you can add to your config:

(add-hook #'org-ai-after-chat-insertion-hook
          (lambda (_ _)
            (when (eq major-mode 'org-mode)
              (org-indent-indent-buffer))))
riclage commented 1 year ago

@rksm your workaround works for small files but slows Emacs to a crawl in larger ones.

Maybe it would help to have a specific hook for after the full chat response is inserted here?

rksm commented 1 year ago

I think the simpler option is to use the first argument to customize the behavior:

(add-hook #'org-ai-after-chat-insertion-hook
          (lambda (what _)
            (when (and (eq what 'end)
                       (eq major-mode 'org-mode))
              (org-indent-indent-buffer))))
tavisrudd commented 1 year ago

I've been using a similar hook, but with an additional check to see if org-indent-mode is active: https://github.com/rksm/org-ai/issues/18#issuecomment-1737931580. A more efficient approach would use (org-indent-refresh-maybe beg end _) which is intended to be called from things like after-change-functions.