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->taglines = 1;
27 w->tagexpand = TRUE;
28 w->body.w = w;
29 w->id = ++winid;
30 incref(&w->ref);
31 if(globalincref)
32 incref(&w->ref);
33 w->ctlfid = ~0;
34 w->utflastqid = -1;
35 r1 = r;
37 w->tagtop = r;
38 w->tagtop.max.y = r.min.y + font->height;
39 r1.max.y = r1.min.y + w->taglines*font->height;
41 incref(&reffont.ref);
42 f = fileaddtext(nil, &w->tag);
43 textinit(&w->tag, f, r1, &reffont, tagcols);
44 w->tag.what = Tag;
45 /* tag is a copy of the contents, not a tracked image */
46 if(clone){
47 textdelete(&w->tag, 0, w->tag.file->b.nc, TRUE);
48 nc = clone->tag.file->b.nc;
49 rp = runemalloc(nc);
50 bufread(&clone->tag.file->b, 0, rp, nc);
51 textinsert(&w->tag, 0, rp, nc, TRUE);
52 free(rp);
53 filereset(w->tag.file);
54 textsetselect(&w->tag, nc, nc);
55 }
56 r1 = r;
57 r1.min.y += w->taglines*font->height + 1;
58 if(r1.max.y < r1.min.y)
59 r1.max.y = r1.min.y;
60 f = nil;
61 if(clone){
62 f = clone->body.file;
63 w->body.org = clone->body.org;
64 w->isscratch = clone->isscratch;
65 rf = rfget(FALSE, FALSE, FALSE, clone->body.reffont->f->name);
66 }else
67 rf = rfget(FALSE, FALSE, FALSE, nil);
68 f = fileaddtext(f, &w->body);
69 w->body.what = Body;
70 textinit(&w->body, f, r1, rf, textcols);
71 r1.min.y -= 1;
72 r1.max.y = r1.min.y+1;
73 draw(screen, r1, tagcols[BORD], nil, ZP);
74 textscrdraw(&w->body);
75 w->r = r;
76 br.min = w->tag.scrollr.min;
77 br.max.x = br.min.x + Dx(button->r);
78 br.max.y = br.min.y + Dy(button->r);
79 draw(screen, br, button, nil, button->r.min);
80 w->filemenu = TRUE;
81 w->maxlines = w->body.fr.maxlines;
82 w->autoindent = globalautoindent;
83 if(clone){
84 w->dirty = clone->dirty;
85 w->autoindent = clone->autoindent;
86 textsetselect(&w->body, clone->body.q0, clone->body.q1);
87 winsettag(w);
88 }
89 }
91 /*
92 * Draw the appropriate button.
93 */
94 void
95 windrawbutton(Window *w)
96 {
97 Image *b;
98 Rectangle br;
100 b = button;
101 if(!w->isdir && !w->isscratch && (w->body.file->mod || w->body.ncache))
102 b = modbutton;
103 br.min = w->tag.scrollr.min;
104 br.max.x = br.min.x + Dx(b->r);
105 br.max.y = br.min.y + Dy(b->r);
106 draw(screen, br, b, nil, b->r.min);
109 /*
110 * Compute number of tag lines required
111 * to display entire tag text.
112 */
113 int
114 wintaglines(Window *w, Rectangle r)
116 int n;
117 Rune rune;
119 if(!w->tagexpand)
120 return 1;
121 w->tag.fr.noredraw = 1;
122 textresize(&w->tag, r, TRUE);
123 w->tag.fr.noredraw = 0;
124 w->tagsafe = FALSE;
126 /* can't use more than we have */
127 if(w->tag.fr.nlines >= w->tag.fr.maxlines)
128 return w->tag.fr.maxlines;
130 /* if tag ends with \n, include empty line at end for typing */
131 n = w->tag.fr.nlines;
132 if(w->tag.file->b.nc > 0){
133 bufread(&w->tag.file->b, w->tag.file->b.nc-1, &rune, 1);
134 if(rune == '\n')
135 n++;
137 if(n == 0)
138 n = 1;
139 return n;
142 int
143 winresize(Window *w, Rectangle r, int safe, int keepextra)
145 int oy, y, mouseintag, mouseinbody, tagresized;
146 Point p;
147 Rectangle r1;
149 mouseintag = ptinrect(mouse->xy, w->tag.all);
150 mouseinbody = ptinrect(mouse->xy, w->body.all);
152 /* tagtop is first line of tag */
153 w->tagtop = r;
154 w->tagtop.max.y = r.min.y+font->height;
156 r1 = r;
157 r1.max.y = min(r.max.y, r1.min.y + w->taglines*font->height);
159 /* If needed, recompute number of lines in tag. */
160 if(!safe || !w->tagsafe || !eqrect(w->tag.all, r1)){
161 w->taglines = wintaglines(w, r);
162 r1.max.y = min(r.max.y, r1.min.y + w->taglines*font->height);
165 /* If needed, resize & redraw tag. */
166 y = r1.max.y;
167 tagresized = 0;
168 if(!safe || !w->tagsafe || !eqrect(w->tag.all, r1)){
169 tagresized = 1;
170 textresize(&w->tag, r1, TRUE);
171 y = w->tag.fr.r.max.y;
172 windrawbutton(w);
173 w->tagsafe = TRUE;
175 /* If mouse is in tag, pull up as tag closes. */
176 if(mouseintag && !ptinrect(mouse->xy, w->tag.all)){
177 p = mouse->xy;
178 p.y = w->tag.all.max.y-3;
179 moveto(mousectl, p);
182 /* If mouse is in body, push down as tag expands. */
183 if(mouseinbody && ptinrect(mouse->xy, w->tag.all)){
184 p = mouse->xy;
185 p.y = w->tag.all.max.y+3;
186 moveto(mousectl, p);
190 /* If needed, resize & redraw body. */
191 r1 = r;
192 r1.min.y = y;
193 if(!safe || !eqrect(w->body.all, r1)){
194 oy = y;
195 if(y+1+w->body.fr.font->height <= r.max.y){ /* room for one line */
196 r1.min.y = y;
197 r1.max.y = y+1;
198 draw(screen, r1, tagcols[BORD], nil, ZP);
199 y++;
200 r1.min.y = min(y, r.max.y);
201 r1.max.y = r.max.y;
202 }else{
203 r1.min.y = y;
204 r1.max.y = y;
206 y = textresize(&w->body, r1, keepextra);
207 w->r = r;
208 w->r.max.y = y;
209 textscrdraw(&w->body);
210 w->body.all.min.y = oy;
212 w->maxlines = min(w->body.fr.nlines, max(w->maxlines, w->body.fr.maxlines));
213 return w->r.max.y;
216 void
217 winlock1(Window *w, int owner)
219 incref(&w->ref);
220 qlock(&w->lk);
221 w->owner = owner;
224 void
225 winlock(Window *w, int owner)
227 int i;
228 File *f;
230 f = w->body.file;
231 for(i=0; i<f->ntext; i++)
232 winlock1(f->text[i]->w, owner);
235 void
236 winunlock(Window *w)
238 int i;
239 File *f;
241 /*
242 * subtle: loop runs backwards to avoid tripping over
243 * winclose indirectly editing f->text and freeing f
244 * on the last iteration of the loop.
245 */
246 f = w->body.file;
247 for(i=f->ntext-1; i>=0; i--){
248 w = f->text[i]->w;
249 w->owner = 0;
250 qunlock(&w->lk);
251 winclose(w);
255 void
256 winmousebut(Window *w)
258 moveto(mousectl, addpt(w->tag.scrollr.min,
259 divpt(Pt(Dx(w->tag.scrollr), font->height), 2)));
262 void
263 windirfree(Window *w)
265 int i;
266 Dirlist *dl;
268 if(w->isdir){
269 for(i=0; i<w->ndl; i++){
270 dl = w->dlp[i];
271 free(dl->r);
272 free(dl);
274 free(w->dlp);
276 w->dlp = nil;
277 w->ndl = 0;
280 void
281 winclose(Window *w)
283 int i;
285 if(decref(&w->ref) == 0){
286 windirfree(w);
287 textclose(&w->tag);
288 textclose(&w->body);
289 if(activewin == w)
290 activewin = nil;
291 for(i=0; i<w->nincl; i++)
292 free(w->incl[i]);
293 free(w->incl);
294 free(w->events);
295 free(w);
299 void
300 windelete(Window *w)
302 Xfid *x;
304 x = w->eventx;
305 if(x){
306 w->nevents = 0;
307 free(w->events);
308 w->events = nil;
309 w->eventx = nil;
310 sendp(x->c, nil); /* wake him up */
314 void
315 winundo(Window *w, int isundo)
317 Text *body;
318 int i;
319 File *f;
320 Window *v;
322 w->utflastqid = -1;
323 body = &w->body;
324 fileundo(body->file, isundo, &body->q0, &body->q1);
325 textshow(body, body->q0, body->q1, 1);
326 f = body->file;
327 for(i=0; i<f->ntext; i++){
328 v = f->text[i]->w;
329 v->dirty = (f->seq != v->putseq);
330 if(v != w){
331 v->body.q0 = v->body.fr.p0+v->body.org;
332 v->body.q1 = v->body.fr.p1+v->body.org;
335 winsettag(w);
338 void
339 winsetname(Window *w, Rune *name, int n)
341 Text *t;
342 Window *v;
343 int i;
344 static Rune Lslashguide[] = { '/', 'g', 'u', 'i', 'd', 'e', 0 };
345 static Rune Lpluserrors[] = { '+', 'E', 'r', 'r', 'o', 'r', 's', 0 };
347 t = &w->body;
348 if(runeeq(t->file->name, t->file->nname, name, n) == TRUE)
349 return;
350 w->isscratch = FALSE;
351 if(n>=6 && runeeq(Lslashguide, 6, name+(n-6), 6))
352 w->isscratch = TRUE;
353 else if(n>=7 && runeeq(Lpluserrors, 7, name+(n-7), 7))
354 w->isscratch = TRUE;
355 filesetname(t->file, name, n);
356 for(i=0; i<t->file->ntext; i++){
357 v = t->file->text[i]->w;
358 winsettag(v);
359 v->isscratch = w->isscratch;
363 void
364 wintype(Window *w, Text *t, Rune r)
366 int i;
368 texttype(t, r);
369 if(t->what == Body)
370 for(i=0; i<t->file->ntext; i++)
371 textscrdraw(t->file->text[i]);
372 winsettag(w);
375 void
376 wincleartag(Window *w)
378 int i, n;
379 Rune *r;
381 /* w must be committed */
382 n = w->tag.file->b.nc;
383 r = runemalloc(n);
384 bufread(&w->tag.file->b, 0, r, n);
385 for(i=0; i<n; i++)
386 if(r[i]==' ' || r[i]=='\t')
387 break;
388 for(; i<n; i++)
389 if(r[i] == '|')
390 break;
391 if(i == n)
392 return;
393 i++;
394 textdelete(&w->tag, i, n, TRUE);
395 free(r);
396 w->tag.file->mod = FALSE;
397 if(w->tag.q0 > i)
398 w->tag.q0 = i;
399 if(w->tag.q1 > i)
400 w->tag.q1 = i;
401 textsetselect(&w->tag, w->tag.q0, w->tag.q1);
404 void
405 winsettag1(Window *w)
407 int i, j, k, n, bar, dirty, resize;
408 Rune *new, *old, *r;
409 uint q0, q1;
410 static Rune Ldelsnarf[] = { ' ', 'D', 'e', 'l', ' ',
411 'S', 'n', 'a', 'r', 'f', 0 };
412 static Rune Lundo[] = { ' ', 'U', 'n', 'd', 'o', 0 };
413 static Rune Lredo[] = { ' ', 'R', 'e', 'd', 'o', 0 };
414 static Rune Lget[] = { ' ', 'G', 'e', 't', 0 };
415 static Rune Lput[] = { ' ', 'P', 'u', 't', 0 };
416 static Rune Llook[] = { ' ', 'L', 'o', 'o', 'k', ' ', 0 };
417 static Rune Lpipe[] = { ' ', '|', 0 };
419 /* there are races that get us here with stuff in the tag cache, so we take extra care to sync it */
420 if(w->tag.ncache!=0 || w->tag.file->mod)
421 wincommit(w, &w->tag); /* check file name; also guarantees we can modify tag contents */
422 old = runemalloc(w->tag.file->b.nc+1);
423 bufread(&w->tag.file->b, 0, old, w->tag.file->b.nc);
424 old[w->tag.file->b.nc] = '\0';
425 for(i=0; i<w->tag.file->b.nc; i++)
426 if(old[i]==' ' || old[i]=='\t')
427 break;
428 if(runeeq(old, i, w->body.file->name, w->body.file->nname) == FALSE){
429 textdelete(&w->tag, 0, i, TRUE);
430 textinsert(&w->tag, 0, w->body.file->name, w->body.file->nname, TRUE);
431 free(old);
432 old = runemalloc(w->tag.file->b.nc+1);
433 bufread(&w->tag.file->b, 0, old, w->tag.file->b.nc);
434 old[w->tag.file->b.nc] = '\0';
437 /* compute the text for the whole tag, replacing current only if it differs */
438 new = runemalloc(w->body.file->nname+100);
439 i = 0;
440 runemove(new+i, w->body.file->name, w->body.file->nname);
441 i += w->body.file->nname;
442 runemove(new+i, Ldelsnarf, 10);
443 i += 10;
444 if(w->filemenu){
445 if(w->body.needundo || w->body.file->delta.nc>0 || w->body.ncache){
446 runemove(new+i, Lundo, 5);
447 i += 5;
449 if(w->body.file->epsilon.nc > 0){
450 runemove(new+i, Lredo, 5);
451 i += 5;
453 dirty = w->body.file->nname && (w->body.ncache || w->body.file->seq!=w->putseq);
454 if(!w->isdir && dirty){
455 runemove(new+i, Lput, 4);
456 i += 4;
459 if(w->isdir){
460 runemove(new+i, Lget, 4);
461 i += 4;
463 runemove(new+i, Lpipe, 2);
464 i += 2;
465 r = runestrchr(old, '|');
466 if(r)
467 k = r-old+1;
468 else{
469 k = w->tag.file->b.nc;
470 if(w->body.file->seq == 0){
471 runemove(new+i, Llook, 6);
472 i += 6;
475 new[i] = 0;
477 /* replace tag if the new one is different */
478 resize = 0;
479 if(runeeq(new, i, old, k) == FALSE){
480 resize = 1;
481 n = k;
482 if(n > i)
483 n = i;
484 for(j=0; j<n; j++)
485 if(old[j] != new[j])
486 break;
487 q0 = w->tag.q0;
488 q1 = w->tag.q1;
489 textdelete(&w->tag, j, k, TRUE);
490 textinsert(&w->tag, j, new+j, i-j, TRUE);
491 /* try to preserve user selection */
492 r = runestrchr(old, '|');
493 if(r){
494 bar = r-old;
495 if(q0 > bar){
496 bar = (runestrchr(new, '|')-new)-bar;
497 w->tag.q0 = q0+bar;
498 w->tag.q1 = q1+bar;
502 free(old);
503 free(new);
504 w->tag.file->mod = FALSE;
505 n = w->tag.file->b.nc+w->tag.ncache;
506 if(w->tag.q0 > n)
507 w->tag.q0 = n;
508 if(w->tag.q1 > n)
509 w->tag.q1 = n;
510 textsetselect(&w->tag, w->tag.q0, w->tag.q1);
511 windrawbutton(w);
512 if(resize){
513 w->tagsafe = 0;
514 winresize(w, w->r, TRUE, TRUE);
518 void
519 winsettag(Window *w)
521 int i;
522 File *f;
523 Window *v;
525 f = w->body.file;
526 for(i=0; i<f->ntext; i++){
527 v = f->text[i]->w;
528 if(v->col->safe || v->body.fr.maxlines>0)
529 winsettag1(v);
533 void
534 wincommit(Window *w, Text *t)
536 Rune *r;
537 int i;
538 File *f;
540 textcommit(t, TRUE);
541 f = t->file;
542 if(f->ntext > 1)
543 for(i=0; i<f->ntext; i++)
544 textcommit(f->text[i], FALSE); /* no-op for t */
545 if(t->what == Body)
546 return;
547 r = runemalloc(w->tag.file->b.nc);
548 bufread(&w->tag.file->b, 0, r, w->tag.file->b.nc);
549 for(i=0; i<w->tag.file->b.nc; i++)
550 if(r[i]==' ' || r[i]=='\t')
551 break;
552 if(runeeq(r, i, w->body.file->name, w->body.file->nname) == FALSE){
553 seq++;
554 filemark(w->body.file);
555 w->body.file->mod = TRUE;
556 w->dirty = TRUE;
557 winsetname(w, r, i);
558 winsettag(w);
560 free(r);
563 void
564 winaddincl(Window *w, Rune *r, int n)
566 char *a;
567 Dir *d;
568 Runestr rs;
570 a = runetobyte(r, n);
571 d = dirstat(a);
572 if(d == nil){
573 if(a[0] == '/')
574 goto Rescue;
575 rs = dirname(&w->body, r, n);
576 r = rs.r;
577 n = rs.nr;
578 free(a);
579 a = runetobyte(r, n);
580 d = dirstat(a);
581 if(d == nil)
582 goto Rescue;
583 r = runerealloc(r, n+1);
584 r[n] = 0;
586 free(a);
587 if((d->qid.type&QTDIR) == 0){
588 free(d);
589 warning(nil, "%s: not a directory\n", a);
590 free(r);
591 return;
593 free(d);
594 w->nincl++;
595 w->incl = realloc(w->incl, w->nincl*sizeof(Rune*));
596 memmove(w->incl+1, w->incl, (w->nincl-1)*sizeof(Rune*));
597 w->incl[0] = runemalloc(n+1);
598 runemove(w->incl[0], r, n);
599 free(r);
600 return;
602 Rescue:
603 warning(nil, "%s: %r\n", a);
604 free(r);
605 free(a);
606 return;
609 int
610 winclean(Window *w, int conservative) /* as it stands, conservative is always TRUE */
612 if(w->isscratch || w->isdir) /* don't whine if it's a guide file, error window, etc. */
613 return TRUE;
614 if(!conservative && w->nopen[QWevent]>0)
615 return TRUE;
616 if(w->dirty){
617 if(w->body.file->nname)
618 warning(nil, "%.*S modified\n", w->body.file->nname, w->body.file->name);
619 else{
620 if(w->body.file->b.nc < 100) /* don't whine if it's too small */
621 return TRUE;
622 warning(nil, "unnamed file modified\n");
624 w->dirty = FALSE;
625 return FALSE;
627 return TRUE;
630 char*
631 winctlprint(Window *w, char *buf, int fonts)
633 sprint(buf, "%11d %11d %11d %11d %11d ", w->id, w->tag.file->b.nc,
634 w->body.file->b.nc, w->isdir, w->dirty);
635 if(fonts)
636 return smprint("%s%11d %q %11d ", buf, Dx(w->body.fr.r),
637 w->body.reffont->f->name, w->body.fr.maxtab);
638 return buf;
641 void
642 winevent(Window *w, char *fmt, ...)
644 int n;
645 char *b;
646 Xfid *x;
647 va_list arg;
649 if(w->nopen[QWevent] == 0)
650 return;
651 if(w->owner == 0)
652 error("no window owner");
653 va_start(arg, fmt);
654 b = vsmprint(fmt, arg);
655 va_end(arg);
656 if(b == nil)
657 error("vsmprint failed");
658 n = strlen(b);
659 w->events = erealloc(w->events, w->nevents+1+n);
660 w->events[w->nevents++] = w->owner;
661 memmove(w->events+w->nevents, b, n);
662 free(b);
663 w->nevents += n;
664 x = w->eventx;
665 if(x){
666 w->eventx = nil;
667 sendp(x->c, nil);