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
17
votes
Accepted
call interactive function from elisp code without worrying about arguments
If you want to call an interactive function from within elisp, but
call it as if you invoked it interactively (e.g., via M-x some-command), you can wrap it in the call-interactively function.
The ...
Dan♦
- 33.3k
14
votes
Accepted
Insert character by its Unicode name
From the documentation of insert-char, I cannot see why
(insert-char "GREEK SMALL LETTER EPSILON")
doesn't work.
It doesn't work because insert-char understands Unicode character names only ...
8
votes
How to write a function with an interactive choice of the value of the argument?
(defun foo (choice)
"..."
(interactive
(list (completing-read "Choose: "
'(("1" . "Cow") ("2" . "Rabbit") ("3" . "Dog")) nil t)))
(message "You chose `%s'" choice)
...
8
votes
How to repeat a command immediately after calling it?
You are looking for repeat, bound by default to C-x z. The
first part of the docstring:
(repeat REPEAT-ARG)
Repeat most recently executed command.
If REPEAT-ARG is non-nil (interactively, ...
Dan♦
- 33.3k
8
votes
Accepted
Break key as a shortcut -- what am I doing wrong?
You ask several related questions, but this is the main one, I think:
How does one find out what name emacs gives to a key such as the one labeled pause/break on my keyboard?
For Emacs's description ...
8
votes
Default argument for interactive function?
(defun hello (st)
(interactive (list (read-string "Your name: " "toto")))
(message "Hello Mr %s" st))
where "toto" is the initial-input.
6
votes
Can Emacs use tramp to run in an interactive session on a remote HPC node?
I have been struggling to do the same. I found a different route that allowed me to do basically the same thing.
You can open a shell buffer by M-x shell and from there connect to the login node, and ...
6
votes
Accepted
How to write a function with an interactive choice of the value of the argument?
The function you are looking for is read-char-choice.
I cite the corresponding documentation:
(read-char-choice PROMPT CHARS &optional INHIBIT-KEYBOARD-QUIT)
Read and return one of CHARS, ...
5
votes
Accepted
Detect interactive use of `kill-buffer` in `kill-buffer-hook`
Test the variable this-command to see whether it is kill-buffer
5
votes
Accepted
Read input char without pressing RET
You apparently want to read a character and immediately dispatch behavior based on what it is.
Help > Search Documentation > Find Any Object by Name (command apropos) tells you, for read plus char ...
5
votes
How can I read a single character from the minibuffer?
The question was answered long ago, but this additional answer may provide some assistance to other searchers.
read-char-choice allows you to specify a list of choices. The fn will not return until ...
5
votes
Accepted
How to repeat a command immediately after calling it?
There is also repeat-complex-command (normally bound to C-x ESC ESC as well as several others (that's the one I use, so I remember that)). This will go back further in the history to the last command ...
5
votes
Accepted
Combine two interactive functions
[I believe you meant sort-lines, not sort - that's the only way the question makes sense, so I am going to assume it.]
Both sort-lines and delete-duplicate-lines operate on the selected region. You ...
4
votes
Near-identical commands on multiple keys
Let's look at this logically: you want to have near-identical commands bound to C-f2 and C-f3. The only difference between these commands is whether they store the thing under point in the f2 memory ...
4
votes
Near-identical commands on multiple keys
You can bind one command to several key-sequences and access the calling key-sequence within the command with this-command-keys.
That is demonstrated with the following code. There the same command my-...
4
votes
How to reuse last input in command with a prompt?
Commands that read input from the minibuffer use functions such as completing-read, read-string, read-regexp, etc. The most general of these is read-from-minibuffer.
All such functions take a HISTORY ...
4
votes
Accepted
how to provide a parameter for interactive lamda function
The (lambda () (interactive) ...) syntax is just fine. The problem you are encountering is with your invocation of helm-swoop. The arguments for that function must be keyed (preceded by their ...
4
votes
Accepted
(interactive "r") can't be used directly after Emacs startup
The use-region-p test should be within the interactive spec.
"r" is for functions that always use the region. You probably want to use something like:
(defun try-region (&optional begin end)
(...
3
votes
Accepted
"Invalid use of '\' in replacement text" with non-interactive calls
(Credits go to Tobias)
replace-regexp (and query-replace-regexp) have their second read argument malaxed into a suitable form for the elisp call. In particular, in case the replacement string ...
3
votes
Accepted
How to reuse last input in command with a prompt?
You could use read-string and give they default value def-say-word and save in later.
(setq def-say-word "default value")
(defun say-word (word)
(interactive
(list (read-string (format "Say ...
3
votes
Problem with giving inputs to a function programmatically
The function sunrise-sunset is an interactive function takes a numeric prefix argument as a parameter. It does not take the latitude and longitude as parameters, which is what you're trying to pass ...
3
votes
Accepted
Use write-region in read-only buffer
The interactive code * is used to: "Signal an error if the current buffer is read-only. Special."
https://www.gnu.org/software/emacs/manual/html_node/elisp/Interactive-Codes.html
Thus, the line ...
3
votes
Accepted
Assign buffer-name to variable
Use cond to bind either the buffer provided or the current buffer to my-source-buffer. No need for the extra line below. As there is actually only one condition an if statement would probably serve ...
3
votes
Accepted
Asterisk in interactive specification
First, this is untrue, assuming I understand what you mean:
On the other hand, pretty much any command can be made a no-op by a zero prefix argument
Beyond that, the answer, IMO, is that you do what ...
3
votes
Accepted
How to prompt for function use be used within a function?
One way is to use an interactive code to prompt you for the function.
(defun simple-function (input-function)
(interactive "aInput function: ")
(dotimes (counter 10)
(funcall input-...
3
votes
command print no results with (interactive "r")
You're confusing the echoing of the return value by M-: with the action of M-x.
M-: expressly evaluates a sexp and prints the resulting value. M-x invokes a command.
Your command does not, itself, ...
3
votes
Accepted
How to preselect initial text in the minibuffer with Elisp?
Use macro minibuffer-with-setup-hook to set the mark at the end of the minibuffer prompt. read-string then inserts the initial text with the mark active, which means that initial text is selected as ...
3
votes
Accepted
Can there be two prefix arguments?
There are no multiple prefix arguments.
The interactive specification (interactive "*P\nP") means that the one-and-only raw prefix argument is used twice for the command arguments.
In the ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
interactive × 148commands × 23
key-bindings × 12
functions × 12
prefix-argument × 10
arguments × 10
buffers × 6
region × 6
string × 5
defun × 5
debugging × 4
regular-expressions × 4
minibuffer × 4
read × 4
call-interactively × 4
org-mode × 3
helm × 3
shell × 3
completion × 3
files × 3
advice × 3
characters × 3
completing-read × 3
thing-at-point × 3
spacemacs × 2