From a99759398841d86928c7ad4d8248f907765cbeb2 Mon Sep 17 00:00:00 2001
From: KatolaZ <katolaz@freaknet.org>
Date: Sat, 27 Jul 2019 08:31:24 +0100
Subject: add crop-to-visible function (C)

---
 TODO       |  2 +-
 config.mk  |  2 +-
 draw.c     |  1 +
 gramscii.1 |  5 +++++
 gramscii.h |  1 +
 screen.c   | 30 +++++++++++++++++++++++++++---
 6 files changed, 36 insertions(+), 5 deletions(-)

diff --git a/TODO b/TODO
index 59534ac..c04d8f3 100644
--- a/TODO
+++ b/TODO
@@ -1,5 +1,4 @@
 + optimize redraws (redraw only the modified rectangle)
-+ add crop command (C)
 - fix bug with 'g' commands in arrow mode
 - add screen geometry option (-g 25x80?)
 - read file at point
@@ -25,6 +24,7 @@
 - allow scrolling (both vertical and horizontal)
 - catch SIGWINCH and react appropriately (after scrolling is 
   enabled)
+* add crop command (C)
 * reorganise code
 * change screen management (i.e., dynamic array of lines)
 * add action multiplier (e.g., "7h" moves left by 7 cols)
diff --git a/config.mk b/config.mk
index c801aca..ab8d432 100644
--- a/config.mk
+++ b/config.mk
@@ -2,5 +2,5 @@ PREFIX = /usr/local
 BINDIR = ${PREFIX}/bin
 MANDIR = ${PREFIX}/share/man
 
-CFLAGS = -O3 -std=c90 -pedantic -Wall
+CFLAGS = -O0 -std=c99 -pedantic -Wall
 CC = cc
diff --git a/draw.c b/draw.c
index 9e20601..9724a1f 100644
--- a/draw.c
+++ b/draw.c
@@ -303,6 +303,7 @@ void visual_box(FILE *fc){
 				f = get_key(fc, "fill char: "); /** FALLTHROUGH **/
 			case 'x':/* erase */
 				erase_box(orig_x, orig_y, f);
+				erase_blank_lines(MIN(y,orig_y), MAX(y, orig_y));
 				modified = 1;
 				goto vis_exit;
 				break;
diff --git a/gramscii.1 b/gramscii.1
index 5e955ef..10a1943 100644
--- a/gramscii.1
+++ b/gramscii.1
@@ -52,6 +52,11 @@ mode:
 .BI R
 Redraw the screen
 .TP 5m
+.BI C
+Crop chart to the largest non-blank region. The first line and the first
+column of the cropped chart will contain the first non-blank line and
+the first non-blank column of the original chart, respectively.
+.TP 5m
 .BI q
 Quit gramscii, and prompt for a filename if the current screen contains
 unsaved changes.
diff --git a/gramscii.h b/gramscii.h
index 6666515..b329d89 100644
--- a/gramscii.h
+++ b/gramscii.h
@@ -120,6 +120,7 @@ void erase_line(int i);
 void erase_screen();
 void go_to(int where);
 void crop_to_nonblank();
+void erase_blank_lines(int y1, int y2);
 
 /** drawing-related functions **/
 int change_style(char c);
diff --git a/screen.c b/screen.c
index 0c5f2d8..965d440 100644
--- a/screen.c
+++ b/screen.c
@@ -197,6 +197,26 @@ void update_current(){
 	fflush(stdout);
 }
 
+void erase_blank_lines(int y1, int y2){
+	int j;
+	if (y1 > y2){
+		y1 ^= y2;
+		y2 ^= y1;
+		y1 ^= y2;
+	}
+	
+	for (; y1 <= y2; y1++){
+		j = screen[y1].lst; 
+		while (j>=0 && isblank(screen[y1].s[j]))
+			j--;
+		if (j<0){
+			screen[y1].lst = -1;
+			screen[y1].s[0] = '\0';
+		}
+	}
+}
+
+
 void erase_line(int i){
 	screen[i].lst = -1;
 	screen[i].s[0] = '\0';
@@ -446,13 +466,16 @@ void find_nonblank_rect(int *x1, int *y1, int *x2, int *y2){
 		*y2 = i;
 		if (i < *y1)
 			*y1 = i;
-		if (screen[i].lst > *x2)
-			*x2 = screen[i].lst;
 		j = 0;
-		while(j <= screen[i].lst && isblank(first=screen[i].s[j]))
+		while((j <= screen[i].lst)  && isblank(first=screen[i].s[j]))
 			j++;
 		if (j < *x1)
 			*x1 = j;
+		j = screen[i].lst;
+		while(isblank(screen[i].s[j]))
+			j--;
+		if (j > *x2)
+			*x2 = j;
 	}
 }
 
@@ -478,6 +501,7 @@ void crop_to_nonblank(){
 	fprintf(stderr, "crop rectangle: (%d, %d)-(%d, %d)\n", x1, y1, x2, y2);
 #endif
 	crop_to_rect(x1, y1, x2, y2);
+	modified=1;
 	redraw();
 }
 
-- 
cgit v1.2.3