Blob


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"
14 static Point prevmouse;
15 static Window *mousew;
17 Range
18 range(int q0, int q1)
19 {
20 Range r;
22 r.q0 = q0;
23 r.q1 = q1;
24 return r;
25 }
27 Runestr
28 runestr(Rune *r, uint n)
29 {
30 Runestr rs;
32 rs.r = r;
33 rs.nr = n;
34 return rs;
35 }
37 void
38 cvttorunes(char *p, int n, Rune *r, int *nb, int *nr, int *nulls)
39 {
40 uchar *q;
41 Rune *s;
42 int j, w;
44 /*
45 * Always guaranteed that n bytes may be interpreted
46 * without worrying about partial runes. This may mean
47 * reading up to UTFmax-1 more bytes than n; the caller
48 * knows this. If n is a firm limit, the caller should
49 * set p[n] = 0.
50 */
51 q = (uchar*)p;
52 s = r;
53 for(j=0; j<n; j+=w){
54 if(*q < Runeself){
55 w = 1;
56 *s = *q++;
57 }else{
58 w = chartorune(s, (char*)q);
59 q += w;
60 }
61 if(*s)
62 s++;
63 else if(nulls)
64 *nulls = TRUE;
65 }
66 *nb = (char*)q-p;
67 *nr = s-r;
68 }
70 void
71 error(char *s)
72 {
73 fprint(2, "acme: %s: %r\n", s);
74 threadexitsall(nil);
75 }
77 Window*
78 errorwin1(Rune *dir, int ndir, Rune **incl, int nincl)
79 {
80 Window *w;
81 Rune *r;
82 int i, n;
83 static Rune Lpluserrors[] = { '+', 'E', 'r', 'r', 'o', 'r', 's', 0 };
85 r = runemalloc(ndir+8);
86 if((n = ndir) != 0){
87 runemove(r, dir, ndir);
88 r[n++] = L'/';
89 }
90 runemove(r+n, Lpluserrors, 7);
91 n += 7;
92 w = lookfile(r, n);
93 if(w == nil){
94 if(row.ncol == 0)
95 if(rowadd(&row, nil, -1) == nil)
96 error("can't create column to make error window");
97 w = coladd(row.col[row.ncol-1], nil, nil, -1);
98 w->filemenu = FALSE;
99 winsetname(w, r, n);
101 free(r);
102 for(i=nincl; --i>=0; ){
103 n = runestrlen(incl[i]);
104 r = runemalloc(n);
105 runemove(r, incl[i], n);
106 winaddincl(w, r, n);
108 w->autoindent = globalautoindent;
109 return w;
112 /* make new window, if necessary; return with it locked */
113 Window*
114 errorwin(Mntdir *md, int owner)
116 Window *w;
118 for(;;){
119 if(md == nil)
120 w = errorwin1(nil, 0, nil, 0);
121 else
122 w = errorwin1(md->dir, md->ndir, md->incl, md->nincl);
123 winlock(w, owner);
124 if(w->col != nil)
125 break;
126 /* window was deleted too fast */
127 winunlock(w);
129 return w;
132 /*
133 * Incoming window should be locked.
134 * It will be unlocked and returned window
135 * will be locked in its place.
136 */
137 Window*
138 errorwinforwin(Window *w)
140 int i, n, nincl, owner;
141 Rune **incl;
142 Runestr dir;
143 Text *t;
145 t = &w->body;
146 dir = dirname(t, nil, 0);
147 if(dir.nr==1 && dir.r[0]=='.'){ /* sigh */
148 free(dir.r);
149 dir.r = nil;
150 dir.nr = 0;
152 incl = nil;
153 nincl = w->nincl;
154 if(nincl > 0){
155 incl = emalloc(nincl*sizeof(Rune*));
156 for(i=0; i<nincl; i++){
157 n = runestrlen(w->incl[i]);
158 incl[i] = runemalloc(n+1);
159 runemove(incl[i], w->incl[i], n);
162 owner = w->owner;
163 winunlock(w);
164 for(;;){
165 w = errorwin1(dir.r, dir.nr, incl, nincl);
166 winlock(w, owner);
167 if(w->col != nil)
168 break;
169 /* window deleted too fast */
170 winunlock(w);
172 return w;
175 typedef struct Warning Warning;
177 struct Warning{
178 Mntdir *md;
179 Buffer buf;
180 Warning *next;
181 };
183 static Warning *warnings;
185 static
186 void
187 addwarningtext(Mntdir *md, Rune *r, int nr)
189 Warning *warn;
191 for(warn = warnings; warn; warn=warn->next){
192 if(warn->md == md){
193 bufinsert(&warn->buf, warn->buf.nc, r, nr);
194 return;
197 warn = emalloc(sizeof(Warning));
198 warn->next = warnings;
199 warn->md = md;
200 if(md)
201 fsysincid(md);
202 warnings = warn;
203 bufinsert(&warn->buf, 0, r, nr);
204 nbsendp(cwarn, 0);
207 /* called while row is locked */
208 void
209 flushwarnings(void)
211 Warning *warn, *next;
212 Window *w;
213 Text *t;
214 int owner, nr, q0, n;
215 Rune *r;
217 for(warn=warnings; warn; warn=next) {
218 w = errorwin(warn->md, 'E');
219 t = &w->body;
220 owner = w->owner;
221 if(owner == 0)
222 w->owner = 'E';
223 wincommit(w, t);
224 /*
225 * Most commands don't generate much output. For instance,
226 * Edit ,>cat goes through /dev/cons and is already in blocks
227 * because of the i/o system, but a few can. Edit ,p will
228 * put the entire result into a single hunk. So it's worth doing
229 * this in blocks (and putting the text in a buffer in the first
230 * place), to avoid a big memory footprint.
231 */
232 r = fbufalloc();
233 q0 = t->file->b.nc;
234 for(n = 0; n < warn->buf.nc; n += nr){
235 nr = warn->buf.nc - n;
236 if(nr > RBUFSIZE)
237 nr = RBUFSIZE;
238 bufread(&warn->buf, n, r, nr);
239 textbsinsert(t, t->file->b.nc, r, nr, TRUE, &nr);
241 textshow(t, q0, t->file->b.nc, 1);
242 free(r);
243 winsettag(t->w);
244 textscrdraw(t);
245 w->owner = owner;
246 w->dirty = FALSE;
247 winunlock(w);
248 bufclose(&warn->buf);
249 next = warn->next;
250 if(warn->md)
251 fsysdelid(warn->md);
252 free(warn);
254 warnings = nil;
257 void
258 warning(Mntdir *md, char *s, ...)
260 Rune *r;
261 va_list arg;
263 va_start(arg, s);
264 r = runevsmprint(s, arg);
265 va_end(arg);
266 if(r == nil)
267 error("runevsmprint failed");
268 addwarningtext(md, r, runestrlen(r));
269 free(r);
272 int
273 runeeq(Rune *s1, uint n1, Rune *s2, uint n2)
275 if(n1 != n2)
276 return FALSE;
277 return memcmp(s1, s2, n1*sizeof(Rune)) == 0;
280 uint
281 min(uint a, uint b)
283 if(a < b)
284 return a;
285 return b;
288 uint
289 max(uint a, uint b)
291 if(a > b)
292 return a;
293 return b;
296 char*
297 runetobyte(Rune *r, int n)
299 char *s;
301 if(r == nil)
302 return nil;
303 s = emalloc(n*UTFmax+1);
304 setmalloctag(s, getcallerpc(&r));
305 snprint(s, n*UTFmax+1, "%.*S", n, r);
306 return s;
309 Rune*
310 bytetorune(char *s, int *ip)
312 Rune *r;
313 int nb, nr;
315 nb = strlen(s);
316 r = runemalloc(nb+1);
317 cvttorunes(s, nb, r, &nb, &nr, nil);
318 r[nr] = '\0';
319 *ip = nr;
320 return r;
323 int
324 isalnum(Rune c)
326 /*
327 * Hard to get absolutely right. Use what we know about ASCII
328 * and assume anything above the Latin control characters is
329 * potentially an alphanumeric.
330 */
331 if(c <= ' ')
332 return FALSE;
333 if(0x7F<=c && c<=0xA0)
334 return FALSE;
335 if(utfrune("!\"#$%&'()*+,-./:;<=>?@[\\]^`{|}~", c))
336 return FALSE;
337 return TRUE;
340 int
341 rgetc(void *v, uint n)
343 return ((Rune*)v)[n];
346 int
347 tgetc(void *a, uint n)
349 Text *t;
351 t = a;
352 if(n >= t->file->b.nc)
353 return 0;
354 return textreadc(t, n);
357 Rune*
358 skipbl(Rune *r, int n, int *np)
360 while(n>0 && (*r==' ' || *r=='\t' || *r=='\n')){
361 --n;
362 r++;
364 *np = n;
365 return r;
368 Rune*
369 findbl(Rune *r, int n, int *np)
371 while(n>0 && *r!=' ' && *r!='\t' && *r!='\n'){
372 --n;
373 r++;
375 *np = n;
376 return r;
379 void
380 savemouse(Window *w)
382 prevmouse = mouse->xy;
383 mousew = w;
386 void
387 restoremouse(Window *w)
389 if(mousew!=nil && mousew==w)
390 moveto(mousectl, prevmouse);
391 mousew = nil;
394 void
395 clearmouse()
397 mousew = nil;
400 char*
401 estrdup(char *s)
403 char *t;
405 t = strdup(s);
406 if(t == nil)
407 error("strdup failed");
408 setmalloctag(t, getcallerpc(&s));
409 return t;
412 void*
413 emalloc(uint n)
415 void *p;
417 p = malloc(n);
418 if(p == nil)
419 error("malloc failed");
420 setmalloctag(p, getcallerpc(&n));
421 memset(p, 0, n);
422 return p;
425 void*
426 erealloc(void *p, uint n)
428 p = realloc(p, n);
429 if(p == nil)
430 error("realloc failed");
431 setmalloctag(p, getcallerpc(&n));
432 return p;
435 /*
436 * Heuristic city.
437 */
438 Window*
439 makenewwindow(Text *t)
441 Column *c;
442 Window *w, *bigw, *emptyw;
443 Text *emptyb;
444 int i, y, el;
446 if(activecol)
447 c = activecol;
448 else if(seltext && seltext->col)
449 c = seltext->col;
450 else if(t && t->col)
451 c = t->col;
452 else{
453 if(row.ncol==0 && rowadd(&row, nil, -1)==nil)
454 error("can't make column");
455 c = row.col[row.ncol-1];
457 activecol = c;
458 if(t==nil || t->w==nil || c->nw==0)
459 return coladd(c, nil, nil, -1);
461 /* find biggest window and biggest blank spot */
462 emptyw = c->w[0];
463 bigw = emptyw;
464 for(i=1; i<c->nw; i++){
465 w = c->w[i];
466 /* use >= to choose one near bottom of screen */
467 if(w->body.fr.maxlines >= bigw->body.fr.maxlines)
468 bigw = w;
469 if(w->body.fr.maxlines-w->body.fr.nlines >= emptyw->body.fr.maxlines-emptyw->body.fr.nlines)
470 emptyw = w;
472 emptyb = &emptyw->body;
473 el = emptyb->fr.maxlines-emptyb->fr.nlines;
474 /* if empty space is big, use it */
475 if(el>15 || (el>3 && el>(bigw->body.fr.maxlines-1)/2))
476 y = emptyb->fr.r.min.y+emptyb->fr.nlines*font->height;
477 else{
478 /* if this window is in column and isn't much smaller, split it */
479 if(t->col==c && Dy(t->w->r)>2*Dy(bigw->r)/3)
480 bigw = t->w;
481 y = (bigw->r.min.y + bigw->r.max.y)/2;
483 w = coladd(c, nil, nil, y);
484 if(w->body.fr.maxlines < 2)
485 colgrow(w->col, w, 1);
486 return w;