Blob


1 #include <u.h>
2 #include <libc.h>
3 #include <draw.h>
4 #include <thread.h>
5 #include <cursor.h>
6 #include <mouse.h>
7 #include <keyboard.h>
8 #include <frame.h>
9 #include <fcall.h>
10 #include <regexp.h>
11 #include <9pclient.h>
12 #include <plumb.h>
13 #include "dat.h"
14 #include "fns.h"
16 CFid *plumbsendfid;
17 CFid *plumbeditfid;
19 Window* openfile(Text*, Expand*);
21 int nuntitled;
23 void
24 plumbproc(void *v)
25 {
26 Plumbmsg *m;
28 USED(v);
29 threadsetname("plumbproc");
30 for(;;){
31 m = plumbrecvfid(plumbeditfid);
32 if(m == nil)
33 threadexits(nil);
34 sendp(cplumb, m);
35 }
36 }
38 void
39 startplumbing(void)
40 {
41 plumbeditfid = plumbopenfid("edit", OREAD|OCEXEC);
42 if(plumbeditfid == nil)
43 fprint(2, "acme: can't initialize plumber: %r\n");
44 else{
45 cplumb = chancreate(sizeof(Plumbmsg*), 0);
46 chansetname(cplumb, "cplumb");
47 threadcreate(plumbproc, nil, STACK);
48 }
49 plumbsendfid = plumbopenfid("send", OWRITE|OCEXEC);
50 }
53 void
54 look3(Text *t, uint q0, uint q1, int external)
55 {
56 int n, c, f, expanded;
57 Text *ct;
58 Expand e;
59 Rune *r;
60 uint p;
61 Plumbmsg *m;
62 Runestr dir;
63 char buf[32];
65 ct = seltext;
66 if(ct == nil)
67 seltext = t;
68 expanded = expand(t, q0, q1, &e);
69 if(!external && t->w!=nil && t->w->nopen[QWevent]>0){
70 /* send alphanumeric expansion to external client */
71 if(expanded == FALSE)
72 return;
73 f = 0;
74 if((e.u.at!=nil && t->w!=nil) || (e.nname>0 && lookfile(e.name, e.nname)!=nil))
75 f = 1; /* acme can do it without loading a file */
76 if(q0!=e.q0 || q1!=e.q1)
77 f |= 2; /* second (post-expand) message follows */
78 if(e.nname)
79 f |= 4; /* it's a file name */
80 c = 'l';
81 if(t->what == Body)
82 c = 'L';
83 n = q1-q0;
84 if(n <= EVENTSIZE){
85 r = runemalloc(n);
86 bufread(&t->file->b, q0, r, n);
87 winevent(t->w, "%c%d %d %d %d %.*S\n", c, q0, q1, f, n, n, r);
88 free(r);
89 }else
90 winevent(t->w, "%c%d %d %d 0 \n", c, q0, q1, f, n);
91 if(q0==e.q0 && q1==e.q1)
92 return;
93 if(e.nname){
94 n = e.nname;
95 if(e.a1 > e.a0)
96 n += 1+(e.a1-e.a0);
97 r = runemalloc(n);
98 runemove(r, e.name, e.nname);
99 if(e.a1 > e.a0){
100 r[e.nname] = ':';
101 bufread(&e.u.at->file->b, e.a0, r+e.nname+1, e.a1-e.a0);
103 }else{
104 n = e.q1 - e.q0;
105 r = runemalloc(n);
106 bufread(&t->file->b, e.q0, r, n);
108 f &= ~2;
109 if(n <= EVENTSIZE)
110 winevent(t->w, "%c%d %d %d %d %.*S\n", c, e.q0, e.q1, f, n, n, r);
111 else
112 winevent(t->w, "%c%d %d %d 0 \n", c, e.q0, e.q1, f, n);
113 free(r);
114 goto Return;
116 if(plumbsendfid != nil){
117 /* send whitespace-delimited word to plumber */
118 m = emalloc(sizeof(Plumbmsg));
119 m->src = estrdup("acme");
120 m->dst = nil;
121 dir = dirname(t, nil, 0);
122 if(dir.nr==1 && dir.r[0]=='.'){ /* sigh */
123 free(dir.r);
124 dir.r = nil;
125 dir.nr = 0;
127 if(dir.nr == 0)
128 m->wdir = estrdup(wdir);
129 else
130 m->wdir = runetobyte(dir.r, dir.nr);
131 free(dir.r);
132 m->type = estrdup("text");
133 m->attr = nil;
134 buf[0] = '\0';
135 if(q1 == q0){
136 if(t->q1>t->q0 && t->q0<=q0 && q0<=t->q1){
137 q0 = t->q0;
138 q1 = t->q1;
139 }else{
140 p = q0;
141 while(q0>0 && (c=tgetc(t, q0-1))!=' ' && c!='\t' && c!='\n')
142 q0--;
143 while(q1<t->file->b.nc && (c=tgetc(t, q1))!=' ' && c!='\t' && c!='\n')
144 q1++;
145 if(q1 == q0){
146 plumbfree(m);
147 goto Return;
149 sprint(buf, "click=%d", p-q0);
150 m->attr = plumbunpackattr(buf);
153 r = runemalloc(q1-q0);
154 bufread(&t->file->b, q0, r, q1-q0);
155 m->data = runetobyte(r, q1-q0);
156 m->ndata = strlen(m->data);
157 free(r);
158 if(m->ndata<messagesize-1024 && plumbsendtofid(plumbsendfid, m) >= 0){
159 plumbfree(m);
160 goto Return;
162 plumbfree(m);
163 /* plumber failed to match; fall through */
166 /* interpret alphanumeric string ourselves */
167 if(expanded == FALSE)
168 return;
169 if(e.name || e.u.at)
170 openfile(t, &e);
171 else{
172 if(t->w == nil)
173 return;
174 ct = &t->w->body;
175 if(t->w != ct->w)
176 winlock(ct->w, 'M');
177 if(t == ct)
178 textsetselect(ct, e.q1, e.q1);
179 n = e.q1 - e.q0;
180 r = runemalloc(n);
181 bufread(&t->file->b, e.q0, r, n);
182 if(search(ct, r, n) && e.jump)
183 moveto(mousectl, addpt(frptofchar(&ct->fr, ct->fr.p0), Pt(4, ct->fr.font->height-4)));
184 if(t->w != ct->w)
185 winunlock(ct->w);
186 free(r);
189 Return:
190 free(e.name);
191 free(e.bname);
194 int
195 plumbgetc(void *a, uint n)
197 Rune *r;
199 r = a;
200 if(n<0 || n>runestrlen(r))
201 return 0;
202 return r[n];
205 void
206 plumblook(Plumbmsg *m)
208 Expand e;
209 char *addr;
211 if(m->ndata >= BUFSIZE){
212 warning(nil, "insanely long file name (%d bytes) in plumb message (%.32s...)\n", m->ndata, m->data);
213 return;
215 e.q0 = 0;
216 e.q1 = 0;
217 if(m->data[0] == '\0')
218 return;
219 e.u.ar = nil;
220 e.bname = m->data;
221 e.name = bytetorune(e.bname, &e.nname);
222 e.jump = TRUE;
223 e.a0 = 0;
224 e.a1 = 0;
225 addr = plumblookup(m->attr, "addr");
226 if(addr != nil){
227 e.u.ar = bytetorune(addr, &e.a1);
228 e.agetc = plumbgetc;
230 openfile(nil, &e);
231 free(e.name);
232 free(e.u.at);
233 drawtopwindow();
236 void
237 plumbshow(Plumbmsg *m)
239 Window *w;
240 Rune rb[256], *r;
241 int nb, nr;
242 Runestr rs;
243 char *name, *p, namebuf[16];
245 w = makenewwindow(nil);
246 name = plumblookup(m->attr, "filename");
247 if(name == nil){
248 name = namebuf;
249 nuntitled++;
250 snprint(namebuf, sizeof namebuf, "Untitled-%d", nuntitled);
252 p = nil;
253 if(name[0]!='/' && m->wdir!=nil && m->wdir[0]!='\0'){
254 nb = strlen(m->wdir) + 1 + strlen(name) + 1;
255 p = emalloc(nb);
256 snprint(p, nb, "%s/%s", m->wdir, name);
257 name = p;
259 cvttorunes(name, strlen(name), rb, &nb, &nr, nil);
260 free(p);
261 rs = cleanrname(runestr(rb, nr));
262 winsetname(w, rs.r, rs.nr);
263 r = runemalloc(m->ndata);
264 cvttorunes(m->data, m->ndata, r, &nb, &nr, nil);
265 textinsert(&w->body, 0, r, nr, TRUE);
266 free(r);
267 w->body.file->mod = FALSE;
268 w->dirty = FALSE;
269 winsettag(w);
270 textscrdraw(&w->body);
271 textsetselect(&w->tag, w->tag.file->b.nc, w->tag.file->b.nc);
272 drawtopwindow();
275 int
276 search(Text *ct, Rune *r, uint n)
278 uint q, nb, maxn;
279 int around;
280 Rune *s, *b, *c;
282 if(n==0 || n>ct->file->b.nc)
283 return FALSE;
284 if(2*n > RBUFSIZE){
285 warning(nil, "string too long\n");
286 return FALSE;
288 maxn = max(2*n, RBUFSIZE);
289 s = fbufalloc();
290 b = s;
291 nb = 0;
292 b[nb] = 0;
293 around = 0;
294 q = ct->q1;
295 for(;;){
296 if(q >= ct->file->b.nc){
297 q = 0;
298 around = 1;
299 nb = 0;
300 b[nb] = 0;
302 if(nb > 0){
303 c = runestrchr(b, r[0]);
304 if(c == nil){
305 q += nb;
306 nb = 0;
307 b[nb] = 0;
308 if(around && q>=ct->q1)
309 break;
310 continue;
312 q += (c-b);
313 nb -= (c-b);
314 b = c;
316 /* reload if buffer covers neither string nor rest of file */
317 if(nb<n && nb!=ct->file->b.nc-q){
318 nb = ct->file->b.nc-q;
319 if(nb >= maxn)
320 nb = maxn-1;
321 bufread(&ct->file->b, q, s, nb);
322 b = s;
323 b[nb] = '\0';
325 /* this runeeq is fishy but the null at b[nb] makes it safe */
326 if(runeeq(b, n, r, n)==TRUE){
327 if(ct->w){
328 textshow(ct, q, q+n, 1);
329 winsettag(ct->w);
330 }else{
331 ct->q0 = q;
332 ct->q1 = q+n;
334 seltext = ct;
335 fbuffree(s);
336 return TRUE;
338 if(around && q>=ct->q1)
339 break;
340 --nb;
341 b++;
342 q++;
344 fbuffree(s);
345 return FALSE;
348 int
349 isfilec(Rune r)
351 static Rune Lx[] = { '.', '-', '+', '/', ':', 0 };
352 if(isalnum(r))
353 return TRUE;
354 if(runestrchr(Lx, r))
355 return TRUE;
356 return FALSE;
359 /* Runestr wrapper for cleanname */
360 Runestr
361 cleanrname(Runestr rs)
363 char *s;
364 int nb, nulls;
366 s = runetobyte(rs.r, rs.nr);
367 cleanname(s);
368 cvttorunes(s, strlen(s), rs.r, &nb, &rs.nr, &nulls);
369 free(s);
370 return rs;
373 Runestr
374 includefile(Rune *dir, Rune *file, int nfile)
376 int m, n;
377 char *a;
378 Rune *r;
379 static Rune Lslash[] = { '/', 0 };
381 m = runestrlen(dir);
382 a = emalloc((m+1+nfile)*UTFmax+1);
383 sprint(a, "%S/%.*S", dir, nfile, file);
384 n = access(a, 0);
385 free(a);
386 if(n < 0)
387 return runestr(nil, 0);
388 r = runemalloc(m+1+nfile);
389 runemove(r, dir, m);
390 runemove(r+m, Lslash, 1);
391 runemove(r+m+1, file, nfile);
392 free(file);
393 return cleanrname(runestr(r, m+1+nfile));
396 static Rune *objdir;
398 Runestr
399 includename(Text *t, Rune *r, int n)
401 Window *w;
402 char buf[128];
403 Rune Lsysinclude[] = { '/', 's', 'y', 's', '/', 'i', 'n', 'c', 'l', 'u', 'd', 'e', 0 };
404 Rune Lusrinclude[] = { '/', 'u', 's', 'r', '/', 'i', 'n', 'c', 'l', 'u', 'd', 'e', 0 };
405 Rune Lusrlocalinclude[] = { '/', 'u', 's', 'r', '/', 'l', 'o', 'c', 'a', 'l',
406 '/', 'i', 'n', 'c', 'l', 'u', 'd', 'e', 0 };
407 Rune Lusrlocalplan9include[] = { '/', 'u', 's', 'r', '/', 'l', 'o', 'c', 'a', 'l',
408 '/', 'p', 'l', 'a', 'n', '9', '/', 'i', 'n', 'c', 'l', 'u', 'd', 'e', 0 };
409 Runestr file;
410 int i;
412 if(objdir==nil && objtype!=nil){
413 sprint(buf, "/%s/include", objtype);
414 objdir = bytetorune(buf, &i);
415 objdir = runerealloc(objdir, i+1);
416 objdir[i] = '\0';
419 w = t->w;
420 if(n==0 || r[0]=='/' || w==nil)
421 goto Rescue;
422 if(n>2 && r[0]=='.' && r[1]=='/')
423 goto Rescue;
424 file.r = nil;
425 file.nr = 0;
426 for(i=0; i<w->nincl && file.r==nil; i++)
427 file = includefile(w->incl[i], r, n);
429 if(file.r == nil)
430 file = includefile(Lsysinclude, r, n);
431 if(file.r == nil)
432 file = includefile(Lusrlocalplan9include, r, n);
433 if(file.r == nil)
434 file = includefile(Lusrlocalinclude, r, n);
435 if(file.r == nil)
436 file = includefile(Lusrinclude, r, n);
437 if(file.r==nil && objdir!=nil)
438 file = includefile(objdir, r, n);
439 if(file.r == nil)
440 goto Rescue;
441 return file;
443 Rescue:
444 return runestr(r, n);
447 Runestr
448 dirname(Text *t, Rune *r, int n)
450 Rune *b, c;
451 uint m, nt;
452 int slash;
453 Runestr tmp;
455 b = nil;
456 if(t==nil || t->w==nil)
457 goto Rescue;
458 nt = t->w->tag.file->b.nc;
459 if(nt == 0)
460 goto Rescue;
461 if(n>=1 && r[0]=='/')
462 goto Rescue;
463 b = runemalloc(nt+n+1);
464 bufread(&t->w->tag.file->b, 0, b, nt);
465 slash = -1;
466 for(m=0; m<nt; m++){
467 c = b[m];
468 if(c == '/')
469 slash = m;
470 if(c==' ' || c=='\t')
471 break;
473 if(slash < 0)
474 goto Rescue;
475 runemove(b+slash+1, r, n);
476 free(r);
477 return cleanrname(runestr(b, slash+1+n));
479 Rescue:
480 free(b);
481 tmp = runestr(r, n);
482 if(r)
483 return cleanrname(tmp);
484 return tmp;
487 int
488 expandfile(Text *t, uint q0, uint q1, Expand *e)
490 int i, n, nname, colon, eval;
491 uint amin, amax;
492 Rune *r, c;
493 Window *w;
494 Runestr rs;
496 amax = q1;
497 if(q1 == q0){
498 colon = -1;
499 while(q1<t->file->b.nc && isfilec(c=textreadc(t, q1))){
500 if(c == ':'){
501 colon = q1;
502 break;
504 q1++;
506 while(q0>0 && (isfilec(c=textreadc(t, q0-1)) || isaddrc(c) || isregexc(c))){
507 q0--;
508 if(colon<0 && c==':')
509 colon = q0;
511 /*
512 * if it looks like it might begin file: , consume address chars after :
513 * otherwise terminate expansion at :
514 */
515 if(colon >= 0){
516 q1 = colon;
517 if(colon<t->file->b.nc-1 && isaddrc(textreadc(t, colon+1))){
518 q1 = colon+1;
519 while(q1<t->file->b.nc-1 && isaddrc(textreadc(t, q1)))
520 q1++;
523 if(q1 > q0)
524 if(colon >= 0){ /* stop at white space */
525 for(amax=colon+1; amax<t->file->b.nc; amax++)
526 if((c=textreadc(t, amax))==' ' || c=='\t' || c=='\n')
527 break;
528 }else
529 amax = t->file->b.nc;
531 amin = amax;
532 e->q0 = q0;
533 e->q1 = q1;
534 n = q1-q0;
535 if(n == 0)
536 return FALSE;
537 /* see if it's a file name */
538 r = runemalloc(n);
539 bufread(&t->file->b, q0, r, n);
540 /* first, does it have bad chars? */
541 nname = -1;
542 for(i=0; i<n; i++){
543 c = r[i];
544 if(c==':' && nname<0){
545 if(q0+i+1<t->file->b.nc && (i==n-1 || isaddrc(textreadc(t, q0+i+1))))
546 amin = q0+i;
547 else
548 goto Isntfile;
549 nname = i;
552 if(nname == -1)
553 nname = n;
554 for(i=0; i<nname; i++)
555 if(!isfilec(r[i]))
556 goto Isntfile;
557 /*
558 * See if it's a file name in <>, and turn that into an include
559 * file name if so. Should probably do it for "" too, but that's not
560 * restrictive enough syntax and checking for a #include earlier on the
561 * line would be silly.
562 */
563 if(q0>0 && textreadc(t, q0-1)=='<' && q1<t->file->b.nc && textreadc(t, q1)=='>'){
564 rs = includename(t, r, nname);
565 r = rs.r;
566 nname = rs.nr;
568 else if(amin == q0)
569 goto Isfile;
570 else{
571 rs = dirname(t, r, nname);
572 r = rs.r;
573 nname = rs.nr;
575 e->bname = runetobyte(r, nname);
576 /* if it's already a window name, it's a file */
577 w = lookfile(r, nname);
578 if(w != nil)
579 goto Isfile;
580 /* if it's the name of a file, it's a file */
581 if(access(e->bname, 0) < 0){
582 free(e->bname);
583 e->bname = nil;
584 goto Isntfile;
587 Isfile:
588 e->name = r;
589 e->nname = nname;
590 e->u.at = t;
591 e->a0 = amin+1;
592 eval = FALSE;
593 address(TRUE, nil, range(-1,-1), range(0,0), t, e->a0, amax, tgetc, &eval, (uint*)&e->a1);
594 return TRUE;
596 Isntfile:
597 free(r);
598 return FALSE;
601 int
602 expand(Text *t, uint q0, uint q1, Expand *e)
604 memset(e, 0, sizeof *e);
605 e->agetc = tgetc;
606 /* if in selection, choose selection */
607 e->jump = TRUE;
608 if(q1==q0 && t->q1>t->q0 && t->q0<=q0 && q0<=t->q1){
609 q0 = t->q0;
610 q1 = t->q1;
611 if(t->what == Tag)
612 e->jump = FALSE;
615 if(expandfile(t, q0, q1, e))
616 return TRUE;
618 if(q0 == q1){
619 while(q1<t->file->b.nc && isalnum(textreadc(t, q1)))
620 q1++;
621 while(q0>0 && isalnum(textreadc(t, q0-1)))
622 q0--;
624 e->q0 = q0;
625 e->q1 = q1;
626 return q1 > q0;
629 Window*
630 lookfile(Rune *s, int n)
632 int i, j, k;
633 Window *w;
634 Column *c;
635 Text *t;
637 /* avoid terminal slash on directories */
638 if(n>1 && s[n-1] == '/')
639 --n;
640 for(j=0; j<row.ncol; j++){
641 c = row.col[j];
642 for(i=0; i<c->nw; i++){
643 w = c->w[i];
644 t = &w->body;
645 k = t->file->nname;
646 if(k>1 && t->file->name[k-1] == '/')
647 k--;
648 if(runeeq(t->file->name, k, s, n)){
649 w = w->body.file->curtext->w;
650 if(w->col != nil) /* protect against race deleting w */
651 return w;
655 return nil;
658 Window*
659 lookid(int id, int dump)
661 int i, j;
662 Window *w;
663 Column *c;
665 for(j=0; j<row.ncol; j++){
666 c = row.col[j];
667 for(i=0; i<c->nw; i++){
668 w = c->w[i];
669 if(dump && w->dumpid == id)
670 return w;
671 if(!dump && w->id == id)
672 return w;
675 return nil;
679 Window*
680 openfile(Text *t, Expand *e)
682 Range r;
683 Window *w, *ow;
684 int eval, i, n;
685 Rune *rp;
686 uint dummy;
688 if(e->nname == 0){
689 w = t->w;
690 if(w == nil)
691 return nil;
692 }else
693 w = lookfile(e->name, e->nname);
694 if(w){
695 t = &w->body;
696 if(!t->col->safe && t->fr.maxlines==0) /* window is obscured by full-column window */
697 colgrow(t->col, t->col->w[0], 1);
698 }else{
699 ow = nil;
700 if(t)
701 ow = t->w;
702 w = makenewwindow(t);
703 t = &w->body;
704 winsetname(w, e->name, e->nname);
705 textload(t, 0, e->bname, 1);
706 t->file->mod = FALSE;
707 t->w->dirty = FALSE;
708 winsettag(t->w);
709 textsetselect(&t->w->tag, t->w->tag.file->b.nc, t->w->tag.file->b.nc);
710 if(ow != nil){
711 for(i=ow->nincl; --i>=0; ){
712 n = runestrlen(ow->incl[i]);
713 rp = runemalloc(n);
714 runemove(rp, ow->incl[i], n);
715 winaddincl(w, rp, n);
717 w->autoindent = ow->autoindent;
718 }else
719 w->autoindent = globalautoindent;
721 if(e->a1 == e->a0)
722 eval = FALSE;
723 else{
724 eval = TRUE;
725 r = address(TRUE, t, range(-1,-1), range(t->q0, t->q1), e->u.at, e->a0, e->a1, e->agetc, &eval, &dummy);
726 if(eval == FALSE)
727 e->jump = FALSE; /* don't jump if invalid address */
729 if(eval == FALSE){
730 r.q0 = t->q0;
731 r.q1 = t->q1;
733 textshow(t, r.q0, r.q1, 1);
734 winsettag(t->w);
735 seltext = t;
736 if(e->jump)
737 moveto(mousectl, addpt(frptofchar(&t->fr, t->fr.p0), Pt(4, font->height-4)));
738 return w;
741 void
742 new(Text *et, Text *t, Text *argt, int flag1, int flag2, Rune *arg, int narg)
744 int ndone;
745 Rune *a, *f;
746 int na, nf;
747 Expand e;
748 Runestr rs;
750 getarg(argt, FALSE, TRUE, &a, &na);
751 if(a){
752 new(et, t, nil, flag1, flag2, a, na);
753 if(narg == 0)
754 return;
756 /* loop condition: *arg is not a blank */
757 for(ndone=0; ; ndone++){
758 a = findbl(arg, narg, &na);
759 if(a == arg){
760 if(ndone==0 && et->col!=nil)
761 winsettag(coladd(et->col, nil, nil, -1));
762 break;
764 nf = narg-na;
765 f = runemalloc(nf);
766 runemove(f, arg, nf);
767 rs = dirname(et, f, nf);
768 f = rs.r;
769 nf = rs.nr;
770 memset(&e, 0, sizeof e);
771 e.name = f;
772 e.nname = nf;
773 e.bname = runetobyte(f, nf);
774 e.jump = TRUE;
775 openfile(et, &e);
776 free(f);
777 free(e.bname);
778 arg = skipbl(a, na, &narg);