16 Image *textcols[NCOL];
19 TABDIR = 3 /* width of tabs in directory windows */
23 textinit(Text *t, File *f, Rectangle r, Reffont *rf, Image *cols[NCOL])
28 t->scrollr.max.x = r.min.x+Scrollwid;
30 r.min.x += Scrollwid+Scrollgap;
35 memmove(t->Frame.cols, cols, sizeof t->Frame.cols);
36 textredraw(t, r, rf->f, screen, -1);
40 textredraw(Text *t, Rectangle r, Font *f, Image *b, int odx)
45 frinit(t, r, f, b, t->Frame.cols);
47 rr.min.x -= Scrollwid+Scrollgap; /* back fill to scroll bar */
48 draw(t->b, rr, t->cols[BACK], nil, ZP);
49 /* use no wider than 3-space tabs in a directory */
53 maxt = min(TABDIR, maxtab);
57 t->maxtab = maxt*stringwidth(f, "0");
58 if(t->what==Body && t->w->isdir && odx!=Dx(t->all)){
61 textcolumnate(t, t->w->dlp, t->w->ndl);
66 textsetselect(t, t->q0, t->q1);
71 textresize(Text *t, Rectangle r)
76 r.max.y -= Dy(r)%t->font->height;
82 t->scrollr.max.x = r.min.x+Scrollwid;
84 r.min.x += Scrollwid+Scrollgap;
86 textredraw(t, r, t->font, t->b, odx);
95 filedeltext(t->file, t);
111 dircmp(void *a, void *b)
118 n = min(da->nr, db->nr);
119 i = memcmp(da->r, db->r, n*sizeof(Rune));
122 return da->nr - db->nr;
126 textcolumnate(Text *t, Dirlist **dlp, int ndl)
128 int i, j, w, colw, mint, maxt, ncol, nrow;
132 if(t->file->ntext > 1)
134 mint = stringwidth(t->font, "0");
135 /* go for narrower tabs if set more than 3 wide */
136 t->maxtab = min(maxtab, TABDIR)*mint;
139 for(i=0; i<ndl; i++){
142 if(maxt-w%maxt < mint || w%maxt==0)
152 ncol = max(1, Dx(t->r)/colw);
153 nrow = (ndl+ncol-1)/ncol;
156 for(i=0; i<nrow; i++){
157 for(j=i; j<ndl; j+=nrow){
159 fileinsert(t->file, q1, dl->r, dl->nr);
164 if(maxt-w%maxt < mint){
165 fileinsert(t->file, q1, L"\t", 1);
170 fileinsert(t->file, q1, L"\t", 1);
175 fileinsert(t->file, q1, L"\n", 1);
181 textload(Text *t, uint q0, char *file, int setqid)
185 int fd, i, j, n, ndl, nulls;
191 if(t->ncache!=0 || t->file->nc || t->w==nil || t!=&t->w->body)
193 if(t->w->isdir && t->file->nname==0){
194 warning(nil, "empty directory name\n");
197 fd = open(file, OREAD);
199 warning(nil, "can't open %s: %r\n", file);
204 warning(nil, "can't fstat %s: %r\n", file);
208 if(d->qid.type & QTDIR){
209 /* this is checked in get() but it's possible the file changed underfoot */
210 if(t->file->ntext > 1){
211 warning(nil, "%s is a directory; can't read with multiple windows on it\n", file);
215 t->w->filemenu = FALSE;
216 if(t->file->nname > 0 && t->file->name[t->file->nname-1] != '/'){
217 rp = runemalloc(t->file->nname+1);
218 runemove(rp, t->file->name, t->file->nname);
219 rp[t->file->nname] = '/';
220 winsetname(t->w, rp, t->file->nname+1);
226 while((n=dirread(fd, &dbuf)) > 0){
228 dl = emalloc(sizeof(Dirlist));
229 j = strlen(dbuf[i].name);
230 tmp = emalloc(j+1+1);
231 memmove(tmp, dbuf[i].name, j);
232 if(dbuf[i].qid.type & QTDIR)
235 dl->r = bytetorune(tmp, &dl->nr);
236 dl->wid = stringwidth(t->font, tmp);
239 dlp = realloc(dlp, ndl*sizeof(Dirlist*));
244 qsort(dlp, ndl, sizeof(Dirlist*), dircmp);
247 textcolumnate(t, dlp, ndl);
251 t->w->filemenu = TRUE;
252 q1 = q0 + fileload(t->file, q0, fd, &nulls);
255 t->file->dev = d->dev;
256 t->file->mtime = d->mtime;
257 t->file->qidpath = d->qid.path;
261 for(q=q0; q<q1; q+=n){
265 bufread(t->file, q, rp, n);
268 else if(q <= t->org+t->nchars)
269 frinsert(t, rp, rp+n, q-t->org);
274 for(i=0; i<t->file->ntext; i++){
275 u = t->file->text[i];
277 if(u->org > u->file->nc) /* will be 0 because of reset(), but safety first */
279 textresize(u, u->all);
280 textbacknl(u, u->org, 0); /* go to beginning of line */
282 textsetselect(u, q0, q0);
285 warning(nil, "%s: NUL bytes elided\n", file);
295 textbsinsert(Text *t, uint q0, Rune *r, uint n, int tofile, int *nrp)
300 if(t->what == Tag){ /* can't happen but safety first: mustn't backspace over file name */
302 textinsert(t, q0, r, n, tofile);
328 textdelete(t, q0, q0+initial, tofile);
331 textinsert(t, q0, tp, n, tofile);
340 textinsert(Text *t, uint q0, Rune *r, uint n, int tofile)
345 if(tofile && t->ncache != 0)
346 error("text.insert");
350 fileinsert(t->file, q0, r, n);
353 t->w->utflastqid = -1;
355 if(t->file->ntext > 1)
356 for(i=0; i<t->file->ntext; i++){
357 u = t->file->text[i];
359 u->w->dirty = TRUE; /* always a body */
360 textinsert(u, q0, r, n, FALSE);
361 textsetselect(u, u->q0, u->q1);
373 else if(q0 <= t->org+t->nchars)
374 frinsert(t, r, r+n, q0-t->org);
380 winevent(t->w, "%c%d %d 0 %d %.*S\n", c, q0, q0+n, n, n, r);
382 winevent(t->w, "%c%d %d 0 0 \n", c, q0, q0+n, n);
401 if(t->lastlinefull || t->nofill)
407 n = t->file->nc-(t->org+t->nchars);
410 if(n > 2000) /* educated guess at reasonable amount */
412 bufread(t->file, t->org+t->nchars, rp, n);
414 * it's expensive to frinsert more than we need, so
417 nl = t->maxlines-t->nlines;
426 frinsert(t, rp, rp+i, t->nchars);
427 }while(t->lastlinefull == FALSE);
432 textdelete(Text *t, uint q0, uint q1, int tofile)
438 if(tofile && t->ncache != 0)
439 error("text.delete");
444 filedelete(t->file, q0, q1);
447 t->w->utflastqid = -1;
449 if(t->file->ntext > 1)
450 for(i=0; i<t->file->ntext; i++){
451 u = t->file->text[i];
453 u->w->dirty = TRUE; /* always a body */
454 textdelete(u, q0, q1, FALSE);
455 textsetselect(u, u->q0, u->q1);
461 t->q0 -= min(n, t->q0-q0);
463 t->q1 -= min(n, t->q1-q0);
466 else if(q0 < t->org+t->nchars){
482 winevent(t->w, "%c%d %d 0 0 \n", c, q0, q1);
487 textconstrain(Text *t, uint q0, uint q1, uint *p0, uint *p1)
489 *p0 = min(q0, t->file->nc);
490 *p1 = min(q1, t->file->nc);
494 textreadc(Text *t, uint q)
498 if(t->cq0<=q && q<t->cq0+t->ncache)
499 r = t->cache[q-t->cq0];
501 bufread(t->file, q, &r, 1);
506 textbswidth(Text *t, Rune c)
512 /* there is known to be at least one character to erase */
513 if(c == 0x08) /* ^H: erase character */
518 r = textreadc(t, q-1);
519 if(r == '\n'){ /* eat at most one more character */
520 if(q == t->q0) /* eat the newline */
526 if(eq && skipping) /* found one; stop skipping */
528 else if(!eq && !skipping)
537 textfilewidth(Text *t, uint q0, int oneelement)
544 r = textreadc(t, q-1);
547 if(oneelement && r=='/')
555 textcomplete(Text *t)
566 /* control-f: filename completion; works back to white space or / */
567 if(t->q0<t->file->nc && textreadc(t, t->q0)>' ') /* must be at end of word */
569 nstr = textfilewidth(t, t->q0, TRUE);
570 str = runemalloc(nstr);
571 npath = textfilewidth(t, t->q0-nstr, FALSE);
572 path = runemalloc(npath);
579 for(i=0; i<nstr; i++)
580 str[i] = textreadc(t, q++);
581 q = t->q0-nstr-npath;
582 for(i=0; i<npath; i++)
583 path[i] = textreadc(t, q++);
584 /* is path rooted? if not, we need to make it relative to window path */
585 if(npath>0 && path[0]=='/')
586 dir = (Runestr){path, npath};
588 dir = dirname(t, nil, 0);
589 if(dir.nr + 1 + npath > nelem(tmp)){
595 dir.r = runestrdup(L".");
597 runemove(tmp, dir.r, dir.nr);
599 runemove(tmp+dir.nr+1, path, npath);
603 dir = cleanrname(dir);
606 s = smprint("%.*S", nstr, str);
607 dirs = smprint("%.*S", dir.nr, dir.r);
608 c = complete(dirs, s);
611 warning(nil, "error attempting completion: %r\n");
616 warning(nil, "%.*S%s%.*S*%s\n",
618 dir.nr>0 && dir.r[dir.nr-1]!='/' ? "/" : "",
620 c->nmatch? "" : ": no matches in:");
621 for(i=0; i<c->nfile; i++)
622 warning(nil, " %s\n", c->filename[i]);
626 rp = runesmprint("%s", c->string);
638 texttype(Text *t, Rune r)
652 textshow(t, t->q0-1, t->q0-1, TRUE);
656 if(t->q1 < t->file->nc)
657 textshow(t, t->q1+1, t->q1+1, TRUE);
663 n = mousescrollsize(t->maxlines);
670 q0 = t->org+frcharofpt(t, Pt(t->r.min.x, t->r.min.y+n*t->font->height));
672 textsetorigin(t, q0, TRUE);
678 n = mousescrollsize(t->maxlines);
683 q0 = textbacknl(t, t->org, n);
685 textsetorigin(t, q0, TRUE);
689 textshow(t, 0, 0, FALSE);
693 textshow(t, t->file->nc, t->file->nc, FALSE);
695 case 0x01: /* ^A: beginning of line */
697 /* go to where ^U would erase, if not already at BOL */
699 if(t->q0>0 && textreadc(t, t->q0-1)!='\n')
700 nnb = textbswidth(t, 0x15);
701 textshow(t, t->q0-nnb, t->q0-nnb, TRUE);
703 case 0x05: /* ^E: end of line */
706 while(q0<t->file->nc && textreadc(t, q0)!='\n')
708 textshow(t, q0, q0, TRUE);
718 cut(t, t, nil, TRUE, TRUE, nil, 0);
721 textshow(t, t->q0, t->q0, 1);
725 rp = textcomplete(t);
729 break; /* fall through to normal insertion case */
732 textsetselect(t, t->eq0, t->q0);
736 case 0x08: /* ^H: erase character */
737 case 0x15: /* ^U: erase line */
738 case 0x17: /* ^W: erase word */
739 if(t->q0 == 0) /* nothing to erase */
741 nnb = textbswidth(t, r);
744 /* if selection is at beginning of window, avoid deleting invisible text */
751 for(i=0; i<t->file->ntext; i++){
752 u = t->file->text[i];
758 error("text.type backspace");
762 textdelete(u, q1-n, q1, FALSE);
765 if(u->eq0==q1 || u->eq0==~0)
768 textdelete(u, q0, q0+nb, TRUE);
770 textsetselect(u, u->q0, u->q1);
772 textsetselect(t, q0, q0);
775 for(i=0; i<t->file->ntext; i++)
776 textfill(t->file->text[i]);
779 if(t->what == Body && t->w->autoindent){
780 /* find beginning of previous line using backspace code */
781 nnb = textbswidth(t, 0x15); /* ^U case */
782 rp = runemalloc(nnb + 1);
785 for(i=0; i<nnb; i++){
786 r = textreadc(t, t->q0-nnb+i);
787 if(r != ' ' && r != '\t')
792 break; /* fall through to normal code */
794 /* otherwise ordinary character; just insert, typically in caches of all texts */
795 for(i=0; i<t->file->ntext; i++){
796 u = t->file->text[i];
801 else if(t->q0 != u->cq0+u->ncache)
802 error("text.type cq1");
803 textinsert(u, t->q0, rp, nr, FALSE);
805 textsetselect(u, u->q0, u->q1);
806 if(u->ncache+nr > u->ncachealloc){
807 u->ncachealloc += 10 + nr;
808 u->cache = runerealloc(u->cache, u->ncachealloc);
810 runemove(u->cache+u->ncache, rp, nr);
815 textsetselect(t, t->q0+nr, t->q0+nr);
816 if(r=='\n' && t->w!=nil)
821 textcommit(Text *t, int tofile)
826 fileinsert(t->file, t->cq0, t->cache, t->ncache);
829 t->w->utflastqid = -1;
834 static Text *clicktext;
835 static uint clickmsec;
836 static Text *selecttext;
840 * called from frame library
843 framescroll(Frame *f, int dl)
845 if(f != &selecttext->Frame)
846 error("frameselect not right frame");
847 textframescroll(selecttext, dl);
851 textframescroll(Text *t, int dl)
860 q0 = textbacknl(t, t->org, -dl);
861 if(selectq > t->org+t->p0)
862 textsetselect(t, t->org+t->p0, selectq);
864 textsetselect(t, selectq, t->org+t->p0);
866 if(t->org+t->nchars == t->file->nc)
868 q0 = t->org+frcharofpt(t, Pt(t->r.min.x, t->r.min.y+dl*t->font->height));
869 if(selectq > t->org+t->p1)
870 textsetselect(t, t->org+t->p1, selectq);
872 textsetselect(t, selectq, t->org+t->p1);
874 textsetorigin(t, q0, TRUE);
887 * To have double-clicking and chording, we double-click
888 * immediately if it might make sense.
893 selectq = t->org+frcharofpt(t, mouse->xy);
894 if(clicktext==t && mouse->msec-clickmsec<500)
895 if(q0==q1 && selectq==q0){
896 textdoubleclick(t, &q0, &q1);
897 textsetselect(t, q0, q1);
898 flushimage(display, 1);
901 /* stay here until something interesting happens */
904 while(mouse->buttons==b && abs(mouse->xy.x-x)<3 && abs(mouse->xy.y-y)<3);
905 mouse->xy.x = x; /* in case we're calling frselect */
907 q0 = t->q0; /* may have changed */
911 if(mouse->buttons == b){
912 t->Frame.scroll = framescroll;
913 frselect(t, mousectl);
914 /* horrible botch: while asleep, may have lost selection altogether */
915 if(selectq > t->file->nc)
916 selectq = t->org + t->p0;
917 t->Frame.scroll = nil;
922 if(selectq > t->org+t->nchars)
928 if(q0==t->q0 && clicktext==t && mouse->msec-clickmsec<500){
929 textdoubleclick(t, &q0, &q1);
933 clickmsec = mouse->msec;
937 textsetselect(t, q0, q1);
938 flushimage(display, 1);
939 state = 0; /* undo when possible; +1 for cut, -1 for paste */
940 while(mouse->buttons){
944 if(state==0 && t->what==Body){
946 filemark(t->w->body.file);
949 if(state==-1 && t->what==Body){
951 textsetselect(t, q0, t->q0);
953 }else if(state != 1){
954 cut(t, t, nil, TRUE, TRUE, nil, 0);
958 if(state==1 && t->what==Body){
960 textsetselect(t, q0, t->q1);
962 }else if(state != -1){
963 paste(t, t, nil, TRUE, FALSE, nil, 0);
970 flushimage(display, 1);
971 while(mouse->buttons == b)
978 textshow(Text *t, uint q0, uint q1, int doselect)
986 textsetselect(t, q0, q1);
989 if(t->w!=nil && t->maxlines==0)
990 colgrow(t->col, t->w, 1);
992 textsetselect(t, q0, q1);
993 qe = t->org+t->nchars;
994 if(t->org<=q0 && (q0<qe || (q0==qe && qe==t->file->nc+t->ncache)))
997 if(t->w->nopen[QWevent] > 0)
998 nl = 3*t->maxlines/4;
1001 q = textbacknl(t, q0, nl);
1002 /* avoid going backwards if trying to go forwards - long lines! */
1003 if(!(q0>t->org && q<t->org))
1004 textsetorigin(t, q, TRUE);
1005 while(q0 > t->org+t->nchars)
1006 textsetorigin(t, t->org+1, FALSE);
1012 region(int a, int b)
1022 selrestore(Frame *f, Point pt0, uint p0, uint p1)
1024 if(p1<=f->p0 || p0>=f->p1){
1026 frdrawsel0(f, pt0, p0, p1, f->cols[BACK], f->cols[TEXT]);
1029 if(p0>=f->p0 && p1<=f->p1){
1030 /* entirely inside */
1031 frdrawsel0(f, pt0, p0, p1, f->cols[HIGH], f->cols[HTEXT]);
1035 /* they now are known to overlap */
1037 /* before selection */
1039 frdrawsel0(f, pt0, p0, f->p0, f->cols[BACK], f->cols[TEXT]);
1041 pt0 = frptofchar(f, p0);
1043 /* after selection */
1045 frdrawsel0(f, frptofchar(f, f->p1), f->p1, p1, f->cols[BACK], f->cols[TEXT]);
1048 /* inside selection */
1049 frdrawsel0(f, pt0, p0, p1, f->cols[HIGH], f->cols[HTEXT]);
1053 textsetselect(Text *t, uint q0, uint q1)
1057 /* t->p0 and t->p1 are always right; t->q0 and t->q1 may be off */
1060 /* compute desired p0,p1 from q0,q1 */
1071 if(p0==t->p0 && p1==t->p1)
1073 /* screen disagrees with desired selection */
1074 if(t->p1<=p0 || p1<=t->p0 || p0==p1 || t->p1==t->p0){
1075 /* no overlap or too easy to bother trying */
1076 frdrawsel(t, frptofchar(t, t->p0), t->p0, t->p1, 0);
1077 frdrawsel(t, frptofchar(t, p0), p0, p1, 1);
1080 /* overlap; avoid unnecessary painting */
1082 /* extend selection backwards */
1083 frdrawsel(t, frptofchar(t, p0), p0, t->p0, 1);
1084 }else if(p0 > t->p0){
1085 /* trim first part of selection */
1086 frdrawsel(t, frptofchar(t, t->p0), t->p0, p0, 0);
1089 /* extend selection forwards */
1090 frdrawsel(t, frptofchar(t, t->p1), t->p1, p1, 1);
1091 }else if(p1 < t->p1){
1092 /* trim last part of selection */
1093 frdrawsel(t, frptofchar(t, p1), p1, t->p1, 0);
1102 * Release the button in less than DELAY ms and it's considered a null selection
1103 * if the mouse hardly moved, regardless of whether it crossed a char boundary.
1111 xselect(Frame *f, Mousectl *mc, Image *col, uint *p1p) /* when called, button is down */
1113 uint p0, p1, q, tmp;
1115 Point mp, pt0, pt1, qt;
1124 frtick(f, frptofchar(f, f->p0), 0);
1125 p0 = p1 = frcharofpt(f, mp);
1126 pt0 = frptofchar(f, p0);
1127 pt1 = frptofchar(f, p1);
1131 q = frcharofpt(f, mc->xy);
1135 if(reg != region(q, p0)){ /* crossed starting point; reset */
1137 selrestore(f, pt0, p0, p1);
1139 selrestore(f, pt1, p1, p0);
1142 reg = region(q, p0);
1144 frdrawsel0(f, pt0, p0, p1, col, display->white);
1146 qt = frptofchar(f, q);
1149 frdrawsel0(f, pt1, p1, q, col, display->white);
1152 selrestore(f, qt, q, p1);
1155 selrestore(f, pt1, p1, q);
1157 frdrawsel0(f, qt, q, p1, col, display->white);
1164 flushimage(f->display, 1);
1166 }while(mc->buttons == b);
1167 if(mc->msec-msec < DELAY && p0!=p1
1168 && abs(mp.x-mc->xy.x)<MINMOVE
1169 && abs(mp.y-mc->xy.y)<MINMOVE) {
1171 selrestore(f, pt0, p0, p1);
1173 selrestore(f, pt1, p1, p0);
1181 pt0 = frptofchar(f, p0);
1184 selrestore(f, pt0, p0, p1);
1187 frtick(f, frptofchar(f, f->p0), 1);
1188 flushimage(f->display, 1);
1194 textselect23(Text *t, uint *q0, uint *q1, Image *high, int mask)
1199 p0 = xselect(t, mousectl, high, &p1);
1200 buts = mousectl->buttons;
1201 if((buts & mask) == 0){
1206 while(mousectl->buttons)
1207 readmouse(mousectl);
1212 textselect2(Text *t, uint *q0, uint *q1, Text **tp)
1217 buts = textselect23(t, q0, q1, but2col, 4);
1220 if(buts & 1){ /* pick up argument */
1228 textselect3(Text *t, uint *q0, uint *q1)
1232 h = (textselect23(t, q0, q1, but3col, 1|2) == 0);
1236 static Rune left1[] = { L'{', L'[', L'(', L'<', L'«', 0 };
1237 static Rune right1[] = { L'}', L']', L')', L'>', L'»', 0 };
1238 static Rune left2[] = { L'\n', 0 };
1239 static Rune left3[] = { L'\'', L'"', L'`', 0 };
1257 textdoubleclick(Text *t, uint *q0, uint *q1)
1263 for(i=0; left[i]!=nil; i++){
1267 /* try matching character to left, looking right */
1271 c = textreadc(t, q-1);
1272 p = runestrchr(l, c);
1274 if(textclickmatch(t, c, r[p-l], 1, &q))
1278 /* try matching character to right, looking left */
1279 if(q == t->file->nc)
1282 c = textreadc(t, q);
1283 p = runestrchr(r, c);
1285 if(textclickmatch(t, c, l[p-r], -1, &q)){
1286 *q1 = *q0+(*q0<t->file->nc && c=='\n');
1288 if(c!='\n' || q!=0 || textreadc(t, 0)=='\n')
1294 /* try filling out word to right */
1295 while(*q1<t->file->nc && isalnum(textreadc(t, *q1)))
1297 /* try filling out word to left */
1298 while(*q0>0 && isalnum(textreadc(t, *q0-1)))
1303 textclickmatch(Text *t, int cl, int cr, int dir, uint *q)
1311 if(*q == t->file->nc)
1313 c = textreadc(t, *q);
1319 c = textreadc(t, *q);
1327 return cl=='\n' && nest==1;
1331 textbacknl(Text *t, uint p, uint n)
1335 /* look for start of this line if n==0 */
1336 if(n==0 && p>0 && textreadc(t, p-1)!='\n')
1339 while(i-->0 && p>0){
1340 --p; /* it's at a newline now; back over it */
1343 /* at 128 chars, call it a line anyway */
1344 for(j=128; --j>0 && p>0; p--)
1345 if(textreadc(t, p-1)=='\n')
1352 textsetorigin(Text *t, uint org, int exact)
1358 if(org>0 && !exact){
1359 /* org is an estimate of the char posn; find a newline */
1360 /* don't try harder than 256 chars */
1361 for(i=0; i<256 && org<t->file->nc; i++){
1362 if(textreadc(t, org) == '\n'){
1371 if(a>=0 && a<t->nchars){
1373 fixup = 1; /* frdelete can leave end of last line in wrong selection mode; it doesn't know what follows */
1375 else if(a<0 && -a<t->nchars){
1378 bufread(t->file, org, r, n);
1379 frinsert(t, r, r+n, 0);
1382 frdelete(t, 0, t->nchars);
1386 textsetselect(t, t->q0, t->q1);
1387 if(fixup && t->p1 > t->p0)
1388 frdrawsel(t, frptofchar(t, t->p1-1), t->p1-1, t->p1, 1);
1396 /* do t->delete(0, t->nc, TRUE) without building backup stuff */
1397 textsetselect(t, t->org, t->org);
1398 frdelete(t, 0, t->nchars);