I am creating a new, special kind of buffer that I am using to display messages. I want to disable all keymaps within this new buffer.

This special buffer could be created at any time, and therefore, I don't know which keymaps may or may not currently active at the time of this buffer's creation.

Is there any way to disable each and every active keymap for a given buffer?

Thanks in advance.

  • Do you mean "frame" or "window"? In general, a frame can display multiple buffers -- and a frame can have multiple windows. If you have a buffer in frame A with keymap Z, that keymap will not disappear if you view the same buffer in frame B. Keymaps are not frame-local. You can have buffer-local keymaps. For a better understanding of how this might work, see "How to display a help buffer/window full frame size (not full screen)": emacs.stackexchange.com/a/24037/2287 That answer contains buffer-local keymap
    – lawlist
    Dec 7 '16 at 23:54
  • I know the difference between buffers, windows, and frames, but I didn't ask my question clearly. I want to create a new, unique buffer and open it within a unique frame in one, single window. I already know how to do all that. And I know about buffer-local keymaps, and that's what I plan to create. However, what I want to figure out is this: I want to also disable all other keymaps that might be active at the time I create this new buffer.
    – HippoMan
    Dec 8 '16 at 1:58
  • There are too many possibilities, in my opinion, to answer the question: new buffer has a major-mode v. new buffer is in fundamental mode; new buffer is a cloned/indirect buffer; how many minor-modes with keymaps are active in the new buffer; are there local keymaps in the buffer; are there mouse-over keymaps and mode-line keymaps in the new buffer; etc. If you already know how to handle everything about creating a new frame and buffer-local keymaps, then consider removing all of that from the question so the readers do not spend time thinking about that aspect.
    – lawlist
    Dec 8 '16 at 2:23
  • See also the section of the manual regarding Active Keymaps: gnu.org/software/emacs/manual/html_node/elisp/… E.g., the function current-active-maps
    – lawlist
    Dec 8 '16 at 2:42
  • I have now rewritten my question.
    – HippoMan
    Dec 8 '16 at 2:42

You can use a minor-mode and use a default mapping. Have a look at the define-key documentation for details about default definitions in keymaps.

(define-minor-mode disable-keys-mode
  "Disables all keys."
  :lighter " dk"
  :keymap (let ((map (make-sparse-keymap)))
            (define-key map (kbd "C-g") (lambda ()
                                          (disable-keys-mode -1)))
            (define-key map [t] 'ignore)
  • 1
    Ah ... I didn't know about [t]. That saves a lot of code. Thank you. I take this as the best answer.
    – HippoMan
    Dec 8 '16 at 14:44

Thanks to the pointer from lawlist, I now know how to do this. This over-simplified code gives a general overview of the procedure ...

(let ((cam (current-active-maps t))
  (while cam
    (setq lm (copy-keymap (car cam)))
    (setq cam (cdr cam))
    (suppress-keymap lm)
    (use-local-map lm)))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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