Emacs 函数和命令的关系 [英] Relationship between Emacs functions and commands

查看:17
本文介绍了Emacs 函数和命令的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,在 Emacs 中,我可以运行 commands,例如 Mx(顺便说一下,我认为它代表 execute-extended-command).这个命令 M-x 本身用于运行诸如 customize_face 之类的东西,例如通过在 minibuffer 中输入 M-x Customize-face .

From what I understand, in Emacs I can run commands such as M-x (which by the way I believe stands for execute-extended-command). This command M-x itself is used to run things like customize_face e.g. by typing M-x customize-face in the minibuffer.

我的问题是:

Q.1. customize-face命令吗?还是功能?我们是否说 customize-face 被传递给 command M-x 作为参数?

Q.1. Is customize-face a command? or is it a function? And do we say that customize-face is passed to the command M-x as an argument?

问题 2 是否所有 Emacs 命令都有关联的 Emacs 功能?(即,当我输入 M-x Customize-face 时,我假设调用了一个定义的函数).如果是这样,我如何从命令名称中查找函数名称?(反之亦然)

Q.2 Do all Emacs commands have an associated Emacs function? (i.e. when I enter M-x customize-face I presume a defined function is called). If so, how can I look up the function name from the command name? (and viceversa)

推荐答案

是的,所有 Emacs 命令都是函数,但并非所有函数都是 Emacs 命令.您可以使用 (interactive):

Yes, all Emacs commands are functions, but not all functions are Emacs commands. You can make an arbitrary elisp function a command accessible via M-x using (interactive):

(defun my-command ()
 "This is the docstring"
 (interactive)
 (do-foo)
 (do-bar))

既然您已经将 my-command 定义为交互式,您可以立即使用 M-x my-command 访问它.Emacs 会自动为您使用姓名进行所有簿记.

Now that you've defined my-command as interactive, you can immediately access it with M-x my-command. Emacs does all the bookkeeping with the name for you automatically.

这是添加新命令所需的全部!然后,您可以使用以下内容将其绑定到一个键:

This is all you have to do to add a new command! You can then bind it to a key with something like:

(global-set-key (kbd "C-c f") 'my-command)

此外,每个键绑定都与这样的交互功能相关联.您可以使用 C-h k 并输入您的按键序列来找到哪个键调用了哪个函数.这将为您提供将在该键序列上调用的函数的文档.如果你运行我给你的代码,执行 C-h k C-c f 会给你一个包含(除其他外)你的文档字符串的缓冲区:

Moreover, every key-binding is associated with an interactive function like this. You can find which function is called by which key using C-h k and entering your key sequence. This will give you the documentation for the function that would be called on that key sequence. If you ran the code I gave you, doing C-h k C-c f would give you a buffer containing (among other things) your doc-string:

C-c f runs the command my-command, which is an interactive Lisp
function.

It is bound to C-c f.

(my-command)

This is the docstring

所以:所有 Emacs 命令都只是用 (interactive) 定义的函数.(实际上,Emacs 的 C 内核也有一些原始函数,但这不是很重要.)

So: all Emacs commands are just functions defined with (interactive). (Actually, there are also some primitive functions from Emacs's C core, but that isn't super important.)

命令和函数之间的这种简单而优雅的关系——在任何一个方向都很容易遵循——是使 Emacs 如此易于定制的部分原因.如果您想知道您的正常操作调用了哪些函数,您可以轻松查找它们,如果您想添加更多命令,您只需在函数中多出一行即可.

This simple and elegant relationship between commands and functions--which is easy to follow in either direction--is part of what makes Emacs so easy to customize. If you ever wonder what functions your normal actions called, you can easily look them up, and if you want to add more commands, you just have one extra line in your function.

这篇关于Emacs 函数和命令的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