util.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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. typedef struct Warning Warning;
  104. struct Warning{
  105. Mntdir *md;
  106. Buffer buf;
  107. Warning *next;
  108. };
  109. static Warning *warnings;
  110. static
  111. void
  112. addwarningtext(Mntdir *md, Rune *r, int nr)
  113. {
  114. Warning *warn;
  115. for(warn = warnings; warn; warn=warn->next){
  116. if(warn->md == md){
  117. bufinsert(&warn->buf, warn->buf.nc, r, nr);
  118. return;
  119. }
  120. }
  121. warn = emalloc(sizeof(Warning));
  122. warn->next = warnings;
  123. warnings = warn;
  124. bufinsert(&warn->buf, 0, r, nr);
  125. }
  126. /* called while row is locked */
  127. void
  128. flushwarnings(void)
  129. {
  130. Warning *warn, *next;
  131. Window *w;
  132. Text *t;
  133. int owner, nr, q0, n;
  134. Rune *r;
  135. if(row.ncol == 0){ /* really early error */
  136. rowinit(&row, screen->clipr);
  137. rowadd(&row, nil, -1);
  138. rowadd(&row, nil, -1);
  139. if(row.ncol == 0)
  140. error("initializing columns in flushwarnings()");
  141. }
  142. for(warn=warnings; warn; warn=next) {
  143. w = errorwin(warn->md, 'E');
  144. t = &w->body;
  145. owner = w->owner;
  146. if(owner == 0)
  147. w->owner = 'E';
  148. wincommit(w, t);
  149. /*
  150. * Most commands don't generate much output. For instance,
  151. * Edit ,>cat goes through /dev/cons and is already in blocks
  152. * because of the i/o system, but a few can. Edit ,p will
  153. * put the entire result into a single hunk. So it's worth doing
  154. * this in blocks (and putting the text in a buffer in the first
  155. * place), to avoid a big memory footprint.
  156. */
  157. r = fbufalloc();
  158. q0 = t->file->nc;
  159. for(n = 0; n < warn->buf.nc; n += nr){
  160. nr = warn->buf.nc - n;
  161. if(nr > RBUFSIZE)
  162. nr = RBUFSIZE;
  163. bufread(&warn->buf, n, r, nr);
  164. textbsinsert(t, t->file->nc, r, nr, TRUE, &nr);
  165. }
  166. textshow(t, q0, t->file->nc, 1);
  167. free(r);
  168. winsettag(t->w);
  169. textscrdraw(t);
  170. w->owner = owner;
  171. w->dirty = FALSE;
  172. winunlock(w);
  173. bufclose(&warn->buf);
  174. next = warn->next;
  175. free(warn);
  176. }
  177. warnings = nil;
  178. }
  179. void
  180. warning(Mntdir *md, char *s, ...)
  181. {
  182. Rune *r;
  183. va_list arg;
  184. va_start(arg, s);
  185. r = runevsmprint(s, arg);
  186. va_end(arg);
  187. if(r == nil)
  188. error("runevsmprint failed");
  189. addwarningtext(md, r, runestrlen(r));
  190. }
  191. int
  192. runeeq(Rune *s1, uint n1, Rune *s2, uint n2)
  193. {
  194. if(n1 != n2)
  195. return FALSE;
  196. return memcmp(s1, s2, n1*sizeof(Rune)) == 0;
  197. }
  198. uint
  199. min(uint a, uint b)
  200. {
  201. if(a < b)
  202. return a;
  203. return b;
  204. }
  205. uint
  206. max(uint a, uint b)
  207. {
  208. if(a > b)
  209. return a;
  210. return b;
  211. }
  212. char*
  213. runetobyte(Rune *r, int n)
  214. {
  215. char *s;
  216. if(r == nil)
  217. return nil;
  218. s = emalloc(n*UTFmax+1);
  219. setmalloctag(s, getcallerpc(&r));
  220. snprint(s, n*UTFmax+1, "%.*S", n, r);
  221. return s;
  222. }
  223. Rune*
  224. bytetorune(char *s, int *ip)
  225. {
  226. Rune *r;
  227. int nb, nr;
  228. nb = strlen(s);
  229. r = runemalloc(nb+1);
  230. cvttorunes(s, nb, r, &nb, &nr, nil);
  231. r[nr] = '\0';
  232. *ip = nr;
  233. return r;
  234. }
  235. int
  236. isalnum(Rune c)
  237. {
  238. /*
  239. * Hard to get absolutely right. Use what we know about ASCII
  240. * and assume anything above the Latin control characters is
  241. * potentially an alphanumeric.
  242. */
  243. if(c <= ' ')
  244. return FALSE;
  245. if(0x7F<=c && c<=0xA0)
  246. return FALSE;
  247. if(utfrune("!\"#$%&'()*+,-./:;<=>?@[\\]^`{|}~", c))
  248. return FALSE;
  249. return TRUE;
  250. }
  251. int
  252. rgetc(void *v, uint n)
  253. {
  254. return ((Rune*)v)[n];
  255. }
  256. int
  257. tgetc(void *a, uint n)
  258. {
  259. Text *t;
  260. t = a;
  261. if(n >= t->file->nc)
  262. return 0;
  263. return textreadc(t, n);
  264. }
  265. Rune*
  266. skipbl(Rune *r, int n, int *np)
  267. {
  268. while(n>0 && (*r==' ' || *r=='\t' || *r=='\n')){
  269. --n;
  270. r++;
  271. }
  272. *np = n;
  273. return r;
  274. }
  275. Rune*
  276. findbl(Rune *r, int n, int *np)
  277. {
  278. while(n>0 && *r!=' ' && *r!='\t' && *r!='\n'){
  279. --n;
  280. r++;
  281. }
  282. *np = n;
  283. return r;
  284. }
  285. void
  286. savemouse(Window *w)
  287. {
  288. prevmouse = mouse->xy;
  289. mousew = w;
  290. }
  291. void
  292. restoremouse(Window *w)
  293. {
  294. if(mousew!=nil && mousew==w)
  295. moveto(mousectl, prevmouse);
  296. mousew = nil;
  297. }
  298. void
  299. clearmouse()
  300. {
  301. mousew = nil;
  302. }
  303. char*
  304. estrdup(char *s)
  305. {
  306. char *t;
  307. t = strdup(s);
  308. if(t == nil)
  309. error("strdup failed");
  310. setmalloctag(t, getcallerpc(&s));
  311. return t;
  312. }
  313. void*
  314. emalloc(uint n)
  315. {
  316. void *p;
  317. p = malloc(n);
  318. if(p == nil)
  319. error("malloc failed");
  320. setmalloctag(p, getcallerpc(&n));
  321. memset(p, 0, n);
  322. return p;
  323. }
  324. void*
  325. erealloc(void *p, uint n)
  326. {
  327. p = realloc(p, n);
  328. if(p == nil)
  329. error("realloc failed");
  330. setmalloctag(p, getcallerpc(&n));
  331. return p;
  332. }
  333. /*
  334. * Heuristic city.
  335. */
  336. Window*
  337. makenewwindow(Text *t)
  338. {
  339. Column *c;
  340. Window *w, *bigw, *emptyw;
  341. Text *emptyb;
  342. int i, y, el;
  343. if(activecol)
  344. c = activecol;
  345. else if(seltext && seltext->col)
  346. c = seltext->col;
  347. else if(t && t->col)
  348. c = t->col;
  349. else{
  350. if(row.ncol==0 && rowadd(&row, nil, -1)==nil)
  351. error("can't make column");
  352. c = row.col[row.ncol-1];
  353. }
  354. activecol = c;
  355. if(t==nil || t->w==nil || c->nw==0)
  356. return coladd(c, nil, nil, -1);
  357. /* find biggest window and biggest blank spot */
  358. emptyw = c->w[0];
  359. bigw = emptyw;
  360. for(i=1; i<c->nw; i++){
  361. w = c->w[i];
  362. /* use >= to choose one near bottom of screen */
  363. if(w->body.maxlines >= bigw->body.maxlines)
  364. bigw = w;
  365. if(w->body.maxlines-w->body.nlines >= emptyw->body.maxlines-emptyw->body.nlines)
  366. emptyw = w;
  367. }
  368. emptyb = &emptyw->body;
  369. el = emptyb->maxlines-emptyb->nlines;
  370. /* if empty space is big, use it */
  371. if(el>15 || (el>3 && el>(bigw->body.maxlines-1)/2))
  372. y = emptyb->r.min.y+emptyb->nlines*font->height;
  373. else{
  374. /* if this window is in column and isn't much smaller, split it */
  375. if(t->col==c && Dy(t->w->r)>2*Dy(bigw->r)/3)
  376. bigw = t->w;
  377. y = (bigw->r.min.y + bigw->r.max.y)/2;
  378. }
  379. w = coladd(c, nil, nil, y);
  380. if(w->body.maxlines < 2)
  381. colgrow(w->col, w, 1);
  382. return w;
  383. }