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 int winid;
17 b3994ec5 2003-12-11 devnull wininit(Window *w, Window *clone, Rectangle r)
19 b3994ec5 2003-12-11 devnull Rectangle r1, br;
21 b3994ec5 2003-12-11 devnull Reffont *rf;
22 b3994ec5 2003-12-11 devnull Rune *rp;
25 b3994ec5 2003-12-11 devnull w->tag.w = w;
26 33dc4226 2005-10-31 devnull w->taglines = 1;
27 768d880d 2005-12-21 devnull w->tagexpand = TRUE;
28 b3994ec5 2003-12-11 devnull w->body.w = w;
29 b3994ec5 2003-12-11 devnull w->id = ++winid;
30 b3994ec5 2003-12-11 devnull incref(&w->ref);
31 5a8e63b2 2004-02-29 devnull if(globalincref)
32 5a8e63b2 2004-02-29 devnull incref(&w->ref);
33 b3994ec5 2003-12-11 devnull w->ctlfid = ~0;
34 b3994ec5 2003-12-11 devnull w->utflastqid = -1;
37 33dc4226 2005-10-31 devnull w->tagtop = r;
38 33dc4226 2005-10-31 devnull w->tagtop.max.y = r.min.y + font->height;
40 33dc4226 2005-10-31 devnull r1.max.y = r1.min.y + w->taglines*font->height;
41 b3994ec5 2003-12-11 devnull incref(&reffont.ref);
42 b3994ec5 2003-12-11 devnull f = fileaddtext(nil, &w->tag);
43 b3994ec5 2003-12-11 devnull textinit(&w->tag, f, r1, &reffont, tagcols);
44 b3994ec5 2003-12-11 devnull w->tag.what = Tag;
45 b3994ec5 2003-12-11 devnull /* tag is a copy of the contents, not a tracked image */
46 b3994ec5 2003-12-11 devnull if(clone){
47 b3994ec5 2003-12-11 devnull textdelete(&w->tag, 0, w->tag.file->b.nc, TRUE);
48 b3994ec5 2003-12-11 devnull nc = clone->tag.file->b.nc;
49 b3994ec5 2003-12-11 devnull rp = runemalloc(nc);
50 b3994ec5 2003-12-11 devnull bufread(&clone->tag.file->b, 0, rp, nc);
51 b3994ec5 2003-12-11 devnull textinsert(&w->tag, 0, rp, nc, TRUE);
52 b3994ec5 2003-12-11 devnull free(rp);
53 b3994ec5 2003-12-11 devnull filereset(w->tag.file);
54 b3994ec5 2003-12-11 devnull textsetselect(&w->tag, nc, nc);
56 cbeb0b26 2006-04-01 devnull /*assert(w->body.w == w); */
58 33dc4226 2005-10-31 devnull r1.min.y += w->taglines*font->height + 1;
59 b3994ec5 2003-12-11 devnull if(r1.max.y < r1.min.y)
60 b3994ec5 2003-12-11 devnull r1.max.y = r1.min.y;
62 b3994ec5 2003-12-11 devnull if(clone){
63 b3994ec5 2003-12-11 devnull f = clone->body.file;
64 b3994ec5 2003-12-11 devnull w->body.org = clone->body.org;
65 b3994ec5 2003-12-11 devnull w->isscratch = clone->isscratch;
66 b3994ec5 2003-12-11 devnull rf = rfget(FALSE, FALSE, FALSE, clone->body.reffont->f->name);
68 b3994ec5 2003-12-11 devnull rf = rfget(FALSE, FALSE, FALSE, nil);
69 cbeb0b26 2006-04-01 devnull /*assert(w->body.w == w); */
70 b3994ec5 2003-12-11 devnull f = fileaddtext(f, &w->body);
71 b3994ec5 2003-12-11 devnull w->body.what = Body;
72 b3994ec5 2003-12-11 devnull textinit(&w->body, f, r1, rf, textcols);
73 b3994ec5 2003-12-11 devnull r1.min.y -= 1;
74 b3994ec5 2003-12-11 devnull r1.max.y = r1.min.y+1;
75 b3994ec5 2003-12-11 devnull draw(screen, r1, tagcols[BORD], nil, ZP);
76 b3994ec5 2003-12-11 devnull textscrdraw(&w->body);
77 b3994ec5 2003-12-11 devnull w->r = r;
78 b3994ec5 2003-12-11 devnull br.min = w->tag.scrollr.min;
79 b3994ec5 2003-12-11 devnull br.max.x = br.min.x + Dx(button->r);
80 b3994ec5 2003-12-11 devnull br.max.y = br.min.y + Dy(button->r);
81 b3994ec5 2003-12-11 devnull draw(screen, br, button, nil, button->r.min);
82 b3994ec5 2003-12-11 devnull w->filemenu = TRUE;
83 b3994ec5 2003-12-11 devnull w->maxlines = w->body.fr.maxlines;
84 53998c99 2004-09-28 devnull w->autoindent = globalautoindent;
85 cbeb0b26 2006-04-01 devnull /*assert(w->body.w == w); */
86 b3994ec5 2003-12-11 devnull if(clone){
87 b3994ec5 2003-12-11 devnull w->dirty = clone->dirty;
88 2a373ea4 2005-03-18 devnull w->autoindent = clone->autoindent;
89 b3994ec5 2003-12-11 devnull textsetselect(&w->body, clone->body.q0, clone->body.q1);
90 b3994ec5 2003-12-11 devnull winsettag(w);
95 1bcfd909 2007-03-15 devnull * Draw the appropriate button.
98 1bcfd909 2007-03-15 devnull windrawbutton(Window *w)
100 1bcfd909 2007-03-15 devnull Image *b;
101 1bcfd909 2007-03-15 devnull Rectangle br;
103 1bcfd909 2007-03-15 devnull b = button;
104 1bcfd909 2007-03-15 devnull if(!w->isdir && !w->isscratch && (w->body.file->mod || w->body.ncache))
105 1bcfd909 2007-03-15 devnull b = modbutton;
106 1bcfd909 2007-03-15 devnull br.min = w->tag.scrollr.min;
107 1bcfd909 2007-03-15 devnull br.max.x = br.min.x + Dx(b->r);
108 1bcfd909 2007-03-15 devnull br.max.y = br.min.y + Dy(b->r);
109 1bcfd909 2007-03-15 devnull draw(screen, br, b, nil, b->r.min);
113 33dc4226 2005-10-31 devnull * Compute number of tag lines required
114 33dc4226 2005-10-31 devnull * to display entire tag text.
117 33dc4226 2005-10-31 devnull wintaglines(Window *w, Rectangle r)
120 33dc4226 2005-10-31 devnull Rune rune;
122 f8dea3c1 2005-12-16 devnull /* TAG policy here */
124 33dc4226 2005-10-31 devnull if(!w->tagexpand)
125 33dc4226 2005-10-31 devnull return 1;
126 33dc4226 2005-10-31 devnull w->tag.fr.noredraw = 1;
127 33dc4226 2005-10-31 devnull textresize(&w->tag, r, TRUE);
128 33dc4226 2005-10-31 devnull w->tag.fr.noredraw = 0;
129 d76a49fb 2007-03-16 devnull w->tagsafe = FALSE;
131 f8dea3c1 2005-12-16 devnull /* can't use more than we have */
132 33dc4226 2005-10-31 devnull if(w->tag.fr.nlines >= w->tag.fr.maxlines)
133 33dc4226 2005-10-31 devnull return w->tag.fr.maxlines;
135 f8dea3c1 2005-12-16 devnull /* if tag ends with \n, include empty line at end for typing */
136 33dc4226 2005-10-31 devnull n = w->tag.fr.nlines;
137 8b9a1d4c 2006-02-06 devnull if(w->tag.file->b.nc > 0)
138 8b9a1d4c 2006-02-06 devnull bufread(&w->tag.file->b, w->tag.file->b.nc-1, &rune, 1);
139 33dc4226 2005-10-31 devnull if(rune == '\n')
141 8b9a1d4c 2006-02-06 devnull if(n == 0)
143 33dc4226 2005-10-31 devnull return n;
147 33dc4226 2005-10-31 devnull winresize(Window *w, Rectangle r, int safe, int keepextra)
149 cd1d0ab0 2006-01-12 devnull int oy, y, mouseintag, tagresized;
150 ad80f870 2005-12-16 devnull Point p;
151 1bcfd909 2007-03-15 devnull Rectangle r1;
153 20ae0b0f 2005-12-16 devnull if(0) fprint(2, "winresize %d %R safe=%d keep=%d h=%d\n", w->id, r, safe, keepextra, font->height);
154 33dc4226 2005-10-31 devnull w->tagtop = r;
155 33dc4226 2005-10-31 devnull w->tagtop.max.y = r.min.y+font->height;
158 f8dea3c1 2005-12-16 devnull * TAG If necessary, recompute the number of lines that should
159 f8dea3c1 2005-12-16 devnull * be in the tag.
162 f8dea3c1 2005-12-16 devnull r1.max.y = min(r.max.y, r1.min.y + w->taglines*font->height);
163 f8dea3c1 2005-12-16 devnull y = r1.max.y;
164 ad80f870 2005-12-16 devnull mouseintag = ptinrect(mouse->xy, w->tag.all);
165 cd1d0ab0 2006-01-12 devnull if(!safe || !w->tagsafe || !eqrect(w->tag.all, r1)){
167 f8dea3c1 2005-12-16 devnull w->taglines = wintaglines(w, r);
169 f8dea3c1 2005-12-16 devnull /* END TAG */
172 33dc4226 2005-10-31 devnull r1.max.y = min(r.max.y, r1.min.y + w->taglines*font->height);
173 b3994ec5 2003-12-11 devnull y = r1.max.y;
174 cd1d0ab0 2006-01-12 devnull tagresized = 0;
175 1bcfd909 2007-03-15 devnull if(0) fprint(2, "winresize tag %R %R\n", w->tag.all, r1);
176 d76a49fb 2007-03-16 devnull if(!safe || !w->tagsafe || !eqrect(w->tag.all, r1)){
177 cd1d0ab0 2006-01-12 devnull tagresized = 1;
178 cd1d0ab0 2006-01-12 devnull if(0) fprint(2, "resize tag %R => %R\n", w->tag.all, r1);
179 d5233ccb 2005-10-31 devnull textresize(&w->tag, r1, TRUE);
180 20ae0b0f 2005-12-16 devnull if(0) fprint(2, "=> %R (%R)\n", w->tag.all, w->tag.fr.r);
181 d5233ccb 2005-10-31 devnull y = w->tag.fr.r.max.y;
182 1bcfd909 2007-03-15 devnull windrawbutton(w);
183 d76a49fb 2007-03-16 devnull w->tagsafe = TRUE;
184 ad80f870 2005-12-16 devnull /* TAG */
185 ad80f870 2005-12-16 devnull if(mouseintag && !ptinrect(mouse->xy, w->tag.all)){
186 ad80f870 2005-12-16 devnull p = mouse->xy;
187 ad80f870 2005-12-16 devnull p.y = w->tag.all.max.y-3;
188 ad80f870 2005-12-16 devnull moveto(mousectl, p);
190 ad80f870 2005-12-16 devnull /* END TAG */
195 33dc4226 2005-10-31 devnull r1.min.y = y;
196 cd1d0ab0 2006-01-12 devnull if(tagresized || !safe || !eqrect(w->body.all, r1)){
198 cd1d0ab0 2006-01-12 devnull if(0) fprint(2, "resizing body; safe=%d all=%R r1=%R\n", safe, w->body.all, r1);
199 33dc4226 2005-10-31 devnull if(y+1+w->body.fr.font->height <= r.max.y){ /* room for one line */
200 b3994ec5 2003-12-11 devnull r1.min.y = y;
201 33dc4226 2005-10-31 devnull r1.max.y = y+1;
202 33dc4226 2005-10-31 devnull draw(screen, r1, tagcols[BORD], nil, ZP);
204 33dc4226 2005-10-31 devnull r1.min.y = min(y, r.max.y);
205 33dc4226 2005-10-31 devnull r1.max.y = r.max.y;
207 33dc4226 2005-10-31 devnull r1.min.y = y;
208 33dc4226 2005-10-31 devnull r1.max.y = y;
210 cd1d0ab0 2006-01-12 devnull if(0) fprint(2, "resizing body; new r=%R; r1=%R\n", r, r1);
211 b3994ec5 2003-12-11 devnull w->r = r;
212 33dc4226 2005-10-31 devnull w->r.max.y = textresize(&w->body, r1, keepextra);
213 cd1d0ab0 2006-01-12 devnull if(0) fprint(2, "after textresize: body.all=%R\n", w->body.all);
214 b3994ec5 2003-12-11 devnull textscrdraw(&w->body);
215 cd1d0ab0 2006-01-12 devnull w->body.all.min.y = oy;
217 b3994ec5 2003-12-11 devnull w->maxlines = min(w->body.fr.nlines, max(w->maxlines, w->body.fr.maxlines));
218 b3994ec5 2003-12-11 devnull return w->r.max.y;
222 b3994ec5 2003-12-11 devnull winlock1(Window *w, int owner)
224 b3994ec5 2003-12-11 devnull incref(&w->ref);
225 b3994ec5 2003-12-11 devnull qlock(&w->lk);
226 b3994ec5 2003-12-11 devnull w->owner = owner;
230 b3994ec5 2003-12-11 devnull winlock(Window *w, int owner)
233 b3994ec5 2003-12-11 devnull File *f;
235 b3994ec5 2003-12-11 devnull f = w->body.file;
236 b3994ec5 2003-12-11 devnull for(i=0; i<f->ntext; i++)
237 b3994ec5 2003-12-11 devnull winlock1(f->text[i]->w, owner);
241 b3994ec5 2003-12-11 devnull winunlock(Window *w)
244 b3994ec5 2003-12-11 devnull File *f;
247 725d9fd7 2005-07-28 devnull * subtle: loop runs backwards to avoid tripping over
248 725d9fd7 2005-07-28 devnull * winclose indirectly editing f->text and freeing f
249 725d9fd7 2005-07-28 devnull * on the last iteration of the loop.
251 b3994ec5 2003-12-11 devnull f = w->body.file;
252 725d9fd7 2005-07-28 devnull for(i=f->ntext-1; i>=0; i--){
253 b3994ec5 2003-12-11 devnull w = f->text[i]->w;
254 b3994ec5 2003-12-11 devnull w->owner = 0;
255 b3994ec5 2003-12-11 devnull qunlock(&w->lk);
256 b3994ec5 2003-12-11 devnull winclose(w);
261 b3994ec5 2003-12-11 devnull winmousebut(Window *w)
263 33dc4226 2005-10-31 devnull moveto(mousectl, addpt(w->tag.scrollr.min,
264 33dc4226 2005-10-31 devnull divpt(Pt(Dx(w->tag.scrollr), font->height), 2)));
268 b3994ec5 2003-12-11 devnull windirfree(Window *w)
271 b3994ec5 2003-12-11 devnull Dirlist *dl;
273 b3994ec5 2003-12-11 devnull if(w->isdir){
274 b3994ec5 2003-12-11 devnull for(i=0; i<w->ndl; i++){
275 b3994ec5 2003-12-11 devnull dl = w->dlp[i];
276 b3994ec5 2003-12-11 devnull free(dl->r);
277 b3994ec5 2003-12-11 devnull free(dl);
279 b3994ec5 2003-12-11 devnull free(w->dlp);
281 b3994ec5 2003-12-11 devnull w->dlp = nil;
282 b3994ec5 2003-12-11 devnull w->ndl = 0;
286 b3994ec5 2003-12-11 devnull winclose(Window *w)
290 b3994ec5 2003-12-11 devnull if(decref(&w->ref) == 0){
291 b3994ec5 2003-12-11 devnull windirfree(w);
292 b3994ec5 2003-12-11 devnull textclose(&w->tag);
293 b3994ec5 2003-12-11 devnull textclose(&w->body);
294 b3994ec5 2003-12-11 devnull if(activewin == w)
295 b3994ec5 2003-12-11 devnull activewin = nil;
296 b3994ec5 2003-12-11 devnull for(i=0; i<w->nincl; i++)
297 b3994ec5 2003-12-11 devnull free(w->incl[i]);
298 b3994ec5 2003-12-11 devnull free(w->incl);
299 b3994ec5 2003-12-11 devnull free(w->events);
300 b3994ec5 2003-12-11 devnull free(w);
305 b3994ec5 2003-12-11 devnull windelete(Window *w)
307 b3994ec5 2003-12-11 devnull Xfid *x;
309 b3994ec5 2003-12-11 devnull x = w->eventx;
311 b3994ec5 2003-12-11 devnull w->nevents = 0;
312 b3994ec5 2003-12-11 devnull free(w->events);
313 b3994ec5 2003-12-11 devnull w->events = nil;
314 b3994ec5 2003-12-11 devnull w->eventx = nil;
315 b3994ec5 2003-12-11 devnull sendp(x->c, nil); /* wake him up */
320 b3994ec5 2003-12-11 devnull winundo(Window *w, int isundo)
322 b3994ec5 2003-12-11 devnull Text *body;
324 b3994ec5 2003-12-11 devnull File *f;
325 b3994ec5 2003-12-11 devnull Window *v;
327 b3994ec5 2003-12-11 devnull w->utflastqid = -1;
328 b3994ec5 2003-12-11 devnull body = &w->body;
329 b3994ec5 2003-12-11 devnull fileundo(body->file, isundo, &body->q0, &body->q1);
330 b3994ec5 2003-12-11 devnull textshow(body, body->q0, body->q1, 1);
331 b3994ec5 2003-12-11 devnull f = body->file;
332 b3994ec5 2003-12-11 devnull for(i=0; i<f->ntext; i++){
333 b3994ec5 2003-12-11 devnull v = f->text[i]->w;
334 b3994ec5 2003-12-11 devnull v->dirty = (f->seq != v->putseq);
335 b3994ec5 2003-12-11 devnull if(v != w){
336 b3994ec5 2003-12-11 devnull v->body.q0 = v->body.fr.p0+v->body.org;
337 b3994ec5 2003-12-11 devnull v->body.q1 = v->body.fr.p1+v->body.org;
340 b3994ec5 2003-12-11 devnull winsettag(w);
344 b3994ec5 2003-12-11 devnull winsetname(Window *w, Rune *name, int n)
346 b3994ec5 2003-12-11 devnull Text *t;
347 b3994ec5 2003-12-11 devnull Window *v;
349 b3994ec5 2003-12-11 devnull static Rune Lslashguide[] = { '/', 'g', 'u', 'i', 'd', 'e', 0 };
350 b3994ec5 2003-12-11 devnull static Rune Lpluserrors[] = { '+', 'E', 'r', 'r', 'o', 'r', 's', 0 };
351 b3994ec5 2003-12-11 devnull t = &w->body;
352 b3994ec5 2003-12-11 devnull if(runeeq(t->file->name, t->file->nname, name, n) == TRUE)
354 b3994ec5 2003-12-11 devnull w->isscratch = FALSE;
355 b3994ec5 2003-12-11 devnull if(n>=6 && runeeq(Lslashguide, 6, name+(n-6), 6))
356 b3994ec5 2003-12-11 devnull w->isscratch = TRUE;
357 b3994ec5 2003-12-11 devnull else if(n>=7 && runeeq(Lpluserrors, 7, name+(n-7), 7))
358 b3994ec5 2003-12-11 devnull w->isscratch = TRUE;
359 b3994ec5 2003-12-11 devnull filesetname(t->file, name, n);
360 b3994ec5 2003-12-11 devnull for(i=0; i<t->file->ntext; i++){
361 b3994ec5 2003-12-11 devnull v = t->file->text[i]->w;
362 b3994ec5 2003-12-11 devnull winsettag(v);
363 b3994ec5 2003-12-11 devnull v->isscratch = w->isscratch;
368 b3994ec5 2003-12-11 devnull wintype(Window *w, Text *t, Rune r)
372 b3994ec5 2003-12-11 devnull texttype(t, r);
373 b3994ec5 2003-12-11 devnull if(t->what == Body)
374 b3994ec5 2003-12-11 devnull for(i=0; i<t->file->ntext; i++)
375 b3994ec5 2003-12-11 devnull textscrdraw(t->file->text[i]);
376 b3994ec5 2003-12-11 devnull winsettag(w);
380 b3994ec5 2003-12-11 devnull wincleartag(Window *w)
382 b3994ec5 2003-12-11 devnull int i, n;
383 b3994ec5 2003-12-11 devnull Rune *r;
385 b3994ec5 2003-12-11 devnull /* w must be committed */
386 b3994ec5 2003-12-11 devnull n = w->tag.file->b.nc;
387 b3994ec5 2003-12-11 devnull r = runemalloc(n);
388 b3994ec5 2003-12-11 devnull bufread(&w->tag.file->b, 0, r, n);
389 b3994ec5 2003-12-11 devnull for(i=0; i<n; i++)
390 b3994ec5 2003-12-11 devnull if(r[i]==' ' || r[i]=='\t')
392 b3994ec5 2003-12-11 devnull for(; i<n; i++)
393 b3994ec5 2003-12-11 devnull if(r[i] == '|')
395 b3994ec5 2003-12-11 devnull if(i == n)
398 b3994ec5 2003-12-11 devnull textdelete(&w->tag, i, n, TRUE);
399 b3994ec5 2003-12-11 devnull free(r);
400 b3994ec5 2003-12-11 devnull w->tag.file->mod = FALSE;
401 b3994ec5 2003-12-11 devnull if(w->tag.q0 > i)
402 b3994ec5 2003-12-11 devnull w->tag.q0 = i;
403 b3994ec5 2003-12-11 devnull if(w->tag.q1 > i)
404 b3994ec5 2003-12-11 devnull w->tag.q1 = i;
405 b3994ec5 2003-12-11 devnull textsetselect(&w->tag, w->tag.q0, w->tag.q1);
409 b3994ec5 2003-12-11 devnull winsettag1(Window *w)
411 77c5c852 2006-01-12 devnull int bar, dirty, i, j, k, n, ntagname, resize;
412 d96da29b 2005-03-14 devnull Rune *new, *old, *r, *tagname;
413 b3994ec5 2003-12-11 devnull uint q0, q1;
414 b3994ec5 2003-12-11 devnull static Rune Ldelsnarf[] = { ' ', 'D', 'e', 'l', ' ',
415 b3994ec5 2003-12-11 devnull 'S', 'n', 'a', 'r', 'f', 0 };
416 b3994ec5 2003-12-11 devnull static Rune Lundo[] = { ' ', 'U', 'n', 'd', 'o', 0 };
417 b3994ec5 2003-12-11 devnull static Rune Lredo[] = { ' ', 'R', 'e', 'd', 'o', 0 };
418 b3994ec5 2003-12-11 devnull static Rune Lget[] = { ' ', 'G', 'e', 't', 0 };
419 b3994ec5 2003-12-11 devnull static Rune Lput[] = { ' ', 'P', 'u', 't', 0 };
420 b3994ec5 2003-12-11 devnull static Rune Llook[] = { ' ', 'L', 'o', 'o', 'k', ' ', 0 };
421 b3994ec5 2003-12-11 devnull static Rune Lpipe[] = { ' ', '|', 0 };
422 b3994ec5 2003-12-11 devnull /* there are races that get us here with stuff in the tag cache, so we take extra care to sync it */
423 b3994ec5 2003-12-11 devnull if(w->tag.ncache!=0 || w->tag.file->mod)
424 b3994ec5 2003-12-11 devnull wincommit(w, &w->tag); /* check file name; also guarantees we can modify tag contents */
425 b3994ec5 2003-12-11 devnull old = runemalloc(w->tag.file->b.nc+1);
426 b3994ec5 2003-12-11 devnull bufread(&w->tag.file->b, 0, old, w->tag.file->b.nc);
427 b3994ec5 2003-12-11 devnull old[w->tag.file->b.nc] = '\0';
428 b3994ec5 2003-12-11 devnull for(i=0; i<w->tag.file->b.nc; i++)
429 b3994ec5 2003-12-11 devnull if(old[i]==' ' || old[i]=='\t')
432 d96da29b 2005-03-14 devnull /* make sure the file name is set correctly in the tag */
433 d96da29b 2005-03-14 devnull ntagname = w->body.file->nname;
434 d96da29b 2005-03-14 devnull tagname = runemalloc(ntagname);
435 d96da29b 2005-03-14 devnull runemove(tagname, w->body.file->name, ntagname);
436 44e41b1f 2005-07-13 devnull abbrevenv(&tagname, (uint*)&ntagname);
439 3b33df99 2005-03-18 devnull * XXX Why is this here instead of letting the code
440 3b33df99 2005-03-18 devnull * down below do the work?
442 d96da29b 2005-03-14 devnull if(runeeq(old, i, tagname, ntagname) == FALSE){
443 3b33df99 2005-03-18 devnull q0 = w->tag.q0;
444 3b33df99 2005-03-18 devnull q1 = w->tag.q1;
445 b3994ec5 2003-12-11 devnull textdelete(&w->tag, 0, i, TRUE);
446 d96da29b 2005-03-14 devnull textinsert(&w->tag, 0, tagname, ntagname, TRUE);
447 b3994ec5 2003-12-11 devnull free(old);
448 b3994ec5 2003-12-11 devnull old = runemalloc(w->tag.file->b.nc+1);
449 b3994ec5 2003-12-11 devnull bufread(&w->tag.file->b, 0, old, w->tag.file->b.nc);
450 b3994ec5 2003-12-11 devnull old[w->tag.file->b.nc] = '\0';
451 3b33df99 2005-03-18 devnull if(q0 >= i){
453 3b33df99 2005-03-18 devnull * XXX common case - typing at end of name
455 3b33df99 2005-03-18 devnull w->tag.q0 = q0+ntagname-i;
456 3b33df99 2005-03-18 devnull w->tag.q1 = q1+ntagname-i;
460 d96da29b 2005-03-14 devnull /* compute the text for the whole tag, replacing current only if it differs */
461 d96da29b 2005-03-14 devnull new = runemalloc(ntagname+100);
463 d96da29b 2005-03-14 devnull runemove(new+i, tagname, ntagname);
464 d96da29b 2005-03-14 devnull i += ntagname;
465 b3994ec5 2003-12-11 devnull runemove(new+i, Ldelsnarf, 10);
466 b3994ec5 2003-12-11 devnull i += 10;
467 b3994ec5 2003-12-11 devnull if(w->filemenu){
468 cd1d0ab0 2006-01-12 devnull if(w->body.needundo || w->body.file->delta.nc>0 || w->body.ncache){
469 b3994ec5 2003-12-11 devnull runemove(new+i, Lundo, 5);
472 b3994ec5 2003-12-11 devnull if(w->body.file->epsilon.nc > 0){
473 b3994ec5 2003-12-11 devnull runemove(new+i, Lredo, 5);
476 b3994ec5 2003-12-11 devnull dirty = w->body.file->nname && (w->body.ncache || w->body.file->seq!=w->putseq);
477 b3994ec5 2003-12-11 devnull if(!w->isdir && dirty){
478 b3994ec5 2003-12-11 devnull runemove(new+i, Lput, 4);
482 b3994ec5 2003-12-11 devnull if(w->isdir){
483 b3994ec5 2003-12-11 devnull runemove(new+i, Lget, 4);
486 b3994ec5 2003-12-11 devnull runemove(new+i, Lpipe, 2);
488 b3994ec5 2003-12-11 devnull r = runestrchr(old, '|');
490 b3994ec5 2003-12-11 devnull k = r-old+1;
492 b3994ec5 2003-12-11 devnull k = w->tag.file->b.nc;
493 b3994ec5 2003-12-11 devnull if(w->body.file->seq == 0){
494 b3994ec5 2003-12-11 devnull runemove(new+i, Llook, 6);
498 b3994ec5 2003-12-11 devnull new[i] = 0;
499 b3994ec5 2003-12-11 devnull if(runestrlen(new) != i)
500 b3994ec5 2003-12-11 devnull fprint(2, "s '%S' len not %d\n", new, i);
501 b3994ec5 2003-12-11 devnull assert(i==runestrlen(new));
503 d96da29b 2005-03-14 devnull /* replace tag if the new one is different */
504 77c5c852 2006-01-12 devnull resize = 0;
505 b3994ec5 2003-12-11 devnull if(runeeq(new, i, old, k) == FALSE){
506 77c5c852 2006-01-12 devnull resize = 1;
508 b3994ec5 2003-12-11 devnull if(n > i)
510 b3994ec5 2003-12-11 devnull for(j=0; j<n; j++)
511 b3994ec5 2003-12-11 devnull if(old[j] != new[j])
513 b3994ec5 2003-12-11 devnull q0 = w->tag.q0;
514 b3994ec5 2003-12-11 devnull q1 = w->tag.q1;
515 b3994ec5 2003-12-11 devnull textdelete(&w->tag, j, k, TRUE);
516 b3994ec5 2003-12-11 devnull textinsert(&w->tag, j, new+j, i-j, TRUE);
517 b3994ec5 2003-12-11 devnull /* try to preserve user selection */
518 b3994ec5 2003-12-11 devnull r = runestrchr(old, '|');
520 b3994ec5 2003-12-11 devnull bar = r-old;
521 b3994ec5 2003-12-11 devnull if(q0 > bar){
522 b3994ec5 2003-12-11 devnull bar = (runestrchr(new, '|')-new)-bar;
523 b3994ec5 2003-12-11 devnull w->tag.q0 = q0+bar;
524 b3994ec5 2003-12-11 devnull w->tag.q1 = q1+bar;
528 d96da29b 2005-03-14 devnull free(tagname);
529 b3994ec5 2003-12-11 devnull free(old);
530 b3994ec5 2003-12-11 devnull free(new);
531 b3994ec5 2003-12-11 devnull w->tag.file->mod = FALSE;
532 b3994ec5 2003-12-11 devnull n = w->tag.file->b.nc+w->tag.ncache;
533 b3994ec5 2003-12-11 devnull if(w->tag.q0 > n)
534 b3994ec5 2003-12-11 devnull w->tag.q0 = n;
535 b3994ec5 2003-12-11 devnull if(w->tag.q1 > n)
536 b3994ec5 2003-12-11 devnull w->tag.q1 = n;
537 b3994ec5 2003-12-11 devnull textsetselect(&w->tag, w->tag.q0, w->tag.q1);
538 1bcfd909 2007-03-15 devnull windrawbutton(w);
539 cd1d0ab0 2006-01-12 devnull if(resize){
540 cd1d0ab0 2006-01-12 devnull w->tagsafe = 0;
541 77c5c852 2006-01-12 devnull winresize(w, w->r, TRUE, TRUE);
546 b3994ec5 2003-12-11 devnull winsettag(Window *w)
549 b3994ec5 2003-12-11 devnull File *f;
550 b3994ec5 2003-12-11 devnull Window *v;
552 b3994ec5 2003-12-11 devnull f = w->body.file;
553 b3994ec5 2003-12-11 devnull for(i=0; i<f->ntext; i++){
554 b3994ec5 2003-12-11 devnull v = f->text[i]->w;
555 b3994ec5 2003-12-11 devnull if(v->col->safe || v->body.fr.maxlines>0)
556 b3994ec5 2003-12-11 devnull winsettag1(v);
561 b3994ec5 2003-12-11 devnull wincommit(Window *w, Text *t)
563 b3994ec5 2003-12-11 devnull Rune *r;
565 b3994ec5 2003-12-11 devnull File *f;
567 b3994ec5 2003-12-11 devnull textcommit(t, TRUE);
568 b3994ec5 2003-12-11 devnull f = t->file;
569 b3994ec5 2003-12-11 devnull if(f->ntext > 1)
570 b3994ec5 2003-12-11 devnull for(i=0; i<f->ntext; i++)
571 b3994ec5 2003-12-11 devnull textcommit(f->text[i], FALSE); /* no-op for t */
572 b3994ec5 2003-12-11 devnull if(t->what == Body)
574 b3994ec5 2003-12-11 devnull r = runemalloc(w->tag.file->b.nc);
575 b3994ec5 2003-12-11 devnull bufread(&w->tag.file->b, 0, r, w->tag.file->b.nc);
576 b3994ec5 2003-12-11 devnull for(i=0; i<w->tag.file->b.nc; i++)
577 b3994ec5 2003-12-11 devnull if(r[i]==' ' || r[i]=='\t')
579 44e41b1f 2005-07-13 devnull expandenv(&r, (uint*)&i);
580 b3994ec5 2003-12-11 devnull if(runeeq(r, i, w->body.file->name, w->body.file->nname) == FALSE){
582 b3994ec5 2003-12-11 devnull filemark(w->body.file);
583 b3994ec5 2003-12-11 devnull w->body.file->mod = TRUE;
584 b3994ec5 2003-12-11 devnull w->dirty = TRUE;
585 b3994ec5 2003-12-11 devnull winsetname(w, r, i);
586 b3994ec5 2003-12-11 devnull winsettag(w);
588 b3994ec5 2003-12-11 devnull free(r);
592 b3994ec5 2003-12-11 devnull winaddincl(Window *w, Rune *r, int n)
594 b3994ec5 2003-12-11 devnull char *a;
596 b3994ec5 2003-12-11 devnull Runestr rs;
598 b3994ec5 2003-12-11 devnull a = runetobyte(r, n);
599 b3994ec5 2003-12-11 devnull d = dirstat(a);
600 b3994ec5 2003-12-11 devnull if(d == nil){
601 b3994ec5 2003-12-11 devnull if(a[0] == '/')
602 b3994ec5 2003-12-11 devnull goto Rescue;
603 b3994ec5 2003-12-11 devnull rs = dirname(&w->body, r, n);
604 b3994ec5 2003-12-11 devnull r = rs.r;
605 b3994ec5 2003-12-11 devnull n = rs.nr;
606 b3994ec5 2003-12-11 devnull free(a);
607 b3994ec5 2003-12-11 devnull a = runetobyte(r, n);
608 b3994ec5 2003-12-11 devnull d = dirstat(a);
609 b3994ec5 2003-12-11 devnull if(d == nil)
610 b3994ec5 2003-12-11 devnull goto Rescue;
611 b3994ec5 2003-12-11 devnull r = runerealloc(r, n+1);
612 b3994ec5 2003-12-11 devnull r[n] = 0;
614 b3994ec5 2003-12-11 devnull free(a);
615 b3994ec5 2003-12-11 devnull if((d->qid.type&QTDIR) == 0){
616 b3994ec5 2003-12-11 devnull free(d);
617 b3994ec5 2003-12-11 devnull warning(nil, "%s: not a directory\n", a);
618 b3994ec5 2003-12-11 devnull free(r);
621 b3994ec5 2003-12-11 devnull free(d);
622 b3994ec5 2003-12-11 devnull w->nincl++;
623 b3994ec5 2003-12-11 devnull w->incl = realloc(w->incl, w->nincl*sizeof(Rune*));
624 b3994ec5 2003-12-11 devnull memmove(w->incl+1, w->incl, (w->nincl-1)*sizeof(Rune*));
625 b3994ec5 2003-12-11 devnull w->incl[0] = runemalloc(n+1);
626 b3994ec5 2003-12-11 devnull runemove(w->incl[0], r, n);
627 b3994ec5 2003-12-11 devnull free(r);
631 b3994ec5 2003-12-11 devnull warning(nil, "%s: %r\n", a);
632 b3994ec5 2003-12-11 devnull free(r);
633 b3994ec5 2003-12-11 devnull free(a);
638 b3994ec5 2003-12-11 devnull winclean(Window *w, int conservative) /* as it stands, conservative is always TRUE */
640 b3994ec5 2003-12-11 devnull if(w->isscratch || w->isdir) /* don't whine if it's a guide file, error window, etc. */
641 b3994ec5 2003-12-11 devnull return TRUE;
642 b3994ec5 2003-12-11 devnull if(!conservative && w->nopen[QWevent]>0)
643 b3994ec5 2003-12-11 devnull return TRUE;
644 b3994ec5 2003-12-11 devnull if(w->dirty){
645 b3994ec5 2003-12-11 devnull if(w->body.file->nname)
646 b3994ec5 2003-12-11 devnull warning(nil, "%.*S modified\n", w->body.file->nname, w->body.file->name);
648 b3994ec5 2003-12-11 devnull if(w->body.file->b.nc < 100) /* don't whine if it's too small */
649 b3994ec5 2003-12-11 devnull return TRUE;
650 b3994ec5 2003-12-11 devnull warning(nil, "unnamed file modified\n");
652 b3994ec5 2003-12-11 devnull w->dirty = FALSE;
653 b3994ec5 2003-12-11 devnull return FALSE;
655 b3994ec5 2003-12-11 devnull return TRUE;
659 b3994ec5 2003-12-11 devnull winctlprint(Window *w, char *buf, int fonts)
661 627bae9c 2005-01-04 devnull sprint(buf, "%11d %11d %11d %11d %11d ", w->id, w->tag.file->b.nc,
662 b3994ec5 2003-12-11 devnull w->body.file->b.nc, w->isdir, w->dirty);
663 b3994ec5 2003-12-11 devnull if(fonts)
664 627bae9c 2005-01-04 devnull return smprint("%s%11d %q %11d ", buf, Dx(w->body.fr.r),
665 627bae9c 2005-01-04 devnull w->body.reffont->f->name, w->body.fr.maxtab);
666 627bae9c 2005-01-04 devnull return buf;
670 b3994ec5 2003-12-11 devnull winevent(Window *w, char *fmt, ...)
673 b3994ec5 2003-12-11 devnull char *b;
674 b3994ec5 2003-12-11 devnull Xfid *x;
675 b3994ec5 2003-12-11 devnull va_list arg;
677 b3994ec5 2003-12-11 devnull if(w->nopen[QWevent] == 0)
679 b3994ec5 2003-12-11 devnull if(w->owner == 0)
680 b3994ec5 2003-12-11 devnull error("no window owner");
681 b3994ec5 2003-12-11 devnull va_start(arg, fmt);
682 b3994ec5 2003-12-11 devnull b = vsmprint(fmt, arg);
683 b3994ec5 2003-12-11 devnull va_end(arg);
684 b3994ec5 2003-12-11 devnull if(b == nil)
685 b3994ec5 2003-12-11 devnull error("vsmprint failed");
686 b3994ec5 2003-12-11 devnull n = strlen(b);
687 95409400 2006-02-21 devnull w->events = erealloc(w->events, w->nevents+1+n);
688 b3994ec5 2003-12-11 devnull w->events[w->nevents++] = w->owner;
689 b3994ec5 2003-12-11 devnull memmove(w->events+w->nevents, b, n);
690 b3994ec5 2003-12-11 devnull free(b);
691 b3994ec5 2003-12-11 devnull w->nevents += n;
692 b3994ec5 2003-12-11 devnull x = w->eventx;
694 b3994ec5 2003-12-11 devnull w->eventx = nil;
695 b3994ec5 2003-12-11 devnull sendp(x->c, nil);
700 d96da29b 2005-03-14 devnull * This is here as a first stab at something.
701 d96da29b 2005-03-14 devnull * Run acme with the -'$' flag to enable it.
703 d96da29b 2005-03-14 devnull * This code isn't quite right, in that it doesn't play well with
704 d96da29b 2005-03-14 devnull * the plumber and with other applications. For example:
706 d96da29b 2005-03-14 devnull * If the window tag is $home/bin and you execute script, then acme runs
707 d96da29b 2005-03-14 devnull * script in $home/bin, via the shell, so everything is fine. If you do
708 d96da29b 2005-03-14 devnull * execute "echo $home", it too goes to the shell so you see the value
709 d96da29b 2005-03-14 devnull * of $home. And if you right-click on script, then acme plumbs "script"
710 d96da29b 2005-03-14 devnull * in the directory "/home/you/bin", so that works, but if you right-click
711 d96da29b 2005-03-14 devnull * on "$home/bin/script", then what? It's not correct to expand in acme
712 d96da29b 2005-03-14 devnull * since what you're plumbing might be a price tag for all we know. So the
713 d96da29b 2005-03-14 devnull * plumber has to expand it, but in order to do that the plumber should
714 d96da29b 2005-03-14 devnull * probably publish (and allow users to change) the set of variables it is
715 d96da29b 2005-03-14 devnull * using in expansions.
717 d96da29b 2005-03-14 devnull * Rob has suggested that a better solution is to make tag lines expand
718 d96da29b 2005-03-14 devnull * automatically to fit the necessary number of lines.
720 d96da29b 2005-03-14 devnull * The best solution, of course, is to use nice short path names, but this
721 d96da29b 2005-03-14 devnull * is not always possible.
725 d96da29b 2005-03-14 devnull expandenv(Rune **rp, uint *np)
727 d96da29b 2005-03-14 devnull char *s, *t;
728 d96da29b 2005-03-14 devnull Rune *p, *r, *q;
729 d96da29b 2005-03-14 devnull uint n, pref;
730 d96da29b 2005-03-14 devnull int nb, nr, slash;
731 d96da29b 2005-03-14 devnull Runestr rs;
733 d96da29b 2005-03-14 devnull if(!dodollarsigns)
734 d96da29b 2005-03-14 devnull return FALSE;
736 d96da29b 2005-03-14 devnull r = *rp;
737 d96da29b 2005-03-14 devnull n = *np;
738 d96da29b 2005-03-14 devnull if(n == 0 || r[0] != '$')
739 d96da29b 2005-03-14 devnull return FALSE;
740 d96da29b 2005-03-14 devnull for(p=r+1; *p && *p != '/'; p++)
742 d96da29b 2005-03-14 devnull pref = p-r;
743 d96da29b 2005-03-14 devnull s = runetobyte(r+1, pref-1);
744 d96da29b 2005-03-14 devnull if((t = getenv(s)) == nil){
745 d96da29b 2005-03-14 devnull free(s);
746 d96da29b 2005-03-14 devnull return FALSE;
749 d96da29b 2005-03-14 devnull q = runemalloc(utflen(t)+(n-pref));
750 d96da29b 2005-03-14 devnull cvttorunes(t, strlen(t), q, &nb, &nr, nil);
751 d96da29b 2005-03-14 devnull assert(nr==utflen(t));
752 d96da29b 2005-03-14 devnull runemove(q+nr, p, n-pref);
753 d96da29b 2005-03-14 devnull free(r);
754 d96da29b 2005-03-14 devnull rs = runestr(q, nr+(n-pref));
755 d96da29b 2005-03-14 devnull slash = rs.nr>0 && q[rs.nr-1] == '/';
756 d96da29b 2005-03-14 devnull rs = cleanrname(rs);
757 d96da29b 2005-03-14 devnull if(slash){
758 d96da29b 2005-03-14 devnull rs.r = runerealloc(rs.r, rs.nr+1);
759 d96da29b 2005-03-14 devnull rs.r[rs.nr++] = '/';
761 d96da29b 2005-03-14 devnull *rp = rs.r;
762 d96da29b 2005-03-14 devnull *np = rs.nr;
763 d96da29b 2005-03-14 devnull free(t);
764 d96da29b 2005-03-14 devnull return TRUE;
767 d96da29b 2005-03-14 devnull extern char **environ;
768 d96da29b 2005-03-14 devnull Rune **runeenv;
771 d96da29b 2005-03-14 devnull * Weird sort order -- shorter names first,
772 d96da29b 2005-03-14 devnull * prefer lowercase over uppercase ($home over $HOME),
773 d96da29b 2005-03-14 devnull * then do normal string comparison.
776 d96da29b 2005-03-14 devnull runeenvcmp(const void *va, const void *vb)
778 d96da29b 2005-03-14 devnull Rune *a, *b;
779 d96da29b 2005-03-14 devnull int na, nb;
781 d96da29b 2005-03-14 devnull a = *(Rune**)va;
782 d96da29b 2005-03-14 devnull b = *(Rune**)vb;
783 d96da29b 2005-03-14 devnull na = runestrchr(a, '=') - a;
784 d96da29b 2005-03-14 devnull nb = runestrchr(b, '=') - b;
785 d96da29b 2005-03-14 devnull if(na < nb)
786 d96da29b 2005-03-14 devnull return -1;
787 d96da29b 2005-03-14 devnull if(nb > na)
788 d96da29b 2005-03-14 devnull return 1;
789 d96da29b 2005-03-14 devnull if(na == 0)
790 d96da29b 2005-03-14 devnull return 0;
791 d96da29b 2005-03-14 devnull if(islowerrune(a[0]) && !islowerrune(b[0]))
792 d96da29b 2005-03-14 devnull return -1;
793 d96da29b 2005-03-14 devnull if(islowerrune(b[0]) && !islowerrune(a[0]))
794 d96da29b 2005-03-14 devnull return 1;
795 d96da29b 2005-03-14 devnull return runestrncmp(a, b, na);
799 d96da29b 2005-03-14 devnull mkruneenv(void)
801 d96da29b 2005-03-14 devnull int i, bad, n, nr;
802 d96da29b 2005-03-14 devnull char *p;
803 d96da29b 2005-03-14 devnull Rune *r, *q;
806 d96da29b 2005-03-14 devnull for(i=0; environ[i]; i++){
808 d96da29b 2005-03-14 devnull * Ignore some pollution.
810 d96da29b 2005-03-14 devnull if(environ[i][0] == '_')
811 d96da29b 2005-03-14 devnull continue;
812 d96da29b 2005-03-14 devnull if(strncmp(environ[i], "PWD=", 4) == 0)
813 d96da29b 2005-03-14 devnull continue;
814 d96da29b 2005-03-14 devnull if(strncmp(environ[i], "OLDPWD=", 7) == 0)
815 d96da29b 2005-03-14 devnull continue;
818 d96da29b 2005-03-14 devnull * Must be a rooted path.
820 d96da29b 2005-03-14 devnull if((p = strchr(environ[i], '=')) == nil || *(p+1) != '/')
821 d96da29b 2005-03-14 devnull continue;
824 d96da29b 2005-03-14 devnull * Only use the ones that we accept in look - all isfilec
826 d96da29b 2005-03-14 devnull bad = 0;
827 d96da29b 2005-03-14 devnull r = bytetorune(environ[i], &nr);
828 d96da29b 2005-03-14 devnull for(q=r; *q != '='; q++)
829 d96da29b 2005-03-14 devnull if(!isfilec(*q)){
830 d96da29b 2005-03-14 devnull free(r);
831 d96da29b 2005-03-14 devnull bad = 1;
834 d96da29b 2005-03-14 devnull if(!bad){
835 d96da29b 2005-03-14 devnull runeenv = erealloc(runeenv, (n+1)*sizeof(runeenv[0]));
836 d96da29b 2005-03-14 devnull runeenv[n++] = r;
839 d96da29b 2005-03-14 devnull runeenv = erealloc(runeenv, (n+1)*sizeof(runeenv[0]));
840 d96da29b 2005-03-14 devnull runeenv[n] = nil;
841 d96da29b 2005-03-14 devnull qsort(runeenv, n, sizeof(runeenv[0]), runeenvcmp);
845 d96da29b 2005-03-14 devnull abbrevenv(Rune **rp, uint *np)
847 d96da29b 2005-03-14 devnull int i, len, alen;
848 d96da29b 2005-03-14 devnull Rune *r, *p, *q;
851 d96da29b 2005-03-14 devnull if(!dodollarsigns)
852 d96da29b 2005-03-14 devnull return FALSE;
854 d96da29b 2005-03-14 devnull r = *rp;
855 d96da29b 2005-03-14 devnull n = *np;
856 d96da29b 2005-03-14 devnull if(n == 0 || r[0] != '/')
857 d96da29b 2005-03-14 devnull return FALSE;
859 d96da29b 2005-03-14 devnull if(runeenv == nil)
860 d96da29b 2005-03-14 devnull mkruneenv();
862 d96da29b 2005-03-14 devnull for(i=0; runeenv[i]; i++){
863 d96da29b 2005-03-14 devnull p = runestrchr(runeenv[i], '=')+1;
864 d96da29b 2005-03-14 devnull len = runestrlen(p);
865 d96da29b 2005-03-14 devnull if(len <= n && (r[len]==0 || r[len]=='/') && runeeq(r, len, p, len)==TRUE){
866 d96da29b 2005-03-14 devnull alen = (p-1) - runeenv[i];
867 d96da29b 2005-03-14 devnull q = runemalloc(1+alen+n-len);
868 d96da29b 2005-03-14 devnull q[0] = '$';
869 d96da29b 2005-03-14 devnull runemove(q+1, runeenv[i], alen);
870 d96da29b 2005-03-14 devnull runemove(q+1+alen, r+len, n-len);
871 d96da29b 2005-03-14 devnull free(r);
872 d96da29b 2005-03-14 devnull *rp = q;
873 d96da29b 2005-03-14 devnull *np = 1+alen+n-len;
874 d96da29b 2005-03-14 devnull return TRUE;
877 d96da29b 2005-03-14 devnull return FALSE;