unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* [PATCH] newcomment.el (comment-line): New command.
@ 2015-01-26 19:17 Artur Malabarba
  2015-01-26 22:41 ` Stefan Monnier
  0 siblings, 1 reply; 9+ messages in thread
From: Artur Malabarba @ 2015-01-26 19:17 UTC (permalink / raw)
  To: emacs-devel; +Cc: Stefan Monnier

[-- Attachment #1: Type: text/plain, Size: 1276 bytes --]

Is there any particular reason why there's no `comment-line' command in Emacs?
If not, I'd like to propose the following patch to newcomment.el


---
 lisp/ChangeLog     |  4 ++++
 lisp/newcomment.el | 12 ++++++++++++
 2 files changed, 16 insertions(+)

diff --git a/lisp/ChangeLog b/lisp/ChangeLog
index ff352a2..9e6d18f 100644
--- a/lisp/ChangeLog
+++ b/lisp/ChangeLog
@@ -1,3 +1,7 @@
+2015-01-26  Artur Malabarba  <bruce.connor.am@gmail.com>
+
+    * newcomment.el (comment-line): New command.
+
 2015-01-26  Stefan Monnier  <monnier@iro.umontreal.ca>


diff --git a/lisp/newcomment.el b/lisp/newcomment.el
index e307eac..8f6ac5a 100644
--- a/lisp/newcomment.el
+++ b/lisp/newcomment.el
@@ -1451,6 +1451,18 @@ unless optional argument SOFT is non-nil."
             (end-of-line 0)
             (insert comend))))))))))))

+;;;###autoload
+(defun 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")
+  (comment-or-uncomment-region
+   (line-beginning-position)
+   (goto-char (line-end-position n)))
+  (forward-line 1)
+  (back-to-indentation))
+
 (provide 'newcomment)

 ;;; newcomment.el ends here
-- 
2.2.2

[-- Attachment #2: 0001-newcomment.el-comment-line-New-command.patch --]
[-- Type: text/x-patch, Size: 1340 bytes --]

From 5e30c5c5b4120123a98021f49062ec5e39c083dc Mon Sep 17 00:00:00 2001
From: Artur Malabarba <bruce.connor.am@gmail.com>
Date: Mon, 26 Jan 2015 17:11:24 -0200
Subject: [PATCH] newcomment.el (comment-line): New command.

---
 lisp/ChangeLog     |  4 ++++
 lisp/newcomment.el | 12 ++++++++++++
 2 files changed, 16 insertions(+)

diff --git a/lisp/ChangeLog b/lisp/ChangeLog
index ff352a2..9e6d18f 100644
--- a/lisp/ChangeLog
+++ b/lisp/ChangeLog
@@ -1,3 +1,7 @@
+2015-01-26  Artur Malabarba  <bruce.connor.am@gmail.com>
+
+	* newcomment.el (comment-line): New command.
+
 2015-01-26  Stefan Monnier  <monnier@iro.umontreal.ca>
 
 
diff --git a/lisp/newcomment.el b/lisp/newcomment.el
index e307eac..8f6ac5a 100644
--- a/lisp/newcomment.el
+++ b/lisp/newcomment.el
@@ -1451,6 +1451,18 @@ unless optional argument SOFT is non-nil."
 		    (end-of-line 0)
 		    (insert comend))))))))))))
 
+;;;###autoload
+(defun 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")
+  (comment-or-uncomment-region
+   (line-beginning-position)
+   (goto-char (line-end-position n)))
+  (forward-line 1)
+  (back-to-indentation))
+
 (provide 'newcomment)
 
 ;;; newcomment.el ends here
-- 
2.2.2


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: [PATCH] newcomment.el (comment-line): New command.
  2015-01-26 19:17 [PATCH] newcomment.el (comment-line): New command Artur Malabarba
@ 2015-01-26 22:41 ` Stefan Monnier
  2015-01-27  2:35   ` Artur Malabarba
  0 siblings, 1 reply; 9+ messages in thread
From: Stefan Monnier @ 2015-01-26 22:41 UTC (permalink / raw)
  To: Artur Malabarba; +Cc: emacs-devel

> Is there any particular reason why there's no `comment-line' command
> in Emacs?

Probably because the question goes both ways: is there any particular
reason we'd need a comment-line command?

   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.


        Stefan



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] newcomment.el (comment-line): New command.
  2015-01-26 22:41 ` Stefan Monnier
@ 2015-01-27  2:35   ` Artur Malabarba
  2015-01-27  2:53     ` Drew Adams
  0 siblings, 1 reply; 9+ messages in thread
From: Artur Malabarba @ 2015-01-27  2:35 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: emacs-devel

[-- Attachment #1: Type: text/plain, Size: 1117 bytes --]

On 26 Jan 2015 20:41, "Stefan Monnier" <monnier@iro.umontreal.ca> wrote:
>
> > Is there any particular reason why there's no `comment-line' command
> > in Emacs?
>
> Probably because the question goes both ways: is there any particular
> reason we'd need a comment-line command?
>
>    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.

