Browse code

fix bug with rtrim and moveupdown (sometimes didn't move correctly when at end of file)

Dario Rodriguez authored on 10/10/2020 22:34:34
Showing 1 changed files
... ...
@@ -399,7 +399,6 @@ re_processkey_editing(re_t *re, SDL_Event *event)
399 399
                 return(-1); /* sanity check failed */
400 400
         /* convert RETURN KEYDOWN to TEXTINPUT */
401 401
         if(event->type==SDL_KEYDOWN && event->key.keysym.sym==SDLK_RETURN) {
402
-#warning file having two \n at the end, delete the second with DEL staying to the right of the first, if we go down then, it does an scroll but it shouldn't
403 402
                 memset(&fakeevent,0,sizeof(SDL_Event));
404 403
                 event=&fakeevent;
405 404
                 event->type=SDL_TEXTINPUT;
... ...
@@ -413,7 +412,7 @@ re_processkey_editing(re_t *re, SDL_Event *event)
413 412
                 strncpy(event->text.text,spaces+(re->curcol%8),sizeof(event->text.text));
414 413
                 event->text.text[sizeof(event->text.text)-1]='\0';
415 414
         } else if(event->type==SDL_KEYDOWN && event->key.keysym.sym==SDLK_DELETE) {
416
-#if 0
415
+#if 1
417 416
 fprintf(stderr,"SDL_KEYDOWN: DELETE\n");
418 417
 #endif
419 418
                 long realend;
... ...
@@ -488,6 +487,11 @@ fprintf(stderr,"SDL_TEXTINPUT:\"%s\"\n",event->text.text);
488 487
                         int trimmed;
489 488
                         if(re_rtrim(re,re->cursorpos-len,&trimmed))
490 489
                                 re->cursorpos-=trimmed;
490
+#if 1
491
+#if 0
492
+fprintf(stderr,"SDL_TEXTINPUT: Insering newline\n");
493
+#endif
494
+#endif
491 495
                         re->curline++;
492 496
                         re->curcol=0;
493 497
                 } else {
... ...
@@ -732,12 +736,14 @@ re_moveupdown(re_t *re, int totalinc)
732 736
         long newpos,newpos2;
733 737
         int inc,doneinc;
734 738
         long realstart;
739
+        int needs_inccol;
735 740
         if(re==NULL)
736 741
                 return(-1); /* sanity check failed */
737 742
         if(totalinc==0)
738 743
                 return(0); /* nothing to do */
739 744
         inc=(totalinc<0)?-1:1;
740 745
         newpos=re->cursorpos; /* get rid of compiler warning (will be overwitten in the loop as totalinc never is 0) */
746
+        needs_inccol=0;
741 747
         for(doneinc=0;doneinc!=totalinc;doneinc+=inc) {
742 748
 #if 0
743 749
 fprintf(stderr,"MOVING from cursorpos:%li line:%i col:%i\n",re->cursorpos,re->curline,re->curcol);
... ...
@@ -747,16 +753,18 @@ fprintf(stderr,"MOVING from cursorpos:%li line:%i col:%i\n",re->cursorpos,re->cu
747 753
                         break; /* couldn't get current line data, we are at start/end */
748 754
                 }
749 755
                 re->cursorpos=realstart;
756
+                needs_inccol=1;
750 757
                 re->curline+=inc;
751
-                re_fixorigin(re);
752 758
 #if 0
753 759
 fprintf(stderr,"MOVING   to cursorpos:%li line:%i col:%i\n",re->cursorpos,re->curline,re->curcol);
754 760
 #endif
755 761
         }
762
+        if(!needs_inccol)
763
+                return(-1);
756 764
         if(redata_line_inccol(re->data,re->cursorpos,re->curcol,&newpos2,NULL)==-1) {
757 765
                 /* error advancing cursor, "emergency" repositioning */
758 766
 #if 0
759
-fprintf(stderr,"COLUMN ERROR\n");
767
+fprintf(stderr,"SETTING COLUMN ERROR\n");
760 768
 #endif
761 769
                 re->curcol=0;
762 770
                 newpos2=newpos;
... ...
@@ -768,6 +776,7 @@ fprintf(stderr,"COLUMN from cursorpos:%li line:%i col:%i\n",re->cursorpos,re->cu
768 776
 #if 0
769 777
 fprintf(stderr,"COLUMN   to cursorpos:%li line:%i col:%i\n",re->cursorpos,re->curline,re->curcol);
770 778
 #endif
779
+        re_fixorigin(re);
771 780
         re->contentsdirty=1;
772 781
         re->headerdirty=1;
773 782
         return(0);
... ...
@@ -869,13 +878,17 @@ re_rtrim(re_t *re, long curpos, int *trimmed)
869 878
                 *trimmed=0;
870 879
         if(redata_line_rawinfo(re->data,curpos,&startpos,&start,&len,NULL)==0 &&
871 880
            len>1 && start[len-1]=='\n' && start[len-2]==' ' && curpos==(startpos+len-1)) {
872
-                for(n=1;(n+1)<(len-1) && start[len-1-n-1]==' ';n++)
873
-                        ;
874
-#if 0
875
-fprintf(stderr,"Trying to DELETE SPACES del %i bytes\n",n);
876
-#endif
877
-                redata_op_del(re->data,curpos-n,n,NULL);
878
-                re->cursorpos-=n;
881
+                /* count the number of spaces at end of line */
882
+                n=0;
883
+                while((len-1-n-1)>=0 && start[len-1-n-1]==' ')
884
+                        n++;
885
+                /* delete the counted number of spaces at end of line */
886
+                redata_op_del(re->data,startpos+len-1-n,n,NULL);
887
+                /* fix cursorpos if it is after the deleted characters or over one of the deleted characters */
888
+                if(re->cursorpos>=(startpos+len-1))
889
+                        re->cursorpos-=n;
890
+                else if(re->cursorpos>=(startpos+len-1-n))
891
+                        re->cursorpos=(startpos+len-1-n);
879 892
                 if(trimmed!=NULL)
880 893
                         *trimmed=n;
881 894
         }
... ...
@@ -892,7 +905,7 @@ re_drawheader_editing(re_t *re)
892 905
                 line=col=-1;
893 906
         reui_fill(re->ui,0,0,re->ui->w,re->ui->fontheight,COLOR_STATUSBG);
894 907
         /* for the user, lines start at 0 (internally they start at 0) */
895
-        reui_printf(re->ui,0,0,COLOR_STATUSFG,"File: %s Line:%i (%i) Col:%i (%i) Pos:%li",re->filename,re->curline+1,line+1,re->curcol+1,col+1,re->cursorpos);
908
+        reui_printf(re->ui,0,0,COLOR_STATUSFG,"File: %s Line:%i (%i) Col:%i (%i) Pos:%li Size:%li",re->filename,re->curline+1,line+1,re->curcol+1,col+1,re->cursorpos,redata_getused(re->data));
896 909
         re->headerdirty=0;
897 910
         re->ui->rendererdirty=1;
898 911
         return(0);