emacs lisp,如何获取缓冲区主要模式?

发布于 2024-08-20 13:58:22 字数 109 浏览 7 评论 0原文

我尝试搜索谷歌并查看手册,但仍然找不到如何获取缓冲区对象的主要模式。你能帮我举个例子或参考吗?感谢

我能找到的唯一解决方案是在更改缓冲区然后更改回原始缓冲区后查询主要模式。有更好的方法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

肩上的翅膀 2024-08-27 13:58:22

这有问题吗?

(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 将在返回时恢复您的缓冲区。

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.

活泼老夫 2024-08-27 13:58:22

对于当前缓冲区:

(message "%s" major-mode)

For current buffer:

(message "%s" major-mode)
情深已缘浅 2024-08-27 13:58:22

一个简单的方法是使用 buffer-local-value 函数,因为major-mode 是一个缓冲区局部变量:

(buffer-local-value 'major-mode (get-buffer "*scratch*"))

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*"))
若言繁花未落 2024-08-27 13:58:22

只是从以前的答案扩展 - 不带参数调用来获取当前缓冲区的模式:

(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))))

例如在 *scratch* 缓冲区中:

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

(buffer-mode "tasks.org") => 'org-mode

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
老旧海报 2024-08-27 13:58:22

好吧,describe-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...

音栖息无 2024-08-27 13:58:22

简单评价一下:

(print major-mode)

Simply evaluate this:

(print major-mode)
随风而去 2024-08-27 13:58:22

另一种方法是,除了直接读取 major-mode 变量之外,还可以直接读取 mode-name 变量。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文