How to redirect message/echo output to a buffer in Emacs?

两盒软妹~` 提交于 2020-12-31 04:38:15

问题


I'm writing a couple of helper functions for my use. They first call up org-publish-project and then call external scripts on that output. I'd like to collect all output from the execution in a temp buffer that pops up.

The external stuff is easier. The function shell-command accepts a second argument about a buffer where to send stdout. But org-publish-project only echoes stuff to minibuffer and it shows on *Messages* if anywhere. Could I somehow redirect all echoes to a given buffer?


回答1:


No, there's no such redirect, sadly. You can try to advise the message function, which will catch many of those messages, tho not necessarily all of them.

(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))



回答2:


Depending on what org-publish-project internally uses to display messages, the following might work:

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



回答3:


To temporarily redirect all message output to the current buffer, do the following:

(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"))


来源:https://stackoverflow.com/questions/21524488/how-to-redirect-message-echo-output-to-a-buffer-in-emacs

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!