home *** CD-ROM | disk | FTP | other *** search
Text File | 1991-08-22 | 28.4 KB | 2,019 lines |
- Path: wupost!decwrl!amdcad!pyramid!oliveb!olivea!samsung!crackers!m2c!wpi.WPI.EDU!rcarter
- From: rcarter@wpi.WPI.EDU (Randolph Carter (nee. Joseph H. Allen))
- Newsgroups: alt.sources
- Subject: J (Joe's Editor) - Part 2 of 3
- Message-ID: <1991Aug22.043404.193@wpi.WPI.EDU>
- Date: 22 Aug 91 04:34:04 GMT
- Sender: rcarter@wpi.WPI.EDU (Randolph Carter (nee. Joseph H. Allen))
- Organization: Kadath Tours, Inc.
- Lines: 2008
-
-
- -- Cut here; take text between this line and the last 'cut' line
- from+=helpwidth-width+1;
- break;
- }
- if(*from!=*too)
- {
- cpos(i,j);
- tputcc((unsigned char)(*too= *from));
- oxpos++;
- }
- from++;
- too++;
- }
- for(;j!=width-1;j++)
- {
- if(have) return;
- if(' '!=*too)
- {
- cpos(i,j);
- tputcc((unsigned char)(*too= ' '));
- oxpos++;
- }
- too++;
- }
- too++;
- }
- hupd=0;
- }
-
- int updall=0;
-
- dupdate()
- {
- int xp,yp;
-
- aflush();
- if(hupd) dupdatehelp();
- if(upd)
- {
- int total=height-wind;
- struct window *x;
- dupdate1(1);
- stwin(curwin);
- x=curwin;
- curwin=topwin;
- xp=xpos; yp=ypos;
- do
- {
- if((curbuf==x->buffer || updall) && curwin!=x)
- {
- if(total<=0) break;
- ldwin(curwin);
- total-=curwin->height;
- newy=1;
- dupdate1(0);
- stwin(curwin);
- }
- else if(curwin==x)
- total-=curwin->height;
- }
- while(curwin=curwin->next,curwin!=topwin);
- updall=0;
- ldwin(x);
- curwin=x;
- cpos(ypos=yp,xpos=xp);
- }
- }
-
- invalidate(line)
- {
- int x;
- for(x=0;x<width;x++) scrn[width*line+x]= -1;
- }
-
- int tattrib(c)
- unsigned char c;
- {
- int mmode=0;
- if(c>=128)
- {
- mmode|=INVERSE;
- c-=128;
- }
- if(c==127)
- {
- mmode|=UNDERLINE;
- c='?';
- }
- else if(c<32)
- {
- mmode|=UNDERLINE;
- c+='@';
- }
- attrib(mmode);
- return c;
- }
-
- tputcc(c)
- unsigned char c;
- {
- eputc(tattrib(c));
- }
-
- tputss(s)
- unsigned char *s;
- {
- while(*s) tputcc(*(s++));
- }
-
- int backup=0;
- FILE *handle;
- unsigned char gfnam[PATHSIZE];
-
- TXTSIZ bufsiz; /* Size of buffer */
- TXTPTR point; /* The point */
- TXTPTR buffer; /* The buffer */
- TXTPTR filend; /* First character not in buffer */
- TXTPTR hole; /* Beginning of hole */
- TXTPTR ehole; /* First character not in hole */
- int changed=0; /* Set when file has been changed */
-
- fmopen()
- {
- buffer=(unsigned char *)TXTMALLOC(bufsiz=HOLESIZE);
- point=buffer;
- hole=buffer;
- ehole=buffer+HOLESIZE;
- filend=ehole;
- changed=0;
- }
-
- fmexpand(amount)
- unsigned amount;
- {
- if(filend+amount-buffer>bufsiz)
- {
- unsigned char *old=buffer;
- buffer=(TXTPTR)TXTREALLOC(buffer,bufsiz=(filend+amount+HOLESIZE-buffer));
- point+=buffer-old;
- filend+=buffer-old;
- hole+=buffer-old;
- ehole+=buffer-old;
- }
- }
-
- fmhole()
- {
- if(point==hole) return;
- if(point==ehole)
- {
- point=hole;
- return;
- }
- if(point<hole)
- {
- bmove(ehole-(hole-point),point,hole-point);
- ehole-=(hole-point);
- hole=point;
- }
- else
- {
- bmove(hole,ehole,point-ehole);
- hole+=point-ehole;
- ehole=point;
- point=hole;
- }
- }
-
- fmbig(size)
- TXTSIZ size;
- {
- if(size>fmholesize())
- {
- size+=HOLESIZE;
- fmexpand(size);
- bmove(ehole+size,ehole,filend-ehole);
- ehole+=size;
- filend+=size;
- }
- }
-
- int fmfnl()
- {
- while(((point==hole)?(point=ehole):point)!=filend)
- if(*point==NL) return 1;
- else point++;
- return 0;
- }
-
- int fmrnl()
- {
- if(fmrc()==NL) return 1;
- while((point==ehole?point=hole:point)!=buffer)
- if(*(--point)==NL) return 1;
- return 0;
- }
-
-
- int nundorecs=0;
- struct undorec
- {
- struct undorec *next;
- TXTSIZ size;
- TXTSIZ where;
- struct buffer *buf;
- unsigned char *buffer;
- }
- *undorecs=0;
-
- fminsu(size)
- TXTSIZ size;
- {
- struct window *z;
- if(undorecs) undorecs->buf=0;
- if(curbuf==markbuf)
- {
- if(fmnote()<markb) markb+=size;
- if(fmnote()<marke) marke+=size;
- }
- z=topwin;
- do
- {
- if(z->buffer==curbuf)
- {
- if(z==curwin)
- {
- if(fmnote()<saddr) saddr+=size;
- }
- else
- {
- if(fmnote()<z->saddr) z->saddr+=size;
- if(fmnote()<z->cursor) z->cursor+=size;
- }
- }
- z=z->next;
- }
- while(z!=topwin);
- }
-
- struct undorec *undoptr=0;
- int undoflag=1;
-
- undo()
- {
- extend=0;
- if(undoptr)
- {
- undoflag=0;
- fmdel(undoptr->size);
- undoflag=1;
- undoptr=undoptr->next;
- if(!undoptr)
- {
- marke=markb;
- return;
- }
- }
- else undoptr=undorecs;
- fminss(undoptr->buffer,undoptr->size);
- markbuf=curbuf;
- markb=fmnote();
- marke=markb+undoptr->size;
- }
-
- redo()
- {
- extend=0;
- if(undoptr)
- {
- undoflag=0;
- fmdel(undoptr->size);
- undoflag=1;
- if(undoptr==undorecs)
- {
- undoptr=0;
- marke=markb;
- return;
- }
- else
- {
- struct undorec *p;
- for(p=undorecs;p->next!=undoptr;p=p->next);
- undoptr=p;
- }
- }
- else for(undoptr=undorecs;undoptr->next;undoptr=undoptr->next);
- fminss(undoptr->buffer,undoptr->size);
- markbuf=curbuf;
- markb=fmnote();
- marke=markb+undoptr->size;
- }
-
- fmdelu(size)
- TXTSIZ size;
- {
- struct window *z;
- struct undorec *it;
- if(undoflag)
- {
- if(undorecs)
- if(undorecs->buf==curbuf && (undorecs->where==fmnote()))
- {
- /* Add to end */
- undorecs->buffer=(unsigned char *)realloc(undorecs->buffer,
- undorecs->size+size);
- fmcpy(undorecs->buffer+undorecs->size,size);
- undorecs->size+=size;
- }
- else if(undorecs->buf==curbuf && (undorecs->where==fmnote()+size))
- {
- /* Add to beginning */
- undorecs->buffer=(unsigned char *)realloc(
- undorecs->buffer,undorecs->size+size);
- bbkwd(undorecs->buffer+size,undorecs->buffer,undorecs->size);
- fmcpy(undorecs->buffer,size);
- undorecs->size+=size;
- undorecs->where-=size;
- }
- else goto in;
- else
- {
- in:
- /* New record */
- it=(struct undorec *)malloc(sizeof(struct undorec));
- it->next=undorecs;
- undorecs=it;
- it->buf=curbuf;
- it->size=size;
- it->where=fmnote();
- it->buffer=(unsigned char *)malloc(size);
- fmcpy(it->buffer,size);
- ++nundorecs;
- if(nundorecs==20)
- {
- struct undorec *p;
- for(it=undorecs;it->next;p=it,it=it->next);
- free(it->buffer);
- free(it);
- p->next=0;
- }
- }
- }
- if(markbuf==curbuf)
- {
- if(fmnote()<markb) markb-=umin(size,markb-fmnote());
- if(fmnote()<marke) marke-=umin(size,marke-fmnote());
- }
- z=topwin;
- do
- {
- if(curbuf==z->buffer)
- {
- if(z==curwin)
- {
- if(fmnote()<saddr) saddr-=umin(size,saddr-fmnote());
- }
- else
- {
- if(fmnote()<z->saddr) z->saddr-=umin(size,z->saddr-fmnote());
- if(fmnote()<z->cursor) z->cursor-=umin(size,z->cursor-fmnote());
- }
- }
- z=z->next;
- }
- while(z!=topwin);
- }
-
- fmdel(x)
- TXTSIZ x;
- {
- fmhole();
- fmdelu(x);
- ehole+=x;
- changed=1;
- }
-
- fminss(string,size)
- unsigned char *string;
- unsigned size;
- {
- fminsu(size);
- fmhole();
- if(size>fmholesize()) fmbig(size);
- bmove(hole,string,size);
- hole+=size;
- changed=1;
- }
-
- fmcpy(string,size)
- unsigned char *string;
- {
- fmhole();
- bbkwd(string,ehole,size);
- }
-
- int fmcmp(string,size)
- unsigned char *string;
- int size;
- {
- unsigned char *x;
- if(point==hole) point=ehole;
- if(hole>point && hole<point+size && hole!=ehole)
- {
- if(fmcmp(string,hole-point)) return 1;
- else
- {
- x=point;
- point=ehole;
- if(fmcmp(string+(hole-x),size-(hole-x)))
- {
- point=x;
- return 1;
- }
- else
- {
- point=x;
- return 0;
- }
- }
- }
- else
- {
- x=point;
- do
- if(*(x++)!=*(string++)) return 1;
- while(--size);
- return 0;
- }
- }
-
- int tupp(c)
- unsigned char c;
- {
- if(c>='a' && c<='z') return c+'A'-'a';
- else return c;
- }
-
- int fmicmp(string,size)
- unsigned char *string;
- int size;
- {
- unsigned char *x;
- if(point==hole) point=ehole;
- if(hole>point && hole<point+size && hole!=ehole)
- {
- if(fmcmp(string,hole-point)) return 1;
- else
- {
- x=point;
- point=ehole;
- if(fmcmp(string+(hole-x),size-(hole-x)))
- {
- point=x;
- return 1;
- }
- else
- {
- point=x;
- return 0;
- }
- }
- }
- else
- {
- x=point;
- do
- if(tupp(*(x++))!=tupp(*(string++))) return 1;
- while(--size);
- return 0;
- }
- }
-
- int fmsave(file,size)
- FILE *file;
- TXTSIZ size;
- {
- if(!size) return 1;
- if(point==hole) point=ehole;
- if(hole>point && hole<point+size && hole!=ehole)
- {
- if(hole-point!=fwrite(point,1,hole-point,file)) return 0;
- if(size-(hole-point)!=fwrite(ehole,1,size-(hole-point),file)) return 0;
- return 1;
- }
- else
- return size==fwrite(point,1,size,file);
- }
-
- int fminsfil(file)
- FILE *file;
- {
- struct stat buf;
- TXTSIZ amount;
- fstat(fileno(file),&buf);
- if(buf.st_size==0) return 1;
- fminsu(buf.st_size);
- changed=1;
- fmhole();
- fmbig(buf.st_size);
- amount=fread(hole,1,buf.st_size,file);
- hole+=amount;
- return amount==buf.st_size;
- }
-
- int getl(prompt,dat)
- unsigned char *prompt;
- unsigned char *dat;
- {
- int ch,x;
- cpos(height-1,0);
- tattrib(' ');
- eputs(prompt);
- eputs(" (^C to abort): ");
- eputs(dat);
- eputs("\033[K");
- x=strlen(dat);
- oxpos=strlen(prompt)+x+21;
- while(1)
- {
- ch=anext();
- if(ch=='L'-'@')
- {
- ch= -1;
- break;
- }
- if(ch==13 || ch==10)
- {
- ch=1;
- break;
- }
- if(ch>=32 && ch<127)
- {
- dat[x+1]=0;
- dat[x++]=ch;
- tputcc(ch);
- oxpos++;
- continue;
- }
- if((ch==8 || ch==127) && x)
- {
- x--;
- dat[x]=0;
- eputs("\010 \010");
- oxpos--;
- continue;
- }
- if(ch==3)
- {
- ch=0;
- break;
- }
- }
- invalidate(height-1);
- return ch;
- }
-
- msg(ms)
- unsigned char *ms;
- {
- cpos(height-1,0);
- tattrib(' ');
- eputs(ms);
- eputc(' ');
- eputs("\033[K");
- oxpos=1+strlen(ms);
- if(ms[0]=='\033') oxpos-=7;
- eputs("\033[K");
- invalidate(height-1);
- anext();
- }
-
- int askyn(ms)
- unsigned char *ms;
- {
- int ch;
- cpos(height-1,0);
- tattrib(' ');
- eputs(ms);
- oxpos=strlen(ms);
- if(ms[0]=='\033') oxpos-=7;
- eputs("\033[K");
- invalidate(height-1);
- up:
- ch=anext();
- switch(ch)
- {
- case 'y':
- case 'n':
- eputc(ch);
- ch&=0x5f;
- break;
- case 'Y':
- case 'N':
- eputc(ch);
- break;
- case 3:
- ch= -1;
- break;
- default:
- goto up;
- }
- return ch;
- }
-
- int query(ms)
- unsigned char *ms;
- {
- cpos(height-1,0);
- tattrib(' ');
- eputs(ms);
- oxpos=strlen(ms);
- if(ms[0]=='\033') oxpos-=7;
- eputs("\033[K");
- invalidate(height-1);
- return anext();
- }
-
- int nquery(ms)
- unsigned char *ms;
- {
- cpos(height-1,0);
- tattrib(' ');
- eputs(ms);
- oxpos=strlen(ms);
- if(ms[0]=='\033') oxpos-=7;
- eputs("\033[K");
- cpos(ypos,xpos);
- invalidate(height-1);
- return anext();
- }
-
- imsg()
- {
- unsigned char s[PATHSIZE];
- tattrib(' ');
- if(omsg)
- {
- eputs(omsg);
- invalidate(1);
- }
- sprintf(s,"\033[%d;1H\033[7m** Joe's Editor version 0.0.0 (1991) ** \
- \033[m\033[2H",height);
- eputs(s);
- invalidate(height-1);
- upd=1;
- }
-
- int pic;
- int autoind;
- int overwrite;
- int wrap;
- int tabmagic;
- TXTSIZ rmargin;
-
- int options=0;
- unsigned char sstring[PATHSIZE];
- unsigned char rstring[PATHSIZE];
- int len;
-
- TXTSIZ markb=0;
- TXTSIZ marke=0;
-
- TXTSIZ added;
- TXTSIZ extend;
- int leave; /* set if editor should now exit */
-
- TXTSIZ getrcol()
- {
- TXTSIZ x,y;
- unsigned char ch;
- x=fmnote();
- if(fmnrnl()) fmgetc();
- y=0;
- while(fmnote()!=x)
- {
- ch=fmgetc();
- if(ch==TAB)
- while((++y)&7);
- else
- y++;
- }
- return y;
- }
-
- gocol(x)
- TXTSIZ x;
- {
- TXTSIZ y;
- int ch;
- if(fmnrnl()) fmgetc();
- extend=0;
- for(y=0;y!=x;y++)
- {
- if(fmeof()) goto dn;
- ch=fmgetc();
- if(ch==NL)
- {
- fmpoint(fmnote()-1);
- extend=x;
- return;
- }
- if(ch==TAB)
- {
- while((++y)&7)
- {
- if(y==x)
- {
- fmpoint(fmnote()-1);
- dn:
- extend=x;
- return;
- }
- }
- y--;
- }
- }
- }
-
- TXTSIZ calcs()
- {
- TXTSIZ y=0;
- if(fmnrnl()) fmgetc();
- extend=0;
- while(! (fmeof()?1:fmrc()==NL))
- if(fmrc()==' ')
- {
- ++y;
- fmgetc();
- }
- else if(fmrc()==TAB)
- {
- do ++y; while(y%TABWIDTH);
- fmgetc();
- }
- else break;
- return y;
- }
-
- unfill()
- {
- fmfnl();
- extend=0;
- while(fmnote())
- {
- unsigned char x=fmrgetc();
- if(x==' ' || x==TAB) fmdel(1);
- else
- {
- fmgetc();
- break;
- }
- }
- }
-
- /* Fill from end of line to extend position */
-
- fillup()
- {
- TXTSIZ x;
- if(extend && pic)
- {
- x=getrcol();
- while(extend>x)
- {
- fminsc(' ');
- fmgetc();
- ++x;
- }
- }
- extend=0;
- }
-
- /* Save current buffer in named file. Returns 0 on error. Clears 'changed'
- * variable if sucessfull
- */
-
- int saveit1(tos)
- unsigned char *tos;
- {
- unsigned char sting[PATHSIZE];
- TXTSIZ temp=fmnote();
- fmpoint(0);
- handle=fopen(tos,"w+");
- if(handle)
- {
- if(!fmsave(handle,fmsize()))
- {
- sprintf(sting,"\033[7mError writing to file %s\033[m",tos);
- msg(sting);
- fmpoint(temp);
- return(0);
- }
- fmpoint(temp);
- if(fclose(handle)==EOF)
- {
- sprintf(sting,"\033[7mError closing file %s\033[m",tos);
- msg(sting);
- fmpoint(temp);
- return(0);
- }
- changed=0;
- curbuf->changed=0;
- return(1);
- }
- else
- {
- sprintf(sting,"\033[7mError opening file %s\033[m",tos);
- msg(sting);
- fmpoint(temp);
- return(0);
- }
- }
-
- rewrite()
- {
- unsigned char s[25];
- int *t,c;
- oxpos= 0;
- oypos= 0;
- tops= 0;
- bots= height-1;
- if(scroll) sprintf(s,"\033[m\033[1;%dr\033[H\033[J",height);
- else sprintf(s,"\033[m\033[H\033[J");
- eputs(s);
- t=scrn;
- c=width*height;
- do *(t++)= ' '; while(--c);
- upd=1;
- newy=1;
- updall=1;
- if(helpon) hupd=1;
- }
-
- /* Toggle help text */
-
- thelp()
- {
- struct window *x;
- newy=1;
- upd=1;
- if(helpon)
- {
- x=topwin;
- do
- {
- if(x->hheight) x->height=x->hheight;
- else x->height*=height, x->height/=height-wind;
- x=x->next;
- }
- while(x!=topwin);
- wind=0, hupd=0;
- }
- else
- {
- hupd=1, wind=helplines;
- x=topwin;
- do
- {
- x->hheight=x->height;
- x->height*=height-wind;
- x->height/=height;
- x=x->next;
- }
- while(x!=topwin);
- }
- helpon= !helpon;
- wfit();
- }
-
- /* Move cursor to beginning of file */
-
- bof()
- {
- extend=0;
- fmpoint(0);
- newy=1;
- }
-
- /* Move cursor to beginning of line */
-
- bol()
- {
- if(fmnrnl()) fmgetc();
- extend=0;
- }
-
- /* Move cursor to end of line */
-
- eol()
- {
- extend=0;
- fmfnl();
- }
-
- /* Move cursor to end of file */
-
- eof()
- {
- extend=0;
- fmpoint(fmsize());
- newy=1;
- }
-
- /* Move cursor right */
-
- urtarw()
- {
- fillup();
- extend=0;
- if(fmeof())
- {
- if(pic)
- {
- into:
- fminsc(' ');
- fmgetc();
- }
- return;
- }
- else if(fmrc()==NL)
- {
- if(pic) goto into;
- bol();
- udnarw();
- return;
- }
- fmgetc();
- }
-
- rtarw()
- {
- fillup();
- extend=0;
- if(fmeof())
- {
- if(pic)
- {
- into:
- fminsc(' ');
- fmgetc();
- }
- return;
- }
- else if(fmrc()==NL)
- {
- if(pic) goto into;
- newy=1;
- }
- fmgetc();
- }
-
- ultarw()
- {
- fillup();
- if(extend) extend=0;
- else if(fmnote())
- {
- fmpoint(fmnote()-1);
- if(fmrc()==NL)
- {
- fmgetc();
- uuparw();
- eol();
- }
- }
- }
-
- ltarw()
- {
- fillup();
- if(extend) extend=0;
- else
- {
- if(fmnote())
- fmpoint(fmnote()-1);
- if(fmrc()==NL) newy=1;
- }
- }
-
- /* Move cursor up */
-
- uparw()
- {
- TXTSIZ x;
- x=getcol();
- bol();
- if(fmnote())
- {
- fmpoint(fmnote()-1);
- if(fmnrnl())
- fmgetc();
- }
- gocol(x);
- newy=1;
- }
-
- /* user's cursor up routine (uses scrolling regions) */
-
- uuparw()
- {
- TXTSIZ sve=fmnote();
- int y=(curwin->wind+1)*width;
- int x;
- if(scroll)
- {
- if(fmnrnl())
- {
- if(fmnote()+1==saddr)
- {
- if(fmnrnl()) fmgetc();
- saddr=fmnote();
- setregn(curwin->wind+1,curwin->wind+(curwin->height-1));
- cpos(curwin->wind+1,oxpos);
- tattrib(' ');
- eputs("\033M");
- for(x=(curwin->wind+curwin->height)*width-1;x>=y+width;x--)
- scrn[x]=scrn[x-width];
- for(x=y;x<y+width;x++) scrn[x]= ' ';
- }
- fmpoint(sve);
- }
- else
- fmpoint(sve);
- }
- uparw();
- }
-
- /* Move cursor down */
-
- dnarw()
- {
- TXTSIZ x;
- newy=1;
- x=getcol();
- if(!fmfnl())
- bol();
- else
- fmgetc();
- gocol(x);
- }
-
- /* user's down arrow function */
-
- udnarw()
- {
- TXTSIZ sve=fmnote();
- int x;
- if(!fmfnl())
- {
- if(pic)
- {
- fminsc(NL);
- fmpoint(sve);
- udnarw();
- return;
- }
- else
- {
- goto cant;
- }
- }
- if(scroll)
- {
- if(ypos!=curwin->height+curwin->wind-1) goto cant;
- for(x=0;x!=curwin->height-2;x++) fmnrnl();
- fmfnl();
- fmgetc();
- saddr=fmnote();
- setregn(curwin->wind+1,curwin->wind+curwin->height-1);
- cpos((curwin->wind+curwin->height-1),oxpos);
- tattrib(' ');
- eputc(10);
- for(x=(curwin->wind+1)*width;x!=(curwin->wind+curwin->height-1)*width;x++)
- scrn[x]=scrn[x+width];
- for(x=(curwin->wind+curwin->height-1)*width;
- x!=(curwin->wind+curwin->height)*width;x++)
- scrn[x]= ' ';
- }
- cant:
- fmpoint(sve);
- dnarw();
- }
-
- /* Magic Tabs (tm) */
-
- TXTSIZ tabcol; /* Original column of text preceeded by tab stops */
-
- tabmark()
- {
- TXTSIZ cur=fmnote();
- unsigned char c;
- tabcol=0;
- if(!tabmagic) return;
- while(!fmeof())
- {
- c=fmgetc();
- if(c=='\t')
- {
- while(!fmeof())
- {
- c=fmgetc();
- if(c=='\n') break;
- if(c!='\t')
- {
- fmrgetc();
- tabcol=getrcol();
- break;
- }
- }
- fmpoint(cur); return;
- }
- if(c=='\n') break;
- }
- fmpoint(cur); return;
- }
-
- tabfix()
- {
- TXTSIZ cur=fmnote(),newcol;
- unsigned char c;
- if(!tabcol) return;
- while(!fmeof())
- {
- c=fmgetc();
- if(c=='\t')
- {
- while(!fmeof())
- {
- c=fmgetc();
- if(c=='\n') break;
- if(c!='\t')
- {
- fmrgetc();
- newcol=getrcol();
- while(newcol<tabcol)
- {
- fminsc('\t');
- newcol+=8;
- }
- fmrgetc();
- while(newcol>tabcol)
- {
- if(fmrgetc()=='\t')
- {
- fmdel(1);
- newcol-=8;
- }
- else break;
- }
- break;
- }
- }
- fmpoint(cur); return;
- }
- if(c=='\n') break;
- }
- fmpoint(cur); return;
- }
-
- /* Delete character under cursor */
-
- delch()
- {
- unsigned char c;
- int x;
- if(extend && pic) return;
- if(extend)
- {
- extend=0;
- return;
- }
- if(!fmeof())
- {
- if((c=fmrc())==NL && scroll)
- {
- if(ypos<curwin->wind+curwin->height-2)
- {
- for(x=(ypos+1)*width;x<width*(curwin->wind+curwin->height-1);x++)
- scrn[x]=scrn[x+width];
- for(x=(curwin->wind+curwin->height-1)*width;
- x<(curwin->wind+curwin->height)*width;x++) scrn[x]= ' ';
- setregn(ypos+1,(curwin->wind+curwin->height-1));
- cpos((curwin->wind+curwin->height-1),oxpos);
- tattrib(' ');
- eputc(10);
- }
- fmdel(1);
- }
- else if(c==TAB) fmdel(1);
- else
- {
- tabmark();
- fmdel(1);
- tabfix();
- }
- }
- }
-
- type(ch)
- unsigned char ch;
- {
- int ox=oxpos;
- int x,y;
- TXTSIZ temp, temp1;
- int eflag=0;
- if(quote8th)
- {
- quote8th=0;
- ch|=128;
- }
- ypos=oypos;
- if(extend)
- {
- if(ch!=NL) fillup();
- else extend=0;
- eflag=1;
- }
- if(ch==NL)
- {
- if(overwrite && !tabmagic && !fmeof()) fmdel(1);
- fminsc(ch);
- fmgetc();
- newy=1;
- if(ypos!=(curwin->wind+curwin->height-1))
- {
- if(!fmeof())
- {
- if(ypos<curwin->wind+curwin->height-2 && scroll)
- {
- setregn(ypos+1,(curwin->wind+curwin->height-1));
- cpos(ypos+1,oxpos);
- tattrib(' ');
- eputs("\033M");
- cpos(ypos+1,0);
- for(x=(curwin->wind+curwin->height)*width-1;x>=(ypos+2)*width;x--)
- scrn[x]=scrn[x-width];
- for(x=(ypos+1)*width;x<(ypos+2)*width;x++) scrn[x]=' ';
- }
- else cpos(ypos+1,0);
- }
- else
- cpos(ypos+1,0);
- }
- else if(scroll)
- {
- setregn(curwin->wind+1,(curwin->wind+curwin->height-1));
- cpos((curwin->height+curwin->wind-1),0);
- tattrib(' ');
- eputc(10);
- for(x=curwin->wind*width;x<(curwin->wind+curwin->height-1)*width;x++)
- scrn[x]=scrn[x+width];
- for(x=(curwin->wind+curwin->height-1)*width;
- x<(curwin->wind+curwin->height)*width;x++) scrn[x]= ' ';
- temp=fmnote();
- fmpoint(saddr);
- fmfnl();
- fmgetc();
- saddr=fmnote();
- fmpoint(temp);
- }
- if(ox<(width-2) && (fmeof()) && scroll) uuu=1;
- if(autoind)
- {
- temp=fmnote();
- uparw();
- for(x=0;1;x++)
- {
- ch=fmgetc();
- if(!(ch==' ' || ch==TAB)) break;
- temp1=fmnote();
- fmpoint(temp);
- fminsc(ch);
- uuu=0;
- added++;
- fmpoint(temp1);
- temp++;
- }
- fmpoint(fmnote()-(x+1));
- dnarw();
- y=overwrite, overwrite=0;
- for(;x;x--) rtarw();
- overwrite=y;
- }
- }
- else
- {
- if(overwrite)
- {
- if(!tabmagic)
- {
- if(!fmeof())
- {
- unsigned char c=fmrc();
- fmdel(1);
- if(ch!=TAB && c!=TAB && c!=NL && ox<(width-2)) uuu=1;
- }
- else if(ch!=TAB && ox<(width-2)) uuu=1;
- }
- else
- if(fmrc()!=NL && !fmeof())
- if(ch==TAB && fmrc()!=TAB)
- {
- TXTSIZ foo=getrcol();
- do
- {
- if(fmeof()) break;
- if(fmrc()==NL) break;
- if(fmrc()==TAB)
- {
- fmdel(1);
- break;
- }
- else fmdel(1);
- ++foo;
- }
- while(foo&7);
- }
- else if(ch!=TAB && fmrc()==TAB)
- {
- TXTSIZ tt;
- tabmark();
- if(tt=tabcol)
- {
- fminsc(ch);
- tabmark();
- fmdel(1);
- if(tabcol!=tt) fmdel(1);
- }
- }
- else
- {
- fmdel(1);
- if(ch!=TAB && ox<(width-2)) uuu=1;
- }
- else if(ox<(width-2) && ch!=TAB) uuu=1;
- }
- if(wrap)
- {
- unsigned char xx;
- if(getrcol()<rmargin) goto skip;
- if(ch==' ')
- type(NL);
- else
- {
- temp=fmnote();
- while(1)
- {
- if(fmnote())
- {
- fmpoint(fmnote()-1);
- xx=fmrc();
- if(xx==NL) break;
- if(xx==' ' || x==TAB)
- {
- fmdel(1);
- added=0;
- type(NL);
- temp+=added;
- break;
- }
- }
- else break;
- }
- fmpoint(temp);
- fminsc(ch);
- rtarw();
- uuu=0;
- }
- }
- else
- {
- skip:
- if(overwrite || ch==TAB) fminsc(ch);
- else
- {
- tabmark();
- fminsc(ch);
- tabfix();
- }
- if(ch!=TAB && ch!=NL)
- {
- if(fmnote()>=markb && fmnote()<marke) ch^=128;
- fmgetc();
- tputcc(ch);
- scrn[ypos*width+oxpos]=ch;
- oxpos++;
- if(fmeof()) { if(!eflag && ox<width-2) uuu=1; }
- else if(fmrc()==NL && !eflag && ox<width-2) uuu=1;
- }
- else fmgetc();
- }
- }
- }
-
- itype(ch)
- unsigned char ch;
- {
- int x,y;
- TXTSIZ temp,temp1;
- if(extend)
- {
- if(ch!= NL) fillup();
- else extend=0;
- }
- if(ch==NL)
- {
- fminsc(ch);
- fmgetc();
- newy=1;
- if(autoind)
- {
- temp=fmnote();
- uparw();
- for(x=0;1;x++)
- {
- ch=fmgetc();
- if(!(ch==' ' || ch==TAB)) break;
- temp1=fmnote();
- fmpoint(temp);
- fminsc(ch);
- added++;
- fmpoint(temp1);
- temp++;
- }
- fmpoint(fmnote()-(x+1));
- dnarw();
- y=overwrite, overwrite=0;
- for(;x;x--) rtarw();
- overwrite=y;
- }
- }
- else
- {
- if(overwrite)
- if(!fmeof()) fmdel(1);
- if(wrap)
- {
- if(getrcol()<rmargin) goto skip;
- if(ch==' ')
- itype(NL);
- else
- {
- temp=fmnote();
- while(1)
- {
- if(fmnote())
- {
- fmpoint(fmnote()-1);
- x=fmrc();
- if(x==NL) break;
- if(x==' ' || x==TAB)
- {
- fmdel(1);
- added=0;
- itype(NL);
- temp+=added;
- break;
- }
- }
- else break;
- }
- fmpoint(temp);
- fminsc(ch);
- rtarw();
- }
- }
- else
- {
- skip:
- fminsc(ch);
- rtarw();
- }
- }
- }
-
- /* Insert space */
-
- inss()
- {
- int t=overwrite;
- if(extend)
- {
- extend=0;
- return;
- }
- overwrite=0;
- type(' ');
- ltarw();
- overwrite=t;
- }
-
- /* Deleting backspace */
-
- backs()
- {
- int flag=0,c;
- if(extend)
- {
- extend=0;
- return;
- }
- if(fmeof()) c=1;
- else if(fmrc()==NL) c=1;
- if(fmnote())
- {
- ultarw();
- if(fmrc()==TAB) flag=1;
- if(overwrite && !tabmagic)
- {
- itype(' ');
- ultarw();
- }
- else if(overwrite && tabmagic)
- {
- if(c) delch();
- else if(!flag)
- {
- itype(' ');
- ltarw();
- }
- }
- else delch();
- if(oxpos && !flag)
- {
- eputc(8), tputcc(32), eputc(8), oxpos--,scrn[oypos*width+oxpos]=32;
- if(fmeof()) uuu=1;
- else if(fmrc()==NL || overwrite) uuu=1;
- }
- }
- }
-
- /* quit: exit without saving */
-
- eexit()
- {
- int c;
- if(curwin->next==curwin)
- {
- if(changed)
- {
- c=askyn("Do you really want to throw away this file?");
- if(c=='N') return;
- if(c== -1) return;
- dclose("File not saved.");
- }
- else
- {
- dclose("File not changed so no update needed");
- }
- leave=1;
- }
- else
- {
- struct window *t=curwin;
- if(changed && curbuf->count==1)
- {
- c=askyn("Do you really want to throw away this file?");
- if(c=='N') return;
- if(c== -1) return;
- }
- if(curbuf->count==1)
- {
- struct undorec *u;
- for(u=undorecs;u;u=u->next) if(u->buf==curbuf) u->buf=0;
- free(curbuf->buf), free(curbuf);
- if(curbuf==markbuf) markbuf=0;
- }
- else curbuf->count--;
- curwin->next->prev=curwin->prev;
- curwin->prev->next=curwin->next;
- curwin=curwin->prev;
- free(t);
- ldwin(curwin);
- if(topwin==t) topwin=curwin;
- wfit();
- }
- }
-
- pgup()
- {
- int nlins=curwin->height-1;
- int hlins=nlins/2;
- int x,y;
- TXTSIZ curpos,z;
- if(!hlins) hlins=1;
- z=getcol();
- curpos=fmnote();
- fmpoint(saddr);
- for(x=0;x<hlins;x++)
- {
- if(!fmnrnl())
- {
- if(!x)
- {
- gocol(z);
- newy=1;
- return;
- }
- else
- break;
- }
- }
- if(fmnrnl()) fmgetc();
- saddr=fmnote();
- fmpoint(curpos);
- setregn(curwin->wind+1,(curwin->wind+curwin->height-1));
- cpos(curwin->wind+1,oxpos);
- tattrib(' ');
- for(y=0;y<x;y++)
- {
- if(scroll) eputs("\033M");
- fmnrnl();
- }
- if(fmnrnl()) fmgetc();
- cpos(oypos,oxpos);
- gocol(z);
- x*=width;
- if(scroll) for(y=(curwin->wind+1)*width;y<x+(curwin->wind+1)*width;y++)
- {
- scrn[y+x]=scrn[y];
- scrn[y]= ' ';
- }
- }
-
- pgdn()
- {
- int nlins=curwin->height-1;
- int hlins=nlins/2;
- TXTSIZ curpos,z;
- int x,y;
- z=getcol();
- curpos=fmnote();
- x=nlins;
- fmpoint(saddr);
- do
- {
- if(fmfnl()) fmgetc();
- else
- {
- newy=1;
- gocol(z);
- return;
- }
- }
- while(--x);
- for(x=1;x<hlins;x++)
- {
- if(fmfnl()) fmgetc();
- else break;
- }
-
- fmpoint(saddr);
- for(y=0;y<x;y++)
- {
- fmfnl();
- fmgetc();
- }
- saddr=fmnote();
-
- setregn(curwin->wind+1,(curwin->wind+curwin->height-1));
- cpos((curwin->wind+curwin->height-1),oxpos);
- fmpoint(curpos);
- tattrib(' ');
- for(y=0;y<x;y++)
- {
- fmfnl();
- fmgetc();
- if(scroll) eputc(10);
- }
-
- gocol(z);
- cpos(ypos,xpos);
- if(scroll)
- {
- y=width*x;
- for(curpos=(curwin->wind+1)*width+y;curpos<(curwin->wind+curwin->height)*
- width;curpos++)
- scrn[curpos-y]=scrn[curpos];
- for(curpos=(curwin->wind+curwin->height)*width-width*x;
- curpos<(curwin->wind+curwin->height)*width;curpos++)
- scrn[curpos]= ' ';
- }
- }
-
- deleol()
- {
- TXTSIZ temp=fmnote();
- TXTSIZ temp1;
- if(extend && pic) return;
- extend=0;
- fmfnl();
- temp1=fmnote()-temp;
- fmpoint(temp);
- if(temp1) fmdel(temp1);
- }
-
- dellin()
- {
- bol();
- deleol();
- delch();
- }
-
- fixpath(s)
- unsigned char *s;
- {
- unsigned char tmp[PATHSIZE], *p, c;
- struct passwd *passwd;
- if(*s=='~')
- {
- p=s+1;
- while(*p!='/' && *p) ++p;
- if(c= *p)
- {
- *p=0;
- if(passwd=getpwnam(s+1))
- {
- *p=c;
- strcpy(tmp,passwd->pw_dir);
- strcat(tmp,p);
- strcpy(s,tmp);
- }
- }
- }
- }
-
- exsave()
- {
- unsigned char sting[PATHSIZE];
- if(!changed)
- {
- eexit();
- return;
- }
- if(gfnam[0]==0)
- {
- if(!getl("Save file",gfnam))
- return;
- fixpath(gfnam);
- }
- else if(!backup)
- {
- sprintf(sting,"/bin/cp %s %s~",gfnam,gfnam);
- cpos(height-2,0);
- system(sting);
- cpos(ypos,xpos);
- }
- if(saveit1(gfnam))
- {
- sprintf(sting,"File %s saved.",gfnam);
- if(curwin->next==curwin)
- {
- dclose(sting);
- leave=1;
- }
- else
- eexit();
- }
- }
-
- saveit()
- {
- unsigned char gfnam1[PATHSIZE];
- unsigned char sting[PATHSIZE];
- strcpy(gfnam1,gfnam);
- if(!getl("Save file",gfnam1))
- return;
- fixpath(gfnam1);
- if(!backup && !strcmp(gfnam1,gfnam))
- {
- sprintf(sting,"/bin/cp %s %s~",gfnam,gfnam);
- cpos(height-2,0);
- system(sting);
- cpos(ypos,xpos);
- }
- saveit1(gfnam1);
- }
-
- findline()
- {
- unsigned char sting[PATHSIZE];
- TXTSIZ x;
- sting[0]=0;
- if(!getl("Goto line",sting))
- return;
- x=atol(sting);
- if(!x)
- {
- msg("\033[7mBad line number\033[m");
- return;
- }
- x--;
- bof();
- for(;x;x--)
- {
- if(!fmfnl()) break;
- fmgetc();
- }
- newy=1;
- cntr=1;
- return;
- }
-
- int search()
- {
- if(options&s_backwards)
- {
- while(fmnote())
- {
- fmrgetc();
- if(options&s_ignore) { if(!fmicmp(sstring,len)) return 1; }
- else if(!fmcmp(sstring,len)) return 1;
- }
- return 0;
- }
- else
- {
- while(fmnote()+len<=fmsize())
- {
- if(!(options&s_ignore)) { if(!fmcmp(sstring,len)) return 1; }
- else if(!fmicmp(sstring,len)) return 1;
- fmgetc();
- }
- return 0;
- }
- }
-
- find(c)
- {
- int x;
- int opts=0;
- int n=0;
- int rest=0;
- int rlen;
- TXTSIZ p;
- unsigned char ss[80];
- extend=0;
- if(c=='L'-'@' && sstring[0]) goto srch;
- ss[0]=0;
- if(!(x=getl("Search string",ss))) return;
- if(x== -1)
- {
- if(ss[0])
- strcpy(sstring,ss);
- goto srch;
- }
- if(!ss[0]) return;
- strcpy(sstring,ss);
- ss[0]=0;
- if(!getl("(I)gnore case (B)ackwards (R)eplace n",ss)) return;
- for(x=0;ss[x];x++)
- {
- if(ss[x]=='i' || ss[x]=='I') opts|=s_ignore;
- if(ss[x]=='b' || ss[x]=='B') opts|=s_backwards;
- if(ss[x]=='r' || ss[x]=='R') opts|=s_replace;
- if(ss[x]=='x' || ss[x]=='X') opts|=s_regex;
- if(ss[x]>='0' && ss[x]<='9') n*=10, n+=ss[x]-'0';
- }
- options=opts;
- if(options&s_replace)
- {
- ss[0]=0;
- if(!(x=getl("Replace with",ss))) return;
- if(x!= -1)
- strcpy(rstring,ss);
- }
- srch:
- if(!sstring[0]) return;
- len=strlen(sstring);
- rlen=strlen(rstring);
- rpt:
- p=fmnote();
- if(search())
- {
- if(!(options&s_backwards)) fmpoint(fmnote()+len);
- if(options&s_replace)
- {
- if(rest) goto dn;
- newy=1;
- upd=1;
- cntr=1;
- extend=0;
- dupdate();
- again:
- x=nquery(
- "Replace? (Yes, No, ^C to abort or R to replace rest without asking)");
- if(x=='n' || x=='N') goto rpt;
- if(x== 3) return;
- if(x=='y' || x=='Y') goto dn;
- if(x=='r' || x=='R')
- {
- rest=1;
- goto dn;
- }
- goto again;
- dn:
- if(options&s_backwards)
- {
- fmdel(len);
- fminss(rstring,rlen);
- }
- else
- {
- fmpoint(fmnote()-len);
- fmdel(len);
- fminss(rstring,rlen);
- fmpoint(fmnote()+rlen);
- }
- if(n)
- if(n==1) goto exi;
- else n--;
- goto rpt;
- }
- else if(n)
- {
- if(n==1) goto exi;
- n--;
- goto rpt;
- }
- }
- else
- {
- if(!(options&s_replace) || n>1)
- msg("Not found");
- fmpoint(p);
- return;
- }
- exi:
- cntr=1;
- newy=1;
- }
-
- findnext()
- {
- find('L'-'@');
- }
-
- findfirst()
- {
- find(0);
- }
-
- struct buffer *markbuf;
-
- setbeg()
- {
- markb=fmnote();
- if(markbuf!=curbuf)
- {
- markbuf=curbuf;
- marke=0;
- }
- }
-
- setend()
- {
- marke=fmnote();
- if(markbuf!=curbuf)
- {
- markbuf=curbuf;
- markb=0;
- }
- }
-
- writeblk()
- {
- unsigned char gfnam1[PATHSIZE];
- unsigned char sting[PATHSIZE];
- TXTSIZ sv=fmnote();
- struct buffer *bt=curbuf;
- if(markbuf)
- {
- stbuf(curbuf);
- ldbuf(markbuf);
- }
- if(markb>=marke || marke>fmsize() || !markbuf)
- {
- msg("\033[7mThe block is not marked properly\033[m Mark it with ^KB & ^KK");
- if(markbuf)
- ldbuf(bt);
- return;
- }
- gfnam1[0]=0;
- if(!getl("File to write block to",gfnam1))
- {
- ldbuf(bt);
- return;
- }
- fixpath(gfnam1);
- handle=fopen(gfnam1,"w+");
- if(handle)
- {
- fmpoint(markb);
- if(!fmsave(handle,marke-markb))
- {
- sprintf(sting,"\033[7mError writting to file %s\033[m",gfnam1);
- msg(sting);
- }
- stbuf(markbuf);
- ldbuf(bt);
- fmpoint(sv);
- -- Cut here
- --
- /* rcarter@wpi.wpi.edu */ /* Amazing */ /* Joseph H. Allen */
- int a[1817];main(z,p,q,r){for(p=80;q+p-80;p-=2*a[p])for(z=9;z--;)q=3&(r=time(0)
- +r*57)/7,q=q?q-1?q-2?1-p%79?-1:0:p%79-77?1:0:p<1659?79:0:p>158?-79:0,q?!a[p+q*2
- ]?a[p+=a[p+=q]=q]=q:0:0;for(;q++-1817;)printf(q%79?"%c":"%c\n"," #"[!a[q-1]]);}
-