Julie
Julie

Reputation: 13

How can I run multiple commands in emacs?

I try to run several commands in a sequence in Emacs. None of the commands needs an argument (indent-region, untabify, and delete-trailing-whitespace). I tried to follow the older post on emacs-key-binding-for-multiple-commands and came up with the following solution:

(defun format-properly ()
    "Run `indent-region', `untabify' and `delete-trailing-whitespace' in sequence."
    (interactive)
    (indent-region)
    (untabify)
    (delete-trailing-whitespace))

(global-set-key (kbd "C-c a b c") 'format-properly)

This gives me the following error message when I try to run it: "Wrong number of arguments: (2 . 3), 0".

Since I have zero experience with lisp, I don't have any idea what to do and would be happy about any suggestion. :)

Thanks! Julie

Upvotes: 1

Views: 1859

Answers (1)

jpkotta
jpkotta

Reputation: 9427

To build on lawlist's comment, interactive commands often take non-optional arguments, even when you don't explicitly supply them. There's a special way to declare an interactive command that takes the region: (interactive "r"), that will pass the region to the command automatically.

(defun format-properly (b e)
  "Run `indent-region', `untabify' and `delete-trailing-whitespace' in sequence."
  (interactive "*r")
  (when (use-region-p)
    (save-restriction
      (narrow-to-region b e)
      (indent-region (point-min) (point-max))
      (untabify (point-min) (point-max))
      (delete-trailing-whitespace (point-min) (point-max)))))

Try looking at the docs for interactive and any other functions or variables you're interested in with C-h o (describe-symbol).

Upvotes: 4

Related Questions