如何将消息/回声输出重定向到EMAC中的缓冲区



我正在编写一些助手功能供我使用。他们首先调用org-publish-project,然后在该输出上调用外部脚本。我想从弹出的临时缓冲区中收集所有执行中的所有输出。

外部内容更容易。该函数shell-command接受第二个关于发送stdout的缓冲区的参数。但是org-publish-project仅回声将东西与MINIBUFFER相呼应,并且在*Messages*上显示。我可以以某种方式将所有回声重定向到给定的缓冲区吗?

不,可悲的是没有这样的重定向。您可以尝试建议message功能,这将捕获许多消息,这不一定是所有消息。

(defvar my-message-output-buffer nil)
(defadvice message (around my-redirect activate)
  (if my-message-output-buffer
      (with-current-buffer my-message-output-buffer
        (insert (apply #'format (ad-get-args 0))))
    ad-do-it))

取决于org-publish-project内部用于显示消息的内容,以下可能有效:

(with-output-to-temp-buffer "*foo*"
  (do-stuff))
(pop-to-buffer "*foo*")

暂时将所有消息输出重定向到当前缓冲区,请执行以下操作:

(defvar orig-message (symbol-function 'message))
(defun message-to-buffer (format-string &rest args)
  (insert (apply 'format format-string args) "n"))
(defmacro with-messages-to-buffer (&rest body)
  `(progn (fset 'message (symbol-function 'message-to-buffer))
          (unwind-protect
              (progn ,@body)
            (fset 'message orig-message))))
;; Usage
(with-messages-to-buffer
 (message "hello"))

最新更新