How to easily experiment with elisp code that involves moving a cursor (caret)?
caret, elisp, emacs
Solution
Compile the function with edebug (`C-u C-M-x` instead of `C-M-x`), switch to a buffer to experiment in, invoke the function via M-:, and then single-step (`n`) in the debugger.
Otherwise, learn to think in bigger chunks, and test in your test buffer with M-:. This is what I do for nearly everything, including very complicated code like cperl-mode.
Problem
I usually play with elisp code on my scratch buffer. I find it hard to play with elisp code that moves cursors in this way because I don't know how to separate the buffer for editing code and the buffer for testing the code. For example if I want to play with the following code: ``` (backward-up-list 1) (backward-sexp 1) (kill-sexp 2) ``` from searching with intelligent bracket counting elisp, I'd like to run one line at a time and see what each line does. But the code moves the caret in the very scratch buffer I pasted that code in and I'm already using that caret to edit or run the code. Another problem is that this code is supposed to be tested on a TeX document and my scratch buffer is not a TeX document. It all comes down to how to separate the painter and the painting. Though in that example, just looking at the C-h f manual would be enough to understand what's going on. But that's only because this example code is simple enough.