wind.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <draw.h>
  4. #include <thread.h>
  5. #include <cursor.h>
  6. #include <mouse.h>
  7. #include <keyboard.h>
  8. #include <frame.h>
  9. #include <fcall.h>
  10. #include <plumb.h>
  11. #include "dat.h"
  12. #include "fns.h"
  13. int winid;
  14. void
  15. wininit(Window *w, Window *clone, Rectangle r)
  16. {
  17. Rectangle r1, br;
  18. File *f;
  19. Reffont *rf;
  20. Rune *rp;
  21. int nc;
  22. w->tag.w = w;
  23. w->body.w = w;
  24. w->id = ++winid;
  25. incref(w);
  26. if(globalincref)
  27. incref(w);
  28. w->ctlfid = ~0;
  29. w->utflastqid = -1;
  30. r1 = r;
  31. r1.max.y = r1.min.y + font->height;
  32. incref(&reffont);
  33. f = fileaddtext(nil, &w->tag);
  34. textinit(&w->tag, f, r1, &reffont, tagcols);
  35. w->tag.what = Tag;
  36. /* tag is a copy of the contents, not a tracked image */
  37. if(clone){
  38. textdelete(&w->tag, 0, w->tag.file->nc, TRUE);
  39. nc = clone->tag.file->nc;
  40. rp = runemalloc(nc);
  41. bufread(clone->tag.file, 0, rp, nc);
  42. textinsert(&w->tag, 0, rp, nc, TRUE);
  43. free(rp);
  44. filereset(w->tag.file);
  45. textsetselect(&w->tag, nc, nc);
  46. }
  47. r1 = r;
  48. r1.min.y += font->height + 1;
  49. if(r1.max.y < r1.min.y)
  50. r1.max.y = r1.min.y;
  51. f = nil;
  52. if(clone){
  53. f = clone->body.file;
  54. w->body.org = clone->body.org;
  55. w->isscratch = clone->isscratch;
  56. rf = rfget(FALSE, FALSE, FALSE, clone->body.reffont->f->name);
  57. }else
  58. rf = rfget(FALSE, FALSE, FALSE, nil);
  59. f = fileaddtext(f, &w->body);
  60. w->body.what = Body;
  61. textinit(&w->body, f, r1, rf, textcols);
  62. r1.min.y -= 1;
  63. r1.max.y = r1.min.y+1;
  64. draw(screen, r1, tagcols[BORD], nil, ZP);
  65. textscrdraw(&w->body);
  66. w->r = r;
  67. w->r.max.y = w->body.r.max.y;
  68. br.min = w->tag.scrollr.min;
  69. br.max.x = br.min.x + Dx(button->r);
  70. br.max.y = br.min.y + Dy(button->r);
  71. draw(screen, br, button, nil, button->r.min);
  72. w->filemenu = TRUE;
  73. w->maxlines = w->body.maxlines;
  74. w->autoindent = globalautoindent;
  75. if(clone){
  76. w->dirty = clone->dirty;
  77. textsetselect(&w->body, clone->body.q0, clone->body.q1);
  78. winsettag(w);
  79. w->autoindent = clone->autoindent;
  80. }
  81. }
  82. int
  83. delrunepos(Window *w)
  84. {
  85. int n;
  86. Rune rune;
  87. for(n=0; n<w->tag.file->nc; n++) {
  88. bufread(w->tag.file, n, &rune, 1);
  89. if(rune == ' ')
  90. break;
  91. }
  92. n += 2;
  93. if(n >= w->tag.file->nc)
  94. return -1;
  95. return n;
  96. }
  97. void
  98. movetodel(Window *w)
  99. {
  100. int n;
  101. n = delrunepos(w);
  102. if(n < 0)
  103. return;
  104. moveto(mousectl, addpt(frptofchar(&w->tag, n), Pt(4, w->tag.font->height-4)));
  105. }
  106. int
  107. winresize(Window *w, Rectangle r, int safe)
  108. {
  109. Rectangle r1;
  110. int y;
  111. Image *b;
  112. Rectangle br;
  113. r1 = r;
  114. r1.max.y = r1.min.y + font->height;
  115. y = r1.max.y;
  116. if(!safe || !eqrect(w->tag.r, r1)){
  117. y = textresize(&w->tag, r1);
  118. b = button;
  119. if(w->body.file->mod && !w->isdir && !w->isscratch)
  120. b = modbutton;
  121. br.min = w->tag.scrollr.min;
  122. br.max.x = br.min.x + Dx(b->r);
  123. br.max.y = br.min.y + Dy(b->r);
  124. draw(screen, br, b, nil, b->r.min);
  125. }
  126. if(!safe || !eqrect(w->body.r, r1)){
  127. if(y+1+font->height > r.max.y){ /* no body */
  128. r1.min.y = y;
  129. r1.max.y = y;
  130. textresize(&w->body, r1);
  131. w->r = r;
  132. w->r.max.y = y;
  133. return y;
  134. }
  135. r1 = r;
  136. r1.min.y = y;
  137. r1.max.y = y + 1;
  138. draw(screen, r1, tagcols[BORD], nil, ZP);
  139. r1.min.y = y + 1;
  140. r1.max.y = r.max.y;
  141. y = textresize(&w->body, r1);
  142. w->r = r;
  143. w->r.max.y = y;
  144. textscrdraw(&w->body);
  145. }
  146. w->maxlines = min(w->body.nlines, max(w->maxlines, w->body.maxlines));
  147. return w->r.max.y;
  148. }
  149. void
  150. winlock1(Window *w, int owner)
  151. {
  152. incref(w);
  153. qlock(w);
  154. w->owner = owner;
  155. }
  156. void
  157. winlock(Window *w, int owner)
  158. {
  159. int i;
  160. File *f;
  161. f = w->body.file;
  162. for(i=0; i<f->ntext; i++)
  163. winlock1(f->text[i]->w, owner);
  164. }
  165. void
  166. winunlock(Window *w)
  167. {
  168. int i;
  169. File *f;
  170. /*
  171. * subtle: loop runs backwards to avoid tripping over
  172. * winclose indirectly editing f->text and freeing f
  173. * on the last iteration of the loop.
  174. */
  175. f = w->body.file;
  176. for(i=f->ntext-1; i>=0; i--){
  177. w = f->text[i]->w;
  178. w->owner = 0;
  179. qunlock(w);
  180. winclose(w);
  181. }
  182. }
  183. void
  184. winmousebut(Window *w)
  185. {
  186. moveto(mousectl, divpt(addpt(w->tag.scrollr.min, w->tag.scrollr.max), 2));
  187. }
  188. void
  189. windirfree(Window *w)
  190. {
  191. int i;
  192. Dirlist *dl;
  193. if(w->isdir){
  194. for(i=0; i<w->ndl; i++){
  195. dl = w->dlp[i];
  196. free(dl->r);
  197. free(dl);
  198. }
  199. free(w->dlp);
  200. }
  201. w->dlp = nil;
  202. w->ndl = 0;
  203. }
  204. void
  205. winclose(Window *w)
  206. {
  207. int i;
  208. if(decref(w) == 0){
  209. windirfree(w);
  210. textclose(&w->tag);
  211. textclose(&w->body);
  212. if(activewin == w)
  213. activewin = nil;
  214. for(i=0; i<w->nincl; i++)
  215. free(w->incl[i]);
  216. free(w->incl);
  217. free(w->events);
  218. free(w);
  219. }
  220. }
  221. void
  222. windelete(Window *w)
  223. {
  224. Xfid *x;
  225. x = w->eventx;
  226. if(x){
  227. w->nevents = 0;
  228. free(w->events);
  229. w->events = nil;
  230. w->eventx = nil;
  231. sendp(x->c, nil); /* wake him up */
  232. }
  233. }
  234. void
  235. winundo(Window *w, int isundo)
  236. {
  237. Text *body;
  238. int i;
  239. File *f;
  240. Window *v;
  241. w->utflastqid = -1;
  242. body = &w->body;
  243. fileundo(body->file, isundo, &body->q0, &body->q1);
  244. textshow(body, body->q0, body->q1, 1);
  245. f = body->file;
  246. for(i=0; i<f->ntext; i++){
  247. v = f->text[i]->w;
  248. v->dirty = (f->seq != v->putseq);
  249. if(v != w){
  250. v->body.q0 = v->body.p0+v->body.org;
  251. v->body.q1 = v->body.p1+v->body.org;
  252. }
  253. }
  254. winsettag(w);
  255. }
  256. void
  257. winsetname(Window *w, Rune *name, int n)
  258. {
  259. Text *t;
  260. Window *v;
  261. int i;
  262. t = &w->body;
  263. if(runeeq(t->file->name, t->file->nname, name, n) == TRUE)
  264. return;
  265. w->isscratch = FALSE;
  266. if(n>=6 && runeeq(L"/guide", 6, name+(n-6), 6))
  267. w->isscratch = TRUE;
  268. else if(n>=7 && runeeq(L"+Errors", 7, name+(n-7), 7))
  269. w->isscratch = TRUE;
  270. filesetname(t->file, name, n);
  271. for(i=0; i<t->file->ntext; i++){
  272. v = t->file->text[i]->w;
  273. winsettag(v);
  274. v->isscratch = w->isscratch;
  275. }
  276. }
  277. void
  278. wintype(Window *w, Text *t, Rune r)
  279. {
  280. int i;
  281. texttype(t, r);
  282. if(t->what == Body)
  283. for(i=0; i<t->file->ntext; i++)
  284. textscrdraw(t->file->text[i]);
  285. winsettag(w);
  286. }
  287. void
  288. wincleartag(Window *w)
  289. {
  290. int i, n;
  291. Rune *r;
  292. /* w must be committed */
  293. n = w->tag.file->nc;
  294. r = runemalloc(n);
  295. bufread(w->tag.file, 0, r, n);
  296. for(i=0; i<n; i++)
  297. if(r[i]==' ' || r[i]=='\t')
  298. break;
  299. for(; i<n; i++)
  300. if(r[i] == '|')
  301. break;
  302. if(i == n)
  303. return;
  304. i++;
  305. textdelete(&w->tag, i, n, TRUE);
  306. free(r);
  307. w->tag.file->mod = FALSE;
  308. if(w->tag.q0 > i)
  309. w->tag.q0 = i;
  310. if(w->tag.q1 > i)
  311. w->tag.q1 = i;
  312. textsetselect(&w->tag, w->tag.q0, w->tag.q1);
  313. }
  314. void
  315. winsettag1(Window *w)
  316. {
  317. int i, j, k, n, bar, dirty;
  318. Rune *new, *old, *r;
  319. Image *b;
  320. uint q0, q1;
  321. Rectangle br;
  322. /* there are races that get us here with stuff in the tag cache, so we take extra care to sync it */
  323. if(w->tag.ncache!=0 || w->tag.file->mod)
  324. wincommit(w, &w->tag); /* check file name; also guarantees we can modify tag contents */
  325. old = runemalloc(w->tag.file->nc+1);
  326. bufread(w->tag.file, 0, old, w->tag.file->nc);
  327. old[w->tag.file->nc] = '\0';
  328. for(i=0; i<w->tag.file->nc; i++)
  329. if(old[i]==' ' || old[i]=='\t')
  330. break;
  331. if(runeeq(old, i, w->body.file->name, w->body.file->nname) == FALSE){
  332. textdelete(&w->tag, 0, i, TRUE);
  333. textinsert(&w->tag, 0, w->body.file->name, w->body.file->nname, TRUE);
  334. free(old);
  335. old = runemalloc(w->tag.file->nc+1);
  336. bufread(w->tag.file, 0, old, w->tag.file->nc);
  337. old[w->tag.file->nc] = '\0';
  338. }
  339. new = runemalloc(w->body.file->nname+100);
  340. i = 0;
  341. runemove(new+i, w->body.file->name, w->body.file->nname);
  342. i += w->body.file->nname;
  343. runemove(new+i, L" Del Snarf", 10);
  344. i += 10;
  345. if(w->filemenu){
  346. if(w->body.file->delta.nc>0 || w->body.ncache){
  347. runemove(new+i, L" Undo", 5);
  348. i += 5;
  349. }
  350. if(w->body.file->epsilon.nc > 0){
  351. runemove(new+i, L" Redo", 5);
  352. i += 5;
  353. }
  354. dirty = w->body.file->nname && (w->body.ncache || w->body.file->seq!=w->putseq);
  355. if(!w->isdir && dirty){
  356. runemove(new+i, L" Put", 4);
  357. i += 4;
  358. }
  359. }
  360. if(w->isdir){
  361. runemove(new+i, L" Get", 4);
  362. i += 4;
  363. }
  364. runemove(new+i, L" |", 2);
  365. i += 2;
  366. r = runestrchr(old, '|');
  367. if(r)
  368. k = r-old+1;
  369. else{
  370. k = w->tag.file->nc;
  371. if(w->body.file->seq == 0){
  372. runemove(new+i, L" Look ", 6);
  373. i += 6;
  374. }
  375. }
  376. if(runeeq(new, i, old, k) == FALSE){
  377. n = k;
  378. if(n > i)
  379. n = i;
  380. for(j=0; j<n; j++)
  381. if(old[j] != new[j])
  382. break;
  383. q0 = w->tag.q0;
  384. q1 = w->tag.q1;
  385. textdelete(&w->tag, j, k, TRUE);
  386. textinsert(&w->tag, j, new+j, i-j, TRUE);
  387. /* try to preserve user selection */
  388. r = runestrchr(old, '|');
  389. if(r){
  390. bar = r-old;
  391. if(q0 > bar){
  392. bar = (runestrchr(new, '|')-new)-bar;
  393. w->tag.q0 = q0+bar;
  394. w->tag.q1 = q1+bar;
  395. }
  396. }
  397. }
  398. free(old);
  399. free(new);
  400. w->tag.file->mod = FALSE;
  401. n = w->tag.file->nc+w->tag.ncache;
  402. if(w->tag.q0 > n)
  403. w->tag.q0 = n;
  404. if(w->tag.q1 > n)
  405. w->tag.q1 = n;
  406. textsetselect(&w->tag, w->tag.q0, w->tag.q1);
  407. b = button;
  408. if(!w->isdir && !w->isscratch && (w->body.file->mod || w->body.ncache))
  409. b = modbutton;
  410. br.min = w->tag.scrollr.min;
  411. br.max.x = br.min.x + Dx(b->r);
  412. br.max.y = br.min.y + Dy(b->r);
  413. draw(screen, br, b, nil, b->r.min);
  414. }
  415. void
  416. winsettag(Window *w)
  417. {
  418. int i;
  419. File *f;
  420. Window *v;
  421. f = w->body.file;
  422. for(i=0; i<f->ntext; i++){
  423. v = f->text[i]->w;
  424. if(v->col->safe || v->body.maxlines>0)
  425. winsettag1(v);
  426. }
  427. }
  428. void
  429. wincommit(Window *w, Text *t)
  430. {
  431. Rune *r;
  432. int i;
  433. File *f;
  434. textcommit(t, TRUE);
  435. f = t->file;
  436. if(f->ntext > 1)
  437. for(i=0; i<f->ntext; i++)
  438. textcommit(f->text[i], FALSE); /* no-op for t */
  439. if(t->what == Body)
  440. return;
  441. r = runemalloc(w->tag.file->nc);
  442. bufread(w->tag.file, 0, r, w->tag.file->nc);
  443. for(i=0; i<w->tag.file->nc; i++)
  444. if(r[i]==' ' || r[i]=='\t')
  445. break;
  446. if(runeeq(r, i, w->body.file->name, w->body.file->nname) == FALSE){
  447. seq++;
  448. filemark(w->body.file);
  449. w->body.file->mod = TRUE;
  450. w->dirty = TRUE;
  451. winsetname(w, r, i);
  452. winsettag(w);
  453. }
  454. free(r);
  455. }
  456. void
  457. winaddincl(Window *w, Rune *r, int n)
  458. {
  459. char *a;
  460. Dir *d;
  461. Runestr rs;
  462. a = runetobyte(r, n);
  463. d = dirstat(a);
  464. if(d == nil){
  465. if(a[0] == '/')
  466. goto Rescue;
  467. rs = dirname(&w->body, r, n);
  468. r = rs.r;
  469. n = rs.nr;
  470. free(a);
  471. a = runetobyte(r, n);
  472. d = dirstat(a);
  473. if(d == nil)
  474. goto Rescue;
  475. r = runerealloc(r, n+1);
  476. r[n] = 0;
  477. }
  478. free(a);
  479. if((d->qid.type&QTDIR) == 0){
  480. free(d);
  481. warning(nil, "%s: not a directory\n", a);
  482. free(r);
  483. return;
  484. }
  485. free(d);
  486. w->nincl++;
  487. w->incl = realloc(w->incl, w->nincl*sizeof(Rune*));
  488. memmove(w->incl+1, w->incl, (w->nincl-1)*sizeof(Rune*));
  489. w->incl[0] = runemalloc(n+1);
  490. runemove(w->incl[0], r, n);
  491. free(r);
  492. return;
  493. Rescue:
  494. warning(nil, "%s: %r\n", a);
  495. free(r);
  496. free(a);
  497. return;
  498. }
  499. int
  500. winclean(Window *w, int conservative) /* as it stands, conservative is always TRUE */
  501. {
  502. if(w->isscratch || w->isdir) /* don't whine if it's a guide file, error window, etc. */
  503. return TRUE;
  504. if(!conservative && w->nopen[QWevent]>0)
  505. return TRUE;
  506. if(w->dirty){
  507. if(w->body.file->nname)
  508. warning(nil, "%.*S modified\n", w->body.file->nname, w->body.file->name);
  509. else{
  510. if(w->body.file->nc < 100) /* don't whine if it's too small */
  511. return TRUE;
  512. warning(nil, "unnamed file modified\n");
  513. }
  514. w->dirty = FALSE;
  515. return FALSE;
  516. }
  517. return TRUE;
  518. }
  519. char*
  520. winctlprint(Window *w, char *buf, int fonts)
  521. {
  522. sprint(buf, "%11d %11d %11d %11d %11d ", w->id, w->tag.file->nc,
  523. w->body.file->nc, w->isdir, w->dirty);
  524. if(fonts)
  525. return smprint("%s%11d %q %11d " , buf, Dx(w->body.r),
  526. w->body.reffont->f->name, w->body.maxtab);
  527. return buf;
  528. }
  529. void
  530. winevent(Window *w, char *fmt, ...)
  531. {
  532. int n;
  533. char *b;
  534. Xfid *x;
  535. va_list arg;
  536. if(w->nopen[QWevent] == 0)
  537. return;
  538. if(w->owner == 0)
  539. error("no window owner");
  540. va_start(arg, fmt);
  541. b = vsmprint(fmt, arg);
  542. va_end(arg);
  543. if(b == nil)
  544. error("vsmprint failed");
  545. n = strlen(b);
  546. w->events = realloc(w->events, w->nevents+1+n);
  547. w->events[w->nevents++] = w->owner;
  548. memmove(w->events+w->nevents, b, n);
  549. free(b);
  550. w->nevents += n;
  551. x = w->eventx;
  552. if(x){
  553. w->eventx = nil;
  554. sendp(x->c, nil);
  555. }
  556. }