emacs-devel
[Top][All Lists]
Advanced

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

[PATCH] Add pasting-in-terminal check.


From: Michal Nazarewicz
Subject: [PATCH] Add pasting-in-terminal check.
Date: Thu, 17 Jan 2013 12:44:09 +0100

From: Michal Nazarewicz <address@hidden>

When typing text, Emacs inserts undo boundaries every 20 characters.
This means that when user runs Emacs inside of a terminal and pastes
a lot of text for instance via middle click the text is divided into
20-character long chunks each being a separate undo entry.

This commit tries to detect such situations, by assuming that user
cannot type 20 characters in less than 100 milliseconds.  If pasting
is detected, the boundary is not added and the whole text is inserted
as a single undo entry.
---
 etc/NEWS      |    5 +++++
 src/ChangeLog |    7 +++++++
 src/cmds.c    |   18 +++++++++++++++++-
 3 files changed, 29 insertions(+), 1 deletions(-)

diff --git a/etc/NEWS b/etc/NEWS
index 8400cd0..46b558e 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -65,6 +65,11 @@ POSIX ACL interfaces.
 ** New commands `toggle-frame-fullscreen' and `toggle-frame-maximized',
 bound to <f11> and M-<f10>, respectively.
 
+** Pasting in terminal detection added.
+If a lot of text is typed into a buffer in a short time, Emacs assumes
+it has been pasted on a terminal (eg. via middle-click on a terminal
+emulator) and treats the whole text as a single undo event.
+
 
 * Changes in Specialized Modes and Packages in Emacs 24.4
 
diff --git a/src/ChangeLog b/src/ChangeLog
index ee75f99..c625be7 100644
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,3 +1,10 @@
+2013-01-17  Michal Nazarewicz  <address@hidden>
+
+       Detect pasting on a terminal by assuming human cannot type 20
+       characters in 100 milliseconds.
+       * cmds.c (Fself_insert_command): Added is_human_typing check.
+       (is_human_typing): New function.
+
 2013-01-17  Dmitry Antipov  <address@hidden>
 
        * lisp.h (toplevel): Add comment about using Lisp_Save_Value
diff --git a/src/cmds.c b/src/cmds.c
index 3ebad50..286beb5 100644
--- a/src/cmds.c
+++ b/src/cmds.c
@@ -265,6 +265,21 @@ The command `delete-forward-char' is preferable for 
interactive use.  */)
   return Qnil;
 }
 
+static bool
+is_human_typing(void)
+{
+  static EMACS_TIME undo_timer;
+
+  EMACS_TIME t;
+
+  t = undo_timer;
+  undo_timer = current_emacs_time();
+  t = sub_emacs_time(undo_timer, t);
+
+  /* Assume it's human if typing 20 chars took at least 100 ms. */
+  return EMACS_SECS(t) >= 1 || EMACS_NSECS(t) >= 100000000;
+}
+
 static int nonundocount;
 
 /* Note that there's code in command_loop_1 which typically avoids
@@ -290,7 +305,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++;



reply via email to

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