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