21 Window* openfile(Text*, Expand*);
31 threadsetname("plumbproc");
33 m = plumbrecvfid(plumbeditfid);
43 plumbeditfid = plumbopenfid("edit", OREAD|OCEXEC);
44 if(plumbeditfid == nil)
45 fprint(2, "acme: can't initialize plumber: %r\n");
47 cplumb = chancreate(sizeof(Plumbmsg*), 0);
48 threadcreate(plumbproc, nil, STACK);
50 plumbsendfid = plumbopenfid("send", OWRITE|OCEXEC);
55 look3(Text *t, uint q0, uint q1, int external)
57 int n, c, f, expanded;
69 expanded = expand(t, q0, q1, &e);
70 if(!external && t->w!=nil && t->w->nopen[QWevent]>0){
71 /* send alphanumeric expansion to external client */
75 if((e.u.at!=nil && t->w!=nil) || (e.nname>0 && lookfile(e.name, e.nname)!=nil))
76 f = 1; /* acme can do it without loading a file */
77 if(q0!=e.q0 || q1!=e.q1)
78 f |= 2; /* second (post-expand) message follows */
80 f |= 4; /* it's a file name */
87 bufread(&t->file->b, q0, r, n);
88 winevent(t->w, "%c%d %d %d %d %.*S\n", c, q0, q1, f, n, n, r);
91 winevent(t->w, "%c%d %d %d 0 \n", c, q0, q1, f, n);
92 if(q0==e.q0 && q1==e.q1)
99 runemove(r, e.name, e.nname);
102 bufread(&e.u.at->file->b, e.a0, r+e.nname+1, e.a1-e.a0);
107 bufread(&t->file->b, e.q0, r, n);
111 winevent(t->w, "%c%d %d %d %d %.*S\n", c, e.q0, e.q1, f, n, n, r);
113 winevent(t->w, "%c%d %d %d 0 \n", c, e.q0, e.q1, f, n);
117 if(plumbsendfid != nil){
118 /* send whitespace-delimited word to plumber */
119 m = emalloc(sizeof(Plumbmsg));
120 m->src = estrdup("acme");
122 dir = dirname(t, nil, 0);
123 if(dir.nr==1 && dir.r[0]=='.'){ /* sigh */
129 m->wdir = estrdup(wdir);
131 m->wdir = runetobyte(dir.r, dir.nr);
133 m->type = estrdup("text");
137 if(t->q1>t->q0 && t->q0<=q0 && q0<=t->q1){
142 while(q0>0 && (c=tgetc(t, q0-1))!=' ' && c!='\t' && c!='\n')
144 while(q1<t->file->b.nc && (c=tgetc(t, q1))!=' ' && c!='\t' && c!='\n')
150 sprint(buf, "click=%d", p-q0);
151 m->attr = plumbunpackattr(buf);
154 r = runemalloc(q1-q0);
155 bufread(&t->file->b, q0, r, q1-q0);
156 m->data = runetobyte(r, q1-q0);
157 m->ndata = strlen(m->data);
159 if(m->ndata<messagesize-1024 && plumbsendtofid(plumbsendfid, m) >= 0){
164 /* plumber failed to match; fall through */
167 /* interpret alphanumeric string ourselves */
168 if(expanded == FALSE)
179 textsetselect(ct, e.q1, e.q1);
182 bufread(&t->file->b, e.q0, r, n);
183 if(search(ct, r, n) && e.jump)
184 moveto(mousectl, addpt(frptofchar(&ct->fr, ct->fr.p0), Pt(4, ct->fr.font->height-4)));
196 plumbgetc(void *a, uint n)
201 if(n<0 || n>runestrlen(r))
207 plumblook(Plumbmsg *m)
212 if(m->ndata >= BUFSIZE){
213 warning(nil, "insanely long file name (%d bytes) in plumb message (%.32s...)\n", m->ndata, m->data);
218 if(m->data[0] == '\0')
222 e.name = bytetorune(e.bname, &e.nname);
226 addr = plumblookup(m->attr, "addr");
228 e.u.ar = bytetorune(addr, &e.a1);
238 plumbshow(Plumbmsg *m)
244 char *name, *p, namebuf[16];
246 w = makenewwindow(nil);
247 name = plumblookup(m->attr, "filename");
251 snprint(namebuf, sizeof namebuf, "Untitled-%d", nuntitled);
254 if(name[0]!='/' && m->wdir!=nil && m->wdir[0]!='\0'){
255 nb = strlen(m->wdir) + 1 + strlen(name) + 1;
257 snprint(p, nb, "%s/%s", m->wdir, name);
260 cvttorunes(name, strlen(name), rb, &nb, &nr, nil);
262 rs = cleanrname(runestr(rb, nr));
263 winsetname(w, rs.r, rs.nr);
264 r = runemalloc(m->ndata);
265 cvttorunes(m->data, m->ndata, r, &nb, &nr, nil);
266 textinsert(&w->body, 0, r, nr, TRUE);
268 w->body.file->mod = FALSE;
271 textscrdraw(&w->body);
272 textsetselect(&w->tag, w->tag.file->b.nc, w->tag.file->b.nc);
277 search(Text *ct, Rune *r, uint n)
283 if(n==0 || n>ct->file->b.nc)
286 warning(nil, "string too long\n");
289 maxn = max(2*n, RBUFSIZE);
297 if(q >= ct->file->b.nc){
304 c = runestrchr(b, r[0]);
309 if(around && q>=ct->q1)
317 /* reload if buffer covers neither string nor rest of file */
318 if(nb<n && nb!=ct->file->b.nc-q){
319 nb = ct->file->b.nc-q;
322 bufread(&ct->file->b, q, s, nb);
326 /* this runeeq is fishy but the null at b[nb] makes it safe */
327 if(runeeq(b, n, r, n)==TRUE){
329 textshow(ct, q, q+n, 1);
339 if(around && q>=ct->q1)
352 static Rune Lx[] = { '.', '-', '+', '/', ':', 0 };
355 if(runestrchr(Lx, r))
360 /* Runestr wrapper for cleanname */
362 cleanrname(Runestr rs)
367 s = runetobyte(rs.r, rs.nr);
369 cvttorunes(s, strlen(s), rs.r, &nb, &rs.nr, &nulls);
375 includefile(Rune *dir, Rune *file, int nfile)
380 static Rune Lslash[] = { '/', 0 };
383 a = emalloc((m+1+nfile)*UTFmax+1);
384 sprint(a, "%S/%.*S", dir, nfile, file);
388 return runestr(nil, 0);
389 r = runemalloc(m+1+nfile);
391 runemove(r+m, Lslash, 1);
392 runemove(r+m+1, file, nfile);
394 return cleanrname(runestr(r, m+1+nfile));
400 includename(Text *t, Rune *r, int n)
404 Rune Lsysinclude[] = { '/', 's', 'y', 's', '/', 'i', 'n', 'c', 'l', 'u', 'd', 'e', 0 };
405 Rune Lusrinclude[] = { '/', 'u', 's', 'r', '/', 'i', 'n', 'c', 'l', 'u', 'd', 'e', 0 };
406 Rune Lusrlocalinclude[] = { '/', 'u', 's', 'r', '/', 'l', 'o', 'c', 'a', 'l',
407 '/', 'i', 'n', 'c', 'l', 'u', 'd', 'e', 0 };
408 Rune Lusrlocalplan9include[] = { '/', 'u', 's', 'r', '/', 'l', 'o', 'c', 'a', 'l',
409 '/', 'p', 'l', 'a', 'n', '9', '/', 'i', 'n', 'c', 'l', 'u', 'd', 'e', 0 };
413 if(objdir==nil && objtype!=nil){
414 sprint(buf, "/%s/include", objtype);
415 objdir = bytetorune(buf, &i);
416 objdir = runerealloc(objdir, i+1);
421 if(n==0 || r[0]=='/' || w==nil)
423 if(n>2 && r[0]=='.' && r[1]=='/')
427 for(i=0; i<w->nincl && file.r==nil; i++)
428 file = includefile(w->incl[i], r, n);
431 file = includefile(Lsysinclude, r, n);
433 file = includefile(Lusrlocalplan9include, r, n);
435 file = includefile(Lusrlocalinclude, r, n);
437 file = includefile(Lusrinclude, r, n);
438 if(file.r==nil && objdir!=nil)
439 file = includefile(objdir, r, n);
445 return runestr(r, n);
449 dirname(Text *t, Rune *r, int n)
457 if(t==nil || t->w==nil)
459 nt = t->w->tag.file->b.nc;
462 if(n>=1 && r[0]=='/')
464 b = runemalloc(nt+n+1);
465 bufread(&t->w->tag.file->b, 0, b, nt);
471 if(c==' ' || c=='\t')
476 runemove(b+slash+1, r, n);
478 return cleanrname(runestr(b, slash+1+n));
484 return cleanrname(tmp);
489 expandfile(Text *t, uint q0, uint q1, Expand *e)
491 int i, n, nname, colon, eval;
500 while(q1<t->file->b.nc && isfilec(c=textreadc(t, q1))){
507 while(q0>0 && (isfilec(c=textreadc(t, q0-1)) || isaddrc(c) || isregexc(c))){
509 if(colon<0 && c==':')
513 * if it looks like it might begin file: , consume address chars after :
514 * otherwise terminate expansion at :
518 if(colon<t->file->b.nc-1 && isaddrc(textreadc(t, colon+1))){
520 while(q1<t->file->b.nc-1 && isaddrc(textreadc(t, q1)))
525 if(colon >= 0){ /* stop at white space */
526 for(amax=colon+1; amax<t->file->b.nc; amax++)
527 if((c=textreadc(t, amax))==' ' || c=='\t' || c=='\n')
530 amax = t->file->b.nc;
538 /* see if it's a file name */
540 bufread(&t->file->b, q0, r, n);
541 /* first, does it have bad chars? */
545 if(c==':' && nname<0){
546 if(q0+i+1<t->file->b.nc && (i==n-1 || isaddrc(textreadc(t, q0+i+1))))
555 for(i=0; i<nname; i++)
559 * See if it's a file name in <>, and turn that into an include
560 * file name if so. Should probably do it for "" too, but that's not
561 * restrictive enough syntax and checking for a #include earlier on the
562 * line would be silly.
564 if(q0>0 && textreadc(t, q0-1)=='<' && q1<t->file->b.nc && textreadc(t, q1)=='>'){
565 rs = includename(t, r, nname);
572 rs = dirname(t, r, nname);
576 e->bname = runetobyte(r, nname);
577 /* if it's already a window name, it's a file */
578 w = lookfile(r, nname);
581 /* if it's the name of a file, it's a file */
582 if(access(e->bname, 0) < 0){
594 address(TRUE, nil, range(-1,-1), range(0,0), t, e->a0, amax, tgetc, &eval, (uint*)&e->a1);
603 expand(Text *t, uint q0, uint q1, Expand *e)
605 memset(e, 0, sizeof *e);
607 /* if in selection, choose selection */
609 if(q1==q0 && t->q1>t->q0 && t->q0<=q0 && q0<=t->q1){
616 if(expandfile(t, q0, q1, e))
620 while(q1<t->file->b.nc && isalnum(textreadc(t, q1)))
622 while(q0>0 && isalnum(textreadc(t, q0-1)))
631 lookfile(Rune *s, int n)
638 /* avoid terminal slash on directories */
639 if(n>1 && s[n-1] == '/')
641 for(j=0; j<row.ncol; j++){
643 for(i=0; i<c->nw; i++){
647 if(k>1 && t->file->name[k-1] == '/')
649 if(runeeq(t->file->name, k, s, n)){
650 w = w->body.file->curtext->w;
651 if(w->col != nil) /* protect against race deleting w */
660 lookid(int id, int dump)
666 for(j=0; j<row.ncol; j++){
668 for(i=0; i<c->nw; i++){
670 if(dump && w->dumpid == id)
672 if(!dump && w->id == id)
681 openfile(Text *t, Expand *e)
694 w = lookfile(e->name, e->nname);
697 if(!t->col->safe && t->fr.maxlines==0) /* window is obscured by full-column window */
698 colgrow(t->col, t->col->w[0], 1);
703 w = makenewwindow(t);
705 winsetname(w, e->name, e->nname);
706 textload(t, 0, e->bname, 1);
707 t->file->mod = FALSE;
710 textsetselect(&t->w->tag, t->w->tag.file->b.nc, t->w->tag.file->b.nc);
712 for(i=ow->nincl; --i>=0; ){
713 n = runestrlen(ow->incl[i]);
715 runemove(rp, ow->incl[i], n);
716 winaddincl(w, rp, n);
718 w->autoindent = ow->autoindent;
720 w->autoindent = globalautoindent;
726 r = address(TRUE, t, range(-1,-1), range(t->q0, t->q1), e->u.at, e->a0, e->a1, e->agetc, &eval, &dummy);
728 e->jump = FALSE; /* don't jump if invalid address */
734 textshow(t, r.q0, r.q1, 1);
738 moveto(mousectl, addpt(frptofchar(&t->fr, t->fr.p0), Pt(4, font->height-4)));
743 new(Text *et, Text *t, Text *argt, int flag1, int flag2, Rune *arg, int narg)
751 getarg(argt, FALSE, TRUE, &a, &na);
753 new(et, t, nil, flag1, flag2, a, na);
757 /* loop condition: *arg is not a blank */
758 for(ndone=0; ; ndone++){
759 a = findbl(arg, narg, &na);
761 if(ndone==0 && et->col!=nil)
762 winsettag(coladd(et->col, nil, nil, -1));
767 runemove(f, arg, nf);
768 rs = dirname(et, f, nf);
771 memset(&e, 0, sizeof e);
774 e.bname = runetobyte(f, nf);
779 arg = skipbl(a, na, &narg);