util.c 7.9 KB

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