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 w->ctlfid = ~0;
30 w->utflastqid = -1;
31 r1 = r;
32 r1.max.y = r1.min.y + font->height;
33 incref(&reffont.ref);
34 f = fileaddtext(nil, &w->tag);
35 textinit(&w->tag, f, r1, &reffont, tagcols);
36 w->tag.what = Tag;
37 /* tag is a copy of the contents, not a tracked image */
38 if(clone){
39 textdelete(&w->tag, 0, w->tag.file->b.nc, TRUE);
40 nc = clone->tag.file->b.nc;
41 rp = runemalloc(nc);
42 bufread(&clone->tag.file->b, 0, rp, nc);
43 textinsert(&w->tag, 0, rp, nc, TRUE);
44 free(rp);
45 filereset(w->tag.file);
46 textsetselect(&w->tag, nc, nc);
47 }
48 r1 = r;
49 r1.min.y += font->height + 1;
50 if(r1.max.y < r1.min.y)
51 r1.max.y = r1.min.y;
52 f = nil;
53 if(clone){
54 f = clone->body.file;
55 w->body.org = clone->body.org;
56 w->isscratch = clone->isscratch;
57 rf = rfget(FALSE, FALSE, FALSE, clone->body.reffont->f->name);
58 }else
59 rf = rfget(FALSE, FALSE, FALSE, nil);
60 f = fileaddtext(f, &w->body);
61 w->body.what = Body;
62 textinit(&w->body, f, r1, rf, textcols);
63 r1.min.y -= 1;
64 r1.max.y = r1.min.y+1;
65 draw(screen, r1, tagcols[BORD], nil, ZP);
66 textscrdraw(&w->body);
67 w->r = r;
68 w->r.max.y = w->body.fr.r.max.y;
69 br.min = w->tag.scrollr.min;
70 br.max.x = br.min.x + Dx(button->r);
71 br.max.y = br.min.y + Dy(button->r);
72 draw(screen, br, button, nil, button->r.min);
73 w->filemenu = TRUE;
74 w->maxlines = w->body.fr.maxlines;
75 if(clone){
76 w->dirty = clone->dirty;
77 textsetselect(&w->body, clone->body.q0, clone->body.q1);
78 winsettag(w);
79 }
80 }
82 int
83 winresize(Window *w, Rectangle r, int safe)
84 {
85 Rectangle r1;
86 int y;
87 Image *b;
88 Rectangle br;
90 r1 = r;
91 r1.max.y = r1.min.y + font->height;
92 y = r1.max.y;
93 if(!safe || !eqrect(w->tag.fr.r, r1)){
94 y = textresize(&w->tag, r1);
95 b = button;
96 if(w->body.file->mod && !w->isdir && !w->isscratch)
97 b = modbutton;
98 br.min = w->tag.scrollr.min;
99 br.max.x = br.min.x + Dx(b->r);
100 br.max.y = br.min.y + Dy(b->r);
101 draw(screen, br, b, nil, b->r.min);
103 if(!safe || !eqrect(w->body.fr.r, r1)){
104 if(y+1+font->height > r.max.y){ /* no body */
105 r1.min.y = y;
106 r1.max.y = y;
107 textresize(&w->body, r1);
108 w->r = r;
109 w->r.max.y = y;
110 return y;
112 r1 = r;
113 r1.min.y = y;
114 r1.max.y = y + 1;
115 draw(screen, r1, tagcols[BORD], nil, ZP);
116 r1.min.y = y + 1;
117 r1.max.y = r.max.y;
118 y = textresize(&w->body, r1);
119 w->r = r;
120 w->r.max.y = y;
121 textscrdraw(&w->body);
123 w->maxlines = min(w->body.fr.nlines, max(w->maxlines, w->body.fr.maxlines));
124 return w->r.max.y;
127 void
128 winlock1(Window *w, int owner)
130 incref(&w->ref);
131 qlock(&w->lk);
132 w->owner = owner;
135 void
136 winlock(Window *w, int owner)
138 int i;
139 File *f;
141 f = w->body.file;
142 for(i=0; i<f->ntext; i++)
143 winlock1(f->text[i]->w, owner);
146 void
147 winunlock(Window *w)
149 int i;
150 File *f;
152 f = w->body.file;
153 for(i=0; i<f->ntext; i++){
154 w = f->text[i]->w;
155 w->owner = 0;
156 qunlock(&w->lk);
157 winclose(w);
158 /* winclose() can change up f->text; beware */
159 if(f->ntext>0 && w != f->text[i]->w)
160 --i; /* winclose() deleted window */
164 void
165 winmousebut(Window *w)
167 moveto(mousectl, divpt(addpt(w->tag.scrollr.min, w->tag.scrollr.max), 2));
170 void
171 windirfree(Window *w)
173 int i;
174 Dirlist *dl;
176 if(w->isdir){
177 for(i=0; i<w->ndl; i++){
178 dl = w->dlp[i];
179 free(dl->r);
180 free(dl);
182 free(w->dlp);
184 w->dlp = nil;
185 w->ndl = 0;
188 void
189 winclose(Window *w)
191 int i;
193 if(decref(&w->ref) == 0){
194 windirfree(w);
195 textclose(&w->tag);
196 textclose(&w->body);
197 if(activewin == w)
198 activewin = nil;
199 for(i=0; i<w->nincl; i++)
200 free(w->incl[i]);
201 free(w->incl);
202 free(w->events);
203 free(w);
207 void
208 windelete(Window *w)
210 Xfid *x;
212 x = w->eventx;
213 if(x){
214 w->nevents = 0;
215 free(w->events);
216 w->events = nil;
217 w->eventx = nil;
218 sendp(x->c, nil); /* wake him up */
222 void
223 winundo(Window *w, int isundo)
225 Text *body;
226 int i;
227 File *f;
228 Window *v;
230 w->utflastqid = -1;
231 body = &w->body;
232 fileundo(body->file, isundo, &body->q0, &body->q1);
233 textshow(body, body->q0, body->q1, 1);
234 f = body->file;
235 for(i=0; i<f->ntext; i++){
236 v = f->text[i]->w;
237 v->dirty = (f->seq != v->putseq);
238 if(v != w){
239 v->body.q0 = v->body.fr.p0+v->body.org;
240 v->body.q1 = v->body.fr.p1+v->body.org;
243 winsettag(w);
246 void
247 winsetname(Window *w, Rune *name, int n)
249 Text *t;
250 Window *v;
251 int i;
252 static Rune Lslashguide[] = { '/', 'g', 'u', 'i', 'd', 'e', 0 };
253 static Rune Lpluserrors[] = { '+', 'E', 'r', 'r', 'o', 'r', 's', 0 };
254 t = &w->body;
255 if(runeeq(t->file->name, t->file->nname, name, n) == TRUE)
256 return;
257 w->isscratch = FALSE;
258 if(n>=6 && runeeq(Lslashguide, 6, name+(n-6), 6))
259 w->isscratch = TRUE;
260 else if(n>=7 && runeeq(Lpluserrors, 7, name+(n-7), 7))
261 w->isscratch = TRUE;
262 filesetname(t->file, name, n);
263 for(i=0; i<t->file->ntext; i++){
264 v = t->file->text[i]->w;
265 winsettag(v);
266 v->isscratch = w->isscratch;
270 void
271 wintype(Window *w, Text *t, Rune r)
273 int i;
275 texttype(t, r);
276 if(t->what == Body)
277 for(i=0; i<t->file->ntext; i++)
278 textscrdraw(t->file->text[i]);
279 winsettag(w);
282 void
283 wincleartag(Window *w)
285 int i, n;
286 Rune *r;
288 /* w must be committed */
289 n = w->tag.file->b.nc;
290 r = runemalloc(n);
291 bufread(&w->tag.file->b, 0, r, n);
292 for(i=0; i<n; i++)
293 if(r[i]==' ' || r[i]=='\t')
294 break;
295 for(; i<n; i++)
296 if(r[i] == '|')
297 break;
298 if(i == n)
299 return;
300 i++;
301 textdelete(&w->tag, i, n, TRUE);
302 free(r);
303 w->tag.file->mod = FALSE;
304 if(w->tag.q0 > i)
305 w->tag.q0 = i;
306 if(w->tag.q1 > i)
307 w->tag.q1 = i;
308 textsetselect(&w->tag, w->tag.q0, w->tag.q1);
311 void
312 winsettag1(Window *w)
314 int i, j, k, n, bar, dirty;
315 Rune *new, *old, *r;
316 Image *b;
317 uint q0, q1;
318 Rectangle br;
319 static Rune Ldelsnarf[] = { ' ', 'D', 'e', 'l', ' ',
320 'S', 'n', 'a', 'r', 'f', 0 };
321 static Rune Lundo[] = { ' ', 'U', 'n', 'd', 'o', 0 };
322 static Rune Lredo[] = { ' ', 'R', 'e', 'd', 'o', 0 };
323 static Rune Lget[] = { ' ', 'G', 'e', 't', 0 };
324 static Rune Lput[] = { ' ', 'P', 'u', 't', 0 };
325 static Rune Llook[] = { ' ', 'L', 'o', 'o', 'k', ' ', 0 };
326 static Rune Lpipe[] = { ' ', '|', 0 };
327 /* there are races that get us here with stuff in the tag cache, so we take extra care to sync it */
328 if(w->tag.ncache!=0 || w->tag.file->mod)
329 wincommit(w, &w->tag); /* check file name; also guarantees we can modify tag contents */
330 old = runemalloc(w->tag.file->b.nc+1);
331 bufread(&w->tag.file->b, 0, old, w->tag.file->b.nc);
332 old[w->tag.file->b.nc] = '\0';
333 for(i=0; i<w->tag.file->b.nc; i++)
334 if(old[i]==' ' || old[i]=='\t')
335 break;
336 if(runeeq(old, i, w->body.file->name, w->body.file->nname) == FALSE){
337 textdelete(&w->tag, 0, i, TRUE);
338 textinsert(&w->tag, 0, w->body.file->name, w->body.file->nname, TRUE);
339 free(old);
340 old = runemalloc(w->tag.file->b.nc+1);
341 bufread(&w->tag.file->b, 0, old, w->tag.file->b.nc);
342 old[w->tag.file->b.nc] = '\0';
344 new = runemalloc(w->body.file->nname+100);
345 i = 0;
346 runemove(new+i, w->body.file->name, w->body.file->nname);
347 i += w->body.file->nname;
348 runemove(new+i, Ldelsnarf, 10);
349 i += 10;
350 if(w->filemenu){
351 if(w->body.file->delta.nc>0 || w->body.ncache){
352 runemove(new+i, Lundo, 5);
353 i += 5;
355 if(w->body.file->epsilon.nc > 0){
356 runemove(new+i, Lredo, 5);
357 i += 5;
359 dirty = w->body.file->nname && (w->body.ncache || w->body.file->seq!=w->putseq);
360 if(!w->isdir && dirty){
361 runemove(new+i, Lput, 4);
362 i += 4;
365 if(w->isdir){
366 runemove(new+i, Lget, 4);
367 i += 4;
369 runemove(new+i, Lpipe, 2);
370 i += 2;
371 r = runestrchr(old, '|');
372 if(r)
373 k = r-old+1;
374 else{
375 k = w->tag.file->b.nc;
376 if(w->body.file->seq == 0){
377 runemove(new+i, Llook, 6);
378 i += 6;
381 new[i] = 0;
382 if(runestrlen(new) != i)
383 fprint(2, "s '%S' len not %d\n", new, i);
384 assert(i==runestrlen(new));
385 if(runeeq(new, i, old, k) == FALSE){
386 n = k;
387 if(n > i)
388 n = i;
389 for(j=0; j<n; j++)
390 if(old[j] != new[j])
391 break;
392 q0 = w->tag.q0;
393 q1 = w->tag.q1;
394 textdelete(&w->tag, j, k, TRUE);
395 textinsert(&w->tag, j, new+j, i-j, TRUE);
396 /* try to preserve user selection */
397 r = runestrchr(old, '|');
398 if(r){
399 bar = r-old;
400 if(q0 > bar){
401 bar = (runestrchr(new, '|')-new)-bar;
402 w->tag.q0 = q0+bar;
403 w->tag.q1 = q1+bar;
407 free(old);
408 free(new);
409 w->tag.file->mod = FALSE;
410 n = w->tag.file->b.nc+w->tag.ncache;
411 if(w->tag.q0 > n)
412 w->tag.q0 = n;
413 if(w->tag.q1 > n)
414 w->tag.q1 = n;
415 textsetselect(&w->tag, w->tag.q0, w->tag.q1);
416 b = button;
417 if(!w->isdir && !w->isscratch && (w->body.file->mod || w->body.ncache))
418 b = modbutton;
419 br.min = w->tag.scrollr.min;
420 br.max.x = br.min.x + Dx(b->r);
421 br.max.y = br.min.y + Dy(b->r);
422 draw(screen, br, b, nil, b->r.min);
425 void
426 winsettag(Window *w)
428 int i;
429 File *f;
430 Window *v;
432 f = w->body.file;
433 for(i=0; i<f->ntext; i++){
434 v = f->text[i]->w;
435 if(v->col->safe || v->body.fr.maxlines>0)
436 winsettag1(v);
440 void
441 wincommit(Window *w, Text *t)
443 Rune *r;
444 int i;
445 File *f;
447 textcommit(t, TRUE);
448 f = t->file;
449 if(f->ntext > 1)
450 for(i=0; i<f->ntext; i++)
451 textcommit(f->text[i], FALSE); /* no-op for t */
452 if(t->what == Body)
453 return;
454 r = runemalloc(w->tag.file->b.nc);
455 bufread(&w->tag.file->b, 0, r, w->tag.file->b.nc);
456 for(i=0; i<w->tag.file->b.nc; i++)
457 if(r[i]==' ' || r[i]=='\t')
458 break;
459 if(runeeq(r, i, w->body.file->name, w->body.file->nname) == FALSE){
460 seq++;
461 filemark(w->body.file);
462 w->body.file->mod = TRUE;
463 w->dirty = TRUE;
464 winsetname(w, r, i);
465 winsettag(w);
467 free(r);
470 void
471 winaddincl(Window *w, Rune *r, int n)
473 char *a;
474 Dir *d;
475 Runestr rs;
477 a = runetobyte(r, n);
478 d = dirstat(a);
479 if(d == nil){
480 if(a[0] == '/')
481 goto Rescue;
482 rs = dirname(&w->body, r, n);
483 r = rs.r;
484 n = rs.nr;
485 free(a);
486 a = runetobyte(r, n);
487 d = dirstat(a);
488 if(d == nil)
489 goto Rescue;
490 r = runerealloc(r, n+1);
491 r[n] = 0;
493 free(a);
494 if((d->qid.type&QTDIR) == 0){
495 free(d);
496 warning(nil, "%s: not a directory\n", a);
497 free(r);
498 return;
500 free(d);
501 w->nincl++;
502 w->incl = realloc(w->incl, w->nincl*sizeof(Rune*));
503 memmove(w->incl+1, w->incl, (w->nincl-1)*sizeof(Rune*));
504 w->incl[0] = runemalloc(n+1);
505 runemove(w->incl[0], r, n);
506 free(r);
507 return;
509 Rescue:
510 warning(nil, "%s: %r\n", a);
511 free(r);
512 free(a);
513 return;
516 int
517 winclean(Window *w, int conservative) /* as it stands, conservative is always TRUE */
519 if(w->isscratch || w->isdir) /* don't whine if it's a guide file, error window, etc. */
520 return TRUE;
521 if(!conservative && w->nopen[QWevent]>0)
522 return TRUE;
523 if(w->dirty){
524 if(w->body.file->nname)
525 warning(nil, "%.*S modified\n", w->body.file->nname, w->body.file->name);
526 else{
527 if(w->body.file->b.nc < 100) /* don't whine if it's too small */
528 return TRUE;
529 warning(nil, "unnamed file modified\n");
531 w->dirty = FALSE;
532 return FALSE;
534 return TRUE;
537 void
538 winctlprint(Window *w, char *buf, int fonts)
540 int n;
542 n = sprint(buf, "%11d %11d %11d %11d %11d ", w->id, w->tag.file->b.nc,
543 w->body.file->b.nc, w->isdir, w->dirty);
544 if(fonts)
545 sprint(buf+n, "%11d %s" , Dx(w->body.fr.r), w->body.reffont->f->name);
548 void
549 winevent(Window *w, char *fmt, ...)
551 int n;
552 char *b;
553 Xfid *x;
554 va_list arg;
556 if(w->nopen[QWevent] == 0)
557 return;
558 if(w->owner == 0)
559 error("no window owner");
560 va_start(arg, fmt);
561 b = vsmprint(fmt, arg);
562 va_end(arg);
563 if(b == nil)
564 error("vsmprint failed");
565 n = strlen(b);
566 w->events = realloc(w->events, w->nevents+1+n);
567 w->events[w->nevents++] = w->owner;
568 memmove(w->events+w->nevents, b, n);
569 free(b);
570 w->nevents += n;
571 x = w->eventx;
572 if(x){
573 w->eventx = nil;
574 sendp(x->c, nil);