How to replace the displayfn from message
to a function which displays the result in a temporary window? (I have limited knowledge of elisp.)
-
Welcome to emacs.SE! It's not really clear what you want to do. Could you try to be more explicit? What is the 'define-word' package? – JeanPierre Oct 21 '18 at 20:14
2 Answers
Instead of
(message "ahoy")
create a new buffer, switch to it and insert the message there:
(progn (switch-to-buffer (generate-new-buffer "message"))
(insert "ahoy"))

- 1,925
- 10
- 16
To give a bit of context, define-word
is an Emacs mode which provides dictionary definitions given a word [1]. By default it sends its output to the messages
buffer, but it provides an overridable display function. Here's my solution to redirect the output, which is by no means an "advanced" one. First, based on the answer by choroba, I defined my display function like so:
(defun my/display-word (&rest args)
"Create a buffer for display word instead of using messages."
(interactive)
(let
((buffer (generate-new-buffer "Define Word")))
(set-buffer buffer)
(set-buffer-major-mode buffer)
(apply 'insert args)
(display-buffer buffer))
)
Then I copied the code from Henrik Lissner's config [2], which loops through all available services and adds them to the alist:
(setq define-word-displayfn-alist
(cl-loop for (service . _) in define-word-services
collect (cons service #'my/display-word)))
The end result is as follows:
If you are interested in the details of my investigation, please see the ticket I raised against the project [3].
Update 1
Actually, I found the creation of many buffers quite annoying so I updated the function to this:
(defun my/display-word (&rest args)
"Create a buffer for display word instead of using messages."
(interactive)
(let
((buffer (get-buffer-create "Define Word")))
(set-buffer buffer)
(erase-buffer)
(set-buffer-major-mode buffer)
(apply 'insert args)
(display-buffer buffer))
)
[3] #29: Redefining display function to output to a buffer instead of messages

- 233
- 1
- 7