19 Window* openfile(Text*, Expand*);
31 threadsetname("plumbproc");
34 * Loop so that if plumber is restarted, acme need not be.
41 while((fid = plumbopenfid("edit", OREAD|OCEXEC)) == nil){
47 plumbsendfid = plumbopenfid("send", OWRITE|OCEXEC);
53 m = plumbrecvfid(plumbeditfid);
75 cplumb = chancreate(sizeof(Plumbmsg*), 0);
76 chansetname(cplumb, "cplumb");
77 threadcreate(plumbthread, nil, STACK);
82 look3(Text *t, uint q0, uint q1, int external)
84 int n, c, f, expanded;
96 expanded = expand(t, q0, q1, &e);
97 if(!external && t->w!=nil && t->w->nopen[QWevent]>0){
98 /* send alphanumeric expansion to external client */
102 if((e.u.at!=nil && t->w!=nil) || (e.nname>0 && lookfile(e.name, e.nname)!=nil))
103 f = 1; /* acme can do it without loading a file */
104 if(q0!=e.q0 || q1!=e.q1)
105 f |= 2; /* second (post-expand) message follows */
107 f |= 4; /* it's a file name */
114 bufread(&t->file->b, q0, r, n);
115 winevent(t->w, "%c%d %d %d %d %.*S\n", c, q0, q1, f, n, n, r);
118 winevent(t->w, "%c%d %d %d 0 \n", c, q0, q1, f, n);
119 if(q0==e.q0 && q1==e.q1)
126 runemove(r, e.name, e.nname);
129 bufread(&e.u.at->file->b, e.a0, r+e.nname+1, e.a1-e.a0);
134 bufread(&t->file->b, e.q0, r, n);
138 winevent(t->w, "%c%d %d %d %d %.*S\n", c, e.q0, e.q1, f, n, n, r);
140 winevent(t->w, "%c%d %d %d 0 \n", c, e.q0, e.q1, f, n);
144 if(plumbsendfid != nil){
145 /* send whitespace-delimited word to plumber */
146 m = emalloc(sizeof(Plumbmsg));
147 m->src = estrdup("acme");
149 dir = dirname(t, nil, 0);
150 if(dir.nr==1 && dir.r[0]=='.'){ /* sigh */
156 m->wdir = estrdup(wdir);
158 m->wdir = runetobyte(dir.r, dir.nr);
160 m->type = estrdup("text");
164 if(t->q1>t->q0 && t->q0<=q0 && q0<=t->q1){
169 while(q0>0 && (c=tgetc(t, q0-1))!=' ' && c!='\t' && c!='\n')
171 while(q1<t->file->b.nc && (c=tgetc(t, q1))!=' ' && c!='\t' && c!='\n')
177 sprint(buf, "click=%d", p-q0);
178 m->attr = plumbunpackattr(buf);
181 r = runemalloc(q1-q0);
182 bufread(&t->file->b, q0, r, q1-q0);
183 m->data = runetobyte(r, q1-q0);
184 m->ndata = strlen(m->data);
186 if(m->ndata<messagesize-1024 && plumbsendtofid(plumbsendfid, m) >= 0){
191 /* plumber failed to match; fall through */
194 /* interpret alphanumeric string ourselves */
195 if(expanded == FALSE)
206 textsetselect(ct, e.q1, e.q1);
209 bufread(&t->file->b, e.q0, r, n);
210 if(search(ct, r, n) && e.jump)
211 moveto(mousectl, addpt(frptofchar(&ct->fr, ct->fr.p0), Pt(4, ct->fr.font->height-4)));
223 plumbgetc(void *a, uint n)
234 plumblook(Plumbmsg *m)
239 if(m->ndata >= BUFSIZE){
240 warning(nil, "insanely long file name (%d bytes) in plumb message (%.32s...)\n", m->ndata, m->data);
245 if(m->data[0] == '\0')
249 e.name = bytetorune(e.bname, &e.nname);
253 addr = plumblookup(m->attr, "addr");
255 e.u.ar = bytetorune(addr, &e.a1);
265 plumbshow(Plumbmsg *m)
271 char *name, *p, namebuf[16];
274 w = makenewwindow(nil);
275 name = plumblookup(m->attr, "filename");
279 snprint(namebuf, sizeof namebuf, "Untitled-%d", nuntitled);
282 if(name[0]!='/' && m->wdir!=nil && m->wdir[0]!='\0'){
283 nb = strlen(m->wdir) + 1 + strlen(name) + 1;
285 snprint(p, nb, "%s/%s", m->wdir, name);
288 cvttorunes(name, strlen(name), rb, &nb, &nr, nil);
290 rs = cleanrname(runestr(rb, nr));
291 winsetname(w, rs.r, rs.nr);
292 r = runemalloc(m->ndata);
293 cvttorunes(m->data, m->ndata, r, &nb, &nr, nil);
294 textinsert(&w->body, 0, r, nr, TRUE);
296 w->body.file->mod = FALSE;
299 textscrdraw(&w->body);
300 textsetselect(&w->tag, w->tag.file->b.nc, w->tag.file->b.nc);
304 search(Text *ct, Rune *r, uint n)
310 if(n==0 || n>ct->file->b.nc)
313 warning(nil, "string too long\n");
316 maxn = max(2*n, RBUFSIZE);
324 if(q >= ct->file->b.nc){
331 c = runestrchr(b, r[0]);
336 if(around && q>=ct->q1)
344 /* reload if buffer covers neither string nor rest of file */
345 if(nb<n && nb!=ct->file->b.nc-q){
346 nb = ct->file->b.nc-q;
349 bufread(&ct->file->b, q, s, nb);
353 /* this runeeq is fishy but the null at b[nb] makes it safe */
354 if(runeeq(b, n, r, n)==TRUE){
356 textshow(ct, q, q+n, 1);
369 if(around && q>=ct->q1)
379 static Rune Lx[] = { '.', '-', '+', '/', ':', 0 };
382 if(runestrchr(Lx, r))
387 /* Runestr wrapper for cleanname */
389 cleanrname(Runestr rs)
394 s = runetobyte(rs.r, rs.nr);
396 cvttorunes(s, strlen(s), rs.r, &nb, &rs.nr, &nulls);
402 includefile(Rune *dir, Rune *file, int nfile)
407 static Rune Lslash[] = { '/', 0 };
410 a = emalloc((m+1+nfile)*UTFmax+1);
411 sprint(a, "%S/%.*S", dir, nfile, file);
415 return runestr(nil, 0);
416 r = runemalloc(m+1+nfile);
418 runemove(r+m, Lslash, 1);
419 runemove(r+m+1, file, nfile);
421 return cleanrname(runestr(r, m+1+nfile));
427 includename(Text *t, Rune *r, int n)
431 Rune Lsysinclude[] = { '/', 's', 'y', 's', '/', 'i', 'n', 'c', 'l', 'u', 'd', 'e', 0 };
432 Rune Lusrinclude[] = { '/', 'u', 's', 'r', '/', 'i', 'n', 'c', 'l', 'u', 'd', 'e', 0 };
433 Rune Lusrlocalinclude[] = { '/', 'u', 's', 'r', '/', 'l', 'o', 'c', 'a', 'l',
434 '/', 'i', 'n', 'c', 'l', 'u', 'd', 'e', 0 };
435 Rune Lusrlocalplan9include[] = { '/', 'u', 's', 'r', '/', 'l', 'o', 'c', 'a', 'l',
436 '/', 'p', 'l', 'a', 'n', '9', '/', 'i', 'n', 'c', 'l', 'u', 'd', 'e', 0 };
440 if(objdir==nil && objtype!=nil){
441 sprint(buf, "/%s/include", objtype);
442 objdir = bytetorune(buf, &i);
443 objdir = runerealloc(objdir, i+1);
448 if(n==0 || r[0]=='/' || w==nil)
450 if(n>2 && r[0]=='.' && r[1]=='/')
454 for(i=0; i<w->nincl && file.r==nil; i++)
455 file = includefile(w->incl[i], r, n);
458 file = includefile(Lsysinclude, r, n);
460 file = includefile(Lusrlocalplan9include, r, n);
462 file = includefile(Lusrlocalinclude, r, n);
464 file = includefile(Lusrinclude, r, n);
465 if(file.r==nil && objdir!=nil)
466 file = includefile(objdir, r, n);
472 return runestr(r, n);
476 dirname(Text *t, Rune *r, int n)
484 if(t==nil || t->w==nil)
486 nt = t->w->tag.file->b.nc;
489 if(n>=1 && r[0]=='/')
491 b = runemalloc(nt+n+1);
492 bufread(&t->w->tag.file->b, 0, b, nt);
498 if(c==' ' || c=='\t')
503 runemove(b+slash+1, r, n);
505 return cleanrname(runestr(b, slash+1+n));
511 return cleanrname(tmp);
516 expandfile(Text *t, uint q0, uint q1, Expand *e)
518 int i, n, nname, colon, eval;
527 while(q1<t->file->b.nc && isfilec(c=textreadc(t, q1))){
534 while(q0>0 && (isfilec(c=textreadc(t, q0-1)) || isaddrc(c) || isregexc(c))){
536 if(colon<0 && c==':')
540 * if it looks like it might begin file: , consume address chars after :
541 * otherwise terminate expansion at :
545 if(colon<t->file->b.nc-1 && isaddrc(textreadc(t, colon+1))){
547 while(q1<t->file->b.nc && isaddrc(textreadc(t, q1)))
552 if(colon >= 0){ /* stop at white space */
553 for(amax=colon+1; amax<t->file->b.nc; amax++)
554 if((c=textreadc(t, amax))==' ' || c=='\t' || c=='\n')
557 amax = t->file->b.nc;
565 /* see if it's a file name */
567 bufread(&t->file->b, q0, r, n);
568 /* first, does it have bad chars? */
572 if(c==':' && nname<0){
573 if(q0+i+1<t->file->b.nc && (i==n-1 || isaddrc(textreadc(t, q0+i+1))))
582 for(i=0; i<nname; i++)
586 * See if it's a file name in <>, and turn that into an include
587 * file name if so. Should probably do it for "" too, but that's not
588 * restrictive enough syntax and checking for a #include earlier on the
589 * line would be silly.
591 if(q0>0 && textreadc(t, q0-1)=='<' && q1<t->file->b.nc && textreadc(t, q1)=='>'){
592 rs = includename(t, r, nname);
599 rs = dirname(t, r, nname);
603 e->bname = runetobyte(r, nname);
604 /* if it's already a window name, it's a file */
605 w = lookfile(r, nname);
608 /* if it's the name of a file, it's a file */
609 if(ismtpt(e->bname) || access(e->bname, 0) < 0){
621 address(TRUE, nil, range(-1,-1), range(0,0), t, e->a0, amax, tgetc, &eval, (uint*)&e->a1);
630 expand(Text *t, uint q0, uint q1, Expand *e)
632 memset(e, 0, sizeof *e);
634 /* if in selection, choose selection */
636 if(q1==q0 && t->q1>t->q0 && t->q0<=q0 && q0<=t->q1){
643 if(expandfile(t, q0, q1, e))
647 while(q1<t->file->b.nc && isalnum(textreadc(t, q1)))
649 while(q0>0 && isalnum(textreadc(t, q0-1)))
658 lookfile(Rune *s, int n)
665 /* avoid terminal slash on directories */
666 if(n>1 && s[n-1] == '/')
668 for(j=0; j<row.ncol; j++){
670 for(i=0; i<c->nw; i++){
674 if(k>1 && t->file->name[k-1] == '/')
676 if(runeeq(t->file->name, k, s, n)){
677 w = w->body.file->curtext->w;
678 if(w->col != nil) /* protect against race deleting w */
687 lookid(int id, int dump)
693 for(j=0; j<row.ncol; j++){
695 for(i=0; i<c->nw; i++){
697 if(dump && w->dumpid == id)
699 if(!dump && w->id == id)
708 openfile(Text *t, Expand *e)
724 w = lookfile(e->name, e->nname);
725 if(w == nil && e->name[0] != '/'){
727 * Unrooted path in new window.
728 * This can happen if we type a pwd-relative path
729 * in the topmost tag or the column tags.
730 * Most of the time plumber takes care of these,
731 * but plumber might not be running or might not
732 * be configured to accept plumbed directories.
733 * Make the name a full path, just like we would if
734 * opening via the plumber.
736 n = utflen(wdir)+1+e->nname+1;
738 runesnprint(rp, n, "%s/%.*S", wdir, e->nname, e->name);
739 rs = cleanrname(runestr(rp, n-1));
743 w = lookfile(e->name, e->nname);
748 if(!t->col->safe && t->fr.maxlines==0) /* window is obscured by full-column window */
749 colgrow(t->col, t->col->w[0], 1);
754 w = makenewwindow(t);
756 winsetname(w, e->name, e->nname);
757 if(textload(t, 0, e->bname, 1) >= 0)
758 t->file->unread = FALSE;
759 t->file->mod = FALSE;
762 textsetselect(&t->w->tag, t->w->tag.file->b.nc, t->w->tag.file->b.nc);
764 for(i=ow->nincl; --i>=0; ){
765 n = runestrlen(ow->incl[i]);
767 runemove(rp, ow->incl[i], n);
768 winaddincl(w, rp, n);
770 w->autoindent = ow->autoindent;
772 w->autoindent = globalautoindent;
778 r = address(TRUE, t, range(-1,-1), range(t->q0, t->q1), e->u.at, e->a0, e->a1, e->agetc, &eval, &dummy);
781 warning(nil, "addresses out of order\n");
784 e->jump = FALSE; /* don't jump if invalid address */
790 textshow(t, r.q0, r.q1, 1);
794 moveto(mousectl, addpt(frptofchar(&t->fr, t->fr.p0), Pt(4, font->height-4)));
799 new(Text *et, Text *t, Text *argt, int flag1, int flag2, Rune *arg, int narg)
807 getarg(argt, FALSE, TRUE, &a, &na);
809 new(et, t, nil, flag1, flag2, a, na);
813 /* loop condition: *arg is not a blank */
814 for(ndone=0; ; ndone++){
815 a = findbl(arg, narg, &na);
817 if(ndone==0 && et->col!=nil)
818 winsettag(coladd(et->col, nil, nil, -1));
823 runemove(f, arg, nf);
824 rs = dirname(et, f, nf);
825 memset(&e, 0, sizeof e);
828 e.bname = runetobyte(rs.r, rs.nr);
833 arg = skipbl(a, na, &narg);