bug#46621: Copy line

classic Classic list List threaded Threaded
33 messages Options
12
Reply | Threaded
Open this post in threaded view
|

bug#46621: Copy line

Juri Linkov-2
Why there is still no such fundamental command as duplicating the current
line?  This command slightly extends the existing copy-from-above-command:

diff --git a/lisp/misc.el b/lisp/misc.el
index 09f6011f98..b3b7d8355f 100644
--- a/lisp/misc.el
+++ b/lisp/misc.el
@@ -61,6 +61,16 @@ copy-from-above-command
  (+ n (point)))))))
     (insert string)))
 
+;;;###autoload
+(defun copy-line (&optional arg)
+  "Duplicate the current line ARG times."
+  (interactive "p")
+  (dotimes (_ arg)
+    (forward-line 1)
+    (insert "\n")
+    (forward-line -1)
+    (copy-from-above-command)))
+
 ;; Variation of `zap-to-char'.
 
 ;;;###autoload
Reply | Threaded
Open this post in threaded view
|

bug#46621: [External] : bug#46621: Copy line

Drew Adams
> Why there is still no such fundamental command as duplicating the
> current line?  This command slightly extends the existing
> copy-from-above-command:

 "still"?
 "fundamental command"?

In the 35+ years of Emacs, has anyone actually
requested/missed this?

We seem to be adding this & that simple command
more & more - perhaps just because it's available
in other editors?

(Next, we'll dedicate a default key for this...)

The right approach for this kind of thing, IMO, is
for users to create and use such a command.  Then
after a while they start asking for it by default,
and we maybe add it, if it's a real lack.

I've nothing against such a command.  But I've
also never felt the need for it.



Reply | Threaded
Open this post in threaded view
|

bug#46621: Copy line

Lars Ingebrigtsen
In reply to this post by Juri Linkov-2
Juri Linkov <[hidden email]> writes:

> Why there is still no such fundamental command as duplicating the current
> line?  This command slightly extends the existing copy-from-above-command:

Hm...  So this would basically just allow you to get X number of the
line?

Well, I don't object to adding it, but I'm not sure what the use case is
here?  The only time I can remember wanting something like that is when
I'm filling a buffer with text for test purposes, which is perhaps not a
compelling use case.

--
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no



Reply | Threaded
Open this post in threaded view
|

bug#46621: Copy line

Eli Zaretskii
> Cc: [hidden email], Juri Linkov <[hidden email]>
> Date: Fri, 19 Feb 2021 21:27:09 +0100
> From:  Daniel Martín via "Bug reports for GNU Emacs,
>  the Swiss army knife of text editors" <[hidden email]>
>
> > Well, I don't object to adding it, but I'm not sure what the use case is
> > here?  The only time I can remember wanting something like that is when
> > I'm filling a buffer with text for test purposes, which is perhaps not a
> > compelling use case.
>
> In programming modes, if you need to declare a bunch of variables, you
> may duplicate the current line several times and then proceed to rename
> them.

I usually use M-/ for that.  It makes typing something that you just
typed very easy: just a single character followed by M-/ usually does
the job.  This allows me to type just what is needed, without
redundant "renaming".



Reply | Threaded
Open this post in threaded view
|

bug#46621: [External] : bug#46621: Copy line

Richard Stallman
In reply to this post by Drew Adams
[[[ To any NSA and FBI agents reading my email: please consider    ]]]
[[[ whether defending the US Constitution against all enemies,     ]]]
[[[ foreign or domestic, requires you to follow Snowden's example. ]]]

  > We seem to be adding this & that simple command
  > more & more - perhaps just because it's available
  > in other editors?

I agree.  Just because we don't need any longer to struggle
to keep Emacs down to 8 megabytes is no reason to add commands
without real demand.


--
Dr Richard Stallman
Chief GNUisance of the GNU Project (https://gnu.org)
Founder, Free Software Foundation (https://fsf.org)
Internet Hall-of-Famer (https://internethalloffame.org)





Reply | Threaded
Open this post in threaded view
|

bug#46621: Copy line

Lars Ingebrigtsen
In reply to this post by Lars Ingebrigtsen
Daniel Martín <[hidden email]> writes:

> In programming modes, if you need to declare a bunch of variables, you
> may duplicate the current line several times and then proceed to rename
> them.  I see the proposed new command may be useful in that scenario.

Right; makes sense.

> This has been a hot topic in Q&A sites:
> https://stackoverflow.com/questions/88399/how-do-i-duplicate-a-whole-line-in-emacs

OK, so there seems to be some general demand for this command, so I
think we should go ahead and add it, unless somebody strongly objects.

> Also, why not name the command `duplicate-line'? I think it's a more
> descriptive name and more discoverable via apropos (along with
> `delete-duplicate-lines', for example).

That does seem like an even better name for the command.  What do you
think, Juri?

--
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no



Reply | Threaded
Open this post in threaded view
|

bug#46621: Copy line

Lars Ingebrigtsen
In reply to this post by Eli Zaretskii
Eli Zaretskii <[hidden email]> writes:

> I usually use M-/ for that.  It makes typing something that you just
> typed very easy: just a single character followed by M-/ usually does
> the job.  This allows me to type just what is needed, without
> redundant "renaming".

`dabbrev-expand'?  I just tried "emacs -Q" and then `M-/' and Emacs just
beeped at me:

---
user-error: No dynamic expansion for ‘buffer.

’ found
---

(Because "buffer." was the previous word in the *scratch* buffer.)

--
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no



Reply | Threaded
Open this post in threaded view
|

bug#46621: Copy line

Eli Zaretskii
> From: Lars Ingebrigtsen <[hidden email]>
> Cc: Daniel Martín <[hidden email]>,
>   [hidden email],
>   [hidden email]
> Date: Sat, 20 Feb 2021 14:05:44 +0100
>
> Eli Zaretskii <[hidden email]> writes:
>
> > I usually use M-/ for that.  It makes typing something that you just
> > typed very easy: just a single character followed by M-/ usually does
> > the job.  This allows me to type just what is needed, without
> > redundant "renaming".
>
> `dabbrev-expand'?  I just tried "emacs -Q" and then `M-/' and Emacs just
> beeped at me:

You need to type the beginning of what you want it to complete before
M-/.  Try

  b M-/
  f M-/
  f M-/ M-/ M-/

etc.



Reply | Threaded
Open this post in threaded view
|

bug#46621: Copy line

Eli Zaretskii
> From: Lars Ingebrigtsen <[hidden email]>
> Cc: [hidden email],  [hidden email],  [hidden email]
> Date: Sat, 20 Feb 2021 14:18:06 +0100
>
> >   b M-/
> >   f M-/
> >   f M-/ M-/ M-/
> >
> > etc.
>
> Ah, I see.  I've never used that command before.

It's a great means to type much less, and not just in programming
modes.  When writing email as well, for example.

> However, this just copies a single word, and not a line?

I mentioned M-/ because of this rationale:

> > In programming modes, if you need to declare a bunch of variables, you
> > may duplicate the current line several times and then proceed to rename
> > them.

> In programming modes, if you need to declare a bunch of variables, you
> may duplicate the current line several times and then proceed to rename
> them.

So assume you have this:

  ptrdiff_t foobar;

and you want to add this:

  ptrdiff_t bazquux;

Instead of copying the "foobar" line and then editing "foobar" into
"bazquux", you can do

  p M-/ bazquux;

IOW, instead of copying lines, it usually makes more sense in PL modes
to be able to easily repeat what has been typed recently, because
source lines are rarely exact copies of some other line.

So that use case I think is not the best one to justify this new
command.  Which is not to say I object to adding it, just that at
least some people may think they need it because they aren't aware of
some Emacs feature.  Because I _never_ in all the years of writing
code felt the need to have such a command.



Reply | Threaded
Open this post in threaded view
|

bug#46621: Copy line

Juri Linkov-2
In reply to this post by Lars Ingebrigtsen
>> In programming modes, if you need to declare a bunch of variables, you
>> may duplicate the current line several times and then proceed to rename
>> them.  I see the proposed new command may be useful in that scenario.
>
> Right; makes sense.
>
>> This has been a hot topic in Q&A sites:
>> https://stackoverflow.com/questions/88399/how-do-i-duplicate-a-whole-line-in-emacs
>
> OK, so there seems to be some general demand for this command, so I
> think we should go ahead and add it, unless somebody strongly objects.

Also tired of reading endless discussions on Q&A sites about reinventing
such fundamental feature as duplicating a line.

>> Also, why not name the command `duplicate-line'? I think it's a more
>> descriptive name and more discoverable via apropos (along with
>> `delete-duplicate-lines', for example).
>
> That does seem like an even better name for the command.  What do you
> think, Juri?

`duplicate-line' is a better name, but the problem is that is based on the
existing command `copy-from-above-command'.  Should they share
the same name prefix?  Please decide, I have no opinion on this. :-)



