wind.c 12 KB

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