Browse code

Show manpage of word under cursor with Control+F1 (opens a printout)

Dario Rodriguez authored on 22/01/2024 21:46:48
Showing 1 changed files
... ...
@@ -1,4 +1,4 @@
1
-/*
1
+	/*
2 2
  * recenteditor.c
3 3
  *
4 4
  * A programmers editor
... ...
@@ -298,6 +298,7 @@ int re_clipget(re_t *re);
298 298
 int re_addprint(re_t *re, typeprintout_t typeprintout);
299 299
 int re_delprint(re_t *re, int nprint);
300 300
 int re_rtrim(re_t *re, long curpos, int *trimmed);
301
+int re_extractword2buf(re_t *re, redata_t *data, long pos, char *buf, int sizebuf, int *usedbuf);
301 302
 long re_getmatchingbracket(re_t *re,long posini, char originalchar, char *matchingchar);
302 303
 int re_drawheader_editing(re_t *re);
303 304
 int re_drawheader_command(re_t *re);
... ...
@@ -537,7 +538,9 @@ fprintf(stderr,"RENDER\n");
537 538
                                         case SDL_MOUSEBUTTONDOWN:
538 539
                                         case SDL_MOUSEBUTTONUP:
539 540
                                         case SDL_MOUSEMOTION:
540
-                                                if(!(event.type==SDL_MOUSEMOTION && re->mouseselactive==0)) {
541
+                                                if(!(event.type==SDL_MOUSEMOTION && re->mouseselactive==0)
542
+                                                  && printout->type!=printoutHelp
543
+                                                  && printout->type!=printoutManpage) {
541 544
                                                         int mx,my;
542 545
                                                         int newposx,newposy;
543 546
                                                         long tmppos;
... ...
@@ -718,32 +721,11 @@ fprintf(stderr,"Resizing from %ix%i to %ix%i...\n",re->ui->w,re->ui->h,(int)even
718 721
                                                         if(redata_linecol2pos(re->data, re->originline+newposy, re->origincol+newposx,&tmppos,NULL)==0) {
719 722
                                                                 char searchbuf[1024]; /* magic number: max. size of string to search (truncated if bigger) */
720 723
                                                                 int usedsearchbuf;
721
-                                                                char utfchar[5];
722
-                                                                int usedutfchar;
723
-                                                                long startpos,curpos,endpos;
724
-                                                                int c;
724
+                                                                long curpos;
725 725
                                                                 redata_t *funclisting;
726
-                                                                /* search start of word */
727
-                                                                for(curpos=startpos=tmppos;redata_getprevutf8char(re->data,curpos,utfchar,sizeof(utfchar),&usedutfchar)==0;startpos=curpos) {
728
-                                                                        curpos-=usedutfchar;
729
-                                                                        if(usedutfchar==1) {
730
-                                                                                c=utfchar[0];
731
-                                                                                if(!((c>='0' && c<='9') || (c>='a' && c<='z') || (c>='A' && c<='Z') || c=='_'))
732
-                                                                                        break;
733
-                                                                        }
734
-                                                                }
735
-                                                                /* search end of word */
736
-                                                                for(curpos=endpos=tmppos;redata_getutf8char(re->data,curpos,utfchar,sizeof(utfchar),&usedutfchar)==0;endpos=curpos) {
737
-                                                                        curpos+=usedutfchar;
738
-                                                                        if(usedutfchar==1) {
739
-                                                                                c=utfchar[0];
740
-                                                                                if(!((c>='0' && c<='9') || (c>='a' && c<='z') || (c>='A' && c<='Z') || c=='_'))
741
-                                                                                        break;
742
-                                                                        }
743
-                                                                }
744 726
                                                                 /* extract word, generate funclisting, search word in funclisting */
745 727
                                                                 funclisting=NULL;