Reply | Threaded
Open this post in threaded view
|

bug#46621: Copy line

Richard Stallman
[[[ To any NSA and FBI agents reading my email: please consider    ]]]
[[[ whether defending the US Constitution against all enemies,     ]]]
[[[ foreign or domestic, requires you to follow Snowden's example. ]]]

  > >> In programming modes, if you need to declare a bunch of variables, you
  > >> may duplicate the current line several times and then proceed to rename
  > >> them.  I see the proposed new command may be useful in that scenario.

Once in a while I want to do that.  It is easy: C-a C-k C-k,
they C-y as many times as needed.

Instead of a command to duplicate the current line repeatedly,
how about a command to yank the current kill repeatedly?
That would be useful in a much broader range of situations.

I wonder if the current meaning of the numeric argument to C-y (reach
back in the kill ring) is actually useful.  Would it be better for
it to repeat the yank in this way?


--
Dr Richard Stallman
Chief GNUisance of the GNU Project (https://gnu.org)
Founder, Free Software Foundation (https://fsf.org)
Internet Hall-of-Famer (https://internethalloffame.org)





Reply | Threaded
Open this post in threaded view
|

bug#46621: Copy line

Juri Linkov-2
In reply to this post by Juri Linkov-2
>   > >> In programming modes, if you need to declare a bunch of variables, you
>   > >> may duplicate the current line several times and then proceed to rename
>   > >> them.  I see the proposed new command may be useful in that scenario.
>
> Once in a while I want to do that.  It is easy: C-a C-k C-k,
> they C-y as many times as needed.

Let's compare these two solutions.  Trying to copy 10 lines:

  C-a C-k C-k C-y C-y C-y C-y C-y C-y C-y C-y C-y C-y

and adds an unrequested entry to the kill ring
that the user needs to remove from the kill ring later.

When bound to a short key, it's just

  C-12 C-c c

and doesn't change the contents of the kill ring.

> Instead of a command to duplicate the current line repeatedly,
> how about a command to yank the current kill repeatedly?
> That would be useful in a much broader range of situations.
>
> I wonder if the current meaning of the numeric argument to C-y (reach
> back in the kill ring) is actually useful.  Would it be better for
> it to repeat the yank in this way?

It's not realistic to change the meaning of the numeric arg to C-y.
People already use the current meaning for decades.



Reply | Threaded
Open this post in threaded view
|

bug#46621: Copy line

Lars Ingebrigtsen
Eli Zaretskii <[hidden email]> writes:

> What is the use case for an Emacs user to need to copy a line 12
> times, and is it frequent enough to justify binding the command to a
> key, and to C-c key on top of that?

That users are asking about a command like this on stackexchange seems to
indicate that some users would find this useful.

--
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no



Reply | Threaded
Open this post in threaded view
|

bug#46621: Copy line

Lars Ingebrigtsen
In reply to this post by Juri Linkov-2
Juri Linkov <[hidden email]> writes:

> `duplicate-line' is a better name, but the problem is that is based on the
> existing command `copy-from-above-command'.  Should they share
> the same name prefix?  Please decide, I have no opinion on this. :-)

I think it's fine to go with `duplicate-line', even if it's conceptually
similar to `copy-from-above-command'.

--
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no



Reply | Threaded
Open this post in threaded view
|

bug#46621: Copy line

Eli Zaretskii
In reply to this post by Lars Ingebrigtsen
On February 21, 2021 3:12:19 PM GMT+02:00, Lars Ingebrigtsen <[hidden email]> wrote:
> Eli Zaretskii <[hidden email]> writes:
>
> > What is the use case for an Emacs user to need to copy a line 12
> > times, and is it frequent enough to justify binding the command to a
> > key, and to C-c key on top of that?
>
> That users are asking about a command like this on stackexchange seems
> to
> indicate that some users would find this useful.

They asked about making a dozen copies of a line?



Reply | Threaded
Open this post in threaded view
|

bug#46621: Copy line

Lars Ingebrigtsen
Eli Zaretskii <[hidden email]> writes:

> They asked about making a dozen copies of a line?

I don't think anybody asked for a dozen copies in particular.

I think we should add the command, but I'm not convinced it deserves a
default key binding -- we can add that later if it turns out that this
is the break-out most-popular new command in Emacs 28.

--
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no



Reply | Threaded
Open this post in threaded view
|

bug#46621: Copy line

Eli Zaretskii
> From: Lars Ingebrigtsen <[hidden email]>
> Cc: Juri Linkov <[hidden email]>, Richard Stallman <[hidden email]>,
>  [hidden email], [hidden email]
> Date: Sun, 21 Feb 2021 16:51:34 +0100
>
> Eli Zaretskii <[hidden email]> writes:
>
> > They asked about making a dozen copies of a line?
>
> I don't think anybody asked for a dozen copies in particular.

Then why did Juri decide to countermand Richard's arguments by a
strawman?

> I think we should add the command, but I'm not convinced it deserves a
> default key binding -- we can add that later if it turns out that this
> is the break-out most-popular new command in Emacs 28.

Richard's arguments still stand, and I'd prefer to seriously consider
rather than dismiss them.



Reply | Threaded
Open this post in threaded view
|

bug#46621: [External] : bug#46621: Copy line

Drew Adams
In reply to this post by Juri Linkov-2
> Also, let's not forget that Emacs has keyboard
> macros, and those can be run with a repeat count.

+1 for emphasizing this.

We should maybe emphasize it more, particularly
when someone used to another editor asks why
Emacs doesn't have this or that key/action.

Instead, we too often, I think, start thinking
about whether Emacs should indeed add such a
command (and key).

Newbies, in particular, could do well to learn
about using keyboard macros early.

(I haven't used the tutorial in ages.  Do we
introduce recording and playing back macros?)

Speaking of which, it might be good to speak
more of "recording" and "playing back" a
sequence of actions, and less of "macros" in
this context.

I think that many people are used to turning
on "recording" in various contexts - it's not
a big deal as a notion, whereas "macro" sounds
exotic and can sound complicated/intimidating.
Reply | Threaded
Open this post in threaded view
|

bug#46621: [External] : bug#46621: Copy line

Drew Adams
In reply to this post by Lars Ingebrigtsen
> That users are asking about a command like this
> on stackexchange seems to indicate that some
> users would find this useful.

Not necessarily.  Which users?  Sometimes newbies
to Emacs are looking for what they're used to in
some other editor, and not seeing that in Emacs
they think "missing feature".

And answers on emacs.SE often set newbies straight
in this regard, saying "this is how you use Emacs
to do what you want".

The first response by us shouldn't be a knee-jerk
reaction to add what someone says is missing.  We
should first check what people who use Emacs do,
i.e., see if what seems to be missing really
represents a problem to be fixed.



Reply | Threaded
Open this post in threaded view
|

bug#46621: Copy line

Juri Linkov-2
In reply to this post by Juri Linkov-2
>> > Once in a while I want to do that.  It is easy: C-a C-k C-k,
>> > they C-y as many times as needed.
>>
>> Let's compare these two solutions.  Trying to copy 10 lines:
>>
>>   C-a C-k C-k C-y C-y C-y C-y C-y C-y C-y C-y C-y C-y
>>
>> and adds an unrequested entry to the kill ring
>> that the user needs to remove from the kill ring later.
>>
>> When bound to a short key, it's just
>>
>>   C-12 C-c c
>>
>> and doesn't change the contents of the kill ring.
>
> What is the use case for an Emacs user to need to copy a line 12
> times, and is it frequent enough

Yes, it is frequently used, as these Q&A sites indicate.

> to justify binding the command to a key, and to C-c key on top
> of that?

ISTR, 'C-c letter' keys are reserved for users, so users can bind the
command to 'C-c c' or any other short key.  But I can't prove if such
convention of 'C-c letter' for user keys really exists, I can't find it
in the manual.

Or it could be bound to some short key sequence by default.
Even without a keybinding the key sequence is very short:

  M-10 M-x dup RET

> Also, let's not forget that Emacs has keyboard macros, and those can
> be run with a repeat count.

This is how long it takes with keyboard macros:

  C-a C-k C-k C-x ( C-y C-x ) C-10 C-x e



12