为什么某些 Emacs 功能不能通过`M-x` 使用? [英] Why are some Emacs functions not available via `M-x`?
问题描述
,它允许您输入命令的名称并运行它.
eval-expression
,可让您键入包含 函数 调用的任意 S 表达式并对其求值.函数,您可以使用 M-: 调用,用于实现 Emacs 功能、自定义(例如在您的 .emacs
中)和插件.函数参数通常通过在 S 表达式中调用函数来传递.
任何具有interactive
形式在其定义中.interactive
形式描述了函数在作为命令调用时应该如何获取其参数.例如,如果函数在其定义中有 (interactive "bGive me a buffer: ")
,那么该函数将可以用 M-x 调用.当用户使用 Mx 调用函数时,Emacs 将提示用户输入缓冲区名称(因为 b
),并且他们键入的名称将作为参数传递到函数.
使函数成为命令的目的是让最终用户更容易调用它,而不仅仅是 Emacs Lisp 程序员.命令(使用 M-x 运行)通过以下方式更易于交互运行:
- 您不必用
()
将命令名称括起来以使其成为有效的 S 表达式. - 可以自动传递参数(例如光标位置),也可以提示您输入参数,这样您就不必记住需要哪些参数.
- 当提示输入参数时,您可以自动完成它,因为
interactive
表单的 代码字符(如b
)指定期望的输入类型.
不能用 Mx 调用函数 set-face-attribute
的原因是它的定义不包含 interactive
形式,所以 set-face-attribute
不是命令.您必须在 S 表达式中将其作为普通函数调用.您可以使用 M-: 从迷你缓冲区执行此操作,或者使用其他任何 评估代码的方法.
Emacs Mini Manual → Concepts → Command 有一个简短的,对正常功能和命令之间差异的不同措辞解释.Emacs 函数和命令之间的关系 解释了本答案中没有的一些细节.
This Stack Overflow answer told me that I can set Emacs’s font size with set-face-attribute
:
(set-face-attribute 'default nil :height 100)
The comments reveal that you can’t run set-face-attribute
with M-x:
Instead, you have to run it with M-::
Why are some commands, like set-face-attribute
, not available via M-x?
- M-x is bound to the command
execute-extended-command
, which lets you type the name of a command and runs it. - M-: is bound to the command
eval-expression
, which lets you type an arbitrary S-expression containing function calls and evaluates it.
Functions, which you can call with M-:, are used to implement Emacs features, customizations (such as in your .emacs
), and plugins. Function arguments are normally passed by calling the function in an S-expression.
Any function can also be a command if it has an interactive
form in its definition. The interactive
form describes how the function should get its arguments when called as a command. For example, if the function has (interactive "bGive me a buffer: ")
in its definition, then the function will be callable with M-x. When the user calls the function with M-x, Emacs will prompt the user for a buffer name (because of the b
), and the name they type will be passed as an argument to the function.
The point of making a function a command is to make calling it easy for end-users, not just for Emacs Lisp programmers. Commands (run with M-x) are easier to run interactively in these ways:
- You don’t have to surround the command name with
()
to make it a valid S-expression. - Arguments can be passed automatically (such as the cursor position), or you can be prompted for them so you don’t have to remember what arguments are needed.
- When prompted for an argument, you can auto-complete it, because the
interactive
form’s code characters (likeb
) specify what type of input to expect.
The reason you can’t call the function set-face-attribute
with M-x is that its definition does not contain an interactive
form, and so set-face-attribute
is not a command. You must call it as a plain function, in S-expressions. You can do that from the minibuffer with M-:, or from other places with any of the other ways of evaluating code.
Emacs Mini Manual → Concepts → Command has a short, differently-worded explanation of the difference between normal functions and commands. Relationship between Emacs functions and commands explains some details not in this answer.
这篇关于为什么某些 Emacs 功能不能通过`M-x` 使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!