在哪个文件中定义了键绑定?

发布于 2024-07-19 06:33:55 字数 150 浏览 9 评论 0原文

考虑 Emacs 中的击键,例如 Cx Cs。 我可以通过输入 Ch c keytrip 找出它调用的函数,但是如何找到定义此键绑定的位置?

(也许正确的答案是无法决定,因为键盘映射不存储此类信息。)

Considering a keystroke in Emacs, e.g. C-x C-s. I can figure out which function it invokes by typing C-h c keystroke, but how can I find where this keybinding was defined?

(Maybe the right answer is that it cannot be decided, because the keymaps don't store this kind of information.)

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

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

发布评论

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

评论(2

同尘 2024-07-26 06:33:55

Emacs 不保留有关键绑定与函数关联的位置的信息。 事实上,答案通常是不确定的。

很多时候,键绑定被设置为次要模式的副作用,或者通过模式挂钩设置。 次要模式的按键绑定通常存储在一些变量中,例如 comint-mode-map 。 该键盘映射通过设置模式变量 comint-mode 来启用。 但是,该模式映射是一个全局变量,任何人、任何地方都可以向其添加键绑定。 因此,做你想做的事本质上就是跟踪谁设置了具有特定值的变量。 类似地,对于仅通过模式挂钩在本地设置的键绑定(使用 local-set-key)。 另外,您甚至可以通过 文件变量。 所有这些只是说明 Emacs 中的键绑定是短暂的。

您能做的最好的事情就是找到与该键关联的功能,然后查看该文件(如果模式拆分为多个文件,则查看任何相关文件)。

由于可以通过多种方式设置/关联击键(全局、主要模式、次要模式、覆盖次要模式、本地缓冲区、文本属性等),emacs 中的键绑定查找相当复杂。 如需快速概览,请查看此文档

您可以编写这样的函数来查看次要模式,以查看可能在哪里设置键绑定,但当然我的第一个测试显示那里没有定义键绑定。 但也许代码会有启发性。

(defun guess-where-keybinding-is-defined (key)
  "try to guess where a key binding might be defined"
  (interactive (list (read-key-sequence "Describe key: ")))
  (let ((bindings (minor-mode-key-binding key))
        found)
    (while (and bindings (not found))
      (if (setq found (caar bindings))
          (find-function (cdar bindings)))
      (setq bindings (cdr bindings)))))

您是否有想要解决的特定问题(除了这个问题)?

The information as to where keybindings are associated with functions is not kept by Emacs. In fact, the answer is usually undefined.

Many times key bindings are set up as a side-effect of a minor mode, or through mode-hooks. The key binding for minor modes is often stored in some variable like comint-mode-map. This keymap is enabled by setting the mode-variable comint-mode. However, that mode map is a global variable and anyone, anywhere can add key bindings to it. So, doing what you want is essentially tracking who set a variable with a particular value. Similarly for key bindings that are just set locally through mode hooks (using local-set-key). Plus you can even set up key bindings through file variables. All of this is just to say that the key bindings in Emacs are ephemeral.

About the best you can do is find the function associated with the key, and look in that file (or any related files if the mode is split into multiple files).

Key binding lookup in emacs is fairly involved due to the myriad of ways you can set/associate key strokes (globally, major mode, minor modes, overriding minor modes, local to buffers, text properties, etc.). For a quick overview, check out this documentation.

You can write a function like this to look in the minor modes to see where a keybinding might be set, but of course my first test showed the keybinding wasn't defined there. But perhaps the code will be instructive.

(defun guess-where-keybinding-is-defined (key)
  "try to guess where a key binding might be defined"
  (interactive (list (read-key-sequence "Describe key: ")))
  (let ((bindings (minor-mode-key-binding key))
        found)
    (while (and bindings (not found))
      (if (setq found (caar bindings))
          (find-function (cdar bindings)))
      (setq bindings (cdr bindings)))))

Is there a specific problem you're trying to solve (other than this question)?

眼前雾蒙蒙 2024-07-26 06:33:55

只需执行 Mx find-function

来自 Emacs 文档:

(查找函数)

查找函数的定义
近点。

查找包含以下内容的源文件
函数近点的定义
(由选择
中的“函数调用点”)
缓冲区并将点放置在
定义。 移动前设置标记,如果
缓冲区已存在。

Just do M-x find-function

From Emacs doc:

(find-function function)

Find the definition of the function
near point.

Finds the source file containing the
definition of the function near point
(selected by
`function-called-at-point') in a
buffer and places point before the
definition. Set mark before moving, if
the buffer already existed.

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