emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: New comment-sexp command and key bind


From: João Távora
Subject: Re: New comment-sexp command and key bind
Date: Fri, 21 Aug 2015 12:31:40 +0100

This is interesting, I just tried it.

Was it your intention to make the uncommenting behaviour for
consecutive sexps different from the commenting behaviour? The former
keeps point at the beginning of the sexp, while the latter advances
one sexp. I thing they should be uniform.

I would prefer to *not* advance sexps. But I realize that that has the
drawback that it is difficult to advance one recently commented sexp.

For now I'll stick to C-M-SPC M-;

João

On Fri, Aug 21, 2015 at 12:11 PM, Artur Malabarba
<address@hidden> wrote:
> I'd like to add the following command that comments and uncomments
> sexps. I'm attacking a gif to show its behavior, but it is pretty much
> the same as our current comment-line, except it acts on sexps.
>
> Furthermore, I'd like to bind it to `C-M-;' since C-M- is a relatively
> common modifier for sexp-stuff.
>
> -----
> (defun uncomment-sexp (&optional n)
>   "Uncomment a sexp around point."
>   (interactive "P")
>   (let* ((initial-point (point-marker))
>          (p)
>          (end (save-excursion
>                 (when (elt (syntax-ppss) 4)
>                   (re-search-backward comment-start-skip
>                                       (line-beginning-position)
>                                       t))
>                 (setq p (point-marker))
>                 (comment-forward (point-max))
>                 (point-marker)))
>          (beg (save-excursion
>                 (forward-line 0)
>                 (while (= end (save-excursion
>                                 (comment-forward (point-max))
>                                 (point)))
>                   (forward-line -1))
>                 (goto-char (line-end-position))
>                 (re-search-backward comment-start-skip
>                                     (line-beginning-position)
>                                     t)
>                 (while (looking-at-p comment-start-skip)
>                   (forward-char -1))
>                 (point-marker))))
>     (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.
>       (ignore-errors
>         (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)
>           (ignore-errors
>             (comment-region (point) end))
>         ;; If this is a closing delimiter, pull it up.
>         (goto-char end)
>         (skip-chars-forward "\r\n[:blank:]")
>         (when (= 5 (car (syntax-after (point))))
>           (delete-indentation))))
>     ;; 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)
>              (save-excursion
>                (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:]")
>                (point))
>              (save-excursion
>                (comment-forward 1)
>                (point))))
>       (uncomment-sexp n)
>     (dotimes (_ (or n 1))
>       (comment-sexp--raw))))



-- 
João Távora



reply via email to

[Prev in Thread] Current Thread [Next in Thread]