Blame


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"
13 b3994ec5 2003-12-11 devnull
14 b3994ec5 2003-12-11 devnull int winid;
15 b3994ec5 2003-12-11 devnull
16 b3994ec5 2003-12-11 devnull void
17 b3994ec5 2003-12-11 devnull wininit(Window *w, Window *clone, Rectangle r)
18 b3994ec5 2003-12-11 devnull {
19 b3994ec5 2003-12-11 devnull Rectangle r1, br;
20 b3994ec5 2003-12-11 devnull File *f;
21 b3994ec5 2003-12-11 devnull Reffont *rf;
22 b3994ec5 2003-12-11 devnull Rune *rp;
23 b3994ec5 2003-12-11 devnull int nc;
24 b3994ec5 2003-12-11 devnull
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;
35 b3994ec5 2003-12-11 devnull r1 = r;
36 d5233ccb 2005-10-31 devnull
37 33dc4226 2005-10-31 devnull w->tagtop = r;
38 33dc4226 2005-10-31 devnull w->tagtop.max.y = r.min.y + font->height;
39 d5233ccb 2005-10-31 devnull
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);
55 b3994ec5 2003-12-11 devnull }
56 cbeb0b26 2006-04-01 devnull /*assert(w->body.w == w); */
57 b3994ec5 2003-12-11 devnull r1 = r;
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;
61 b3994ec5 2003-12-11 devnull f = nil;
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);
67 b3994ec5 2003-12-11 devnull }else
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);
91 b3994ec5 2003-12-11 devnull }
92 b3994ec5 2003-12-11 devnull }
93 b3994ec5 2003-12-11 devnull
94 33dc4226 2005-10-31 devnull /*
95 1bcfd909 2007-03-15 devnull * Draw the appropriate button.
96 1bcfd909 2007-03-15 devnull */
97 1bcfd909 2007-03-15 devnull void
98 1bcfd909 2007-03-15 devnull windrawbutton(Window *w)
99 1bcfd909 2007-03-15 devnull {
100 1bcfd909 2007-03-15 devnull Image *b;
101 1bcfd909 2007-03-15 devnull Rectangle br;
102 1bcfd909 2007-03-15 devnull
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);
110 1bcfd909 2007-03-15 devnull }
111 1bcfd909 2007-03-15 devnull
112 1bcfd909 2007-03-15 devnull /*
113 33dc4226 2005-10-31 devnull * Compute number of tag lines required
114 33dc4226 2005-10-31 devnull * to display entire tag text.
115 33dc4226 2005-10-31 devnull */
116 b3994ec5 2003-12-11 devnull int
117 33dc4226 2005-10-31 devnull wintaglines(Window *w, Rectangle r)
118 b3994ec5 2003-12-11 devnull {
119 33dc4226 2005-10-31 devnull int n;
120 33dc4226 2005-10-31 devnull Rune rune;
121 33dc4226 2005-10-31 devnull
122 f8dea3c1 2005-12-16 devnull /* TAG policy here */
123 f8dea3c1 2005-12-16 devnull
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;
130 f8dea3c1 2005-12-16 devnull
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;
134 f8dea3c1 2005-12-16 devnull
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')
140 33dc4226 2005-10-31 devnull n++;
141 8b9a1d4c 2006-02-06 devnull if(n == 0)
142 8b9a1d4c 2006-02-06 devnull n = 1;
143 33dc4226 2005-10-31 devnull return n;
144 33dc4226 2005-10-31 devnull }
145 33dc4226 2005-10-31 devnull
146 33dc4226 2005-10-31 devnull int
147 33dc4226 2005-10-31 devnull winresize(Window *w, Rectangle r, int safe, int keepextra)
148 33dc4226 2005-10-31 devnull {
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;
152 b3994ec5 2003-12-11 devnull
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;
156 33dc4226 2005-10-31 devnull
157 f8dea3c1 2005-12-16 devnull /*
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.
160 f8dea3c1 2005-12-16 devnull */
161 f8dea3c1 2005-12-16 devnull r1 = r;
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)){
166 ad80f870 2005-12-16 devnull
167 f8dea3c1 2005-12-16 devnull w->taglines = wintaglines(w, r);
168 f8dea3c1 2005-12-16 devnull }
169 f8dea3c1 2005-12-16 devnull /* END TAG */
170 f8dea3c1 2005-12-16 devnull
171 33dc4226 2005-10-31 devnull r1 = r;
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);
189 ad80f870 2005-12-16 devnull }
190 ad80f870 2005-12-16 devnull /* END TAG */
191 b3994ec5 2003-12-11 devnull }
192 f8dea3c1 2005-12-16 devnull
193 33dc4226 2005-10-31 devnull
194 33dc4226 2005-10-31 devnull r1 = r;
195 33dc4226 2005-10-31 devnull r1.min.y = y;
196 cd1d0ab0 2006-01-12 devnull if(tagresized || !safe || !eqrect(w->body.all, r1)){
197 cd1d0ab0 2006-01-12 devnull oy = y;
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);
203 33dc4226 2005-10-31 devnull y++;
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;
206 33dc4226 2005-10-31 devnull }else{
207 33dc4226 2005-10-31 devnull r1.min.y = y;
208 33dc4226 2005-10-31 devnull r1.max.y = y;
209 33dc4226 2005-10-31 devnull }
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;
216 b3994ec5 2003-12-11 devnull }
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;
219 b3994ec5 2003-12-11 devnull }
220 b3994ec5 2003-12-11 devnull
221 b3994ec5 2003-12-11 devnull void
222 b3994ec5 2003-12-11 devnull winlock1(Window *w, int owner)
223 b3994ec5 2003-12-11 devnull {
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;
227 b3994ec5 2003-12-11 devnull }
228 b3994ec5 2003-12-11 devnull
229 b3994ec5 2003-12-11 devnull void
230 b3994ec5 2003-12-11 devnull winlock(Window *w, int owner)
231 b3994ec5 2003-12-11 devnull {
232 b3994ec5 2003-12-11 devnull int i;
233 b3994ec5 2003-12-11 devnull File *f;
234 b3994ec5 2003-12-11 devnull
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);
238 b3994ec5 2003-12-11 devnull }
239 b3994ec5 2003-12-11 devnull
240 b3994ec5 2003-12-11 devnull void
241 b3994ec5 2003-12-11 devnull winunlock(Window *w)
242 b3994ec5 2003-12-11 devnull {
243 b3994ec5 2003-12-11 devnull int i;
244 b3994ec5 2003-12-11 devnull File *f;
245 b3994ec5 2003-12-11 devnull
246 725d9fd7 2005-07-28 devnull /*
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.
250 725d9fd7 2005-07-28 devnull */
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);
257 b3994ec5 2003-12-11 devnull }
258 b3994ec5 2003-12-11 devnull }
259 b3994ec5 2003-12-11 devnull
260 b3994ec5 2003-12-11 devnull void
261 b3994ec5 2003-12-11 devnull winmousebut(Window *w)
262 b3994ec5 2003-12-11 devnull {
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)));
265 b3994ec5 2003-12-11 devnull }
266 b3994ec5 2003-12-11 devnull
267 b3994ec5 2003-12-11 devnull void
268 b3994ec5 2003-12-11 devnull windirfree(Window *w)
269 b3994ec5 2003-12-11 devnull {
270 b3994ec5 2003-12-11 devnull int i;
271 b3994ec5 2003-12-11 devnull Dirlist *dl;
272 b3994ec5 2003-12-11 devnull
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);
278 b3994ec5 2003-12-11 devnull }
279 b3994ec5 2003-12-11 devnull free(w->dlp);
280 b3994ec5 2003-12-11 devnull }
281 b3994ec5 2003-12-11 devnull w->dlp = nil;
282 b3994ec5 2003-12-11 devnull w->ndl = 0;
283 b3994ec5 2003-12-11 devnull }
284 b3994ec5 2003-12-11 devnull
285 b3994ec5 2003-12-11 devnull void
286 b3994ec5 2003-12-11 devnull winclose(Window *w)
287 b3994ec5 2003-12-11 devnull {
288 b3994ec5 2003-12-11 devnull int i;
289 b3994ec5 2003-12-11 devnull
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);
301 b3994ec5 2003-12-11 devnull }
302 b3994ec5 2003-12-11 devnull }
303 b3994ec5 2003-12-11 devnull
304 b3994ec5 2003-12-11 devnull void
305 b3994ec5 2003-12-11 devnull windelete(Window *w)
306 b3994ec5 2003-12-11 devnull {
307 b3994ec5 2003-12-11 devnull Xfid *x;
308 b3994ec5 2003-12-11 devnull
309 b3994ec5 2003-12-11 devnull x = w->eventx;
310 b3994ec5 2003-12-11 devnull if(x){
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 */
316 b3994ec5 2003-12-11 devnull }
317 b3994ec5 2003-12-11 devnull }
318 b3994ec5 2003-12-11 devnull
319 b3994ec5 2003-12-11 devnull void
320 b3994ec5 2003-12-11 devnull winundo(Window *w, int isundo)
321 b3994ec5 2003-12-11 devnull {
322 b3994ec5 2003-12-11 devnull Text *body;
323 b3994ec5 2003-12-11 devnull int i;
324 b3994ec5 2003-12-11 devnull File *f;
325 b3994ec5 2003-12-11 devnull Window *v;
326 b3994ec5 2003-12-11 devnull
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;
338 b3994ec5 2003-12-11 devnull }
339 b3994ec5 2003-12-11 devnull }
340 b3994ec5 2003-12-11 devnull winsettag(w);
341 b3994ec5 2003-12-11 devnull }
342 b3994ec5 2003-12-11 devnull
343 b3994ec5 2003-12-11 devnull void
344 b3994ec5 2003-12-11 devnull winsetname(Window *w, Rune *name, int n)
345 b3994ec5 2003-12-11 devnull {
346 b3994ec5 2003-12-11 devnull Text *t;
347 b3994ec5 2003-12-11 devnull Window *v;
348 b3994ec5 2003-12-11 devnull int i;
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)
353 b3994ec5 2003-12-11 devnull return;
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;
364 b3994ec5 2003-12-11 devnull }
365 b3994ec5 2003-12-11 devnull }
366 b3994ec5 2003-12-11 devnull
367 b3994ec5 2003-12-11 devnull void
368 b3994ec5 2003-12-11 devnull wintype(Window *w, Text *t, Rune r)
369 b3994ec5 2003-12-11 devnull {
370 b3994ec5 2003-12-11 devnull int i;
371 b3994ec5 2003-12-11 devnull
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);
377 b3994ec5 2003-12-11 devnull }
378 b3994ec5 2003-12-11 devnull
379 b3994ec5 2003-12-11 devnull void
380 b3994ec5 2003-12-11 devnull wincleartag(Window *w)
381 b3994ec5 2003-12-11 devnull {
382 b3994ec5 2003-12-11 devnull int i, n;
383 b3994ec5 2003-12-11 devnull Rune *r;
384 b3994ec5 2003-12-11 devnull
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')
391 b3994ec5 2003-12-11 devnull break;
392 b3994ec5 2003-12-11 devnull for(; i<n; i++)
393 b3994ec5 2003-12-11 devnull if(r[i] == '|')
394 b3994ec5 2003-12-11 devnull break;
395 b3994ec5 2003-12-11 devnull if(i == n)
396 b3994ec5 2003-12-11 devnull return;
397 b3994ec5 2003-12-11 devnull i++;
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);
406 b3994ec5 2003-12-11 devnull }
407 b3994ec5 2003-12-11 devnull
408 b3994ec5 2003-12-11 devnull void
409 b3994ec5 2003-12-11 devnull winsettag1(Window *w)
410 b3994ec5 2003-12-11 devnull {
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')
430 b3994ec5 2003-12-11 devnull break;
431 d96da29b 2005-03-14 devnull
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);
437 d96da29b 2005-03-14 devnull
438 3b33df99 2005-03-18 devnull /*
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?
441 3b33df99 2005-03-18 devnull */
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){
452 3b33df99 2005-03-18 devnull /*
453 3b33df99 2005-03-18 devnull * XXX common case - typing at end of name
454 3b33df99 2005-03-18 devnull */
455 3b33df99 2005-03-18 devnull w->tag.q0 = q0+ntagname-i;
456 3b33df99 2005-03-18 devnull w->tag.q1 = q1+ntagname-i;
457 3b33df99 2005-03-18 devnull }
458 b3994ec5 2003-12-11 devnull }
459 d96da29b 2005-03-14 devnull
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);
462 b3994ec5 2003-12-11 devnull i = 0;
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);
470 b3994ec5 2003-12-11 devnull i += 5;
471 b3994ec5 2003-12-11 devnull }
472 b3994ec5 2003-12-11 devnull if(w->body.file->epsilon.nc > 0){
473 b3994ec5 2003-12-11 devnull runemove(new+i, Lredo, 5);
474 b3994ec5 2003-12-11 devnull i += 5;
475 b3994ec5 2003-12-11 devnull }
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);
479 b3994ec5 2003-12-11 devnull i += 4;
480 b3994ec5 2003-12-11 devnull }
481 b3994ec5 2003-12-11 devnull }
482 b3994ec5 2003-12-11 devnull if(w->isdir){
483 b3994ec5 2003-12-11 devnull runemove(new+i, Lget, 4);
484 b3994ec5 2003-12-11 devnull i += 4;
485 b3994ec5 2003-12-11 devnull }
486 b3994ec5 2003-12-11 devnull runemove(new+i, Lpipe, 2);
487 b3994ec5 2003-12-11 devnull i += 2;
488 b3994ec5 2003-12-11 devnull r = runestrchr(old, '|');
489 b3994ec5 2003-12-11 devnull if(r)
490 b3994ec5 2003-12-11 devnull k = r-old+1;
491 b3994ec5 2003-12-11 devnull else{
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);
495 b3994ec5 2003-12-11 devnull i += 6;
496 b3994ec5 2003-12-11 devnull }
497 b3994ec5 2003-12-11 devnull }
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));
502 d96da29b 2005-03-14 devnull
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;
507 b3994ec5 2003-12-11 devnull n = k;
508 b3994ec5 2003-12-11 devnull if(n > i)
509 b3994ec5 2003-12-11 devnull 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])
512 b3994ec5 2003-12-11 devnull break;
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, '|');
519 b3994ec5 2003-12-11 devnull if(r){
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;
525 b3994ec5 2003-12-11 devnull }
526 b3994ec5 2003-12-11 devnull }
527 b3994ec5 2003-12-11 devnull }
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);
542 cd1d0ab0 2006-01-12 devnull }
543 b3994ec5 2003-12-11 devnull }
544 b3994ec5 2003-12-11 devnull
545 b3994ec5 2003-12-11 devnull void
546 b3994ec5 2003-12-11 devnull winsettag(Window *w)
547 b3994ec5 2003-12-11 devnull {
548 b3994ec5 2003-12-11 devnull int i;
549 b3994ec5 2003-12-11 devnull File *f;
550 b3994ec5 2003-12-11 devnull Window *v;
551 b3994ec5 2003-12-11 devnull
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);
557 b3994ec5 2003-12-11 devnull }
558 b3994ec5 2003-12-11 devnull }
559 b3994ec5 2003-12-11 devnull
560 b3994ec5 2003-12-11 devnull void
561 b3994ec5 2003-12-11 devnull wincommit(Window *w, Text *t)
562 b3994ec5 2003-12-11 devnull {
563 b3994ec5 2003-12-11 devnull Rune *r;
564 b3994ec5 2003-12-11 devnull int i;
565 b3994ec5 2003-12-11 devnull File *f;
566 b3994ec5 2003-12-11 devnull
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)
573 b3994ec5 2003-12-11 devnull return;
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')
578 b3994ec5 2003-12-11 devnull break;
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){
581 b3994ec5 2003-12-11 devnull seq++;
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);
587 b3994ec5 2003-12-11 devnull }
588 b3994ec5 2003-12-11 devnull free(r);
589 b3994ec5 2003-12-11 devnull }
590 b3994ec5 2003-12-11 devnull
591 b3994ec5 2003-12-11 devnull void
592 b3994ec5 2003-12-11 devnull winaddincl(Window *w, Rune *r, int n)
593 b3994ec5 2003-12-11 devnull {
594 b3994ec5 2003-12-11 devnull char *a;
595 b3994ec5 2003-12-11 devnull Dir *d;
596 b3994ec5 2003-12-11 devnull Runestr rs;
597 b3994ec5 2003-12-11 devnull
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;
613 b3994ec5 2003-12-11 devnull }
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);
619 b3994ec5 2003-12-11 devnull return;
620 b3994ec5 2003-12-11 devnull }
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);
628 b3994ec5 2003-12-11 devnull return;
629 b3994ec5 2003-12-11 devnull
630 b3994ec5 2003-12-11 devnull Rescue:
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);
634 b3994ec5 2003-12-11 devnull return;
635 b3994ec5 2003-12-11 devnull }
636 b3994ec5 2003-12-11 devnull
637 b3994ec5 2003-12-11 devnull int
638 b3994ec5 2003-12-11 devnull winclean(Window *w, int conservative) /* as it stands, conservative is always TRUE */
639 b3994ec5 2003-12-11 devnull {
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);
647 b3994ec5 2003-12-11 devnull else{
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");
651 b3994ec5 2003-12-11 devnull }
652 b3994ec5 2003-12-11 devnull w->dirty = FALSE;
653 b3994ec5 2003-12-11 devnull return FALSE;
654 b3994ec5 2003-12-11 devnull }
655 b3994ec5 2003-12-11 devnull return TRUE;
656 b3994ec5 2003-12-11 devnull }
657 b3994ec5 2003-12-11 devnull
658 627bae9c 2005-01-04 devnull char*
659 b3994ec5 2003-12-11 devnull winctlprint(Window *w, char *buf, int fonts)
660 b3994ec5 2003-12-11 devnull {
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;
667 b3994ec5 2003-12-11 devnull }
668 b3994ec5 2003-12-11 devnull
669 b3994ec5 2003-12-11 devnull void
670 b3994ec5 2003-12-11 devnull winevent(Window *w, char *fmt, ...)
671 b3994ec5 2003-12-11 devnull {
672 b3994ec5 2003-12-11 devnull int n;
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;
676 b3994ec5 2003-12-11 devnull
677 b3994ec5 2003-12-11 devnull if(w->nopen[QWevent] == 0)
678 b3994ec5 2003-12-11 devnull return;
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;
693 b3994ec5 2003-12-11 devnull if(x){
694 b3994ec5 2003-12-11 devnull w->eventx = nil;
695 b3994ec5 2003-12-11 devnull sendp(x->c, nil);
696 d96da29b 2005-03-14 devnull }
697 d96da29b 2005-03-14 devnull }
698 d96da29b 2005-03-14 devnull
699 d96da29b 2005-03-14 devnull /*
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.
702 d96da29b 2005-03-14 devnull *
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:
705 d96da29b 2005-03-14 devnull *
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.
716 d96da29b 2005-03-14 devnull *
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.
719 d96da29b 2005-03-14 devnull *
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.
722 d96da29b 2005-03-14 devnull */
723 d96da29b 2005-03-14 devnull
724 d96da29b 2005-03-14 devnull int
725 d96da29b 2005-03-14 devnull expandenv(Rune **rp, uint *np)
726 d96da29b 2005-03-14 devnull {
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;
732 d96da29b 2005-03-14 devnull
733 d96da29b 2005-03-14 devnull if(!dodollarsigns)
734 d96da29b 2005-03-14 devnull return FALSE;
735 d96da29b 2005-03-14 devnull
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++)
741 d96da29b 2005-03-14 devnull ;
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;
747 b3994ec5 2003-12-11 devnull }
748 d96da29b 2005-03-14 devnull
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++] = '/';
760 d96da29b 2005-03-14 devnull }
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;
765 b3994ec5 2003-12-11 devnull }
766 d96da29b 2005-03-14 devnull
767 d96da29b 2005-03-14 devnull extern char **environ;
768 d96da29b 2005-03-14 devnull Rune **runeenv;
769 d96da29b 2005-03-14 devnull
770 d96da29b 2005-03-14 devnull /*
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.
774 d96da29b 2005-03-14 devnull */
775 d96da29b 2005-03-14 devnull int
776 d96da29b 2005-03-14 devnull runeenvcmp(const void *va, const void *vb)
777 d96da29b 2005-03-14 devnull {
778 d96da29b 2005-03-14 devnull Rune *a, *b;
779 d96da29b 2005-03-14 devnull int na, nb;
780 d96da29b 2005-03-14 devnull
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);
796 d96da29b 2005-03-14 devnull }
797 d96da29b 2005-03-14 devnull
798 d96da29b 2005-03-14 devnull void
799 d96da29b 2005-03-14 devnull mkruneenv(void)
800 d96da29b 2005-03-14 devnull {
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;
804 d96da29b 2005-03-14 devnull
805 d96da29b 2005-03-14 devnull n = 0;
806 d96da29b 2005-03-14 devnull for(i=0; environ[i]; i++){
807 d96da29b 2005-03-14 devnull /*
808 d96da29b 2005-03-14 devnull * Ignore some pollution.
809 d96da29b 2005-03-14 devnull */
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;
816 d96da29b 2005-03-14 devnull
817 d96da29b 2005-03-14 devnull /*
818 d96da29b 2005-03-14 devnull * Must be a rooted path.
819 d96da29b 2005-03-14 devnull */
820 d96da29b 2005-03-14 devnull if((p = strchr(environ[i], '=')) == nil || *(p+1) != '/')
821 d96da29b 2005-03-14 devnull continue;
822 d96da29b 2005-03-14 devnull
823 d96da29b 2005-03-14 devnull /*
824 d96da29b 2005-03-14 devnull * Only use the ones that we accept in look - all isfilec
825 d96da29b 2005-03-14 devnull */
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;
832 d96da29b 2005-03-14 devnull break;
833 d96da29b 2005-03-14 devnull }
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;
837 d96da29b 2005-03-14 devnull }
838 d96da29b 2005-03-14 devnull }
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);
842 d96da29b 2005-03-14 devnull }
843 d96da29b 2005-03-14 devnull
844 d96da29b 2005-03-14 devnull int
845 d96da29b 2005-03-14 devnull abbrevenv(Rune **rp, uint *np)
846 d96da29b 2005-03-14 devnull {
847 d96da29b 2005-03-14 devnull int i, len, alen;
848 d96da29b 2005-03-14 devnull Rune *r, *p, *q;
849 d96da29b 2005-03-14 devnull uint n;
850 d96da29b 2005-03-14 devnull
851 d96da29b 2005-03-14 devnull if(!dodollarsigns)
852 d96da29b 2005-03-14 devnull return FALSE;
853 d96da29b 2005-03-14 devnull
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;
858 d96da29b 2005-03-14 devnull
859 d96da29b 2005-03-14 devnull if(runeenv == nil)
860 d96da29b 2005-03-14 devnull mkruneenv();
861 d96da29b 2005-03-14 devnull
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;
875 d96da29b 2005-03-14 devnull }
876 d96da29b 2005-03-14 devnull }
877 d96da29b 2005-03-14 devnull return FALSE;
878 d96da29b 2005-03-14 devnull }
879 d96da29b 2005-03-14 devnull