From 82fc5ad2fd92d97b806b4509572f7d9716cec927 Mon Sep 17 00:00:00 2001
From: Marcin Kulik <marcin.kulik@gmail.com>
Date: Sun, 14 Oct 2012 22:52:15 +0200
Subject: [PATCH 1/1] Ignore prefix key inside pasted text

It prevents interpretation of a character that is set as a prefix key
when pasting text from outside of tmux (mouse middle-click,
ctrl-shift-v, etc...).

It allows setting any printable character as a prefix and still being
able to paste text including this character without a risk of messing up
tmux window/session.

For example now you can use backtick character (`) as a prefix and you
can safely paste bash code that uses backticks.
---
 server-client.c | 11 +++++++++++
 tmux.h          |  1 +
 2 files changed, 12 insertions(+)

diff --git a/server-client.c b/server-client.c
index 435cd06..5b00a84 100644
--- a/server-client.c
+++ b/server-client.c
@@ -353,6 +353,10 @@ server_client_handle_key(struct client *c, int key)
 	/* Update the activity timer. */
 	if (gettimeofday(&c->activity_time, NULL) != 0)
 		fatal("gettimeofday failed");
+
+	/* Save previous activity time for detection of prefix in pasted text. */
+	memcpy(&s->prev_activity_time, &s->activity_time, sizeof s->activity_time);
+
 	memcpy(&s->activity_time, &c->activity_time, sizeof s->activity_time);
 
 	w = c->session->curw->window;
@@ -396,6 +400,13 @@ server_client_handle_key(struct client *c, int key)
 	else
 		isprefix = 0;
 
+	/* Treat prefix as a regular key when pasting is detected. */
+	if (isprefix) {
+		timersub(&s->activity_time, &s->prev_activity_time, &tv);
+		if (tv.tv_sec == 0 && tv.tv_usec < 1000)
+			isprefix = 0;
+	}
+
 	/* No previous prefix key. */
 	if (!(c->flags & CLIENT_PREFIX)) {
 		if (isprefix)
diff --git a/tmux.h b/tmux.h
index 3751b67..00d0bf2 100644
--- a/tmux.h
+++ b/tmux.h
@@ -1090,6 +1090,7 @@ struct session {
 
 	struct timeval	 creation_time;
 	struct timeval	 activity_time;
+	struct timeval	 prev_activity_time;
 
 	u_int		 sx;
 	u_int		 sy;
-- 
1.7.11.3

