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 <complete.h>
12 #include "dat.h"
13 #include "fns.h"
15 Image *tagcols[NCOL];
16 Image *textcols[NCOL];
17 static Rune Ldot[] = { '.', 0 };
19 enum{
20 TABDIR = 3 /* width of tabs in directory windows */
21 };
23 void
24 textinit(Text *t, File *f, Rectangle r, Reffont *rf, Image *cols[NCOL])
25 {
26 t->file = f;
27 t->all = r;
28 t->scrollr = r;
29 t->scrollr.max.x = r.min.x+Scrollwid;
30 t->lastsr = nullrect;
31 r.min.x += Scrollwid+Scrollgap;
32 t->eq0 = ~0;
33 t->ncache = 0;
34 t->reffont = rf;
35 t->tabstop = maxtab;
36 memmove(t->fr.cols, cols, sizeof t->fr.cols);
37 textredraw(t, r, rf->f, screen, -1);
38 }
40 void
41 textredraw(Text *t, Rectangle r, Font *f, Image *b, int odx)
42 {
43 int maxt;
44 Rectangle rr;
46 frinit(&t->fr, r, f, b, t->fr.cols);
47 rr = t->fr.r;
48 rr.min.x -= Scrollwid+Scrollgap; /* back fill to scroll bar */
49 draw(t->fr.b, rr, t->fr.cols[BACK], nil, ZP);
50 /* use no wider than 3-space tabs in a directory */
51 maxt = maxtab;
52 if(t->what == Body){
53 if(t->w->isdir)
54 maxt = min(TABDIR, maxtab);
55 else
56 maxt = t->tabstop;
57 }
58 t->fr.maxtab = maxt*stringwidth(f, "0");
59 if(t->what==Body && t->w->isdir && odx!=Dx(t->all)){
60 if(t->fr.maxlines > 0){
61 textreset(t);
62 textcolumnate(t, t->w->dlp, t->w->ndl);
63 textshow(t, 0, 0, 1);
64 }
65 }else{
66 textfill(t);
67 textsetselect(t, t->q0, t->q1);
68 }
69 }
71 int
72 textresize(Text *t, Rectangle r)
73 {
74 int odx;
76 if(Dy(r) > 0)
77 r.max.y -= Dy(r)%t->fr.font->height;
78 else
79 r.max.y = r.min.y;
80 odx = Dx(t->all);
81 t->all = r;
82 t->scrollr = r;
83 t->scrollr.max.x = r.min.x+Scrollwid;
84 t->lastsr = nullrect;
85 r.min.x += Scrollwid+Scrollgap;
86 frclear(&t->fr, 0);
87 textredraw(t, r, t->fr.font, screen, odx);
88 return r.max.y;
89 }
91 void
92 textclose(Text *t)
93 {
94 free(t->cache);
95 frclear(&t->fr, 1);
96 filedeltext(t->file, t);
97 t->file = nil;
98 rfclose(t->reffont);
99 if(argtext == t)
100 argtext = nil;
101 if(typetext == t)
102 typetext = nil;
103 if(seltext == t)
104 seltext = nil;
105 if(mousetext == t)
106 mousetext = nil;
107 if(barttext == t)
108 barttext = nil;
111 int
112 dircmp(const void *a, const void *b)
114 Dirlist *da, *db;
115 int i, n;
117 da = *(Dirlist**)a;
118 db = *(Dirlist**)b;
119 n = min(da->nr, db->nr);
120 i = memcmp(da->r, db->r, n*sizeof(Rune));
121 if(i)
122 return i;
123 return da->nr - db->nr;
126 void
127 textcolumnate(Text *t, Dirlist **dlp, int ndl)
129 int i, j, w, colw, mint, maxt, ncol, nrow;
130 Dirlist *dl;
131 uint q1;
132 static Rune Lnl[] = { '\n', 0 };
133 static Rune Ltab[] = { '\t', 0 };
135 if(t->file->ntext > 1)
136 return;
137 mint = stringwidth(t->fr.font, "0");
138 /* go for narrower tabs if set more than 3 wide */
139 t->fr.maxtab = min(maxtab, TABDIR)*mint;
140 maxt = t->fr.maxtab;
141 colw = 0;
142 for(i=0; i<ndl; i++){
143 dl = dlp[i];
144 w = dl->wid;
145 if(maxt-w%maxt < mint || w%maxt==0)
146 w += mint;
147 if(w % maxt)
148 w += maxt-(w%maxt);
149 if(w > colw)
150 colw = w;
152 if(colw == 0)
153 ncol = 1;
154 else
155 ncol = max(1, Dx(t->fr.r)/colw);
156 nrow = (ndl+ncol-1)/ncol;
158 q1 = 0;
159 for(i=0; i<nrow; i++){
160 for(j=i; j<ndl; j+=nrow){
161 dl = dlp[j];
162 fileinsert(t->file, q1, dl->r, dl->nr);
163 q1 += dl->nr;
164 if(j+nrow >= ndl)
165 break;
166 w = dl->wid;
167 if(maxt-w%maxt < mint){
168 fileinsert(t->file, q1, Ltab, 1);
169 q1++;
170 w += mint;
172 do{
173 fileinsert(t->file, q1, Ltab, 1);
174 q1++;
175 w += maxt-(w%maxt);
176 }while(w < colw);
178 fileinsert(t->file, q1, Lnl, 1);
179 q1++;
183 uint
184 textload(Text *t, uint q0, char *file, int setqid)
186 Rune *rp;
187 Dirlist *dl, **dlp;
188 int fd, i, j, n, ndl, nulls;
189 uint q, q1;
190 Dir *d, *dbuf;
191 char *tmp;
192 Text *u;
194 if(t->ncache!=0 || t->file->b.nc || t->w==nil || t!=&t->w->body || (t->w->isdir && t->file->nname==0))
195 error("text.load");
196 if(t->w->isdir && t->file->nname==0){
197 warning(nil, "empty directory name");
198 return 0;
200 fd = open(file, OREAD);
201 if(fd < 0){
202 warning(nil, "can't open %s: %r\n", file);
203 return 0;
205 d = dirfstat(fd);
206 if(d == nil){
207 warning(nil, "can't fstat %s: %r\n", file);
208 goto Rescue;
210 nulls = FALSE;
211 if(d->qid.type & QTDIR){
212 /* this is checked in get() but it's possible the file changed underfoot */
213 if(t->file->ntext > 1){
214 warning(nil, "%s is a directory; can't read with multiple windows on it\n", file);
215 goto Rescue;
217 t->w->isdir = TRUE;
218 t->w->filemenu = FALSE;
219 if(t->file->name[t->file->nname-1] != '/'){
220 rp = runemalloc(t->file->nname+1);
221 runemove(rp, t->file->name, t->file->nname);
222 rp[t->file->nname] = '/';
223 winsetname(t->w, rp, t->file->nname+1);
224 free(rp);
226 dlp = nil;
227 ndl = 0;
228 dbuf = nil;
229 while((n=dirread(fd, &dbuf)) > 0){
230 for(i=0; i<n; i++){
231 dl = emalloc(sizeof(Dirlist));
232 j = strlen(dbuf[i].name);
233 tmp = emalloc(j+1+1);
234 memmove(tmp, dbuf[i].name, j);
235 if(dbuf[i].qid.type & QTDIR)
236 tmp[j++] = '/';
237 tmp[j] = '\0';
238 dl->r = bytetorune(tmp, &dl->nr);
239 dl->wid = stringwidth(t->fr.font, tmp);
240 free(tmp);
241 ndl++;
242 dlp = realloc(dlp, ndl*sizeof(Dirlist*));
243 dlp[ndl-1] = dl;
245 free(dbuf);
247 qsort(dlp, ndl, sizeof(Dirlist*), dircmp);
248 t->w->dlp = dlp;
249 t->w->ndl = ndl;
250 textcolumnate(t, dlp, ndl);
251 q1 = t->file->b.nc;
252 }else{
253 t->w->isdir = FALSE;
254 t->w->filemenu = TRUE;
255 q1 = q0 + fileload(t->file, q0, fd, &nulls);
257 if(setqid){
258 t->file->dev = d->dev;
259 t->file->mtime = d->mtime;
260 t->file->qidpath = d->qid.path;
262 close(fd);
263 rp = fbufalloc();
264 for(q=q0; q<q1; q+=n){
265 n = q1-q;
266 if(n > RBUFSIZE)
267 n = RBUFSIZE;
268 bufread(&t->file->b, q, rp, n);
269 if(q < t->org)
270 t->org += n;
271 else if(q <= t->org+t->fr.nchars)
272 frinsert(&t->fr, rp, rp+n, q-t->org);
273 if(t->fr.lastlinefull)
274 break;
276 fbuffree(rp);
277 for(i=0; i<t->file->ntext; i++){
278 u = t->file->text[i];
279 if(u != t){
280 if(u->org > u->file->b.nc) /* will be 0 because of reset(), but safety first */
281 u->org = 0;
282 textresize(u, u->all);
283 textbacknl(u, u->org, 0); /* go to beginning of line */
285 textsetselect(u, q0, q0);
287 if(nulls)
288 warning(nil, "%s: NUL bytes elided\n", file);
289 free(d);
290 return q1-q0;
292 Rescue:
293 close(fd);
294 return 0;
297 uint
298 textbsinsert(Text *t, uint q0, Rune *r, uint n, int tofile, int *nrp)
300 Rune *bp, *tp, *up;
301 int i, initial;
303 if(t->what == Tag){ /* can't happen but safety first: mustn't backspace over file name */
304 Err:
305 textinsert(t, q0, r, n, tofile);
306 *nrp = n;
307 return q0;
309 bp = r;
310 for(i=0; i<n; i++)
311 if(*bp++ == '\b'){
312 --bp;
313 initial = 0;
314 tp = runemalloc(n);
315 runemove(tp, r, i);
316 up = tp+i;
317 for(; i<n; i++){
318 *up = *bp++;
319 if(*up == '\b')
320 if(up == tp)
321 initial++;
322 else
323 --up;
324 else
325 up++;
327 if(initial){
328 if(initial > q0)
329 initial = q0;
330 q0 -= initial;
331 textdelete(t, q0, q0+initial, tofile);
333 n = up-tp;
334 textinsert(t, q0, tp, n, tofile);
335 free(tp);
336 *nrp = n;
337 return q0;
339 goto Err;
342 void
343 textinsert(Text *t, uint q0, Rune *r, uint n, int tofile)
345 int c, i;
346 Text *u;
348 if(tofile && t->ncache != 0)
349 error("text.insert");
350 if(n == 0)
351 return;
352 if(tofile){
353 fileinsert(t->file, q0, r, n);
354 if(t->what == Body){
355 t->w->dirty = TRUE;
356 t->w->utflastqid = -1;
358 if(t->file->ntext > 1)
359 for(i=0; i<t->file->ntext; i++){
360 u = t->file->text[i];
361 if(u != t){
362 u->w->dirty = TRUE; /* always a body */
363 textinsert(u, q0, r, n, FALSE);
364 textsetselect(u, u->q0, u->q1);
365 textscrdraw(u);
370 if(q0 < t->q1)
371 t->q1 += n;
372 if(q0 < t->q0)
373 t->q0 += n;
374 if(q0 < t->org)
375 t->org += n;
376 else if(q0 <= t->org+t->fr.nchars)
377 frinsert(&t->fr, r, r+n, q0-t->org);
378 if(t->w){
379 c = 'i';
380 if(t->what == Body)
381 c = 'I';
382 if(n <= EVENTSIZE)
383 winevent(t->w, "%c%d %d 0 %d %.*S\n", c, q0, q0+n, n, n, r);
384 else
385 winevent(t->w, "%c%d %d 0 0 \n", c, q0, q0+n, n);
390 void
391 textfill(Text *t)
393 Rune *rp;
394 int i, n, m, nl;
396 if(t->fr.lastlinefull || t->nofill)
397 return;
398 if(t->ncache > 0){
399 if(t->w != nil)
400 wincommit(t->w, t);
401 else
402 textcommit(t, TRUE);
404 rp = fbufalloc();
405 do{
406 n = t->file->b.nc-(t->org+t->fr.nchars);
407 if(n == 0)
408 break;
409 if(n > 2000) /* educated guess at reasonable amount */
410 n = 2000;
411 bufread(&t->file->b, t->org+t->fr.nchars, rp, n);
412 /*
413 * it's expensive to frinsert more than we need, so
414 * count newlines.
415 */
416 nl = t->fr.maxlines-t->fr.nlines;
417 m = 0;
418 for(i=0; i<n; ){
419 if(rp[i++] == '\n'){
420 m++;
421 if(m >= nl)
422 break;
425 frinsert(&t->fr, rp, rp+i, t->fr.nchars);
426 }while(t->fr.lastlinefull == FALSE);
427 fbuffree(rp);
430 void
431 textdelete(Text *t, uint q0, uint q1, int tofile)
433 uint n, p0, p1;
434 int i, c;
435 Text *u;
437 if(tofile && t->ncache != 0)
438 error("text.delete");
439 n = q1-q0;
440 if(n == 0)
441 return;
442 if(tofile){
443 filedelete(t->file, q0, q1);
444 if(t->what == Body){
445 t->w->dirty = TRUE;
446 t->w->utflastqid = -1;
448 if(t->file->ntext > 1)
449 for(i=0; i<t->file->ntext; i++){
450 u = t->file->text[i];
451 if(u != t){
452 u->w->dirty = TRUE; /* always a body */
453 textdelete(u, q0, q1, FALSE);
454 textsetselect(u, u->q0, u->q1);
455 textscrdraw(u);
459 if(q0 < t->q0)
460 t->q0 -= min(n, t->q0-q0);
461 if(q0 < t->q1)
462 t->q1 -= min(n, t->q1-q0);
463 if(q1 <= t->org)
464 t->org -= n;
465 else if(q0 < t->org+t->fr.nchars){
466 p1 = q1 - t->org;
467 if(p1 > t->fr.nchars)
468 p1 = t->fr.nchars;
469 if(q0 < t->org){
470 t->org = q0;
471 p0 = 0;
472 }else
473 p0 = q0 - t->org;
474 frdelete(&t->fr, p0, p1);
475 textfill(t);
477 if(t->w){
478 c = 'd';
479 if(t->what == Body)
480 c = 'D';
481 winevent(t->w, "%c%d %d 0 0 \n", c, q0, q1);
485 void
486 textconstrain(Text *t, uint q0, uint q1, uint *p0, uint *p1)
488 *p0 = min(q0, t->file->b.nc);
489 *p1 = min(q1, t->file->b.nc);
492 Rune
493 textreadc(Text *t, uint q)
495 Rune r;
497 if(t->cq0<=q && q<t->cq0+t->ncache)
498 r = t->cache[q-t->cq0];
499 else
500 bufread(&t->file->b, q, &r, 1);
501 return r;
504 int
505 textbswidth(Text *t, Rune c)
507 uint q, eq;
508 Rune r;
509 int skipping;
511 /* there is known to be at least one character to erase */
512 if(c == 0x08) /* ^H: erase character */
513 return 1;
514 q = t->q0;
515 skipping = TRUE;
516 while(q > 0){
517 r = textreadc(t, q-1);
518 if(r == '\n'){ /* eat at most one more character */
519 if(q == t->q0) /* eat the newline */
520 --q;
521 break;
523 if(c == 0x17){
524 eq = isalnum(r);
525 if(eq && skipping) /* found one; stop skipping */
526 skipping = FALSE;
527 else if(!eq && !skipping)
528 break;
530 --q;
532 return t->q0-q;
535 int
536 textfilewidth(Text *t, uint q0, int oneelement)
538 uint q;
539 Rune r;
541 q = q0;
542 while(q > 0){
543 r = textreadc(t, q-1);
544 if(r <= ' ')
545 break;
546 if(oneelement && r=='/')
547 break;
548 --q;
550 return q0-q;
553 Rune*
554 textcomplete(Text *t)
556 int i, nstr, npath;
557 uint q;
558 Rune tmp[200];
559 Rune *str, *path;
560 Rune *rp;
561 Completion *c;
562 char *s, *dirs;
563 Runestr dir;
565 /* control-f: filename completion; works back to white space or / */
566 if(t->q0<t->file->b.nc && textreadc(t, t->q0)>' ') /* must be at end of word */
567 return nil;
568 nstr = textfilewidth(t, t->q0, TRUE);
569 str = runemalloc(nstr);
570 npath = textfilewidth(t, t->q0-nstr, FALSE);
571 path = runemalloc(npath);
573 c = nil;
574 rp = nil;
575 dirs = nil;
577 q = t->q0-nstr;
578 for(i=0; i<nstr; i++)
579 str[i] = textreadc(t, q++);
580 q = t->q0-nstr-npath;
581 for(i=0; i<npath; i++)
582 path[i] = textreadc(t, q++);
583 /* is path rooted? if not, we need to make it relative to window path */
584 if(npath>0 && path[0]=='/')
585 dir = runestr(path, npath);
586 else{
587 dir = dirname(t, nil, 0);
588 if(dir.nr + 1 + npath > nelem(tmp)){
589 free(dir.r);
590 goto Return;
592 if(dir.nr == 0){
593 dir.nr = 1;
594 dir.r = runestrdup(Ldot);
596 runemove(tmp, dir.r, dir.nr);
597 tmp[dir.nr] = '/';
598 runemove(tmp+dir.nr+1, path, npath);
599 free(dir.r);
600 dir.r = tmp;
601 dir.nr += 1+npath;
602 dir = cleanrname(dir);
605 s = smprint("%.*S", nstr, str);
606 dirs = smprint("%.*S", dir.nr, dir.r);
607 c = complete(dirs, s);
608 free(s);
609 if(c == nil){
610 warning(nil, "error attempting completion: %r\n");
611 goto Return;
614 if(!c->advance){
615 warning(nil, "%.*S%s%.*S*%s\n",
616 dir.nr, dir.r,
617 dir.nr>0 && dir.r[dir.nr-1]!='/' ? "/" : "",
618 nstr, str,
619 c->nmatch ? "" : ": no matches in:");
620 for(i=0; i<c->nfile; i++)
621 warning(nil, " %s\n", c->filename[i]);
624 if(c->advance)
625 rp = runesmprint("%s", c->string);
626 else
627 rp = nil;
628 Return:
629 freecompletion(c);
630 free(dirs);
631 free(str);
632 free(path);
633 return rp;
636 void
637 texttype(Text *t, Rune r)
639 uint q0, q1;
640 int nnb, nb, n, i;
641 int nr;
642 Rune *rp;
643 Text *u;
645 if(t->what!=Body && r=='\n')
646 return;
647 nr = 1;
648 rp = &r;
649 switch(r){
650 case Kleft:
651 if(t->q0 > 0){
652 if(t->w)
653 wincommit(t->w, t);
654 else
655 textcommit(t, TRUE);
656 textshow(t, t->q0-1, t->q0-1, TRUE);
658 return;
659 case Kright:
660 if(t->q1 < t->file->b.nc){
661 wincommit(t->w, t);
662 textshow(t, t->q1+1, t->q1+1, TRUE);
664 return;
665 case Kdown:
666 n = t->fr.maxlines/3;
667 goto case_Down;
668 case Kscrollonedown:
669 n = mousescrollsize(t->fr.maxlines);
670 if(n <= 0)
671 n = 1;
672 goto case_Down;
673 case Kpgdown:
674 n = 2*t->fr.maxlines/3;
675 case_Down:
676 q0 = t->org+frcharofpt(&t->fr, Pt(t->fr.r.min.x, t->fr.r.min.y+n*t->fr.font->height));
677 textsetorigin(t, q0, TRUE);
678 return;
679 case Kup:
680 n = t->fr.maxlines/3;
681 goto case_Up;
682 case Kscrolloneup:
683 n = mousescrollsize(t->fr.maxlines);
684 goto case_Up;
685 case Kpgup:
686 n = 2*t->fr.maxlines/3;
687 case_Up:
688 q0 = textbacknl(t, t->org, n);
689 textsetorigin(t, q0, TRUE);
690 return;
691 case Khome:
692 textshow(t, 0, 0, FALSE);
693 return;
694 case Kend:
695 if(t->w)
696 wincommit(t->w, t);
697 else
698 textcommit(t, TRUE);
699 textshow(t, t->file->b.nc, t->file->b.nc, FALSE);
700 return;
702 if(t->what == Body){
703 seq++;
704 filemark(t->file);
706 if(t->q1 > t->q0){
707 if(t->ncache != 0)
708 error("text.type");
709 cut(t, t, nil, TRUE, TRUE, nil, 0);
710 t->eq0 = ~0;
712 textshow(t, t->q0, t->q0, 1);
713 switch(r){
714 case 0x06: /* ^F: complete */
715 case Kins:
716 rp = textcomplete(t);
717 if(rp == nil)
718 return;
719 nr = runestrlen(rp);
720 break; /* fall through to normal insertion case */
721 case 0x1B:
722 if(t->eq0 != ~0)
723 textsetselect(t, t->eq0, t->q0);
724 if(t->ncache > 0){
725 if(t->w != nil)
726 wincommit(t->w, t);
727 else
728 textcommit(t, TRUE);
730 return;
731 case 0x08: /* ^H: erase character */
732 case 0x15: /* ^U: erase line */
733 case 0x17: /* ^W: erase word */
734 if(t->q0 == 0) /* nothing to erase */
735 return;
736 nnb = textbswidth(t, r);
737 q1 = t->q0;
738 q0 = q1-nnb;
739 /* if selection is at beginning of window, avoid deleting invisible text */
740 if(q0 < t->org){
741 q0 = t->org;
742 nnb = q1-q0;
744 if(nnb <= 0)
745 return;
746 for(i=0; i<t->file->ntext; i++){
747 u = t->file->text[i];
748 u->nofill = TRUE;
749 nb = nnb;
750 n = u->ncache;
751 if(n > 0){
752 if(q1 != u->cq0+n)
753 error("text.type backspace");
754 if(n > nb)
755 n = nb;
756 u->ncache -= n;
757 textdelete(u, q1-n, q1, FALSE);
758 nb -= n;
760 if(u->eq0==q1 || u->eq0==~0)
761 u->eq0 = q0;
762 if(nb && u==t)
763 textdelete(u, q0, q0+nb, TRUE);
764 if(u != t)
765 textsetselect(u, u->q0, u->q1);
766 else
767 textsetselect(t, q0, q0);
768 u->nofill = FALSE;
770 for(i=0; i<t->file->ntext; i++)
771 textfill(t->file->text[i]);
772 return;
773 case '\n':
774 if(t->w->autoindent){
775 /* find beginning of previous line using backspace code */
776 nnb = textbswidth(t, 0x15); /* ^U case */
777 rp = runemalloc(nnb + 1);
778 nr = 0;
779 rp[nr++] = r;
780 for(i=0; i<nnb; i++){
781 r = textreadc(t, t->q0-nnb+i);
782 if(r != ' ' && r != '\t')
783 break;
784 rp[nr++] = r;
787 break; /* fall through to normal code */
789 /* otherwise ordinary character; just insert, typically in caches of all texts */
790 for(i=0; i<t->file->ntext; i++){
791 u = t->file->text[i];
792 if(u->eq0 == ~0)
793 u->eq0 = t->q0;
794 if(u->ncache == 0)
795 u->cq0 = t->q0;
796 else if(t->q0 != u->cq0+u->ncache)
797 error("text.type cq1");
798 textinsert(u, t->q0, rp, nr, FALSE);
799 if(u != t)
800 textsetselect(u, u->q0, u->q1);
801 if(u->ncache+nr > u->ncachealloc){
802 u->ncachealloc += 10 + nr;
803 u->cache = runerealloc(u->cache, u->ncachealloc);
805 runemove(u->cache+u->ncache, rp, nr);
806 u->ncache += nr;
808 if(rp != &r)
809 free(rp);
810 textsetselect(t, t->q0+nr, t->q0+nr);
811 if(r=='\n' && t->w!=nil)
812 wincommit(t->w, t);
815 void
816 textcommit(Text *t, int tofile)
818 if(t->ncache == 0)
819 return;
820 if(tofile)
821 fileinsert(t->file, t->cq0, t->cache, t->ncache);
822 if(t->what == Body){
823 t->w->dirty = TRUE;
824 t->w->utflastqid = -1;
826 t->ncache = 0;
829 static Text *clicktext;
830 static uint clickmsec;
831 static Text *selecttext;
832 static uint selectq;
834 /*
835 * called from frame library
836 */
837 void
838 framescroll(Frame *f, int dl)
840 if(f != &selecttext->fr)
841 error("frameselect not right frame");
842 textframescroll(selecttext, dl);
845 void
846 textframescroll(Text *t, int dl)
848 uint q0;
850 if(dl == 0){
851 scrsleep(100);
852 return;
854 if(dl < 0){
855 q0 = textbacknl(t, t->org, -dl);
856 if(selectq > t->org+t->fr.p0)
857 textsetselect(t, t->org+t->fr.p0, selectq);
858 else
859 textsetselect(t, selectq, t->org+t->fr.p0);
860 }else{
861 if(t->org+t->fr.nchars == t->file->b.nc)
862 return;
863 q0 = t->org+frcharofpt(&t->fr, Pt(t->fr.r.min.x, t->fr.r.min.y+dl*t->fr.font->height));
864 if(selectq > t->org+t->fr.p1)
865 textsetselect(t, t->org+t->fr.p1, selectq);
866 else
867 textsetselect(t, selectq, t->org+t->fr.p1);
869 textsetorigin(t, q0, TRUE);
873 void
874 textselect(Text *t)
876 uint q0, q1;
877 int b, x, y;
878 int state, op;
880 selecttext = t;
881 /*
882 * To have double-clicking and chording, we double-click
883 * immediately if it might make sense.
884 */
885 b = mouse->buttons;
886 q0 = t->q0;
887 q1 = t->q1;
888 selectq = t->org+frcharofpt(&t->fr, mouse->xy);
889 if(clicktext==t && mouse->msec-clickmsec<500)
890 if(q0==q1 && selectq==q0){
891 textdoubleclick(t, &q0, &q1);
892 textsetselect(t, q0, q1);
893 flushimage(display, 1);
894 x = mouse->xy.x;
895 y = mouse->xy.y;
896 /* stay here until something interesting happens */
897 do
898 readmouse(mousectl);
899 while(mouse->buttons==b && abs(mouse->xy.x-x)<3 && abs(mouse->xy.y-y)<3);
900 mouse->xy.x = x; /* in case we're calling frselect */
901 mouse->xy.y = y;
902 q0 = t->q0; /* may have changed */
903 q1 = t->q1;
904 selectq = q0;
906 if(mouse->buttons == b){
907 t->fr.scroll = framescroll;
908 frselect(&t->fr, mousectl);
909 /* horrible botch: while asleep, may have lost selection altogether */
910 if(selectq > t->file->b.nc)
911 selectq = t->org + t->fr.p0;
912 t->fr.scroll = 0;
913 if(selectq < t->org)
914 q0 = selectq;
915 else
916 q0 = t->org + t->fr.p0;
917 if(selectq > t->org+t->fr.nchars)
918 q1 = selectq;
919 else
920 q1 = t->org+t->fr.p1;
922 if(q0 == q1){
923 if(q0==t->q0 && clicktext==t && mouse->msec-clickmsec<500){
924 textdoubleclick(t, &q0, &q1);
925 clicktext = nil;
926 }else{
927 clicktext = t;
928 clickmsec = mouse->msec;
930 }else
931 clicktext = nil;
932 textsetselect(t, q0, q1);
933 flushimage(display, 1);
934 state = op = 0; /* undo when possible; +1 for cut, -1 for paste */
935 while(mouse->buttons){
936 mouse->msec = 0;
937 b = mouse->buttons;
938 if(b & 6){
939 if(state==0 && op==0 && t->what==Body){
940 seq++;
941 filemark(t->w->body.file);
943 if(b & 2){
944 if(state==-1 && t->what==Body){
945 winundo(t->w, TRUE);
946 textsetselect(t, q0, t->q0);
947 state = 0;
948 }else if(state != 1 && op != -1){
949 cut(t, t, nil, TRUE, TRUE, nil, 0);
950 op = state = 1;
952 }else{
953 if(state==1 && t->what==Body){
954 winundo(t->w, TRUE);
955 textsetselect(t, q0, t->q1);
956 state = 0;
957 }else if(state != -1 && op != 1){
958 paste(t, t, nil, TRUE, FALSE, nil, 0);
959 op = state = -1;
962 textscrdraw(t);
963 clearmouse();
965 flushimage(display, 1);
966 while(mouse->buttons == b)
967 readmouse(mousectl);
968 clicktext = nil;
972 void
973 textshow(Text *t, uint q0, uint q1, int doselect)
975 int qe;
976 int nl;
977 uint q;
979 if(t->what != Body){
980 if(doselect)
981 textsetselect(t, q0, q1);
982 return;
984 if(t->w!=nil && t->fr.maxlines==0)
985 colgrow(t->col, t->w, 1);
986 if(doselect)
987 textsetselect(t, q0, q1);
988 qe = t->org+t->fr.nchars;
989 if(t->org<=q0 && (q0<qe || (q0==qe && qe==t->file->b.nc+t->ncache)))
990 textscrdraw(t);
991 else{
992 if(t->w->nopen[QWevent] > 0)
993 nl = 3*t->fr.maxlines/4;
994 else
995 nl = t->fr.maxlines/4;
996 q = textbacknl(t, q0, nl);
997 /* avoid going backwards if trying to go forwards - long lines! */
998 if(!(q0>t->org && q<t->org))
999 textsetorigin(t, q, TRUE);
1000 while(q0 > t->org+t->fr.nchars)
1001 textsetorigin(t, t->org+1, FALSE);
1005 static
1006 int
1007 region(int a, int b)
1009 if(a < b)
1010 return -1;
1011 if(a == b)
1012 return 0;
1013 return 1;
1016 void
1017 selrestore(Frame *f, Point pt0, uint p0, uint p1)
1019 if(p1<=f->p0 || p0>=f->p1){
1020 /* no overlap */
1021 frdrawsel0(f, pt0, p0, p1, f->cols[BACK], f->cols[TEXT]);
1022 return;
1024 if(p0>=f->p0 && p1<=f->p1){
1025 /* entirely inside */
1026 frdrawsel0(f, pt0, p0, p1, f->cols[HIGH], f->cols[HTEXT]);
1027 return;
1030 /* they now are known to overlap */
1032 /* before selection */
1033 if(p0 < f->p0){
1034 frdrawsel0(f, pt0, p0, f->p0, f->cols[BACK], f->cols[TEXT]);
1035 p0 = f->p0;
1036 pt0 = frptofchar(f, p0);
1038 /* after selection */
1039 if(p1 > f->p1){
1040 frdrawsel0(f, frptofchar(f, f->p1), f->p1, p1, f->cols[BACK], f->cols[TEXT]);
1041 p1 = f->p1;
1043 /* inside selection */
1044 frdrawsel0(f, pt0, p0, p1, f->cols[HIGH], f->cols[HTEXT]);
1047 void
1048 textsetselect(Text *t, uint q0, uint q1)
1050 int p0, p1;
1052 /* t->fr.p0 and t->fr.p1 are always right; t->q0 and t->q1 may be off */
1053 t->q0 = q0;
1054 t->q1 = q1;
1055 /* compute desired p0,p1 from q0,q1 */
1056 p0 = q0-t->org;
1057 p1 = q1-t->org;
1058 if(p0 < 0)
1059 p0 = 0;
1060 if(p1 < 0)
1061 p1 = 0;
1062 if(p0 > t->fr.nchars)
1063 p0 = t->fr.nchars;
1064 if(p1 > t->fr.nchars)
1065 p1 = t->fr.nchars;
1066 if(p0==t->fr.p0 && p1==t->fr.p1)
1067 return;
1068 /* screen disagrees with desired selection */
1069 if(t->fr.p1<=p0 || p1<=t->fr.p0 || p0==p1 || t->fr.p1==t->fr.p0){
1070 /* no overlap or too easy to bother trying */
1071 frdrawsel(&t->fr, frptofchar(&t->fr, t->fr.p0), t->fr.p0, t->fr.p1, 0);
1072 frdrawsel(&t->fr, frptofchar(&t->fr, p0), p0, p1, 1);
1073 goto Return;
1075 /* overlap; avoid unnecessary painting */
1076 if(p0 < t->fr.p0){
1077 /* extend selection backwards */
1078 frdrawsel(&t->fr, frptofchar(&t->fr, p0), p0, t->fr.p0, 1);
1079 }else if(p0 > t->fr.p0){
1080 /* trim first part of selection */
1081 frdrawsel(&t->fr, frptofchar(&t->fr, t->fr.p0), t->fr.p0, p0, 0);
1083 if(p1 > t->fr.p1){
1084 /* extend selection forwards */
1085 frdrawsel(&t->fr, frptofchar(&t->fr, t->fr.p1), t->fr.p1, p1, 1);
1086 }else if(p1 < t->fr.p1){
1087 /* trim last part of selection */
1088 frdrawsel(&t->fr, frptofchar(&t->fr, p1), p1, t->fr.p1, 0);
1091 Return:
1092 t->fr.p0 = p0;
1093 t->fr.p1 = p1;
1097 * Release the button in less than DELAY ms and it's considered a null selection
1098 * if the mouse hardly moved, regardless of whether it crossed a char boundary.
1100 enum {
1101 DELAY = 2,
1102 MINMOVE = 4,
1105 uint
1106 xselect(Frame *f, Mousectl *mc, Image *col, uint *p1p) /* when called, button is down */
1108 uint p0, p1, q, tmp;
1109 ulong msec;
1110 Point mp, pt0, pt1, qt;
1111 int reg, b;
1113 mp = mc->m.xy;
1114 b = mc->m.buttons;
1115 msec = mc->m.msec;
1117 /* remove tick */
1118 if(f->p0 == f->p1)
1119 frtick(f, frptofchar(f, f->p0), 0);
1120 p0 = p1 = frcharofpt(f, mp);
1121 pt0 = frptofchar(f, p0);
1122 pt1 = frptofchar(f, p1);
1123 reg = 0;
1124 frtick(f, pt0, 1);
1125 do{
1126 q = frcharofpt(f, mc->m.xy);
1127 if(p1 != q){
1128 if(p0 == p1)
1129 frtick(f, pt0, 0);
1130 if(reg != region(q, p0)){ /* crossed starting point; reset */
1131 if(reg > 0)
1132 selrestore(f, pt0, p0, p1);
1133 else if(reg < 0)
1134 selrestore(f, pt1, p1, p0);
1135 p1 = p0;
1136 pt1 = pt0;
1137 reg = region(q, p0);
1138 if(reg == 0)
1139 frdrawsel0(f, pt0, p0, p1, col, display->white);
1141 qt = frptofchar(f, q);
1142 if(reg > 0){
1143 if(q > p1)
1144 frdrawsel0(f, pt1, p1, q, col, display->white);
1146 else if(q < p1)
1147 selrestore(f, qt, q, p1);
1148 }else if(reg < 0){
1149 if(q > p1)
1150 selrestore(f, pt1, p1, q);
1151 else
1152 frdrawsel0(f, qt, q, p1, col, display->white);
1154 p1 = q;
1155 pt1 = qt;
1157 if(p0 == p1)
1158 frtick(f, pt0, 1);
1159 flushimage(f->display, 1);
1160 readmouse(mc);
1161 }while(mc->m.buttons == b);
1162 if(mc->m.msec-msec < DELAY && p0!=p1
1163 && abs(mp.x-mc->m.xy.x)<MINMOVE
1164 && abs(mp.y-mc->m.xy.y)<MINMOVE) {
1165 if(reg > 0)
1166 selrestore(f, pt0, p0, p1);
1167 else if(reg < 0)
1168 selrestore(f, pt1, p1, p0);
1169 p1 = p0;
1171 if(p1 < p0){
1172 tmp = p0;
1173 p0 = p1;
1174 p1 = tmp;
1176 pt0 = frptofchar(f, p0);
1177 if(p0 == p1)
1178 frtick(f, pt0, 0);
1179 selrestore(f, pt0, p0, p1);
1180 /* restore tick */
1181 if(f->p0 == f->p1)
1182 frtick(f, frptofchar(f, f->p0), 1);
1183 flushimage(f->display, 1);
1184 *p1p = p1;
1185 return p0;
1188 int
1189 textselect23(Text *t, uint *q0, uint *q1, Image *high, int mask)
1191 uint p0, p1;
1192 int buts;
1194 p0 = xselect(&t->fr, mousectl, high, &p1);
1195 buts = mousectl->m.buttons;
1196 if((buts & mask) == 0){
1197 *q0 = p0+t->org;
1198 *q1 = p1+t->org;
1201 while(mousectl->m.buttons)
1202 readmouse(mousectl);
1203 return buts;
1206 int
1207 textselect2(Text *t, uint *q0, uint *q1, Text **tp)
1209 int buts;
1211 *tp = nil;
1212 buts = textselect23(t, q0, q1, but2col, 4);
1213 if(buts & 4)
1214 return 0;
1215 if(buts & 1){ /* pick up argument */
1216 *tp = argtext;
1217 return 1;
1219 return 1;
1222 int
1223 textselect3(Text *t, uint *q0, uint *q1)
1225 int h;
1227 h = (textselect23(t, q0, q1, but3col, 1|2) == 0);
1228 return h;
1231 static Rune left1[] = { '{', '[', '(', '<', 0xab, 0 };
1232 static Rune right1[] = { '}', ']', ')', '>', 0xbb, 0 };
1233 static Rune left2[] = { '\n', 0 };
1234 static Rune left3[] = { '\'', '"', '`', 0 };
1236 static
1237 Rune *left[] = {
1238 left1,
1239 left2,
1240 left3,
1241 nil
1243 static
1244 Rune *right[] = {
1245 right1,
1246 left2,
1247 left3,
1248 nil
1251 void
1252 textdoubleclick(Text *t, uint *q0, uint *q1)
1254 int c, i;
1255 Rune *r, *l, *p;
1256 uint q;
1258 for(i=0; left[i]!=nil; i++){
1259 q = *q0;
1260 l = left[i];
1261 r = right[i];
1262 /* try matching character to left, looking right */
1263 if(q == 0)
1264 c = '\n';
1265 else
1266 c = textreadc(t, q-1);
1267 p = runestrchr(l, c);
1268 if(p != nil){
1269 if(textclickmatch(t, c, r[p-l], 1, &q))
1270 *q1 = q-(c!='\n');
1271 return;
1273 /* try matching character to right, looking left */
1274 if(q == t->file->b.nc)
1275 c = '\n';
1276 else
1277 c = textreadc(t, q);
1278 p = runestrchr(r, c);
1279 if(p != nil){
1280 if(textclickmatch(t, c, l[p-r], -1, &q)){
1281 *q1 = *q0+(*q0<t->file->b.nc && c=='\n');
1282 *q0 = q;
1283 if(c!='\n' || q!=0 || textreadc(t, 0)=='\n')
1284 (*q0)++;
1286 return;
1289 /* try filling out word to right */
1290 while(*q1<t->file->b.nc && isalnum(textreadc(t, *q1)))
1291 (*q1)++;
1292 /* try filling out word to left */
1293 while(*q0>0 && isalnum(textreadc(t, *q0-1)))
1294 (*q0)--;
1297 int
1298 textclickmatch(Text *t, int cl, int cr, int dir, uint *q)
1300 Rune c;
1301 int nest;
1303 nest = 1;
1304 for(;;){
1305 if(dir > 0){
1306 if(*q == t->file->b.nc)
1307 break;
1308 c = textreadc(t, *q);
1309 (*q)++;
1310 }else{
1311 if(*q == 0)
1312 break;
1313 (*q)--;
1314 c = textreadc(t, *q);
1316 if(c == cr){
1317 if(--nest==0)
1318 return 1;
1319 }else if(c == cl)
1320 nest++;
1322 return cl=='\n' && nest==1;
1325 uint
1326 textbacknl(Text *t, uint p, uint n)
1328 int i, j;
1330 /* look for start of this line if n==0 */
1331 if(n==0 && p>0 && textreadc(t, p-1)!='\n')
1332 n = 1;
1333 i = n;
1334 while(i-->0 && p>0){
1335 --p; /* it's at a newline now; back over it */
1336 if(p == 0)
1337 break;
1338 /* at 128 chars, call it a line anyway */
1339 for(j=128; --j>0 && p>0; p--)
1340 if(textreadc(t, p-1)=='\n')
1341 break;
1343 return p;
1346 void
1347 textsetorigin(Text *t, uint org, int exact)
1349 int i, a, fixup;
1350 Rune *r;
1351 uint n;
1353 if(org>0 && !exact){
1354 /* org is an estimate of the char posn; find a newline */
1355 /* don't try harder than 256 chars */
1356 for(i=0; i<256 && org<t->file->b.nc; i++){
1357 if(textreadc(t, org) == '\n'){
1358 org++;
1359 break;
1361 org++;
1364 a = org-t->org;
1365 fixup = 0;
1366 if(a>=0 && a<t->fr.nchars){
1367 frdelete(&t->fr, 0, a);
1368 fixup = 1; /* frdelete can leave end of last line in wrong selection mode; it doesn't know what follows */
1370 else if(a<0 && -a<t->fr.nchars){
1371 n = t->org - org;
1372 r = runemalloc(n);
1373 bufread(&t->file->b, org, r, n);
1374 frinsert(&t->fr, r, r+n, 0);
1375 free(r);
1376 }else
1377 frdelete(&t->fr, 0, t->fr.nchars);
1378 t->org = org;
1379 textfill(t);
1380 textscrdraw(t);
1381 textsetselect(t, t->q0, t->q1);
1382 if(fixup && t->fr.p1 > t->fr.p0)
1383 frdrawsel(&t->fr, frptofchar(&t->fr, t->fr.p1-1), t->fr.p1-1, t->fr.p1, 1);
1386 void
1387 textreset(Text *t)
1389 t->file->seq = 0;
1390 t->eq0 = ~0;
1391 /* do t->delete(0, t->nc, TRUE) without building backup stuff */
1392 textsetselect(t, t->org, t->org);
1393 frdelete(&t->fr, 0, t->fr.nchars);
1394 t->org = 0;
1395 t->q0 = 0;
1396 t->q1 = 0;
1397 filereset(t->file);
1398 bufreset(&t->file->b);