14
votes

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.

My questions are:

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?

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)

2
M-x is not a function, it is a key sequence which is bound to a function. As it happens, it is bound to a function which takes a command as a parameter; when called interactively, its interactive form causes it to prompt for a command name.tripleee

2 Answers

23
votes

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

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)

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

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

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.

6
votes

One more detail: for people who wonder why you'd need to add (interactive) to label a function as being also a command, the trick is that interactive is there to explain how to provide arguments to the function. So (interactive) says "this is also a command and when run interactively, just call the function without arguments", where (interactive (list 2)) says "this is also a command and when run interactively, evaluate the expression (list 2) will build the list of arguments to pass to the command".

There's been discussion about extending interactive so that instead of only taking the form (interactive ARGs-FORM) it can also take the form (interactive ARGS-FORM RETURN-FORM) where RETURN-FORM specifies what to do with the return value, when called interactively. Typical example: current-column as a function does not print anything but just return a number, but when run interactively (i.e. used as a command) if it doesn't print anything it becomes useless, so the RETURN-FORM would take the number returned and print it.