Endless Parentheses

Concise ramblings on Emacs productivity.

A comment-or-uncomment-sexp command

Commenting is a very frequent piece of a programmer’s workflow, and it’s important to make it seamless and simple. For the more statemental languages, that’s as easy as writing a custom comment-line command. However, when you’re writing in Lisp languages, that just won’t do. Trying to comment out lines in a sexp-oriented structure, feels a lot like trying to hit a nail with a heavy screwdriver—it sometimes gets the job done, but it mostly just leads to frustration.

That said, a comment-sexp command is considerably more complicated to write. Not because commenting sexps is hard, but because it is quite difficult to identify sexps when removing comments. Still, I’m nothing if not stubborn. So after much hair pulling and teeth gritting, I have finally found a version I’m happy with.


The gif above speaks for itself, so I’ll just give you the code and let you play with it.

(defun uncomment-sexp (&optional n)
  "Uncomment a sexp around point."
  (interactive "P")
  (let* ((initial-point (point-marker))
         (inhibit-field-text-motion t)
         (end (save-excursion
                (when (elt (syntax-ppss) 4)
                  (re-search-backward comment-start-skip
                (setq p (point-marker))
                (comment-forward (point-max))
         (beg (save-excursion
                (forward-line 0)
                (while (and (not (bobp))
                            (= end (save-excursion
                                     (comment-forward (point-max))
                  (forward-line -1))
                (goto-char (line-end-position))
                (re-search-backward comment-start-skip
                  (while (looking-at-p comment-start-skip)
                    (forward-char -1)))
    (unless (= beg end)
      (uncomment-region beg end)
      (goto-char p)
      ;; Indentify the "top-level" sexp inside the comment.
      (while (and (ignore-errors (backward-up-list) t)
                  (>= (point) beg))
        (skip-chars-backward (rx (syntax expression-prefix)))
        (setq p (point-marker)))
      ;; Re-comment everything before it. 
        (comment-region beg p))
      ;; And everything after it.
      (goto-char p)
      (forward-sexp (or n 1))
      (skip-chars-forward "\r\n[:blank:]")
      (if (< (point) end)
            (comment-region (point) end))
        ;; If this is a closing delimiter, pull it up.
        (goto-char end)
        (skip-chars-forward "\r\n[:blank:]")
        (when (eq 5 (car (syntax-after (point))))
    ;; Without a prefix, it's more useful to leave point where
    ;; it was.
    (unless n
      (goto-char initial-point))))

(defun comment-sexp--raw ()
  "Comment the sexp at point or ahead of point."
  (pcase (or (bounds-of-thing-at-point 'sexp)
               (skip-chars-forward "\r\n[:blank:]")
               (bounds-of-thing-at-point 'sexp)))
    (`(,l . ,r)
     (goto-char r)
     (skip-chars-forward "\r\n[:blank:]")
       (comment-region l r))
     (skip-chars-forward "\r\n[:blank:]"))))

(defun comment-or-uncomment-sexp (&optional n)
  "Comment the sexp at point and move past it.
If already inside (or before) a comment, uncomment instead.
With a prefix argument N, (un)comment that many sexps."
  (interactive "P")
  (if (or (elt (syntax-ppss) 4)
          (< (save-excursion
               (skip-chars-forward "\r\n[:blank:]")
               (comment-forward 1)
      (uncomment-sexp n)
    (dotimes (_ (or n 1))

And, of course, don’t forget to bind it.

(global-set-key (kbd "C-M-;") #'comment-or-uncomment-sexp)

Tags: clojure, lisp, programming, init.el, emacs

Say thanks on Gratipay
comments powered by Disqus