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 */
49 draw(t->fr.b, rr, t->fr.cols[BACK], nil, ZP);
50 /* use no wider than 3-space tabs in a directory */
54 maxt = min(TABDIR, maxtab);
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){
62 textcolumnate(t, t->w->dlp, t->w->ndl);
67 textsetselect(t, t->q0, t->q1);
72 textresize(Text *t, Rectangle r, int keepextra)
77 r.max.y -= Dy(r)%t->fr.font->height;
80 t->scrollr.max.x = r.min.x+Scrollwid;
82 r.min.x += Scrollwid+Scrollgap;
84 textredraw(t, r, t->fr.font, screen, Dx(t->all));
85 if(keepextra && t->fr.r.max.y < t->all.max.y){
87 r.min.y = t->fr.r.max.y;
88 r.max.y = t->all.max.y;
89 draw(screen, r, t->fr.cols[BACK], nil, ZP);
99 filedeltext(t->file, t);
115 dircmp(const void *a, const void *b)
122 n = min(da->nr, db->nr);
123 i = memcmp(da->r, db->r, n*sizeof(Rune));
126 return da->nr - db->nr;
130 textcolumnate(Text *t, Dirlist **dlp, int ndl)
132 int i, j, w, colw, mint, maxt, ncol, nrow;
135 static Rune Lnl[] = { '\n', 0 };
136 static Rune Ltab[] = { '\t', 0 };
138 if(t->file->ntext > 1)
140 mint = stringwidth(t->fr.font, "0");
141 /* go for narrower tabs if set more than 3 wide */
142 t->fr.maxtab = min(maxtab, TABDIR)*mint;
145 for(i=0; i<ndl; i++){
148 if(maxt-w%maxt < mint || w%maxt==0)
158 ncol = max(1, Dx(t->fr.r)/colw);
159 nrow = (ndl+ncol-1)/ncol;
162 for(i=0; i<nrow; i++){
163 for(j=i; j<ndl; j+=nrow){
165 fileinsert(t->file, q1, dl->r, dl->nr);
170 if(maxt-w%maxt < mint){
171 fileinsert(t->file, q1, Ltab, 1);
176 fileinsert(t->file, q1, Ltab, 1);
181 fileinsert(t->file, q1, Lnl, 1);
187 textload(Text *t, uint q0, char *file, int setqid)
191 int fd, i, j, n, ndl, nulls;
197 if(t->ncache!=0 || t->file->b.nc || t->w==nil || t!=&t->w->body || (t->w->isdir && t->file->nname==0))
199 if(t->w->isdir && t->file->nname==0){
200 warning(nil, "empty directory name");
203 fd = open(file, OREAD);
205 warning(nil, "can't open %s: %r\n", file);
210 warning(nil, "can't fstat %s: %r\n", file);
214 if(d->qid.type & QTDIR){
215 /* this is checked in get() but it's possible the file changed underfoot */
216 if(t->file->ntext > 1){
217 warning(nil, "%s is a directory; can't read with multiple windows on it\n", file);
221 t->w->filemenu = FALSE;
222 if(t->file->name[t->file->nname-1] != '/'){
223 rp = runemalloc(t->file->nname+1);
224 runemove(rp, t->file->name, t->file->nname);
225 rp[t->file->nname] = '/';
226 winsetname(t->w, rp, t->file->nname+1);
232 while((n=dirread(fd, &dbuf)) > 0){
234 dl = emalloc(sizeof(Dirlist));
235 j = strlen(dbuf[i].name);
236 tmp = emalloc(j+1+1);
237 memmove(tmp, dbuf[i].name, j);
238 if(dbuf[i].qid.type & QTDIR)
241 dl->r = bytetorune(tmp, &dl->nr);
242 dl->wid = stringwidth(t->fr.font, tmp);
245 dlp = realloc(dlp, ndl*sizeof(Dirlist*));
250 qsort(dlp, ndl, sizeof(Dirlist*), dircmp);
253 textcolumnate(t, dlp, ndl);
257 t->w->filemenu = TRUE;
258 q1 = q0 + fileload(t->file, q0, fd, &nulls);
261 t->file->dev = d->dev;
262 t->file->mtime = d->mtime;
263 t->file->qidpath = d->qid.path;
267 for(q=q0; q<q1; q+=n){
271 bufread(&t->file->b, q, rp, n);
274 else if(q <= t->org+t->fr.nchars)
275 frinsert(&t->fr, rp, rp+n, q-t->org);
276 if(t->fr.lastlinefull)
280 for(i=0; i<t->file->ntext; i++){
281 u = t->file->text[i];
283 if(u->org > u->file->b.nc) /* will be 0 because of reset(), but safety first */
285 textresize(u, u->all, TRUE);
286 textbacknl(u, u->org, 0); /* go to beginning of line */
288 textsetselect(u, q0, q0);
291 warning(nil, "%s: NUL bytes elided\n", file);
301 textbsinsert(Text *t, uint q0, Rune *r, uint n, int tofile, int *nrp)
306 if(t->what == Tag){ /* can't happen but safety first: mustn't backspace over file name */
308 textinsert(t, q0, r, n, tofile);
334 textdelete(t, q0, q0+initial, tofile);
337 textinsert(t, q0, tp, n, tofile);
346 textinsert(Text *t, uint q0, Rune *r, uint n, int tofile)
351 if(tofile && t->ncache != 0)
352 error("text.insert");
356 fileinsert(t->file, q0, r, n);
359 t->w->utflastqid = -1;
361 if(t->file->ntext > 1)
362 for(i=0; i<t->file->ntext; i++){
363 u = t->file->text[i];
365 u->w->dirty = TRUE; /* always a body */
366 textinsert(u, q0, r, n, FALSE);
367 textsetselect(u, u->q0, u->q1);
379 else if(q0 <= t->org+t->fr.nchars)
380 frinsert(&t->fr, r, r+n, q0-t->org);
386 winevent(t->w, "%c%d %d 0 %d %.*S\n", c, q0, q0+n, n, n, r);
388 winevent(t->w, "%c%d %d 0 0 \n", c, q0, q0+n, n);
407 if(t->fr.lastlinefull || t->nofill)
413 n = t->file->b.nc-(t->org+t->fr.nchars);
416 if(n > 2000) /* educated guess at reasonable amount */
418 bufread(&t->file->b, t->org+t->fr.nchars, rp, n);
420 * it's expensive to frinsert more than we need, so
423 nl = t->fr.maxlines-t->fr.nlines;
432 frinsert(&t->fr, rp, rp+i, t->fr.nchars);
433 }while(t->fr.lastlinefull == FALSE);
438 textdelete(Text *t, uint q0, uint q1, int tofile)
444 if(tofile && t->ncache != 0)
445 error("text.delete");
450 filedelete(t->file, q0, q1);
453 t->w->utflastqid = -1;
455 if(t->file->ntext > 1)
456 for(i=0; i<t->file->ntext; i++){
457 u = t->file->text[i];
459 u->w->dirty = TRUE; /* always a body */
460 textdelete(u, q0, q1, FALSE);
461 textsetselect(u, u->q0, u->q1);
467 t->q0 -= min(n, t->q0-q0);
469 t->q1 -= min(n, t->q1-q0);
472 else if(q0 < t->org+t->fr.nchars){
474 if(p1 > t->fr.nchars)
481 frdelete(&t->fr, p0, p1);
488 winevent(t->w, "%c%d %d 0 0 \n", c, q0, q1);
493 textconstrain(Text *t, uint q0, uint q1, uint *p0, uint *p1)
495 *p0 = min(q0, t->file->b.nc);
496 *p1 = min(q1, t->file->b.nc);
500 textreadc(Text *t, uint q)
504 if(t->cq0<=q && q<t->cq0+t->ncache)
505 r = t->cache[q-t->cq0];
507 bufread(&t->file->b, q, &r, 1);
512 textbswidth(Text *t, Rune c)
518 /* there is known to be at least one character to erase */
519 if(c == 0x08) /* ^H: erase character */
524 r = textreadc(t, q-1);
525 if(r == '\n'){ /* eat at most one more character */
526 if(q == t->q0) /* eat the newline */
532 if(eq && skipping) /* found one; stop skipping */
534 else if(!eq && !skipping)
543 textfilewidth(Text *t, uint q0, int oneelement)
550 r = textreadc(t, q-1);
553 if(oneelement && r=='/')
561 textcomplete(Text *t)
572 /* control-f: filename completion; works back to white space or / */
573 if(t->q0<t->file->b.nc && textreadc(t, t->q0)>' ') /* must be at end of word */
575 nstr = textfilewidth(t, t->q0, TRUE);
576 str = runemalloc(nstr);
577 npath = textfilewidth(t, t->q0-nstr, FALSE);
578 path = runemalloc(npath);
585 for(i=0; i<nstr; i++)
586 str[i] = textreadc(t, q++);
587 q = t->q0-nstr-npath;
588 for(i=0; i<npath; i++)
589 path[i] = textreadc(t, q++);
590 /* is path rooted? if not, we need to make it relative to window path */
591 if(npath>0 && path[0]=='/')
592 dir = runestr(path, npath);
594 dir = dirname(t, nil, 0);
595 if(dir.nr + 1 + npath > nelem(tmp)){
601 dir.r = runestrdup(Ldot);
603 runemove(tmp, dir.r, dir.nr);
605 runemove(tmp+dir.nr+1, path, npath);
609 dir = cleanrname(dir);
612 s = smprint("%.*S", nstr, str);
613 dirs = smprint("%.*S", dir.nr, dir.r);
614 c = complete(dirs, s);
617 warning(nil, "error attempting completion: %r\n");
622 warning(nil, "%.*S%s%.*S*%s\n",
624 dir.nr>0 && dir.r[dir.nr-1]!='/' ? "/" : "",
626 c->nmatch ? "" : ": no matches in:");
627 for(i=0; i<c->nfile; i++)
628 warning(nil, " %s\n", c->filename[i]);
632 rp = runesmprint("%s", c->string);
644 texttype(Text *t, Rune r)
655 * Used to disallow \n in tag here.
656 * Also if typing in tag, mark that resize might be necessary.
658 if(t->what!=Body && t->what!=Tag && r=='\n')
661 t->w->tagsafe = FALSE;
670 textshow(t, t->q0-1, t->q0-1, TRUE);
674 if(t->q1 < t->file->b.nc){
676 textshow(t, t->q1+1, t->q1+1, TRUE);
684 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);
706 n = t->fr.maxlines/3;
713 n = mousescrollsize(t->fr.maxlines);
716 n = 2*t->fr.maxlines/3;
718 q0 = textbacknl(t, t->org, n);
719 textsetorigin(t, q0, TRUE);
723 textshow(t, 0, 0, FALSE);
727 textshow(t, t->file->b.nc, t->file->b.nc, FALSE);
729 case 0x01: /* ^A: beginning of line */
731 /* go to where ^U would erase, if not already at BOL */
733 if(t->q0>0 && textreadc(t, t->q0-1)!='\n')
734 nnb = textbswidth(t, 0x15);
735 textshow(t, t->q0-nnb, t->q0-nnb, TRUE);
737 case 0x05: /* ^E: end of line */
740 while(q0<t->file->b.nc && textreadc(t, q0)!='\n')
742 textshow(t, q0, q0, TRUE);
744 /* TAG policy here */
746 /* expand tag to show all text */
747 if(!t->w->tagexpand){
748 t->w->tagexpand = TRUE;
749 winresize(t->w, t->w->r, FALSE, TRUE);
754 /* shrink tag to single line */
756 t->w->tagexpand = FALSE;
758 /* move mouse to stay in tag */
760 if(ptinrect(p, t->w->tag.all)
761 && !ptinrect(p, t->w->tagtop)){
762 p.y = t->w->tagtop.min.y + Dy(t->w->tagtop)/2;
765 winresize(t->w, t->w->r, FALSE, TRUE);
777 cut(t, t, nil, TRUE, TRUE, nil, 0);
780 textshow(t, t->q0, t->q0, 1);
782 case 0x06: /* ^F: complete */
784 rp = textcomplete(t);
788 break; /* fall through to normal insertion case */
791 textsetselect(t, t->eq0, t->q0);
795 case 0x08: /* ^H: erase character */
796 case 0x15: /* ^U: erase line */
797 case 0x17: /* ^W: erase word */
798 if(t->q0 == 0) /* nothing to erase */
800 nnb = textbswidth(t, r);
803 /* if selection is at beginning of window, avoid deleting invisible text */
810 for(i=0; i<t->file->ntext; i++){
811 u = t->file->text[i];
817 error("text.type backspace");
821 textdelete(u, q1-n, q1, FALSE);
824 if(u->eq0==q1 || u->eq0==~0)
827 textdelete(u, q0, q0+nb, TRUE);
829 textsetselect(u, u->q0, u->q1);
831 textsetselect(t, q0, q0);
834 for(i=0; i<t->file->ntext; i++)
835 textfill(t->file->text[i]);
838 if(t->w->autoindent){
839 /* find beginning of previous line using backspace code */
840 nnb = textbswidth(t, 0x15); /* ^U case */
841 rp = runemalloc(nnb + 1);
844 for(i=0; i<nnb; i++){
845 r = textreadc(t, t->q0-nnb+i);
846 if(r != ' ' && r != '\t')
851 break; /* fall through to normal code */
853 /* otherwise ordinary character; just insert, typically in caches of all texts */
854 for(i=0; i<t->file->ntext; i++){
855 u = t->file->text[i];
860 else if(t->q0 != u->cq0+u->ncache)
861 error("text.type cq1");
863 * Change the tag before we add to ncache,
864 * so that if the window body is resized the
865 * commit will not find anything in ncache.
867 if(u->what==Body && u->ncache == 0){
872 textinsert(u, t->q0, rp, nr, FALSE);
874 textsetselect(u, u->q0, u->q1);
875 if(u->ncache+nr > u->ncachealloc){
876 u->ncachealloc += 10 + nr;
877 u->cache = runerealloc(u->cache, u->ncachealloc);
879 runemove(u->cache+u->ncache, rp, nr);
884 textsetselect(t, t->q0+nr, t->q0+nr);
885 if(r=='\n' && t->w!=nil)
890 textcommit(Text *t, int tofile)
895 fileinsert(t->file, t->cq0, t->cache, t->ncache);
898 t->w->utflastqid = -1;
903 static Text *clicktext;
904 static uint clickmsec;
905 static Text *selecttext;
909 * called from frame library
912 framescroll(Frame *f, int dl)
914 if(f != &selecttext->fr)
915 error("frameselect not right frame");
916 textframescroll(selecttext, dl);
920 textframescroll(Text *t, int dl)
929 q0 = textbacknl(t, t->org, -dl);
930 if(selectq > t->org+t->fr.p0)
931 textsetselect(t, t->org+t->fr.p0, selectq);
933 textsetselect(t, selectq, t->org+t->fr.p0);
935 if(t->org+t->fr.nchars == t->file->b.nc)
937 q0 = t->org+frcharofpt(&t->fr, Pt(t->fr.r.min.x, t->fr.r.min.y+dl*t->fr.font->height));
938 if(selectq > t->org+t->fr.p1)
939 textsetselect(t, t->org+t->fr.p1, selectq);
941 textsetselect(t, selectq, t->org+t->fr.p1);
943 textsetorigin(t, q0, TRUE);
953 enum { None, Cut, Paste };
957 * To have double-clicking and chording, we double-click
958 * immediately if it might make sense.
963 selectq = t->org+frcharofpt(&t->fr, mouse->xy);
964 if(clicktext==t && mouse->msec-clickmsec<500)
965 if(q0==q1 && selectq==q0){
966 textdoubleclick(t, &q0, &q1);
967 textsetselect(t, q0, q1);
968 flushimage(display, 1);
971 /* stay here until something interesting happens */
974 while(mouse->buttons==b && abs(mouse->xy.x-x)<3 && abs(mouse->xy.y-y)<3);
975 mouse->xy.x = x; /* in case we're calling frselect */
977 q0 = t->q0; /* may have changed */
981 if(mouse->buttons == b){
982 t->fr.scroll = framescroll;
983 frselect(&t->fr, mousectl);
984 /* horrible botch: while asleep, may have lost selection altogether */
985 if(selectq > t->file->b.nc)
986 selectq = t->org + t->fr.p0;
991 q0 = t->org + t->fr.p0;
992 if(selectq > t->org+t->fr.nchars)
995 q1 = t->org+t->fr.p1;
998 if(q0==t->q0 && clicktext==t && mouse->msec-clickmsec<500){
999 textdoubleclick(t, &q0, &q1);
1003 clickmsec = mouse->msec;
1007 textsetselect(t, q0, q1);
1008 flushimage(display, 1);
1009 state = None; /* what we've done; undo when possible */
1010 while(mouse->buttons){
1014 if(state==None && t->what==Body){
1016 filemark(t->w->body.file);
1019 if(state==Paste && t->what==Body){
1020 winundo(t->w, TRUE);
1021 textsetselect(t, q0, t->q1);
1023 }else if(state != Cut){
1024 cut(t, t, nil, TRUE, TRUE, nil, 0);
1028 if(state==Cut && t->what==Body){
1029 winundo(t->w, TRUE);
1030 textsetselect(t, q0, t->q1);
1032 }else if(state != Paste){
1033 paste(t, t, nil, TRUE, FALSE, nil, 0);
1040 flushimage(display, 1);
1041 while(mouse->buttons == b)
1042 readmouse(mousectl);
1048 textshow(Text *t, uint q0, uint q1, int doselect)
1054 if(t->what != Body){
1056 textsetselect(t, q0, q1);
1059 if(t->w!=nil && t->fr.maxlines==0)
1060 colgrow(t->col, t->w, 1);
1062 textsetselect(t, q0, q1);
1063 qe = t->org+t->fr.nchars;
1064 if(t->org<=q0 && (q0<qe || (q0==qe && qe==t->file->b.nc+t->ncache)))
1067 if(t->w->nopen[QWevent] > 0)
1068 nl = 3*t->fr.maxlines/4;
1070 nl = t->fr.maxlines/4;
1071 q = textbacknl(t, q0, nl);
1072 /* avoid going backwards if trying to go forwards - long lines! */
1073 if(!(q0>t->org && q<t->org))
1074 textsetorigin(t, q, TRUE);
1075 while(q0 > t->org+t->fr.nchars)
1076 textsetorigin(t, t->org+1, FALSE);
1082 region(int a, int b)
1092 selrestore(Frame *f, Point pt0, uint p0, uint p1)
1094 if(p1<=f->p0 || p0>=f->p1){
1096 frdrawsel0(f, pt0, p0, p1, f->cols[BACK], f->cols[TEXT]);
1099 if(p0>=f->p0 && p1<=f->p1){
1100 /* entirely inside */
1101 frdrawsel0(f, pt0, p0, p1, f->cols[HIGH], f->cols[HTEXT]);
1105 /* they now are known to overlap */
1107 /* before selection */
1109 frdrawsel0(f, pt0, p0, f->p0, f->cols[BACK], f->cols[TEXT]);
1111 pt0 = frptofchar(f, p0);
1113 /* after selection */
1115 frdrawsel0(f, frptofchar(f, f->p1), f->p1, p1, f->cols[BACK], f->cols[TEXT]);
1118 /* inside selection */
1119 frdrawsel0(f, pt0, p0, p1, f->cols[HIGH], f->cols[HTEXT]);
1123 textsetselect(Text *t, uint q0, uint q1)
1127 /* t->fr.p0 and t->fr.p1 are always right; t->q0 and t->q1 may be off */
1130 /* compute desired p0,p1 from q0,q1 */
1140 if(p0 > t->fr.nchars)
1142 if(p1 > t->fr.nchars){
1146 if(p0==t->fr.p0 && p1==t->fr.p1){
1147 if(p0 == p1 && ticked != t->fr.ticked)
1148 frtick(&t->fr, frptofchar(&t->fr, p0), ticked);
1151 /* screen disagrees with desired selection */
1152 if(t->fr.p1<=p0 || p1<=t->fr.p0 || p0==p1 || t->fr.p1==t->fr.p0){
1153 /* no overlap or too easy to bother trying */
1154 frdrawsel(&t->fr, frptofchar(&t->fr, t->fr.p0), t->fr.p0, t->fr.p1, 0);
1155 if(p0 != p1 || ticked)
1156 frdrawsel(&t->fr, frptofchar(&t->fr, p0), p0, p1, 1);
1159 /* overlap; avoid unnecessary painting */
1161 /* extend selection backwards */
1162 frdrawsel(&t->fr, frptofchar(&t->fr, p0), p0, t->fr.p0, 1);
1163 }else if(p0 > t->fr.p0){
1164 /* trim first part of selection */
1165 frdrawsel(&t->fr, frptofchar(&t->fr, t->fr.p0), t->fr.p0, p0, 0);
1168 /* extend selection forwards */
1169 frdrawsel(&t->fr, frptofchar(&t->fr, t->fr.p1), t->fr.p1, p1, 1);
1170 }else if(p1 < t->fr.p1){
1171 /* trim last part of selection */
1172 frdrawsel(&t->fr, frptofchar(&t->fr, p1), p1, t->fr.p1, 0);
1181 * Release the button in less than DELAY ms and it's considered a null selection
1182 * if the mouse hardly moved, regardless of whether it crossed a char boundary.
1190 xselect(Frame *f, Mousectl *mc, Image *col, uint *p1p) /* when called, button is down */
1192 uint p0, p1, q, tmp;
1194 Point mp, pt0, pt1, qt;
1203 frtick(f, frptofchar(f, f->p0), 0);
1204 p0 = p1 = frcharofpt(f, mp);
1205 pt0 = frptofchar(f, p0);
1206 pt1 = frptofchar(f, p1);
1210 q = frcharofpt(f, mc->m.xy);
1214 if(reg != region(q, p0)){ /* crossed starting point; reset */
1216 selrestore(f, pt0, p0, p1);
1218 selrestore(f, pt1, p1, p0);
1221 reg = region(q, p0);
1223 frdrawsel0(f, pt0, p0, p1, col, display->white);
1225 qt = frptofchar(f, q);
1228 frdrawsel0(f, pt1, p1, q, col, display->white);
1231 selrestore(f, qt, q, p1);
1234 selrestore(f, pt1, p1, q);
1236 frdrawsel0(f, qt, q, p1, col, display->white);
1243 flushimage(f->display, 1);
1245 }while(mc->m.buttons == b);
1246 if(mc->m.msec-msec < DELAY && p0!=p1
1247 && abs(mp.x-mc->m.xy.x)<MINMOVE
1248 && abs(mp.y-mc->m.xy.y)<MINMOVE) {
1250 selrestore(f, pt0, p0, p1);
1252 selrestore(f, pt1, p1, p0);
1260 pt0 = frptofchar(f, p0);
1263 selrestore(f, pt0, p0, p1);
1266 frtick(f, frptofchar(f, f->p0), 1);
1267 flushimage(f->display, 1);
1273 textselect23(Text *t, uint *q0, uint *q1, Image *high, int mask)
1278 p0 = xselect(&t->fr, mousectl, high, &p1);
1279 buts = mousectl->m.buttons;
1280 if((buts & mask) == 0){
1285 while(mousectl->m.buttons)
1286 readmouse(mousectl);
1291 textselect2(Text *t, uint *q0, uint *q1, Text **tp)
1296 buts = textselect23(t, q0, q1, but2col, 4);
1299 if(buts & 1){ /* pick up argument */
1307 textselect3(Text *t, uint *q0, uint *q1)
1311 h = (textselect23(t, q0, q1, but3col, 1|2) == 0);
1315 static Rune left1[] = { '{', '[', '(', '<', 0xab, 0 };
1316 static Rune right1[] = { '}', ']', ')', '>', 0xbb, 0 };
1317 static Rune left2[] = { '\n', 0 };
1318 static Rune left3[] = { '\'', '"', '`', 0 };
1336 textdoubleclick(Text *t, uint *q0, uint *q1)
1342 for(i=0; left[i]!=nil; i++){
1346 /* try matching character to left, looking right */
1350 c = textreadc(t, q-1);
1351 p = runestrchr(l, c);
1353 if(textclickmatch(t, c, r[p-l], 1, &q))
1357 /* try matching character to right, looking left */
1358 if(q == t->file->b.nc)
1361 c = textreadc(t, q);
1362 p = runestrchr(r, c);
1364 if(textclickmatch(t, c, l[p-r], -1, &q)){
1365 *q1 = *q0+(*q0<t->file->b.nc && c=='\n');
1367 if(c!='\n' || q!=0 || textreadc(t, 0)=='\n')
1373 /* try filling out word to right */
1374 while(*q1<t->file->b.nc && isalnum(textreadc(t, *q1)))
1376 /* try filling out word to left */
1377 while(*q0>0 && isalnum(textreadc(t, *q0-1)))
1382 textclickmatch(Text *t, int cl, int cr, int dir, uint *q)
1390 if(*q == t->file->b.nc)
1392 c = textreadc(t, *q);
1398 c = textreadc(t, *q);
1406 return cl=='\n' && nest==1;
1410 textbacknl(Text *t, uint p, uint n)
1414 /* look for start of this line if n==0 */
1415 if(n==0 && p>0 && textreadc(t, p-1)!='\n')
1418 while(i-->0 && p>0){
1419 --p; /* it's at a newline now; back over it */
1422 /* at 128 chars, call it a line anyway */
1423 for(j=128; --j>0 && p>0; p--)
1424 if(textreadc(t, p-1)=='\n')
1431 textsetorigin(Text *t, uint org, int exact)
1437 if(org>0 && !exact){
1438 /* org is an estimate of the char posn; find a newline */
1439 /* don't try harder than 256 chars */
1440 for(i=0; i<256 && org<t->file->b.nc; i++){
1441 if(textreadc(t, org) == '\n'){
1450 if(a>=0 && a<t->fr.nchars){
1451 frdelete(&t->fr, 0, a);
1452 fixup = 1; /* frdelete can leave end of last line in wrong selection mode; it doesn't know what follows */
1454 else if(a<0 && -a<t->fr.nchars){
1457 bufread(&t->file->b, org, r, n);
1458 frinsert(&t->fr, r, r+n, 0);
1461 frdelete(&t->fr, 0, t->fr.nchars);
1465 textsetselect(t, t->q0, t->q1);
1466 if(fixup && t->fr.p1 > t->fr.p0)
1467 frdrawsel(&t->fr, frptofchar(&t->fr, t->fr.p1-1), t->fr.p1-1, t->fr.p1, 1);
1475 /* do t->delete(0, t->nc, TRUE) without building backup stuff */
1476 textsetselect(t, t->org, t->org);
1477 frdelete(&t->fr, 0, t->fr.nchars);
1482 bufreset(&t->file->b);