all repos — acme @ 690197f35d10c46cf487c496e33f08228ffe0c32

fork of the acme editor from plan9port - keybinds, tweaks, config.h, etc

mail/win.c (raw)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
#include <u.h>
#include <libc.h>
#include <bio.h>
#include <thread.h>
#include <plumb.h>
#include <9pclient.h>
#include "dat.h"

Window*
newwindow(void)
{
	char buf[12];
	Window *w;

	w = emalloc(sizeof(Window));
	w->ctl = fsopen(acmefs, "new/ctl", ORDWR|OCEXEC);
	if(w->ctl == nil || fsread(w->ctl, buf, 12)!=12)
		error("can't open window ctl file: %r");

	w->id = atoi(buf);
	w->event = winopenfile(w, "event");
	w->addr = nil;	/* will be opened when needed */
	w->body = nil;
	w->data = nil;
	w->cevent = chancreate(sizeof(Event*), 0);
	w->ref = 1;
	return w;
}

void
winincref(Window *w)
{
	qlock(&w->lk);
	++w->ref;
	qunlock(&w->lk);
}

void
windecref(Window *w)
{
	qlock(&w->lk);
	if(--w->ref > 0){
		qunlock(&w->lk);
		return;
	}
	fsclose(w->event);
	chanfree(w->cevent);
	free(w);
}

void
winsetdump(Window *w, char *dir, char *cmd)
{
	if(dir != nil)
		ctlprint(w->ctl, "dumpdir %s\n", dir);
	if(cmd != nil)
		ctlprint(w->ctl, "dump %s\n", cmd);
}

void
wineventproc(void *v)
{
	Window *w;
	int i;

	w = v;
	for(i=0; ; i++){
		if(i >= NEVENT)
			i = 0;
		wingetevent(w, &w->e[i]);
		sendp(w->cevent, &w->e[i]);
	}
}

static CFid*
winopenfile1(Window *w, char *f, int m)
{
	char buf[64];
	CFid* fd;

	sprint(buf, "%d/%s", w->id, f);
	fd = fsopen(acmefs, buf, m|OCEXEC);
	if(fd == nil)
		error("can't open window file %s: %r", f);
	return fd;
}

CFid*
winopenfile(Window *w, char *f)
{
	return winopenfile1(w, f, ORDWR);
}

void
wintagwrite(Window *w, char *s, int n)
{
	CFid* fid;

	fid = winopenfile(w, "tag");
	if(fswrite(fid, s, n) != n)
		error("tag write: %r");
	fsclose(fid);
}

void
winname(Window *w, char *s)
{
	int len;
	char *ns, *sp;
	Rune r = L'␣';	/* visible space */

	len = 0;
	ns = emalloc(strlen(s)*runelen(r) + 1);
	for(sp = s; *sp != '\0'; sp++, len++){
		if(isspace(*sp)){
			len += runetochar(ns+len, &r)-1;
			continue;
		}
		*(ns+len) = *sp;
	}
	ctlprint(w->ctl, "name %s\n", ns);
	free(ns);
	return;
}

void
winopenbody(Window *w, int mode)
{
	char buf[256];
	CFid* fid;

	sprint(buf, "%d/body", w->id);
	fid = fsopen(acmefs, buf, mode|OCEXEC);
	w->body = fid;
	if(w->body == nil)
		error("can't open window body file: %r");
}

void
winclosebody(Window *w)
{
	if(w->body != nil){
		fsclose(w->body);
		w->body = nil;
	}
}

void
winwritebody(Window *w, char *s, int n)
{
	if(w->body == nil)
		winopenbody(w, OWRITE);
	if(fswrite(w->body, s, n) != n)
		error("write error to window: %r");
}

int
wingetec(Window *w)
{
	if(w->nbuf == 0){
		w->nbuf = fsread(w->event, w->buf, sizeof w->buf);
		if(w->nbuf <= 0){
			/* probably because window has exited, and only called by wineventproc, so just shut down */
			windecref(w);
			threadexits(nil);
		}
		w->bufp = w->buf;
	}
	w->nbuf--;
	return *w->bufp++;
}

int
wingeten(Window *w)
{
	int n, c;

	n = 0;
	while('0'<=(c=wingetec(w)) && c<='9')
		n = n*10+(c-'0');
	if(c != ' ')
		error("event number syntax");
	return n;
}

