jcubic
jcubic

Reputation: 66650

Emacs Lisp function with optional argument call other function

I have function like this:

(defun swap-region-ring ()
  "replace selected text with the one from kill ring"
  (interactive)
  (backward-delete-char (- (point) (mark)))
  (yank))

(global-set-key (kbd "C-c y") 'swap-region-ring)

How can I rewrite that function to call yank with argument and have also optional argument so it act the same as yank? So I can call C-u 2 C-c y

Upvotes: 0

Views: 363

Answers (1)

legoscia
legoscia

Reputation: 41658

yank happens to accept a "raw prefix argument" as its argument, so you can pick it up and forward it:

(defun swap-region-ring (&optional arg)
  "replace selected text with the one from kill ring"
  (interactive "*P")
  (backward-delete-char (- (point) (mark)))
  (yank arg))

Type C-h f interactive for more information about the interactive spec.

Upvotes: 1

Related Questions