Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interactive "r" elisp defun with additional args?

Is it possible to write an interactive defun with code "r" that has an additional optional argument (so that it does things within the selected region, but with another argument)? I would like something like the following:

(defun my-function (start end &optional arg)
  "Do something with selected region"
  (interactive "r")
  (if arg
      (setq val arg)
    (setq val 2))
  (do things...))

Looking at the documentation it says

'r': Point and the mark, as two numeric arguments, smallest first. This is the only code letter that specifies two successive arguments rather than one. No I/O.

I'm not sure if the 'No I/O' and 'two successive arguments' means that it takes 2 and only 2 arguments (i.e., limited to the region's start and end point as args). Although it allows me to evaluate and run the defun with an additional argument, Emacs appears to be ignoring it.

Thank you.

like image 236
beardc Avatar asked Jan 21 '23 01:01

beardc


1 Answers

To make interactive ask for multiple parameters, separate them with a newline character. For instance, if you want your third parameter be bound to the value of the prefix argument, define your function like this:

(defun my-function (start end &optional arg)
  "Do something with selected region"
  (interactive "r\np")
  (if arg
      (setq val arg)
    (setq val 2))
  (do things...))

M-x describe-function interactive gives you further information.

like image 140
Thomas Avatar answered Jan 28 '23 10:01

Thomas