xhcoding / emacs-aichat

AI Chat in Emacs, including OpenAI and Bing Chat
62 stars 8 forks source link

[feature request] 请求添加一个自定义命令功能 #3

Closed BluePadge closed 1 year ago

BluePadge commented 1 year ago

``你好,我正在使用这个Plugin,非常好用,比直接在网页上使用舒服了,十分感谢你的贡献。

我想要一个功能,自己选中一段代码,可以为选中内容添加前缀,比如:“请 解释(优化、检查)如下代码:”,“请为这个链接概括其中的亮点:” 这样我就可以省去很多复制粘贴的工作了,使用起来更加方便。 回复窗口能半屏或者三分之一屏幕在侧边展开就更好了。

我尝试在 BingChat 的帮助下,来编写上述功能的代码,都失败在 promise 函数部分了。 希望你在空闲时间可以考虑一下这个功能,或者可以给我一些提示,下面是我在 Bing 的帮助下写的代码

  (defun my-bingai-chat (prefix)
    "Start a Bingai chat with the selected text as input."
    ;; (interactive "r")
    (let* ((said (buffer-substring-no-properties (region-beginning) (region-end)))
           (said (concat prefix said)); ; add the prefix to the selected text
           (if (bingai--replying-p)
               (message "Please wait for the replying finiahed before saying.")
             (let* ((chat-buffer (bingai--chat-get-buffer))
                    (chat (bingai--chat-new
                           :buffer chat-buffer
                           :said said)))
               (split-window) ; split the current window
               (switch-to-buffer chat-buffer)
               (bingai--chat-say chat (not (bingai--started-p)))
               (promise-then (bingai--say said (lambda (msg)
                                                 (bingai--chat-handle-reply msg chat)))
                             (lambda (_)
                               (bingai--chat-handle-reply-finished chat))
                             (lambda (msg)
                               (bingai--chat-handle-reply-error chat msg))))))))

  (defun explain-artical-bingai ()
    (interactive)
    (my-bingai-chat "帮我总结一下这篇文章的内容和亮点:"))
xhcoding commented 1 year ago

bingai-chat 是可以直接调用的,我加了一个选项 bingai-chat-display-function 来控制如何显示 buffer,你可以直接修改,也可以临时修改,下面是一个翻译选中区域的例子, 调用这个函数时,buffer 会显示在底部

(defun my/bingai-chat-translate-region ()
  (interactive)
  (let ((bingai-chat-display-function (lambda (chat-buffer)
                                               (with-current-buffer chat-buffer
                                                 (goto-char (point-max)))
                                               (display-buffer chat-buffer '(display-buffer-at-bottom . ())))))
    (when (region-active-p)
      (bingai-chat (format  "请翻译 “%s”" (buffer-substring-no-properties (region-beginning) (region-end)))))))

如果你用了其它弹出窗口插件,也可以让它以弹出窗口的形式出现,比如我用了 popper ,把 aichat.md 加入规则列表中:

  (setq popper-reference-buffers
        '(
          help-mode
          rustic-cargo-run-mode
          lsp-bridge-ref-mode
          "^\\*eshell.*\\*$" eshell-mode
          "^aichat\\.md$"
          ))
BluePadge commented 1 year ago

感谢你提供的代码,完美的解决我的问题。