16 Image *textcols[NCOL];
17 static Rune Ldot[] = { '.', 0 };
20 TABDIR = 3 /* width of tabs in directory windows */
24 textinit(Text *t, File *f, Rectangle r, Reffont *rf, Image *cols[NCOL])
29 t->scrollr.max.x = r.min.x+Scrollwid;
31 r.min.x += Scrollwid+Scrollgap;
36 memmove(t->fr.cols, cols, sizeof t->fr.cols);
37 textredraw(t, r, rf->f, screen, -1);
41 textredraw(Text *t, Rectangle r, Font *f, Image *b, int odx)
46 frinit(&t->fr, r, f, b, t->fr.cols);
48 rr.min.x -= Scrollwid+Scrollgap; /* back fill to scroll bar */
50 draw(t->fr.b, rr, t->fr.cols[BACK], nil, ZP);
51 /* use no wider than 3-space tabs in a directory */
55 maxt = min(TABDIR, maxtab);
59 t->fr.maxtab = maxt*stringwidth(f, "0");
60 if(t->what==Body && t->w->isdir && odx!=Dx(t->all)){
61 if(t->fr.maxlines > 0){
63 textcolumnate(t, t->w->dlp, t->w->ndl);
68 textsetselect(t, t->q0, t->q1);
73 textresize(Text *t, Rectangle r, int keepextra)
80 r.max.y -= Dy(r)%t->fr.font->height;
84 t->scrollr.max.x = r.min.x+Scrollwid;
86 r.min.x += Scrollwid+Scrollgap;
88 textredraw(t, r, t->fr.font, t->fr.b, odx);
89 if(keepextra && t->fr.r.max.y < t->all.max.y && !t->fr.noredraw){
90 /* draw background in bottom fringe of window */
92 r.min.y = t->fr.r.max.y;
93 r.max.y = t->all.max.y;
94 draw(screen, r, t->fr.cols[BACK], nil, ZP);
104 filedeltext(t->file, t);
120 dircmp(const void *a, const void *b)
127 n = min(da->nr, db->nr);
128 i = memcmp(da->r, db->r, n*sizeof(Rune));
131 return da->nr - db->nr;
135 textcolumnate(Text *t, Dirlist **dlp, int ndl)
137 int i, j, w, colw, mint, maxt, ncol, nrow;
140 static Rune Lnl[] = { '\n', 0 };
141 static Rune Ltab[] = { '\t', 0 };
143 if(t->file->ntext > 1)
145 mint = stringwidth(t->fr.font, "0");
146 /* go for narrower tabs if set more than 3 wide */
147 t->fr.maxtab = min(maxtab, TABDIR)*mint;
150 for(i=0; i<ndl; i++){
153 if(maxt-w%maxt < mint || w%maxt==0)
163 ncol = max(1, Dx(t->fr.r)/colw);
164 nrow = (ndl+ncol-1)/ncol;
167 for(i=0; i<nrow; i++){
168 for(j=i; j<ndl; j+=nrow){
170 fileinsert(t->file, q1, dl->r, dl->nr);
175 if(maxt-w%maxt < mint){
176 fileinsert(t->file, q1, Ltab, 1);
181 fileinsert(t->file, q1, Ltab, 1);
186 fileinsert(t->file, q1, Lnl, 1);
192 textload(Text *t, uint q0, char *file, int setqid)
196 int fd, i, j, n, ndl, nulls;
202 if(t->ncache!=0 || t->file->b.nc || t->w==nil || t!=&t->w->body)
204 if(t->w->isdir && t->file->nname==0){
205 warning(nil, "empty directory name");
209 warning(nil, "will not open self mount point %s\n", file);
212 fd = open(file, OREAD);
214 warning(nil, "can't open %s: %r\n", file);
219 warning(nil, "can't fstat %s: %r\n", file);
223 if(d->qid.type & QTDIR){
224 /* this is checked in get() but it's possible the file changed underfoot */
225 if(t->file->ntext > 1){
226 warning(nil, "%s is a directory; can't read with multiple windows on it\n", file);
230 t->w->filemenu = FALSE;
231 if(t->file->name[t->file->nname-1] != '/'){
232 rp = runemalloc(t->file->nname+1);
233 runemove(rp, t->file->name, t->file->nname);
234 rp[t->file->nname] = '/';
235 winsetname(t->w, rp, t->file->nname+1);
241 while((n=dirread(fd, &dbuf)) > 0){
243 dl = emalloc(sizeof(Dirlist));
244 j = strlen(dbuf[i].name);
245 tmp = emalloc(j+1+1);
246 memmove(tmp, dbuf[i].name, j);
247 if(dbuf[i].qid.type & QTDIR)
250 dl->r = bytetorune(tmp, &dl->nr);
251 dl->wid = stringwidth(t->fr.font, tmp);
254 dlp = realloc(dlp, ndl*sizeof(Dirlist*));
259 qsort(dlp, ndl, sizeof(Dirlist*), dircmp);
262 textcolumnate(t, dlp, ndl);
266 t->w->filemenu = TRUE;
267 q1 = q0 + fileload(t->file, q0, fd, &nulls);
270 t->file->dev = d->dev;
271 t->file->mtime = d->mtime;
272 t->file->qidpath = d->qid.path;
276 for(q=q0; q<q1; q+=n){
280 bufread(&t->file->b, q, rp, n);
283 else if(q <= t->org+t->fr.nchars)
284 frinsert(&t->fr, rp, rp+n, q-t->org);
285 if(t->fr.lastlinefull)
289 for(i=0; i<t->file->ntext; i++){
290 u = t->file->text[i];
292 if(u->org > u->file->b.nc) /* will be 0 because of reset(), but safety first */
294 textresize(u, u->all, TRUE);
295 textbacknl(u, u->org, 0); /* go to beginning of line */
297 textsetselect(u, q0, q0);
300 warning(nil, "%s: NUL bytes elided\n", file);
310 textbsinsert(Text *t, uint q0, Rune *r, uint n, int tofile, int *nrp)
315 if(t->what == Tag){ /* can't happen but safety first: mustn't backspace over file name */
317 textinsert(t, q0, r, n, tofile);
343 textdelete(t, q0, q0+initial, tofile);
346 textinsert(t, q0, tp, n, tofile);
355 textinsert(Text *t, uint q0, Rune *r, uint n, int tofile)
360 if(tofile && t->ncache != 0)
361 error("text.insert");
365 fileinsert(t->file, q0, r, n);
368 t->w->utflastqid = -1;
370 if(t->file->ntext > 1)
371 for(i=0; i<t->file->ntext; i++){
372 u = t->file->text[i];
374 u->w->dirty = TRUE; /* always a body */
375 textinsert(u, q0, r, n, FALSE);
376 textsetselect(u, u->q0, u->q1);
390 else if(q0 <= t->org+t->fr.nchars)
391 frinsert(&t->fr, r, r+n, q0-t->org);
397 winevent(t->w, "%c%d %d 0 %d %.*S\n", c, q0, q0+n, n, n, r);
399 winevent(t->w, "%c%d %d 0 0 \n", c, q0, q0+n, n);
418 if(t->fr.lastlinefull || t->nofill)
424 n = t->file->b.nc-(t->org+t->fr.nchars);
427 if(n > 2000) /* educated guess at reasonable amount */
429 bufread(&t->file->b, t->org+t->fr.nchars, rp, n);
431 * it's expensive to frinsert more than we need, so
434 nl = t->fr.maxlines-t->fr.nlines;
443 frinsert(&t->fr, rp, rp+i, t->fr.nchars);
444 }while(t->fr.lastlinefull == FALSE);
449 textdelete(Text *t, uint q0, uint q1, int tofile)
455 if(tofile && t->ncache != 0)
456 error("text.delete");
461 filedelete(t->file, q0, q1);
464 t->w->utflastqid = -1;
466 if(t->file->ntext > 1)
467 for(i=0; i<t->file->ntext; i++){
468 u = t->file->text[i];
470 u->w->dirty = TRUE; /* always a body */
471 textdelete(u, q0, q1, FALSE);
472 textsetselect(u, u->q0, u->q1);
478 t->iq1 -= min(n, t->iq1-q0);
480 t->q0 -= min(n, t->q0-q0);
482 t->q1 -= min(n, t->q1-q0);
485 else if(q0 < t->org+t->fr.nchars){
487 if(p1 > t->fr.nchars)
494 frdelete(&t->fr, p0, p1);
501 winevent(t->w, "%c%d %d 0 0 \n", c, q0, q1);
506 textconstrain(Text *t, uint q0, uint q1, uint *p0, uint *p1)
508 *p0 = min(q0, t->file->b.nc);
509 *p1 = min(q1, t->file->b.nc);
513 textreadc(Text *t, uint q)
517 if(t->cq0<=q && q<t->cq0+t->ncache)
518 r = t->cache[q-t->cq0];
520 bufread(&t->file->b, q, &r, 1);
525 textbswidth(Text *t, Rune c)
531 /* there is known to be at least one character to erase */
532 if(c == 0x08) /* ^H: erase character */
537 r = textreadc(t, q-1);
538 if(r == '\n'){ /* eat at most one more character */
539 if(q == t->q0) /* eat the newline */
545 if(eq && skipping) /* found one; stop skipping */
547 else if(!eq && !skipping)
556 textfilewidth(Text *t, uint q0, int oneelement)
563 r = textreadc(t, q-1);
566 if(oneelement && r=='/')
574 textcomplete(Text *t)
585 /* control-f: filename completion; works back to white space or / */
586 if(t->q0<t->file->b.nc && textreadc(t, t->q0)>' ') /* must be at end of word */
588 nstr = textfilewidth(t, t->q0, TRUE);
589 str = runemalloc(nstr);
590 npath = textfilewidth(t, t->q0-nstr, FALSE);
591 path = runemalloc(npath);
598 for(i=0; i<nstr; i++)
599 str[i] = textreadc(t, q++);
600 q = t->q0-nstr-npath;
601 for(i=0; i<npath; i++)
602 path[i] = textreadc(t, q++);
603 /* is path rooted? if not, we need to make it relative to window path */
604 if(npath>0 && path[0]=='/')
605 dir = runestr(path, npath);
607 dir = dirname(t, nil, 0);
608 if(dir.nr + 1 + npath > nelem(tmp)){
614 dir.r = runestrdup(Ldot);
616 runemove(tmp, dir.r, dir.nr);
618 runemove(tmp+dir.nr+1, path, npath);
622 dir = cleanrname(dir);
625 s = smprint("%.*S", nstr, str);
626 dirs = smprint("%.*S", dir.nr, dir.r);
627 c = complete(dirs, s);
630 warning(nil, "error attempting completion: %r\n");
635 warning(nil, "%.*S%s%.*S*%s\n",
637 dir.nr>0 && dir.r[dir.nr-1]!='/' ? "/" : "",
639 c->nmatch ? "" : ": no matches in:");
640 for(i=0; i<c->nfile; i++)
641 warning(nil, " %s\n", c->filename[i]);
645 rp = runesmprint("%s", c->string);
657 texttype(Text *t, Rune r)
665 if(t->what!=Body && t->what!=Tag && r=='\n')
668 t->w->tagsafe = FALSE;
676 textshow(t, t->q0-1, t->q0-1, TRUE);
680 if(t->q1 < t->file->b.nc)
681 textshow(t, t->q1+1, t->q1+1, TRUE);
686 n = t->fr.maxlines/3;
691 n = mousescrollsize(t->fr.maxlines);
696 n = 2*t->fr.maxlines/3;
698 q0 = t->org+frcharofpt(&t->fr, Pt(t->fr.r.min.x, t->fr.r.min.y+n*t->fr.font->height));
699 textsetorigin(t, q0, TRUE);
704 n = t->fr.maxlines/3;
709 n = mousescrollsize(t->fr.maxlines);
712 n = 2*t->fr.maxlines/3;
714 q0 = textbacknl(t, t->org, n);
715 textsetorigin(t, q0, TRUE);
719 if(t->org > t->iq1) {
720 q0 = textbacknl(t, t->iq1, 1);
721 textsetorigin(t, q0, TRUE);
723 textshow(t, 0, 0, FALSE);
727 if(t->iq1 > t->org+t->fr.nchars) {
728 q0 = textbacknl(t, t->iq1, 1);
729 textsetorigin(t, q0, TRUE);
731 textshow(t, t->file->b.nc, t->file->b.nc, FALSE);
733 case 0x01: /* ^A: beginning of line */
735 /* go to where ^U would erase, if not already at BOL */
737 if(t->q0>0 && textreadc(t, t->q0-1)!='\n')
738 nnb = textbswidth(t, 0x15);
739 textshow(t, t->q0-nnb, t->q0-nnb, TRUE);
741 case 0x05: /* ^E: end of line */
744 while(q0<t->file->b.nc && textreadc(t, q0)!='\n')
746 textshow(t, q0, q0, TRUE);
748 case Kcmd+'c': /* %C: copy */
750 cut(t, t, nil, TRUE, FALSE, nil, 0);
752 case Kcmd+'z': /* %Z: undo */
754 undo(t, nil, nil, TRUE, 0, nil, 0);
758 /* expand tag to show all text */
759 if(!t->w->tagexpand){
760 t->w->tagexpand = TRUE;
761 winresize(t->w, t->w->r, FALSE, TRUE);
766 /* shrink tag to single line */
768 t->w->tagexpand = FALSE;
770 winresize(t->w, t->w->r, FALSE, TRUE);
778 /* cut/paste must be done after the seq++/filemark */
780 case Kcmd+'x': /* %X: cut */
786 cut(t, t, nil, TRUE, TRUE, nil, 0);
787 textshow(t, t->q0, t->q0, 1);
790 case Kcmd+'v': /* %V: paste */
796 paste(t, t, nil, TRUE, FALSE, nil, 0);
797 textshow(t, t->q0, t->q1, 1);
804 cut(t, t, nil, TRUE, TRUE, nil, 0);
807 textshow(t, t->q0, t->q0, 1);
809 case 0x06: /* ^F: complete */
811 rp = textcomplete(t);
815 break; /* fall through to normal insertion case */
818 textsetselect(t, t->eq0, t->q0);
823 case 0x08: /* ^H: erase character */
824 case 0x15: /* ^U: erase line */
825 case 0x17: /* ^W: erase word */
826 if(t->q0 == 0) /* nothing to erase */
828 nnb = textbswidth(t, r);
831 /* if selection is at beginning of window, avoid deleting invisible text */
838 for(i=0; i<t->file->ntext; i++){
839 u = t->file->text[i];
845 error("text.type backspace");
849 textdelete(u, q1-n, q1, FALSE);
852 if(u->eq0==q1 || u->eq0==~0)
855 textdelete(u, q0, q0+nb, TRUE);
857 textsetselect(u, u->q0, u->q1);
859 textsetselect(t, q0, q0);
862 for(i=0; i<t->file->ntext; i++)
863 textfill(t->file->text[i]);
867 if(t->w->autoindent){
868 /* find beginning of previous line using backspace code */
869 nnb = textbswidth(t, 0x15); /* ^U case */
870 rp = runemalloc(nnb + 1);
873 for(i=0; i<nnb; i++){
874 r = textreadc(t, t->q0-nnb+i);
875 if(r != ' ' && r != '\t')
880 break; /* fall through to normal code */
882 /* otherwise ordinary character; just insert, typically in caches of all texts */
883 for(i=0; i<t->file->ntext; i++){
884 u = t->file->text[i];
889 else if(t->q0 != u->cq0+u->ncache)
890 error("text.type cq1");
892 * Change the tag before we add to ncache,
893 * so that if the window body is resized the
894 * commit will not find anything in ncache.
896 if(u->what==Body && u->ncache == 0){
901 textinsert(u, t->q0, rp, nr, FALSE);
903 textsetselect(u, u->q0, u->q1);
904 if(u->ncache+nr > u->ncachealloc){
905 u->ncachealloc += 10 + nr;
906 u->cache = runerealloc(u->cache, u->ncachealloc);
908 runemove(u->cache+u->ncache, rp, nr);
913 textsetselect(t, t->q0+nr, t->q0+nr);
914 if(r=='\n' && t->w!=nil)
920 textcommit(Text *t, int tofile)
925 fileinsert(t->file, t->cq0, t->cache, t->ncache);
928 t->w->utflastqid = -1;
933 static Text *clicktext;
934 static uint clickmsec;
935 static Text *selecttext;
939 * called from frame library
942 framescroll(Frame *f, int dl)
944 if(f != &selecttext->fr)
945 error("frameselect not right frame");
946 textframescroll(selecttext, dl);
950 textframescroll(Text *t, int dl)
959 q0 = textbacknl(t, t->org, -dl);
960 if(selectq > t->org+t->fr.p0)
961 textsetselect(t, t->org+t->fr.p0, selectq);
963 textsetselect(t, selectq, t->org+t->fr.p0);
965 if(t->org+t->fr.nchars == t->file->b.nc)
967 q0 = t->org+frcharofpt(&t->fr, Pt(t->fr.r.min.x, t->fr.r.min.y+dl*t->fr.font->height));
968 if(selectq > t->org+t->fr.p1)
969 textsetselect(t, t->org+t->fr.p1, selectq);
971 textsetselect(t, selectq, t->org+t->fr.p1);
973 textsetorigin(t, q0, TRUE);
983 enum { None, Cut, Paste };
987 * To have double-clicking and chording, we double-click
988 * immediately if it might make sense.
993 selectq = t->org+frcharofpt(&t->fr, mouse->xy);
994 if(clicktext==t && mouse->msec-clickmsec<500)
995 if(q0==q1 && selectq==q0){
996 textdoubleclick(t, &q0, &q1);
997 textsetselect(t, q0, q1);
998 flushimage(display, 1);
1001 /* stay here until something interesting happens */
1003 readmouse(mousectl);
1004 while(mouse->buttons==b && abs(mouse->xy.x-x)<3 && abs(mouse->xy.y-y)<3);
1005 mouse->xy.x = x; /* in case we're calling frselect */
1007 q0 = t->q0; /* may have changed */
1011 if(mouse->buttons == b){
1012 t->fr.scroll = framescroll;
1013 frselect(&t->fr, mousectl);
1014 /* horrible botch: while asleep, may have lost selection altogether */
1015 if(selectq > t->file->b.nc)
1016 selectq = t->org + t->fr.p0;
1018 if(selectq < t->org)
1021 q0 = t->org + t->fr.p0;
1022 if(selectq > t->org+t->fr.nchars)
1025 q1 = t->org+t->fr.p1;
1028 if(q0==t->q0 && clicktext==t && mouse->msec-clickmsec<500){
1029 textdoubleclick(t, &q0, &q1);
1033 clickmsec = mouse->msec;
1037 textsetselect(t, q0, q1);
1038 flushimage(display, 1);
1039 state = None; /* what we've done; undo when possible */
1040 while(mouse->buttons){
1044 if(state==None && t->what==Body){
1046 filemark(t->w->body.file);
1049 if(state==Paste && t->what==Body){
1050 winundo(t->w, TRUE);
1051 textsetselect(t, q0, t->q1);
1053 }else if(state != Cut){
1054 cut(t, t, nil, TRUE, TRUE, nil, 0);
1058 if(state==Cut && t->what==Body){
1059 winundo(t->w, TRUE);
1060 textsetselect(t, q0, t->q1);
1062 }else if(state != Paste){
1063 paste(t, t, nil, TRUE, FALSE, nil, 0);
1070 flushimage(display, 1);
1071 while(mouse->buttons == b)
1072 readmouse(mousectl);
1078 textshow(Text *t, uint q0, uint q1, int doselect)
1084 if(t->what != Body){
1086 textsetselect(t, q0, q1);
1089 if(t->w!=nil && t->fr.maxlines==0)
1090 colgrow(t->col, t->w, 1);
1092 textsetselect(t, q0, q1);
1093 qe = t->org+t->fr.nchars;
1094 if(t->org<=q0 && (q0<qe || (q0==qe && qe==t->file->b.nc+t->ncache)))
1097 if(t->w->nopen[QWevent] > 0)
1098 nl = 3*t->fr.maxlines/4;
1100 nl = t->fr.maxlines/4;
1101 q = textbacknl(t, q0, nl);
1102 /* avoid going backwards if trying to go forwards - long lines! */
1103 if(!(q0>t->org && q<t->org))
1104 textsetorigin(t, q, TRUE);
1105 while(q0 > t->org+t->fr.nchars)
1106 textsetorigin(t, t->org+1, FALSE);
1112 region(int a, int b)
1122 selrestore(Frame *f, Point pt0, uint p0, uint p1)
1124 if(p1<=f->p0 || p0>=f->p1){
1126 frdrawsel0(f, pt0, p0, p1, f->cols[BACK], f->cols[TEXT]);
1129 if(p0>=f->p0 && p1<=f->p1){
1130 /* entirely inside */
1131 frdrawsel0(f, pt0, p0, p1, f->cols[HIGH], f->cols[HTEXT]);
1135 /* they now are known to overlap */
1137 /* before selection */
1139 frdrawsel0(f, pt0, p0, f->p0, f->cols[BACK], f->cols[TEXT]);
1141 pt0 = frptofchar(f, p0);
1143 /* after selection */
1145 frdrawsel0(f, frptofchar(f, f->p1), f->p1, p1, f->cols[BACK], f->cols[TEXT]);
1148 /* inside selection */
1149 frdrawsel0(f, pt0, p0, p1, f->cols[HIGH], f->cols[HTEXT]);
1153 textsetselect(Text *t, uint q0, uint q1)
1157 /* t->fr.p0 and t->fr.p1 are always right; t->q0 and t->q1 may be off */
1160 /* compute desired p0,p1 from q0,q1 */
1170 if(p0 > t->fr.nchars)
1172 if(p1 > t->fr.nchars){
1176 if(p0==t->fr.p0 && p1==t->fr.p1){
1177 if(p0 == p1 && ticked != t->fr.ticked)
1178 frtick(&t->fr, frptofchar(&t->fr, p0), ticked);
1181 /* screen disagrees with desired selection */
1182 if(t->fr.p1<=p0 || p1<=t->fr.p0 || p0==p1 || t->fr.p1==t->fr.p0){
1183 /* no overlap or too easy to bother trying */
1184 frdrawsel(&t->fr, frptofchar(&t->fr, t->fr.p0), t->fr.p0, t->fr.p1, 0);
1185 if(p0 != p1 || ticked)
1186 frdrawsel(&t->fr, frptofchar(&t->fr, p0), p0, p1, 1);
1189 /* overlap; avoid unnecessary painting */
1191 /* extend selection backwards */
1192 frdrawsel(&t->fr, frptofchar(&t->fr, p0), p0, t->fr.p0, 1);
1193 }else if(p0 > t->fr.p0){
1194 /* trim first part of selection */
1195 frdrawsel(&t->fr, frptofchar(&t->fr, t->fr.p0), t->fr.p0, p0, 0);
1198 /* extend selection forwards */
1199 frdrawsel(&t->fr, frptofchar(&t->fr, t->fr.p1), t->fr.p1, p1, 1);
1200 }else if(p1 < t->fr.p1){
1201 /* trim last part of selection */
1202 frdrawsel(&t->fr, frptofchar(&t->fr, p1), p1, t->fr.p1, 0);
1211 * Release the button in less than DELAY ms and it's considered a null selection
1212 * if the mouse hardly moved, regardless of whether it crossed a char boundary.
1220 xselect(Frame *f, Mousectl *mc, Image *col, uint *p1p) /* when called, button is down */
1222 uint p0, p1, q, tmp;
1224 Point mp, pt0, pt1, qt;
1233 frtick(f, frptofchar(f, f->p0), 0);
1234 p0 = p1 = frcharofpt(f, mp);
1235 pt0 = frptofchar(f, p0);
1236 pt1 = frptofchar(f, p1);
1240 q = frcharofpt(f, mc->m.xy);
1244 if(reg != region(q, p0)){ /* crossed starting point; reset */
1246 selrestore(f, pt0, p0, p1);
1248 selrestore(f, pt1, p1, p0);
1251 reg = region(q, p0);
1253 frdrawsel0(f, pt0, p0, p1, col, display->white);
1255 qt = frptofchar(f, q);
1258 frdrawsel0(f, pt1, p1, q, col, display->white);
1261 selrestore(f, qt, q, p1);
1264 selrestore(f, pt1, p1, q);
1266 frdrawsel0(f, qt, q, p1, col, display->white);
1273 flushimage(f->display, 1);
1275 }while(mc->m.buttons == b);
1276 if(mc->m.msec-msec < DELAY && p0!=p1
1277 && abs(mp.x-mc->m.xy.x)<MINMOVE
1278 && abs(mp.y-mc->m.xy.y)<MINMOVE) {
1280 selrestore(f, pt0, p0, p1);
1282 selrestore(f, pt1, p1, p0);
1290 pt0 = frptofchar(f, p0);
1293 selrestore(f, pt0, p0, p1);
1296 frtick(f, frptofchar(f, f->p0), 1);
1297 flushimage(f->display, 1);
1303 textselect23(Text *t, uint *q0, uint *q1, Image *high, int mask)
1308 p0 = xselect(&t->fr, mousectl, high, &p1);
1309 buts = mousectl->m.buttons;
1310 if((buts & mask) == 0){
1315 while(mousectl->m.buttons)
1316 readmouse(mousectl);
1321 textselect2(Text *t, uint *q0, uint *q1, Text **tp)
1326 buts = textselect23(t, q0, q1, but2col, 4);
1329 if(buts & 1){ /* pick up argument */
1337 textselect3(Text *t, uint *q0, uint *q1)
1341 h = (textselect23(t, q0, q1, but3col, 1|2) == 0);
1345 static Rune left1[] = { '{', '[', '(', '<', 0xab, 0 };
1346 static Rune right1[] = { '}', ']', ')', '>', 0xbb, 0 };
1347 static Rune left2[] = { '\n', 0 };
1348 static Rune left3[] = { '\'', '"', '`', 0 };
1366 textdoubleclick(Text *t, uint *q0, uint *q1)
1372 if(textclickhtmlmatch(t, q0, q1))
1375 for(i=0; left[i]!=nil; i++){
1379 /* try matching character to left, looking right */
1383 c = textreadc(t, q-1);
1384 p = runestrchr(l, c);
1386 if(textclickmatch(t, c, r[p-l], 1, &q))
1390 /* try matching character to right, looking left */
1391 if(q == t->file->b.nc)
1394 c = textreadc(t, q);
1395 p = runestrchr(r, c);
1397 if(textclickmatch(t, c, l[p-r], -1, &q)){
1398 *q1 = *q0+(*q0<t->file->b.nc && c=='\n');
1400 if(c!='\n' || q!=0 || textreadc(t, 0)=='\n')
1407 /* try filling out word to right */
1408 while(*q1<t->file->b.nc && isalnum(textreadc(t, *q1)))
1410 /* try filling out word to left */
1411 while(*q0>0 && isalnum(textreadc(t, *q0-1)))
1416 textclickmatch(Text *t, int cl, int cr, int dir, uint *q)
1424 if(*q == t->file->b.nc)
1426 c = textreadc(t, *q);
1432 c = textreadc(t, *q);
1440 return cl=='\n' && nest==1;
1443 // Is the text starting at location q an html tag?
1444 // Return 1 for <a>, -1 for </a>, 0 for no tag or <a />.
1445 // Set *q1, if non-nil, to the location after the tag.
1447 ishtmlstart(Text *t, uint q, uint *q1)
1451 if(q+2 > t->file->b.nc)
1453 if(textreadc(t, q++) != '<')
1455 c = textreadc(t, q++);
1459 if(q >= t->file->b.nc)
1462 c = textreadc(t, q++);
1466 if(c1 == '/') // closing tag
1468 if(c2 == '/' || c2 == '!') // open + close tag or comment
1473 // Is the text ending at location q an html tag?
1474 // Return 1 for <a>, -1 for </a>, 0 for no tag or <a />.
1475 // Set *q0, if non-nil, to the start of the tag.
1477 ishtmlend(Text *t, uint q, uint *q0)
1483 if(textreadc(t, --q) != '>')
1485 c = textreadc(t, --q);
1492 c = textreadc(t, --q);
1496 if(c1 == '/') // closing tag
1498 if(c2 == '/' || c2 == '!') // open + close tag or comment
1504 textclickhtmlmatch(Text *t, uint *q0, uint *q1)
1510 // after opening tag? scan forward for closing tag
1511 if(ishtmlend(t, q, nil) == 1) {
1513 while(q < t->file->b.nc) {
1514 n = ishtmlstart(t, q, &nq);
1528 // before closing tag? scan backward for opening tag
1529 if(ishtmlstart(t, q, nil) == -1) {
1532 n = ishtmlend(t, q, &nq);
1550 textbacknl(Text *t, uint p, uint n)
1554 /* look for start of this line if n==0 */
1555 if(n==0 && p>0 && textreadc(t, p-1)!='\n')
1558 while(i-->0 && p>0){
1559 --p; /* it's at a newline now; back over it */
1562 /* at 128 chars, call it a line anyway */
1563 for(j=128; --j>0 && p>0; p--)
1564 if(textreadc(t, p-1)=='\n')
1571 textsetorigin(Text *t, uint org, int exact)
1577 if(org>0 && !exact){
1578 /* org is an estimate of the char posn; find a newline */
1579 /* don't try harder than 256 chars */
1580 for(i=0; i<256 && org<t->file->b.nc; i++){
1581 if(textreadc(t, org) == '\n'){
1590 if(a>=0 && a<t->fr.nchars){
1591 frdelete(&t->fr, 0, a);
1592 fixup = 1; /* frdelete can leave end of last line in wrong selection mode; it doesn't know what follows */
1594 else if(a<0 && -a<t->fr.nchars){
1597 bufread(&t->file->b, org, r, n);
1598 frinsert(&t->fr, r, r+n, 0);
1601 frdelete(&t->fr, 0, t->fr.nchars);
1605 textsetselect(t, t->q0, t->q1);
1606 if(fixup && t->fr.p1 > t->fr.p0)
1607 frdrawsel(&t->fr, frptofchar(&t->fr, t->fr.p1-1), t->fr.p1-1, t->fr.p1, 1);
1615 /* do t->delete(0, t->nc, TRUE) without building backup stuff */
1616 textsetselect(t, t->org, t->org);
1617 frdelete(&t->fr, 0, t->fr.nchars);
1622 bufreset(&t->file->b);