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