746
-                                                                if(redata_getsubstr(re->data,startpos,endpos,searchbuf,sizeof(searchbuf),&usedsearchbuf)==0
728
+                                                                if(re_extractword2buf(re, re->data, tmppos, searchbuf, sizeof(searchbuf), &usedsearchbuf)==0
747 729
                                                                    && (funclisting=re_getfunclisting(re))!=NULL
748 730
                                                                    && (curpos=redata_searchforward(funclisting,0,searchbuf,usedsearchbuf))!=-1) {
749 731
                                                                         char linebuf[32],*cmdend;
... ...
@@ -1385,6 +1367,8 @@ fprintf(stderr,"SDL_KEYDOWN: BACKSPACE%s\n",(event==&fakeevent)?" (fake)":"");
1385 1367
                 re->headerdirty=1;
1386 1368
         } else if(event->key.keysym.sym==SDLK_F1 && (SDL_GetModState()&(KMOD_CTRL|KMOD_SHIFT|KMOD_ALT))==0) {
1387 1369
                 re_addprint(re,printoutHelp);
1370
+        } else if(event->key.keysym.sym==SDLK_F1 && (SDL_GetModState()&KMOD_CTRL)!=0 && (SDL_GetModState()&(KMOD_SHIFT|KMOD_ALT))==0 ) {
1371
+                re_addprint(re,printoutManpage);
1388 1372
         } else if(event->key.keysym.sym==SDLK_F2 || (event->key.keysym.sym==SDLK_s && (SDL_GetModState()&KMOD_CTRL)!=0)) {
1389 1373
                 char *errormsg=NULL;
1390 1374
                 if(redata_save(re->data,re->filename,&errormsg)!=-1)
... ...
@@ -2255,12 +2239,11 @@ re_addprint(re_t *re, typeprintout_t typeprintout)
2255 2239
         }
2256 2240
         if(i>=re->sizeprints)
2257 2241
                 return(-1); /* INTERNAL ERROR */
2258
-        /* setup window */
2259
-        if((re->prints[i].ui=reui_init(DEFAULTFONTHEIGHT*re->fontheightpercent/100,re->ui))==NULL)
2260
-                return(-1); /* couldn't init window */
2261 2242
         if(typeprintout==printoutHelp
2262
-          || typeprintout==printoutManpage
2263 2243
           || typeprintout==printoutCopy) {
2244
+                /* setup window */
2245
+                if((re->prints[i].ui=reui_init(DEFAULTFONTHEIGHT*re->fontheightpercent/100,re->ui))==NULL)
2246
+                        return(-1); /* couldn't init window */
2264 2247
                 /* inmutable printout */
2265 2248
                 if((re->prints[i].data=redata_init(redata_highlighter_register,NULL))==NULL) {
2266 2249
                         reui_free(re->prints[i].ui),re->prints[i].ui=NULL;
... ...
@@ -2293,7 +2276,7 @@ Recenteditor help\n\
2293 2276
 \n\
2294 2277
  F1           - Open a printout with this help\n\
2295 2278
  Shift+F1     - (TODO) Update hints with functions definitions of all open editors\n\
2296
- Control+F1   - (TODO) Open manpage of word under the cursor in a printout\n\
2279
+ Control+F1   - Open manpage of word under the cursor in a printout\n\
2297 2280
  F2           - Save\n\
2298 2281
 \n\
2299 2282
  Cursor keys        - Move cursor\n\
... ...
@@ -2310,7 +2293,7 @@ Recenteditor help\n\
2310 2293
  Home/End           - Go to the start/end of the line\n\
2311 2294
  Control+'0'        - Set default font size\n\
2312 2295
  Shift+Ctrl+'0'     - Set default color permutation\n\
2313
- Control+C          - Copy selection to clipboard. Will deselect current selection.\n\
2296
+ Control+C          - Copy selection to clipboard. Will deselect selection.\n\
2314 2297
  Control+K+Command  - Block command, see below\n\
2315 2298
  Control+L          - Search next (requires a previous search with Control+Q+F)\n\
2316 2299
  Control+N          - Show/hide line numbers\n\
... ...
@@ -2335,7 +2318,7 @@ Editing commands (Control+Q+Command)\n\
2335 2318
 Block commands (Control+K+Command)\n\
2336 2319
 ----------------------------------\n\
2337 2320
 \n\
2338
- Control+K+Q - Close editor (exit program -- don\'t ask, it is a legacy key combo)\n\
2321
+ Control+K+Q - Close editor (exit program -- don\'t ask, legacy key combo)\n\
2339 2322
  Control+K+H - Show/hide selection\n\
2340 2323
  Control+K+B - Set start of selection to cursor position\n\
2341 2324
  Control+K+K - Set end of selection to cursor position\n\
... ...
@@ -2354,8 +2337,55 @@ Printout window\n\
2354 2337
                         redata_op_add(re->prints[i].data,0,helptext,sizeof(helptext)-1,NULL);
2355 2338
                         re->contentsdirty=1;
2356 2339
                 }
2340
+        } else if(typeprintout==printoutManpage) {
2341
+                char searchbuf[1024]; /* magic number: max. size of string to search (truncated if bigger) */
2342
+                char title[256];
2343
+                int lenprefix;
2344
+                int usedsearchbuf;
2345
+                long tmppos;
2346
+                int nread;
2347
+                FILE *fp;
2348
+                strncpy(searchbuf,"man ",sizeof(searchbuf));
2349
+                searchbuf[sizeof(searchbuf)-1]='\0';
2350
+                lenprefix=strlen(searchbuf);
2351
+                fp=NULL;
2352
+                if(redata_linecol2pos(re->data, re->curline, re->curcol,&tmppos,NULL)==0
2353
+                  && re_extractword2buf(re, re->data, tmppos, searchbuf+lenprefix, sizeof(searchbuf)-lenprefix, &usedsearchbuf)==0
2354
+                  && strncpy(title,searchbuf,sizeof(title))!=NULL
2355
+                  && (title[sizeof(title)-1]='\0')=='\0'
2356
+                  && (fp=popen(searchbuf,"r"))!=NULL
2357
+                  && (nread=fread(searchbuf,1,sizeof(searchbuf),fp))>0
2358
+                ) {
2359
+                        if((re->prints[i].ui=reui_init(DEFAULTFONTHEIGHT*re->fontheightpercent/100,re->ui))==NULL
2360
+                          || (re->prints[i].data=redata_init(redata_highlighter_register,NULL))==NULL) {
2361
+                                if(re->prints[i].ui!=NULL)
2362
+                                        reui_free(re->prints[i].ui),re->prints[i].ui=NULL;
2363
+                                return(-1); /* couldn't init data store */
2364
+                        }
2365
+                        re->usedprints++;
2366
+                        reui_title(re->prints[i].ui,title);
2367
+                        redata_op_add(re->prints[i].data,redata_getused(re->prints[i].data),searchbuf,nread,NULL);
2368
+                        while((nread=fread(searchbuf,1,sizeof(searchbuf),fp))>0) {
2369
+                                redata_op_add(re->prints[i].data,redata_getused(re->prints[i].data),searchbuf,nread,NULL);
2370
+                        }
2371
+                        pclose(fp),fp=NULL;
2372
+                        re->contentsdirty=1;
2373
+                } else {
2374
+                        if(fp!=NULL)
2375
+                                pclose(fp),fp=NULL;
2376
+                        re_delprint(re,i);
2377
+                        re->command=COMMAND_WARNING;
2378
+                        snprintf(re->commandbuf,sizeof(re->commandbuf),"Couldn't show manpage.");
2379
+                        re->commandbuf[sizeof(re->commandbuf)-1]='\0';
2380
+                        re->headerdirty=1;
2381
+                        return(0);
2382
+                }
2357 2383
         } else {
2358 2384
                 char mytitle[256];
2385
+                /* setup window */
2386
+                if((re->prints[i].ui=reui_init(DEFAULTFONTHEIGHT*re->fontheightpercent/100,re->ui))==NULL)
2387
+                        return(-1); /* couldn't init window */
2388
+                /* setup title */
2359 2389
                 snprintf(mytitle,sizeof(mytitle),"view %s",re->filename);
2360 2390
                 mytitle[sizeof(mytitle)-1]='\0';
2361 2391
                 reui_title(re->prints[i].ui,mytitle);
... ...
@@ -2408,6 +2438,39 @@ re_rtrim(re_t *re, long curpos, int *trimmed)
2408 2438
         return(0);
2409 2439
 }
2410 2440
 
2441
+int
2442
+re_extractword2buf(re_t *re, redata_t *data, long pos, char *buf, int sizebuf, int *usedbuf)
2443
+{
2444
+        char utfchar[5];
2445
+        int usedutfchar;
2446
+        long startpos,curpos,endpos;
2447
+        int c;
2448
+        if(re==NULL || data==NULL || pos<0)
2449
+                return(-1); /* sanity check failed */
2450
+        /* search start of word */
2451
+        for(curpos=startpos=pos;redata_getprevutf8char(re->data,curpos,utfchar,sizeof(utfchar),&usedutfchar)==0;startpos=curpos) {
2452
+                curpos-=usedutfchar;
2453
+                if(usedutfchar==1) {
2454
+                        c=utfchar[0];
2455
+                        if(!((c>='0' && c<='9') || (c>='a' && c<='z') || (c>='A' && c<='Z') || c=='_'))
2456
+                                break;
2457
+                }
2458
+        }
2459
+        /* search end of word */
2460
+        for(curpos=endpos=pos;redata_getutf8char(re->data,curpos,utfchar,sizeof(utfchar),&usedutfchar)==0;endpos=curpos) {
2461
+                curpos+=usedutfchar;
2462
+                if(usedutfchar==1) {
2463
+                        c=utfchar[0];
2464
+                        if(!((c>='0' && c<='9') || (c>='a' && c<='z') || (c>='A' && c<='Z') || c=='_'))
2465
+                                break;
2466
+                }
2467
+        }
2468
+        if(redata_getsubstr(re->data,startpos,endpos,buf,sizebuf-1,usedbuf)!=0)
2469
+                return(-1);
2470
+        buf[*usedbuf]='\0';
2471
+        return(0);
2472
+}
2473
+
2411 2474
 long
2412 2475
 re_getmatchingbracket(re_t *re,long posini, char originalchar, char *matchingchar)
2413 2476
 {
... ...
@@ -2612,6 +2675,7 @@ re_drawcontents(re_t *re, printout_t *printout)
2612 2675
         showonlyn=0;
2613 2676
         flaglineno=(printout==NULL)?re->showlinenumbers:
2614 2677
                    (printout->type==printoutHelp)?0:
2678
+                   (printout->type==printoutManpage)?0:
2615 2679
                    1;
2616 2680
         linenosize=linenowidth=0;
2617 2681
         if(printout==NULL && re->viewonly==1) {