I have tried to search Google and look in the manual, but still cannot find how to get major mode of a buffer object. Can you help me with an example or a reference. Thanks
only solution I could find was to query major-mode after changing the buffer and then changing back to original buffer. Is there a better way to do it?
Usually, the major mode is automatically set by Emacs, when you first visit a file or create a buffer (see Choosing File Modes). You can explicitly select a new major mode by using an M-x command.
A buffer may have multiple minor modes. To get a list of the all the available modes, both major and minor, on your system's version of Emacs, look up "mode" using Emacs' apropos command. Do this by pressing C-h a and entering the word mode .
The standard default value is fundamental-mode . If the default value is nil , then whenever Emacs creates a new buffer via a command such as C-x b ( switch-to-buffer ), the new buffer is put in the major mode of the previously current buffer.
If you invoke the mode command directly with no prefix argument (either via M-x , or by binding it to a key and typing that key; see Customizing Key Bindings), that toggles the minor mode. The minor mode is turned on if it was off, and turned off if it was on.
Is there a problem with that?
(defun buffer-mode (buffer-or-string) "Returns the major mode associated with a buffer." (with-current-buffer buffer-or-string major-mode))
with-current-buffer
will restore your buffer when it returns.
For current buffer:
(message "%s" major-mode)
A simple way to do this is to use the buffer-local-value
function since major-mode
is a buffer-local variable:
(buffer-local-value 'major-mode (get-buffer "*scratch*"))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With