int
wingeter(Window *w, char *buf, int *nb)
{
	Rune r;
	int n;

	r = wingetec(w);
	buf[0] = r;
	n = 1;
	if(r >= Runeself) {
		while(!fullrune(buf, n))
			buf[n++] = wingetec(w);
		chartorune(&r, buf);
	} 
	*nb = n;
	return r;
}

void
wingetevent(Window *w, Event *e)
{
	int i, nb;

	e->c1 = wingetec(w);
	e->c2 = wingetec(w);
	e->q0 = wingeten(w);
	e->q1 = wingeten(w);
	e->flag = wingeten(w);
	e->nr = wingeten(w);
	if(e->nr > EVENTSIZE)
		error("event string too long");
	e->nb = 0;
	for(i=0; i<e->nr; i++){
		e->r[i] = wingeter(w, e->b+e->nb, &nb);
		e->nb += nb;
	}
	e->r[e->nr] = 0;
	e->b[e->nb] = 0;
	if(wingetec(w) != '\n')
		error("event syntax error");
}

void
winwriteevent(Window *w, Event *e)
{
	fsprint(w->event, "%c%c%d %d\n", e->c1, e->c2, e->q0, e->q1);
}

void
winread(Window *w, uint q0, uint q1, char *data)
{
	int m, n, nr;
	char buf[256];

	if(w->addr == nil)
		w->addr = winopenfile(w, "addr");
	if(w->data == nil)
		w->data = winopenfile(w, "data");
	m = q0;
	while(m < q1){
		n = sprint(buf, "#%d", m);
		if(fswrite(w->addr, buf, n) != n)
			error("error writing addr: %r");
		n = fsread(w->data, buf, sizeof buf);
		if(n <= 0)
			error("reading data: %r");
		nr = utfnlen(buf, n);
		while(m+nr >q1){
			do; while(n>0 && (buf[--n]&0xC0)==0x80);
			--nr;
		}
		if(n == 0)
			break;
		memmove(data, buf, n);
		data += n;
		*data = 0;
		m += nr;
	}
}

void
windormant(Window *w)
{
	if(w->addr != nil){
		fsclose(w->addr);
		w->addr = nil;
	}
	if(w->body != nil){
		fsclose(w->body);
		w->body = nil;
	}
	if(w->data != nil){
		fsclose(w->data);
		w->data = nil;
	}
}


int
windel(Window *w, int sure)
{
	if(sure)
		fswrite(w->ctl, "delete\n", 7);
	else if(fswrite(w->ctl, "del\n", 4) != 4)
		return 0;
	/* event proc will die due to read error from event file */
	windormant(w);
	fsclose(w->ctl);
	w->ctl = nil;
	return 1;
}

void
winclean(Window *w)
{
	ctlprint(w->ctl, "clean\n");
}

int
winsetaddr(Window *w, char *addr, int errok)
{
	if(w->addr == nil)
		w->addr = winopenfile(w, "addr");
	if(fswrite(w->addr, addr, strlen(addr)) < 0){
		if(!errok)
			error("error writing addr(%s): %r", addr);
		return 0;
	}
	return 1;
}

int
winselect(Window *w, char *addr, int errok)
{
	if(winsetaddr(w, addr, errok)){
		ctlprint(w->ctl, "dot=addr\n");
		return 1;
	}
	return 0;
}

char*
winreadbody(Window *w, int *np)	/* can't use readfile because acme doesn't report the length */
{
	char *s;
	int m, na, n;

	if(w->body != nil)
		winclosebody(w);
	winopenbody(w, OREAD);
	s = nil;
	na = 0;
	n = 0;
	for(;;){
		if(na < n+512){
			na += 1024;
			s = realloc(s, na+1);
		}
		m = fsread(w->body, s+n, na-n);
		if(m <= 0)
			break;
		n += m;
	}
	s[n] = 0;
	winclosebody(w);
	*np = n;
	return s;
}

char*
winselection(Window *w)
{
	int m, n;
	char *buf;
	char tmp[256];
	CFid* fid;

	fid = winopenfile1(w, "rdsel", OREAD);
	if(fid == nil)
		error("can't open rdsel: %r");
	n = 0;
	buf = nil;
	for(;;){
		m = fsread(fid, tmp, sizeof tmp);
		if(m <= 0)
			break;
		buf = erealloc(buf, n+m+1);
		memmove(buf+n, tmp, m);
		n += m;
		buf[n] = '\0';
	}
	fsclose(fid);
	return buf;
}