2

There is an option to

repeat a macro an infinite number of times until the function quits (for example, by reaching the end of the buffer) or if the user cancels the command in keyboard macros

by using C-u0 as prefix to C-xe.

How can we mimic this behavior in Elisp code with the loop? For example, I would like to write a code that brings point to the outermost bracket to check if it is of a certain value:

(defun outer-paren()
  (interactive)
  (while condition
    (up-list)))

What should condition be to mimic keyboard macro infinite argument behavior?

((some (|text) embedded) within brackets) --> |((some (text) embedded) within brackets)

1

You really ask two questions:

  1. How to iterate forever?

    (while t ...)

    See the Elisp manual, node Iteration.

    The first argument of while is a sexp. It is evaluated to see if the body needs to be evaluated again. The sexp t always evaluates to itself, a non-nil value, so the body is always reevaluated.

  2. How to get the outermost list?

    (defun outer-paren ()
      "Return the outermost list."
      (interactive)
      (let ((xs  nil))
        (while (setq xs  (list-at-point)) (up-list))
        xs))

But I recommend using tap-list-at-point (or tap-unquoted-list-at-point), from library Thing-At-Point-Plus, rather than the vanilla list-at-point.

2
  • How do I prevent Scan error: "Unbalanced parentheses", 186, 186 from cropping up when the function is executed? – Sati Feb 9 '20 at 18:03
  • 1
    Updated, to reflect that second question. (You really should pose only one question per question, however.) – Drew Feb 9 '20 at 18:33

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.