How easy is it to experiment with elisp code that involves moving the cursor (caret)?

I usually play with elisp code on my buffer from scratch. I find it difficult to play with elisp code that moves cursors this way because I don’t know how to separate the buffer for editing the 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 a search using the elsc count , I would like to run one line at a time and see what each line does. But the code moves the cursor in the buffer itself from scratch, which I inserted into this code, and I already use this caret to edit or run the code. Another problem is that this code needs to be tested on a TeX document, and my zero buffer is not a TeX document.

It all comes down to how to separate the artist and the picture.

Although in this example, just looking at the Ch f manual will be enough to understand what is going on. But this is only because this sample code is quite simple.

+4
source share
4 answers

Compile the function with edebug ( Cu CMx instead of CMx ), switch to the buffer for experiments, call the function via M-, and then one-step ( n ) in the debugger.

Otherwise, learn to think in large chunks and test in the test buffer using M- :. This is what I do for almost everything, including very complex code like cperl-mode .

+5
source

In addition to M-: (i.e., eval-expression), also see with-selected-window . He performs his body in the context of this window. For example, if you have two windows,

 (with-selected-window (next-window) (backward-up-list 1)) 

will perform the backward-up-list operation in another window.

+3
source

I found some workarounds (similar to selected windows)

Using set-buffer with progn:

 (progn (set-buffer "edithere.el") (insert "hello") (beginning-of-line)) 

The name of the edithere.el file must be present. Press CMx to evaluate the form of the run. You can also use let. When you want to write a command that edits the buffer or moves the cursor, instead of starting with "(defun ...", you can start with "(progn ..." as described above, and after it ends, change to defun .

Using with-current-buffer: (Press CMx to evaluate the form with the current buffer)

 (with-current-buffer "edithere.el" (insert "hello") (beginning-of-line)) 
+1
source

Cx 4 f . Enter the file name: foo.el Put your code there and test it there. A lot is better than a *scratch* buffer (Emacs-Lisp mode, on the one hand), and you can more easily save your work.

0
source

All Articles