1

I'm in org-mode and would like to have C-c C-c ' run org-edit-special then mark-whole-buffer then indent-region then org-edit-special to get back out again. I've tried

(defun my-run-org-babel-codeblock-format ()
  "Run org babel codeblock formatting in sequence."
  (interactive)
  (org-edit-special)
  (mark-whole-buffer)
  (indent-region))

(global-set-key (kbd "C-c C-'") 'my-run-org-babel-codeblock-format)

but this doesn't work:

my-run-org-babel-codeblock-format: Wrong number of arguments: (2 . 3), 0

Apparently multiple means no more than two commands. That is, when I remove (indent-region) it seems to work. Is there something I can do to have all four commands under one key binding?

  • indent-region requires at least two parameters, as in (indent-region START END &optional COLUMN) – Ruy Nov 17 '17 at 21:30
  • I do `C-M-\` and it automatically aligns the selected region of code. What sort of parameters do I need to tell it? – 147pm Nov 17 '17 at 21:37
  • Try (let ((x (mark)) (y (point))) (indent-region (min x y) (max x y))) – Ruy Nov 17 '17 at 21:48
  • START should be (buffer-end -1) and END should be (buffer-end 1). – NickD Nov 17 '17 at 21:50
  • BTW, mark-whole-buffer is for interactive use only: you should avoid using it in programs (precisely because it sets the mark). – NickD Nov 17 '17 at 21:52
2

Various options:

  1. Make it a keyboard macro rather than an Elisp function. I.e. rather than record the functions you invoke, only record the keys you press.
  2. reproduce faithfully the interactive calls to the functions:

    (defun my-run-org-babel-codeblock-format ()
      "Run org babel codeblock formatting in sequence."
      (interactive)
      (call-interactively 'org-edit-special)
      (call-interactively 'mark-whole-buffer)
      (call-interactively 'indent-region))
    
  3. Mimic by hand the computation of the arguments:

    (defun my-run-org-babel-codeblock-format ()
      "Run org babel codeblock formatting in sequence."
      (interactive)
      (org-edit-special)
      (mark-whole-buffer)
      (indent-region (region-beginning) (region-end)))
    
  4. Avoid commands which aren't meant to be used within functions:

    (defun my-run-org-babel-codeblock-format ()
      "Run org babel codeblock formatting in sequence."
      (interactive)
      (org-edit-special)
      (indent-region (point-min) (point-max)))
    
  5. ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.