add crop-to-visible function (C) - gramscii - A simple editor for ASCII box-and-arrow charts
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) Tags
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit a99759398841d86928c7ad4d8248f907765cbeb2
 (DIR) parent b38ed132a7df231fc08ce384d8559e6648fdd0cc
 (HTM) Author: KatolaZ <katolaz@freaknet.org>
       Date:   Sat, 27 Jul 2019 08:31:24 +0100
       
       add crop-to-visible function (C)
       
       Diffstat:
         M TODO                                |       2 +-
         M config.mk                           |       2 +-
         M draw.c                              |       1 +
         M gramscii.1                          |       5 +++++
         M gramscii.h                          |       1 +
         M screen.c                            |      30 +++++++++++++++++++++++++++---
       
       6 files changed, 36 insertions(+), 5 deletions(-)
       ---
 (DIR) diff --git 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)
 (DIR) diff --git 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
 (DIR) diff --git 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;
 (DIR) diff --git 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.
 (DIR) diff --git 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);
 (DIR) diff --git 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();
        }