60

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?

Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
Anycorn
  • 50,217
  • 42
  • 167
  • 261

7 Answers7

45

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.

haxney
  • 3,358
  • 4
  • 30
  • 31
Aidan Cully
  • 5,457
  • 24
  • 27
  • 12
    You could also do this with `(with-current-buffer buffer-or-string major-mode)` instead of `save-excursion`. – haxney Feb 11 '10 at 09:53
  • 2
    @haxney: Using `(save-excursion (set-buffer ..) ...)` is indeed deprecated in favor of `(with-current-buffer ...)`. The byte-compiler of Emacs-24 warns about such uses. – Stefan Sep 19 '12 at 20:12
  • @stefan: Where might an emacs noob see such deprecations? – Realraptor Jul 03 '18 at 15:01
  • As noted, the byte-compiler warns about it: just byte-compile your files, it will give you useful advice. – Stefan Jul 03 '18 at 17:08
  • It works, but I'm curious if emacs can handle this straightforward? Why should we change the target buffer shortly to current, apply function, and change it back? Why isn't there a function that takes a buffer and return its major-mode directly? Is it for some historical convention? Or there are deeper facts in emacs that prevent us doing that? – Student Apr 21 '20 at 01:51
41

For current buffer:

(message "%s" major-mode)
Adobe
  • 12,967
  • 10
  • 85
  • 126
36

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*"))
7

Just extending from previous answers - call with no arguments to get the current buffer's mode:

(defun buffer-mode (&optional buffer-or-name)
  "Returns the major mode associated with a buffer.
If buffer-or-name is nil return current buffer's mode."
  (buffer-local-value 'major-mode
   (if buffer-or-name (get-buffer buffer-or-name) (current-buffer))))

E.g. in *scratch* buffer:

(buffer-mode) => 'lisp-interaction-mode

(buffer-mode "tasks.org") => 'org-mode
Brian Burns
  • 20,575
  • 8
  • 83
  • 77
0

Well, describe-mode takes an optional buffer argument, but that displays the help... and I'm not exactly sure what it returns...

But that's the best I could find in a brief search... sorry...

Brian Postow
  • 11,709
  • 17
  • 81
  • 125
0

Simply evaluate this:

(print major-mode)
sidharth arya
  • 245
  • 3
  • 11
0

Another way, apart from directly readind the major-mode variable would be by directly readind the mode-name variable.

alinsoar
  • 15,386
  • 4
  • 57
  • 74