emacs-devel
[Top][All Lists]
Advanced

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

Re: [PATCH] newcomment.el (comment-line): New command.


From: Artur Malabarba
Subject: Re: [PATCH] newcomment.el (comment-line): New command.
Date: Sun, 1 Feb 2015 17:48:30 -0200

FWIW, the command I suggested is indeed repeatable because it moves
`forward-line' after commenting the line. So you can just invoke it
repeatedly to comment several lines (for those who don't use prefix
arguments).

I blogged about this feature this Monday [1], and there was an
above-average amount of comments saying "I do something similar" (7 is
by no means a crowd, but is above average :-P). This is a slight
indicator that people do expect this feature. Also, as I mentioned
before, the people coming from simpler IDEs are sure to miss this
feature. Which is a shame given how simple it is to implement.

[1]: http://endlessparentheses.com/implementing-comment-line.html

Just to clarify, this is not meant as replacement for comment-dwim,
this is a complement. For most languages, commenting a line is a very
common action, and having to do `C-a C-SPC C-n M-;' for that is sad to
say the least. comment-line is both faster and more natural when one
is commenting 1 or a handful of lines.

I would like to politely insist this gets included into newcomment.el
(with a keybind). I can do it myself if I get approval on a keybind.
I'd like something repeatable, like C-; or C-M-; but I'm also
receptive of Drew's suggestion of C-x C-;


Below is the most recent version, but I can also provide a version
which acts on active region (see link).

----------------------------------------

(defun endless/comment-line (n)
  "Comment or uncomment current line and leave point after it.
With positive prefix, apply to N lines including current one.
With negative prefix, apply to -N lines above."
  (interactive "p")
  (let ((range (list (line-beginning-position)
                     (goto-char (line-end-position n)))))
    (comment-or-uncomment-region
     (apply #'min range)
     (apply #'max range)))
  (forward-line 1)
  (back-to-indentation))


2015-01-27 0:53 GMT-02:00 Drew Adams <address@hidden>:
>>>    C-a C-SPC C-n M-;
>>>
>>> works for me when I need it.  M-x comment-line RET is much longer (even
>>> if you shorten it with something like partial-completion), so for such
>>> a command to make sense, you'd need a key binding for it.
>>
>> Yes, you would. I didn't mention keybinds yet, because I was tackling this 
>> by parts.
>>
>> I bind it to `C-;'. Another viable key might be `C-M-;'. But it definitely
>> needs a key.
>>
>> When you want to comment/uncomment something between 1 and 9 lines, it
>> always requires less keypresses than the M-; alternative.
>> Besides, commenting a single line is such a common scenario, I find it
>> deserves a single key instead of a 4 key combo.
>> It's also something that a lot of IDE's assign to a hotkey, so I'm sure
>> I'm not the only one that finds it useful. :-) And people who come from
>> other editors are sure to miss it.
>
> FWIW, I use a variant of `comment-region' (below).  I bind it to
> `C-x C-;'.  (The other, repeatable, keys you mention are too good to
> waste on a non-repeating command.)
>
> (defun comment-region-lines (beg end &optional arg)
>   "Like `comment-region' (which see), but comment/uncomment whole lines."
>   (interactive "*r\nP")
>   (if (> beg end) (let (mid) (setq mid beg beg end end mid)))
>   (let ((bol  (save-excursion (goto-char beg)
>                               (line-beginning-position)))
>         (eol  (save-excursion (goto-char end)
>                               (if (bolp) (point) (line-end-position)))))
>     (comment-region bol eol arg)))
>
> If the region is empty then it comments the current line.
>
> (But it does not distinguish active from inactive region.
> That would be another possible behavior choice.)
>
> I use this all the time.  I use `M-;' (`comment-dwim') only
> for an end-of-line comment (as in the good old days).  I find
> that `M-;' is quite poor for commenting and uncommenting the
> region (including nesting and unnesting comment levels etc.).
>
> `comment-region' is much better for that, IMO.  And as enhanced
> above, it comments whole lines, which is what I typically want.



reply via email to

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