Browse code

make printout data change as the original data changes (that is, printouts are now a view to another part of the file)

Dario Rodriguez authored on 02/09/2021 20:36:13
Showing 1 changed files
... ...
@@ -66,6 +66,7 @@ typedef struct print_t {
66 66
         redata_t *data;
67 67
         int originline;
68 68
         int origincol;
69
+        int showonlyn;
69 70
         int dirty;
70 71
 } printout_t;
71 72
 
... ...
@@ -259,8 +260,15 @@ fprintf(stderr,"REDRAW Header (command)\n");
259 260
                                 }
260 261
                         }
261 262
                 }
262
-                if(re->contentsdirty)
263
+                if(re->contentsdirty) {
264
+                        /* set associated printouts as dirty too */
265
+                        for(i=0;i<re->sizeprints;i++) {
266
+                                if(re->prints[i].ui!=NULL && re->prints[i].data==NULL)
267
+                                        re->prints[i].dirty=1;
268
+                        }
269
+                        /* redraw contents */
263 270
                         re_drawcontents(re,NULL);
271
+                }
264 272
                 if(re->ui->rendererdirty) {
265 273
 #if 0
266 274
 fprintf(stderr,"RENDER\n");
... ...
@@ -661,12 +669,34 @@ re_textinsert(re_t *re, char *text, int sizetext)
661 669
         }
662 670
         if(redata_op_add(re->data,cursorpos,text,sizetext,NULL)!=0)
663 671
                 return(-1); /* couldn't add requested text */
672
+        /* fix selection */
664 673
         if(fixsel) {
665 674
                 if(cursorpos<=selstart)
666 675
                         redata_pos2linecol(re->data,selstart+sizetext,&(re->sellinefrom),&(re->selcolfrom));
667 676
                 if(cursorpos<=selend)
668 677
                         redata_pos2linecol(re->data,selend+sizetext,&(re->sellineto),&(re->selcolto));
669 678
         }
679
+        /* fix printouts scope */
680
+        if(re->usedprints>0) {
681
+                int i;
682
+                int numnl;
683
+                printout_t *printout;
684
+                for(numnl=0,ptr=strchr(text,'\n');ptr!=NULL;ptr=strchr(ptr+1,'\n'))
685
+                        numnl++;
686
+                for(i=0;i<re->usedprints;i++) {
687
+                        printout=re->prints+i;
688
+                        if(printout->data!=NULL)
689
+                                continue; /* has its own copy of the data */
690
+                        if(printout->originline>re->curline) {
691
+                                printout->originline+=numnl;
692
+                        } else if(printout->showonlyn>0
693
+                            && re->curline>=printout->originline
694
+                            && re->curline<(printout->originline+printout->showonlyn)) {
695
+                                printout->showonlyn+=numnl;
696
+                        }
697
+                }
698
+        }
699
+        /* fix cursor pos */
670 700
         for(ptr=text,next=memchr(text,'\n',sizetext);ptr!=NULL;ptr=(next!=NULL)?next+1:NULL,next=(ptr!=NULL)?memchr(ptr,'\n',sizetext-(ptr-text)):NULL) {
671 701
                 len=(next!=NULL)?(next-ptr):sizetext-(ptr-text);
672 702
                 re->curcol+=redata_generic_utf8len(ptr,len);
... ...
@@ -907,6 +937,23 @@ fprintf(stderr,"SDL_KEYDOWN: BACKSPACE%s\n",(event==&fakeevent)?" (fake)":"");
907 937
                         dellen=cursorpos-delpos;
908 938
                         redata_op_del(re->data,delpos,dellen,NULL);
909 939
                         cursorpos=delpos;
940
+                        /* fix printouts scope */
941
+                        if(re->usedprints>0) {
942
+                                int i;
943
+                                printout_t *printout;
944
+                                for(i=0;i<re->usedprints;i++) {
945
+                                        printout=re->prints+i;
946
+                                        if(printout->data!=NULL)
947
+                                                continue; /* has its own copy of the data */
948
+                                        if(printout->originline>re->curline) {
949
+                                                printout->originline--;
950
+                                        } else if(printout->showonlyn>1
951
+                                            && re->curline>=printout->originline
952
+                                            && re->curline<(printout->originline+printout->showonlyn)) {
953
+                                                printout->showonlyn--;
954
+                                        }
955
+                                }
956
+                        }
910 957
                 }
911 958
                 redata_pos2linecol(re->data,cursorpos,&(re->curline),&(re->curcol));
