21

(defun some-function (beginning end) (interactive "r") (if (use-region-p) (message "The region is active, and is from %d to %d" beginning end) (message "The region is still there (from % d to %d), but it is inactive" beginning end))) The use of (interactive "r") means that the parameters beginning and end will automatically ...


20

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


11

Hard to believe that people described this here without also giving you links to Emacs's own descriptions of this: In the Emacs manual, node Arguments. In the Elisp manual, node Prefix Command Arguments. "Ask Emacs!": C-h i, choose a manual, i prefix argument RET. In a nutshell, though this is said well enough by other answers here, and is described in ...


9

Hit C-c C-b to select a buffer for IELM to work upon.


9

When raw prefix Interactive Code "P" is used, the argument is passed on as it is whereas "p" converts the arg to a number. After evaluating the below elisp, try out C-u M-x my/fn-with-num-arg and then C-u M-x my/fn-with-raw-arg to see the difference because C-u passes a list argument (4). (defun my/debug-fn (arg) (let ((msg)) (if (numberp arg) ...


9

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 when called interactively (e.g. via C-x8RET or M-xinsert-charRET), as stated in its docstring: Interactively, prompt for CHARACTER. You can specify CHARACTER ...


8

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


8

According to shell's interactive form, as long as current-prefix-arg is non-nil, shell will ask user a buffer to use, so you can set current-prefix-arg to non-nil (4 is used in following as an example): (let ((current-prefix-arg 4)) (call-interactively 'shell)) or simulate executing shell with a prefix 4 (M-4 M-x shell): (execute-extended-command 4 "...


8

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


7

Just to add a bit more detail to @kaushalmodi's answer (and useful test case): The raw argument lets you distinguish between arguments provided with universal-argument and digit-argument. With a numeric prefix arg there is no way to distinguish the universal argument C-u from a prefix arg of 4 (i.e. C-4). With the raw argument these are different: (4) vs ...


7

Interactive simply provides a way to call the function via M-x. You can use venv-workon normally, like any other emacs-lisp function. Here's an example: (require 'virtualenvwrapper) (venv-workon "my-python-project") For more detail on what interactive does, see Using Interactive from the Elisp docs. Essentially, interactive marks a function as a ...


7

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


7

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


7

Forms in the function body are executed in order, just as you would expect. When you choose "y" if does not "jump" past the first message call. If looks like it did because the first message gets immediately replaced by the second one. To check that this is true, open the *Messages* buffer and then do M-x foo. You will see that foo adds two messages to the ...


6

You can look up the stack using the backtrace frames, here is an implementation getting the name of the current function. Can functions access their name? The code in that question looks up the stack until it finds the frame representing a function call which is the current function, simply continue to move up the backtrace frames until you find the second ...


6

Rather than read-char I recommend read-key. The difference is that read-key obeys all the usual remappings such as input-decode-map and function-key-map, so it will work properly in a tty.


5

If it's ok, that the function isn't interactive in the first place, something like this might work. (defmacro define-noninteractive-minor-mode (mode doc &optional init-value lighter keymap &rest body) `(cl-macrolet ((interactive (&rest _))) (...


5

Given the function (defun foo (arg1 arg2) (interactive (list 'interactive-value1 'interactive-value2) (message "%s %s" arg1 arg2)) You would get (foo 'value1 'value2) => "value1 value2" (call-interactively 'foo) => "interactive-value1 interactive-value2" And when actually using foo as a command M-x foo RET => "interactive-value1 interactive-...


5

In addition to built-in ways to read single events such as read-char and read-char-exclusive, here's an option to read a single character, but also specify which characters are acceptable input: (defun read-char-picky (prompt chars &optional inherit-input-method seconds) "Read characters like in `read-char-exclusive', but if input is not one of CHARS, ...


5

Here's a more elegant way of doing it: (defun foo (arg) (interactive (list (if (consp current-prefix-arg) (read-number "Number: ") (prefix-numeric-value current-prefix-arg)))) (message "prefix = %0d" arg)) current-prefix-arg holds the value you'd get from (interactive "P"). You can convert it to what (interactive "p") would ...


5

So as per my earlier comment, Emacs doesn't really support what you're asking for. I would strongly recommend that you simply define a global command in the normal way, and make it behave appropriately based on the presence or not of a local variable (which could be directory-local). It can still be called from anywhere, but you would ensure that it never ...


5

This is because read-char is advised to have special behaviour. When running in 'multiple-cursors-mode' it caches the character read the first time. If this cache is full then it does not ask for a character again. Whatever function is called by "c" in the interactive description is not advised in this way. The implementation is incredibly hackish: for ...


5

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 that required interaction (i.e. prompted for input).


5

Test the variable this-command to see whether it is kill-buffer


5

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, prompting for PROMPT. Any input that is not one of CHARS is ignored. If optional argument INHIBIT-KEYBOARD-QUIT is non-nil, ignore keyboard-quit events ...


5

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 first part of the docstring reads: (call-interactively FUNCTION &optional RECORD-FLAG KEYS) Call FUNCTION, providing args according to its interactive ...


4

call-interactively is what interprets the (interactive "cPROMPT") specification, the c option is dispatched to read-char. Therefore, the following should work in a non-interactive context: (read-char "(C)hoose (A)n (O)ption")


4

The current interactive command is: C-hv this-command RET That doesn't use the stack (I don't have a good answer for that), but it's the normal mechanism for doing this kind of thing.


4

When you're writing Emacs Lisp code, or indeed any sort of code, one of the most important things is to divide your work clearly, so users and programmers both can see role of every function. Interactive functions or commands are supposed to be invoked by user directly. They allow to use functionality you want to provide. You should not declare interactive ...


4

Do you want to visit (e.g., create) a file that has the same name as the buffer, even if the buffer is not visiting a file? If so: (defun foo (file) (interactive (list (expand-file-name (or (buffer-file-name) (buffer-name))))) (require 'tramp) (find-file (concat "/sudo::" file))) Do you want to raise an error if the current buffer is not visiting ...


Only top voted, non community-wiki answers of a minimum length are eligible