home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 133_01 / e8 < prev    next >
Text File  |  1985-03-10  |  4KB  |  213 lines

  1.  /*
  2.     e screen editor
  3.  
  4.     (C) G. Nigel Gilbert, MICROLOGY, 1981
  5.  
  6.     August-December 1981
  7.  
  8.     FILE: e8
  9.  
  10.     FUNCTIONS: loc,gettext,getline,inject,deltp,puttext,storetext,
  11.             readtext,getc,opentext,balloc,error,addhistory
  12.  
  13.     PURPOSE: get and put text lines into and out of storage
  14.  
  15. */
  16.  
  17. #include "e.h"
  18.  
  19. loc(line,move)    /*returns line+move, adjusted to be within text*/
  20. int line, move;
  21. {
  22.     int y;
  23.  
  24.     if ( (y=line+move) < 1) y=1;
  25.     if (lastl == UNKNOWN && y > lastread) readtext(y);
  26.     if (y > lastl) return lastl;
  27.     return y;
  28. }
  29.  
  30. gettext(line)    /*makes 'line' the current line*/
  31. int line;
  32. {
  33.     char *getline();
  34.  
  35.     strcpy(text,getline(line));
  36.     cline=line;
  37. }
  38.  
  39. char *getline(line)    /*returns address of text of line 'line' and updates
  40.                 page usage*/
  41. int line;
  42. {
  43.     struct addr *tptr;
  44.     int slot;
  45.  
  46.     tptr=tp[loc(line,0)];
  47.     if ( (slot=pageloc[tptr->page]) <= 0)
  48.         slot=swappin(tptr->page);
  49.     if (usage[slot] >= INUSE) usage[slot]=++clock;
  50.     return slotaddr[slot] + tptr->moffset;
  51. }
  52.  
  53. inject(line,txt)    /* inserts 'txt' after 'line' */
  54. int line;
  55. char *txt;
  56. {
  57.     int l;
  58.     struct addr *tptr;
  59.  
  60.     addhistory(line,HISTINSERT);
  61.     if ( &tp[lastread+2] >= slotaddr[tppages]) {
  62.         /*need another slot to store tp's in */
  63.         if (tppages+2 == slotsinmem) {
  64.             error("Too many lines of text");
  65.             return FAIL;
  66.             }
  67.         if (usage[tppages] == NOTAVAIL) /*ie page is being alloc'ed into*/
  68.             allocp=(PAGESIZE+1); /*force alloc into new page*/
  69.         if (usage[tppages] != FREE) swapout(tppages);
  70.         usage[tppages++]=NOTAVAIL;
  71.         }
  72.     tptr=tp[++line];
  73.     if (line < ++lastread)
  74.         movmem(tptr,tptr+1,(lastread-line)*sizeof(*tptr));
  75.     storetext(tptr,txt);
  76.     if (lastl != UNKNOWN)lastl++;
  77.     return line;
  78. }
  79.  
  80. delete(from)    /*deletes a line by shifting pointers*/
  81. int from;
  82. {
  83.     struct addr *tptr;
  84.  
  85.     addhistory(from,HISTDELETE);
  86.     tptr=tp[from];
  87.     movmem(tptr+1,tptr,(lastread-from)*sizeof(*tptr));
  88.     if (lastl != UNKNOWN) lastl--;
  89.     lastread--;
  90.     if (lastl < 1)lastl=1;
  91.     if (lastread < 1)lastread=1;
  92. }
  93.  
  94. puttext()    /*replaces cline's text if it has been altered*/
  95. {
  96.     if (altered) {
  97.         addhistory(cline,HISTREPLACE);
  98.         altered=NO;
  99.         storetext(tp[cline],text);
  100.         }
  101. }
  102.  
  103. storetext(tpptr,textptr)     /*stores text at textptr and updates tp record*/
  104. struct addr *tpptr;
  105. char *textptr;
  106. {
  107.     int p, cp, balloc();
  108.  
  109.  
  110.     cp=strlen(textptr);
  111.     
  112.     if (!trail) {
  113.         while (--cp >= 0 && isspace(textptr[cp]));
  114.         textptr[++cp]='\0';
  115.         }
  116.         
  117.     tpptr->moffset=p=balloc(1+cp);
  118.     tpptr->page=newpage;
  119.     strcpy(p+npaddr,textptr);
  120. }
  121.  
  122. readtext(line)    /*reads file being edited into virtual memory until 'line'
  123.           is read, or eof.  If eof, sets lastl to number of last line
  124.           read.  File is assumed to be already open*/
  125. int line;
  126. {
  127.     char txt[LLIM], *t;
  128.     int i, c, l;
  129.  
  130.     storehist=NO;
  131.     while (lastread < line) {
  132.         for (i=0, t=&txt[0]; i < LLIM && ((c=getc(textbuf)) >= ' ' ||
  133.             c != '\n' && c != FAIL && c != ENDFILE); i++) {
  134.                     if (c) *(t++)=c;
  135.                 else i--;
  136.                 }
  137.         if (txt[i-1] == '\r') t--; 
  138.         *t='\0';
  139.         if ( (l=inject(lastread,txt)) == FAIL) goto toomuch;
  140.         else if ((lastread=l)%100 == 0) putlineno(lastread);
  141.         if ( (goteof= c == ENDFILE || c == FAIL) )  goto eof;
  142.         checkkey();
  143.         }
  144.     goto ok;
  145. eof: 
  146.     fclose(textbuf);
  147. toomuch: 
  148.     lastl=lastread;
  149. ok:    storehist=YES;
  150. }
  151.  
  152. opentext(name)    /*open the file being edited for reading*/
  153. char *name;
  154. {
  155.     if (fopen(name,textbuf) == FAIL) {
  156.         error("Can't open file");
  157.         lastl=1;
  158.         return FAIL;
  159.         }
  160.     return YES;
  161. }
  162.  
  163. balloc(n)    /*allocate and return the offset in newpage of a vector size n*/
  164. unsigned n;
  165. {
  166.     sint slot;
  167.  
  168.     if (allocp  + n >= PAGESIZE) {
  169.         /*no room in current newpage; get another*/
  170.         if (pageloc[newpage] > 0)usage[pageloc[newpage]]=INUSE;
  171.         pageloc[++newpage]=slot=freememslot();
  172.         usage[slot]=NOTAVAIL;
  173.         allocp=0; 
  174.         npaddr=slotaddr[slot];
  175.         }
  176.     allocp+=n;
  177.     return allocp-n;
  178. }
  179.  
  180. error(message)
  181. char *message;
  182. {
  183.     if (errmess != NULL) return;
  184.     gotoxy(32,0);
  185.     puts(errmess=message);
  186.     resetcursor();
  187.     inbufp=0;
  188. }
  189.  
  190. addhistory(l,type)
  191. int l;
  192. sint type;
  193. {
  194.     struct histstep *step;
  195.  
  196.     if(!storehist) return;
  197.  
  198.     step=history[histptr];
  199.     if (++histptr == HISTLEN) histptr=0;
  200.     if (histcnt < HISTLEN) histcnt++;
  201.  
  202.     step->histp.page=tp[l].page;
  203.     step->histp.moffset=tp[l].moffset;
  204.     step->histline=l;
  205.     step->histtype=type;
  206.     step->histcomm=ncommand;
  207. }
  208. strlen(textptr);
  209.     
  210.     if (!trail) {
  211.         while (--cp >= 0 && isspace(textptr[cp]));
  212.         textptr[++cp]='\0';
  213.         }