抛砖引玉: 介绍命令行AI编程工具aider以及emacs集成aider.el

Álvaro R. #FreeVenezuela 🇻🇪: "More #emacs experiments applying #chatgpt changes…" - Indieweb.Social chatgpt-shell 做了一个 smerge 的形式。

您好,感谢花时间研究颜色的问题。好些人都在问如何让aider在emacs里的输出不丢失颜色。vterm好像确实能保持aider输出的颜色,不过感觉vterm这个包是个比较强的dependency,安装好像还要编译.c文件什么的。这样就又依赖于gcc工具链。

您上面的帖子说到给aider配置颜色(eg. # 3492eb for day),我没太看懂,能请您进一步解释一下吗?这个方法能不能解决aider在emacs输出(comint)的颜色问题?谢谢。

vterm后端难以复制,对于不喜欢vterm后端的人,难以接受

我感觉还是用magit review一下好点,生成的代码不一定就符合我的要求

不过感觉vterm这个包是个比较强的dependency,安装好像还要编译.c文件什么的。这样就又依赖于gcc工具链。

这点我同意,其实不一定要是 vterm,其实只要是一个全功能的终端就可以正常显示颜色。除了 vterm 之外,还有一个纯 elisp 的实现 emacs-eat 也是一个不错的选择。但是因为我个人用的是 vterm,所以知道怎么 hack vterm 来实现我想要的功能。我并不了解 emacs-eat 所以不确定该如何 hack。你可以参考一下我的配置的一部分(请展开已折叠的部分),将 vterm 用作 ipython 的 REPL。

据我了解,emacs 没有可以在 Windows 上可以使用的终端插件,因此如果要保证兼容 windows,应该必须使用 comint。

如果不嫌麻烦的话,可以将和后端交互的代码再包一层,然后用户可以选择 aider-backendcomintvterm (eat) 之间来进行切换。

(progn
    (defvar my*ipython-str-process-func 'identity "The function to process the string before sending it to the ipython REPL.")
    (defvar my*ipython-use-bracketed-paste-mode t "Whether use bracketed paste mode for sending string to the ipython REPL.")
    (defvar my*ipython-start-pattern "" "The first string to send to the ipython REPL before sending the text.")
    (defvar my*ipython-end-pattern "
" "The last string to send to the ipython REPL after sending the text.")
    (defun my~ipython-start
            (&optional arg)
        "Create a ipython REPL buffer.
Start a new ipython session or switch to an already active session. Return
the buffer selected (or created). With a numeric prefix arg,create or
switch to the session with that number as a suffix."
        (interactive "P")
        (require 'vterm)
        (let
                ((vterm-buffer-name
                  (format "*%s*" "ipython"))
                 (vterm-shell "ipython")
                 (repl-buffer)
                 (repl-buffer-exist-p
                  (get-buffer
                   (if arg
                           (format "*%s*<%d>" "ipython" arg)
                       (format "*%s*" "ipython")))))
            (setq repl-buffer
                  (vterm arg))))
    (defun my~ipython-send-region
            (beg end &optional session)
        "Send the region delimited by BEG and END to inferior ipython.
With numeric prefix argument, send region to the process associated
with that number."
        (let
                ((repl-buffer-name
                  (if session
                          (format "*%s*<%d>" "ipython" session)
                      (format "*%s*" "ipython")))
                 (str
                  (buffer-substring-no-properties beg end)))
            (with-current-buffer repl-buffer-name
                (vterm-send-string my*ipython-start-pattern)
                (vterm-send-string
                 (funcall my*ipython-str-process-func str)
                 my*ipython-use-bracketed-paste-mode)
                (vterm-send-string my*ipython-end-pattern))))
    (evil-define-operator my~ipython-send-region-operator
        (beg end session)
        "A evil operator wrapper around `my~ipython-send-region'. With a numeric
prefix argument, send the region to the ipython process associated with
that number" :move-point nil
        (interactive "<r>P")
        (my~ipython-send-region beg end session))
    (defun my~ipython-hide-window
            (&optional arg)
        "hide the ipython window. With numeric prefix argument, hide
the window with that number as a suffix."
        (interactive "P")
        (when-let*
                ((vterm-buffer-name
                  (if arg
                          (format "*%s*<%d>" "ipython" arg)
                      (format "*%s*" "ipython")))
                 (buf
                  (get-buffer vterm-buffer-name))
                 (vterm-buffer-window
                  (get-buffer-window buf)))
            (delete-window vterm-buffer-window))))

您上面的帖子说到给aider配置颜色(eg. # 3492eb for day),我没太看懂,能请您进一步解释一下吗?这个方法能不能解决aider在emacs输出(comint)的颜色问题?谢谢。

并不能,这个只是修改默认主题的颜色而已,因为 aider 默认的用户输入是绿色的,我个人特别不喜欢绿色,将它改成了别的颜色而已。

1 个赞

对于不喜欢vterm后端的人,难以接受

vterm 确实有一些缺点,比如需要编译 C,不支持 windows。

vterm后端难以复制

猫大指的是不方便选中 vterm buffer 的输出内容进行复制吗?

可能不同的用法吧,互相之间不能理解。smerge 和 commit 之后一样可以 undo,颗粒度更细。

有的时候aider 返回的不止一个文件修改

还是要保持Emacs文本编辑的属性, 因为输出的其实是很多有用的代码, 而不是命令行偶尔需要的状态。 vterm 绑定会导致Emacs用其他终端和shell的人很难受。