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);
388 else if(q0 <= t->org+t->fr.nchars)
389 frinsert(&t->fr, r, r+n, q0-t->org);
395 winevent(t->w, "%c%d %d 0 %d %.*S\n", c, q0, q0+n, n, n, r);
397 winevent(t->w, "%c%d %d 0 0 \n", c, q0, q0+n, n);
416 if(t->fr.lastlinefull || t->nofill)
422 n = t->file->b.nc-(t->org+t->fr.nchars);
425 if(n > 2000) /* educated guess at reasonable amount */
427 bufread(&t->file->b, t->org+t->fr.nchars, rp, n);
429 * it's expensive to frinsert more than we need, so
432 nl = t->fr.maxlines-t->fr.nlines;
441 frinsert(&t->fr, rp, rp+i, t->fr.nchars);
442 }while(t->fr.lastlinefull == FALSE);
447 textdelete(Text *t, uint q0, uint q1, int tofile)
453 if(tofile && t->ncache != 0)
454 error("text.delete");
459 filedelete(t->file, q0, q1);
462 t->w->utflastqid = -1;
464 if(t->file->ntext > 1)
465 for(i=0; i<t->file->ntext; i++){
466 u = t->file->text[i];
468 u->w->dirty = TRUE; /* always a body */
469 textdelete(u, q0, q1, FALSE);
470 textsetselect(u, u->q0, u->q1);
476 t->q0 -= min(n, t->q0-q0);
478 t->q1 -= min(n, t->q1-q0);
481 else if(q0 < t->org+t->fr.nchars){
483 if(p1 > t->fr.nchars)
490 frdelete(&t->fr, p0, p1);
497 winevent(t->w, "%c%d %d 0 0 \n", c, q0, q1);
502 textconstrain(Text *t, uint q0, uint q1, uint *p0, uint *p1)
504 *p0 = min(q0, t->file->b.nc);
505 *p1 = min(q1, t->file->b.nc);
509 textreadc(Text *t, uint q)
513 if(t->cq0<=q && q<t->cq0+t->ncache)
514 r = t->cache[q-t->cq0];
516 bufread(&t->file->b, q, &r, 1);
521 textbswidth(Text *t, Rune c)
527 /* there is known to be at least one character to erase */
528 if(c == 0x08) /* ^H: erase character */
533 r = textreadc(t, q-1);
534 if(r == '\n'){ /* eat at most one more character */
535 if(q == t->q0) /* eat the newline */
541 if(eq && skipping) /* found one; stop skipping */
543 else if(!eq && !skipping)
552 textfilewidth(Text *t, uint q0, int oneelement)
559 r = textreadc(t, q-1);
562 if(oneelement && r=='/')
570 textcomplete(Text *t)
581 /* control-f: filename completion; works back to white space or / */
582 if(t->q0<t->file->b.nc && textreadc(t, t->q0)>' ') /* must be at end of word */
584 nstr = textfilewidth(t, t->q0, TRUE);
585 str = runemalloc(nstr);
586 npath = textfilewidth(t, t->q0-nstr, FALSE);
587 path = runemalloc(npath);
594 for(i=0; i<nstr; i++)
595 str[i] = textreadc(t, q++);
596 q = t->q0-nstr-npath;
597 for(i=0; i<npath; i++)
598 path[i] = textreadc(t, q++);
599 /* is path rooted? if not, we need to make it relative to window path */
600 if(npath>0 && path[0]=='/')
601 dir = runestr(path, npath);
603 dir = dirname(t, nil, 0);
604 if(dir.nr + 1 + npath > nelem(tmp)){
610 dir.r = runestrdup(Ldot);
612 runemove(tmp, dir.r, dir.nr);
614 runemove(tmp+dir.nr+1, path, npath);
618 dir = cleanrname(dir);
621 s = smprint("%.*S", nstr, str);
622 dirs = smprint("%.*S", dir.nr, dir.r);
623 c = complete(dirs, s);
626 warning(nil, "error attempting completion: %r\n");
631 warning(nil, "%.*S%s%.*S*%s\n",
633 dir.nr>0 && dir.r[dir.nr-1]!='/' ? "/" : "",
635 c->nmatch ? "" : ": no matches in:");
636 for(i=0; i<c->nfile; i++)
637 warning(nil, " %s\n", c->filename[i]);
641 rp = runesmprint("%s", c->string);
653 texttype(Text *t, Rune r)
661 if(t->what!=Body && t->what!=Tag && r=='\n')
664 t->w->tagsafe = FALSE;
672 textshow(t, t->q0-1, t->q0-1, TRUE);
676 if(t->q1 < t->file->b.nc){
678 textshow(t, t->q1+1, t->q1+1, TRUE);
684 n = t->fr.maxlines/3;
689 n = mousescrollsize(t->fr.maxlines);
694 n = 2*t->fr.maxlines/3;
696 q0 = t->org+frcharofpt(&t->fr, Pt(t->fr.r.min.x, t->fr.r.min.y+n*t->fr.font->height));
697 textsetorigin(t, q0, TRUE);
702 n = t->fr.maxlines/3;
707 n = mousescrollsize(t->fr.maxlines);
710 n = 2*t->fr.maxlines/3;
712 q0 = textbacknl(t, t->org, n);
713 textsetorigin(t, q0, TRUE);
717 textshow(t, 0, 0, FALSE);
721 textshow(t, t->file->b.nc, t->file->b.nc, FALSE);
723 case 0x01: /* ^A: beginning of line */
725 /* go to where ^U would erase, if not already at BOL */
727 if(t->q0>0 && textreadc(t, t->q0-1)!='\n')
728 nnb = textbswidth(t, 0x15);
729 textshow(t, t->q0-nnb, t->q0-nnb, TRUE);
731 case 0x05: /* ^E: end of line */
734 while(q0<t->file->b.nc && textreadc(t, q0)!='\n')
736 textshow(t, q0, q0, TRUE);
738 case Kcmd+'c': /* %C: copy */
740 cut(t, t, nil, TRUE, FALSE, nil, 0);
744 /* expand tag to show all text */
745 if(!t->w->tagexpand){
746 t->w->tagexpand = TRUE;
747 winresize(t->w, t->w->r, FALSE, TRUE);
752 /* shrink tag to single line */
754 t->w->tagexpand = FALSE;
756 winresize(t->w, t->w->r, FALSE, TRUE);
764 /* cut/paste must be done after the seq++/filemark */
766 case Kcmd+'x': /* %X: cut */
772 cut(t, t, nil, TRUE, TRUE, nil, 0);
773 textshow(t, t->q0, t->q0, 1);
775 case Kcmd+'v': /* %V: paste */
781 paste(t, t, nil, TRUE, FALSE, nil, 0);
782 textshow(t, t->q0, t->q1, 1);
788 cut(t, t, nil, TRUE, TRUE, nil, 0);
791 textshow(t, t->q0, t->q0, 1);
793 case 0x06: /* ^F: complete */
795 rp = textcomplete(t);
799 break; /* fall through to normal insertion case */
802 textsetselect(t, t->eq0, t->q0);
806 case 0x08: /* ^H: erase character */
807 case 0x15: /* ^U: erase line */
808 case 0x17: /* ^W: erase word */
809 if(t->q0 == 0) /* nothing to erase */
811 nnb = textbswidth(t, r);
814 /* if selection is at beginning of window, avoid deleting invisible text */
821 for(i=0; i<t->file->ntext; i++){
822 u = t->file->text[i];
828 error("text.type backspace");
832 textdelete(u, q1-n, q1, FALSE);
835 if(u->eq0==q1 || u->eq0==~0)
838 textdelete(u, q0, q0+nb, TRUE);
840 textsetselect(u, u->q0, u->q1);
842 textsetselect(t, q0, q0);
845 for(i=0; i<t->file->ntext; i++)
846 textfill(t->file->text[i]);
849 if(t->w->autoindent){
850 /* find beginning of previous line using backspace code */
851 nnb = textbswidth(t, 0x15); /* ^U case */
852 rp = runemalloc(nnb + 1);
855 for(i=0; i<nnb; i++){
856 r = textreadc(t, t->q0-nnb+i);
857 if(r != ' ' && r != '\t')
862 break; /* fall through to normal code */
864 /* otherwise ordinary character; just insert, typically in caches of all texts */
865 for(i=0; i<t->file->ntext; i++){
866 u = t->file->text[i];
871 else if(t->q0 != u->cq0+u->ncache)
872 error("text.type cq1");
874 * Change the tag before we add to ncache,
875 * so that if the window body is resized the
876 * commit will not find anything in ncache.
878 if(u->what==Body && u->ncache == 0){
883 textinsert(u, t->q0, rp, nr, FALSE);
885 textsetselect(u, u->q0, u->q1);
886 if(u->ncache+nr > u->ncachealloc){
887 u->ncachealloc += 10 + nr;
888 u->cache = runerealloc(u->cache, u->ncachealloc);
890 runemove(u->cache+u->ncache, rp, nr);
895 textsetselect(t, t->q0+nr, t->q0+nr);
896 if(r=='\n' && t->w!=nil)
901 textcommit(Text *t, int tofile)
906 fileinsert(t->file, t->cq0, t->cache, t->ncache);
909 t->w->utflastqid = -1;
914 static Text *clicktext;
915 static uint clickmsec;
916 static Text *selecttext;
920 * called from frame library
923 framescroll(Frame *f, int dl)
925 if(f != &selecttext->fr)
926 error("frameselect not right frame");
927 textframescroll(selecttext, dl);
931 textframescroll(Text *t, int dl)
940 q0 = textbacknl(t, t->org, -dl);
941 if(selectq > t->org+t->fr.p0)
942 textsetselect(t, t->org+t->fr.p0, selectq);
944 textsetselect(t, selectq, t->org+t->fr.p0);
946 if(t->org+t->fr.nchars == t->file->b.nc)
948 q0 = t->org+frcharofpt(&t->fr, Pt(t->fr.r.min.x, t->fr.r.min.y+dl*t->fr.font->height));
949 if(selectq > t->org+t->fr.p1)
950 textsetselect(t, t->org+t->fr.p1, selectq);
952 textsetselect(t, selectq, t->org+t->fr.p1);
954 textsetorigin(t, q0, TRUE);
964 enum { None, Cut, Paste };
968 * To have double-clicking and chording, we double-click
969 * immediately if it might make sense.
974 selectq = t->org+frcharofpt(&t->fr, mouse->xy);
975 if(clicktext==t && mouse->msec-clickmsec<500)
976 if(q0==q1 && selectq==q0){
977 textdoubleclick(t, &q0, &q1);
978 textsetselect(t, q0, q1);
979 flushimage(display, 1);
982 /* stay here until something interesting happens */
985 while(mouse->buttons==b && abs(mouse->xy.x-x)<3 && abs(mouse->xy.y-y)<3);
986 mouse->xy.x = x; /* in case we're calling frselect */
988 q0 = t->q0; /* may have changed */
992 if(mouse->buttons == b){
993 t->fr.scroll = framescroll;
994 frselect(&t->fr, mousectl);
995 /* horrible botch: while asleep, may have lost selection altogether */
996 if(selectq > t->file->b.nc)
997 selectq = t->org + t->fr.p0;
1002 q0 = t->org + t->fr.p0;
1003 if(selectq > t->org+t->fr.nchars)
1006 q1 = t->org+t->fr.p1;
1009 if(q0==t->q0 && clicktext==t && mouse->msec-clickmsec<500){
1010 textdoubleclick(t, &q0, &q1);
1014 clickmsec = mouse->msec;
1018 textsetselect(t, q0, q1);
1019 flushimage(display, 1);
1020 state = None; /* what we've done; undo when possible */
1021 while(mouse->buttons){
1025 if(state==None && t->what==Body){
1027 filemark(t->w->body.file);
1030 if(state==Paste && t->what==Body){
1031 winundo(t->w, TRUE);
1032 textsetselect(t, q0, t->q1);
1034 }else if(state != Cut){
1035 cut(t, t, nil, TRUE, TRUE, nil, 0);
1039 if(state==Cut && t->what==Body){
1040 winundo(t->w, TRUE);
1041 textsetselect(t, q0, t->q1);
1043 }else if(state != Paste){
1044 paste(t, t, nil, TRUE, FALSE, nil, 0);
1051 flushimage(display, 1);
1052 while(mouse->buttons == b)
1053 readmouse(mousectl);
1059 textshow(Text *t, uint q0, uint q1, int doselect)
1065 if(t->what != Body){
1067 textsetselect(t, q0, q1);
1070 if(t->w!=nil && t->fr.maxlines==0)
1071 colgrow(t->col, t->w, 1);
1073 textsetselect(t, q0, q1);
1074 qe = t->org+t->fr.nchars;
1075 if(t->org<=q0 && (q0<qe || (q0==qe && qe==t->file->b.nc+t->ncache)))
1078 if(t->w->nopen[QWevent] > 0)
1079 nl = 3*t->fr.maxlines/4;
1081 nl = t->fr.maxlines/4;
1082 q = textbacknl(t, q0, nl);
1083 /* avoid going backwards if trying to go forwards - long lines! */
1084 if(!(q0>t->org && q<t->org))
1085 textsetorigin(t, q, TRUE);
1086 while(q0 > t->org+t->fr.nchars)
1087 textsetorigin(t, t->org+1, FALSE);
1093 region(int a, int b)
1103 selrestore(Frame *f, Point pt0, uint p0, uint p1)
1105 if(p1<=f->p0 || p0>=f->p1){
1107 frdrawsel0(f, pt0, p0, p1, f->cols[BACK], f->cols[TEXT]);
1110 if(p0>=f->p0 && p1<=f->p1){
1111 /* entirely inside */
1112 frdrawsel0(f, pt0, p0, p1, f->cols[HIGH], f->cols[HTEXT]);
1116 /* they now are known to overlap */
1118 /* before selection */
1120 frdrawsel0(f, pt0, p0, f->p0, f->cols[BACK], f->cols[TEXT]);
1122 pt0 = frptofchar(f, p0);
1124 /* after selection */
1126 frdrawsel0(f, frptofchar(f, f->p1), f->p1, p1, f->cols[BACK], f->cols[TEXT]);
1129 /* inside selection */
1130 frdrawsel0(f, pt0, p0, p1, f->cols[HIGH], f->cols[HTEXT]);
1134 textsetselect(Text *t, uint q0, uint q1)
1138 /* t->fr.p0 and t->fr.p1 are always right; t->q0 and t->q1 may be off */
1141 /* compute desired p0,p1 from q0,q1 */
1151 if(p0 > t->fr.nchars)
1153 if(p1 > t->fr.nchars){
1157 if(p0==t->fr.p0 && p1==t->fr.p1){
1158 if(p0 == p1 && ticked != t->fr.ticked)
1159 frtick(&t->fr, frptofchar(&t->fr, p0), ticked);
1162 /* screen disagrees with desired selection */
1163 if(t->fr.p1<=p0 || p1<=t->fr.p0 || p0==p1 || t->fr.p1==t->fr.p0){
1164 /* no overlap or too easy to bother trying */
1165 frdrawsel(&t->fr, frptofchar(&t->fr, t->fr.p0), t->fr.p0, t->fr.p1, 0);
1166 if(p0 != p1 || ticked)
1167 frdrawsel(&t->fr, frptofchar(&t->fr, p0), p0, p1, 1);
1170 /* overlap; avoid unnecessary painting */
1172 /* extend selection backwards */
1173 frdrawsel(&t->fr, frptofchar(&t->fr, p0), p0, t->fr.p0, 1);
1174 }else if(p0 > t->fr.p0){
1175 /* trim first part of selection */
1176 frdrawsel(&t->fr, frptofchar(&t->fr, t->fr.p0), t->fr.p0, p0, 0);
1179 /* extend selection forwards */
1180 frdrawsel(&t->fr, frptofchar(&t->fr, t->fr.p1), t->fr.p1, p1, 1);
1181 }else if(p1 < t->fr.p1){
1182 /* trim last part of selection */
1183 frdrawsel(&t->fr, frptofchar(&t->fr, p1), p1, t->fr.p1, 0);
1192 * Release the button in less than DELAY ms and it's considered a null selection
1193 * if the mouse hardly moved, regardless of whether it crossed a char boundary.
1201 xselect(Frame *f, Mousectl *mc, Image *col, uint *p1p) /* when called, button is down */
1203 uint p0, p1, q, tmp;
1205 Point mp, pt0, pt1, qt;
1214 frtick(f, frptofchar(f, f->p0), 0);
1215 p0 = p1 = frcharofpt(f, mp);
1216 pt0 = frptofchar(f, p0);
1217 pt1 = frptofchar(f, p1);
1221 q = frcharofpt(f, mc->m.xy);
1225 if(reg != region(q, p0)){ /* crossed starting point; reset */
1227 selrestore(f, pt0, p0, p1);
1229 selrestore(f, pt1, p1, p0);
1232 reg = region(q, p0);
1234 frdrawsel0(f, pt0, p0, p1, col, display->white);
1236 qt = frptofchar(f, q);
1239 frdrawsel0(f, pt1, p1, q, col, display->white);
1242 selrestore(f, qt, q, p1);
1245 selrestore(f, pt1, p1, q);
1247 frdrawsel0(f, qt, q, p1, col, display->white);
1254 flushimage(f->display, 1);
1256 }while(mc->m.buttons == b);
1257 if(mc->m.msec-msec < DELAY && p0!=p1
1258 && abs(mp.x-mc->m.xy.x)<MINMOVE
1259 && abs(mp.y-mc->m.xy.y)<MINMOVE) {
1261 selrestore(f, pt0, p0, p1);
1263 selrestore(f, pt1, p1, p0);
1271 pt0 = frptofchar(f, p0);
1274 selrestore(f, pt0, p0, p1);
1277 frtick(f, frptofchar(f, f->p0), 1);
1278 flushimage(f->display, 1);
1284 textselect23(Text *t, uint *q0, uint *q1, Image *high, int mask)
1289 p0 = xselect(&t->fr, mousectl, high, &p1);
1290 buts = mousectl->m.buttons;
1291 if((buts & mask) == 0){
1296 while(mousectl->m.buttons)
1297 readmouse(mousectl);
1302 textselect2(Text *t, uint *q0, uint *q1, Text **tp)
1307 buts = textselect23(t, q0, q1, but2col, 4);
1310 if(buts & 1){ /* pick up argument */
1318 textselect3(Text *t, uint *q0, uint *q1)
1322 h = (textselect23(t, q0, q1, but3col, 1|2) == 0);
1326 static Rune left1[] = { '{', '[', '(', '<', 0xab, 0 };
1327 static Rune right1[] = { '}', ']', ')', '>', 0xbb, 0 };
1328 static Rune left2[] = { '\n', 0 };
1329 static Rune left3[] = { '\'', '"', '`', 0 };
1347 textdoubleclick(Text *t, uint *q0, uint *q1)
1353 if(textclickhtmlmatch(t, q0, q1))
1356 for(i=0; left[i]!=nil; i++){
1360 /* try matching character to left, looking right */
1364 c = textreadc(t, q-1);
1365 p = runestrchr(l, c);
1367 if(textclickmatch(t, c, r[p-l], 1, &q))
1371 /* try matching character to right, looking left */
1372 if(q == t->file->b.nc)
1375 c = textreadc(t, q);
1376 p = runestrchr(r, c);
1378 if(textclickmatch(t, c, l[p-r], -1, &q)){
1379 *q1 = *q0+(*q0<t->file->b.nc && c=='\n');
1381 if(c!='\n' || q!=0 || textreadc(t, 0)=='\n')
1388 /* try filling out word to right */
1389 while(*q1<t->file->b.nc && isalnum(textreadc(t, *q1)))
1391 /* try filling out word to left */
1392 while(*q0>0 && isalnum(textreadc(t, *q0-1)))
1397 textclickmatch(Text *t, int cl, int cr, int dir, uint *q)
1405 if(*q == t->file->b.nc)
1407 c = textreadc(t, *q);
1413 c = textreadc(t, *q);
1421 return cl=='\n' && nest==1;
1424 // Is the text starting at location q an html tag?
1425 // Return 1 for <a>, -1 for </a>, 0 for no tag or <a />.
1426 // Set *q1, if non-nil, to the location after the tag.
1428 ishtmlstart(Text *t, uint q, uint *q1)
1432 if(q+2 > t->file->b.nc)
1434 if(textreadc(t, q++) != '<')
1436 c = textreadc(t, q++);
1440 if(q >= t->file->b.nc)
1443 c = textreadc(t, q++);
1447 if(c1 == '/') // closing tag
1449 if(c2 == '/' || c2 == '!') // open + close tag or comment
1454 // Is the text ending at location q an html tag?
1455 // Return 1 for <a>, -1 for </a>, 0 for no tag or <a />.
1456 // Set *q0, if non-nil, to the start of the tag.
1458 ishtmlend(Text *t, uint q, uint *q0)
1464 if(textreadc(t, --q) != '>')
1466 c = textreadc(t, --q);
1473 c = textreadc(t, --q);
1477 if(c1 == '/') // closing tag
1479 if(c2 == '/' || c2 == '!') // open + close tag or comment
1485 textclickhtmlmatch(Text *t, uint *q0, uint *q1)
1491 // after opening tag? scan forward for closing tag
1492 if(ishtmlend(t, q, nil) == 1) {
1494 while(q < t->file->b.nc) {
1495 n = ishtmlstart(t, q, &nq);
1509 // before closing tag? scan backward for opening tag
1510 if(ishtmlstart(t, q, nil) == -1) {
1513 n = ishtmlend(t, q, &nq);
1531 textbacknl(Text *t, uint p, uint n)
1535 /* look for start of this line if n==0 */
1536 if(n==0 && p>0 && textreadc(t, p-1)!='\n')
1539 while(i-->0 && p>0){
1540 --p; /* it's at a newline now; back over it */
1543 /* at 128 chars, call it a line anyway */
1544 for(j=128; --j>0 && p>0; p--)
1545 if(textreadc(t, p-1)=='\n')
1552 textsetorigin(Text *t, uint org, int exact)
1558 if(org>0 && !exact){
1559 /* org is an estimate of the char posn; find a newline */
1560 /* don't try harder than 256 chars */
1561 for(i=0; i<256 && org<t->file->b.nc; i++){
1562 if(textreadc(t, org) == '\n'){
1571 if(a>=0 && a<t->fr.nchars){
1572 frdelete(&t->fr, 0, a);
1573 fixup = 1; /* frdelete can leave end of last line in wrong selection mode; it doesn't know what follows */
1575 else if(a<0 && -a<t->fr.nchars){
1578 bufread(&t->file->b, org, r, n);
1579 frinsert(&t->fr, r, r+n, 0);
1582 frdelete(&t->fr, 0, t->fr.nchars);
1586 textsetselect(t, t->q0, t->q1);
1587 if(fixup && t->fr.p1 > t->fr.p0)
1588 frdrawsel(&t->fr, frptofchar(&t->fr, t->fr.p1-1), t->fr.p1-1, t->fr.p1, 1);
1596 /* do t->delete(0, t->nc, TRUE) without building backup stuff */
1597 textsetselect(t, t->org, t->org);
1598 frdelete(&t->fr, 0, t->fr.nchars);
1603 bufreset(&t->file->b);