Subject: bug#18761: closed (Re: [PATCH] Add pasting in terminal check.)

From: [email-masked] (GNU bug Tracking System)
Date: Tue, 20 Jan 2015 13:12:02 +0000

Your bug report

#18761: [PATCH] Add pasting in terminal check.

which was filed against the emacs package, has been closed.

The explanation is attached below, along with your original report.
If you require more details, please reply to [email-masked].

-- 
18761: http://debbugs.gnu.org/cgi/bugreport.cgi?bug=18761
GNU Bug Tracking System
Contact [email-masked] with problems

Subject: Re: [PATCH] Add pasting in terminal check.
From: Michal Nazarewicz <[email-masked]>
To: [email-masked]
Date: Tue, 20 Jan 2015 14:11:42 +0100
List-Id: <debbugs-submit.debbugs.gnu.org>

It looks like ‘bracketed paste mode’ largely fixes this.  My
rxvt-unicode seems to still split a long paste into several shorter
ones, but not as short as 20-characters, and in xterm things seem to
work flawlessly.

-- 
Best regards,                                         _     _
.o. | Liege of Serenely Enlightened Majesty of      o' \,=./ `o
..o | Computer Science,  Michał “mina86” Nazarewicz    (o o)
ooo +--<[email-masked]>--<xmpp:[email-masked]>--ooO--(_)--Ooo--

Subject: [PATCH] Add pasting in terminal check.
From: Michal Nazarewicz <[email-masked]>
To: [email-masked]
Date: Sat, 18 Oct 2014 14:06:23 +0200
List-Id: <debbugs-submit.debbugs.gnu.org>

When user types text, Emacs inserts undo boundaries every 20
characters.  This means that when user pastes (eg. via middle click)
a lot of text into Emacs running in a terminal, the text is divided
into 20-character long chunks each being a separate undo entry.

Detect such situation by assuming that user cannot type 20 characters
in less than 100 milliseconds.  If text is input too fast, assume it
is pasted and do not insert boundary thus treating the whole inserted
text as a single undo entry.
---
 etc/NEWS   |  6 ++++++
 src/cmds.c | 18 +++++++++++++++++-
 2 files changed, 23 insertions(+), 1 deletion(-)

 Admittedly, I don't really use this feature myself but it has been
 contemplated by a colleague.  This is actually a re-posting of almost
 two year old patch[1].

 [1] http://lists.gnu.org/archive/html/emacs-devel/2013-01/msg00389.html

diff --git a/etc/NEWS b/etc/NEWS
index 7113786..5ec644e 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -120,6 +120,12 @@ for Unicode 8.0.  This includes full support for directional isolates
 and the Bidirectional Parentheses Algorithm (BPA) specified by these
 Unicode standards.
 
+** Pasting in terminal detection added.
+In tty mode, if a lot of text is typed into a buffer in a short time, Emacs
+will now assume it has been pasted (eg. by using middle-click on a terminal
+emulator) and treats the whole text as a single undo event.  Previously it
+would split the text in 20-character long chunks and undo each individually.
+
 
 * Changes in Specialized Modes and Packages in Emacs 25.1
 
diff --git a/src/cmds.c b/src/cmds.c
index 9a05218..93a861d 100644
--- a/src/cmds.c
+++ b/src/cmds.c
@@ -257,6 +257,21 @@ because it respects values of `delete-active-region' and `overwrite-mode'.  */)
   return Qnil;
 }
 
+static bool
+is_human_typing(void)
+{
+  static struct timespec undo_timer;
+
+  struct timespec t;
+
+  t = undo_timer;
+  undo_timer = current_timespec();
+  t = timespec_sub(undo_timer, t);
+
+  /* Assume it's human if typing 20 chars took at least 100 ms. */
+  return t.tv_sec >= 1 || t.tv_nsec >= 100000000;
+}
+
 static int nonundocount;
 
 /* Note that there's code in command_loop_1 which typically avoids
@@ -286,7 +301,8 @@ At the end, it runs `post-self-insert-hook'.  */)
     {
       if (nonundocount <= 0 || nonundocount >= 20)
 	{
-	  remove_boundary = 0;
+	  if (is_human_typing())
+	    remove_boundary = 0;
 	  nonundocount = 0;
 	}
       nonundocount++;
-- 
Best regards,                                         _     _
.o. | Liege of Serenely Enlightened Majesty of      o' \,=./ `o
..o | Computer Science,  Michał “mina86” Nazarewicz    (o o)
ooo +--<[email-masked]>--<xmpp:[email-masked]>--ooO--(_)--Ooo--