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) != 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 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 for(warn=warnings; warn; warn=next) {
175 w = errorwin(warn->md, 'E');
176 t = &w->body;
177 owner = w->owner;
178 if(owner == 0)
179 w->owner = 'E';
180 wincommit(w, t);
181 /*
182 * Most commands don't generate much output. For instance,
183 * Edit ,>cat goes through /dev/cons and is already in blocks
184 * because of the i/o system, but a few can. Edit ,p will
185 * put the entire result into a single hunk. So it's worth doing
186 * this in blocks (and putting the text in a buffer in the first
187 * place), to avoid a big memory footprint.
188 */
189 r = fbufalloc();
190 q0 = t->file->b.nc;
191 for(n = 0; n < warn->buf.nc; n += nr){
192 nr = warn->buf.nc - n;
193 if(nr > RBUFSIZE)
194 nr = RBUFSIZE;
195 bufread(&warn->buf, n, r, nr);
196 textbsinsert(t, t->file->b.nc, r, nr, TRUE, &nr);
198 textshow(t, q0, t->file->b.nc, 1);
199 free(r);
200 winsettag(t->w);
201 textscrdraw(t);
202 w->owner = owner;
203 w->dirty = FALSE;
204 winunlock(w);
205 bufclose(&warn->buf);
206 next = warn->next;
207 if(warn->md)
208 fsysdelid(warn->md);
209 free(warn);
211 warnings = nil;
214 void
215 warning(Mntdir *md, char *s, ...)
217 Rune *r;
218 va_list arg;
220 va_start(arg, s);
221 r = runevsmprint(s, arg);
222 va_end(arg);
223 if(r == nil)
224 error("runevsmprint failed");
225 addwarningtext(md, r, runestrlen(r));
228 int
229 runeeq(Rune *s1, uint n1, Rune *s2, uint n2)
231 if(n1 != n2)
232 return FALSE;
233 return memcmp(s1, s2, n1*sizeof(Rune)) == 0;
236 uint
237 min(uint a, uint b)
239 if(a < b)
240 return a;
241 return b;
244 uint
245 max(uint a, uint b)
247 if(a > b)
248 return a;
249 return b;
252 char*
253 runetobyte(Rune *r, int n)
255 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;
265 Rune*
266 bytetorune(char *s, int *ip)
268 Rune *r;
269 int nb, nr;
271 nb = strlen(s);
272 r = runemalloc(nb+1);
273 cvttorunes(s, nb, r, &nb, &nr, nil);
274 r[nr] = '\0';
275 *ip = nr;
276 return r;
279 int
280 isalnum(Rune c)
282 /*
283 * Hard to get absolutely right. Use what we know about ASCII
284 * and assume anything above the Latin control characters is
285 * potentially an alphanumeric.
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;
296 int
297 rgetc(void *v, uint n)
299 return ((Rune*)v)[n];
302 int
303 tgetc(void *a, uint n)
305 Text *t;
307 t = a;
308 if(n >= t->file->b.nc)
309 return 0;
310 return textreadc(t, n);
313 Rune*
314 skipbl(Rune *r, int n, int *np)
316 while(n>0 && (*r==' ' || *r=='\t' || *r=='\n')){
317 --n;
318 r++;
320 *np = n;
321 return r;
324 Rune*
325 findbl(Rune *r, int n, int *np)
327 while(n>0 && *r!=' ' && *r!='\t' && *r!='\n'){
328 --n;
329 r++;
331 *np = n;
332 return r;
335 void
336 savemouse(Window *w)
338 prevmouse = mouse->xy;
339 mousew = w;
342 void
343 restoremouse(Window *w)
345 if(mousew!=nil && mousew==w)
346 moveto(mousectl, prevmouse);
347 mousew = nil;
350 void
351 clearmouse()
353 mousew = nil;
356 char*
357 estrdup(char *s)
359 char *t;
361 t = strdup(s);
362 if(t == nil)
363 error("strdup failed");
364 setmalloctag(t, getcallerpc(&s));
365 return t;
368 void*
369 emalloc(uint n)
371 void *p;
373 p = malloc(n);
374 if(p == nil){
375 fprint(2, "allocating %d from %lux: %r\n", n, getcallerpc(&n));
376 *(int*)0=0;
377 error("malloc failed");
379 setmalloctag(p, getcallerpc(&n));
380 memset(p, 0, n);
381 return p;
384 void*
385 erealloc(void *p, uint n)
387 p = realloc(p, n);
388 if(p == nil){
389 fprint(2, "reallocating %d: %r\n", n);
390 error("realloc failed");
392 setmalloctag(p, getcallerpc(&n));
393 return p;
396 /*
397 * Heuristic city.
398 */
399 Window*
400 makenewwindow(Text *t)
402 Column *c;
403 Window *w, *bigw, *emptyw;
404 Text *emptyb;
405 int i, y, el;
407 if(activecol)
408 c = activecol;
409 else if(seltext && seltext->col)
410 c = seltext->col;
411 else if(t && t->col)
412 c = t->col;
413 else{
414 if(row.ncol==0 && rowadd(&row, nil, -1)==nil)
415 error("can't make column");
416 c = row.col[row.ncol-1];
418 activecol = c;
419 if(t==nil || t->w==nil || c->nw==0)
420 return coladd(c, nil, nil, -1);
422 /* find biggest window and biggest blank spot */
423 emptyw = c->w[0];
424 bigw = emptyw;
425 for(i=1; i<c->nw; i++){
426 w = c->w[i];
427 /* use >= to choose one near bottom of screen */
428 if(w->body.fr.maxlines >= bigw->body.fr.maxlines)
429 bigw = w;
430 if(w->body.fr.maxlines-w->body.fr.nlines >= emptyw->body.fr.maxlines-emptyw->body.fr.nlines)
431 emptyw = w;
433 emptyb = &emptyw->body;
434 el = emptyb->fr.maxlines-emptyb->fr.nlines;
435 /* if empty space is big, use it */
436 if(el>15 || (el>3 && el>(bigw->body.fr.maxlines-1)/2))
437 y = emptyb->fr.r.min.y+emptyb->fr.nlines*font->height;
438 else{
439 /* if this window is in column and isn't much smaller, split it */
440 if(t->col==c && Dy(t->w->r)>2*Dy(bigw->r)/3)
441 bigw = t->w;
442 y = (bigw->r.min.y + bigw->r.max.y)/2;
444 w = coladd(c, nil, nil, y);
445 if(w->body.fr.maxlines < 2)
446 colgrow(w->col, w, 1);
447 return w;