4

What would be the proper way to pop up a window (preferably in the current frame),with a message? I also want to locally (in that buffer only) the q key to a function that will kill the buffer and the window (similar to what is done in IELM's error pop-ups, but it won't be an error pop-up).

I have tried this:

(defun kill-buffer-and-window ()
  (kill-this-buffer)
  (if (not (one-window-p))
      (delete-window)))

(defun cool-message ()
  (interactive)
  (pop-to-buffer "*cool-message-buffer*")
  (use-local-map (make-sparse-keymap)) ; don't change bindings for other buffers
  (local-set-key "q" 'kill-buffer-and-window)
  (insert "Yo!!!"))

(local-set-key "x" 'cool-message)

I know that local-set-key will change the binding for all buffers using the same major-mode -- this is just a quick hack to show what I want.

The problem is that hitting q in the buffer that pops up will not kill it.

What did I miss? Do I need to create a minor mode?

1 Answer 1

6

Use with-help-window:

(with-help-window "*My Help Buffer*" ; Whatever buffer name you like.
   ;; Use `princ`, `prin1`, `terpri`, etc. to put text in the displayed buffer
)

Key q will automatically be bound to do what you want.

C-h f with-help-window says:

with-help-window is a Lisp macro in help.el.

(with-help-window BUFFER-OR-NAME &rest BODY)

Evaluate BODY, send output to BUFFER-OR-NAME and show in a help window.

This construct is like with-temp-buffer-window but unlike that puts the buffer specified by BUFFER-OR-NAME in help-mode and displays a message about how to delete the help window when it’s no longer needed. The help window will be selected if help-window-select is non-nil.

Most of this is done by help-window-setup, which see.

As that suggests, you can alternatively use with-temp-buffer-window.

(Or you can use (with-output-to-temp-buffer BUFFER . BODY)) instead of (with-help-window BUFFER . BODY).)

1
  • Thank you!!! That is perfect, will use it.
    – Jay
    Commented Nov 24, 2018 at 19:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.