912 959
                 if(fixsel) {
... ...
@@ -1766,6 +1813,8 @@ re_addprint(re_t *re)
1766 1813
         /* setup window */
1767 1814
         if((re->prints[i].ui=reui_init(DEFAULTFONTHEIGHT*re->fontheightpercent/100,re->ui))==NULL)
1768 1815
                 return(-1); /* couldn't init window */
1816
+#if 0
1817
+/* option A: printouts are immutable */
1769 1818
         if((re->prints[i].data=redata_init(redata_highlighter_register,NULL))==NULL) {
1770 1819
                 reui_free(re->prints[i].ui),re->prints[i].ui=NULL;
1771 1820
                 return(-1); /* couldn't init data store */
... ...
@@ -1780,6 +1829,14 @@ re_addprint(re_t *re)
1780 1829
                 re_sel_toggle(re);
1781 1830
                 re->contentsdirty=1;
1782 1831
         }
1832
+#else
1833
+/* option B: printouts are a window into a fixed place of the file */
1834
+        re->prints[i].data=NULL;
1835
+        re->prints[i].originline=re->sellinefrom;
1836
+        re->prints[i].origincol=0;
1837
+        re->prints[i].showonlyn=re->sellineto-re->sellinefrom+1;
1838
+        re->usedprints++;
1839
+#endif
1783 1840
         re->prints[i].dirty=1;
1784 1841
         return(0);
1785 1842
 }
... ...
@@ -2002,7 +2059,8 @@ re_drawcontents(re_t *re, printout_t *printout)
2002 2059
         int matchingpos;
2003 2060
         char matchingchar;
2004 2061
         int mline,mcol;
2005
-        if(re==NULL || (printout!=NULL && (printout->ui==NULL || printout->data==NULL)))
2062
+        int showonlyn;
2063
+        if(re==NULL || (printout!=NULL && printout->ui==NULL))
2006 2064
                 return(-1);
2007 2065
         /* init vars and clear screen */
2008 2066
         ui=re->ui;
... ...
@@ -2017,6 +2075,7 @@ re_drawcontents(re_t *re, printout_t *printout)
2017 2075
         curcol=re->curcol;
2018 2076
         maxcol=re->maxcol;
2019 2077
         maxrow=re->maxrow;
2078
+        showonlyn=0;
2020 2079
         if(printout==NULL && re->viewonly==1) {
2021 2080
                 memset(&fakeprintout,0,sizeof(printout_t));
2022 2081
                 fakeprintout.ui=re->ui;
... ...
@@ -2026,7 +2085,7 @@ re_drawcontents(re_t *re, printout_t *printout)
2026 2085
                 printout=&fakeprintout;
2027 2086
         } else if(printout!=NULL) {
2028 2087
                 ui=printout->ui;
2029
-                data=printout->data;
2088
+                data=(printout->data==NULL)?re->data:printout->data;
2030 2089
                 x0=0;
2031 2090
                 y0=0;
2032 2091
                 w=ui->w;
... ...
@@ -2035,6 +2094,7 @@ re_drawcontents(re_t *re, printout_t *printout)
2035 2094
                 origincol=curcol=printout->origincol;
2036 2095
                 maxcol=w/ui->fontwidth-1;
2037 2096
                 maxrow=h/ui->fontheight-1;
2097
+                showonlyn=printout->showonlyn;
2038 2098
         }
2039 2099
         if(redata_linecol2pos(data,curline,curcol,&cursorpos,NULL)!=0)
2040 2100
                 return(0); /* error obtaining position */
... ...
@@ -2053,7 +2113,7 @@ re_drawcontents(re_t *re, printout_t *printout)
2053 2113
         if(printout==NULL) {
2054 2114
                 reui_fill(ui,x0,y0+(curline-originline)*ui->fontheight,w,ui->fontheight+1,"\xef\xef\xef\xff");
2055 2115
         } else {
2056
-                for(y=y0+(printout->originline%2)*ui->fontheight;y<(y0+h);y+=ui->fontheight*2)
2116
+                for(y=y0+((printout->data==NULL)?1:(printout->originline%2))*ui->fontheight;y<(y0+h);y+=ui->fontheight*2)
2057 2117
                         reui_fill(ui,x0,y,w,ui->fontheight+1,"\xef\xef\xef\xff");
2058 2118
         }
2059 2119
         /* highlight the selection */
... ...
@@ -2096,6 +2156,8 @@ re_drawcontents(re_t *re, printout_t *printout)
2096 2156
                 /* definition of vars for tracking linecolor usage */
2097 2157
                 int curlinecolor; /* current linecolor */
2098 2158
                 int usedlenlinecolor; /* number of bytes of current linecolor already drawn */
2159
+                if(showonlyn>0 && ui->fontheight>0 && (showonlyn)<((y-y0)/ui->fontheight))
2160
+                        break; /* this printout is configured to only show up to here */
2099 2161
                 /* get start/end of line */
2100 2162
                 if(redata_line_realstart(data,pos,&realstart)==-1 || redata_line_realend(data,pos,&realend)==-1)
2101 2163
                         break; /* couldn't get real start/end */