URI: 
       add undo in erase mode - gramscii - A simple editor for ASCII box-and-arrow charts
   DIR Log
   DIR Files
   DIR Refs
   DIR Tags
   DIR README
   DIR LICENSE
       ---
   DIR commit 08485aacd8d561f2650175b7a16211262396b2ce
   DIR parent f660595c236a21555d3558dd51afae4a67d651a3
  HTML Author: KatolaZ <katolaz@freaknet.org>
       Date:   Thu,  1 Aug 2019 11:26:12 +0100
       
       add undo in erase mode
       
       Diffstat:
         M TODO                                |      10 +++++-----
         M draw.c                              |      16 ++++++++++++++--
         M gramscii.1                          |      25 ++++++++++++++++++++-----
         M screen.c                            |       2 +-
       
       4 files changed, 40 insertions(+), 13 deletions(-)
       ---
   DIR diff --git a/TODO b/TODO
       @@ -1,9 +1,4 @@
        + optimize redraws (redraw only the modified rectangle)
       -+ undo (by storing lines changed across insert/remove operations)
       -  * re-organise undo-ring management
       -  * add undo for arrow mode
       -  * add undo for text mode
       -  - add undo for erase mode
        - fix bug with 'g' commands in arrow mode
        - add screen geometry option (-g 25x80?)
        - read file at point
       @@ -23,6 +18,11 @@
        - allow scrolling (both vertical and horizontal)
        - catch SIGWINCH and react appropriately (after scrolling is 
          enabled)
       +* undo (by storing lines changed across insert/remove operations)
       +  * re-organise undo-ring management
       +  * add undo for arrow mode
       +  * add undo for text mode
       +  * add undo for erase mode
        * visual selection
          * crop-to-rectangle
          * yank
   DIR diff --git a/draw.c b/draw.c
       @@ -168,7 +168,6 @@ update_box:
        
        void draw_arrow(int x, int y, char *a, int a_len, int fix){
        
       -        /* FIXME: copy affected lines to undo */
                int i, j, cur_dir;
                char line;
                void (*f)(int, int, char);
       @@ -285,13 +284,24 @@ void erase(FILE *fc){
                /*FIXME: add affected lines to undo */
                char c;
                int orig_x = x, orig_y = y;
       +        char first = 1, opened = 0;
                status_bar();
                show_cursor();
       -        invalidate_undo();
                while((c=fgetc(fc))!=EOF && c!=27 && c!= 'x' && c != '\n'){
                        if (!move_around(c, fc)) continue;
                        check_bound();
       +                if (first || 
       +                    (y != orig_y && ! opened) ||
       +                    (y == orig_y && x != orig_x && !opened) ){
       +                        copy_lines_to_ring(MIN(y, orig_y), MAX(y, orig_y), PRV_STATE);
       +                        first = 0;
       +                        opened = 1;
       +                }
                        do_erase(orig_x, orig_y);
       +                if (y != orig_y && opened){
       +                        copy_lines_to_ring(MIN(y, orig_y), MAX(y, orig_y), NEW_STATE);
       +                        opened = 0;
       +                }
                        step = 1;
                        modified = 1;
                        orig_x = x;
       @@ -300,6 +310,8 @@ void erase(FILE *fc){
                        status_bar();
                        show_cursor();
                }
       +        if (opened)
       +                copy_lines_to_ring(y, y, NEW_STATE);
                mode = MOVE;
        }
        
   DIR diff --git a/gramscii.1 b/gramscii.1
       @@ -26,6 +26,26 @@ available for further processing.
        .TP
        .BI -h
        Print short usage unstructions and exit.
       +.PP
       +If one or more files are provided after the last option, gramscii will
       +consider them command files and will read them one after the other
       +(i.e., as if the characters in the file were typed while gramscii was
       +running), before accepting commands from stdin. This allows to use
       +gramscii scripts. For instance, if you start gramscii as:
       +.EX
       +
       +    gramscii file.txt
       +
       +.EE
       +and  "file.txt" contains the lines:
       +.EX
       +
       +    gg10lbLLJJb
       +    gg10l15jbLLJJ
       +
       +.EE
       +then gramscii will show two boxes and then will start accepting
       +commands as usual. 
        .SH COMMANDS
        gramscii is a visual modal editor. Commands are associated to
        keystrokes, and keystrokes have different meaning in different modes.
       @@ -537,11 +557,6 @@ would automatically save the screen into "filename".
        gramscii currently manages only a fixed screen of the same size of the
        screen where it starts from. This will be changed in a future release to
        support scrolling and "virtual" screens of any (reasonable) size.
       -.PP
       -Undo commands are only available in box, visual (cut, fill), arrow, and
       -text mode, and for copy/paste operations. gramscii currently does
       -.B not
       -support undo commands for erase mode. This will be fixed soon.
        .SH AUTHORS
        gramscii is written and maintained by Vincenzo "KatolaZ" Nicosia
        <katolaz@freaknet.org>. You can use, copy, modify, and redistribute
   DIR diff --git a/screen.c b/screen.c
       @@ -445,7 +445,7 @@ void crop_to_rect(int x1, int y1, int x2, int y2){
                int i;
        
                for (i=0; i<= y2-y1; i ++){
       -                ensure_line_length(&(screen.l[i]), screen.l[i+y1].lst);
       +                ensure_line_length(&(screen.l[i]), screen.l[i+y1].lst+1);
                        sprintf(screen.l[i].s, "%s", screen.l[i+y1].s + x1);
                        screen.l[i].lst = screen.l[i+y1].lst - x1;
                }