text.c 28 KB

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