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