Rorschach
Rorschach

Reputation: 32466

Paredit ignore comments when closing parens

Often I run into situations like the following while using paredit, with the point at the | in the following snippet where I want to close the dangling paren, eg.

(let ((foo 1)| ; blag
      )
  nil)

becomes

(let ((foo 1)) ; blag
  nil)

after type ) aka paredit-close-parenthesis at the point.

If the comment wasn't on the line, paredit would close the paren and remove the space. Does anyone have a nice way to enable this feature?

Looking at the code, paredit appears to catch errors where I could add handlers, so I'm wondering if there is a simple solution out there.

Upvotes: 0

Views: 74

Answers (1)

Rorschach
Rorschach

Reputation: 32466

I guess overriding it seems to work so far.

(defun my-paredit-close-round (&optional arg)
  (interactive "P")
  (if arg (paredit-close-round)
    (let ((beg (point)) ;keep comment on same line
          (cmt (paredit-find-comment-on-line)))
      (paredit-move-past-close ?\))
      (and cmt (save-excursion
                 (unless (eq (line-number-at-pos) (line-number-at-pos beg))
                   (goto-char beg))
                 (insert (car cmt)))))))
(advice-add 'paredit-close-round :override #'my-paredit-close-round)

However paredit-backward-barf-sexp still gets stuck at comments and would need to be overriden as well.

Upvotes: 0

Related Questions