0

I have the following custom implementation of insert-pair:

(defun *-insert-pair (&optional arg open close)
  "Wrap next ARG sexps in parentheses.

If there is no next sexp, insert a new pair of parentheses."
  (interactive "p")
  (let ((arg (or arg 1))
        (exists-next-sexp t))
    (save-excursion
      (condition-case nil
          (forward-sexp)
        (scan-error (setq exists-next-sexp nil))))
    (if exists-next-sexp
        (insert-pair arg open close)
      (insert-pair nil open close))))

However, I don't like the use of the boolean var exist-next-sexp. Is there a more idiomatic way to write this so I can get rid of it?

Drew
  • 77,472
  • 10
  • 114
  • 243
Tianxiang Xiong
  • 3,878
  • 18
  • 28
  • 1
  • The last bit could be written as just (insert-pair (and exists-next-sexp arg) open close), which makes clear that insert-pair is used in any case. 2. Don't bother with a second ARG variable (confusing to use the same name, and nothing gained). Just (setq arg (or arg 1).
  • – Drew Feb 24 '17 at 16:40
  • 1
  • Replace (setq exists-next-sexp nil) with (setq arg nil).
  • – npostavs Feb 24 '17 at 18:04