19 Window* openfile(Text*, Expand*, int);
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)
198 openfile(t, &e, 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)
228 if(n<0 || n>runestrlen(r))
234 plumblook(Plumbmsg *m)
240 if(m->ndata >= BUFSIZE){
241 warning(nil, "insanely long file name (%d bytes) in plumb message (%.32s...)\n", m->ndata, m->data);
246 if(m->data[0] == '\0')
250 e.name = bytetorune(e.bname, &e.nname);
254 addr = plumblookup(m->attr, "addr");
256 e.u.ar = bytetorune(addr, &e.a1);
259 newwindow = plumblookup(m->attr, "newwindow") != nil;
260 openfile(nil, &e, newwindow);
267 plumbshow(Plumbmsg *m)
273 char *name, *p, namebuf[16];
275 w = makenewwindow(nil);
276 name = plumblookup(m->attr, "filename");
280 snprint(namebuf, sizeof namebuf, "Untitled-%d", nuntitled);
283 if(name[0]!='/' && m->wdir!=nil && m->wdir[0]!='\0'){
284 nb = strlen(m->wdir) + 1 + strlen(name) + 1;
286 snprint(p, nb, "%s/%s", m->wdir, name);
289 cvttorunes(name, strlen(name), rb, &nb, &nr, nil);
291 rs = cleanrname(runestr(rb, nr));
292 winsetname(w, rs.r, rs.nr);
293 r = runemalloc(m->ndata);
294 cvttorunes(m->data, m->ndata, r, &nb, &nr, nil);
295 textinsert(&w->body, 0, r, nr, TRUE);
297 w->body.file->mod = FALSE;
300 textscrdraw(&w->body);
301 textsetselect(&w->tag, w->tag.file->b.nc, w->tag.file->b.nc);
306 search(Text *ct, Rune *r, uint n)
312 if(n==0 || n>ct->file->b.nc)
315 warning(nil, "string too long\n");
318 maxn = max(2*n, RBUFSIZE);
326 if(q >= ct->file->b.nc){
333 c = runestrchr(b, r[0]);
338 if(around && q>=ct->q1)
346 /* reload if buffer covers neither string nor rest of file */
347 if(nb<n && nb!=ct->file->b.nc-q){
348 nb = ct->file->b.nc-q;
351 bufread(&ct->file->b, q, s, nb);
355 /* this runeeq is fishy but the null at b[nb] makes it safe */
356 if(runeeq(b, n, r, n)==TRUE){
358 textshow(ct, q, q+n, 1);
371 if(around && q>=ct->q1)
381 static Rune Lx[] = { '.', '-', '+', '/', ':', 0 };
384 if(runestrchr(Lx, r))
389 /* Runestr wrapper for cleanname */
391 cleanrname(Runestr rs)
396 s = runetobyte(rs.r, rs.nr);
398 cvttorunes(s, strlen(s), rs.r, &nb, &rs.nr, &nulls);
404 includefile(Rune *dir, Rune *file, int nfile)
409 static Rune Lslash[] = { '/', 0 };
412 a = emalloc((m+1+nfile)*UTFmax+1);
413 sprint(a, "%S/%.*S", dir, nfile, file);
417 return runestr(nil, 0);
418 r = runemalloc(m+1+nfile);
420 runemove(r+m, Lslash, 1);
421 runemove(r+m+1, file, nfile);
423 return cleanrname(runestr(r, m+1+nfile));
429 includename(Text *t, Rune *r, int n)
433 Rune Lsysinclude[] = { '/', 's', 'y', 's', '/', 'i', 'n', 'c', 'l', 'u', 'd', 'e', 0 };
434 Rune Lusrinclude[] = { '/', 'u', 's', 'r', '/', 'i', 'n', 'c', 'l', 'u', 'd', 'e', 0 };
435 Rune Lusrlocalinclude[] = { '/', 'u', 's', 'r', '/', 'l', 'o', 'c', 'a', 'l',
436 '/', 'i', 'n', 'c', 'l', 'u', 'd', 'e', 0 };
437 Rune Lusrlocalplan9include[] = { '/', 'u', 's', 'r', '/', 'l', 'o', 'c', 'a', 'l',
438 '/', 'p', 'l', 'a', 'n', '9', '/', 'i', 'n', 'c', 'l', 'u', 'd', 'e', 0 };
442 if(objdir==nil && objtype!=nil){
443 sprint(buf, "/%s/include", objtype);
444 objdir = bytetorune(buf, &i);
445 objdir = runerealloc(objdir, i+1);
450 if(n==0 || r[0]=='/' || w==nil)
452 if(n>2 && r[0]=='.' && r[1]=='/')
456 for(i=0; i<w->nincl && file.r==nil; i++)
457 file = includefile(w->incl[i], r, n);
460 file = includefile(Lsysinclude, r, n);
462 file = includefile(Lusrlocalplan9include, r, n);
464 file = includefile(Lusrlocalinclude, r, n);
466 file = includefile(Lusrinclude, r, n);
467 if(file.r==nil && objdir!=nil)
468 file = includefile(objdir, r, n);
474 return runestr(r, n);
478 dirname(Text *t, Rune *r, int n)
486 if(t==nil || t->w==nil)
488 if(dodollarsigns && n>=1 && r[0]=='$')
489 expandenv(&r, (uint*)&n);
490 nt = t->w->tag.file->b.nc;
493 if(n>=1 && r[0]=='/')
495 b = runemalloc(nt+n+1);
496 bufread(&t->w->tag.file->b, 0, b, nt);
502 if(c==' ' || c=='\t')
508 if(dodollarsigns && expandenv(&b, (uint*)&slash))
509 b = runerealloc(b, slash+n);
510 runemove(b+slash, r, n);
512 return cleanrname(runestr(b, slash+n));
518 return cleanrname(tmp);
523 expandfile(Text *t, uint q0, uint q1, Expand *e)
525 int i, n, nname, colon, eval;
534 while(q1<t->file->b.nc && isfilec(c=textreadc(t, q1))){
541 while(q0>0 && (isfilec(c=textreadc(t, q0-1)) || isaddrc(c) || isregexc(c))){
543 if(colon<0 && c==':')
547 * if it looks like it might begin file: , consume address chars after :
548 * otherwise terminate expansion at :
552 if(colon<t->file->b.nc-1 && isaddrc(textreadc(t, colon+1))){
554 while(q1<t->file->b.nc && isaddrc(textreadc(t, q1)))
559 if(colon >= 0){ /* stop at white space */
560 for(amax=colon+1; amax<t->file->b.nc; amax++)
561 if((c=textreadc(t, amax))==' ' || c=='\t' || c=='\n')
564 amax = t->file->b.nc;
572 /* see if it's a file name */
573 r = runemalloc(n+1); /* +1 for possible $ below */
574 bufread(&t->file->b, q0, r, n);
575 /* first, does it have bad chars? */
579 if(c==':' && nname<0){
580 if(q0+i+1<t->file->b.nc && (i==n-1 || isaddrc(textreadc(t, q0+i+1))))
589 for(i=0; i<nname; i++){
590 if(dodollarsigns && i==0 && r[0]=='$')
596 * See if it's a file name in <>, and turn that into an include
597 * file name if so. Should probably do it for "" too, but that's not
598 * restrictive enough syntax and checking for a #include earlier on the
599 * line would be silly.
601 if(q0>0 && textreadc(t, q0-1)=='<' && q1<t->file->b.nc && textreadc(t, q1)=='>'){
602 rs = includename(t, r, nname);
609 if(dodollarsigns && r[0] == '$')
610 expandenv(&r, (uint*)&nname);
612 rs = dirname(t, r, nname);
617 e->bname = runetobyte(r, nname);
618 /* if it's already a window name, it's a file */
619 w = lookfile(r, nname);
622 /* if it's the name of a file, it's a file */
623 if(access(e->bname, 0) < 0){
635 address(TRUE, nil, range(-1,-1), range(0,0), t, e->a0, amax, tgetc, &eval, (uint*)&e->a1);
644 expand(Text *t, uint q0, uint q1, Expand *e)
646 memset(e, 0, sizeof *e);
648 /* if in selection, choose selection */
650 if(q1==q0 && t->q1>t->q0 && t->q0<=q0 && q0<=t->q1){
657 if(expandfile(t, q0, q1, e))
660 while(q1<t->file->b.nc && isalnum(textreadc(t, q1)))
662 while(q0>0 && isalnum(textreadc(t, q0-1)))
671 lookfile(Rune *s, int n)
678 /* avoid terminal slash on directories */
679 if(n>1 && s[n-1] == '/')
681 for(j=0; j<row.ncol; j++){
683 for(i=0; i<c->nw; i++){
687 if(k>1 && t->file->name[k-1] == '/')
689 if(runeeq(t->file->name, k, s, n)){
690 w = w->body.file->curtext->w;
691 if(w->col != nil) /* protect against race deleting w */
700 lookid(int id, int dump)
706 for(j=0; j<row.ncol; j++){
708 for(i=0; i<c->nw; i++){
710 if(dump && w->dumpid == id)
712 if(!dump && w->id == id)
721 openfile(Text *t, Expand *e, int newwindow)
737 w = lookfile(e->name, e->nname);
738 if(w == nil && e->name[0] != '/'){
740 * Unrooted path in new window.
741 * This can happen if we type a pwd-relative path
742 * in the topmost tag or the column tags.
743 * Most of the time plumber takes care of these,
744 * but plumber might not be running or might not
745 * be configured to accept plumbed directories.
746 * Make the name a full path, just like we would if
747 * opening via the plumber.
749 n = utflen(wdir)+1+e->nname+1;
751 runesnprint(rp, n, "%s/%.*S", wdir, e->nname, e->name);
752 rs = cleanrname(runestr(rp, n-1));
756 w = lookfile(e->name, e->nname);
760 if(newwindow==TRUE && !w->isdir)
761 w = coladd(w->col, nil, w, -1);
763 if(!t->col->safe && t->fr.maxlines==0) /* window is obscured by full-column window */
764 colgrow(t->col, t->col->w[0], 1);
769 w = makenewwindow(t);
771 winsetname(w, e->name, e->nname);
772 textload(t, 0, e->bname, 1);
773 t->file->mod = FALSE;
776 textsetselect(&t->w->tag, t->w->tag.file->b.nc, t->w->tag.file->b.nc);
778 for(i=ow->nincl; --i>=0; ){
779 n = runestrlen(ow->incl[i]);
781 runemove(rp, ow->incl[i], n);
782 winaddincl(w, rp, n);
784 w->autoindent = ow->autoindent;
786 w->autoindent = globalautoindent;
792 r = address(TRUE, t, range(-1,-1), range(t->q0, t->q1), e->u.at, e->a0, e->a1, e->agetc, &eval, &dummy);
794 e->jump = FALSE; /* don't jump if invalid address */
800 textshow(t, r.q0, r.q1, 1);
804 moveto(mousectl, addpt(frptofchar(&t->fr, t->fr.p0), Pt(4, font->height-4)));
809 new(Text *et, Text *t, Text *argt, int flag1, int flag2, Rune *arg, int narg)
817 getarg(argt, FALSE, TRUE, &a, &na);
819 new(et, t, nil, flag1, flag2, a, na);
823 /* loop condition: *arg is not a blank */
824 for(ndone=0; ; ndone++){
825 a = findbl(arg, narg, &na);
827 if(ndone==0 && et->col!=nil)
828 winsettag(coladd(et->col, nil, nil, -1));
833 runemove(f, arg, nf);
834 rs = dirname(et, f, nf);
835 memset(&e, 0, sizeof e);
838 e.bname = runetobyte(rs.r, rs.nr);
840 openfile(et, &e, FALSE);
843 arg = skipbl(a, na, &narg);