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?

Drew
  • 75,699
  • 9
  • 109
  • 225
Jay
  • 233
  • 1
  • 7

1 Answers1

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

Drew
  • 75,699
  • 9
  • 109
  • 225