From 19448c5a17f6a44ad52a2858fd40acd4b4e5aeed Mon Sep 17 00:00:00 2001
From: David Verhasselt <david@crowdway.com>
Date: Sat, 2 May 2015 15:32:59 +0300
Subject: [PATCH] Add option to exit copy-mode when scrolling past bottom

---
 options-table.c |  5 +++++
 window-copy.c   | 10 +++++++++-
 2 files changed, 14 insertions(+), 1 deletion(-)

diff --git a/options-table.c b/options-table.c
index a4f3ac4..53b83a8 100644
--- a/options-table.c
+++ b/options-table.c
@@ -509,6 +509,11 @@ const struct options_table_entry window_options_table[] = {
 	  .default_num = 1
 	},
 
+	{ .name = "exit-copy-past-bottom",
+      .type = OPTIONS_TABLE_FLAG,
+      .default_num = 0
+  	},
+
 	{ .name = "force-height",
 	  .type = OPTIONS_TABLE_NUMBER,
 	  .minimum = 0,
diff --git a/window-copy.c b/window-copy.c
index 7d5bae4..51d1d49 100644
--- a/window-copy.c
+++ b/window-copy.c
@@ -1848,7 +1848,8 @@ window_copy_cursor_down(struct window_pane *wp, int scroll_only)
 {
 	struct window_copy_mode_data	*data = wp->modedata;
 	struct screen			*s = &data->screen;
-	u_int				 ox, oy, px, py;
+	u_int 					scrolled_past_bottom = 0;
+	u_int 	 					ox, oy, px, py;
 
 	oy = screen_hsize(data->backing) + data->cy - data->oy;
 	ox = window_copy_find_length(wp, oy);
@@ -1862,6 +1863,8 @@ window_copy_cursor_down(struct window_pane *wp, int scroll_only)
 
 	data->cx = data->lastcx;
 	if (scroll_only || data->cy == screen_size_y(s) - 1) {
+		scrolled_past_bottom = data->oy == 0;
+
 		window_copy_scroll_up(wp, 1);
 		if (scroll_only && data->cy > 0)
 			window_copy_redraw_lines(wp, data->cy - 1, 2);
@@ -1883,6 +1886,11 @@ window_copy_cursor_down(struct window_pane *wp, int scroll_only)
 		window_copy_cursor_end_of_line(wp);
 	else if (s->sel.lineflag == LINE_SEL_RIGHT_LEFT)
 		window_copy_cursor_start_of_line(wp);
+
+	if (scrolled_past_bottom && !s->sel.flag &&
+		s->sel.lineflag == LINE_SEL_NONE &&
+		options_get_number(&wp->window->options, "exit-copy-past-bottom"))
+		window_pane_reset_mode(wp);
 }
 
 void
-- 
2.3.2 (Apple Git-55)

