Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

emacs lisp, how to get buffer major mode?

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?

like image 553
Anycorn Avatar asked Feb 10 '10 16:02

Anycorn


People also ask

How do I make Emacs major mode?

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.

How do I change modes in Emacs?

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 .

What is the default mode of Emacs?

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.

How do I enable minor mode in Emacs?

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.


3 Answers

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.

like image 92
Aidan Cully Avatar answered Sep 18 '22 22:09

Aidan Cully


For current buffer:

(message "%s" major-mode) 
like image 28
Adobe Avatar answered Sep 18 '22 22:09

Adobe


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*"))
like image 28
Jeremie Pelletier Avatar answered Sep 22 '22 22:09

Jeremie Pelletier