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 chansetname(cplumb, "cplumb");
49 threadcreate(plumbproc, nil, STACK);
51 plumbsendfid = plumbopenfid("send", OWRITE|OCEXEC);
56 look3(Text *t, uint q0, uint q1, int external)
58 int n, c, f, expanded;
70 expanded = expand(t, q0, q1, &e);
71 if(!external && t->w!=nil && t->w->nopen[QWevent]>0){
72 /* send alphanumeric expansion to external client */
76 if((e.u.at!=nil && t->w!=nil) || (e.nname>0 && lookfile(e.name, e.nname)!=nil))
77 f = 1; /* acme can do it without loading a file */
78 if(q0!=e.q0 || q1!=e.q1)
79 f |= 2; /* second (post-expand) message follows */
81 f |= 4; /* it's a file name */
88 bufread(&t->file->b, q0, r, n);
89 winevent(t->w, "%c%d %d %d %d %.*S\n", c, q0, q1, f, n, n, r);
92 winevent(t->w, "%c%d %d %d 0 \n", c, q0, q1, f, n);
93 if(q0==e.q0 && q1==e.q1)
100 runemove(r, e.name, e.nname);
103 bufread(&e.u.at->file->b, e.a0, r+e.nname+1, e.a1-e.a0);
108 bufread(&t->file->b, e.q0, r, n);
112 winevent(t->w, "%c%d %d %d %d %.*S\n", c, e.q0, e.q1, f, n, n, r);
114 winevent(t->w, "%c%d %d %d 0 \n", c, e.q0, e.q1, f, n);
118 if(plumbsendfid != nil){
119 /* send whitespace-delimited word to plumber */
120 m = emalloc(sizeof(Plumbmsg));
121 m->src = estrdup("acme");
123 dir = dirname(t, nil, 0);
124 if(dir.nr==1 && dir.r[0]=='.'){ /* sigh */
130 m->wdir = estrdup(wdir);
132 m->wdir = runetobyte(dir.r, dir.nr);
134 m->type = estrdup("text");
138 if(t->q1>t->q0 && t->q0<=q0 && q0<=t->q1){
143 while(q0>0 && (c=tgetc(t, q0-1))!=' ' && c!='\t' && c!='\n')
145 while(q1<t->file->b.nc && (c=tgetc(t, q1))!=' ' && c!='\t' && c!='\n')
151 sprint(buf, "click=%d", p-q0);
152 m->attr = plumbunpackattr(buf);
155 r = runemalloc(q1-q0);
156 bufread(&t->file->b, q0, r, q1-q0);
157 m->data = runetobyte(r, q1-q0);
158 m->ndata = strlen(m->data);
160 if(m->ndata<messagesize-1024 && plumbsendtofid(plumbsendfid, m) >= 0){
165 /* plumber failed to match; fall through */
168 /* interpret alphanumeric string ourselves */
169 if(expanded == FALSE)
180 textsetselect(ct, e.q1, e.q1);
183 bufread(&t->file->b, e.q0, r, n);
184 if(search(ct, r, n) && e.jump)
185 moveto(mousectl, addpt(frptofchar(&ct->fr, ct->fr.p0), Pt(4, ct->fr.font->height-4)));
197 plumbgetc(void *a, uint n)
202 if(n<0 || n>runestrlen(r))
208 plumblook(Plumbmsg *m)
213 if(m->ndata >= BUFSIZE){
214 warning(nil, "insanely long file name (%d bytes) in plumb message (%.32s...)\n", m->ndata, m->data);
219 if(m->data[0] == '\0')
223 e.name = bytetorune(e.bname, &e.nname);
227 addr = plumblookup(m->attr, "addr");
229 e.u.ar = bytetorune(addr, &e.a1);
239 plumbshow(Plumbmsg *m)
245 char *name, *p, namebuf[16];
247 w = makenewwindow(nil);
248 name = plumblookup(m->attr, "filename");
252 snprint(namebuf, sizeof namebuf, "Untitled-%d", nuntitled);
255 if(name[0]!='/' && m->wdir!=nil && m->wdir[0]!='\0'){
256 nb = strlen(m->wdir) + 1 + strlen(name) + 1;
258 snprint(p, nb, "%s/%s", m->wdir, name);
261 cvttorunes(name, strlen(name), rb, &nb, &nr, nil);
263 rs = cleanrname(runestr(rb, nr));
264 winsetname(w, rs.r, rs.nr);
265 r = runemalloc(m->ndata);
266 cvttorunes(m->data, m->ndata, r, &nb, &nr, nil);
267 textinsert(&w->body, 0, r, nr, TRUE);
269 w->body.file->mod = FALSE;
272 textscrdraw(&w->body);
273 textsetselect(&w->tag, w->tag.file->b.nc, w->tag.file->b.nc);
278 search(Text *ct, Rune *r, uint n)
284 if(n==0 || n>ct->file->b.nc)
287 warning(nil, "string too long\n");
290 maxn = max(2*n, RBUFSIZE);
298 if(q >= ct->file->b.nc){
305 c = runestrchr(b, r[0]);
310 if(around && q>=ct->q1)
318 /* reload if buffer covers neither string nor rest of file */
319 if(nb<n && nb!=ct->file->b.nc-q){
320 nb = ct->file->b.nc-q;
323 bufread(&ct->file->b, q, s, nb);
327 /* this runeeq is fishy but the null at b[nb] makes it safe */
328 if(runeeq(b, n, r, n)==TRUE){
330 textshow(ct, q, q+n, 1);
340 if(around && q>=ct->q1)
353 static Rune Lx[] = { '.', '-', '+', '/', ':', 0 };
356 if(runestrchr(Lx, r))
361 /* Runestr wrapper for cleanname */
363 cleanrname(Runestr rs)
368 s = runetobyte(rs.r, rs.nr);
370 cvttorunes(s, strlen(s), rs.r, &nb, &rs.nr, &nulls);
376 includefile(Rune *dir, Rune *file, int nfile)
381 static Rune Lslash[] = { '/', 0 };
384 a = emalloc((m+1+nfile)*UTFmax+1);
385 sprint(a, "%S/%.*S", dir, nfile, file);
389 return runestr(nil, 0);
390 r = runemalloc(m+1+nfile);
392 runemove(r+m, Lslash, 1);
393 runemove(r+m+1, file, nfile);
395 return cleanrname(runestr(r, m+1+nfile));
401 includename(Text *t, Rune *r, int n)
405 Rune Lsysinclude[] = { '/', 's', 'y', 's', '/', 'i', 'n', 'c', 'l', 'u', 'd', 'e', 0 };
406 Rune Lusrinclude[] = { '/', 'u', 's', 'r', '/', 'i', 'n', 'c', 'l', 'u', 'd', 'e', 0 };
407 Rune Lusrlocalinclude[] = { '/', 'u', 's', 'r', '/', 'l', 'o', 'c', 'a', 'l',
408 '/', 'i', 'n', 'c', 'l', 'u', 'd', 'e', 0 };
409 Rune Lusrlocalplan9include[] = { '/', 'u', 's', 'r', '/', 'l', 'o', 'c', 'a', 'l',
410 '/', 'p', 'l', 'a', 'n', '9', '/', 'i', 'n', 'c', 'l', 'u', 'd', 'e', 0 };
414 if(objdir==nil && objtype!=nil){
415 sprint(buf, "/%s/include", objtype);
416 objdir = bytetorune(buf, &i);
417 objdir = runerealloc(objdir, i+1);
422 if(n==0 || r[0]=='/' || w==nil)
424 if(n>2 && r[0]=='.' && r[1]=='/')
428 for(i=0; i<w->nincl && file.r==nil; i++)
429 file = includefile(w->incl[i], r, n);
432 file = includefile(Lsysinclude, r, n);
434 file = includefile(Lusrlocalplan9include, r, n);
436 file = includefile(Lusrlocalinclude, r, n);
438 file = includefile(Lusrinclude, r, n);
439 if(file.r==nil && objdir!=nil)
440 file = includefile(objdir, r, n);
446 return runestr(r, n);
450 dirname(Text *t, Rune *r, int n)
458 if(t==nil || t->w==nil)
460 nt = t->w->tag.file->b.nc;
463 if(n>=1 && r[0]=='/')
465 b = runemalloc(nt+n+1);
466 bufread(&t->w->tag.file->b, 0, b, nt);
472 if(c==' ' || c=='\t')
477 runemove(b+slash+1, r, n);
479 return cleanrname(runestr(b, slash+1+n));
485 return cleanrname(tmp);
490 expandfile(Text *t, uint q0, uint q1, Expand *e)
492 int i, n, nname, colon, eval;
501 while(q1<t->file->b.nc && isfilec(c=textreadc(t, q1))){
508 while(q0>0 && (isfilec(c=textreadc(t, q0-1)) || isaddrc(c) || isregexc(c))){
510 if(colon<0 && c==':')
514 * if it looks like it might begin file: , consume address chars after :
515 * otherwise terminate expansion at :
519 if(colon<t->file->b.nc-1 && isaddrc(textreadc(t, colon+1))){
521 while(q1<t->file->b.nc-1 && isaddrc(textreadc(t, q1)))
526 if(colon >= 0){ /* stop at white space */
527 for(amax=colon+1; amax<t->file->b.nc; amax++)
528 if((c=textreadc(t, amax))==' ' || c=='\t' || c=='\n')
531 amax = t->file->b.nc;
539 /* see if it's a file name */
541 bufread(&t->file->b, q0, r, n);
542 /* first, does it have bad chars? */
546 if(c==':' && nname<0){
547 if(q0+i+1<t->file->b.nc && (i==n-1 || isaddrc(textreadc(t, q0+i+1))))
556 for(i=0; i<nname; i++)
560 * See if it's a file name in <>, and turn that into an include
561 * file name if so. Should probably do it for "" too, but that's not
562 * restrictive enough syntax and checking for a #include earlier on the
563 * line would be silly.
565 if(q0>0 && textreadc(t, q0-1)=='<' && q1<t->file->b.nc && textreadc(t, q1)=='>'){
566 rs = includename(t, r, nname);
573 rs = dirname(t, r, nname);
577 e->bname = runetobyte(r, nname);
578 /* if it's already a window name, it's a file */
579 w = lookfile(r, nname);
582 /* if it's the name of a file, it's a file */
583 if(access(e->bname, 0) < 0){
595 address(TRUE, nil, range(-1,-1), range(0,0), t, e->a0, amax, tgetc, &eval, (uint*)&e->a1);
604 expand(Text *t, uint q0, uint q1, Expand *e)
606 memset(e, 0, sizeof *e);
608 /* if in selection, choose selection */
610 if(q1==q0 && t->q1>t->q0 && t->q0<=q0 && q0<=t->q1){
617 if(expandfile(t, q0, q1, e))
621 while(q1<t->file->b.nc && isalnum(textreadc(t, q1)))
623 while(q0>0 && isalnum(textreadc(t, q0-1)))
632 lookfile(Rune *s, int n)
639 /* avoid terminal slash on directories */
640 if(n>1 && s[n-1] == '/')
642 for(j=0; j<row.ncol; j++){
644 for(i=0; i<c->nw; i++){
648 if(k>1 && t->file->name[k-1] == '/')
650 if(runeeq(t->file->name, k, s, n)){
651 w = w->body.file->curtext->w;
652 if(w->col != nil) /* protect against race deleting w */
661 lookid(int id, int dump)
667 for(j=0; j<row.ncol; j++){
669 for(i=0; i<c->nw; i++){
671 if(dump && w->dumpid == id)
673 if(!dump && w->id == id)
682 openfile(Text *t, Expand *e)
695 w = lookfile(e->name, e->nname);
698 if(!t->col->safe && t->fr.maxlines==0) /* window is obscured by full-column window */
699 colgrow(t->col, t->col->w[0], 1);
704 w = makenewwindow(t);
706 winsetname(w, e->name, e->nname);
707 textload(t, 0, e->bname, 1);
708 t->file->mod = FALSE;
711 textsetselect(&t->w->tag, t->w->tag.file->b.nc, t->w->tag.file->b.nc);
713 for(i=ow->nincl; --i>=0; ){
714 n = runestrlen(ow->incl[i]);
716 runemove(rp, ow->incl[i], n);
717 winaddincl(w, rp, n);
719 w->autoindent = ow->autoindent;
721 w->autoindent = globalautoindent;
727 r = address(TRUE, t, range(-1,-1), range(t->q0, t->q1), e->u.at, e->a0, e->a1, e->agetc, &eval, &dummy);
729 e->jump = FALSE; /* don't jump if invalid address */
735 textshow(t, r.q0, r.q1, 1);
739 moveto(mousectl, addpt(frptofchar(&t->fr, t->fr.p0), Pt(4, font->height-4)));
744 new(Text *et, Text *t, Text *argt, int flag1, int flag2, Rune *arg, int narg)
752 getarg(argt, FALSE, TRUE, &a, &na);
754 new(et, t, nil, flag1, flag2, a, na);
758 /* loop condition: *arg is not a blank */
759 for(ndone=0; ; ndone++){
760 a = findbl(arg, narg, &na);
762 if(ndone==0 && et->col!=nil)
763 winsettag(coladd(et->col, nil, nil, -1));
768 runemove(f, arg, nf);
769 rs = dirname(et, f, nf);
772 memset(&e, 0, sizeof e);
775 e.bname = runetobyte(f, nf);
780 arg = skipbl(a, na, &narg);