emacs lisp, how to get buffer major mode?

久未见 提交于 2019-11-28 04:04:39
Aidan Cully

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

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

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...

Simply evaluate this:

(print major-mode)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!