Every command is a function, but not every function is also a command.1
A command includes a call to interactive; this is why commands are commonly referred to as "interactive functions". Commands can be invoked via M-x name-of-command RET, and they can also be bound to a key sequence. Regular functions do not include a call to interactive, can not be ...
progn is a special form borrowed from CL-like Lisp dialects. In their implementations it's composed from multiple progs, expressions that are evaluated and a number that specifies which expression value is returned. prog1 for instance evaluates all expressions and returns the value of the first, prog2 evaluates all expressions and returns the value of the ...
Once upon a time, the sharp quote was necessary for lambdas, now that's no longer the case.
So, it appears that (lambda (x) x) and #'(lambda (x) x) are equivalent, but '(lambda (x) x) is not (most importantly, when byte-compiling).
Yes. In fact, the first two are completely identical when evaluated. As described in the manual page you linked:
Updated answer with expansion time lookup:
I said in my original answer that there may be a way to do this at expansion/compile time instead of run time to give better performance and I finally implemented that today while working on my answer for this question: How can I determine which function was called interactively in the stack?
Here is a function ...
The core point is that there is a difference between a function and a command.
In Emacs lisp, functions are not interactively callable by default. That means you can't access them via M-x or bind them to a key or mouse click. If you want to do that, you need to explicitly declare the function to be interactive, which you do by adding an (interactive) form ...
We have a number of options available.
You can catch / throw to exit the function.
(defun my-func ()
(throw 'my-catch "always going to throw"))
(+ 42 1)))
You can also use block and return-from (although you will need to require cl-macs)
In Emacs Lisp, if foo is a symbol, then 'foo and #'foo are completely equivalent. The latter form (with #') is preferred when foo is a function, as it documents the fact that it is intended to be funcalled.
Your two forms are therefore completely equivalent, and the one with #' is preferred.
Edit: as pointed out by Malabarba, this is not quite true: #' on ...
There are several ways to load Lisp files in Emacs:
load-file takes a literal filename (relative or absolute) and loads the code. Example: (load-file "/usr/local/share/site-lisp/foo.el")
load is like load-file, except that it is more flexible--you can leave out the extension and it will automatically load the "elc" or "el" file as appropriate.
It also looks ...
The idea of transforming functions into different functions with specialized arguments has been elaborated upon in SRFI 26. There's an Emacs Lisp implementation of it in the very useful dash.el library where it's available as -cut.
This risks being closed as unclear. Show your code that provokes an error, as well as telling how you invoke it. Show a backtrace from setting debug-on-error to t.
Your function foo looks OK; bar does not, because indent-region needs two arguments and you are passing only one. What's more, that argument would be the result of evaluating the sexp (start ...
I recommend you do C-h f replace-string RET and read it:
This function is for interactive use only;
in Lisp code use `search-forward' and `replace-match' instead.
So, as suggested replace that call with something like
(while (search-backward "'" beginning t)
(replace-match "\\'" t t)
(goto-char (match-beginning 0)))
Some functions do "...
There seems to be a fair bit of confusion how quoting works. In Lisp, symbols and lists fulfill a dual meaning, depending on whether they're quoted or not:
Unquoted symbol: Evaluate the symbol (by looking up its value in the current environment)
Quoted symbol: Do not evaluate the symbol (by returning the symbol)
Unquoted list: Evaluate the function (by ...
The special form which saves and restores the current point and buffer is save-excursion. So you could write your functions as:
(defun insert-line-below ()
"Insert an empty line below the current line."
(defun insert-line-above ()
"Insert an empty line above the current line."
#' is just shorthand for function, just as ' is shorthand for quote.
You can use it anywhere where you want to indicate to the byte-compiler or the interpreter or a human reader that its argument is expected to be (is treated as) a function.
In many contexts the context determines how the argument is treated if, for example, you simply quote it (use quote ...
Looks like you want mapcar. From the docstring:
(mapcar FUNCTION SEQUENCE)
Apply FUNCTION to each element of SEQUENCE, and make a list of the results.
The result is a list just as long as SEQUENCE.
SEQUENCE may be a list, a vector, a bool-vector, or a string.
(defun identity-fnx (x)
(mapcar #'identity-fnx '(1 2 3)) ; => (1 2 3)
The interactive special form provides the easiest way to get input from a user.
(defun td (variable)
(insert (format "std::cout << \"%s is: \" << %s << std::endl;" variable variable)))
Here "sVariable:" consists of the "s" code character (read a string) and the prompt. (See Using interactive in the Emacs ...
The first example becomes a little clearer if you look further down the function. Just a bit past what you quoted we have
(mapcar (lambda (x) (funcall display-fn x)) choices)
So a list of "choices" is being made and display-fn is being applied to each choice, presumably to modify how they get displayed. A sensible default is that each choice is ...
fset sets a symbol's function definition.
Here, projectile-command-map is used as a prefix command. A prefix command is a symbol whose function definition is a keymap.
The definition of a prefix key is usually the keymap to use for looking up the following event. The definition can also be a Lisp symbol whose function definition is the following keymap; ...
Edit: Woo! I found a function that will take either the normal argument list, or the integer version and return somewhat of a signature: byte-compile-arglist-signature in bytecomp.el!
(byte-compile-arglist-signature 1283) ;; => (3 . 5)
I hope someone else can chime in on whether or not this is documented somewhere but this is what I ...
load-file and load-library are commands for interactive use (where
the main difference is in the initial content of the prompt).
load is the underlying function to use when you write Elisp (such as
in your .emacs).
If you type C-h f <RET>, it describes the function called by the
innermost Lisp expression in the buffer around point, provided that
function name is a valid, defined Lisp function. (That name appears as
the default while you enter the argument.
Leave the C-u off and check the binding for C-SPC (or whatever you're interested in). The universal argument (the C-u) is often used to make commands do different things. However, the docstring of the command will (or at least should) explain what the command does when preceded by universal arguments.
While it is entirely subjective, I would suggest only making functions interactive if you actually expect them to be called that way. Many functions are intended to be used as building blocks rather than end-user commands.
It can be simpler to define a non-interactive function. While these factors are important for any function, they are particularly ...
You are looking for repeat, bound by default to C-x z. The
first part of the docstring:
Repeat most recently executed command.
If REPEAT-ARG is non-nil (interactively, with a prefix argument),
supply a prefix argument to that command. Otherwise, give the
command the same prefix argument it was given before, if any.
If you ...
buffer-file-name is a function, but not an interactive command. M-x calls execute-extended-command, so can't be used on non-command functions.
For more information, see What is the difference between a function and a command?.
"How do you get the function bound to a keymap starting with C-u where it acts differently from an ordinary argument"?
C-u C-SPC and C-SPC will run the same function, just with different arguments.
You'll need to read the documentation or the source code to figure out exactly what the difference are.
Others have mentioned some reasons for not making a given function into a command. Here is another: user discovery and access to commands.
If you ever manipulate or access commands interactively by name (e.g. M-x, C-h a).
(Or if you ever manipulate them by program, then you typically want them distinguished from other functions. But you might argue that ...
What you want is to change what H-x does when in python-mode so that instead of running eval-region (which only works on elisp) it calls python-shell-send-region. You can use define-key to do this:
(define-key python-mode-map (kbd "H-x") #'python-shell-send-region))