[-- Attachment #2: Type: text/html, Size: 1416 bytes --]

^ permalink raw reply	[flat|nested] 9+ messages in thread

* RE: [PATCH] newcomment.el (comment-line): New command.
  2015-01-27  2:35   ` Artur Malabarba
@ 2015-01-27  2:53     ` Drew Adams
  2015-02-01 19:48       ` Artur Malabarba
  0 siblings, 1 reply; 9+ messages in thread
From: Drew Adams @ 2015-01-27  2:53 UTC (permalink / raw)
  To: bruce.connor.am, Stefan Monnier; +Cc: emacs-devel

>>    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.



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] newcomment.el (comment-line): New command.
  2015-01-27  2:53     ` Drew Adams
@ 2015-02-01 19:48       ` Artur Malabarba
  2015-02-01 21:40         ` Drew Adams
                           ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: Artur Malabarba @ 2015-02-01 19:48 UTC (permalink / raw)
  To: Drew Adams; +Cc: Stefan Monnier, emacs-devel

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 <drew.adams@oracle.com>:
>>>    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.



^ permalink raw reply	[flat|nested] 9+ messages in thread

* RE: [PATCH] newcomment.el (comment-line): New command.
  2015-02-01 19:48       ` Artur Malabarba
@ 2015-02-01 21:40         ` Drew Adams
  2015-02-02 12:02           ` Artur Malabarba
  2015-02-02  7:01         ` Thierry Volpiatto
  2015-02-02 17:17         ` Stefan Monnier
  2 siblings, 1 reply; 9+ messages in thread
From: Drew Adams @ 2015-02-01 21:40 UTC (permalink / raw)
  To: bruce.connor.am; +Cc: Stefan Monnier, emacs-devel

> 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).

My bad; I didn't notice that it was repeatable.

Yes, that's useful, and yes, it makes sense to use a repeatable key.
Maybe `C-;'.  (Fits with the repeatable versions of `C-a' and `C-e'
that I use. ;-))

Consider letting a negative prefix arg be carried over to
repetitions, so you can do C-- C-; C-; C-; ... to do it
backwards.  Currently you need to do C-- C-; C-- C-; C-- C-; ...

E.g., something like this:

(defun comment-line (n)
  "Comment or uncomment current line and leave point after it.
With positive prefix arg, apply to N lines including current one.
With negative prefix arg, apply to -N lines above.
When repeated, a negative prefix arg switches direction."
  (interactive "p")
  (when (eq last-command 'comment-line-backward) (setq n (- n)))
  (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)
  (unless (natnump n) (setq this-command 'comment-line-backward)))



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] newcomment.el (comment-line): New command.
  2015-02-01 19:48       ` Artur Malabarba
  2015-02-01 21:40         ` Drew Adams
@ 2015-02-02  7:01         ` Thierry Volpiatto
  2015-02-02 17:17         ` Stefan Monnier
  2 siblings, 0 replies; 9+ messages in thread
From: Thierry Volpiatto @ 2015-02-02  7:01 UTC (permalink / raw)
  To: emacs-devel

Artur Malabarba <bruce.connor.am@gmail.com> writes:

> I'd like something repeatable, like C-; or C-M-; but I'm also
> receptive of Drew's suggestion of C-x C-;

In helm we have a macro that allow binding a key like this:

(helm-define-key-with-subkeys global-map (kbd "C-c M-;") ?\; #'comment-line)

This allow hitting `C-c M-;' on your first line and then hitting only
`;' to call the same command.

-- 
Thierry
Get my Gnupg key:
gpg --keyserver pgp.mit.edu --recv-keys 59F29997 




^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] newcomment.el (comment-line): New command.
  2015-02-01 21:40         ` Drew Adams
@ 2015-02-02 12:02           ` Artur Malabarba
  0 siblings, 0 replies; 9+ messages in thread
From: Artur Malabarba @ 2015-02-02 12:02 UTC (permalink / raw)
  To: Drew Adams; +Cc: emacs-devel

2015-02-01 21:40 GMT+00:00 Drew Adams <drew.adams@oracle.com>:
> Consider letting a negative prefix arg be carried over to
> repetitions, so you can do C-- C-; C-; C-; ... to do it
> backwards.  Currently you need to do C-- C-; C-- C-; C-- C-; ...

Good idea. Will do.



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] newcomment.el (comment-line): New command.
  2015-02-01 19:48       ` Artur Malabarba
  2015-02-01 21:40         ` Drew Adams
  2015-02-02  7:01         ` Thierry Volpiatto
@ 2015-02-02 17:17         ` Stefan Monnier
  2 siblings, 0 replies; 9+ messages in thread
From: Stefan Monnier @ 2015-02-02 17:17 UTC (permalink / raw)
  To: Artur Malabarba; +Cc: Drew Adams, emacs-devel

> I would like to politely insist this gets included into newcomment.el

I'm not opposed to it.

> (with a keybind).

I'm not very happy with using one of the very few remaining
C-<something> keys for this command.  I'm not terminally opposed to it,
but I'm not convinced it's a good idea.  C-M-; sounds like a sexp-level
operation, so I think it's less appropriate for a line-based command.
C-x C-; would be fine, OTOH.


        Stefan



^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2015-02-02 17:17 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-01-26 19:17 [PATCH] newcomment.el (comment-line): New command Artur Malabarba
2015-01-26 22:41 ` Stefan Monnier
2015-01-27  2:35   ` Artur Malabarba
2015-01-27  2:53     ` Drew Adams
2015-02-01 19:48       ` Artur Malabarba
2015-02-01 21:40         ` Drew Adams
2015-02-02 12:02           ` Artur Malabarba
2015-02-02  7:01         ` Thierry Volpiatto
2015-02-02 17:17         ` Stefan Monnier

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).