I want to write a small function that saves the cursor's current position, mark the whole buffer, indents it and then goes back to the previous cursor position. I understand there might be easier way to achieve the same result but I'd like to understand how these principles work in Elisp.
Here's what I tried to do :
(defun indent-whole-buffer () (interactive)
(call-interactively 'point-to-register)
(call-interactively (kbd "RET"))
(mark-whole-buffer)
(call-interactively 'indent-region)
(call-interactively 'jump-to-register)
(call-interactively (kbd "RET"))
)
The blocking point here is the (call-interactively (kbd "RET"))
how can I simulate the RET key, just as if I was doing
M-x point-to-register RET
Just use save-excursion
. That's what it's for. It saves point and mark and which buffer is current, and then restores them for you.
(And if you did decide to roll your own, and you did decide to do it in the way you planned, then just call the functions you need directly - no need to use call-interactively
. Use C-h f
to see how each function is called. For example (point-to-register ?a)
captures point
in register a
, and (indent-region (point-min) (point-max))
indents the whole buffer.)