27
votes
Why can't I bind my function to a key or call it with M-x?
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 ...
Community wiki
20
votes
Accepted
When should sharp quotes be used?
#' 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 ...
15
votes
Accepted
Is everything in Emacs made of functions?
Not everything is a function, no. For starters, Elisp (the language) has many other kinds of object besides functions.
However, I think you're specifically asking whether every interactive action ...
13
votes
Accepted
Insert line above/below
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 ...
13
votes
When should sharp quotes be used?
#' (aka function) can be used in front of (lambda ...) but it's redundant there, so the only place where it's really meaningful is in front of a symbol, as in #'car. In ELisp, #'car and 'car are ...
13
votes
How to delete a function?
Functions are unbound with:
(fmakunbound 'NAME)
(The equivalent for variables is makunbound.)
12
votes
Accepted
How do I invoke a non-interactive lisp function interactively?
You're looking for M-: which runs the command eval-expression
12
votes
Accepted
Are local nested functions possible in elisp?
You can also do it without resorting to cl-lib as follows:
(defun my-fn (x y)
(let ((my-local-fn (lambda (a b) (message "%S" (- a b)))))
(funcall my-local-fn x y)
(funcall my-local-fn y x))...
11
votes
Accepted
Define a function alias within a let binding?
Definitions
This topic confuses me sometimes, so let's start with some definitions, namely those of let:
let is a special form in ‘C source code’.
(let VARLIST BODY...)
Bind variables according to ...
10
votes
Accepted
How to invoke functions that are quoted in let?
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 ...
10
votes
Accepted
How to make interactive functions (simply) undoable?
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 ...
10
votes
Accepted
What does `(fset SYMBOL KEYMAP)` do?
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 ...
10
votes
Org-mode link to function definition
For elisp that is certainly possible. The general strategy might be something like:
[[elisp:(find-function 'describe-function)]]
You could add a description if you wanted to also, e.g.
[[elisp:(...
8
votes
How to save all modified buffers and kill emacs
From the docstring of C-x C-c:
With prefix ARG, silently save all file-visiting buffers, then kill.
So you want to use C-u C-x C-c
EDIT: If you also want that the above silently kills all ...
8
votes
How to bind keys to a function which can be defined per-mode?
(defvar my-local-variable nil
"Variable used on a bufer-local basis to indicate ...")
(make-variable-buffer-local 'my-local-variable)
USAGE:
(setq my-local-variable (lambda () (message "major-...
8
votes
Are local nested functions possible in elisp?
You can use cl-flet or cl-labels from cl-lib. The docstrings:
(cl-flet ((FUNC ARGLIST BODY...) ...) FORM...)
Probably introduced at or before Emacs version 24.3.
Make local function definitions.
...
Dan♦
- 33.4k
8
votes
Accepted
How to replace focus-out-hook with after-focus-change-function in emacs 27?
You could try using something like:
(add-function :after after-focus-change-function #'your-function-here)
So, in your case, something like this should do what you are after:
(add-function :after ...
8
votes
Accepted
Why do people add their initials to the names of tailor-made functions added to their config file?
It's a workaround for a misfeature of elisp: no namespaces. Its main purpose is to prevent name collisions for code written by different users/provided by different packages. It secondarily helps ...
8
votes
When is the first element in the argument list regarded as a function symbol and when not?
the first element of a list is interpreted as a function symbol
That's the most common case, but it isn't an absolute rule.
First, you need to know that every symbol can have both a function meaning (...
7
votes
Accepted
Symbol's function definition is void: insert-string
See this issue. This problem should be fixed in the next release.
Quick fix:
Change the function insert-string to insert in ~/.emacs.d/core/core-spacemacs-buffer.el, and everything works well.
7
votes
Keybinding for buffer word count
Your binding does successfully run the keyboard macro you've specified.
The problem is that the resulting message does not remain in the echo area long enough for you to see it, which is a side-...
7
votes
Accepted
Hook with quoted lambda as an argument
(add-hook 'coffee-mode-hook '(lambda () (coffee-custom)))
Definitely not recommended.
Quoting lambdas like that is not good practice.
As you say, (add-hook 'coffee-mode-hook #'coffee-custom) would ...
7
votes
Accepted
How to delete a function?
In addition to removing the function definition with fmakunbound and variable binding with makunbound, you can also remove the name itself with unintern.
Note also that, unless you save your ...
7
votes
Accepted
Symbol’s function definition is void
You define a local variable, then later call a non-existent function with the same name in the if:
(if (helm-in-frame-p)
Drop the surrounding parentheses and it should work again.
7
votes
How to temporarily change the definition of a function?
How to temporarily change the definition of a function?
Calling a function by its (symbol) name means calling that symbol's function slot. So, to temporarily change the definition of a function that ...
7
votes
Accepted
Why can't I compose commands `mark-whole-buffer` and `copy-region-as-kill` in Elisp code?
What's wrong with your attempt
(copy-region-as-kill (mark-whole-buffer))
This calls the function copy-region-as-kill with one argument, which is the value returned by mark-whole-buffer. All Emacs ...
7
votes
Call function without calling advice defined on it
I've thought about this before, and there's a less-than-perfect solution:
you need to store the original definition before any advice-adding.
(defun f ()
(insert "233\n"))
(setq f.backup ...
6
votes
Accepted
Bringing a function into a lexical environment
You're trying here to access the lexical variable x from outside its lexical scope, which obviously can't work. What you can do is pass a lexical environment to eval:
LEXICAL can also be an actual ...
6
votes
Accepted
simple function in dired to create a dir with current date?
You seem to be over thinking this.
(defun z/dired-insert-date-folder ()
"Create new directory with current date"
(interactive)
(dired-create-directory (format-time-string "%d%m%Y")))
Your ...
6
votes
Accepted
practical relevance of storing value and function into one symbol
Emacs Lisp is a Lisp 2, which means only just what you described: the same symbol can have separate values as (1) a variable and (2) a function. (We call only #1 the "value" of the symbol.) Some ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
functions × 244elisp × 23
variables × 16
key-bindings × 12
interactive × 12
funcall × 12
commands × 11
quote × 11
symbols × 11
org-mode × 9
advice × 9
arguments × 9
defun × 9
debugging × 8
help × 7
list × 7
elisp-macros × 7
buffers × 6
apply × 6
symbol-function × 6
spacemacs × 5
hooks × 5
files × 5
lexical-scoping × 5
regular-expressions × 4