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 <plumb.h>
11 #include "dat.h"
12 #include "fns.h"
14 int winid;
16 void
17 wininit(Window *w, Window *clone, Rectangle r)
18 {
19 Rectangle r1, br;
20 File *f;
21 Reffont *rf;
22 Rune *rp;
23 int nc;
25 w->tag.w = w;
26 w->body.w = w;
27 w->id = ++winid;
28 incref(&w->ref);
29 if(globalincref)
30 incref(&w->ref);
31 w->ctlfid = ~0;
32 w->utflastqid = -1;
33 r1 = r;
34 r1.max.y = r1.min.y + font->height;
35 incref(&reffont.ref);
36 f = fileaddtext(nil, &w->tag);
37 textinit(&w->tag, f, r1, &reffont, tagcols);
38 w->tag.what = Tag;
39 /* tag is a copy of the contents, not a tracked image */
40 if(clone){
41 textdelete(&w->tag, 0, w->tag.file->b.nc, TRUE);
42 nc = clone->tag.file->b.nc;
43 rp = runemalloc(nc);
44 bufread(&clone->tag.file->b, 0, rp, nc);
45 textinsert(&w->tag, 0, rp, nc, TRUE);
46 free(rp);
47 filereset(w->tag.file);
48 textsetselect(&w->tag, nc, nc);
49 }
50 //assert(w->body.w == w);
51 r1 = r;
52 r1.min.y += font->height + 1;
53 if(r1.max.y < r1.min.y)
54 r1.max.y = r1.min.y;
55 f = nil;
56 if(clone){
57 f = clone->body.file;
58 w->body.org = clone->body.org;
59 w->isscratch = clone->isscratch;
60 rf = rfget(FALSE, FALSE, FALSE, clone->body.reffont->f->name);
61 }else
62 rf = rfget(FALSE, FALSE, FALSE, nil);
63 //assert(w->body.w == w);
64 f = fileaddtext(f, &w->body);
65 w->body.what = Body;
66 textinit(&w->body, f, r1, rf, textcols);
67 r1.min.y -= 1;
68 r1.max.y = r1.min.y+1;
69 draw(screen, r1, tagcols[BORD], nil, ZP);
70 textscrdraw(&w->body);
71 w->r = r;
72 w->r.max.y = w->body.fr.r.max.y;
73 br.min = w->tag.scrollr.min;
74 br.max.x = br.min.x + Dx(button->r);
75 br.max.y = br.min.y + Dy(button->r);
76 draw(screen, br, button, nil, button->r.min);
77 w->filemenu = TRUE;
78 w->maxlines = w->body.fr.maxlines;
79 w->autoindent = globalautoindent;
80 //assert(w->body.w == w);
81 if(clone){
82 w->dirty = clone->dirty;
83 w->autoindent = clone->autoindent;
84 textsetselect(&w->body, clone->body.q0, clone->body.q1);
85 winsettag(w);
86 }
87 }
89 int
90 winresize(Window *w, Rectangle r, int safe)
91 {
92 Rectangle r1;
93 int y;
94 Image *b;
95 Rectangle br;
97 r1 = r;
98 r1.max.y = r1.min.y + font->height;
99 y = r1.max.y;
100 if(!safe || !eqrect(w->tag.fr.r, r1)){
101 y = textresize(&w->tag, r1);
102 b = button;
103 if(w->body.file->mod && !w->isdir && !w->isscratch)
104 b = modbutton;
105 br.min = w->tag.scrollr.min;
106 br.max.x = br.min.x + Dx(b->r);
107 br.max.y = br.min.y + Dy(b->r);
108 draw(screen, br, b, nil, b->r.min);
110 if(!safe || !eqrect(w->body.fr.r, r1)){
111 if(y+1+font->height > r.max.y){ /* no body */
112 r1.min.y = y;
113 r1.max.y = y;
114 textresize(&w->body, r1);
115 w->r = r;
116 w->r.max.y = y;
117 return y;
119 r1 = r;
120 r1.min.y = y;
121 r1.max.y = y + 1;
122 draw(screen, r1, tagcols[BORD], nil, ZP);
123 r1.min.y = y + 1;
124 r1.max.y = r.max.y;
125 y = textresize(&w->body, r1);
126 w->r = r;
127 w->r.max.y = y;
128 textscrdraw(&w->body);
130 w->maxlines = min(w->body.fr.nlines, max(w->maxlines, w->body.fr.maxlines));
131 return w->r.max.y;
134 void
135 winlock1(Window *w, int owner)
137 incref(&w->ref);
138 qlock(&w->lk);
139 w->owner = owner;
142 void
143 winlock(Window *w, int owner)
145 int i;
146 File *f;
148 f = w->body.file;
149 for(i=0; i<f->ntext; i++)
150 winlock1(f->text[i]->w, owner);
153 void
154 winunlock(Window *w)
156 int i;
157 File *f;
159 /*
160 * subtle: loop runs backwards to avoid tripping over
161 * winclose indirectly editing f->text and freeing f
162 * on the last iteration of the loop.
163 */
164 f = w->body.file;
165 for(i=f->ntext-1; i>=0; i--){
166 w = f->text[i]->w;
167 w->owner = 0;
168 qunlock(&w->lk);
169 winclose(w);
173 void
174 winmousebut(Window *w)
176 moveto(mousectl, divpt(addpt(w->tag.scrollr.min, w->tag.scrollr.max), 2));
179 void
180 windirfree(Window *w)
182 int i;
183 Dirlist *dl;
185 if(w->isdir){
186 for(i=0; i<w->ndl; i++){
187 dl = w->dlp[i];
188 free(dl->r);
189 free(dl);
191 free(w->dlp);
193 w->dlp = nil;
194 w->ndl = 0;
197 void
198 winclose(Window *w)
200 int i;
202 if(decref(&w->ref) == 0){
203 windirfree(w);
204 textclose(&w->tag);
205 textclose(&w->body);
206 if(activewin == w)
207 activewin = nil;
208 for(i=0; i<w->nincl; i++)
209 free(w->incl[i]);
210 free(w->incl);
211 free(w->events);
212 free(w);
216 void
217 windelete(Window *w)
219 Xfid *x;
221 x = w->eventx;
222 if(x){
223 w->nevents = 0;
224 free(w->events);
225 w->events = nil;
226 w->eventx = nil;
227 sendp(x->c, nil); /* wake him up */
231 void
232 winundo(Window *w, int isundo)
234 Text *body;
235 int i;
236 File *f;
237 Window *v;
239 w->utflastqid = -1;
240 body = &w->body;
241 fileundo(body->file, isundo, &body->q0, &body->q1);
242 textshow(body, body->q0, body->q1, 1);
243 f = body->file;
244 for(i=0; i<f->ntext; i++){
245 v = f->text[i]->w;
246 v->dirty = (f->seq != v->putseq);
247 if(v != w){
248 v->body.q0 = v->body.fr.p0+v->body.org;
249 v->body.q1 = v->body.fr.p1+v->body.org;
252 winsettag(w);
255 void
256 winsetname(Window *w, Rune *name, int n)
258 Text *t;
259 Window *v;
260 int i;
261 static Rune Lslashguide[] = { '/', 'g', 'u', 'i', 'd', 'e', 0 };
262 static Rune Lpluserrors[] = { '+', 'E', 'r', 'r', 'o', 'r', 's', 0 };
263 t = &w->body;
264 if(runeeq(t->file->name, t->file->nname, name, n) == TRUE)
265 return;
266 w->isscratch = FALSE;
267 if(n>=6 && runeeq(Lslashguide, 6, name+(n-6), 6))
268 w->isscratch = TRUE;
269 else if(n>=7 && runeeq(Lpluserrors, 7, name+(n-7), 7))
270 w->isscratch = TRUE;
271 filesetname(t->file, name, n);
272 for(i=0; i<t->file->ntext; i++){
273 v = t->file->text[i]->w;
274 winsettag(v);
275 v->isscratch = w->isscratch;
279 void
280 wintype(Window *w, Text *t, Rune r)
282 int i;
284 texttype(t, r);
285 if(t->what == Body)
286 for(i=0; i<t->file->ntext; i++)
287 textscrdraw(t->file->text[i]);
288 winsettag(w);
291 void
292 wincleartag(Window *w)
294 int i, n;
295 Rune *r;
297 /* w must be committed */
298 n = w->tag.file->b.nc;
299 r = runemalloc(n);
300 bufread(&w->tag.file->b, 0, r, n);
301 for(i=0; i<n; i++)
302 if(r[i]==' ' || r[i]=='\t')
303 break;
304 for(; i<n; i++)
305 if(r[i] == '|')
306 break;
307 if(i == n)
308 return;
309 i++;
310 textdelete(&w->tag, i, n, TRUE);
311 free(r);
312 w->tag.file->mod = FALSE;
313 if(w->tag.q0 > i)
314 w->tag.q0 = i;
315 if(w->tag.q1 > i)
316 w->tag.q1 = i;
317 textsetselect(&w->tag, w->tag.q0, w->tag.q1);
320 void
321 winsettag1(Window *w)
323 int bar, dirty, i, j, k, n, ntagname;
324 Rune *new, *old, *r, *tagname;
325 Image *b;
326 uint q0, q1;
327 Rectangle br;
328 static Rune Ldelsnarf[] = { ' ', 'D', 'e', 'l', ' ',
329 'S', 'n', 'a', 'r', 'f', 0 };
330 static Rune Lundo[] = { ' ', 'U', 'n', 'd', 'o', 0 };
331 static Rune Lredo[] = { ' ', 'R', 'e', 'd', 'o', 0 };
332 static Rune Lget[] = { ' ', 'G', 'e', 't', 0 };
333 static Rune Lput[] = { ' ', 'P', 'u', 't', 0 };
334 static Rune Llook[] = { ' ', 'L', 'o', 'o', 'k', ' ', 0 };
335 static Rune Lpipe[] = { ' ', '|', 0 };
336 /* there are races that get us here with stuff in the tag cache, so we take extra care to sync it */
337 if(w->tag.ncache!=0 || w->tag.file->mod)
338 wincommit(w, &w->tag); /* check file name; also guarantees we can modify tag contents */
339 old = runemalloc(w->tag.file->b.nc+1);
340 bufread(&w->tag.file->b, 0, old, w->tag.file->b.nc);
341 old[w->tag.file->b.nc] = '\0';
342 for(i=0; i<w->tag.file->b.nc; i++)
343 if(old[i]==' ' || old[i]=='\t')
344 break;
346 /* make sure the file name is set correctly in the tag */
347 ntagname = w->body.file->nname;
348 tagname = runemalloc(ntagname);
349 runemove(tagname, w->body.file->name, ntagname);
350 abbrevenv(&tagname, (uint*)&ntagname);
352 /*
353 * XXX Why is this here instead of letting the code
354 * down below do the work?
355 */
356 if(runeeq(old, i, tagname, ntagname) == FALSE){
357 q0 = w->tag.q0;
358 q1 = w->tag.q1;
359 textdelete(&w->tag, 0, i, TRUE);
360 textinsert(&w->tag, 0, tagname, ntagname, TRUE);
361 free(old);
362 old = runemalloc(w->tag.file->b.nc+1);
363 bufread(&w->tag.file->b, 0, old, w->tag.file->b.nc);
364 old[w->tag.file->b.nc] = '\0';
365 if(q0 >= i){
366 /*
367 * XXX common case - typing at end of name
368 */
369 w->tag.q0 = q0+ntagname-i;
370 w->tag.q1 = q1+ntagname-i;
374 /* compute the text for the whole tag, replacing current only if it differs */
375 new = runemalloc(ntagname+100);
376 i = 0;
377 runemove(new+i, tagname, ntagname);
378 i += ntagname;
379 runemove(new+i, Ldelsnarf, 10);
380 i += 10;
381 if(w->filemenu){
382 if(w->body.file->delta.nc>0 || w->body.ncache){
383 runemove(new+i, Lundo, 5);
384 i += 5;
386 if(w->body.file->epsilon.nc > 0){
387 runemove(new+i, Lredo, 5);
388 i += 5;
390 dirty = w->body.file->nname && (w->body.ncache || w->body.file->seq!=w->putseq);
391 if(!w->isdir && dirty){
392 runemove(new+i, Lput, 4);
393 i += 4;
396 if(w->isdir){
397 runemove(new+i, Lget, 4);
398 i += 4;
400 runemove(new+i, Lpipe, 2);
401 i += 2;
402 r = runestrchr(old, '|');
403 if(r)
404 k = r-old+1;
405 else{
406 k = w->tag.file->b.nc;
407 if(w->body.file->seq == 0){
408 runemove(new+i, Llook, 6);
409 i += 6;
412 new[i] = 0;
413 if(runestrlen(new) != i)
414 fprint(2, "s '%S' len not %d\n", new, i);
415 assert(i==runestrlen(new));
417 /* replace tag if the new one is different */
418 if(runeeq(new, i, old, k) == FALSE){
419 n = k;
420 if(n > i)
421 n = i;
422 for(j=0; j<n; j++)
423 if(old[j] != new[j])
424 break;
425 q0 = w->tag.q0;
426 q1 = w->tag.q1;
427 textdelete(&w->tag, j, k, TRUE);
428 textinsert(&w->tag, j, new+j, i-j, TRUE);
429 /* try to preserve user selection */
430 r = runestrchr(old, '|');
431 if(r){
432 bar = r-old;
433 if(q0 > bar){
434 bar = (runestrchr(new, '|')-new)-bar;
435 w->tag.q0 = q0+bar;
436 w->tag.q1 = q1+bar;
440 free(tagname);
441 free(old);
442 free(new);
443 w->tag.file->mod = FALSE;
444 n = w->tag.file->b.nc+w->tag.ncache;
445 if(w->tag.q0 > n)
446 w->tag.q0 = n;
447 if(w->tag.q1 > n)
448 w->tag.q1 = n;
449 textsetselect(&w->tag, w->tag.q0, w->tag.q1);
450 b = button;
451 if(!w->isdir && !w->isscratch && (w->body.file->mod || w->body.ncache))
452 b = modbutton;
453 br.min = w->tag.scrollr.min;
454 br.max.x = br.min.x + Dx(b->r);
455 br.max.y = br.min.y + Dy(b->r);
456 draw(screen, br, b, nil, b->r.min);
459 void
460 winsettag(Window *w)
462 int i;
463 File *f;
464 Window *v;
466 f = w->body.file;
467 for(i=0; i<f->ntext; i++){
468 v = f->text[i]->w;
469 if(v->col->safe || v->body.fr.maxlines>0)
470 winsettag1(v);
474 void
475 wincommit(Window *w, Text *t)
477 Rune *r;
478 int i;
479 File *f;
481 textcommit(t, TRUE);
482 f = t->file;
483 if(f->ntext > 1)
484 for(i=0; i<f->ntext; i++)
485 textcommit(f->text[i], FALSE); /* no-op for t */
486 if(t->what == Body)
487 return;
488 r = runemalloc(w->tag.file->b.nc);
489 bufread(&w->tag.file->b, 0, r, w->tag.file->b.nc);
490 for(i=0; i<w->tag.file->b.nc; i++)
491 if(r[i]==' ' || r[i]=='\t')
492 break;
493 expandenv(&r, (uint*)&i);
494 if(runeeq(r, i, w->body.file->name, w->body.file->nname) == FALSE){
495 seq++;
496 filemark(w->body.file);
497 w->body.file->mod = TRUE;
498 w->dirty = TRUE;
499 winsetname(w, r, i);
500 winsettag(w);
502 free(r);
505 void
506 winaddincl(Window *w, Rune *r, int n)
508 char *a;
509 Dir *d;
510 Runestr rs;
512 a = runetobyte(r, n);
513 d = dirstat(a);
514 if(d == nil){
515 if(a[0] == '/')
516 goto Rescue;
517 rs = dirname(&w->body, r, n);
518 r = rs.r;
519 n = rs.nr;
520 free(a);
521 a = runetobyte(r, n);
522 d = dirstat(a);
523 if(d == nil)
524 goto Rescue;
525 r = runerealloc(r, n+1);
526 r[n] = 0;
528 free(a);
529 if((d->qid.type&QTDIR) == 0){
530 free(d);
531 warning(nil, "%s: not a directory\n", a);
532 free(r);
533 return;
535 free(d);
536 w->nincl++;
537 w->incl = realloc(w->incl, w->nincl*sizeof(Rune*));
538 memmove(w->incl+1, w->incl, (w->nincl-1)*sizeof(Rune*));
539 w->incl[0] = runemalloc(n+1);
540 runemove(w->incl[0], r, n);
541 free(r);
542 return;
544 Rescue:
545 warning(nil, "%s: %r\n", a);
546 free(r);
547 free(a);
548 return;
551 int
552 winclean(Window *w, int conservative) /* as it stands, conservative is always TRUE */
554 if(w->isscratch || w->isdir) /* don't whine if it's a guide file, error window, etc. */
555 return TRUE;
556 if(!conservative && w->nopen[QWevent]>0)
557 return TRUE;
558 if(w->dirty){
559 if(w->body.file->nname)
560 warning(nil, "%.*S modified\n", w->body.file->nname, w->body.file->name);
561 else{
562 if(w->body.file->b.nc < 100) /* don't whine if it's too small */
563 return TRUE;
564 warning(nil, "unnamed file modified\n");
566 w->dirty = FALSE;
567 return FALSE;
569 return TRUE;
572 char*
573 winctlprint(Window *w, char *buf, int fonts)
575 sprint(buf, "%11d %11d %11d %11d %11d ", w->id, w->tag.file->b.nc,
576 w->body.file->b.nc, w->isdir, w->dirty);
577 if(fonts)
578 return smprint("%s%11d %q %11d ", buf, Dx(w->body.fr.r),
579 w->body.reffont->f->name, w->body.fr.maxtab);
580 return buf;
583 void
584 winevent(Window *w, char *fmt, ...)
586 int n;
587 char *b;
588 Xfid *x;
589 va_list arg;
591 if(w->nopen[QWevent] == 0)
592 return;
593 if(w->owner == 0)
594 error("no window owner");
595 va_start(arg, fmt);
596 b = vsmprint(fmt, arg);
597 va_end(arg);
598 if(b == nil)
599 error("vsmprint failed");
600 n = strlen(b);
601 w->events = realloc(w->events, w->nevents+1+n);
602 w->events[w->nevents++] = w->owner;
603 memmove(w->events+w->nevents, b, n);
604 free(b);
605 w->nevents += n;
606 x = w->eventx;
607 if(x){
608 w->eventx = nil;
609 sendp(x->c, nil);
613 /*
614 * This is here as a first stab at something.
615 * Run acme with the -'$' flag to enable it.
617 * This code isn't quite right, in that it doesn't play well with
618 * the plumber and with other applications. For example:
620 * If the window tag is $home/bin and you execute script, then acme runs
621 * script in $home/bin, via the shell, so everything is fine. If you do
622 * execute "echo $home", it too goes to the shell so you see the value
623 * of $home. And if you right-click on script, then acme plumbs "script"
624 * in the directory "/home/you/bin", so that works, but if you right-click
625 * on "$home/bin/script", then what? It's not correct to expand in acme
626 * since what you're plumbing might be a price tag for all we know. So the
627 * plumber has to expand it, but in order to do that the plumber should
628 * probably publish (and allow users to change) the set of variables it is
629 * using in expansions.
631 * Rob has suggested that a better solution is to make tag lines expand
632 * automatically to fit the necessary number of lines.
634 * The best solution, of course, is to use nice short path names, but this
635 * is not always possible.
636 */
638 int
639 expandenv(Rune **rp, uint *np)
641 char *s, *t;
642 Rune *p, *r, *q;
643 uint n, pref;
644 int nb, nr, slash;
645 Runestr rs;
647 if(!dodollarsigns)
648 return FALSE;
650 r = *rp;
651 n = *np;
652 if(n == 0 || r[0] != '$')
653 return FALSE;
654 for(p=r+1; *p && *p != '/'; p++)
656 pref = p-r;
657 s = runetobyte(r+1, pref-1);
658 if((t = getenv(s)) == nil){
659 free(s);
660 return FALSE;
663 q = runemalloc(utflen(t)+(n-pref));
664 cvttorunes(t, strlen(t), q, &nb, &nr, nil);
665 assert(nr==utflen(t));
666 runemove(q+nr, p, n-pref);
667 free(r);
668 rs = runestr(q, nr+(n-pref));
669 slash = rs.nr>0 && q[rs.nr-1] == '/';
670 rs = cleanrname(rs);
671 if(slash){
672 rs.r = runerealloc(rs.r, rs.nr+1);
673 rs.r[rs.nr++] = '/';
675 *rp = rs.r;
676 *np = rs.nr;
677 free(t);
678 return TRUE;
681 extern char **environ;
682 Rune **runeenv;
684 /*
685 * Weird sort order -- shorter names first,
686 * prefer lowercase over uppercase ($home over $HOME),
687 * then do normal string comparison.
688 */
689 int
690 runeenvcmp(const void *va, const void *vb)
692 Rune *a, *b;
693 int na, nb;
695 a = *(Rune**)va;
696 b = *(Rune**)vb;
697 na = runestrchr(a, '=') - a;
698 nb = runestrchr(b, '=') - b;
699 if(na < nb)
700 return -1;
701 if(nb > na)
702 return 1;
703 if(na == 0)
704 return 0;
705 if(islowerrune(a[0]) && !islowerrune(b[0]))
706 return -1;
707 if(islowerrune(b[0]) && !islowerrune(a[0]))
708 return 1;
709 return runestrncmp(a, b, na);
712 void
713 mkruneenv(void)
715 int i, bad, n, nr;
716 char *p;
717 Rune *r, *q;
719 n = 0;
720 for(i=0; environ[i]; i++){
721 /*
722 * Ignore some pollution.
723 */
724 if(environ[i][0] == '_')
725 continue;
726 if(strncmp(environ[i], "PWD=", 4) == 0)
727 continue;
728 if(strncmp(environ[i], "OLDPWD=", 7) == 0)
729 continue;
731 /*
732 * Must be a rooted path.
733 */
734 if((p = strchr(environ[i], '=')) == nil || *(p+1) != '/')
735 continue;
737 /*
738 * Only use the ones that we accept in look - all isfilec
739 */
740 bad = 0;
741 r = bytetorune(environ[i], &nr);
742 for(q=r; *q != '='; q++)
743 if(!isfilec(*q)){
744 free(r);
745 bad = 1;
746 break;
748 if(!bad){
749 runeenv = erealloc(runeenv, (n+1)*sizeof(runeenv[0]));
750 runeenv[n++] = r;
753 runeenv = erealloc(runeenv, (n+1)*sizeof(runeenv[0]));
754 runeenv[n] = nil;
755 qsort(runeenv, n, sizeof(runeenv[0]), runeenvcmp);
758 int
759 abbrevenv(Rune **rp, uint *np)
761 int i, len, alen;
762 Rune *r, *p, *q;
763 uint n;
765 if(!dodollarsigns)
766 return FALSE;
768 r = *rp;
769 n = *np;
770 if(n == 0 || r[0] != '/')
771 return FALSE;
773 if(runeenv == nil)
774 mkruneenv();
776 for(i=0; runeenv[i]; i++){
777 p = runestrchr(runeenv[i], '=')+1;
778 len = runestrlen(p);
779 if(len <= n && (r[len]==0 || r[len]=='/') && runeeq(r, len, p, len)==TRUE){
780 alen = (p-1) - runeenv[i];
781 q = runemalloc(1+alen+n-len);
782 q[0] = '$';
783 runemove(q+1, runeenv[i], alen);
784 runemove(q+1+alen, r+len, n-len);
785 free(r);
786 *rp = q;
787 *np = 1+alen+n-len;
788 return TRUE;
791 return FALSE;