home *** CD-ROM | disk | FTP | other *** search
/ Photo CD Demo 1 / Demo.bin / gle / gle / edt.c < prev    next >
C/C++ Source or Header  |  1992-11-29  |  22KB  |  1,095 lines

  1. #include "all.h"
  2. #include <errno.h>
  3. #include <math.h>
  4. #ifdef __TURBOC__
  5. #define HLINECHAR 205
  6. #include "bios.h"
  7. #include "conio.h"
  8. #else
  9. #define ENOFILE 999
  10. #define HLINECHAR '-'
  11. #include "vaxconio.h"
  12. #endif
  13. #include "edt.h"
  14. int vax_edt(char *s);
  15. int delay(int i);
  16. extern int gle_debug;
  17. int gle_speed;
  18. int gle_nspeed;
  19. int trace_on;
  20. #define true (!false)
  21. #define false 0
  22. #define dbg if (gle_debug>0)
  23. long mem_total(void);
  24. int pick_file(char *r, char *w);
  25. typedef char (*TEXT)[];
  26. int scr_blackwhite;
  27. int scr_left(int i);
  28. int scr_right(int i);
  29. int scr_norm(void);
  30. int scr_inv(void);
  31. int scr_grey(void);
  32. int text_search(void);
  33. int text_deleol(void);
  34. int text_undeleol(void);
  35. int scr_savexy(void);
  36. int scr_restorexy(void);
  37. int scr_gets(char *s);
  38. int text_showerror(void);
  39. int text_findnext(void);
  40. int text_changed(void);
  41. int read_command(char *ans, char *ques);
  42. int read_str(char *s);
  43. int text_load(char *fname);
  44. int text_free(void);
  45. int do_help(char *s);
  46. int int_edt(char *fname);
  47. int vinsert(int y, char *s);
  48. int vdelete(int y);
  49. int menu(void);
  50. int gle_redraw(void);
  51. int scrinsert(int y);
  52. int scrdeleteline(int y);
  53. int text_save(void) ;
  54. int text_eol(void);
  55. int text_delright(void);
  56. int text_delete(void);
  57. int text_undelete(void);
  58. int text_deleteline(void);
  59. int text_left(void);
  60. int text_right(void);
  61. int text_up(void);
  62. int text_down(void);
  63. int text_return(void);
  64. int text_refresh();
  65. int text_putstr(char *s);
  66. int text_setwindow();
  67. int text_saveas(void);
  68. int text_move(int x,int y);
  69. int scr_tab(char *s, int x);
  70. int scr_negtab(char *s, int x, int *fpos, int *scrx);
  71. int fner(char *s);
  72. int fner_clear(void);
  73. int text_setwindow(void);
  74. int text_refresh(void);
  75. int text_main(void);
  76. int text_inkey(void);
  77. int text_scroll(void);
  78. int text_scroll_up(void);
  79. int text_scroll_down(void);
  80. int text_select(void);
  81. int text_cut(void);
  82. int text_paste(void);
  83. int restofline(int y,int x);
  84. int normal_key(int c);
  85. int lineset(int i,char *ss);
  86. int lineinsert(int y, char *ss);
  87. int textinsert(int y);
  88. char *sline(int i);
  89. char *line(int i);
  90. int ncpy(char *d, char *s, int n);
  91. /*------------ GLOBAL VARIABLES --------------*/
  92. extern long gtotalmem;
  93. int max_ngtxt;
  94. extern int ngtxt;
  95. extern char *(*gtxt)[];   /* gtxt is a pointer to an array of poiter to char */
  96. char *(*ctxt)[];    /* cut buffer */
  97. int ncut;
  98. int iserr;
  99. int w_top=0,w_bot=0,w_len,w_cl,w_cx;
  100. #define MAXLINES (max_ngtxt-10)
  101. extern int ngtxt;
  102. int move_pos,onmove;
  103. int ii;
  104. char gbuff[255];
  105. char gbuff2[255];
  106. int selx,sely;
  107. extern char input_file[];
  108. int changed;
  109. int scr_isblackwhite(void);
  110. extern int netxt;    /* errors which occured */
  111. extern char *(*etxt)[];
  112. /*---------------------------------------------------------------------------*/
  113. /* copy gtxt, ngtxt   to local paramters */
  114. int tfx,tfy;
  115. extern int gotfile;
  116. int_edt(char *fname)
  117. {
  118.     int i,j;
  119.  
  120.     tfx = 1;
  121.     tfy = 1;
  122.     w_len = 20;
  123.     scr_blackwhite = scr_isblackwhite();
  124.     if (!gotfile) {
  125.         { char infile[80]; char wildcard[80];
  126.         strcpy(wildcard,"*.gle");
  127.         if (pick_file(infile,wildcard)) strcpy(infile,"test.gle");
  128.         text_load(infile);
  129.         }
  130.     }
  131.     gotfile=true;
  132.     text_setwindow();
  133.     text_refresh();
  134.     text_main();
  135. }
  136. vinsert(int y,char *ss)
  137. {
  138.     int i;
  139.     if (ngtxt>(max_ngtxt-50)) text_expand(2*(ngtxt+50));
  140.     if (ngtxt>MAXLINES) {fner("too many lines in file "); return;}
  141.     ngtxt++;
  142.     for (i=ngtxt;i>y;i--)
  143.         (*gtxt)[i] = (*gtxt)[i-1];
  144.     (*gtxt)[y] = sdup(ss);
  145. }
  146. lineinsert(int y,char *ss)
  147. {
  148.     vinsert(y,ss);
  149.     scrinsert(y);
  150. }
  151.  
  152. vdelete(int y)
  153. {
  154.     int i;
  155.     if (ngtxt==0) {fner("too few lines in file "); return;}
  156.     ngtxt--;
  157.     myfrees((*gtxt)[y],"vdelete");
  158.     for (i=y;i<=ngtxt;i++)
  159.         (*gtxt)[i] = (*gtxt)[i+1];
  160. }
  161. linedelete(int y)
  162. {
  163.     vdelete(y);
  164.     scrdeleteline(y);
  165. }
  166. lineset(int i,char *ss)
  167. {
  168.     char *zzz;
  169.     int same;
  170.     char *d=(*gtxt)[i];
  171.     char *s=ss;
  172.     if (d==0) gle_abort("linset zero\n");
  173.     for (;*d!=0 && *s!=0 && *d==*s ;s++,d++) ;
  174.     same = s-ss;
  175.     myfrees((*gtxt)[i],"Linset");
  176.     zzz = sdup(ss);
  177.     (*gtxt)[i] = zzz;
  178.     restofline(i,same);
  179. }
  180. char *line(int i)
  181. {
  182.     static char xxx[2]="";
  183.     static char eob[7]="[EOB]";
  184.     if (i>ngtxt) {
  185.         if (i==ngtxt+1) return &eob[0];
  186.         else return &xxx[0];
  187.     }
  188.     return (*gtxt)[i];
  189. }
  190. text_free(void)
  191. {
  192.     int i;
  193.     for (i=1;i<=ngtxt;i++) myfrees( (*gtxt)[i] ,"textfree");
  194.     ngtxt = 0;
  195. }
  196. char *tabtospace(char *s);
  197. char *sline(int i)
  198. {
  199.     char *s;
  200.     s = tabtospace(line(i));
  201.     if (strlen(s)>77) {s[77] = '>'; s[78] = 0;}
  202.     return s;
  203. }
  204. char *tabtospace(char *s)
  205. {
  206.     static char buf[255];
  207.     char *o = &buf[0];
  208.     int p=0,k,df;
  209.     for (;*s!=0;s++) {
  210.         if (*s==9) {
  211.             df = (p/8)*8+8-p;
  212.             for (k=1;k<=df;k++) {
  213.                 *o++ = ' ';
  214.                 p++;
  215.             }
  216.         } else {
  217.             *o++ = *s;
  218.             p++;
  219.         }
  220.     }
  221.     *o = 0;
  222.     return &buf[0];
  223. }
  224. text_main()
  225. {
  226.     int c,doask;
  227.     for (;;) {
  228.      doask = true;
  229.      c = text_inkey();
  230.      if (iserr) fner_clear();
  231.      switch (c) {
  232.        case eexitnow:
  233.         doask = false;
  234.        case eescape:
  235.        case equit: /* control c */
  236.         if (text_changed()) break;
  237.         if (doask) {
  238.           fner("Exit from cgle ? (Y,N)  [y]");
  239.           c = text_inkey();
  240.           fner_clear();
  241.           if (tolower(c)=='n') break;
  242.           if (tolower(c)!='y' && c != ereturn) break;
  243.         }
  244.         window(1,1,80,25);
  245.         scr_norm();
  246.         clrscr();
  247.         return;
  248.        case eedt: /* call edt editor */
  249.         text_save();
  250.         clrscr();
  251.         fner("Entering VAX EDT");
  252.         scr_refresh();
  253.         vax_edt(input_file);
  254.         text_free();
  255.         text_load(input_file);
  256.         text_setwindow();
  257.         text_refresh();
  258.         break;
  259.        case efast:
  260.         gle_speed++;
  261.         if (gle_speed>=gle_nspeed) gle_speed = 0;
  262.         sprintf(gbuff,"Speed of display set to {%d}",gle_speed);
  263.         fner(gbuff);
  264.         break;
  265.        case eleft: /* left */
  266.         text_left();
  267.         break;
  268.        case eshell: /* shell, start a subprocess */
  269.         window(1,1,80,25); scr_norm(); clrscr();
  270.         printf("Type in EXIT to return to CGLE\n\n");
  271.         system("");
  272.         text_refresh();
  273.         break;
  274.        case etrace: /* trace */
  275.         fner("Trace is now ON (or off)");
  276.         if (trace_on) trace_on = false; else trace_on = true;
  277.         break;
  278.        case eright: /* right */
  279.         text_right();
  280.         break;
  281.        case eup: /* arrow up */
  282.         text_up();
  283.         break;
  284.        case edown: /* arrow down */
  285.         text_down();
  286.         break;
  287.        case epageup:
  288.         tfy = tfy - 15;
  289.         if (tfy<1) {fner("Backup past top of file"); tfy = 1;}
  290.         text_move(tfx,tfy);
  291.         break;
  292.        case epagedown:
  293.         tfy = tfy + 15;
  294.         if (tfy>ngtxt) {fner("Advance past end of file"); tfy = ngtxt;}
  295.         text_move(tfx,tfy);
  296.         break;
  297.        case edelright:
  298.         changed = true;
  299.         text_right();
  300.         text_delete();
  301.         break;
  302.        case edeleol:
  303.         changed = true;
  304.         text_deleol();
  305.         break;
  306.        case eundeleol:
  307.         changed = true;
  308.         text_undeleol();
  309.         break;
  310.        case edelline:
  311.         changed = true;
  312.         text_deleteline();
  313.         break;
  314.       case edrawit:
  315.         window(1,1,79,24);
  316.         clrscr();
  317.         gotoxy(1,1);
  318.         scr_refresh();
  319.         gle_redraw();
  320.         if (netxt>0) text_showerror();
  321.         text_refresh();
  322.         break;
  323.       case eshowerror:
  324.         text_showerror();
  325.         text_refresh();
  326.         break;
  327.       case eundelline:
  328.         changed = true;
  329.         text_undelete();
  330.         break;
  331.       case eselect:
  332.         text_select();
  333.         break;
  334.       case ehelp: /* help */
  335.         do_help2("gle");
  336.         text_refresh();
  337.         break;
  338.       case esaveas:
  339.         text_saveas();
  340.         text_refresh();
  341.         break;
  342.       case esave: /* save file */
  343.         text_save();
  344.         changed = false;
  345.         break;
  346.       case efindnext:
  347.         text_findnext();
  348.         break;
  349.       case esearch: /* search for string */
  350.         text_search();
  351.         break;
  352.       case egraphmenu: /* graph mode  */
  353.         changed = true;
  354.         menu();
  355.         text_refresh();
  356.         break;
  357.       case eload:
  358.         { char infile[80]; char wildcard[80];
  359.         if (text_changed()) break;
  360.         if (read_command(infile,"Name of file to load <return for menu> ")) break;
  361.         strcpy(wildcard,infile);
  362.         if (strlen(infile)==0) strcpy(wildcard,"*.gle");
  363.         if (strchr(wildcard,'*')!=NULL)
  364.             if (pick_file(infile,wildcard)) goto dontload;
  365.         text_free();
  366.         text_load(infile);
  367.         }
  368.         text_setwindow();
  369. dontload:    text_refresh();
  370.         break;
  371.       case ecut: /* control K , delete line, or block */
  372.         changed = true;
  373.         text_cut();
  374.         break;
  375.       case ebol:
  376.         text_move(1,tfy);
  377.         break;
  378.       case eeol:
  379.         text_move(strlen(line(tfy))+1,tfy);
  380.         break;
  381.       case eendoffile:
  382.         text_move(strlen(line(ngtxt))+1,ngtxt);
  383.         break;
  384.       case etopoffile:
  385.         text_move(1,1);
  386.         break;
  387.       case epaste: /* control Y , Yank line back, or block */
  388.         changed = true;
  389.         text_paste();
  390.         break;
  391.       case ereturn: /* carriage return */
  392.         changed = true;
  393.         text_return();
  394.         break;
  395.       case edelete: /* delete */
  396.         changed = true;
  397.         text_delete();
  398.         break;
  399.       default: /* normal key */
  400.         changed = true;
  401.         onmove = false;
  402.         if (c<26  && c!=9) {fner("Key has no affect"); text_move(tfx,tfy); break;}
  403.         if (c>200)  {fner("Unimplemented command"); text_move(tfx,tfy);}
  404.         else normal_key(c);
  405.         break;
  406.      }
  407.     }
  408. }
  409. text_changed()
  410. {
  411.     int c;
  412.     if (!changed) return false;
  413.     for (;;) {
  414.         fner("Save changes ? (Y,N)");
  415.         c = text_inkey();
  416.         fner_clear();
  417.         if (c==eescape) return true;
  418.         if (tolower(c)=='n') return false;
  419.         if (tolower(c)=='y') {text_save(); return false;}
  420.     }
  421. }
  422. int text_expand(int n);
  423. text_expand(int n)
  424. {
  425.     char **a;
  426.     max_ngtxt = n;
  427.     a = myallocz((n+1)*sizeof(char *));
  428.     if (gtxt!=NULL) {
  429.         memcpy(a,gtxt,(ngtxt+1)*sizeof(char *));
  430.         myfrees(gtxt,"textexpand");
  431.     }
  432.     gtxt = (char *(*)[]) a;
  433. }
  434. text_load(char *fname)
  435. {
  436. static char inbuff[200];
  437. FILE *fptr;
  438. char *s;
  439. int i;
  440. char emess[80];
  441.     changed = false;
  442.     strcpy(input_file,fname);
  443.     s = strchr(input_file,'.');
  444.     if (s==NULL) strcat(input_file,".gle");
  445.     fptr = fopen(input_file,"r");
  446.     if (fptr==NULL) {
  447.         if (errno == ENOFILE) {
  448.         sprintf(emess,"Input file does not exist (%s) ",input_file);
  449.         fner(emess);
  450.         delay(1000);
  451.         } else {
  452.             gprint("Unable to open input file {%s} \n",input_file);
  453.             perror("Reason");
  454.             fner("Press return to continue");
  455.             text_inkey();
  456.         }
  457.     } else {
  458.      for (;!feof(fptr);) {
  459.         if (fgets(inbuff,200,fptr)!=NULL) {
  460.             i = strlen(inbuff);
  461.             if (inbuff[i-1]=='\n') inbuff[i-1] = 0;
  462.             ngtxt++;
  463.             if (ngtxt>(max_ngtxt-300)) {
  464.                 text_expand(2*(ngtxt+500));
  465.             }
  466.             (*gtxt)[ngtxt] = myalloc(i+1);
  467.             strcpy((*gtxt)[ngtxt],inbuff);
  468.         }
  469.      }
  470.      fclose(fptr);
  471.     }
  472.     if (ngtxt==0) (*gtxt)[++ngtxt] = sdup("size 24 18");
  473.     if (ngtxt==1) (*gtxt)[++ngtxt] = sdup("");
  474.     tfx = 1;
  475.     tfy = 1;
  476. }
  477. char errgle[90];
  478. int dont_clear;
  479. gle_abort(char *s)
  480. {
  481.     printf("%s",s); scr_refresh();
  482.     d_tidyup();
  483.     delay(1000);
  484.     dont_clear = true;
  485. #ifdef _TURBOC_
  486.     window(1,1,80,25);
  487.     scr_norm();
  488.     clrscr();
  489. #endif
  490.     printf("\n\n\n\n %s \nGle is aborting, Attempting to save file in abort.gle\n",s);
  491.     strcpy(input_file,"abort.gle");
  492.     text_save();
  493.     printf("File is now saved (hopefully)\n");
  494.     if (strstr(s,"alloc")!=NULL  || strstr(s,"Init")!=NULL) {
  495.         printf("\nGLE is short of memory!!\n\n");
  496.         printf("If you are using very large datasets you should try\n");
  497.         printf("the BIGFILE option, otherwise you probably have too\n");
  498.         printf("many memory resident programs on your pc and to make GLE work\n");
  499.         printf("you should remove these from your CONFIG.SYS and AUTOEXEC.BAT\n");
  500.         printf("\n  (make copies of these files before altering them)\n");
  501.     }
  502.     scr_refresh();
  503.     scr_end();
  504.     exit(1);
  505. }
  506. text_save()
  507. {
  508. static int tver=0;
  509. char tbuf[15];
  510. FILE *fptr;
  511. int i;
  512.     changed = false;
  513.     fner("Saving file");
  514. #ifdef __TURBOC__
  515.     if (input_file[1] == ':') setdisk(toupper(input_file[0])-'A');
  516. #endif
  517.     tver++; if (tver==5) tver=1;
  518.     sprintf(tbuf,"gletmp.t%d",tver);
  519.     unlink(tbuf);
  520.     unlink("glegle.tmp");
  521. #ifdef VAXC
  522. #include <file.h>
  523. {
  524. int file_desc;
  525.     file_desc = open("glegle.tmp",O_WRONLY | O_CREAT | O_TRUNC ,0,"rat=cr","rfm=var");
  526.     if (file_desc==-1) { gprint("Open glegle.tmp error \n");  return;}
  527.     fptr = fdopen(file_desc,"w");
  528. }
  529. #else
  530.     fptr = fopen("glegle.tmp","w");
  531. #endif
  532.     if (fptr==NULL) perror("Unable to open output file GLEGLE.TMP");
  533.     for (i=1;i<=ngtxt;i++) {
  534.         if (fputs(line(i),fptr)==EOF
  535.             && i<ngtxt &&  (strlen(line(i)) > 0) )
  536.             printf("Unable to write line %d {%s} \n",i,line(i));
  537.         fputs("\n",fptr);
  538.     }
  539.     if (fclose(fptr)!=0) perror("Unable to close output file GLEGLE.TMP");
  540. #ifndef VMS
  541.     if (rename(input_file,tbuf)!=0) ; /*
  542.         perror("Unable to rename input file to gletmp.t*"); */
  543. #endif
  544.     if (rename("glegle.tmp",input_file)!=0) perror("Unable to rename GLEGLE.TMP to output file");
  545.     fner_clear();
  546. }
  547. text_bol()
  548. {
  549.     tfx = 1;
  550. }
  551. text_eol()
  552. {
  553.     tfx = strlen(line(tfy))+1;
  554.     text_move(tfx,tfy);
  555. }
  556. text_left()
  557. {
  558.     onmove = false;
  559.     if (tfx==1) {
  560.         if (tfy==1) return;
  561.         tfy--; tfx = strlen(line(tfy))+1;
  562.     } else tfx--;
  563.     text_move(tfx,tfy);
  564. }
  565. text_right()
  566. {
  567.     onmove = false;
  568.     if (tfx== 1+strlen(line(tfy)) ) {
  569.         if (tfy==ngtxt) return;
  570.         tfy++; tfx = 1;
  571.     } else tfx++;
  572.     text_move(tfx,tfy);
  573. }
  574. text_up()
  575. {
  576.     if (tfy==1) {fner("Backup past end of file"); text_move(tfx,tfy); return;}
  577.     if (!onmove) {
  578.         onmove = true;
  579.         move_pos = scr_tab(line(tfy),tfx);
  580.     }
  581.     tfy--;
  582.     scr_negtab(line(tfy),move_pos,&tfx,&ii);
  583.     text_move(tfx,tfy);
  584. }
  585. text_down()
  586. {
  587.     if (tfy==ngtxt) {fner("Advance past end of file"); tfy--;}
  588.     if (!onmove) {
  589.         onmove = true;
  590.         move_pos = scr_tab(line(tfy),tfx);
  591.     }
  592.     tfy++;
  593.     scr_negtab(line(tfy),move_pos,&tfx,&ii);
  594.  /*gotoxy(5,9);gprint("negtab line=%d m=%d f=%d \n",tfy,move_pos,tfx);*/
  595.     text_move(tfx,tfy);
  596. }
  597. text_return()
  598. {
  599.     strcpy(gbuff,line(tfy)+tfx-1);
  600.     ncpy(gbuff2,line(tfy),tfx-1);
  601.     lineset(tfy,gbuff2);
  602.     lineinsert(tfy+1,gbuff);
  603.     text_right();
  604. }
  605. char line_buff[255];
  606. text_undelete()
  607. {
  608.     lineinsert(tfy,line_buff);
  609.     text_move(tfx,tfy);
  610. }
  611. text_undeleol()
  612. {
  613.     strcpy(gbuff2,line(tfy)+tfx-1);
  614.     ncpy(gbuff,line(tfy),tfx-1);
  615.     strcat(gbuff,line_buff);
  616.     lineinsert(tfy,gbuff);
  617.     lineset(tfy+1,gbuff2);
  618.     text_move(tfx,tfy);
  619. }
  620. text_saveas()
  621. {
  622.     char *s;
  623.     if (read_command(input_file,"New gle file name ")) return;
  624.     s = strchr(input_file,'.');
  625.     if (s==NULL) strcat(input_file,".gle");
  626.     text_save();
  627. }
  628. char search_string[100];
  629. text_search()
  630. {
  631.     if (read_command(search_string,"Enter string to search for ")) return;
  632.     strlwr(search_string);
  633.     text_findnext();
  634. }
  635. text_findnext()
  636. {
  637.     int i;
  638.     char fline[100];
  639.     char *p;
  640.     for (i=tfy+1; i<=ngtxt; i++) {
  641.         ncpy(fline,line(i),90);
  642.         strlwr(fline);
  643.         p = strstr(fline,search_string);
  644.         if (p!=NULL) {
  645.             tfy = i; tfx = p - &fline[0] + 1;
  646.             text_move(tfx,tfy);
  647.             return;
  648.         }
  649.     }
  650.     fner("String was not found");
  651. }
  652. text_select()
  653. {
  654.     selx = tfx;    sely = tfy;
  655.     text_move(tfx,tfy);
  656. }
  657. int swapint(int *a,int *b);
  658. swapint(int *a, int *b)
  659. {
  660.     int c;
  661.     c = *a; *a = *b; *b = c;
  662. }
  663. text_cut()
  664. {
  665.     int i,x1=selx,y1=sely,x2=tfx,y2=tfy;
  666.     if (selx==0 && sely==0) return;
  667.     if (y1==y2)  if (x2<x1) swapint(&x1,&x2);
  668.     if (y2<y1) { swapint(&x1,&x2); swapint(&y1,&y2); }
  669.     if (ncut>0) {
  670.         myfrees(ctxt,"textcut");
  671.         for (i=1;i<=ncut;i++) myfree((*ctxt)[i]);
  672.     }
  673.     ctxt = myalloc((y2-y1+3)*4);
  674.     ncut = 1;
  675.     if (y1==y2) {
  676.         ncpy(gbuff,line(y1)+x1-1,x2-x1);
  677.         (*ctxt)[1] = sdup(gbuff);
  678.         ncpy(gbuff,line(y1),x1-1);
  679.         strcat(gbuff,line(y1)+x2-1);
  680.         lineset(y1,gbuff);
  681.         text_move(x1,y1);
  682.         return;
  683.     }
  684.     for (i=y1+1;i<y2;i++) (*ctxt)[++ncut] = sdup(line(i));
  685.     for (i=y1+1;i<y2;i++) linedelete(y1+1);
  686.     (*ctxt)[1] = sdup(line(y1)+x1-1);
  687.     ncpy(gbuff2,line(y1+1),x2-1);
  688.     (*ctxt)[++ncut] = sdup(gbuff2);
  689.     ncpy(gbuff,line(y1),x1-1);
  690.     strcat(gbuff,line(y1+1)+x2-1);
  691.     lineset(y1,gbuff);
  692.     linedelete(y1+1);
  693.     text_move(x1,y1);
  694. }
  695. text_paste()
  696. {
  697.     int i;
  698.     if (ncut==0) return;
  699.     if (ncut==1) {
  700.         ncpy(gbuff,line(tfy),tfx-1);
  701.         strcat(gbuff,(*ctxt)[1]);
  702.         strcat(gbuff,line(tfy)+tfx-1);
  703.         lineset(tfy,gbuff);
  704.         text_move(tfx+strlen((*ctxt)[1]),tfy);
  705.         return;
  706.     }
  707.     ncpy(gbuff,line(tfy),tfx-1);
  708.     strcat(gbuff,(*ctxt)[1]);
  709.     strcpy(gbuff2,(*ctxt)[ncut]);
  710.     strcat(gbuff2,line(tfy)+tfx-1);
  711.     lineset(tfy,gbuff);
  712.     lineinsert(tfy+1,gbuff2);
  713.     for (i=ncut-1;i>1;i--) lineinsert(tfy+1, (*ctxt)[i]);
  714.     text_move(strlen((*ctxt)[ncut])+1,tfy+ncut-1);
  715. }
  716. text_deleol()
  717. {
  718.     if (ngtxt+1==tfy) return;
  719.     strcpy(line_buff,line(tfy)+tfx-1);
  720.     ncpy(gbuff,line(tfy),tfx-1);
  721.     if (tfy<ngtxt) strcat(gbuff,line(tfy+1));
  722.     lineset(tfy,gbuff);
  723.     if (ngtxt<=tfy) {
  724.         gbuff[0] = 0;
  725.         lineinsert(tfy+1,gbuff);
  726.     } else     linedelete(tfy+1);
  727.     text_move(tfx,tfy);
  728. }
  729. text_deleteline()
  730. {
  731.     if (ngtxt==1) return;
  732.     strcpy(line_buff,line(tfy));
  733.     linedelete(tfy);
  734.     if (ngtxt<tfy) {
  735.         gbuff[0] = 0;
  736.         lineinsert(tfy,gbuff);
  737.     }
  738.     text_move(tfx,tfy);
  739. }
  740. text_delete()
  741. {
  742.     int oldx;
  743.     if (tfx==1) {
  744.         if (tfy==1) {fner("Backup past top of file"); return;}
  745.         text_left();
  746.         oldx = tfx;
  747.         strcpy(gbuff2,line(tfy));
  748.         text_deleteline();
  749.         strcat(gbuff2,line(tfy));
  750.         lineset(tfy,gbuff2);
  751.         tfx = oldx;
  752.         text_move(tfx,tfy);
  753.         return;
  754.     }
  755.     ncpy(gbuff,line(tfy),tfx-2);
  756.     strcat(gbuff,line(tfy)+tfx-1);
  757.     tfx--;
  758.     lineset(tfy,gbuff);
  759.     text_move(tfx,tfy);
  760. }
  761. text_setwindow()
  762. {
  763.     if (tfy>w_top-4 || tfy<w_bot+4) w_top = tfy - (w_len-1)/2;
  764.     if (w_top<1) w_top = 1;
  765.     w_bot = w_top + w_len - 1;
  766. }
  767. text_refresh()
  768. {
  769.     char doubleline[90];
  770.     int e,i;
  771.     iserr = true; /* so that function keys appear at bottom */
  772.     window(1,1,80,25);
  773.     scr_norm();
  774.     clrscr();
  775.     gotoxy(1,1); wprintf("Memory Used=%ld  Free=%ld   CGLE V3.3b  Current file={%s}"
  776.         ,mem_total(),coreleft(),input_file);
  777.  
  778.     memset(doubleline,HLINECHAR,80);
  779.     doubleline[78] = 0;
  780.     gotoxy(1,2); cputs(doubleline);
  781.  
  782.     fner_clear();
  783.     window(2,3,79,23);
  784.     gotoxy(1,1);
  785.     e = w_bot;
  786.     if (e>ngtxt) e = ngtxt;
  787.     for (i=w_top;i<=e;i++) {
  788.         gotoxy(1,i-w_top+1);
  789.         cputs( sline(i) );
  790.     }
  791.     if (e<w_bot) {
  792.         gotoxy(1,i-w_top+1);
  793.         cputs("[EOF]");
  794.     }
  795.     gotoxy(scr_tab(line(tfy),tfx), tfy - w_top + 1);
  796. }
  797. /*---------- move cursor to file coloumn x, line y */
  798. text_move(int x,int y)
  799. {
  800.     tfx = x;
  801.     tfy = y;
  802.     if (tfy>ngtxt) tfy = ngtxt;
  803.     if (tfy<1) tfy = 1;
  804. /*    if (tfx>79) tfx = 79; */
  805.     if (tfx>strlen(line(tfy))+1) tfx = strlen(line(tfy))+1;
  806.     if (tfx<1) tfx = 1;
  807.     if (tfy<(w_top+5) || tfy>(w_bot-5)) {
  808.         if (tfy<(w_top-25) || tfy>(w_bot+25)) {
  809.             text_setwindow();
  810.             text_refresh();
  811.         } else {
  812.             text_scroll();
  813.         }
  814.     }
  815.     gotoxy(scr_tab(line(tfy),tfx), tfy - w_top + 1);
  816. }
  817. restofline(int y,int x)
  818. {
  819.     int sx;
  820.     if (x <= strlen(line(y))) {
  821.         sx = scr_tab(line(y),x);
  822.         gotoxy(sx,y-w_top + 1);
  823.         clreol();
  824.         cputs(sline(y)+sx-1);
  825.         gotoxy(scr_tab(line(y),tfx), y - w_top + 1);
  826.     }
  827. }
  828. fixline(int y)
  829. {
  830.     gotoxy(1,y-w_top + 1);
  831.     clreol();
  832.     cputs(sline(y));
  833. }
  834. scrinsert(int y)
  835. {
  836.     gotoxy(1,y-w_top + 1);
  837.     insline();
  838.     cputs(sline(y)); clreol();
  839. }
  840. text_scroll()
  841. {
  842.     int n,i;
  843.     n = w_top - tfy + 5;
  844.     /* printf("n %d, w_top %d  tfy %d \n",n,w_top,tfy); */
  845.     if (w_top-n<1) n = w_top - 1;
  846.     if (n>0) {
  847.         for (i=0;i<n;i++) text_scroll_down();
  848.         return;
  849.     }
  850.     n = tfy - w_bot + 5;
  851.     if (w_bot+n>ngtxt) n = ngtxt - w_bot;
  852.     if (n>0) {
  853.         for (i=0;i<n;i++) text_scroll_up();
  854.         return;
  855.     }
  856. }
  857. /* ------------------------ Find screen position given x pos in string */
  858. scr_tab(char *s, int x)
  859. {
  860.     int p=0,i=0;
  861.     for (;*s!=0 && i<(x-1);i++,s++) {
  862.         if (*s==9)  p = (p/8)*8+8;
  863.         else       p++;
  864.     }
  865.     if (p>= 77) p = 77;
  866.     return ++p;
  867. }
  868. /*------------------------- Find file x position given x pos on screen */
  869. /* and sets scrx to the correct screen position (if in middle of tab) */
  870. scr_negtab(char *s, int x, int *fpos, int *scrx)
  871. {
  872.     int last_pos=0,i;
  873.     *fpos=0;
  874.     *scrx = 0;
  875.  
  876.     for (i=0;*s!=0;s++,i++) {
  877.         if (*s==9) *scrx = (*scrx/8)*8+8;
  878.         else (*scrx)++;
  879.         if ((*scrx+1)>x) {
  880.             *fpos = i+1;
  881.             *scrx = last_pos;
  882.             return;
  883.         }
  884.         last_pos = *scrx + 1;
  885.     }
  886.     if (*scrx==0) (*scrx)++;
  887.     *fpos = i+1;
  888.     if (i==0) *fpos = 1;
  889. }
  890. text_scroll_down()
  891. {
  892. #ifdef __TURBOC__
  893.     window(2,3,79,22);
  894. #endif
  895.     gotoxy(1,1);insline();
  896. #ifdef __TURBOC__
  897.     window(2,3,79,23);
  898. #endif
  899.     w_top--;w_bot--;
  900.     fixline(w_top);
  901. }
  902. text_scroll_up()
  903. {
  904.     char ss[90];
  905.     gotoxy(1,1); delline();
  906.     w_top++; w_bot++;
  907.     fixline(w_bot);
  908. }
  909. scrdeleteline(int y)
  910. {
  911.     gotoxy(1, y - w_top + 1);
  912.     delline();
  913.     fixline(w_bot);
  914. }
  915. extern int noscreenio;
  916. char *function_bar(void);
  917. fner_clear()
  918. {
  919.     if (noscreenio) return;
  920.     if (iserr==false) return;
  921.     if (dont_clear) return;
  922.     window(1,1,80,25);
  923.     gotoxy(1,25);
  924.     scr_grey();
  925.     clreol();
  926.     gotoxy(2,25);
  927.     cputs(function_bar());
  928.     scr_norm();
  929.     iserr = false;
  930.     window(2,3,79,23);
  931. }
  932. extern int noscreenio;
  933. fner(char *s)
  934. {
  935.     if (dont_clear) return;
  936.     if (noscreenio) {
  937.         printf("%s\n",s);
  938.         return;
  939.     }
  940.     scr_savexy();
  941.     iserr = true;
  942.     window(1,1,80,25);
  943.     gotoxy(1,25);
  944.     scr_inv();
  945.     clreol();
  946.     gotoxy(2,25);
  947.     cputs(s);
  948.     scr_norm();
  949.     scr_restorexy();
  950.     scr_refresh();
  951. }
  952. int read_command(char *ans,char *ques)
  953. {
  954.     int rr;
  955.     iserr = true;
  956.     window(1,1,80,25);
  957.     gotoxy(1,24);
  958.     scr_inv();
  959.     clreol();
  960.     gotoxy(2,24);
  961.     cputs(ques);
  962.     rr = read_str(ans);
  963.     gotoxy(1,24);
  964.     scr_norm();
  965.     clreol();
  966.     window(2,3,79,23);
  967.     text_move(tfx,tfy);
  968.     return rr;
  969. }
  970. int read_input(char *ans,char *ques)
  971. {
  972.     int rr;
  973.     iserr = true;
  974.     clreol();
  975.     cputs(ques);
  976.     rr = read_str(ans);
  977.     wprintf("\n");
  978.     return rr;
  979. }
  980.  
  981. int read_str(char *s)
  982. {
  983.     int c,cx=0;
  984.     char mbuff[80];
  985.     *s = 0;
  986.     for (;;) {
  987.      c = text_inkey();
  988.      switch (c) {
  989.        case eescape: /* ESCAPE */
  990.        case equit: /* control c */
  991.         return true;
  992.        case epageup:
  993.         return epageup;
  994.        case epagedown:
  995.         return epagedown;
  996.        case eleft: /* left */
  997.         if (cx <= 0) break;
  998.         cx--;
  999.         scr_left(1);
  1000.         break;
  1001.        case eright: /* right */
  1002.         if (cx >= strlen(s)) break;
  1003.         cx++;
  1004.         scr_right(1);
  1005.         break;
  1006.        case eup: /* arrow up */
  1007.        case edown: /* arrow down */
  1008.       case ehelp: /* f1 help */
  1009.         break;
  1010.       case ereturn: /* carriage return */
  1011.         return false;
  1012.       case edelete: /* delete */
  1013.         if (strlen(s)==0) break;
  1014.         if (cx<1) break;
  1015.         ncpy(mbuff,s,cx-1);
  1016.         strcat(mbuff,s + cx);
  1017.         strcpy(s,mbuff);
  1018.         cx--;
  1019.         scr_left(1);
  1020.         cputs(s + cx);
  1021.         putch(' ');
  1022.         scr_left(strlen(s+cx)+1);
  1023.         break;
  1024.       case eshowerror:
  1025.       case edrawit:
  1026.         break;
  1027.       default: /* normal key */
  1028.         if (c<26  && c!=9) {fner("Key has no affect"); break;}
  1029.         if (c>200)  fner("Unimplemented command");
  1030.         else {
  1031.             ncpy(mbuff,s,cx);
  1032.             mbuff[cx] = c; mbuff[cx+1] = 0;
  1033.             strcat(mbuff,s + cx);
  1034.             strcpy(s,mbuff);
  1035.             cputs(s + cx);
  1036.             cx++;
  1037.             scr_left(strlen(s+cx));
  1038.         }
  1039.         break;
  1040.        }
  1041.      }
  1042. }
  1043. normal_key(int c)
  1044. {
  1045.     static char lbuff[255];
  1046.     if (ngtxt+1==tfy) {
  1047.         text_up();
  1048.         text_eol();
  1049.         text_return();
  1050.     }
  1051.     ncpy(lbuff,line(tfy),tfx-1);
  1052.     lbuff[tfx-1] = c; lbuff[tfx]=0;
  1053.     strcat(lbuff,line(tfy)+tfx-1);
  1054.     tfx++;
  1055.     lineset(tfy,lbuff);
  1056. }
  1057. text_showerror()
  1058. {
  1059.     int i,n;
  1060.     if (netxt==0) {
  1061.         fner("There were no errors, press return");
  1062.         text_inkey();
  1063.         return;
  1064.     }
  1065.     clrscr();
  1066.     n = netxt;
  1067.     if (n>20) n = 17;
  1068.     for (i=1;i<=n;i++) {
  1069.         gotoxy(3,i);
  1070.         cputs( (*etxt)[i] );
  1071.     }
  1072.     gotoxy(1,20); cputs("Press any key to continue");
  1073.     text_inkey();
  1074. }
  1075. #ifndef unix
  1076. void wprintf(va_list arg_list, ...)
  1077. /* Prints to the window */
  1078. {
  1079.      va_list arg_ptr;
  1080.      char *format;
  1081.     char output[200];
  1082.  
  1083.      va_start(arg_ptr, arg_list);
  1084.      format = arg_list;
  1085.      vsprintf(output, format, arg_ptr);
  1086. #ifdef __TURBOC__
  1087.     printf(output);
  1088. #else
  1089.     cputs(output);
  1090. #endif
  1091. }
  1092. #endif
  1093.  
  1094.  
  1095.