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 abort();
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){ /* assign = */
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 typedef struct Warning Warning;
134 struct Warning{
135 Mntdir *md;
136 Buffer buf;
137 Warning *next;
138 };
140 static Warning *warnings;
142 static
143 void
144 addwarningtext(Mntdir *md, Rune *r, int nr)
146 Warning *warn;
148 for(warn = warnings; warn; warn=warn->next){
149 if(warn->md == md){
150 bufinsert(&warn->buf, warn->buf.nc, r, nr);
151 return;
154 warn = emalloc(sizeof(Warning));
155 warn->next = warnings;
156 warn->md = md;
157 if(md)
158 fsysincid(md);
159 warnings = warn;
160 bufinsert(&warn->buf, 0, r, nr);
161 nbsendp(cwarn, 0);
164 /* called while row is locked */
165 void
166 flushwarnings(void)
168 Warning *warn, *next;
169 Window *w;
170 Text *t;
171 int owner, nr, q0, n;
172 Rune *r;
174 if(row.ncol == 0){ /* really early error */
175 rowinit(&row, screen->clipr);
176 rowadd(&row, nil, -1);
177 rowadd(&row, nil, -1);
178 if(row.ncol == 0)
179 error("initializing columns in flushwarnings()");
182 for(warn=warnings; warn; warn=next) {
183 w = errorwin(warn->md, 'E');
184 t = &w->body;
185 owner = w->owner;
186 if(owner == 0)
187 w->owner = 'E';
188 wincommit(w, t);
189 /*
190 * Most commands don't generate much output. For instance,
191 * Edit ,>cat goes through /dev/cons and is already in blocks
192 * because of the i/o system, but a few can. Edit ,p will
193 * put the entire result into a single hunk. So it's worth doing
194 * this in blocks (and putting the text in a buffer in the first
195 * place), to avoid a big memory footprint.
196 */
197 r = fbufalloc();
198 q0 = t->file->b.nc;
199 for(n = 0; n < warn->buf.nc; n += nr){
200 nr = warn->buf.nc - n;
201 if(nr > RBUFSIZE)
202 nr = RBUFSIZE;
203 bufread(&warn->buf, n, r, nr);
204 textbsinsert(t, t->file->b.nc, r, nr, TRUE, &nr);
206 textshow(t, q0, t->file->b.nc, 1);
207 free(r);
208 winsettag(t->w);
209 textscrdraw(t);
210 w->owner = owner;
211 w->dirty = FALSE;
212 winunlock(w);
213 bufclose(&warn->buf);
214 next = warn->next;
215 if(warn->md)
216 fsysdelid(warn->md);
217 free(warn);
219 warnings = nil;
222 void
223 warning(Mntdir *md, char *s, ...)
225 Rune *r;
226 va_list arg;
228 va_start(arg, s);
229 r = runevsmprint(s, arg);
230 va_end(arg);
231 if(r == nil)
232 error("runevsmprint failed");
233 addwarningtext(md, r, runestrlen(r));
236 int
237 runeeq(Rune *s1, uint n1, Rune *s2, uint n2)
239 if(n1 != n2)
240 return FALSE;
241 return memcmp(s1, s2, n1*sizeof(Rune)) == 0;
244 uint
245 min(uint a, uint b)
247 if(a < b)
248 return a;
249 return b;
252 uint
253 max(uint a, uint b)
255 if(a > b)
256 return a;
257 return b;
260 char*
261 runetobyte(Rune *r, int n)
263 char *s;
265 if(r == nil)
266 return nil;
267 s = emalloc(n*UTFmax+1);
268 setmalloctag(s, getcallerpc(&r));
269 snprint(s, n*UTFmax+1, "%.*S", n, r);
270 return s;
273 Rune*
274 bytetorune(char *s, int *ip)
276 Rune *r;
277 int nb, nr;
279 nb = strlen(s);
280 r = runemalloc(nb+1);
281 cvttorunes(s, nb, r, &nb, &nr, nil);
282 r[nr] = '\0';
283 *ip = nr;
284 return r;
287 int
288 isalnum(Rune c)
290 /*
291 * Hard to get absolutely right. Use what we know about ASCII
292 * and assume anything above the Latin control characters is
293 * potentially an alphanumeric.
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;
304 int
305 rgetc(void *v, uint n)
307 return ((Rune*)v)[n];
310 int
311 tgetc(void *a, uint n)
313 Text *t;
315 t = a;
316 if(n >= t->file->b.nc)
317 return 0;
318 return textreadc(t, n);
321 Rune*
322 skipbl(Rune *r, int n, int *np)
324 while(n>0 && (*r==' ' || *r=='\t' || *r=='\n')){
325 --n;
326 r++;
328 *np = n;
329 return r;
332 Rune*
333 findbl(Rune *r, int n, int *np)
335 while(n>0 && *r!=' ' && *r!='\t' && *r!='\n'){
336 --n;
337 r++;
339 *np = n;
340 return r;
343 void
344 savemouse(Window *w)
346 prevmouse = mouse->xy;
347 mousew = w;
350 void
351 restoremouse(Window *w)
353 if(mousew!=nil && mousew==w)
354 moveto(mousectl, prevmouse);
355 mousew = nil;
358 void
359 clearmouse()
361 mousew = nil;
364 char*
365 estrdup(char *s)
367 char *t;
369 t = strdup(s);
370 if(t == nil)
371 error("strdup failed");
372 setmalloctag(t, getcallerpc(&s));
373 return t;
376 void*
377 emalloc(uint n)
379 void *p;
381 p = malloc(n);
382 if(p == nil){
383 fprint(2, "allocating %d from %lux: %r\n", n, getcallerpc(&n));
384 *(int*)0=0;
385 error("malloc failed");
387 setmalloctag(p, getcallerpc(&n));
388 memset(p, 0, n);
389 return p;
392 void*
393 erealloc(void *p, uint n)
395 p = realloc(p, n);
396 if(p == nil){
397 fprint(2, "reallocating %d: %r\n", n);
398 error("realloc failed");
400 setmalloctag(p, getcallerpc(&n));
401 return p;
404 /*
405 * Heuristic city.
406 */
407 Window*
408 makenewwindow(Text *t)
410 Column *c;
411 Window *w, *bigw, *emptyw;
412 Text *emptyb;
413 int i, y, el;
415 if(activecol)
416 c = activecol;
417 else if(seltext && seltext->col)
418 c = seltext->col;
419 else if(t && t->col)
420 c = t->col;
421 else{
422 if(row.ncol==0 && rowadd(&row, nil, -1)==nil)
423 error("can't make column");
424 c = row.col[row.ncol-1];
426 activecol = c;
427 if(t==nil || t->w==nil || c->nw==0)
428 return coladd(c, nil, nil, -1);
430 /* find biggest window and biggest blank spot */
431 emptyw = c->w[0];
432 bigw = emptyw;
433 for(i=1; i<c->nw; i++){
434 w = c->w[i];
435 /* use >= to choose one near bottom of screen */
436 if(w->body.fr.maxlines >= bigw->body.fr.maxlines)
437 bigw = w;
438 if(w->body.fr.maxlines-w->body.fr.nlines >= emptyw->body.fr.maxlines-emptyw->body.fr.nlines)
439 emptyw = w;
441 emptyb = &emptyw->body;
442 el = emptyb->fr.maxlines-emptyb->fr.nlines;
443 /* if empty space is big, use it */
444 if(el>15 || (el>3 && el>(bigw->body.fr.maxlines-1)/2))
445 y = emptyb->fr.r.min.y+emptyb->fr.nlines*font->height;
446 else{
447 /* if this window is in column and isn't much smaller, split it */
448 if(t->col==c && Dy(t->w->r)>2*Dy(bigw->r)/3)
449 bigw = t->w;
450 y = (bigw->r.min.y + bigw->r.max.y)/2;
452 w = coladd(c, nil, nil, y);
453 if(w->body.fr.maxlines < 2)
454 colgrow(w->col, w, 1);
455 return w;