Browse code

bugfix: fix inconsistencies between cursorpos and line/col (now, line/col is the authoritative position)

Dario Rodriguez authored on 30/11/2020 19:20:20
Showing 1 changed files
... ...
@@ -57,7 +57,6 @@ typedef struct re_t {
57 57
         char filename[PATH_MAX];
58 58
         int x, y, w, h; // contents rect
59 59
         int fontheightpercent;
60
-        long cursorpos;
61 60
         int originline,origincol;
62 61
         int curline,curcol;
63 62
         int maxrow,maxcol;
... ...
@@ -182,7 +181,6 @@ fprintf(stderr,"QUESTION INIT: %s: %s\n",re->question->title,re->question->body)
182 181
                                         re->flag_newfile=0;
183 182
                                 re->originline=re->origincol=0;
184 183
                                 re->curline=re->curcol=0;
185
-                                re->cursorpos=0;
186 184
                                 load_pending=0;
187 185
                                 re->headerdirty=1;
188 186
                                 re->contentsdirty=1;
... ...
@@ -471,12 +469,15 @@ fprintf(stderr,"SDL_KEYDOWN: DELETE\n");
471 469
 #endif
472 470
                 long realend;
473 471
                 int at_end;
474
-                if(re->cursorpos==redata_getused(re->data))
472
+                long cursorpos;
473
+                if(redata_linecol2pos(re->data,re->curline,re->curcol,&cursorpos,NULL)!=0
474
+                  || cursorpos==redata_getused(re->data)) {
475 475
                         return(0); /* already at end, nothing to delete */
476
-                if(redata_line_realend(re->data,re->cursorpos,&realend)==-1)
476
+                }
477
+                if(redata_line_realend(re->data,cursorpos,&realend)==-1)
477 478
                         return(-1); /* couldn't get current line info */
478
-                at_end=(re->cursorpos==realend)?1:0;
479
-                if(at_end && (re->cursorpos+1)==redata_getused(re->data))
479
+                at_end=(cursorpos==realend)?1:0;
480
+                if(at_end && (cursorpos+1)==redata_getused(re->data))
480 481
                         return(-1); /* we shouldn't erase the final '\n' */
481 482
                 memset(&fakeevent,0,sizeof(SDL_Event));
482 483
                 event=&fakeevent;
... ...
@@ -485,12 +486,10 @@ fprintf(stderr,"SDL_KEYDOWN: DELETE\n");
485 486
                 redata_undo_groupinit(re->data,NULL);
486 487
                 if(at_end) {
487 488
                         int col;
488
-                        if(redata_pos2linecol(re->data,re->cursorpos,NULL,&col)==-1)
489
+                        if(redata_pos2linecol(re->data,cursorpos,NULL,&col)==-1)
489 490
                                 return(-1); /* couldn't get line info */
490
-                        if(redata_op_addn(re->data,re->cursorpos,' ',re->curcol-col,NULL)!=0)
491
+                        if(redata_op_addn(re->data,cursorpos,' ',re->curcol-col,NULL)!=0)
491 492
                                 return(-1); /* couldn't add spaces up to current displayed pos */
492
-                        re->cursorpos+=re->curcol-col;
493
-                        re->cursorpos++;
494 493
                         re->curline++;
495 494
                         re->curcol=0;
496 495
                 } else {
... ...
@@ -506,6 +505,7 @@ fprintf(stderr,"SDL_KEYDOWN: fake setup ok\n");
506 505
                 long realend;
507 506
                 int at_end;
508 507
                 int nspaces;
508
+                long cursorpos;
509 509
                 if(re->ignorenkeys>0) {
510 510
                         re->ignorenkeys--;
511 511
                         return(0); /* this is an already processed key, ignore it */
... ...
@@ -513,9 +513,11 @@ fprintf(stderr,"SDL_KEYDOWN: fake setup ok\n");
513 513
 #if 0
514 514
 fprintf(stderr,"SDL_TEXTINPUT:\"%s\"\n",event->text.text);
515 515
 #endif
516
-                if(redata_line_realend(re->data,re->cursorpos,&realend)==-1)
516
+                if(redata_linecol2pos(re->data,re->curline,re->curcol,&cursorpos,NULL)!=0
517
+                  || redata_line_realend(re->data,cursorpos,&realend)==-1) {
517 518
                         return(-1); /* couldn't get current line info */
518
-                at_end=(re->cursorpos==realend)?1:0;
519
+                }
520
+                at_end=(cursorpos==realend)?1:0;
519 521
                 for(nspaces=0;event->text.text[nspaces]==' ';nspaces++)
520 522
                         ;
521 523
                 if(nspaces>0 && event->text.text[nspaces]=='\0' && at_end) {
... ...
@@ -526,21 +528,21 @@ fprintf(stderr,"SDL_TEXTINPUT:\"%s\"\n",event->text.text);
526 528
                 redata_undo_groupinit(re->data,NULL);
527 529
                 if(!(event->text.text[0]=='\n' && event->text.text[1]=='\0') && at_end) {
528 530
                         int col;
529
-                        if(redata_pos2linecol(re->data,re->cursorpos,NULL,&col)==-1)
531
+                        if(redata_pos2linecol(re->data,cursorpos,NULL,&col)==-1)
530 532
                                 return(-1); /* couldn't get line info */
531
-                        if(redata_op_addn(re->data,re->cursorpos,' ',re->curcol-col,NULL)!=0)
533
+                        if(redata_op_addn(re->data,cursorpos,' ',re->curcol-col,NULL)!=0)
532 534
                                 return(-1); /* couldn't add spaces up to current displayed pos */
533 535
                         /* increment cursorpos; spaces are 1 byte, so the number of columns advanced is the number of bytes advanced */
534
-                        re->cursorpos+=re->curcol-col;
536
+                        cursorpos+=re->curcol-col;
535 537
                 }
536 538
                 len=strlen(event->text.text);
537
-                if(redata_op_add(re->data,re->cursorpos,event->text.text,len,NULL)!=0)
539
+                if(redata_op_add(re->data,cursorpos,event->text.text,len,NULL)!=0)
538 540
                         return(-1); /* couldn't add requested text */
539
-                re->cursorpos+=len;
541
+                cursorpos+=len;
540 542
                 if(event->text.text[0]=='\n' && event->text.text[1]=='\0') {
541 543
                         int trimmed;
542
-                        if(re_rtrim(re,re->cursorpos-len,&trimmed))
543
-                                re->cursorpos-=trimmed;
544
+                        if(re_rtrim(re,cursorpos-len,&trimmed))
545
+                                cursorpos-=trimmed;
544 546
 #if 1
545 547
 #if 0
546 548
 fprintf(stderr,"SDL_TEXTINPUT: Insering newline\n");
... ...
@@ -587,7 +589,6 @@ fprintf(stderr,"SDL_KEYDOWN: sym:%i\n",event->key.keysym.sym);
587 589
                                 redata_op_del(re->data,pos,pos2-pos,NULL);
588 590
                 }
589 591
                 redata_undo_groupcommit(re->data,NULL);
590
-                redata_linecol2pos(re->data,re->curline,re->curcol,&(re->cursorpos),NULL);
591 592
                 re->headerdirty=1;
592 593
                 re->contentsdirty=1;
593 594
         } else if(event->key.keysym.sym==SDLK_DOWN || event->key.keysym.sym==SDLK_UP) {
... ...
@@ -601,9 +602,10 @@ fprintf(stderr,"SDL_KEYDOWN: sym:%i\n",event->key.keysym.sym);
601 602
                         re_sel_resize(re,oldcol,oldline,(event->key.keysym.sym==SDLK_LEFT)?-1:1);
602 603
                 }
603 604
         } else if((SDL_GetModState()&KMOD_CTRL)!=0 && (event->key.keysym.sym==SDLK_PAGEDOWN || event->key.keysym.sym==SDLK_PAGEUP)) {
604
-                re->cursorpos=(event->key.keysym.sym==SDLK_PAGEDOWN)?(redata_getused(re->data)-1):0;
605
-                re->cursorpos=(re->cursorpos<0)?0:re->cursorpos;
606
-                redata_pos2linecol(re->data,re->cursorpos,&(re->curline),&(re->curcol));
605
+                long cursorpos;
606
+                cursorpos=(event->key.keysym.sym==SDLK_PAGEDOWN)?(redata_getused(re->data)-1):0;
607
+                cursorpos=(cursorpos<0)?0:cursorpos;
608
+                redata_pos2linecol(re->data,cursorpos,&(re->curline),&(re->curcol));
607 609
                 re_fixorigin_center(re);
608 610
                 re->headerdirty=1;
609 611
                 re->contentsdirty=1;
... ...
@@ -611,27 +613,33 @@ fprintf(stderr,"SDL_KEYDOWN: sym:%i\n",event->key.keysym.sym);
611 613
                 re_moveupdown(re,(event->key.keysym.sym==SDLK_PAGEUP)?-(re->maxrow):re->maxrow);
612 614
         } else if(event->key.keysym.sym==SDLK_HOME || event->key.keysym.sym==SDLK_END) {
613 615
                 long newpos;
614
-                if((event->key.keysym.sym==SDLK_HOME && redata_line_realstart(re->data,re->cursorpos,&newpos)==-1)
615
-                  || (event->key.keysym.sym==SDLK_END && redata_line_realend(re->data,re->cursorpos,&newpos)==-1)) {
616
+                long cursorpos;
617
+                if(redata_linecol2pos(re->data,re->curline,re->curcol,&cursorpos,NULL)!=0)
618
+                        return(0); /* error obtaining position */
619
+                if((event->key.keysym.sym==SDLK_HOME && redata_line_realstart(re->data,cursorpos,&newpos)==-1)
620
+                  || (event->key.keysym.sym==SDLK_END && redata_line_realend(re->data,cursorpos,&newpos)==-1)) {
616 621
                         return(-1); /* couldn't get destination pos data */
617 622
                 }
618
-                re->cursorpos=newpos;
619
-                if(redata_pos2linecol(re->data,re->cursorpos,NULL,&(re->curcol))==-1)
623
+                cursorpos=newpos;
624
+                if(redata_pos2linecol(re->data,cursorpos,NULL,&(re->curcol))==-1)
620 625
                         return(-1); /* couldn't get col of current pos */;
621 626
                 re_fixorigin(re);
622 627
                 re->headerdirty=1;
623 628
                 re->contentsdirty=1;
624
-        } else if(event->key.keysym.sym==SDLK_BACKSPACE && re->cursorpos>0) {
629
+        } else if(event->key.keysym.sym==SDLK_BACKSPACE && (re->curline>0 || re->curcol>0)) {
625 630
                 int line,col;
626 631
                 long startpos,newpos,realstart,start;
627 632
                 char *ptr;
628 633
                 int len;
629 634
                 int trimmed;
630 635
                 int doneinc;
636
+                long cursorpos;
637
+                if(redata_linecol2pos(re->data,re->curline,re->curcol,&cursorpos,NULL)!=0)
631 638
 #if 0
632 639
 fprintf(stderr,"SDL_KEYDOWN: BACKSPACE%s\n",(event==&fakeevent)?" (fake)":"");
633 640
 #endif
634
-                if(redata_pos2linecol(re->data,re->cursorpos,&line,&col)==-1)
641
+                        return(0); /* error obtaining position */
642
+                if(redata_pos2linecol(re->data,cursorpos,&line,&col)==-1)
635 643
                         return(-1); /* couldn't get current line data */
636 644
                 if(col<re->curcol) {
637 645
                         re_moveleftright(re,-1);
... ...
@@ -641,11 +649,11 @@ fprintf(stderr,"SDL_KEYDOWN: BACKSPACE%s\n",(event==&fakeevent)?" (fake)":"");
641 649
                         redata_undo_groupinit(re->data,NULL);
642 650
                 /* delete the last character */
643 651
                 if(col>0) {
644
-                        if(redata_line_realstart(re->data,re->cursorpos,&realstart)==-1)
652
+                        if(redata_line_realstart(re->data,cursorpos,&realstart)==-1)
645 653
                                 return(-1); /* couldn't get line info */
646 654
                         /* get the start of the part to delete */
647 655
                         doneinc=0;
648
-                        for(ptr=NULL,len=0,start=0,newpos=re->cursorpos;doneinc!=1;) {
656
+                        for(ptr=NULL,len=0,start=0,newpos=cursorpos;doneinc!=1;) {
649 657
                                 if((newpos-1)<realstart)
650 658
                                         break;
651 659
                                 newpos--;
... ...
@@ -657,24 +665,24 @@ fprintf(stderr,"SDL_KEYDOWN: BACKSPACE%s\n",(event==&fakeevent)?" (fake)":"");
657 665
                                         doneinc++;
658 666
                         }
659 667
                         /* delete */
660
-                        redata_op_del(re->data,newpos,re->cursorpos-newpos,NULL);
661
-                        re->cursorpos=newpos;
668
+                        redata_op_del(re->data,newpos,cursorpos-newpos,NULL);
669
+                        cursorpos=newpos;
662 670
                 } else {
663 671
                         long delpos;
664 672
                         /* to make the code trivial, we're being lazy on '\n' deletion: we call ...rawinfo() for each byte in the multibyte utf-8 sequence */
665
-                        for(delpos=re->cursorpos-1
673
+                        for(delpos=cursorpos-1
666 674
                           ;delpos>0
667 675
                           && redata_line_rawinfo(re->data,delpos,&startpos,&ptr,&len,NULL)==0;) {
668 676
                                 if(redata_generic_utf8isstartbyte(ptr[delpos-startpos]))
669 677
                                         break;
670 678
                         }
671
-                        redata_op_del(re->data,delpos,re->cursorpos-delpos,NULL);
672
-                        re->cursorpos=delpos;
679
+                        redata_op_del(re->data,delpos,cursorpos-delpos,NULL);
680
+                        cursorpos=delpos;
673 681
                 }
674
-                redata_pos2linecol(re->data,re->cursorpos,&(re->curline),&(re->curcol));
682
+                redata_pos2linecol(re->data,cursorpos,&(re->curline),&(re->curcol));
675 683
                 /* if cursor at end of line and there are trailing spaces at the end, delete them too */
676
-                if(re_rtrim(re,re->cursorpos,&trimmed))
677
-                        re->cursorpos-=trimmed;
684
+                if(re_rtrim(re,cursorpos,&trimmed))
685
+                        cursorpos-=trimmed;
678 686
                 /* end of deletion */
679 687
                 redata_undo_groupcommit(re->data,NULL);
680 688
                 re->headerdirty=1;
... ...
@@ -699,23 +707,25 @@ fprintf(stderr,"SDL_KEYDOWN: BACKSPACE%s\n",(event==&fakeevent)?" (fake)":"");
699 707
                 long newpos;
700 708
                 if(redata_op_undo(re->data,&newpos))
701 709
                         return(-1);
702
-                re->cursorpos=newpos;
703
-                redata_pos2linecol(re->data,re->cursorpos,&(re->curline),&(re->curcol));
710
+                redata_pos2linecol(re->data,newpos,&(re->curline),&(re->curcol));
704 711
                 re_fixorigin(re);
705 712
                 re->headerdirty=1;
706 713
                 re->contentsdirty=1;
707 714
         } else if(event->key.keysym.sym==SDLK_l && (SDL_GetModState()&KMOD_CTRL)!=0 && re->cachelastsearch[0]!='\0') {
708 715
                 long newpos;
716
+                long cursorpos;
717
+                if(redata_linecol2pos(re->data,re->curline,re->curcol,&cursorpos,NULL)!=0)
718
+                        return(-1); /* error obtaining position */
709 719
                 /* search next (forward) */
710
-                if((newpos=redata_searchforward(re->data,re->cursorpos+1,re->cachelastsearch,strlen(re->cachelastsearch)))==-1) {
720
+                if((newpos=redata_searchforward(re->data,cursorpos+1,re->cachelastsearch,strlen(re->cachelastsearch)))==-1) {
711 721
                         re->command=COMMAND_WARNING;
712 722
                         snprintf(re->commandbuf,sizeof(re->commandbuf),"String not found");
713 723
                         re->commandbuf[sizeof(re->commandbuf)-1]='\0';
714 724
                         re->headerdirty=1;
715 725
                         return(-1);
716 726
                 }
717
-                re->cursorpos=newpos;
718
-                redata_pos2linecol(re->data,re->cursorpos,&(re->curline),&(re->curcol));
727
+                cursorpos=newpos;
728
+                redata_pos2linecol(re->data,cursorpos,&(re->curline),&(re->curcol));
719 729
                 re_fixorigin_center(re);
720 730
                 re->headerdirty=1;
721 731
                 re->contentsdirty=1;
... ...
@@ -742,8 +752,7 @@ fprintf(stderr,"SDL_KEYDOWN: BACKSPACE%s\n",(event==&fakeevent)?" (fake)":"");
742 752
                                 redata_undo_groupinit(re->data,NULL);
743 753
                                 redata_op_del(re->data,frompos,topos-frompos,NULL);
744 754
                                 redata_undo_groupcommit(re->data,NULL);
745
-                                re->cursorpos=frompos;
746
-                                redata_pos2linecol(re->data,re->cursorpos,&(re->curline),&(re->curcol));
755
+                                redata_pos2linecol(re->data,frompos,&(re->curline),&(re->curcol));
747 756
                                 re_fixorigin(re);
748 757
                                 re->headerdirty=1;
749 758
                         }
... ...
@@ -752,18 +761,20 @@ fprintf(stderr,"SDL_KEYDOWN: BACKSPACE%s\n",(event==&fakeevent)?" (fake)":"");
752 761
                 }
753 762
                 re->ignorenkeys++;
754 763
         } else if(event->key.keysym.sym==SDLK_v && (SDL_GetModState()&KMOD_CTRL)!=0) {
755
-fprintf(stderr,"re_processkey(): received Control+v\n");
764
+                long cursorpos;
756 765
                 /* Mac-HIG/OpenLook-style paste (ctrl+v)*/
766
+                if(redata_linecol2pos(re->data,re->curline,re->curcol,&cursorpos,NULL)!=0)
767
+                        return(-1); /* error obtaining position */
757 768
                 if(re->selactive)
758 769
                         re_sel_toggle(re);
759 770
                 if(SDL_HasClipboardText())
760 771
                         re_selectbuf_replace(re,SDL_GetClipboardText());
761 772
                 if(re->usedselectbuf>0) {
762 773
                         redata_undo_groupinit(re->data,NULL);
763
-                        redata_op_add(re->data,re->cursorpos,re->selectbuf,re->usedselectbuf,NULL);
774
+                        redata_op_add(re->data,cursorpos,re->selectbuf,re->usedselectbuf,NULL);
764 775
                         redata_undo_groupcommit(re->data,NULL);
765
-                        re->cursorpos+=re->usedselectbuf;
766
-                        redata_pos2linecol(re->data,re->cursorpos,&(re->curline),&(re->curcol));
776
+                        cursorpos+=re->usedselectbuf;
777
+                        redata_pos2linecol(re->data,cursorpos,&(re->curline),&(re->curcol));
767 778
                         re_fixorigin(re);
768 779
                         re->headerdirty=1;
769 780
                         re->contentsdirty=1;
... ...
@@ -815,7 +826,10 @@ re_processkey_commandwait(re_t *re, SDL_Event *event)
815 826
         } else if(re->command_first_key=='k' && event->key.keysym.sym==SDLK_y) {
816 827
                 long frompos,topos;
817 828
                 int coldone;
829
+                long cursorpos;
818 830
                 /* wordstar-style blockdel (ctrl+k+y) */
831
+                if(redata_linecol2pos(re->data,re->curline,re->curcol,&cursorpos,NULL)!=0)
832
+                        return(-1); /* error obtaining position */
819 833
                 if(re->selactive
820 834
                   && redata_linecol2pos(re->data,re->sellinefrom,re->selcolfrom,&frompos,NULL)==0
821 835
                   && redata_linecol2pos(re->data,re->sellineto,re->selcolto,&topos,&coldone)==0) {
... ...
@@ -823,9 +837,9 @@ re_processkey_commandwait(re_t *re, SDL_Event *event)
823 837
                         redata_undo_groupinit(re->data,NULL);
824 838
                         redata_op_del(re->data,frompos,topos-frompos,NULL);
825 839
                         redata_undo_groupcommit(re->data,NULL);
826
-                        if(re->cursorpos>frompos) {
827
-                                re->cursorpos=frompos;
828
-                                redata_pos2linecol(re->data,re->cursorpos,&(re->curline),&(re->curcol));
840
+                        if(cursorpos>frompos) {
841
+                                cursorpos=frompos;
842
+                                redata_pos2linecol(re->data,cursorpos,&(re->curline),&(re->curcol));
829 843
                                 re_fixorigin(re);
830 844
                         }
831 845
                         re->headerdirty=1;
... ...
@@ -857,17 +871,19 @@ re_processkey_commandwait(re_t *re, SDL_Event *event)
857 871
                                 insertpos+=re->curcol-coldone;
858 872
                         }
859 873
                         if(is_move) {
874
+                                long cursorpos;
860 875
                                 redata_op_del(re->data,frompos,re->usedselectbuf,NULL);
861 876
                                 redata_op_add(re->data,insertpos-((insertpos>frompos)?re->usedselectbuf:0),re->selectbuf,re->usedselectbuf,NULL);
862
-                                re->cursorpos=insertpos-((insertpos>frompos)?re->usedselectbuf:0)+re->usedselectbuf;
863
-                                redata_pos2linecol(re->data,re->cursorpos,&(re->curline),&(re->curcol));
864
-                                redata_pos2linecol(re->data,re->cursorpos-re->usedselectbuf,&(re->sellinefrom),&(re->selcolfrom));
877
+                                cursorpos=insertpos-((insertpos>frompos)?re->usedselectbuf:0)+re->usedselectbuf;
878
+                                redata_pos2linecol(re->data,cursorpos,&(re->curline),&(re->curcol));
879
+                                redata_pos2linecol(re->data,cursorpos-re->usedselectbuf,&(re->sellinefrom),&(re->selcolfrom));
865 880
                                 re->sellineto=re->curline;
866 881
                                 re->selcolto=re->curcol;
867 882
                         } else {
883
+                                long cursorpos;
868 884
                                 redata_op_add(re->data,insertpos,re->selectbuf,re->usedselectbuf,NULL);
869
-                                re->cursorpos=insertpos+re->usedselectbuf;
870
-                                redata_pos2linecol(re->data,re->cursorpos,&(re->curline),&(re->curcol));
885
+                                cursorpos=insertpos+re->usedselectbuf;
886
+                                redata_pos2linecol(re->data,cursorpos,&(re->curline),&(re->curcol));
871 887
                                 redata_pos2linecol(re->data,insertpos,&(re->sellinefrom),&(re->selcolfrom));
872 888
                                 re->sellineto=re->curline;
873 889
                                 re->selcolto=re->curcol;
... ...
@@ -948,7 +964,6 @@ re_processcommand(re_t *re)
948 964
                         re->headerdirty=1;
949 965
                         return(-1);
950 966
                 }
951
-                re->cursorpos=pos;
952 967
                 re->curline=line;
953 968
                 /* position the line in the center of viewport */
954 969
                 re->originline=line-(re->maxrow/2);
... ...
@@ -956,7 +971,10 @@ re_processcommand(re_t *re)
956 971
                 re->contentsdirty=1;
957 972
         } else if(strcmp(re->command,COMMAND_SEARCHFORWARD)==0) {
958 973
                 long newpos;
959
-                if((newpos=redata_searchforward(re->data,re->cursorpos,re->commandbuf,strlen(re->commandbuf)))==-1) {
974
+                long cursorpos;
975
+                if(redata_linecol2pos(re->data,re->curline,re->curcol,&cursorpos,NULL)!=0)
976
+                        return(-1); /* error obtaining position */
977
+                if((newpos=redata_searchforward(re->data,cursorpos,re->commandbuf,strlen(re->commandbuf)))==-1) {
960 978
                         re->command=COMMAND_WARNING;
961 979
                         snprintf(re->commandbuf,sizeof(re->commandbuf),"String not found");
962 980
                         re->commandbuf[sizeof(re->commandbuf)-1]='\0';
... ...
@@ -965,8 +983,8 @@ re_processcommand(re_t *re)
965 983
                 }
966 984
                 strncpy(re->cachelastsearch,re->commandbuf,sizeof(re->cachelastsearch));
967 985
                 re->cachelastsearch[sizeof(re->cachelastsearch)-1]='\0';
968
-                re->cursorpos=newpos;
969
-                redata_pos2linecol(re->data,re->cursorpos,&(re->curline),&(re->curcol));
986
+                cursorpos=newpos;
987
+                redata_pos2linecol(re->data,cursorpos,&(re->curline),&(re->curcol));
970 988
                 re_fixorigin_center(re);
971 989
                 re->headerdirty=1;
972 990
                 re->contentsdirty=1;
... ...
@@ -1027,8 +1045,7 @@ re_processcommand(re_t *re)
1027 1045
                         redata_op_add(re->data,newpos,re->cachelastreplacewith,rlen,NULL);
1028 1046
                 }
1029 1047
                 redata_undo_groupcommit(re->data,NULL);
1030
-                re->cursorpos=oldpos;
1031
-                redata_pos2linecol(re->data,re->cursorpos,&(re->curline),&(re->curcol));
1048
+                redata_pos2linecol(re->data,oldpos,&(re->curline),&(re->curcol));
1032 1049
                 re_fixorigin_center(re);
1033 1050
                 re->headerdirty=1;
1034 1051
                 re->contentsdirty=1;
... ...
@@ -1061,31 +1078,34 @@ re_moveupdown(re_t *re, int totalinc)
1061 1078
         int inc,doneinc;
1062 1079
         long realstart;
1063 1080
         int needs_inccol;
1081
+        long cursorpos;
1064 1082
         if(re==NULL)
1065 1083
                 return(-1); /* sanity check failed */
1066 1084
         if(totalinc==0)
1067 1085
                 return(0); /* nothing to do */
1068 1086
         inc=(totalinc<0)?-1:1;
1069
-        newpos=re->cursorpos; /* get rid of compiler warning (will be overwitten in the loop as totalinc never is 0) */
1087
+        if(redata_linecol2pos(re->data,re->curline,re->curcol,&cursorpos,NULL)!=0)
1088
+                return(-1); /* error obtaining position */
1089
+        newpos=cursorpos; /* get rid of compiler warning (will be overwitten in the loop as totalinc never is 0) */
1070 1090
         needs_inccol=0;
1071 1091
         for(doneinc=0;doneinc!=totalinc;doneinc+=inc) {
1072 1092
 #if 0
1073
-fprintf(stderr,"MOVING from cursorpos:%li line:%i col:%i\n",re->cursorpos,re->curline,re->curcol);
1093
+fprintf(stderr,"MOVING from cursorpos:%li line:%i col:%i\n",cursorpos,re->curline,re->curcol);
1074 1094
 #endif
1075
-                if((inc==-1 && redata_line_prevrealstart(re->data,re->cursorpos,&realstart)==-1)
1076
-                   || (inc==1 && redata_line_nextrealstart(re->data,re->cursorpos,&realstart)==-1)) {
1095
+                if((inc==-1 && redata_line_prevrealstart(re->data,cursorpos,&realstart)==-1)
1096
+                   || (inc==1 && redata_line_nextrealstart(re->data,cursorpos,&realstart)==-1)) {
1077 1097
                         break; /* couldn't get current line data, we are at start/end */
1078 1098
                 }
1079
-                re->cursorpos=realstart;
1099
+                cursorpos=realstart;
1080 1100
                 needs_inccol=1;
1081 1101
                 re->curline+=inc;
1082 1102
 #if 0
1083
-fprintf(stderr,"MOVING   to cursorpos:%li line:%i col:%i\n",re->cursorpos,re->curline,re->curcol);
1103
+fprintf(stderr,"MOVING   to cursorpos:%li line:%i col:%i\n",cursorpos,re->curline,re->curcol);
1084 1104
 #endif
1085 1105
         }
1086 1106
         if(!needs_inccol)
1087 1107
                 return(-1);
1088
-        if(redata_line_inccol(re->data,re->cursorpos,re->curcol,&newpos2,NULL)==-1) {
1108
+        if(redata_line_inccol(re->data,cursorpos,re->curcol,&newpos2,NULL)==-1) {
1089 1109
                 /* error advancing cursor, "emergency" repositioning */
1090 1110
 #if 0
1091 1111
 fprintf(stderr,"SETTING COLUMN ERROR\n");
... ...
@@ -1094,11 +1114,11 @@ fprintf(stderr,"SETTING COLUMN ERROR\n");
1094 1114
                 newpos2=newpos;
1095 1115
         }
1096 1116
 #if 0
1097
-fprintf(stderr,"COLUMN from cursorpos:%li line:%i col:%i\n",re->cursorpos,re->curline,re->curcol);
1117
+fprintf(stderr,"COLUMN from cursorpos:%li line:%i col:%i\n",cursorpos,re->curline,re->curcol);
1098 1118
 #endif
1099
-        re->cursorpos=newpos2;
1119
+        cursorpos=newpos2;
1100 1120
 #if 0
1101
-fprintf(stderr,"COLUMN   to cursorpos:%li line:%i col:%i\n",re->cursorpos,re->curline,re->curcol);
1121
+fprintf(stderr,"COLUMN   to cursorpos:%li line:%i col:%i\n",cursorpos,re->curline,re->curcol);
1102 1122
 #endif
1103 1123
         re_fixorigin(re);
1104 1124
         re->contentsdirty=1;
... ...
@@ -1115,30 +1135,33 @@ re_moveleftright(re_t *re, int totalinc)
1115 1135
         int len;
1116 1136
         long start;
1117 1137
         int tmpcol,oldcol;
1138
+        long cursorpos;
1118 1139
         if(re==NULL)
1119 1140
                 return(-1); /* sanity check failed */
1120 1141
         if(totalinc==0)
1121 1142
                 return(0); /* nothing to do */
1143
+        if(redata_linecol2pos(re->data,re->curline,re->curcol,&cursorpos,NULL)!=0)
1144
+                return(-1); /* error obtaining position */
1122 1145
         oldcol=re->curcol;
1123 1146
         if(totalinc<0 && (re->curcol+totalinc)<=0) {
1124 1147
                 /* we'll land on the start of the line -- do it trivially */
1125
-                if(redata_line_realstart(re->data,re->cursorpos,&realstart)==-1)
1148
+                if(redata_line_realstart(re->data,cursorpos,&realstart)==-1)
1126 1149
                         return(-1); /* couldn't get current pos */
1127 1150
                 re->curcol=0;
1128
-                re->cursorpos=realstart;
1151
+                cursorpos=realstart;
1129 1152
         } else {
1130 1153
                 /* move a char at a time */
1131
-                if(redata_line_realstart(re->data,re->cursorpos,&realstart)==-1)
1154
+                if(redata_line_realstart(re->data,cursorpos,&realstart)==-1)
1132 1155
                         return(-1); /* couldn't get current pos */
1133 1156
                 inc=(totalinc<0)?-1:1;
1134 1157
                 doneinc=0;
1135
-                if(redata_pos2linecol(re->data,re->cursorpos,NULL,&tmpcol)==-1)
1158
+                if(redata_pos2linecol(re->data,cursorpos,NULL,&tmpcol)==-1)
1136 1159
                         return(-1); /* couldn't get current pos */
1137 1160
                 /* special case: we're just over the '\n' going right, we have to move 1 to enable the generic case to work */
1138 1161
                 if(tmpcol==re->curcol && inc>0) {
1139
-                        if(redata_line_rawinfo(re->data,re->cursorpos,&start,&ptr,&len,NULL)==-1)
1162
+                        if(redata_line_rawinfo(re->data,cursorpos,&start,&ptr,&len,NULL)==-1)
1140 1163
                                 return(-1); /* couldn't get line data */
1141
-                        if(ptr[re->cursorpos-start]=='\n') {
1164
+                        if(ptr[cursorpos-start]=='\n') {
1142 1165
                                 tmpcol++;
1143 1166
                                 totalinc--;
1144 1167
                                 re->curcol++;
... ...
@@ -1155,7 +1178,7 @@ re_moveleftright(re_t *re, int totalinc)
1155 1178
                         }
1156 1179
                 }
1157 1180
                 /* generic case: move one char at a time */
1158
-                for(ptr=NULL,len=0,start=0,newpos=re->cursorpos;doneinc!=totalinc;) {
1181
+                for(ptr=NULL,len=0,start=0,newpos=cursorpos;doneinc!=totalinc;) {
1159 1182
                         if((newpos+inc)<realstart)
1160 1183
                                 break;
1161 1184
                         newpos+=inc;
... ...
@@ -1168,7 +1191,7 @@ re_moveleftright(re_t *re, int totalinc)
1168 1191
                         if(ptr[newpos-start]=='\n')
1169 1192
                                 break;
1170 1193
                 }
1171
-                re->cursorpos=newpos;
1194
+                cursorpos=newpos;
1172 1195
                 re->curcol+=doneinc;
1173 1196
                 if(inc>0 && doneinc<totalinc && ptr!=NULL && ptr[newpos-start]=='\n')
1174 1197
                         re->curcol+=(totalinc-doneinc);
... ...
@@ -1440,11 +1463,6 @@ re_rtrim(re_t *re, long curpos, int *trimmed)
1440 1463
                         n++;
1441 1464
                 /* delete the counted number of spaces at end of line */
1442 1465
                 redata_op_del(re->data,startpos+len-1-n,n,NULL);
1443
-                /* fix cursorpos if it is after the deleted characters or over one of the deleted characters */
1444
-                if(re->cursorpos>=(startpos+len-1))
1445
-                        re->cursorpos-=n;
1446
-                else if(re->cursorpos>=(startpos+len-1-n))
1447
-                        re->cursorpos=(startpos+len-1-n);
1448 1466
                 if(trimmed!=NULL)
1449 1467
                         *trimmed=n;
1450 1468
         }
... ...
@@ -1518,15 +1536,15 @@ re_getmatchingbracket(re_t *re,long posini, char originalchar, char *matchingcha
1518 1536
 int
1519 1537
 re_drawheader_editing(re_t *re)
1520 1538
 {
1521
-        int line,col;
1539
+        long cursorpos;
1522 1540
         if(re==NULL)
1523 1541
                 return(-1);
1524
-        if(redata_pos2linecol(re->data,re->cursorpos,&line,&col)==-1)
1525
-                line=col=-1;
1542
+        if(redata_linecol2pos(re->data,re->curline,re->curcol,&cursorpos,NULL)!=0)
1543
+                return(0); /* error obtaining position */
1526 1544
         reui_fill(re->ui,0,0,re->ui->w,re->ui->fontheight,COLOR_STATUSBG);
1527 1545
         /* for the user, lines start at 0 (internally they start at 0) */
1528
-        reui_printf(re->ui,0,0,COLOR_STATUSFG,"File: %s Line:%i (%i) Col:%i (%i) Pos:%li Size:%li %s"
1529
-          ,re->filename,re->curline+1,line+1,re->curcol+1,col+1,re->cursorpos,redata_getused(re->data)
1546
+        reui_printf(re->ui,0,0,COLOR_STATUSFG,"File: %s Line:%i Col:%i Pos:%li Size:%li %s"
1547
+          ,re->filename,re->curline+1,re->curcol+1,cursorpos,redata_getused(re->data)
1530 1548
           ,"");
1531 1549
 #warning #error MAKE THE PREVIOUS LINE LAST %s print: "CHANGED" when unsaved&not-in-reu, "changed" when unsaved but is in reu, "saved" when it is saved.
1532 1550
         re->headerdirty=0;
... ...
@@ -1595,11 +1613,14 @@ re_drawcontents(re_t *re)
1595 1613
         int matchingpos;
1596 1614
         char matchingchar;
1597 1615
         int mline,mcol;
1616
+        long cursorpos;
1598 1617
         const char *hint;
1599 1618
         const char selcolornormal[]={"\xde\xcf\x7f\xff"};
1600 1619
         const char selcolorcurline[]={"\xf0\xea\xc9\xff"};
1601 1620
         if(re==NULL)
1602 1621
                 return(-1);
1622
+        if(redata_linecol2pos(re->data,re->curline,re->curcol,&cursorpos,NULL)!=0)
1623
+                return(0); /* error obtaining position */
1603 1624
 #if 0
1604 1625
 fprintf(stderr,"re_drawcontents: pre  reuifill1\n");
1605 1626
 #endif
... ...
@@ -1608,7 +1629,7 @@ fprintf(stderr,"re_drawcontents: pre  reuifill1\n");
1608 1629
 fprintf(stderr,"re_drawcontents: post reuifill1\n");
1609 1630
 #endif
1610 1631
         row=re->curline-re->originline;
1611
-        pos=re->cursorpos;
1632
+        pos=cursorpos;
1612 1633
         while(row>0 && pos>0) {
1613 1634
                 if(redata_line_rawinfo(re->data,pos,&newpos,NULL,NULL,&is_continuation)==-1)
1614 1635
                         return(-1);
... ...
@@ -1719,7 +1740,7 @@ fprintf(stderr,"\n");
1719 1740
                                         reui_write(re->ui,re->x+re->ui->fontwidth*(re->curcol-re->origincol),y,"\xff\xff\xff\xff",curptr,utf8charlen);
1720 1741
                                         /* get matching braces/parens/anglebracket/curlybraces for highlighting */
1721 1742
                                         if(utf8charlen==1 && strchr("[]{}<>()",*curptr)!=NULL)
1722
-                                                matchingpos=re_getmatchingbracket(re,re->cursorpos,*curptr,&matchingchar);
1743
+                                                matchingpos=re_getmatchingbracket(re,cursorpos,*curptr,&matchingchar);
1723 1744
                                 }
1724 1745
                         }
1725 1746
                 }
... ...
@@ -1751,7 +1772,7 @@ fprintf(stderr,"\n");
1751 1772
                         reui_balloon(re->ui, '\0', x,y, fg, bg, &matchingchar,1);
1752 1773
         }
1753 1774
         /* display prototypes info if applicable */
1754
-        hint=redata_prototypes_get(re->data, re->cursorpos);
1775
+        hint=redata_prototypes_get(re->data, cursorpos);
1755 1776
         if(hint!=NULL) {
1756 1777
                 if((re->curline-re->originline)>=(re->maxrow/2))
1757 1778
                         reui_balloon(re->ui, '\0', re->x+re->w/2, re->y+re->ui->fontheight*3/2, "\x80\x80\x80\xff", "\xff\xff\xff\xcf",(char *) hint,strlen(hint));