bug-gnu-emacs
[Top][All Lists]
Advanced

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

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


From: Michal Nazarewicz
Subject: bug#18761: [PATCH] Add pasting in terminal check.
Date: Sat, 18 Oct 2014 14:06:23 +0200
User-agent: Notmuch/0.17+15~gb65ca8e (http://notmuchmail.org) Emacs/25.0.50.1 (x86_64-unknown-linux-gnu)

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 +--<mpn@google.com>--<xmpp:mina86@jabber.org>--ooO--(_)--Ooo--





reply via email to

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