Search code examples
emacslispelisp

Emacs: Wrong number of arguments when trying to run an interactive function


I am trying to create a command that will indent a selected region by 4 spaces.

The appropriate commands are: C-u 4 C-x <TAB>, when C-u is a shortcut for "universal-argument" command and C-x <TAB> is a shortcut for indent-rigidly, so I've written this function:

(defun my-tab ()
  (interactive)
  (universal-argument 4)
  (indent-rigidly))

But when I'm trying to run the function (with M-x my-tab) I get this error:

Wrong number of arguments: (0 . 0), 1

What is the problem?

Thanks!


Solution

  • If you look at the documentation for indent-rigidly (C-h f indent-rigidly), you'll notice that it takes 3-4 arguments:

    (indent-rigidly START END ARG &optional INTERACTIVE)
    

    So, you should supply the start and end positions to it as well. You should also just give the ARG normally, instead of using universal-argument.

    (defun my-tab (start end)
      (interactive (if (use-region-p)
                       (list (region-beginning) (region-end))
                     ;; Operate on the current line if region is not to be used.
                     (list (line-beginning-position) (line-end-position))))
      (indent-rigidly start end 4))