util.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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. static Point prevmouse;
  22. static Window *mousew;
  23. void
  24. cvttorunes(char *p, int n, Rune *r, int *nb, int *nr, int *nulls)
  25. {
  26. uint8_t *q;
  27. Rune *s;
  28. int j, w;
  29. /*
  30. * Always guaranteed that n bytes may be interpreted
  31. * without worrying about partial runes. This may mean
  32. * reading up to UTFmax-1 more bytes than n; the caller
  33. * knows this. If n is a firm limit, the caller should
  34. * set p[n] = 0.
  35. */
  36. q = (uint8_t*)p;
  37. s = r;
  38. for(j=0; j<n; j+=w){
  39. if(*q < Runeself){
  40. w = 1;
  41. *s = *q++;
  42. }else{
  43. w = chartorune(s, (char*)q);
  44. q += w;
  45. }
  46. if(*s)
  47. s++;
  48. else if(nulls)
  49. *nulls = TRUE;
  50. }
  51. *nb = (char*)q-p;
  52. *nr = s-r;
  53. }
  54. void
  55. error(char *s)
  56. {
  57. fprint(2, "acme: %s: %r\n", s);
  58. remove(acmeerrorfile);
  59. abort();
  60. }
  61. Window*
  62. errorwin1(Rune *dir, int ndir, Rune **incl, int nincl)
  63. {
  64. Window *w;
  65. Rune *r;
  66. int i, n;
  67. r = runemalloc(ndir+8);
  68. if((n = ndir) != 0){
  69. runemove(r, dir, ndir);
  70. r[n++] = L'/';
  71. }
  72. runemove(r+n, L"+Errors", 7);
  73. n += 7;
  74. w = lookfile(r, n);
  75. if(w == nil){
  76. if(row.ncol == 0)
  77. if(rowadd(&row, nil, -1) == nil)
  78. error("can't create column to make error window");
  79. w = coladd(row.col[row.ncol-1], nil, nil, -1);
  80. w->filemenu = FALSE;
  81. winsetname(w, r, n);
  82. }
  83. free(r);
  84. for(i=nincl; --i>=0; ){
  85. n = runestrlen(incl[i]);
  86. r = runemalloc(n);
  87. runemove(r, incl[i], n);
  88. winaddincl(w, r, n);
  89. }
  90. w->autoindent = globalautoindent;
  91. return w;
  92. }
  93. /* make new window, if necessary; return with it locked */
  94. Window*
  95. errorwin(Mntdir *md, int owner)
  96. {
  97. Window *w;
  98. for(;;){
  99. if(md == nil)
  100. w = errorwin1(nil, 0, nil, 0);
  101. else
  102. w = errorwin1(md->dir, md->ndir, md->incl, md->nincl);
  103. winlock(w, owner);
  104. if(w->col != nil)
  105. break;
  106. /* window was deleted too fast */
  107. winunlock(w);
  108. }
  109. return w;
  110. }
  111. /*
  112. * Incoming window should be locked.
  113. * It will be unlocked and returned window
  114. * will be locked in its place.
  115. */
  116. Window*
  117. errorwinforwin(Window *w)
  118. {
  119. int i, n, nincl, owner;
  120. Rune **incl;
  121. Runestr dir;
  122. Text *t;
  123. t = &w->body;
  124. dir = dirname(t, nil, 0);
  125. if(dir.nr==1 && dir.r[0]=='.'){ /* sigh */
  126. free(dir.r);
  127. dir.r = nil;
  128. dir.nr = 0;
  129. }
  130. incl = nil;
  131. nincl = w->nincl;
  132. if(nincl > 0){
  133. incl = emalloc(nincl*sizeof(Rune*));
  134. for(i=0; i<nincl; i++){
  135. n = runestrlen(w->incl[i]);
  136. incl[i] = runemalloc(n+1);
  137. runemove(incl[i], w->incl[i], n);
  138. }
  139. }
  140. owner = w->owner;
  141. winunlock(w);
  142. for(;;){
  143. w = errorwin1(dir.r, dir.nr, incl, nincl);
  144. winlock(w, owner);
  145. if(w->col != nil)
  146. break;
  147. /* window deleted too fast */
  148. winunlock(w);
  149. }
  150. return w;
  151. }
  152. typedef struct Warning Warning;
  153. struct Warning{
  154. Mntdir *md;
  155. Buffer buf;
  156. Warning *next;
  157. };
  158. static Warning *warnings;
  159. static
  160. void
  161. addwarningtext(Mntdir *md, Rune *r, int nr)
  162. {
  163. Warning *warn;
  164. for(warn = warnings; warn; warn=warn->next){
  165. if(warn->md == md){
  166. bufinsert(&warn->buf, warn->buf.nc, r, nr);
  167. return;
  168. }
  169. }
  170. warn = emalloc(sizeof(Warning));
  171. warn->next = warnings;
  172. warn->md = md;
  173. if(md)
  174. fsysincid(md);
  175. warnings = warn;
  176. bufinsert(&warn->buf, 0, r, nr);
  177. nbsendp(cwarn, 0);
  178. }
  179. /* called while row is locked */
  180. void
  181. flushwarnings(void)
  182. {
  183. Warning *warn, *next;
  184. Window *w;
  185. Text *t;
  186. int owner, nr, q0, n;
  187. Rune *r;
  188. for(warn=warnings; warn; warn=next) {
  189. w = errorwin(warn->md, 'E');
  190. t = &w->body;
  191. owner = w->owner;
  192. if(owner == 0)
  193. w->owner = 'E';
  194. wincommit(w, t);
  195. /*
  196. * Most commands don't generate much output. For instance,
  197. * Edit ,>cat goes through /dev/cons and is already in blocks
  198. * because of the i/o system, but a few can. Edit ,p will
  199. * put the entire result into a single hunk. So it's worth doing
  200. * this in blocks (and putting the text in a buffer in the first
  201. * place), to avoid a big memory footprint.
  202. */
  203. r = fbufalloc();
  204. q0 = t->file->Buffer.nc;
  205. for(n = 0; n < warn->buf.nc; n += nr){
  206. nr = warn->buf.nc - n;
  207. if(nr > RBUFSIZE)
  208. nr = RBUFSIZE;
  209. bufread(&warn->buf, n, r, nr);
  210. textbsinsert(t, t->file->Buffer.nc, r, nr, TRUE, &nr);
  211. }
  212. textshow(t, q0, t->file->Buffer.nc, 1);
  213. free(r);
  214. winsettag(t->w);
  215. textscrdraw(t);
  216. w->owner = owner;
  217. w->dirty = FALSE;
  218. winunlock(w);
  219. bufclose(&warn->buf);
  220. next = warn->next;
  221. if(warn->md)
  222. fsysdelid(warn->md);
  223. free(warn);
  224. }
  225. warnings = nil;
  226. }
  227. void
  228. warning(Mntdir *md, char *s, ...)
  229. {
  230. Rune *r;
  231. va_list arg;
  232. va_start(arg, s);
  233. r = runevsmprint(s, arg);
  234. va_end(arg);
  235. if(r == nil)
  236. error("runevsmprint failed");
  237. addwarningtext(md, r, runestrlen(r));
  238. free(r);
  239. }
  240. int
  241. runeeq(Rune *s1, uint n1, Rune *s2, uint n2)
  242. {
  243. if(n1 != n2)
  244. return FALSE;
  245. return memcmp(s1, s2, n1*sizeof(Rune)) == 0;
  246. }
  247. uint
  248. min(uint a, uint b)
  249. {
  250. if(a < b)
  251. return a;
  252. return b;
  253. }
  254. uint
  255. max(uint a, uint b)
  256. {
  257. if(a > b)
  258. return a;
  259. return b;
  260. }
  261. char*
  262. runetobyte(Rune *r, int n)
  263. {
  264. char *s;
  265. if(r == nil)
  266. return nil;
  267. s = emalloc(n*UTFmax+1);
  268. setmalloctag(s, getcallerpc());
  269. snprint(s, n*UTFmax+1, "%.*S", n, r);
  270. return s;
  271. }
  272. Rune*
  273. bytetorune(char *s, int *ip)
  274. {
  275. Rune *r;
  276. int nb, nr;
  277. nb = strlen(s);
  278. r = runemalloc(nb+1);
  279. cvttorunes(s, nb, r, &nb, &nr, nil);
  280. r[nr] = '\0';
  281. *ip = nr;
  282. return r;
  283. }
  284. int
  285. isalnum(Rune c)
  286. {
  287. /*
  288. * Hard to get absolutely right. Use what we know about ASCII
  289. * and assume anything above the Latin control characters is
  290. * potentially an alphanumeric.
  291. *
  292. * Treat 0xA0 (non-breaking space) as a special alphanumeric
  293. * character [sape]
  294. */
  295. if(c <= ' ')
  296. return FALSE;
  297. if(0x7F<=c && c<0xA0)
  298. return FALSE;
  299. if(utfrune("!\"#$%&'()*+,-./:;<=>?@[\\]^`{|}~", c))
  300. return FALSE;
  301. return TRUE;
  302. }
  303. int
  304. rgetc(void *v, uint n)
  305. {
  306. return ((Rune*)v)[n];
  307. }
  308. int
  309. tgetc(void *a, uint n)
  310. {
  311. Text *t;
  312. t = a;
  313. if(n >= t->file->Buffer.nc)
  314. return 0;
  315. return textreadc(t, n);
  316. }
  317. Rune*
  318. skipbl(Rune *r, int n, int *np)
  319. {
  320. while(n>0 && (*r==' ' || *r=='\t' || *r=='\n')){
  321. --n;
  322. r++;
  323. }
  324. *np = n;
  325. return r;
  326. }
  327. Rune*
  328. findbl(Rune *r, int n, int *np)
  329. {
  330. while(n>0 && *r!=' ' && *r!='\t' && *r!='\n'){
  331. --n;
  332. r++;
  333. }
  334. *np = n;
  335. return r;
  336. }
  337. void
  338. savemouse(Window *w)
  339. {
  340. prevmouse = mouse->xy;
  341. mousew = w;
  342. }
  343. int
  344. restoremouse(Window *w)
  345. {
  346. int did;
  347. did = 0;
  348. if(mousew!=nil && mousew==w){
  349. moveto(mousectl, prevmouse);
  350. did = 1;
  351. }
  352. mousew = nil;
  353. return did;
  354. }
  355. void
  356. clearmouse()
  357. {
  358. mousew = nil;
  359. }
  360. char*
  361. estrdup(char *s)
  362. {
  363. char *t;
  364. t = strdup(s);
  365. if(t == nil)
  366. error("strdup failed");
  367. setmalloctag(t, getcallerpc());
  368. return t;
  369. }
  370. void*
  371. emalloc(uint n)
  372. {
  373. void *p;
  374. p = malloc(n);
  375. if(p == nil)
  376. error("malloc failed");
  377. setmalloctag(p, getcallerpc());
  378. memset(p, 0, n);
  379. return p;
  380. }
  381. void*
  382. erealloc(void *p, uint n)
  383. {
  384. p = realloc(p, n);
  385. if(p == nil)
  386. error("realloc failed");
  387. setmalloctag(p, getcallerpc());
  388. return p;
  389. }
  390. /*
  391. * Heuristic city.
  392. */
  393. Window*
  394. makenewwindow(Text *t)
  395. {
  396. Column *c;
  397. Window *w, *bigw, *emptyw;
  398. Text *emptyb;
  399. int i, y, el;
  400. if(activecol)
  401. c = activecol;
  402. else if(seltext && seltext->col)
  403. c = seltext->col;
  404. else if(t && t->col)
  405. c = t->col;
  406. else{
  407. if(row.ncol==0 && rowadd(&row, nil, -1)==nil)
  408. error("can't make column");
  409. c = row.col[row.ncol-1];
  410. }
  411. activecol = c;
  412. if(t==nil || t->w==nil || c->nw==0)
  413. return coladd(c, nil, nil, -1);
  414. /* find biggest window and biggest blank spot */
  415. emptyw = c->w[0];
  416. bigw = emptyw;
  417. for(i=1; i<c->nw; i++){
  418. w = c->w[i];
  419. /* use >= to choose one near bottom of screen */
  420. if(w->body.Frame.maxlines >= bigw->body.Frame.maxlines)
  421. bigw = w;
  422. if(w->body.Frame.maxlines-w->body.Frame.nlines >=
  423. emptyw->body.Frame.maxlines-emptyw->body.Frame.nlines)
  424. emptyw = w;
  425. }
  426. emptyb = &emptyw->body;
  427. el = emptyb->Frame.maxlines-emptyb->Frame.nlines;
  428. /* if empty space is big, use it */
  429. if(el>15 || (el>3 && el>(bigw->body.Frame.maxlines-1)/2))
  430. y = emptyb->Frame.r.min.y+emptyb->Frame.nlines*font->height;
  431. else{
  432. /* if this window is in column and isn't much smaller, split it */
  433. if(t->col==c && Dy(t->w->r)>2*Dy(bigw->r)/3)
  434. bigw = t->w;
  435. y = (bigw->r.min.y + bigw->r.max.y)/2;
  436. }
  437. w = coladd(c, nil, nil, y);
  438. if(w->body.Frame.maxlines < 2)
  439. colgrow(w->col, w, 1);
  440. return w;
  441. }