1 b3994ec5 2003-12-11 devnull #include <u.h>
2 b3994ec5 2003-12-11 devnull #include <libc.h>
3 b3994ec5 2003-12-11 devnull #include <draw.h>
4 b3994ec5 2003-12-11 devnull #include <thread.h>
5 b3994ec5 2003-12-11 devnull #include <cursor.h>
6 b3994ec5 2003-12-11 devnull #include <mouse.h>
7 b3994ec5 2003-12-11 devnull #include <keyboard.h>
8 b3994ec5 2003-12-11 devnull #include <frame.h>
9 b3994ec5 2003-12-11 devnull #include <fcall.h>
10 b3994ec5 2003-12-11 devnull #include <plumb.h>
11 b3994ec5 2003-12-11 devnull #include "dat.h"
12 b3994ec5 2003-12-11 devnull #include "fns.h"
14 b3994ec5 2003-12-11 devnull static Point prevmouse;
15 b3994ec5 2003-12-11 devnull static Window *mousew;
18 be22ae2d 2004-03-26 devnull range(int q0, int q1)
22 be22ae2d 2004-03-26 devnull r.q0 = q0;
23 be22ae2d 2004-03-26 devnull r.q1 = q1;
24 be22ae2d 2004-03-26 devnull return r;
28 8ad51794 2004-03-25 devnull runestr(Rune *r, uint n)
30 8ad51794 2004-03-25 devnull Runestr rs;
32 8ad51794 2004-03-25 devnull rs.r = r;
33 8ad51794 2004-03-25 devnull rs.nr = n;
34 8ad51794 2004-03-25 devnull return rs;
38 b3994ec5 2003-12-11 devnull cvttorunes(char *p, int n, Rune *r, int *nb, int *nr, int *nulls)
40 b3994ec5 2003-12-11 devnull uchar *q;
42 b3994ec5 2003-12-11 devnull int j, w;
45 b3994ec5 2003-12-11 devnull * Always guaranteed that n bytes may be interpreted
46 b3994ec5 2003-12-11 devnull * without worrying about partial runes. This may mean
47 b3994ec5 2003-12-11 devnull * reading up to UTFmax-1 more bytes than n; the caller
48 b3994ec5 2003-12-11 devnull * knows this. If n is a firm limit, the caller should
49 b3994ec5 2003-12-11 devnull * set p[n] = 0.
51 b3994ec5 2003-12-11 devnull q = (uchar*)p;
53 b3994ec5 2003-12-11 devnull for(j=0; j<n; j+=w){
54 b3994ec5 2003-12-11 devnull if(*q < Runeself){
56 b3994ec5 2003-12-11 devnull *s = *q++;
58 b3994ec5 2003-12-11 devnull w = chartorune(s, (char*)q);
63 b3994ec5 2003-12-11 devnull else if(nulls)
64 b3994ec5 2003-12-11 devnull *nulls = TRUE;
66 b3994ec5 2003-12-11 devnull *nb = (char*)q-p;
67 b3994ec5 2003-12-11 devnull *nr = s-r;
71 b3994ec5 2003-12-11 devnull error(char *s)
73 b3994ec5 2003-12-11 devnull fprint(2, "acme: %s: %r\n", s);
78 b3994ec5 2003-12-11 devnull errorwin1(Rune *dir, int ndir, Rune **incl, int nincl)
80 b3994ec5 2003-12-11 devnull Window *w;
82 b3994ec5 2003-12-11 devnull int i, n;
83 b3994ec5 2003-12-11 devnull static Rune Lpluserrors[] = { '+', 'E', 'r', 'r', 'o', 'r', 's', 0 };
85 5a8e63b2 2004-02-29 devnull r = runemalloc(ndir+8);
86 6d7fdb24 2004-12-27 devnull if((n = ndir) != 0){
87 b3994ec5 2003-12-11 devnull runemove(r, dir, ndir);
88 5a8e63b2 2004-02-29 devnull r[n++] = L'/';
90 b3994ec5 2003-12-11 devnull runemove(r+n, Lpluserrors, 7);
92 b3994ec5 2003-12-11 devnull w = lookfile(r, n);
93 b3994ec5 2003-12-11 devnull if(w == nil){
94 b3994ec5 2003-12-11 devnull if(row.ncol == 0)
95 b3994ec5 2003-12-11 devnull if(rowadd(&row, nil, -1) == nil)
96 b3994ec5 2003-12-11 devnull error("can't create column to make error window");
97 b3994ec5 2003-12-11 devnull w = coladd(row.col[row.ncol-1], nil, nil, -1);
98 b3994ec5 2003-12-11 devnull w->filemenu = FALSE;
99 b3994ec5 2003-12-11 devnull winsetname(w, r, n);
101 b3994ec5 2003-12-11 devnull free(r);
102 b3994ec5 2003-12-11 devnull for(i=nincl; --i>=0; ){
103 b3994ec5 2003-12-11 devnull n = runestrlen(incl[i]);
104 b3994ec5 2003-12-11 devnull r = runemalloc(n);
105 b3994ec5 2003-12-11 devnull runemove(r, incl[i], n);
106 b3994ec5 2003-12-11 devnull winaddincl(w, r, n);
108 5a8e63b2 2004-02-29 devnull w->autoindent = globalautoindent;
109 b3994ec5 2003-12-11 devnull return w;
112 b3994ec5 2003-12-11 devnull /* make new window, if necessary; return with it locked */
114 5a8e63b2 2004-02-29 devnull errorwin(Mntdir *md, int owner)
116 b3994ec5 2003-12-11 devnull Window *w;
118 b3994ec5 2003-12-11 devnull for(;;){
119 b3994ec5 2003-12-11 devnull if(md == nil)
120 b3994ec5 2003-12-11 devnull w = errorwin1(nil, 0, nil, 0);
122 b3994ec5 2003-12-11 devnull w = errorwin1(md->dir, md->ndir, md->incl, md->nincl);
123 5a8e63b2 2004-02-29 devnull winlock(w, owner);
124 b3994ec5 2003-12-11 devnull if(w->col != nil)
126 b3994ec5 2003-12-11 devnull /* window was deleted too fast */
127 5a8e63b2 2004-02-29 devnull winunlock(w);
129 b3994ec5 2003-12-11 devnull return w;
132 5a8e63b2 2004-02-29 devnull typedef struct Warning Warning;
134 5a8e63b2 2004-02-29 devnull struct Warning{
135 5a8e63b2 2004-02-29 devnull Mntdir *md;
136 5a8e63b2 2004-02-29 devnull Buffer buf;
137 5a8e63b2 2004-02-29 devnull Warning *next;
140 5a8e63b2 2004-02-29 devnull static Warning *warnings;
144 5a8e63b2 2004-02-29 devnull addwarningtext(Mntdir *md, Rune *r, int nr)
146 5a8e63b2 2004-02-29 devnull Warning *warn;
148 5a8e63b2 2004-02-29 devnull for(warn = warnings; warn; warn=warn->next){
149 5a8e63b2 2004-02-29 devnull if(warn->md == md){
150 5a8e63b2 2004-02-29 devnull bufinsert(&warn->buf, warn->buf.nc, r, nr);
154 5a8e63b2 2004-02-29 devnull warn = emalloc(sizeof(Warning));
155 5a8e63b2 2004-02-29 devnull warn->next = warnings;
156 8ad51794 2004-03-25 devnull warn->md = md;
158 8ad51794 2004-03-25 devnull fsysincid(md);
159 5a8e63b2 2004-02-29 devnull warnings = warn;
160 5a8e63b2 2004-02-29 devnull bufinsert(&warn->buf, 0, r, nr);
161 8ad51794 2004-03-25 devnull nbsendp(cwarn, 0);
164 8ad51794 2004-03-25 devnull /* called while row is locked */
166 8ad51794 2004-03-25 devnull flushwarnings(void)
168 5a8e63b2 2004-02-29 devnull Warning *warn, *next;
169 b3994ec5 2003-12-11 devnull Window *w;
170 b3994ec5 2003-12-11 devnull Text *t;
171 5a8e63b2 2004-02-29 devnull int owner, nr, q0, n;
172 5a8e63b2 2004-02-29 devnull Rune *r;
174 5a8e63b2 2004-02-29 devnull for(warn=warnings; warn; warn=next) {
175 5a8e63b2 2004-02-29 devnull w = errorwin(warn->md, 'E');
176 5a8e63b2 2004-02-29 devnull t = &w->body;
177 5a8e63b2 2004-02-29 devnull owner = w->owner;
178 5a8e63b2 2004-02-29 devnull if(owner == 0)
179 5a8e63b2 2004-02-29 devnull w->owner = 'E';
180 5a8e63b2 2004-02-29 devnull wincommit(w, t);
182 5a8e63b2 2004-02-29 devnull * Most commands don't generate much output. For instance,
183 5a8e63b2 2004-02-29 devnull * Edit ,>cat goes through /dev/cons and is already in blocks
184 5a8e63b2 2004-02-29 devnull * because of the i/o system, but a few can. Edit ,p will
185 5a8e63b2 2004-02-29 devnull * put the entire result into a single hunk. So it's worth doing
186 5a8e63b2 2004-02-29 devnull * this in blocks (and putting the text in a buffer in the first
187 5a8e63b2 2004-02-29 devnull * place), to avoid a big memory footprint.
189 5a8e63b2 2004-02-29 devnull r = fbufalloc();
190 5a8e63b2 2004-02-29 devnull q0 = t->file->b.nc;
191 5a8e63b2 2004-02-29 devnull for(n = 0; n < warn->buf.nc; n += nr){
192 5a8e63b2 2004-02-29 devnull nr = warn->buf.nc - n;
193 5a8e63b2 2004-02-29 devnull if(nr > RBUFSIZE)
194 5a8e63b2 2004-02-29 devnull nr = RBUFSIZE;
195 5a8e63b2 2004-02-29 devnull bufread(&warn->buf, n, r, nr);
196 5a8e63b2 2004-02-29 devnull textbsinsert(t, t->file->b.nc, r, nr, TRUE, &nr);
198 5a8e63b2 2004-02-29 devnull textshow(t, q0, t->file->b.nc, 1);
199 5a8e63b2 2004-02-29 devnull free(r);
200 5a8e63b2 2004-02-29 devnull winsettag(t->w);
201 5a8e63b2 2004-02-29 devnull textscrdraw(t);
202 5a8e63b2 2004-02-29 devnull w->owner = owner;
203 5a8e63b2 2004-02-29 devnull w->dirty = FALSE;
204 b3994ec5 2003-12-11 devnull winunlock(w);
205 5a8e63b2 2004-02-29 devnull bufclose(&warn->buf);
206 5a8e63b2 2004-02-29 devnull next = warn->next;
207 8ad51794 2004-03-25 devnull if(warn->md)
208 8ad51794 2004-03-25 devnull fsysdelid(warn->md);
209 5a8e63b2 2004-02-29 devnull free(warn);
211 5a8e63b2 2004-02-29 devnull warnings = nil;
215 b3994ec5 2003-12-11 devnull warning(Mntdir *md, char *s, ...)
217 b3994ec5 2003-12-11 devnull Rune *r;
218 b3994ec5 2003-12-11 devnull va_list arg;
220 b3994ec5 2003-12-11 devnull va_start(arg, s);
221 b3994ec5 2003-12-11 devnull r = runevsmprint(s, arg);
222 b3994ec5 2003-12-11 devnull va_end(arg);
223 5a8e63b2 2004-02-29 devnull if(r == nil)
224 5a8e63b2 2004-02-29 devnull error("runevsmprint failed");
225 5a8e63b2 2004-02-29 devnull addwarningtext(md, r, runestrlen(r));
229 b3994ec5 2003-12-11 devnull runeeq(Rune *s1, uint n1, Rune *s2, uint n2)
231 b3994ec5 2003-12-11 devnull if(n1 != n2)
232 b3994ec5 2003-12-11 devnull return FALSE;
233 b3994ec5 2003-12-11 devnull return memcmp(s1, s2, n1*sizeof(Rune)) == 0;
237 b3994ec5 2003-12-11 devnull min(uint a, uint b)
239 b3994ec5 2003-12-11 devnull if(a < b)
240 b3994ec5 2003-12-11 devnull return a;
241 b3994ec5 2003-12-11 devnull return b;
245 b3994ec5 2003-12-11 devnull max(uint a, uint b)
247 b3994ec5 2003-12-11 devnull if(a > b)
248 b3994ec5 2003-12-11 devnull return a;
249 b3994ec5 2003-12-11 devnull return b;
253 b3994ec5 2003-12-11 devnull runetobyte(Rune *r, int n)
255 b3994ec5 2003-12-11 devnull char *s;
257 b3994ec5 2003-12-11 devnull if(r == nil)
258 b3994ec5 2003-12-11 devnull return nil;
259 b3994ec5 2003-12-11 devnull s = emalloc(n*UTFmax+1);
260 b3994ec5 2003-12-11 devnull setmalloctag(s, getcallerpc(&r));
261 b3994ec5 2003-12-11 devnull snprint(s, n*UTFmax+1, "%.*S", n, r);
262 b3994ec5 2003-12-11 devnull return s;
266 b3994ec5 2003-12-11 devnull bytetorune(char *s, int *ip)
268 b3994ec5 2003-12-11 devnull Rune *r;
269 b3994ec5 2003-12-11 devnull int nb, nr;
271 b3994ec5 2003-12-11 devnull nb = strlen(s);
272 b3994ec5 2003-12-11 devnull r = runemalloc(nb+1);
273 b3994ec5 2003-12-11 devnull cvttorunes(s, nb, r, &nb, &nr, nil);
274 b3994ec5 2003-12-11 devnull r[nr] = '\0';
275 b3994ec5 2003-12-11 devnull *ip = nr;
276 b3994ec5 2003-12-11 devnull return r;
280 b3994ec5 2003-12-11 devnull isalnum(Rune c)
283 b3994ec5 2003-12-11 devnull * Hard to get absolutely right. Use what we know about ASCII
284 b3994ec5 2003-12-11 devnull * and assume anything above the Latin control characters is
285 b3994ec5 2003-12-11 devnull * potentially an alphanumeric.
287 b3994ec5 2003-12-11 devnull if(c <= ' ')
288 b3994ec5 2003-12-11 devnull return FALSE;
289 b3994ec5 2003-12-11 devnull if(0x7F<=c && c<=0xA0)
290 b3994ec5 2003-12-11 devnull return FALSE;
291 b3994ec5 2003-12-11 devnull if(utfrune("!\"#$%&'()*+,-./:;<=>?@[\\]^`{|}~", c))
292 b3994ec5 2003-12-11 devnull return FALSE;
293 b3994ec5 2003-12-11 devnull return TRUE;
297 b3994ec5 2003-12-11 devnull rgetc(void *v, uint n)
299 b3994ec5 2003-12-11 devnull return ((Rune*)v)[n];
303 b3994ec5 2003-12-11 devnull tgetc(void *a, uint n)
305 b3994ec5 2003-12-11 devnull Text *t;
308 b3994ec5 2003-12-11 devnull if(n >= t->file->b.nc)
309 b3994ec5 2003-12-11 devnull return 0;
310 b3994ec5 2003-12-11 devnull return textreadc(t, n);
314 b3994ec5 2003-12-11 devnull skipbl(Rune *r, int n, int *np)
316 15680d56 2004-03-05 devnull while(n>0 && (*r==' ' || *r=='\t' || *r=='\n')){
320 b3994ec5 2003-12-11 devnull *np = n;
321 b3994ec5 2003-12-11 devnull return r;
325 b3994ec5 2003-12-11 devnull findbl(Rune *r, int n, int *np)
327 0d0df317 2004-03-05 devnull while(n>0 && *r!=' ' && *r!='\t' && *r!='\n'){
331 b3994ec5 2003-12-11 devnull *np = n;
332 b3994ec5 2003-12-11 devnull return r;
336 b3994ec5 2003-12-11 devnull savemouse(Window *w)
338 b3994ec5 2003-12-11 devnull prevmouse = mouse->xy;
339 b3994ec5 2003-12-11 devnull mousew = w;
343 b3994ec5 2003-12-11 devnull restoremouse(Window *w)
345 b3994ec5 2003-12-11 devnull if(mousew!=nil && mousew==w)
346 b3994ec5 2003-12-11 devnull moveto(mousectl, prevmouse);
347 b3994ec5 2003-12-11 devnull mousew = nil;
351 b3994ec5 2003-12-11 devnull clearmouse()
353 b3994ec5 2003-12-11 devnull mousew = nil;
357 b3994ec5 2003-12-11 devnull estrdup(char *s)
359 b3994ec5 2003-12-11 devnull char *t;
361 b3994ec5 2003-12-11 devnull t = strdup(s);
362 b3994ec5 2003-12-11 devnull if(t == nil)
363 b3994ec5 2003-12-11 devnull error("strdup failed");
364 b3994ec5 2003-12-11 devnull setmalloctag(t, getcallerpc(&s));
365 b3994ec5 2003-12-11 devnull return t;
369 b3994ec5 2003-12-11 devnull emalloc(uint n)
371 b3994ec5 2003-12-11 devnull void *p;
373 b3994ec5 2003-12-11 devnull p = malloc(n);
374 b3994ec5 2003-12-11 devnull if(p == nil){
375 b3994ec5 2003-12-11 devnull fprint(2, "allocating %d from %lux: %r\n", n, getcallerpc(&n));
376 b3994ec5 2003-12-11 devnull *(int*)0=0;
377 b3994ec5 2003-12-11 devnull error("malloc failed");
379 b3994ec5 2003-12-11 devnull setmalloctag(p, getcallerpc(&n));
380 b3994ec5 2003-12-11 devnull memset(p, 0, n);
381 b3994ec5 2003-12-11 devnull return p;
385 b3994ec5 2003-12-11 devnull erealloc(void *p, uint n)
387 b3994ec5 2003-12-11 devnull p = realloc(p, n);
388 b3994ec5 2003-12-11 devnull if(p == nil){
389 b3994ec5 2003-12-11 devnull fprint(2, "reallocating %d: %r\n", n);
390 b3994ec5 2003-12-11 devnull error("realloc failed");
392 b3994ec5 2003-12-11 devnull setmalloctag(p, getcallerpc(&n));
393 b3994ec5 2003-12-11 devnull return p;
397 b3994ec5 2003-12-11 devnull * Heuristic city.
400 b3994ec5 2003-12-11 devnull makenewwindow(Text *t)
402 b3994ec5 2003-12-11 devnull Column *c;
403 b3994ec5 2003-12-11 devnull Window *w, *bigw, *emptyw;
404 b3994ec5 2003-12-11 devnull Text *emptyb;
405 b3994ec5 2003-12-11 devnull int i, y, el;
407 b3994ec5 2003-12-11 devnull if(activecol)
408 b3994ec5 2003-12-11 devnull c = activecol;
409 b3994ec5 2003-12-11 devnull else if(seltext && seltext->col)
410 b3994ec5 2003-12-11 devnull c = seltext->col;
411 b3994ec5 2003-12-11 devnull else if(t && t->col)
412 b3994ec5 2003-12-11 devnull c = t->col;
414 b3994ec5 2003-12-11 devnull if(row.ncol==0 && rowadd(&row, nil, -1)==nil)
415 b3994ec5 2003-12-11 devnull error("can't make column");
416 b3994ec5 2003-12-11 devnull c = row.col[row.ncol-1];
418 b3994ec5 2003-12-11 devnull activecol = c;
419 b3994ec5 2003-12-11 devnull if(t==nil || t->w==nil || c->nw==0)
420 b3994ec5 2003-12-11 devnull return coladd(c, nil, nil, -1);
422 b3994ec5 2003-12-11 devnull /* find biggest window and biggest blank spot */
423 b3994ec5 2003-12-11 devnull emptyw = c->w[0];
424 b3994ec5 2003-12-11 devnull bigw = emptyw;
425 b3994ec5 2003-12-11 devnull for(i=1; i<c->nw; i++){
426 b3994ec5 2003-12-11 devnull w = c->w[i];
427 b3994ec5 2003-12-11 devnull /* use >= to choose one near bottom of screen */
428 b3994ec5 2003-12-11 devnull if(w->body.fr.maxlines >= bigw->body.fr.maxlines)
429 b3994ec5 2003-12-11 devnull bigw = w;
430 b3994ec5 2003-12-11 devnull if(w->body.fr.maxlines-w->body.fr.nlines >= emptyw->body.fr.maxlines-emptyw->body.fr.nlines)
431 b3994ec5 2003-12-11 devnull emptyw = w;
433 b3994ec5 2003-12-11 devnull emptyb = &emptyw->body;
434 b3994ec5 2003-12-11 devnull el = emptyb->fr.maxlines-emptyb->fr.nlines;
435 b3994ec5 2003-12-11 devnull /* if empty space is big, use it */
436 b3994ec5 2003-12-11 devnull if(el>15 || (el>3 && el>(bigw->body.fr.maxlines-1)/2))
437 b3994ec5 2003-12-11 devnull y = emptyb->fr.r.min.y+emptyb->fr.nlines*font->height;
439 b3994ec5 2003-12-11 devnull /* if this window is in column and isn't much smaller, split it */
440 b3994ec5 2003-12-11 devnull if(t->col==c && Dy(t->w->r)>2*Dy(bigw->r)/3)
441 b3994ec5 2003-12-11 devnull bigw = t->w;
442 b3994ec5 2003-12-11 devnull y = (bigw->r.min.y + bigw->r.max.y)/2;
444 b3994ec5 2003-12-11 devnull w = coladd(c, nil, nil, y);
445 b3994ec5 2003-12-11 devnull if(w->body.fr.maxlines < 2)
446 b3994ec5 2003-12-11 devnull colgrow(w->col, w, 1);
447 b3994ec5 2003-12-11 devnull return w;