]> git.lizzy.rs Git - plan9front.git/blob - sys/src/cmd/acme/wind.c
stats: show amount of reclaimable pages (add -r flag)
[plan9front.git] / sys / src / cmd / acme / wind.c
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"
13
14 int     winid;
15
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;
24
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);
31         if(globalincref)
32                 incref(w);
33         w->ctlfid = ~0;
34         w->utflastqid = -1;
35         r1 = r;
36         r1.max.y = r1.min.y + font->height;
37         w->tagtop = r;
38         w->tagtop.max.y = r.min.y + font->height;
39         incref(&reffont);
40         f = fileaddtext(nil, &w->tag);
41         textinit(&w->tag, f, r1, &reffont, tagcols);
42         w->tag.what = Tag;
43         /* tag is a copy of the contents, not a tracked image */
44         if(clone){
45                 textdelete(&w->tag, 0, w->tag.file->nc, TRUE);
46                 nc = clone->tag.file->nc;
47                 rp = runemalloc(nc);
48                 bufread(clone->tag.file, 0, rp, nc);
49                 textinsert(&w->tag, 0, rp, nc, TRUE);
50                 free(rp);
51                 filereset(w->tag.file);
52                 textsetselect(&w->tag, nc, nc);
53         }
54         r1 = r;
55         r1.min.y += w->taglines*font->height + 1;
56         if(r1.max.y < r1.min.y)
57                 r1.max.y = r1.min.y;
58         f = nil;
59         if(clone){
60                 f = clone->body.file;
61                 w->body.org = clone->body.org;
62                 w->isscratch = clone->isscratch;
63                 rf = rfget(FALSE, FALSE, FALSE, clone->body.reffont->f->name);
64         }else
65                 rf = rfget(FALSE, FALSE, FALSE, nil);
66         f = fileaddtext(f, &w->body);
67         w->body.what = Body;
68         textinit(&w->body, f, r1, rf, textcols);
69         r1.min.y -= 1;
70         r1.max.y = r1.min.y+1;
71         draw(screen, r1, tagcols[BORD], nil, ZP);
72         textscrdraw(&w->body);
73         w->r = r;
74         w->r.max.y = w->body.r.max.y;
75         br.min = w->tag.scrollr.min;
76         br.max.x = br.min.x + Dx(button->r);
77         br.max.y = br.min.y + Dy(button->r);
78         draw(screen, br, button, nil, button->r.min);
79         w->filemenu = TRUE;
80         w->maxlines = w->body.maxlines;
81         w->autoindent = globalautoindent;
82         if(clone){
83                 w->dirty = clone->dirty;
84                 w->autoindent = clone->autoindent;
85                 textsetselect(&w->body, clone->body.q0, clone->body.q1);
86                 winsettag(w);
87                 w->autoindent = clone->autoindent;
88         }
89 }
90
91 int
92 delrunepos(Window *w)
93 {
94         int n;
95         Rune rune;
96         
97         for(n=0; n<w->tag.file->nc; n++) {
98                 bufread(w->tag.file, n, &rune, 1);
99                 if(rune == ' ')
100                         break;
101         }
102         n += 2;
103         if(n >= w->tag.file->nc)
104                 return -1;
105         return n;
106 }
107
108 void
109 movetodel(Window *w)
110 {
111         int n;
112         
113         n = delrunepos(w);
114         if(n < 0)
115                 return;
116         moveto(mousectl, addpt(frptofchar(&w->tag, n), Pt(4, w->tag.font->height-4)));
117 }
118
119 /*
120  * Compute number of tag lines required
121  * to display entire tag text.
122  */
123 int
124 wintaglines(Window *w, Rectangle r)
125 {
126         int n;
127         Rune rune;
128         Point p;
129
130         if(!w->tagexpand && !w->showdel)
131                 return 1;
132         w->showdel = FALSE;
133         w->noredraw = 1;
134         textresize(&w->tag, r);
135         w->noredraw = 0;
136         w->tagsafe = FALSE;
137         
138         if(!w->tagexpand) {
139                 /* use just as many lines as needed to show the Del */
140                 n = delrunepos(w);
141                 if(n < 0)
142                         return 1;
143                 p = subpt(frptofchar(&w->tag, n), w->tag.r.min);
144                 return 1 + p.y / w->tag.font->height;
145         }
146                 
147         /* can't use more than we have */
148         if(w->tag.nlines >= w->tag.maxlines)
149                 return w->tag.maxlines;
150
151         /* if tag ends with \n, include empty line at end for typing */
152         n = w->tag.nlines;
153         if(w->tag.file->nc > 0){
154                 bufread(w->tag.file, w->tag.file->nc-1, &rune, 1);
155                 if(rune == '\n')
156                         n++;
157         }
158         if(n == 0)
159                 n = 1;
160         return n;
161 }
162
163 int
164 winresize(Window *w, Rectangle r, int safe)
165 {
166         int oy, mouseintag, mouseinbody;
167         Point p;
168         Rectangle r1;
169         int y;
170         Image *b;
171         Rectangle br;
172
173         mouseintag = ptinrect(mouse->xy, w->tag.all);
174         mouseinbody = ptinrect(mouse->xy, w->body.all);
175
176         w->tagtop = r;
177         w->tagtop.max.y = r.min.y+font->height;
178
179         r1 = r;
180         r1.max.y = r1.min.y + font->height;
181         r1.max.y = min(r.max.y, r1.min.y + w->taglines*font->height);
182
183         if(!safe || !w->tagsafe || !eqrect(w->tag.all, r1)){
184                 w->taglines = wintaglines(w, r);
185                 r1.max.y = min(r.max.y, r1.min.y + w->taglines*font->height);
186         }
187         if(Dy(r1) < font->height)
188                 r1.max.y = r1.min.y+font->height;
189         y = r1.max.y;
190         if(!safe || !eqrect(w->tag.r, r1)){
191                 textresize(&w->tag, r1);
192                 y = w->tag.r.max.y;
193                 b = button;
194                 if(w->body.file->mod && !w->isdir && !w->isscratch)
195                         b = modbutton;
196                 br.min = w->tag.scrollr.min;
197                 br.max.x = br.min.x + Dx(b->r);
198                 br.max.y = br.min.y + Dy(b->r);
199                 draw(screen, br, b, nil, b->r.min);
200
201                 w->tagsafe = TRUE;
202
203                 /* If mouse is in tag, pull up as tag closes. */
204                 if(mouseintag && !ptinrect(mouse->xy, w->tag.all)){
205                         p = mouse->xy;
206                         p.y = w->tag.all.max.y-3;
207                         moveto(mousectl, p);
208                 }
209
210                 /* If mouse is in body, push down as tag expands. */
211                 if(mouseinbody && ptinrect(mouse->xy, w->tag.all)){
212                         p = mouse->xy;
213                         p.y = w->tag.all.max.y+3;
214                         moveto(mousectl, p);
215                 }
216
217         }
218         if(!safe || !eqrect(w->body.r, r1)){
219                 oy = y;
220                 if(y+1+w->body.font->height <= r.max.y){        /* room for one line */
221                         r1.min.y = y;
222                         r1.max.y = y+1;
223                         draw(screen, r1, tagcols[BORD], nil, ZP);
224                         y++;
225                         r1.min.y = min(y, r.max.y);
226                         r1.max.y = r.max.y;
227                 }else{
228                         r1.min.y = y;
229                         r1.max.y = y;
230                 }
231                 y = textresize(&w->body, r1);
232                 w->r = r;
233                 w->r.max.y = y;
234                 textscrdraw(&w->body);
235                 w->body.all.min.y = oy;
236         }
237         w->maxlines = min(w->body.nlines, max(w->maxlines, w->body.maxlines));
238         return w->r.max.y;
239 }
240
241 void
242 winlock1(Window *w, int owner)
243 {
244         incref(w);
245         qlock(w);
246         w->owner = owner;
247 }
248
249 void
250 winlock(Window *w, int owner)
251 {
252         int i;
253         File *f;
254
255         f = w->body.file;
256         for(i=0; i<f->ntext; i++)
257                 winlock1(f->text[i]->w, owner);
258 }
259
260 void
261 winunlock(Window *w)
262 {
263         int i;
264         File *f;
265
266         /*
267          * subtle: loop runs backwards to avoid tripping over
268          * winclose indirectly editing f->text and freeing f
269          * on the last iteration of the loop.
270          */
271         f = w->body.file;
272         for(i=f->ntext-1; i>=0; i--){
273                 w = f->text[i]->w;
274                 w->owner = 0;
275                 qunlock(w);
276                 winclose(w);
277         }
278 }
279
280 void
281 winmousebut(Window *w)
282 {
283         moveto(mousectl, addpt(w->tag.scrollr.min, divpt(Pt(Dx(w->tag.scrollr), font->height), 2)));
284 }
285
286 void
287 windirfree(Window *w)
288 {
289         int i;
290         Dirlist *dl;
291
292         if(w->isdir){
293                 for(i=0; i<w->ndl; i++){
294                         dl = w->dlp[i];
295                         free(dl->r);
296                         free(dl);
297                 }
298                 free(w->dlp);
299         }
300         w->dlp = nil;
301         w->ndl = 0;
302 }
303
304 void
305 winclose(Window *w)
306 {
307         int i;
308
309         if(decref(w) == 0){
310                 windirfree(w);
311                 textclose(&w->tag);
312                 textclose(&w->body);
313                 if(activewin == w)
314                         activewin = nil;
315                 for(i=0; i<w->nincl; i++)
316                         free(w->incl[i]);
317                 free(w->incl);
318                 free(w->events);
319                 free(w);
320         }
321 }
322
323 void
324 windelete(Window *w)
325 {
326         Xfid *x;
327
328         x = w->eventx;
329         if(x){
330                 w->nevents = 0;
331                 free(w->events);
332                 w->events = nil;
333                 w->eventx = nil;
334                 sendp(x->c, nil);       /* wake him up */
335         }
336 }
337
338 void
339 winundo(Window *w, int isundo)
340 {
341         Text *body;
342         int i;
343         File *f;
344         Window *v;
345
346         w->utflastqid = -1;
347         body = &w->body;
348         fileundo(body->file, isundo, &body->q0, &body->q1);
349         textshow(body, body->q0, body->q1, 1);
350         f = body->file;
351         for(i=0; i<f->ntext; i++){
352                 v = f->text[i]->w;
353                 v->dirty = (f->seq != v->putseq);
354                 if(v != w){
355                         v->body.q0 = v->body.p0+v->body.org;
356                         v->body.q1 = v->body.p1+v->body.org;
357                 }
358         }
359         winsettag(w);
360 }
361
362 void
363 winsetname(Window *w, Rune *name, int n)
364 {
365         Text *t;
366         Window *v;
367         int i;
368
369         t = &w->body;
370         if(runeeq(t->file->name, t->file->nname, name, n) == TRUE)
371                 return;
372         w->isscratch = FALSE;
373         if(n>=6 && runeeq(L"/guide", 6, name+(n-6), 6))
374                 w->isscratch = TRUE;
375         else if(n>=7 && runeeq(L"+Errors", 7, name+(n-7), 7))
376                 w->isscratch = TRUE;
377         filesetname(t->file, name, n);
378         for(i=0; i<t->file->ntext; i++){
379                 v = t->file->text[i]->w;
380                 winsettag(v);
381                 v->isscratch = w->isscratch;
382         }
383 }
384
385 void
386 wintype(Window *w, Text *t, Rune r)
387 {
388         int i;
389
390         texttype(t, r);
391         if(t->what == Tag)
392                 w->tagsafe = FALSE;
393         if(t->what == Body)
394                 for(i=0; i<t->file->ntext; i++)
395                         textscrdraw(t->file->text[i]);
396         winsettag(w);
397 }
398
399 void
400 wincleartag(Window *w)
401 {
402         int i, n;
403         Rune *r;
404
405         /* w must be committed */
406         n = w->tag.file->nc;
407         r = runemalloc(n);
408         bufread(w->tag.file, 0, r, n);
409         for(i=0; i<n; i++)
410                 if(r[i]==' ' || r[i]=='\t')
411                         break;
412         for(; i<n; i++)
413                 if(r[i] == '|')
414                         break;
415         if(i == n)
416                 return;
417         i++;
418         textdelete(&w->tag, i, n, TRUE);
419         free(r);
420         w->tag.file->mod = FALSE;
421         if(w->tag.q0 > i)
422                 w->tag.q0 = i;
423         if(w->tag.q1 > i)
424                 w->tag.q1 = i;
425         textsetselect(&w->tag, w->tag.q0, w->tag.q1);
426 }
427
428 void
429 winsettag1(Window *w)
430 {
431         int i, j, k, n, bar, dirty;
432         Rune *new, *old, *r;
433         Image *b;
434         uint q0, q1;
435         Rectangle br;
436
437         /* there are races that get us here with stuff in the tag cache, so we take extra care to sync it */
438         if(w->tag.ncache!=0 || w->tag.file->mod)
439                 wincommit(w, &w->tag);  /* check file name; also guarantees we can modify tag contents */
440         old = runemalloc(w->tag.file->nc+1);
441         bufread(w->tag.file, 0, old, w->tag.file->nc);
442         old[w->tag.file->nc] = '\0';
443         for(i=0; i<w->tag.file->nc; i++)
444                 if(old[i]==' ' || old[i]=='\t')
445                         break;
446         if(runeeq(old, i, w->body.file->name, w->body.file->nname) == FALSE){
447                 textdelete(&w->tag, 0, i, TRUE);
448                 textinsert(&w->tag, 0, w->body.file->name, w->body.file->nname, TRUE);
449                 free(old);
450                 old = runemalloc(w->tag.file->nc+1);
451                 bufread(w->tag.file, 0, old, w->tag.file->nc);
452                 old[w->tag.file->nc] = '\0';
453                 w->tagsafe = FALSE;
454         }
455         new = runemalloc(w->body.file->nname+100);
456         i = 0;
457         runemove(new+i, w->body.file->name, w->body.file->nname);
458         i += w->body.file->nname;
459         runemove(new+i, L" Del Snarf", 10);
460         i += 10;
461         if(w->filemenu){
462                 if(w->body.file->delta.nc>0 || w->body.ncache){
463                         runemove(new+i, L" Undo", 5);
464                         i += 5;
465                 }
466                 if(w->body.file->epsilon.nc > 0){
467                         runemove(new+i, L" Redo", 5);
468                         i += 5;
469                 }
470                 dirty = w->body.file->nname && (w->body.ncache || w->body.file->seq!=w->putseq);
471                 if(!w->isdir && dirty){
472                         runemove(new+i, L" Put", 4);
473                         i += 4;
474                 }
475         }
476         if(w->isdir){
477                 runemove(new+i, L" Get", 4);
478                 i += 4;
479         }
480         runemove(new+i, L" |", 2);
481         i += 2;
482         r = runestrchr(old, '|');
483         if(r)
484                 k = r-old+1;
485         else{
486                 k = w->tag.file->nc;
487                 if(w->body.file->seq == 0){
488                         runemove(new+i, L" Look ", 6);
489                         i += 6;
490                 }
491         }
492
493         new[i] = 0;
494         /* replace tag if the new one is different */
495         if(runeeq(new, i, old, k) == FALSE){
496                 n = k;
497                 if(n > i)
498                         n = i;
499                 for(j=0; j<n; j++)
500                         if(old[j] != new[j])
501                                 break;
502                 q0 = w->tag.q0;
503                 q1 = w->tag.q1;
504                 textdelete(&w->tag, j, k, TRUE);
505                 textinsert(&w->tag, j, new+j, i-j, TRUE);
506                 /* try to preserve user selection */
507                 r = runestrchr(old, '|');
508                 if(r){
509                         bar = r-old;
510                         if(q0 > bar){
511                                 bar = (runestrchr(new, '|')-new)-bar;
512                                 w->tag.q0 = q0+bar;
513                                 w->tag.q1 = q1+bar;
514                         }
515                 }
516                 w->tagsafe = FALSE;
517         }
518         free(old);
519         free(new);
520         w->tag.file->mod = FALSE;
521         n = w->tag.file->nc+w->tag.ncache;
522         if(w->tag.q0 > n)
523                 w->tag.q0 = n;
524         if(w->tag.q1 > n)
525                 w->tag.q1 = n;
526         textsetselect(&w->tag, w->tag.q0, w->tag.q1);
527         b = button;
528         if(!w->isdir && !w->isscratch && (w->body.file->mod || w->body.ncache))
529                 b = modbutton;
530         br.min = w->tag.scrollr.min;
531         br.max.x = br.min.x + Dx(b->r);
532         br.max.y = br.min.y + Dy(b->r);
533         draw(screen, br, b, nil, b->r.min);
534         if(w->tagsafe == FALSE)
535                 winresize(w, w->r, TRUE);
536 }
537
538 void
539 winsettag(Window *w)
540 {
541         int i;
542         File *f;
543         Window *v;
544
545         f = w->body.file;
546         for(i=0; i<f->ntext; i++){
547                 v = f->text[i]->w;
548                 if(v->col->safe || v->body.maxlines>0)
549                         winsettag1(v);
550         }
551 }
552
553 void
554 wincommit(Window *w, Text *t)
555 {
556         Rune *r;
557         int i;
558         File *f;
559
560         textcommit(t, TRUE);
561         f = t->file;
562         if(f->ntext > 1)
563                 for(i=0; i<f->ntext; i++)
564                         textcommit(f->text[i], FALSE);  /* no-op for t */
565         if(t->what == Body)
566                 return;
567         r = runemalloc(w->tag.file->nc);
568         bufread(w->tag.file, 0, r, w->tag.file->nc);
569         for(i=0; i<w->tag.file->nc; i++)
570                 if(r[i]==' ' || r[i]=='\t')
571                         break;
572         if(runeeq(r, i, w->body.file->name, w->body.file->nname) == FALSE){
573                 seq++;
574                 filemark(w->body.file);
575                 w->body.file->mod = TRUE;
576                 w->dirty = TRUE;
577                 winsetname(w, r, i);
578                 winsettag(w);
579         }
580         free(r);
581 }
582
583 void
584 winaddincl(Window *w, Rune *r, int n)
585 {
586         char *a;
587         Dir *d;
588         Runestr rs;
589
590         a = runetobyte(r, n);
591         d = dirstat(a);
592         if(d == nil){
593                 if(a[0] == '/')
594                         goto Rescue;
595                 rs = dirname(&w->body, r, n);
596                 r = rs.r;
597                 n = rs.nr;
598                 free(a);
599                 a = runetobyte(r, n);
600                 d = dirstat(a);
601                 if(d == nil)
602                         goto Rescue;
603                 r = runerealloc(r, n+1);
604                 r[n] = 0;
605         }
606         free(a);
607         if((d->qid.type&QTDIR) == 0){
608                 free(d);
609                 warning(nil, "%s: not a directory\n", a);
610                 free(r);
611                 return;
612         }
613         free(d);
614         w->nincl++;
615         w->incl = realloc(w->incl, w->nincl*sizeof(Rune*));
616         memmove(w->incl+1, w->incl, (w->nincl-1)*sizeof(Rune*));
617         w->incl[0] = runemalloc(n+1);
618         runemove(w->incl[0], r, n);
619         free(r);
620         return;
621
622 Rescue:
623         warning(nil, "%s: %r\n", a);
624         free(r);
625         free(a);
626         return;
627 }
628
629 int
630 winclean(Window *w, int conservative)   /* as it stands, conservative is always TRUE */
631 {
632         if(w->isscratch || w->isdir)    /* don't whine if it's a guide file, error window, etc. */
633                 return TRUE;
634         if(!conservative && w->nopen[QWevent]>0)
635                 return TRUE;
636         if(w->dirty){
637                 if(w->body.file->nname)
638                         warning(nil, "%.*S modified\n", w->body.file->nname, w->body.file->name);
639                 else{
640                         if(w->body.file->nc < 100)      /* don't whine if it's too small */
641                                 return TRUE;
642                         warning(nil, "unnamed file modified\n");
643                 }
644                 w->dirty = FALSE;
645                 return FALSE;
646         }
647         return TRUE;
648 }
649
650 char*
651 winctlprint(Window *w, char *buf, int fonts)
652 {
653         sprint(buf, "%11d %11d %11d %11d %11d ", w->id, w->tag.file->nc,
654                 w->body.file->nc, w->isdir, w->dirty);
655         if(fonts)
656                 return smprint("%s%11d %q %11d " , buf, Dx(w->body.r), 
657                         w->body.reffont->f->name, w->body.maxtab);
658         return buf;
659 }
660
661 void
662 winevent(Window *w, char *fmt, ...)
663 {
664         int n;
665         char *b;
666         Xfid *x;
667         va_list arg;
668
669         if(w->nopen[QWevent] == 0)
670                 return;
671         if(w->owner == 0)
672                 error("no window owner");
673         va_start(arg, fmt);
674         b = vsmprint(fmt, arg);
675         va_end(arg);
676         if(b == nil)
677                 error("vsmprint failed");
678         n = strlen(b);
679         w->events = erealloc(w->events, w->nevents+1+n);
680         w->events[w->nevents++] = w->owner;
681         memmove(w->events+w->nevents, b, n);
682         free(b);
683         w->nevents += n;
684         x = w->eventx;
685         if(x){
686                 w->eventx = nil;
687                 sendp(x->c, nil);
688         }
689 }