new/ 775 0 0 0 11256243714 74625ustar00nemosysnew/acme.c 664 0 0 46266 11256243321 10563ustar00nemosys#include #include #include #include #include #include #include #include #include #include #include "dat.h" #include "fns.h" /* for generating syms in mkfile only: */ #include #include "edit.h" void mousethread(void*); void keyboardthread(void*); void waitthread(void*); void xfidallocthread(void*); void newwindowthread(void*); void plumbproc(void*); Reffont **fontcache; int nfontcache; char wdir[512] = "."; Reffont *reffonts[2]; int snarffd = -1; int mainpid; int plumbsendfd; int plumbeditfd; enum{ NSnarf = 1000 /* less than 1024, I/O buffer size */ }; Rune snarfrune[NSnarf+1]; char *fontnames[2] = { "/lib/font/bit/lucidasans/euro.8.font", "/lib/font/bit/lucm/unicode.9.font" }; Command *command; void acmeerrorinit(void); void readfile(Column*, char*); int shutdown(void*, char*); void derror(Display*, char *errorstr) { error(errorstr); } void threadmain(int argc, char *argv[]) { int i; char *p, *loadfile; char buf[256]; Column *c; int ncol; Display *d; static void *arg[1]; rfork(RFENVG|RFNAMEG); ncol = -1; loadfile = nil; ARGBEGIN{ case 'a': globalautoindent = TRUE; break; case 'b': bartflag = TRUE; break; case 'c': p = ARGF(); if(p == nil) goto Usage; ncol = atoi(p); if(ncol <= 0) goto Usage; break; case 'f': fontnames[0] = ARGF(); if(fontnames[0] == nil) goto Usage; break; case 'F': fontnames[1] = ARGF(); if(fontnames[1] == nil) goto Usage; break; case 'l': loadfile = ARGF(); if(loadfile == nil) goto Usage; break; default: Usage: fprint(2, "usage: acme [-ab] [-c ncol] [-f font] [-F fixedfont] [-l loadfile | file...]\n"); exits("usage"); }ARGEND fontnames[0] = estrdup(fontnames[0]); fontnames[1] = estrdup(fontnames[1]); quotefmtinstall(); cputype = getenv("cputype"); objtype = getenv("objtype"); home = getenv("home"); p = getenv("tabstop"); if(p != nil){ maxtab = strtoul(p, nil, 0); free(p); } if(maxtab == 0) maxtab = 4; if(loadfile) rowloadfonts(loadfile); putenv("font", fontnames[0]); snarffd = open("/dev/snarf", OREAD|OCEXEC); if(cputype){ sprint(buf, "/acme/bin/%s", cputype); bind(buf, "/bin", MBEFORE); } bind("/acme/bin", "/bin", MBEFORE); getwd(wdir, sizeof wdir); acmetagsinit(); if(geninitdraw(nil, derror, fontnames[0], "acme", nil, Refnone) < 0){ fprint(2, "acme: can't open display: %r\n"); exits("geninitdraw"); } d = display; font = d->defaultfont; reffont.f = font; reffonts[0] = &reffont; incref(&reffont); /* one to hold up 'font' variable */ incref(&reffont); /* one to hold up reffonts[0] */ fontcache = emalloc(sizeof(Reffont*)); nfontcache = 1; fontcache[0] = &reffont; iconinit(); timerinit(); rxinit(); cwait = threadwaitchan(); ccommand = chancreate(sizeof(Command**), 0); ckill = chancreate(sizeof(Rune*), 0); cxfidalloc = chancreate(sizeof(Xfid*), 0); cxfidfree = chancreate(sizeof(Xfid*), 0); cnewwindow = chancreate(sizeof(Channel*), 0); cerr = chancreate(sizeof(char*), 0); cedit = chancreate(sizeof(int), 0); cexit = chancreate(sizeof(int), 0); cwarn = chancreate(sizeof(void*), 1); if(cwait==nil || ccommand==nil || ckill==nil || cxfidalloc==nil || cxfidfree==nil || cerr==nil || cexit==nil || cwarn==nil){ fprint(2, "acme: can't create initial channels: %r\n"); exits("channels"); } mousectl = initmouse(nil, screen); if(mousectl == nil){ fprint(2, "acme: can't initialize mouse: %r\n"); exits("mouse"); } mouse = mousectl; keyboardctl = initkeyboard(nil); if(keyboardctl == nil){ fprint(2, "acme: can't initialize keyboard: %r\n"); exits("keyboard"); } mainpid = getpid(); plumbeditfd = plumbopen("edit", OREAD|OCEXEC); if(plumbeditfd >= 0){ cplumb = chancreate(sizeof(Plumbmsg*), 0); proccreate(plumbproc, nil, STACK); } plumbsendfd = plumbopen("send", OWRITE|OCEXEC); fsysinit(); #define WPERCOL 8 disk = diskinit(); if(!loadfile || !rowload(&row, loadfile, TRUE)){ rowinit(&row, screen->clipr); if(ncol < 0){ if(argc == 0) ncol = 2; else{ ncol = (argc+(WPERCOL-1))/WPERCOL; if(ncol < 2) ncol = 2; } } if(ncol == 0) ncol = 2; for(i=0; i=row.ncol) readfile(c, argv[i]); else readfile(row.col[i/WPERCOL], argv[i]); } } flushimage(display, 1); acmeerrorinit(); threadcreate(keyboardthread, nil, STACK); threadcreate(mousethread, nil, STACK); threadcreate(waitthread, nil, STACK); threadcreate(xfidallocthread, nil, STACK); threadcreate(newwindowthread, nil, STACK); threadnotify(shutdown, 1); recvul(cexit); killprocs(); threadexitsall(nil); } void readfile(Column *c, char *s) { Window *w; Rune rb[256]; int nb, nr; Runestr rs; w = coladd(c, nil, nil, -1); cvttorunes(s, strlen(s), rb, &nb, &nr, nil); rs = cleanrname((Runestr){rb, nr}); winsetname(w, rs.r, rs.nr); textload(&w->body, 0, s, 1); w->body.file->mod = FALSE; w->dirty = FALSE; winsettag(w); textscrdraw(&w->body); textsetselect(&w->tag, w->tag.file->nc, w->tag.file->nc); } char *oknotes[] ={ "delete", "hangup", "kill", "exit", nil }; int dumping; int shutdown(void*, char *msg) { int i; killprocs(); if(!dumping && strcmp(msg, "kill")!=0 && strcmp(msg, "exit")!=0 && getpid()==mainpid){ dumping = TRUE; rowdump(&row, nil); } for(i=0; oknotes[i]; i++) if(strncmp(oknotes[i], msg, strlen(oknotes[i])) == 0) threadexitsall(msg); print("acme: %s\n", msg); abort(); return 0; } void killprocs(void) { Command *c; fsysclose(); // if(display) // flushimage(display, 1); for(c=command; c; c=c->next) postnote(PNGROUP, c->pid, "hangup"); remove(acmeerrorfile); } static int errorfd; void acmeerrorproc(void *) { char *buf; int n; threadsetname("acmeerrorproc"); buf = emalloc(8192+1); while((n=read(errorfd, buf, 8192)) >= 0){ buf[n] = '\0'; sendp(cerr, estrdup(buf)); } } void acmeerrorinit(void) { int fd, pfd[2]; char buf[64]; if(pipe(pfd) < 0) error("can't create pipe"); sprint(acmeerrorfile, "/srv/acme.%s.%d", getuser(), mainpid); fd = create(acmeerrorfile, OWRITE, 0666); if(fd < 0){ remove(acmeerrorfile); fd = create(acmeerrorfile, OWRITE, 0666); if(fd < 0) error("can't create acmeerror file"); } sprint(buf, "%d", pfd[0]); write(fd, buf, strlen(buf)); close(fd); /* reopen pfd[1] close on exec */ sprint(buf, "/fd/%d", pfd[1]); errorfd = open(buf, OREAD|OCEXEC); if(errorfd < 0) error("can't re-open acmeerror file"); close(pfd[1]); close(pfd[0]); proccreate(acmeerrorproc, nil, STACK); } void plumbproc(void *) { Plumbmsg *m; threadsetname("plumbproc"); for(;;){ m = plumbrecv(plumbeditfd); if(m == nil) threadexits(nil); sendp(cplumb, m); } } void keyboardthread(void *) { Rune r; Timer *timer; Text *t; enum { KTimer, KKey, NKALT }; static Alt alts[NKALT+1]; alts[KTimer].c = nil; alts[KTimer].v = nil; alts[KTimer].op = CHANNOP; alts[KKey].c = keyboardctl->c; alts[KKey].v = &r; alts[KKey].op = CHANRCV; alts[NKALT].op = CHANEND; timer = nil; typetext = nil; threadsetname("keyboardthread"); for(;;){ switch(alt(alts)){ case KTimer: timerstop(timer); t = typetext; if(t!=nil && t->what==Tag){ winlock(t->w, 'K'); wincommit(t->w, t); winunlock(t->w); flushimage(display, 1); } alts[KTimer].c = nil; alts[KTimer].op = CHANNOP; break; case KKey: casekeyboard: typetext = rowtype(&row, r, mouse->xy); t = typetext; if(t!=nil && t->col!=nil && !(r==Kdown || r==Kleft || r==Kright)) /* scrolling doesn't change activecol */ activecol = t->col; if(t!=nil && t->w!=nil) t->w->body.file->curtext = &t->w->body; if(timer != nil) timercancel(timer); if(t!=nil && t->what==Tag) { timer = timerstart(500); alts[KTimer].c = timer->c; alts[KTimer].op = CHANRCV; }else{ timer = nil; alts[KTimer].c = nil; alts[KTimer].op = CHANNOP; } if(nbrecv(keyboardctl->c, &r) > 0) goto casekeyboard; flushimage(display, 1); break; } } } void mousethread(void *) { Text *t, *argt; int but; uint q0, q1; Window *w; Plumbmsg *pm; Mouse m; char *act; enum { MResize, MMouse, MPlumb, MWarnings, NMALT }; static Alt alts[NMALT+1]; threadsetname("mousethread"); alts[MResize].c = mousectl->resizec; alts[MResize].v = nil; alts[MResize].op = CHANRCV; alts[MMouse].c = mousectl->c; alts[MMouse].v = &mousectl->Mouse; alts[MMouse].op = CHANRCV; alts[MPlumb].c = cplumb; alts[MPlumb].v = ± alts[MPlumb].op = CHANRCV; alts[MWarnings].c = cwarn; alts[MWarnings].v = nil; alts[MWarnings].op = CHANRCV; if(cplumb == nil) alts[MPlumb].op = CHANNOP; alts[NMALT].op = CHANEND; for(;;){ qlock(&row); flushwarnings(); qunlock(&row); flushimage(display, 1); switch(alt(alts)){ case MResize: if(getwindow(display, Refnone) < 0) error("attach to window"); scrlresize(); rowresize(&row, screen->clipr); break; case MPlumb: if(strcmp(pm->type, "text") == 0){ act = plumblookup(pm->attr, "action"); if(act==nil || strcmp(act, "showfile")==0) plumblook(pm); else if(strcmp(act, "showdata")==0) plumbshow(pm); } plumbfree(pm); break; case MWarnings: break; case MMouse: /* * Make a copy so decisions are consistent; mousectl changes * underfoot. Can't just receive into m because this introduces * another race; see /sys/src/libdraw/mouse.c. */ m = mousectl->Mouse; qlock(&row); t = rowwhich(&row, m.xy); if(t!=mousetext && mousetext!=nil && mousetext->w!=nil){ winlock(mousetext->w, 'M'); mousetext->eq0 = ~0; wincommit(mousetext->w, mousetext); winunlock(mousetext->w); } mousetext = t; if(t == nil) goto Continue; w = t->w; if(t==nil || m.buttons==0) goto Continue; but = 0; if(m.buttons == 1) but = 1; else if(m.buttons == 2) but = 2; else if(m.buttons == 4) but = 3; barttext = t; if(t->what==Body && ptinrect(m.xy, t->scrollr)){ if(but){ winlock(w, 'M'); t->eq0 = ~0; textscroll(t, but); winunlock(w); } goto Continue; } /* scroll buttons, wheels, etc. */ if(t->what==Body && w != nil && (m.buttons & (8|16))){ if(m.buttons & 8) but = Kscrolloneup; else but = Kscrollonedown; winlock(w, 'M'); t->eq0 = ~0; texttype(t, but); winunlock(w); goto Continue; } if(ptinrect(m.xy, t->scrollr)){ if(but){ if(t->what == Columntag) rowdragcol(&row, t->col, but); else if(t->what == Tag){ coldragwin(t->col, t->w, but); if(t->w) barttext = &t->w->body; } if(t->col) activecol = t->col; } goto Continue; } if(m.buttons){ if(w) winlock(w, 'M'); t->eq0 = ~0; if(w) wincommit(w, t); else textcommit(t, TRUE); if(m.buttons & 1){ textselect(t); if(w) winsettag(w); argtext = t; seltext = t; if(t->col) activecol = t->col; /* button 1 only */ if(t->w!=nil && t==&t->w->body) activewin = t->w; }else if(m.buttons & 2){ if(textselect2(t, &q0, &q1, &argt)) execute(t, q0, q1, FALSE, argt); }else if(m.buttons & 4){ if(textselect3(t, &q0, &q1)) look3(t, q0, q1, FALSE); } if(w) winunlock(w); goto Continue; } Continue: qunlock(&row); break; } } } /* * There is a race between process exiting and our finding out it was ever created. * This structure keeps a list of processes that have exited we haven't heard of. */ typedef struct Pid Pid; struct Pid { int pid; char msg[ERRMAX]; Pid *next; }; void waitthread(void *) { Waitmsg *w; Command *c, *lc; uint pid; int found, ncmd; Rune *cmd; char *err; Text *t; Pid *pids, *p, *lastp; enum { WErr, WKill, WWait, WCmd, NWALT }; Alt alts[NWALT+1]; threadsetname("waitthread"); pids = nil; alts[WErr].c = cerr; alts[WErr].v = &err; alts[WErr].op = CHANRCV; alts[WKill].c = ckill; alts[WKill].v = &cmd; alts[WKill].op = CHANRCV; alts[WWait].c = cwait; alts[WWait].v = &w; alts[WWait].op = CHANRCV; alts[WCmd].c = ccommand; alts[WCmd].v = &c; alts[WCmd].op = CHANRCV; alts[NWALT].op = CHANEND; command = nil; for(;;){ switch(alt(alts)){ case WErr: qlock(&row); warning(nil, "%s", err); free(err); flushimage(display, 1); qunlock(&row); break; case WKill: found = FALSE; ncmd = runestrlen(cmd); for(c=command; c; c=c->next){ /* -1 for blank */ if(runeeq(c->name, c->nname-1, cmd, ncmd) == TRUE){ if(postnote(PNGROUP, c->pid, "kill") < 0) warning(nil, "kill %S: %r\n", cmd); found = TRUE; } } if(!found) warning(nil, "Kill: no process %S\n", cmd); free(cmd); break; case WWait: pid = w->pid; lc = nil; for(c=command; c; c=c->next){ if(c->pid == pid){ if(lc) lc->next = c->next; else command = c->next; break; } lc = c; } qlock(&row); t = &row.tag; textcommit(t, TRUE); if(c == nil){ /* helper processes use this exit status */ if(strncmp(w->msg, "libthread", 9) != 0){ p = emalloc(sizeof(Pid)); p->pid = pid; strncpy(p->msg, w->msg, sizeof(p->msg)); p->next = pids; pids = p; } }else{ if(search(t, c->name, c->nname)){ textdelete(t, t->q0, t->q1, TRUE); textsetselect(t, 0, 0); } if(w->msg[0]) warning(c->md, "%s\n", w->msg); flushimage(display, 1); } qunlock(&row); free(w); Freecmd: if(c){ if(c->iseditcmd) sendul(cedit, 0); free(c->text); free(c->name); fsysdelid(c->md); free(c); } break; case WCmd: /* has this command already exited? */ lastp = nil; for(p=pids; p!=nil; p=p->next){ if(p->pid == c->pid){ if(p->msg[0]) warning(c->md, "%s\n", p->msg); if(lastp == nil) pids = p->next; else lastp->next = p->next; free(p); goto Freecmd; } lastp = p; } c->next = command; command = c; qlock(&row); t = &row.tag; textcommit(t, TRUE); textinsert(t, 0, c->name, c->nname, TRUE); textsetselect(t, 0, 0); flushimage(display, 1); qunlock(&row); break; } } } void xfidallocthread(void*) { Xfid *xfree, *x; enum { Alloc, Free, N }; static Alt alts[N+1]; threadsetname("xfidallocthread"); alts[Alloc].c = cxfidalloc; alts[Alloc].v = nil; alts[Alloc].op = CHANRCV; alts[Free].c = cxfidfree; alts[Free].v = &x; alts[Free].op = CHANRCV; alts[N].op = CHANEND; xfree = nil; for(;;){ switch(alt(alts)){ case Alloc: x = xfree; if(x) xfree = x->next; else{ x = emalloc(sizeof(Xfid)); x->c = chancreate(sizeof(void(*)(Xfid*)), 0); x->arg = x; threadcreate(xfidctl, x->arg, STACK); } sendp(cxfidalloc, x); break; case Free: x->next = xfree; xfree = x; break; } } } /* this thread, in the main proc, allows fsysproc to get a window made without doing graphics */ void newwindowthread(void*) { Window *w; threadsetname("newwindowthread"); for(;;){ /* only fsysproc is talking to us, so synchronization is trivial */ recvp(cnewwindow); w = makenewwindow(nil); winsettag(w); sendp(cnewwindow, w); } } Reffont* rfget(int fix, int save, int setfont, char *name) { Reffont *r; Font *f; int i; r = nil; if(name == nil){ name = fontnames[fix]; r = reffonts[fix]; } if(r == nil){ for(i=0; if->name) == 0){ r = fontcache[i]; goto Found; } f = openfont(display, name); if(f == nil){ warning(nil, "can't open font file %s: %r\n", name); return nil; } r = emalloc(sizeof(Reffont)); r->f = f; fontcache = erealloc(fontcache, (nfontcache+1)*sizeof(Reffont*)); fontcache[nfontcache++] = r; } Found: if(save){ incref(r); if(reffonts[fix]) rfclose(reffonts[fix]); reffonts[fix] = r; if(name != fontnames[fix]){ free(fontnames[fix]); fontnames[fix] = estrdup(name); } } if(setfont){ reffont.f = r->f; incref(r); rfclose(reffonts[0]); font = r->f; reffonts[0] = r; incref(r); iconinit(); } incref(r); return r; } void rfclose(Reffont *r) { int i; if(decref(r) == 0){ for(i=0; i= nfontcache) warning(nil, "internal error: can't find font in cache\n"); else{ nfontcache--; memmove(fontcache+i, fontcache+i+1, (nfontcache-i)*sizeof(Reffont*)); } freefont(r->f); free(r); } } Cursor boxcursor = { {-7, -7}, {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xF8, 0x1F, 0xF8, 0x1F, 0xF8, 0x1F, 0xF8, 0x1F, 0xF8, 0x1F, 0xF8, 0x1F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, {0x00, 0x00, 0x7F, 0xFE, 0x7F, 0xFE, 0x7F, 0xFE, 0x70, 0x0E, 0x70, 0x0E, 0x70, 0x0E, 0x70, 0x0E, 0x70, 0x0E, 0x70, 0x0E, 0x70, 0x0E, 0x70, 0x0E, 0x7F, 0xFE, 0x7F, 0xFE, 0x7F, 0xFE, 0x00, 0x00} }; void iconinit(void) { Rectangle r; Image *tmp; /* Blue */ tagcols[BACK] = allocimagemix(display, DPalebluegreen, DWhite); tagcols[HIGH] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, DPalegreygreen); tagcols[BORD] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, DPurpleblue); tagcols[TEXT] = display->black; tagcols[HTEXT] = display->black; /* Yellow */ textcols[BACK] = allocimagemix(display, DPaleyellow, DWhite); textcols[HIGH] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, DDarkyellow); textcols[BORD] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, DYellowgreen); textcols[TEXT] = display->black; textcols[HTEXT] = display->black; if(button){ freeimage(button); freeimage(modbutton); freeimage(colbutton); } r = Rect(0, 0, Scrollwid+2, font->height+1); button = allocimage(display, r, screen->chan, 0, DNofill); draw(button, r, tagcols[BACK], nil, r.min); r.max.x -= 2; border(button, r, 2, tagcols[BORD], ZP); r = button->r; modbutton = allocimage(display, r, screen->chan, 0, DNofill); draw(modbutton, r, tagcols[BACK], nil, r.min); r.max.x -= 2; border(modbutton, r, 2, tagcols[BORD], ZP); r = insetrect(r, 2); tmp = allocimage(display, Rect(0,0,1,1), screen->chan, 1, DMedblue); draw(modbutton, r, tmp, nil, ZP); freeimage(tmp); r = button->r; colbutton = allocimage(display, r, screen->chan, 0, DPurpleblue); but2col = allocimage(display, r, screen->chan, 1, 0xAA0000FF); but3col = allocimage(display, r, screen->chan, 1, 0x006600FF); } /* * /dev/snarf updates when the file is closed, so we must open our own * fd here rather than use snarffd */ /* rio truncates larges snarf buffers, so this avoids using the * service if the string is huge */ #define MAXSNARF 100*1024 void putsnarf(void) { int fd, i, n; if(snarffd<0 || snarfbuf.nc==0) return; if(snarfbuf.nc > MAXSNARF) return; fd = open("/dev/snarf", OWRITE); if(fd < 0) return; for(i=0; i= NSnarf) n = NSnarf; bufread(&snarfbuf, i, snarfrune, n); if(fprint(fd, "%.*S", n, snarfrune) < 0) break; } close(fd); } void getsnarf() { int nulls; if(snarfbuf.nc > MAXSNARF) return; if(snarffd < 0) return; seek(snarffd, 0, 0); bufreset(&snarfbuf); bufload(&snarfbuf, 0, snarffd, &nulls); } new/acmetags 664 0 0 164 11256243714 11152ustar00nemosys# $home/lib/acmetags, example. # ^/mail/box/nemo/msgs/.* |Arch |Spam >Reply |Delmesg ^/mail/box/nemo/out/Out.* Post new/fns.h 664 0 0 5643 11256243123 10423ustar00nemosys#pragma varargck argpos warning 2 void warning(Mntdir*, char*, ...); #define fbufalloc() emalloc(BUFSIZE) #define fbuffree(x) free(x) void plumblook(Plumbmsg*m); void plumbshow(Plumbmsg*m); void putsnarf(void); void getsnarf(void); int tempfile(void); void scrlresize(void); Font* getfont(int, int, char*); char* getarg(Text*, int, int, Rune**, int*); char* getbytearg(Text*, int, int, char**); void new(Text*, Text*, Text*, int, int, Rune*, int); void undo(Text*, Text*, Text*, int, int, Rune*, int); void scrsleep(uint); void savemouse(Window*); void restoremouse(Window*); void clearmouse(void); void allwindows(void(*)(Window*, void*), void*); uint loadfile(int, uint, int*, int(*)(void*, uint, Rune*, int), void*); Window* errorwin(Mntdir*, int); Window* errorwinforwin(Window*); Runestr cleanrname(Runestr); void run(Window*, char*, Rune*, int, int, char*, char*, int); void fsysclose(void); void setcurtext(Text*, int); int isfilec(Rune); void rxinit(void); int rxnull(void); Runestr dirname(Text*, Rune*, int); void error(char*); void cvttorunes(char*, int, Rune*, int*, int*, int*); void* tmalloc(uint); void tfree(void); void killprocs(void); void killtasks(void); int runeeq(Rune*, uint, Rune*, uint); int ALEF_tid(void); void iconinit(void); Timer* timerstart(int); void timerstop(Timer*); void timercancel(Timer*); void timerinit(void); void cut(Text*, Text*, Text*, int, int, Rune*, int); void paste(Text*, Text*, Text*, int, int, Rune*, int); void get(Text*, Text*, Text*, int, int, Rune*, int); void put(Text*, Text*, Text*, int, int, Rune*, int); void putfile(File*, int, int, Rune*, int); void fontx(Text*, Text*, Text*, int, int, Rune*, int); int isalnum(Rune); void execute(Text*, uint, uint, int, Text*); int search(Text*, Rune*, uint); void look3(Text*, uint, uint, int); void editcmd(Text*, Rune*, uint); uint min(uint, uint); uint max(uint, uint); Window* lookfile(Rune*, int); Window* lookid(int, int); char* runetobyte(Rune*, int); Rune* bytetorune(char*, int*); void fsysinit(void); Mntdir* fsysmount(Rune*, int, Rune**, int); void fsysincid(Mntdir*); void fsysdelid(Mntdir*); Xfid* respond(Xfid*, Fcall*, char*); int rxcompile(Rune*); int rgetc(void*, uint); int tgetc(void*, uint); int isaddrc(int); int isregexc(int); void *emalloc(uint); void *erealloc(void*, uint); char *estrdup(char*); Range address(Mntdir*, Text*, Range, Range, void*, uint, uint, int (*)(void*, uint), int*, uint*); int rxexecute(Text*, Rune*, uint, uint, Rangeset*); int rxbexecute(Text*, uint, Rangeset*); Window* makenewwindow(Text *t); int expand(Text*, uint, uint, Expand*); Rune* skipbl(Rune*, int, int*); Rune* findbl(Rune*, int, int*); char* edittext(Window*, int, Rune*, int); void flushwarnings(void); void acmetagsinit(void); Rune* tagforfile(Rune*, int, int *); #define runemalloc(a) (Rune*)emalloc((a)*sizeof(Rune)) #define runerealloc(a, b) (Rune*)erealloc((a), (b)*sizeof(Rune)) #define runemove(a, b, c) memmove((a), (b), (c)*sizeof(Rune)) s = p->next; else lastp->next = p->next; free(p); goto Freecmd; } new/mkfile 664 0 0 1105 11256243237 10651ustar00nemosys syms for(i in ????.c) $CC -aa $i >> syms for(;;){ /* only fsysproc is talking to us, so synchronization is trivial */ recvp(cnewwindow); w = makenewwindow(nil); winsettag(w); sendp(cnewwindow, w); } } Reffont* rfget(int fix, int save, int setfont, char *name) { Reffont *r; Font *f; int i; r = nil; if(name == nil){ name = fontnames[fix]; r = reffonts[fix]; } if(r == nil){ for(i=0; if->name) == 0){ r new/regx.c 664 0 0 37372 11256243306 10624ustar00nemosys#include #include #include #include #include #include #include #include #include #include #include "dat.h" #include "fns.h" Rangeset sel; Rune *lastregexp; /* * Machine Information */ typedef struct Inst Inst; struct Inst { uint type; /* < 0x10000 ==> literal, otherwise action */ union { int sid; int subid; int class; Inst *other; Inst *right; }; union{ Inst *left; Inst *next; }; }; #define NPROG 1024 Inst program[NPROG]; Inst *progp; Inst *startinst; /* First inst. of program; might not be program[0] */ Inst *bstartinst; /* same for backwards machine */ Channel *rechan; /* chan(Inst*) */ typedef struct Ilist Ilist; struct Ilist { Inst *inst; /* Instruction of the thread */ Rangeset se; uint startp; /* first char of match */ }; #define NLIST 127 Ilist *tl, *nl; /* This list, next list */ Ilist list[2][NLIST+1]; /* +1 for trailing null */ static Rangeset sempty; /* * Actions and Tokens * * 0x100xx are operators, value == precedence * 0x200xx are tokens, i.e. operands for operators */ #define OPERATOR 0x10000 /* Bitmask of all operators */ #define START 0x10000 /* Start, used for marker on stack */ #define RBRA 0x10001 /* Right bracket, ) */ #define LBRA 0x10002 /* Left bracket, ( */ #define OR 0x10003 /* Alternation, | */ #define CAT 0x10004 /* Concatentation, implicit operator */ #define STAR 0x10005 /* Closure, * */ #define PLUS 0x10006 /* a+ == aa* */ #define QUEST 0x10007 /* a? == a|nothing, i.e. 0 or 1 a's */ #define ANY 0x20000 /* Any character but newline, . */ #define NOP 0x20001 /* No operation, internal use only */ #define BOL 0x20002 /* Beginning of line, ^ */ #define EOL 0x20003 /* End of line, $ */ #define CCLASS 0x20004 /* Character class, [] */ #define NCCLASS 0x20005 /* Negated character class, [^] */ #define END 0x20077 /* Terminate: match found */ #define ISATOR 0x10000 #define ISAND 0x20000 /* * Parser Information */ typedef struct Node Node; struct Node { Inst *first; Inst *last; }; #define NSTACK 20 Node andstack[NSTACK]; Node *andp; int atorstack[NSTACK]; int *atorp; int lastwasand; /* Last token was operand */ int cursubid; int subidstack[NSTACK]; int *subidp; int backwards; int nbra; Rune *exprp; /* pointer to next character in source expression */ #define DCLASS 10 /* allocation increment */ int nclass; /* number active */ int Nclass; /* high water mark */ Rune **class; int negateclass; int addinst(Ilist *l, Inst *inst, Rangeset *sep); void newmatch(Rangeset*); void bnewmatch(Rangeset*); void pushand(Inst*, Inst*); void pushator(int); Node *popand(int); int popator(void); void startlex(Rune*); int lex(void); void operator(int); void operand(int); void evaluntil(int); void optimize(Inst*); void bldcclass(void); void rxinit(void) { rechan = chancreate(sizeof(Inst*), 0); lastregexp = runemalloc(1); } void regxerror(char *e) { lastregexp[0] = 0; warning(nil, "regexp: %s\n", e); sendp(rechan, nil); threadexits(nil); } Inst * newinst(int t) { if(progp >= &program[NPROG]) regxerror("expression too long"); progp->type = t; progp->left = nil; progp->right = nil; return progp++; } void realcompile(void *arg) { int token; Rune *s; threadsetname("regcomp"); s = arg; startlex(s); atorp = atorstack; andp = andstack; subidp = subidstack; cursubid = 0; lastwasand = FALSE; /* Start with a low priority operator to prime parser */ pushator(START-1); while((token=lex()) != END){ if((token&ISATOR) == OPERATOR) operator(token); else operand(token); } /* Close with a low priority operator */ evaluntil(START); /* Force END */ operand(END); evaluntil(START); if(nbra) regxerror("unmatched `('"); --andp; /* points to first and only operand */ sendp(rechan, andp->first); threadexits(nil); } /* r is null terminated */ int rxcompile(Rune *r) { int i, nr; Inst *oprogp; nr = runestrlen(r)+1; if(runeeq(lastregexp, runestrlen(lastregexp)+1, r, nr)==TRUE) return TRUE; lastregexp[0] = 0; for(i=0; itype = NCCLASS; /* UGH */ i->class = nclass-1; /* UGH */ } pushand(i, i); lastwasand = TRUE; } void operator(int t) { if(t==RBRA && --nbra<0) regxerror("unmatched `)'"); if(t==LBRA){ cursubid++; /* silently ignored */ nbra++; if(lastwasand) operator(CAT); }else evaluntil(t); if(t!=RBRA) pushator(t); lastwasand = FALSE; if(t==STAR || t==QUEST || t==PLUS || t==RBRA) lastwasand = TRUE; /* these look like operands */ } void pushand(Inst *f, Inst *l) { if(andp >= &andstack[NSTACK]) error("operand stack overflow"); andp->first = f; andp->last = l; andp++; } void pushator(int t) { if(atorp >= &atorstack[NSTACK]) error("operator stack overflow"); *atorp++=t; if(cursubid >= NRange) *subidp++= -1; else *subidp++=cursubid; } Node * popand(int op) { char buf[64]; if(andp <= &andstack[0]) if(op){ sprint(buf, "missing operand for %c", op); regxerror(buf); }else regxerror("malformed regexp"); return --andp; } int popator() { if(atorp <= &atorstack[0]) error("operator stack underflow"); --subidp; return *--atorp; } void evaluntil(int pri) { Node *op1, *op2, *t; Inst *inst1, *inst2; while(pri==RBRA || atorp[-1]>=pri){ switch(popator()){ case LBRA: op1 = popand('('); inst2 = newinst(RBRA); inst2->subid = *subidp; op1->last->next = inst2; inst1 = newinst(LBRA); inst1->subid = *subidp; inst1->next = op1->first; pushand(inst1, inst2); return; /* must have been RBRA */ default: error("unknown regexp operator"); break; case OR: op2 = popand('|'); op1 = popand('|'); inst2 = newinst(NOP); op2->last->next = inst2; op1->last->next = inst2; inst1 = newinst(OR); inst1->right = op1->first; inst1->left = op2->first; pushand(inst1, inst2); break; case CAT: op2 = popand(0); op1 = popand(0); if(backwards && op2->first->type!=END){ t = op1; op1 = op2; op2 = t; } op1->last->next = op2->first; pushand(op1->first, op2->last); break; case STAR: op2 = popand('*'); inst1 = newinst(OR); op2->last->next = inst1; inst1->right = op2->first; pushand(inst1, inst1); break; case PLUS: op2 = popand('+'); inst1 = newinst(OR); op2->last->next = inst1; inst1->right = op2->first; pushand(op2->first, inst1); break; case QUEST: op2 = popand('?'); inst1 = newinst(OR); inst2 = newinst(NOP); inst1->left = inst2; inst1->right = op2->first; op2->last->next = inst2; pushand(inst1, inst2); break; } } } void optimize(Inst *start) { Inst *inst, *target; for(inst=start; inst->type!=END; inst++){ target = inst->next; while(target->type == NOP) target = target->next; inst->next = target; } } void startlex(Rune *s) { exprp = s; nbra = 0; } int lex(void){ int c; c = *exprp++; switch(c){ case '\\': if(*exprp) if((c= *exprp++)=='n') c='\n'; break; case 0: c = END; --exprp; /* In case we come here again */ break; case '*': c = STAR; break; case '?': c = QUEST; break; case '+': c = PLUS; break; case '|': c = OR; break; case '.': c = ANY; break; case '(': c = LBRA; break; case ')': c = RBRA; break; case '^': c = BOL; break; case '$': c = EOL; break; case '[': c = CCLASS; bldcclass(); break; } return c; } int nextrec(void) { if(exprp[0]==0 || (exprp[0]=='\\' && exprp[1]==0)) regxerror("malformed `[]'"); if(exprp[0] == '\\'){ exprp++; if(*exprp=='n'){ exprp++; return '\n'; } return *exprp++|0x10000; } return *exprp++; } void bldcclass(void) { int c1, c2, n, na; Rune *classp; classp = runemalloc(DCLASS); n = 0; na = DCLASS; /* we have already seen the '[' */ if(*exprp == '^'){ classp[n++] = '\n'; /* don't match newline in negate case */ negateclass = TRUE; exprp++; }else negateclass = FALSE; while((c1 = nextrec()) != ']'){ if(c1 == '-'){ Error: free(classp); regxerror("malformed `[]'"); } if(n+4 >= na){ /* 3 runes plus NUL */ na += DCLASS; classp = runerealloc(classp, na); } if(*exprp == '-'){ exprp++; /* eat '-' */ if((c2 = nextrec()) == ']') goto Error; classp[n+0] = 0xFFFF; classp[n+1] = c1; classp[n+2] = c2; n += 3; }else classp[n++] = c1; } classp[n] = 0; if(nclass == Nclass){ Nclass += DCLASS; class = realloc(class, Nclass*sizeof(Rune*)); } class[nclass++] = classp; } int classmatch(int classno, int c, int negate) { Rune *p; p = class[classno]; while(*p){ if(*p == 0xFFFF){ if(p[1]<=c && c<=p[2]) return !negate; p += 3; }else if(*p++ == c) return !negate; } return negate; } /* * Note optimization in addinst: * *l must be pending when addinst called; if *l has been looked * at already, the optimization is a bug. */ int addinst(Ilist *l, Inst *inst, Rangeset *sep) { Ilist *p; for(p = l; p->inst; p++){ if(p->inst==inst){ if((sep)->r[0].q0 < p->se.r[0].q0) p->se= *sep; /* this would be bug */ return 0; /* It's already there */ } } p->inst = inst; p->se= *sep; (p+1)->inst = nil; return 1; } int rxnull(void) { return startinst==nil || bstartinst==nil; } /* either t!=nil or r!=nil, and we match the string in the appropriate place */ int rxexecute(Text *t, Rune *r, uint startp, uint eof, Rangeset *rp) { int flag; Inst *inst; Ilist *tlp; uint p; int nnl, ntl; int nc, c; int wrapped; int startchar; flag = 0; p = startp; startchar = 0; wrapped = 0; nnl = 0; if(startinst->typetype; list[0][0].inst = list[1][0].inst = nil; sel.r[0].q0 = -1; if(t != nil) nc = t->file->nc; else nc = runestrlen(r); /* Execute machine once for each character */ for(;;p++){ doloop: if(p>=eof || p>=nc){ switch(wrapped++){ case 0: /* let loop run one more click */ case 2: break; case 1: /* expired; wrap to beginning */ if(sel.r[0].q0>=0 || eof!=Infinity) goto Return; list[0][0].inst = list[1][0].inst = nil; p = 0; goto doloop; default: goto Return; } c = 0; }else{ if(((wrapped && p>=startp) || sel.r[0].q0>0) && nnl==0) break; if(t != nil) c = textreadc(t, p); else c = r[p]; } /* fast check for first char */ if(startchar && nnl==0 && c!=startchar) continue; tl = list[flag]; nl = list[flag^=1]; nl->inst = nil; ntl = nnl; nnl = 0; if(sel.r[0].q0<0 && (!wrapped || p= NLIST){ Overflow: warning(nil, "regexp list overflow\n"); sel.r[0].q0 = -1; goto Return; } } /* Execute machine until this list is empty */ for(tlp = tl; inst = tlp->inst; tlp++){ /* assignment = */ Switchstmt: switch(inst->type){ default: /* regular character */ if(inst->type==c){ Addinst: if(addinst(nl, inst->next, &tlp->se)) if(++nnl >= NLIST) goto Overflow; } break; case LBRA: if(inst->subid>=0) tlp->se.r[inst->subid].q0 = p; inst = inst->next; goto Switchstmt; case RBRA: if(inst->subid>=0) tlp->se.r[inst->subid].q1 = p; inst = inst->next; goto Switchstmt; case ANY: if(c!='\n') goto Addinst; break; case BOL: if(p==0 || (t!=nil && textreadc(t, p-1)=='\n') || (r!=nil && r[p-1]=='\n')){ Step: inst = inst->next; goto Switchstmt; } break; case EOL: if(c == '\n') goto Step; break; case CCLASS: if(c>=0 && classmatch(inst->class, c, 0)) goto Addinst; break; case NCCLASS: if(c>=0 && classmatch(inst->class, c, 1)) goto Addinst; break; case OR: /* evaluate right choice later */ if(addinst(tlp, inst->right, &tlp->se)) if(++ntl >= NLIST) goto Overflow; /* efficiency: advance and re-evaluate */ inst = inst->left; goto Switchstmt; case END: /* Match! */ tlp->se.r[0].q1 = p; newmatch(&tlp->se); break; } } } Return: *rp = sel; return sel.r[0].q0 >= 0; } void newmatch(Rangeset *sp) { if(sel.r[0].q0<0 || sp->r[0].q0r[0].q0==sel.r[0].q0 && sp->r[0].q1>sel.r[0].q1)) sel = *sp; } int rxbexecute(Text *t, uint startp, Rangeset *rp) { int flag; Inst *inst; Ilist *tlp; int p; int nnl, ntl; int c; int wrapped; int startchar; flag = 0; nnl = 0; wrapped = 0; p = startp; startchar = 0; if(bstartinst->typetype; list[0][0].inst = list[1][0].inst = nil; sel.r[0].q0= -1; /* Execute machine once for each character, including terminal NUL */ for(;;--p){ doloop: if(p <= 0){ switch(wrapped++){ case 0: /* let loop run one more click */ case 2: break; case 1: /* expired; wrap to end */ if(sel.r[0].q0>=0) goto Return; list[0][0].inst = list[1][0].inst = nil; p = t->file->nc; goto doloop; case 3: default: goto Return; } c = 0; }else{ if(((wrapped && p<=startp) || sel.r[0].q0>0) && nnl==0) break; c = textreadc(t, p-1); } /* fast check for first char */ if(startchar && nnl==0 && c!=startchar) continue; tl = list[flag]; nl = list[flag^=1]; nl->inst = nil; ntl = nnl; nnl = 0; if(sel.r[0].q0<0 && (!wrapped || p>startp)){ /* Add first instruction to this list */ /* the minus is so the optimizations in addinst work */ sempty.r[0].q0 = -p; if(addinst(tl, bstartinst, &sempty)) if(++ntl >= NLIST){ Overflow: warning(nil, "regexp list overflow\n"); sel.r[0].q0 = -1; goto Return; } } /* Execute machine until this list is empty */ for(tlp = tl; inst = tlp->inst; tlp++){ /* assignment = */ Switchstmt: switch(inst->type){ default: /* regular character */ if(inst->type == c){ Addinst: if(addinst(nl, inst->next, &tlp->se)) if(++nnl >= NLIST) goto Overflow; } break; case LBRA: if(inst->subid>=0) tlp->se.r[inst->subid].q0 = p; inst = inst->next; goto Switchstmt; case RBRA: if(inst->subid >= 0) tlp->se.r[inst->subid].q1 = p; inst = inst->next; goto Switchstmt; case ANY: if(c != '\n') goto Addinst; break; case BOL: if(c=='\n' || p==0){ Step: inst = inst->next; goto Switchstmt; } break; case EOL: if(pfile->nc && textreadc(t, p)=='\n') goto Step; break; case CCLASS: if(c>0 && classmatch(inst->class, c, 0)) goto Addinst; break; case NCCLASS: if(c>0 && classmatch(inst->class, c, 1)) goto Addinst; break; case OR: /* evaluate right choice later */ if(addinst(tl, inst->right, &tlp->se)) if(++ntl >= NLIST) goto Overflow; /* efficiency: advance and re-evaluate */ inst = inst->left; goto Switchstmt; case END: /* Match! */ tlp->se.r[0].q0 = -tlp->se.r[0].q0; /* minus sign */ tlp->se.r[0].q1 = p; bnewmatch(&tlp->se); break; } } } Return: *rp = sel; return sel.r[0].q0 >= 0; } void bnewmatch(Rangeset *sp) { int i; if(sel.r[0].q0<0 || sp->r[0].q0>sel.r[0].q1 || (sp->r[0].q0==sel.r[0].q1 && sp->r[0].q1r[i].q1; sel.r[i].q1 = sp->r[i].q0; } } i==RBRA || atorp[-1]>=pri){ switch(popator()){ case LBRA: op1 = popand('('); inst2 = newinst(RBRA); inst2->subid = *subidp; op1->last->next = inst2; inst1 = newinst(LBRA); inst1->subid = *subidp; inst1->next = op1->first; pushand(instnew/tags.c 664 0 0 4012 11256243402 10553ustar00nemosys#include #include #include #include #include #include #include #include #include #include #include #include #include "dat.h" #include "fns.h" enum { Maxcfg = 64 * 1024 }; typedef struct Ftag Ftag; struct Ftag{ Ftag* next; Reprog * reprog; Rune* text; int ntext; }; static Ftag *tags; Rune* tagforfile(Rune *fname, int nfname, int *lenp) { Ftag *tl; Resub match[2]; Rune *text; text = emalloc(sizeof(Rune)*(nfname+1)); memmove(text, fname, sizeof(Rune)*nfname); text[nfname] = 0; if(fname != nil) for(tl = tags; tl != nil; tl = tl->next){ memset(match, 0, sizeof(match)); if(rregexec(tl->reprog, text, match, 2) == 1){ free(text); *lenp = tl->ntext; return tl->text; } } free(text); return nil; } static int newtag(char *re, char *txt) { Ftag *tag; tag = emalloc(sizeof(Ftag)); tag->reprog = regcomp(re); if(tag->reprog == nil){ free(tag); return -1; } if(txt[0] == '|') txt = smprint("%s ", txt+1); else txt = smprint(" %s ", txt); if(txt == nil) sysfatal("no memory"); tag->text = bytetorune(txt, &tag->ntext); free(txt); tag->next = tags; tags = tag; return 0; } static int taglinenb; void regerror(char* e) { fprint(2, "%s: acmetags:%d: %s\n", argv0, taglinenb, e); threadexitsall("tag errors"); } void acmetagsinit(void) { char fname[100]; char *ln; char *t; Biobuf *bin; int i; snprint(fname, sizeof(fname), "%s/lib/acmetags", home); free(home); bin = Bopen(fname, OREAD); if(bin == nil) return; for(i = 1; (ln = Brdstr(bin, '\n', 1)) != nil; i++){ taglinenb = i; if(ln[0] == '#'){ free(ln); continue; } t = utfrune(ln, '\t'); if(t == nil) t = utfrune(ln, ' '); if(t == nil){ fprint(2, "acmetags:%d: no tag text\n", i); free(ln); continue; } *t++ = 0; while(*t == ' ' || *t == '\t') t++; if(*t != '\0') if(newtag(ln, t) < 0) fprint(2, "acmetags:%d: bad regexp\n", i); free(ln); } Bterm(bin); } /* 3 runes plus NUL */ na += DCLASS; classp = runerealloc(classp, na); } if(*exprp == '-'){ exprp++; /* eat '-' */ if((c2 = nextrec()) == ']') goto Error; classp[n+0] = 0xFFFF; classp[n+1] = c1; classp[n+2] = c2; n += 3; }else classp[n++] = c1; } classp[n] = 0; if(nclass == Nclass){ Nclass += DCLASS; class = realloc(class, Nclass*sizeof(Rune*)); } class[nclass++] = classp; } int classmatch(int classno, int c, int negate) { Rune *p; p = class[classnnew/wind.c 664 0 0 26174 11256243444 10621ustar00nemosys#include #include #include #include #include #include #include #include #include #include #include "dat.h" #include "fns.h" int winid; void wininit(Window *w, Window *clone, Rectangle r) { Rectangle r1, br; File *f; Reffont *rf; Rune *rp; int nc; w->tag.w = w; w->body.w = w; w->id = ++winid; incref(w); if(globalincref) incref(w); w->ctlfid = ~0; w->utflastqid = -1; r1 = r; r1.max.y = r1.min.y + font->height; incref(&reffont); f = fileaddtext(nil, &w->tag); textinit(&w->tag, f, r1, &reffont, tagcols); w->tag.what = Tag; /* tag is a copy of the contents, not a tracked image */ if(clone){ textdelete(&w->tag, 0, w->tag.file->nc, TRUE); nc = clone->tag.file->nc; rp = runemalloc(nc); bufread(clone->tag.file, 0, rp, nc); textinsert(&w->tag, 0, rp, nc, TRUE); free(rp); filereset(w->tag.file); textsetselect(&w->tag, nc, nc); } r1 = r; r1.min.y += font->height + 1; if(r1.max.y < r1.min.y) r1.max.y = r1.min.y; f = nil; if(clone){ f = clone->body.file; w->body.org = clone->body.org; w->isscratch = clone->isscratch; rf = rfget(FALSE, FALSE, FALSE, clone->body.reffont->f->name); }else rf = rfget(FALSE, FALSE, FALSE, nil); f = fileaddtext(f, &w->body); w->body.what = Body; textinit(&w->body, f, r1, rf, textcols); r1.min.y -= 1; r1.max.y = r1.min.y+1; draw(screen, r1, tagcols[BORD], nil, ZP); textscrdraw(&w->body); w->r = r; w->r.max.y = w->body.r.max.y; br.min = w->tag.scrollr.min; br.max.x = br.min.x + Dx(button->r); br.max.y = br.min.y + Dy(button->r); draw(screen, br, button, nil, button->r.min); w->filemenu = TRUE; w->maxlines = w->body.maxlines; w->autoindent = globalautoindent; if(clone){ w->dirty = clone->dirty; textsetselect(&w->body, clone->body.q0, clone->body.q1); winsettag(w); w->autoindent = clone->autoindent; } } int winresize(Window *w, Rectangle r, int safe) { Rectangle r1; int y; Image *b; Rectangle br; r1 = r; r1.max.y = r1.min.y + font->height; y = r1.max.y; if(!safe || !eqrect(w->tag.r, r1)){ y = textresize(&w->tag, r1); b = button; if(w->body.file->mod && !w->isdir && !w->isscratch) b = modbutton; br.min = w->tag.scrollr.min; br.max.x = br.min.x + Dx(b->r); br.max.y = br.min.y + Dy(b->r); draw(screen, br, b, nil, b->r.min); } if(!safe || !eqrect(w->body.r, r1)){ if(y+1+font->height > r.max.y){ /* no body */ r1.min.y = y; r1.max.y = y; textresize(&w->body, r1); w->r = r; w->r.max.y = y; return y; } r1 = r; r1.min.y = y; r1.max.y = y + 1; draw(screen, r1, tagcols[BORD], nil, ZP); r1.min.y = y + 1; r1.max.y = r.max.y; y = textresize(&w->body, r1); w->r = r; w->r.max.y = y; textscrdraw(&w->body); } w->maxlines = min(w->body.nlines, max(w->maxlines, w->body.maxlines)); return w->r.max.y; } void winlock1(Window *w, int owner) { incref(w); qlock(w); w->owner = owner; } void winlock(Window *w, int owner) { int i; File *f; f = w->body.file; for(i=0; intext; i++) winlock1(f->text[i]->w, owner); } void winunlock(Window *w) { int i; File *f; /* * subtle: loop runs backwards to avoid tripping over * winclose indirectly editing f->text and freeing f * on the last iteration of the loop. */ f = w->body.file; for(i=f->ntext-1; i>=0; i--){ w = f->text[i]->w; w->owner = 0; qunlock(w); winclose(w); } } void winmousebut(Window *w) { moveto(mousectl, divpt(addpt(w->tag.scrollr.min, w->tag.scrollr.max), 2)); } void windirfree(Window *w) { int i; Dirlist *dl; if(w->isdir){ for(i=0; indl; i++){ dl = w->dlp[i]; free(dl->r); free(dl); } free(w->dlp); } w->dlp = nil; w->ndl = 0; } void winclose(Window *w) { int i; if(decref(w) == 0){ windirfree(w); textclose(&w->tag); textclose(&w->body); if(activewin == w) activewin = nil; for(i=0; inincl; i++) free(w->incl[i]); free(w->incl); free(w->events); free(w); } } void windelete(Window *w) { Xfid *x; x = w->eventx; if(x){ w->nevents = 0; free(w->events); w->events = nil; w->eventx = nil; sendp(x->c, nil); /* wake him up */ } } void winundo(Window *w, int isundo) { Text *body; int i; File *f; Window *v; w->utflastqid = -1; body = &w->body; fileundo(body->file, isundo, &body->q0, &body->q1); textshow(body, body->q0, body->q1, 1); f = body->file; for(i=0; intext; i++){ v = f->text[i]->w; v->dirty = (f->seq != v->putseq); if(v != w){ v->body.q0 = v->body.p0+v->body.org; v->body.q1 = v->body.p1+v->body.org; } } winsettag(w); } void winsetname(Window *w, Rune *name, int n) { Text *t; Window *v; int i; t = &w->body; if(runeeq(t->file->name, t->file->nname, name, n) == TRUE) return; w->isscratch = FALSE; if(n>=6 && runeeq(L"/guide", 6, name+(n-6), 6)) w->isscratch = TRUE; else if(n>=7 && runeeq(L"+Errors", 7, name+(n-7), 7)) w->isscratch = TRUE; filesetname(t->file, name, n); for(i=0; ifile->ntext; i++){ v = t->file->text[i]->w; winsettag(v); v->isscratch = w->isscratch; } } void wintype(Window *w, Text *t, Rune r) { int i; texttype(t, r); if(t->what == Body) for(i=0; ifile->ntext; i++) textscrdraw(t->file->text[i]); winsettag(w); } void wincleartag(Window *w) { int i, n; Rune *r; /* w must be committed */ n = w->tag.file->nc; r = runemalloc(n); bufread(w->tag.file, 0, r, n); for(i=0; itag, i, n, TRUE); free(r); w->tag.file->mod = FALSE; if(w->tag.q0 > i) w->tag.q0 = i; if(w->tag.q1 > i) w->tag.q1 = i; textsetselect(&w->tag, w->tag.q0, w->tag.q1); } void winsettag1(Window *w) { int i, j, k, n, nft, bar, dirty; Rune *new, *old, *r, *ft; Image *b; uint q0, q1; Rectangle br; /* there are races that get us here with stuff in the tag cache, so we take extra care to sync it */ if(w->tag.ncache!=0 || w->tag.file->mod) wincommit(w, &w->tag); /* check file name; also guarantees we can modify tag contents */ old = runemalloc(w->tag.file->nc+1); bufread(w->tag.file, 0, old, w->tag.file->nc); old[w->tag.file->nc] = '\0'; for(i=0; itag.file->nc; i++) if(old[i]==' ' || old[i]=='\t') break; if(runeeq(old, i, w->body.file->name, w->body.file->nname) == FALSE){ textdelete(&w->tag, 0, i, TRUE); textinsert(&w->tag, 0, w->body.file->name, w->body.file->nname, TRUE); free(old); old = runemalloc(w->tag.file->nc+1); bufread(w->tag.file, 0, old, w->tag.file->nc); old[w->tag.file->nc] = '\0'; } new = runemalloc(w->body.file->nname+100); i = 0; runemove(new+i, w->body.file->name, w->body.file->nname); i += w->body.file->nname; runemove(new+i, L" Del Snarf", 10); i += 10; if(w->filemenu){ if(w->body.file->delta.nc>0 || w->body.ncache){ runemove(new+i, L" Undo", 5); i += 5; } if(w->body.file->epsilon.nc > 0){ runemove(new+i, L" Redo", 5); i += 5; } dirty = w->body.file->nname && (w->body.ncache || w->body.file->seq!=w->putseq); if(!w->isdir && dirty){ runemove(new+i, L" Put", 4); i += 4; } } if(w->isdir){ runemove(new+i, L" Get", 4); i += 4; } runemove(new+i, L" |", 2); i += 2; r = runestrchr(old, '|'); if(r) k = r-old+1; else{ k = w->tag.file->nc; if(w->body.file->seq == 0){ ft = tagforfile(w->body.file->name, w->body.file->nname,&nft); if(ft == nil){ runemove(new+i, L" Look ", 6); i += 6; }else{ runemove(new+i, ft, nft); i += nft; } } } if(runeeq(new, i, old, k) == FALSE){ n = k; if(n > i) n = i; for(j=0; jtag.q0; q1 = w->tag.q1; textdelete(&w->tag, j, k, TRUE); textinsert(&w->tag, j, new+j, i-j, TRUE); /* try to preserve user selection */ r = runestrchr(old, '|'); if(r){ bar = r-old; if(q0 > bar){ bar = (runestrchr(new, '|')-new)-bar; w->tag.q0 = q0+bar; w->tag.q1 = q1+bar; } } } free(old); free(new); w->tag.file->mod = FALSE; n = w->tag.file->nc+w->tag.ncache; if(w->tag.q0 > n) w->tag.q0 = n; if(w->tag.q1 > n) w->tag.q1 = n; textsetselect(&w->tag, w->tag.q0, w->tag.q1); b = button; if(!w->isdir && !w->isscratch && (w->body.file->mod || w->body.ncache)) b = modbutton; br.min = w->tag.scrollr.min; br.max.x = br.min.x + Dx(b->r); br.max.y = br.min.y + Dy(b->r); draw(screen, br, b, nil, b->r.min); } void winsettag(Window *w) { int i; File *f; Window *v; f = w->body.file; for(i=0; intext; i++){ v = f->text[i]->w; if(v->col->safe || v->body.maxlines>0) winsettag1(v); } } void wincommit(Window *w, Text *t) { Rune *r; int i; File *f; textcommit(t, TRUE); f = t->file; if(f->ntext > 1) for(i=0; intext; i++) textcommit(f->text[i], FALSE); /* no-op for t */ if(t->what == Body) return; r = runemalloc(w->tag.file->nc); bufread(w->tag.file, 0, r, w->tag.file->nc); for(i=0; itag.file->nc; i++) if(r[i]==' ' || r[i]=='\t') break; if(runeeq(r, i, w->body.file->name, w->body.file->nname) == FALSE){ seq++; filemark(w->body.file); w->body.file->mod = TRUE; w->dirty = TRUE; winsetname(w, r, i); winsettag(w); } free(r); } void winaddincl(Window *w, Rune *r, int n) { char *a; Dir *d; Runestr rs; a = runetobyte(r, n); d = dirstat(a); if(d == nil){ if(a[0] == '/') goto Rescue; rs = dirname(&w->body, r, n); r = rs.r; n = rs.nr; free(a); a = runetobyte(r, n); d = dirstat(a); if(d == nil) goto Rescue; r = runerealloc(r, n+1); r[n] = 0; } free(a); if((d->qid.type&QTDIR) == 0){ free(d); warning(nil, "%s: not a directory\n", a); free(r); return; } free(d); w->nincl++; w->incl = realloc(w->incl, w->nincl*sizeof(Rune*)); memmove(w->incl+1, w->incl, (w->nincl-1)*sizeof(Rune*)); w->incl[0] = runemalloc(n+1); runemove(w->incl[0], r, n); free(r); return; Rescue: warning(nil, "%s: %r\n", a); free(r); free(a); return; } int winclean(Window *w, int conservative) /* as it stands, conservative is always TRUE */ { if(w->isscratch || w->isdir) /* don't whine if it's a guide file, error window, etc. */ return TRUE; if(!conservative && w->nopen[QWevent]>0) return TRUE; if(w->dirty){ if(w->body.file->nname) warning(nil, "%.*S modified\n", w->body.file->nname, w->body.file->name); else{ if(w->body.file->nc < 100) /* don't whine if it's too small */ return TRUE; warning(nil, "unnamed file modified\n"); } w->dirty = FALSE; return FALSE; } return TRUE; } char* winctlprint(Window *w, char *buf, int fonts) { sprint(buf, "%11d %11d %11d %11d %11d ", w->id, w->tag.file->nc, w->body.file->nc, w->isdir, w->dirty); if(fonts) return smprint("%s%11d %q %11d " , buf, Dx(w->body.r), w->body.reffont->f->name, w->body.maxtab); return buf; } void winevent(Window *w, char *fmt, ...) { int n; char *b; Xfid *x; va_list arg; if(w->nopen[QWevent] == 0) return; if(w->owner == 0) error("no window owner"); va_start(arg, fmt); b = vsmprint(fmt, arg); va_end(arg); if(b == nil) error("vsmprint failed"); n = strlen(b); w->events = realloc(w->events, w->nevents+1+n); w->events[w->nevents++] = w->owner; memmove(w->events+w->nevents, b, n); free(b); w->nevents += n; x = w->eventx; if(x){ w->eventx = nil; sendp(x->c, nil); } } atch = clone->isscratch; rf = rfget(FALSE, FALSE, FALSE, clone->body.reffont->f->name); }else rf = rfget(FALSE, FALSE, FALSE, nil); f = fileaddtext(f, &w->body); w->body.what = Body; textinit(&w->body, f, r1, rf, textcols); r1.min.y -= 1; r1.max.y = r1.min.y+1; draw(screen, r1, tagcols[BORD], nil, ZP); textscrdraw(&w->body); w->r = r; w->r.max.y = w->body.r.max.y; br.min