views:

192

answers:

2

hey guys

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?

A: 

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
+5  A: 

Is there a problem with that?

(defun buffer-mode (buffer-or-string)
  "Returns the major mode associated with a buffer."
  (save-excursion
     (set-buffer buffer-or-string)
     major-mode))

save-excursion will restore your buffer when it returns.

Aidan Cully
You could also do this with `(with-current-buffer buffer-or-string major-mode)` instead of `save-excursion`.
haxney