I wanted to jump to the end of the current top-level S-expression as described in this question: How to move up to the ending parenthesis of the enclosing list (if it exists)?. The goal is to position the point in a location such that the current top-level
S-expression can be evaluated with C-x C-e
(and then evaluate it). Based on the answers linked above, I have the following shortcut key sequence:
(defalias 'eval-next-sexp
;; (kbd "M-m C-f C-u C-M-n C-x C-e")
(kbd "M-m C-f C-u C-M-n")
)
(global-set-key (kbd "C-x C-y") 'eval-next-sexp)
As I am using paredit mode, M-m
moves the cursor to the first non-whitespace position, C-f
moves one letter into the S-expression, C-u C-M-n
repeatedly moves up the hierarchy of parentheses (paredit-forward-up
) until it cannot move any further.
So far this has been (mostly) successful in moving the point to where it needs to be, and a subsequent C-x C-e
evaluates the expression.
The problem is that when I tried to combine the move and the evaluation, as shown in the commented line: (kbd "M-m C-f C-u C-M-n C-x C-e")
, the evaluation doesn't happen because the movement command generates an error when stopping:
After 0 kbd macro iterations: Scan error: "Unbalanced parentheses", 1647, 12558
My guess is that C-u C-M-n
reached top and generates an error.
The question is:
How can errors from previous commands as above be ignored and the subsequent key strokes be executed?
Additionally, is it possible to repeat a key combination/command (C-M-n
here) until an error happens? (If so, the going up can be implemented more reliably)
C-u C-u
prefix will suffice for 16 levels which is probably more than enough in practice - but you can use three of them for 64 levels if necessary - that should be enough for all practical purposes.M-m C-f
necessary?M-m C-f
is just a heuristic to get into the top-level sexp. e.g. in|(add1 1)
.C-M-n
will do nothing. After the initial move,(|add1 1)
,C-M-n
works. Maybe there are better ways..C-u
and thought it's the same as a while loop.