Elisp: Get universal-argument
How to Get Universal Argument
To make your command aware of Emacs: Universal Argument (prefix arg) , there are 3 simple ways:
- In the function body, check the builtin variable current-prefix-arg. It holds the value of universal argument.
- Add capital P as prompt string to Elisp: Interactive Form. It passes the value of current-prefix-arg as the function's first arg.
- Add lowercase p as prompt string to Elisp: Interactive Form. It passes converted numerical value of current-prefix-arg as the function's first arg.
(defun xf (x) "print value of `current-prefix-arg'" (interactive "P") (message "%s" x)) (defun xg () "print value of `current-prefix-arg'" (interactive) (message "%s" current-prefix-arg)) ;; try ;; M-x xg ;; C-u M-x xg ;; C-u C-u M-x xg ;; C-u 1 M-x xg ;; C-u 2 M-x xg
Possible Values of Universal Argument
Here's the possible values of
current-prefix-arg
and
(prefix-numeric-value current-prefix-arg)
Key Input | current-prefix-arg | prefix-numeric-value |
---|---|---|
none | nil | 1 |
Ctrl+u - | Symbol - | -1 |
Ctrl+u - 2 | Number -2 | -2 |
Ctrl+u 1 | Number 1 | 1 |
Ctrl+u 4 | Number 4 | 4 |
Ctrl+u | List '(4) | 4 |
Ctrl+u Ctrl+u | List '(16) | 16 |
Ctrl+u Ctrl+u Ctrl+u | List '(64) | 64 |
Convert current-prefix-arg to number
The function prefix-numeric-value
converts current-prefix-arg to number.
(defun xh () "print numerical prefix arg received" (message "%s" (prefix-numeric-value current-prefix-arg) ) )