0

I know that I can use save-excursion to avoid changes to (point), is there a similar function to revert excursion based on the value of BODY. For example:

(revert-excursion BODY)

would revert the excursion if BODY evaluates to nil otherwise the new value of (point) is maintained. I could of course do this by saving the previous (point) and using (goto-char) conditionally, or by executing BODY twice (one to evaluate its value and the second to actually move (point). But I am wondering if there are better ways.

1

I think your first idea is the best:

   ...
   (goto-char (let ((pt (point)))
                (save-excursion 
                  (if BODY 
                     (point)
                    pt)))))

You could probably write a macro for it.

Evaluating BODY twice is not a good idea: it may have side-effects that make it non-idempotent, and/or it may be expensive to evaluate.

| improve this answer | |

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.