Browse code

Make Control+Click search the string under the cursor in the function list and got to the line of the first ocurrence, as simplification of 'go to function definition'.

Dario Rodriguez authored on 08/09/2022 21:59:55
Showing 1 changed files
... ...
@@ -63,7 +63,7 @@
63 63
 #define COLOR_INFOFG "\xee\xee\x46\xff"
64 64
 #define COLOR_INFOFGLIGHT "\x84\xa4\x4c\xff"
65 65
 
66
-typedef struct print_t {
66
+typedef struct printout_t {
67 67
         reui_t *ui;
68 68
         redata_t *data;
69 69
         int originline;
... ...
@@ -495,7 +495,7 @@ fprintf(stderr,"Resizing from %ix%i to %ix%i...\n",re->ui->w,re->ui->h,(int)even
495 495
                                                 int mx,my;
496 496
                                                 mx=(event.type!=SDL_MOUSEMOTION)?event.button.x:event.motion.x;
497 497
                                                 my=(event.type!=SDL_MOUSEMOTION)?event.button.y:event.motion.y;
498
-                                                if(re->mouseselactive==0 && event.type==SDL_MOUSEBUTTONDOWN && my<re->y && re->funclisting==NULL) {
498
+                                                if(re->mouseselactive==0 && event.type==SDL_MOUSEBUTTONDOWN && !(SDL_GetModState()&KMOD_CTRL) && my<re->y && re->funclisting==NULL) {
499 499
                                                         redata_t *funclisting;
500 500
                                                         if((funclisting=re_getfunclisting(re))==NULL)
501 501
                                                                 break; /* mem. insuf. */
... ...
@@ -524,6 +524,72 @@ fprintf(stderr,"Resizing from %ix%i to %ix%i...\n",re->ui->w,re->ui->h,(int)even
524 524
                                                         }
525 525
                                                         redata_free(re->funclisting),re->funclisting=NULL;
526 526
                                                         re->contentsdirty=1;
527
+                                                } else if(event.type==SDL_MOUSEBUTTONUP && (SDL_GetModState()&KMOD_CTRL) && my>=re->y) {
528
+                                                        /* control+click: search function definition */
529
+                                                        int newposx,newposy;
530
+                                                        long tmppos;
531
+                                                        newposx=(mx-re->x-(re->showlinenumbers?(re->ui->fontwidth*7+re->ui->fontwidth/2):0))/re->ui->fontwidth;
532
+                                                        newposx=(newposx<0)?0:newposx;
533
+                                                        newposy=(my-re->y)/re->ui->fontheight;
534
+                                                        if(redata_linecol2pos(re->data, re->originline+newposy, re->origincol+newposx,&tmppos,NULL)==0) {
535
+                                                                char searchbuf[1024]; /* magic number: max. size of string to search (truncated if bigger) */
536
+                                                                int usedsearchbuf;
537
+                                                                char utfchar[5];
538
+                                                                int usedutfchar;
539
+                                                                long startpos,curpos,endpos;
540
+                                                                int c;
541
+                                                                redata_t *funclisting;
542
+                                                                /* search start of word */
543
+                                                                for(curpos=startpos=tmppos;redata_getprevutf8char(re->data,curpos,utfchar,sizeof(utfchar),&usedutfchar)==0;startpos=curpos) {
544
+                                                                        curpos-=usedutfchar;
545
+                                                                        if(usedutfchar==1) {
546
+                                                                                c=utfchar[0];
547
+                                                                                if(!((c>='0' && c<='9') || (c>='a' && c<='z') || (c>='A' && c<='Z') || c=='_'))
548
+                                                                                        break;
549
+                                                                        }
550
+                                                                }
551
+                                                                /* search end of word */
552
+                                                                for(curpos=endpos=tmppos;redata_getutf8char(re->data,curpos,utfchar,sizeof(utfchar),&usedutfchar)==0;endpos=curpos) {
553
+                                                                        curpos+=usedutfchar;
554
+                                                                        if(usedutfchar==1) {
555
+                                                                                c=utfchar[0];
556
+                                                                                if(!((c>='0' && c<='9') || (c>='a' && c<='z') || (c>='A' && c<='Z') || c=='_'))
557
+                                                                                        break;
558
+                                                                        }
559
+                                                                }
560
+                                                                /* extract word, generate funclisting, search word in funclisting */
561
+                                                                funclisting=NULL;
562
+                                                                if(redata_getsubstr(re->data,startpos,endpos,searchbuf,sizeof(searchbuf),&usedsearchbuf)==0
563
+                                                                   && (funclisting=re_getfunclisting(re))!=NULL
564
+                                                                   && (curpos=redata_searchforward(funclisting,0,searchbuf,usedsearchbuf))!=-1) {
565
+                                                                        char linebuf[32],*cmdend;
566
+                                                                        int funcline,funccol;
567
+                                                                        if(redata_pos2linecol(funclisting,curpos,&funcline,&funccol)==0
568
+                                                                           && redata_line_getstartstrtrimmed(funclisting,funcline,linebuf,sizeof(linebuf)," ")==0) {
569
+                                                                                int oldline;
570
+                                                                                if((cmdend=strchr(linebuf,':'))!=NULL)
571
+                                                                                        *cmdend='\0';
572
+                                                                                re->command=COMMAND_GOTOLINE;
573
+                                                                                strncpy(re->commandbuf,linebuf,sizeof(re->commandbuf));
574
+                                                                                re->commandbuf[sizeof(re->commandbuf)-1]='\0';
575
+                                                                                oldline=re->curline;
576
+                                                                                re_processcommand(re);
577
+                                                                                if(oldline!=re->curline) {
578
+                                                                                        /* position the cursor near the top of the window */
579
+                                                                                        re->originline=re->curline-3;
580
+                                                                                        re->originline=(re->originline<0)?0:re->originline;
581
+                                                                                }
582
+                                                                                /* position the cursor at the start of the line */
583
+                                                                                re->origincol=0;
584
+                                                                                re->curcol=0;
585
+                                                                                /* redraw */
586
+                                                                                re->contentsdirty=1;
587
+                                                                        }
588
+                                                                        redata_free(re->funclisting),re->funclisting=NULL;
589
+                                                                }
590
+                                                                if(funclisting!=NULL)
591
+                                                                        redata_free(funclisting),funclisting=NULL;
592
+                                                        }
527 593
                                                 } else if(event.type==SDL_MOUSEMOTION && re->funclisting!=NULL) {
528 594
                                                         if(mx<re->ui->fontwidth*10) {
529 595
                                                                 /* scroll */
... ...
@@ -552,7 +618,7 @@ fprintf(stderr,"Resizing from %ix%i to %ix%i...\n",re->ui->w,re->ui->h,(int)even
552 618
                                                 } else if(!(event.type==SDL_MOUSEMOTION && (re->mouseselactive==0 || my<re->y || mx<re->x))) {
553 619
                                                         int newposx,newposy;
554 620
                                                         long tmppos;
555
-                                                        if(event.type==SDL_MOUSEBUTTONDOWN && event.button.button==SDL_BUTTON_LEFT) {
621
+                                                        if(event.type==SDL_MOUSEBUTTONDOWN && !(SDL_GetModState()&KMOD_CTRL) && event.button.button==SDL_BUTTON_LEFT) {
556 622
                                                                 re->mouseselactive=1;
557 623
                                                         } else if( event.type==SDL_MOUSEBUTTONUP && event.button.button==SDL_BUTTON_LEFT) {
558 624
                                                                 re->mouseselactive=0;