master ohmu/acme2k / src / cmd / acme / look.c
ohmu  ·  2026-09-21
  1#include <u.h>
  2#include <libc.h>
  3#include <draw.h>
  4#include <thread.h>
  5#include <cursor.h>
  6#include <mouse.h>
  7#include <keyboard.h>
  8#include <frame.h>
  9#include <fcall.h>
 10#include <regexp.h>
 11#include <9pclient.h>
 12#include <plumb.h>
 13#include <libsec.h>
 14#include "dat.h"
 15#include "fns.h"
 16
 17CFid *plumbsendfid;
 18CFid *plumbeditfid;
 19
 20Window*	openfile(Text*, Expand*);
 21
 22int	nuntitled;
 23
 24void
 25plumbthread(void *v)
 26{
 27	CFid *fid;
 28	Plumbmsg *m;
 29	Timer *t;
 30
 31	USED(v);
 32	threadsetname("plumbproc");
 33	
 34	/*
 35	 * Loop so that if plumber is restarted, acme need not be.
 36	 */
 37	for(;;){
 38		/*
 39		 * Connect to plumber.
 40		 */
 41		plumbunmount();
 42		while((fid = plumbopenfid("edit", OREAD|OCEXEC)) == nil){
 43			t = timerstart(2000);
 44			recv(t->c, nil);
 45			timerstop(t);
 46		}
 47		plumbeditfid = fid;
 48		plumbsendfid = plumbopenfid("send", OWRITE|OCEXEC);
 49	
 50		/*
 51		 * Relay messages.
 52		 */
 53		for(;;){
 54			m = plumbrecvfid(plumbeditfid);
 55			if(m == nil)
 56				break;
 57			sendp(cplumb, m);
 58		}
 59
 60		/*
 61		 * Lost connection.
 62		 */
 63		fid = plumbsendfid;
 64		plumbsendfid = nil;
 65		fsclose(fid);
 66
 67		fid = plumbeditfid;
 68		plumbeditfid = nil;
 69		fsclose(fid);
 70	}
 71}
 72
 73void
 74startplumbing(void)
 75{
 76	cplumb = chancreate(sizeof(Plumbmsg*), 0);
 77	chansetname(cplumb, "cplumb");
 78	threadcreate(plumbthread, nil, STACK);
 79}
 80
 81
 82void
 83look3(Text *t, uint q0, uint q1, int external)
 84{
 85	int n, c, f, expanded;
 86	Text *ct;
 87	Expand e;
 88	Rune *r;
 89	uint p;
 90	Plumbmsg *m;
 91	Runestr dir;
 92	char buf[32];
 93
 94	ct = seltext;
 95	if(ct == nil)
 96		seltext = t;
 97	expanded = expand(t, q0, q1, &e);
 98	if(!external && t->w!=nil && t->w->nopen[QWevent]>0){
 99		/* send alphanumeric expansion to external client */
100		if(expanded == FALSE)
101			return;
102		f = 0;
103		if((e.u.at!=nil && t->w!=nil) || (e.nname>0 && lookfile(e.name, e.nname)!=nil))
104			f = 1;		/* acme can do it without loading a file */
105		if(q0!=e.q0 || q1!=e.q1)
106			f |= 2;	/* second (post-expand) message follows */
107		if(e.nname)
108			f |= 4;	/* it's a file name */
109		c = 'l';
110		if(t->what == Body)
111			c = 'L';
112		n = q1-q0;
113		if(n <= EVENTSIZE){
114			r = runemalloc(n);
115			bufread(&t->file->b, q0, r, n);
116			winevent(t->w, "%c%d %d %d %d %.*S\n", c, q0, q1, f, n, n, r);
117			free(r);
118		}else
119			winevent(t->w, "%c%d %d %d 0 \n", c, q0, q1, f, n);
120		if(q0==e.q0 && q1==e.q1)
121			return;
122		if(e.nname){
123			n = e.nname;
124			if(e.a1 > e.a0)
125				n += 1+(e.a1-e.a0);
126			r = runemalloc(n);
127			runemove(r, e.name, e.nname);
128			if(e.a1 > e.a0){
129				r[e.nname] = ':';
130				bufread(&e.u.at->file->b, e.a0, r+e.nname+1, e.a1-e.a0);
131			}
132		}else{
133			n = e.q1 - e.q0;
134			r = runemalloc(n);
135			bufread(&t->file->b, e.q0, r, n);
136		}
137		f &= ~2;
138		if(n <= EVENTSIZE)
139			winevent(t->w, "%c%d %d %d %d %.*S\n", c, e.q0, e.q1, f, n, n, r);
140		else
141			winevent(t->w, "%c%d %d %d 0 \n", c, e.q0, e.q1, f, n);
142		free(r);
143		goto Return;
144	}
145	if(plumbsendfid != nil){
146		/* send whitespace-delimited word to plumber */
147		m = emalloc(sizeof(Plumbmsg));
148		m->src = estrdup("acme");
149		m->dst = nil;
150		dir = dirname(t, nil, 0);
151		if(dir.nr==1 && dir.r[0]=='.'){	/* sigh */
152			free(dir.r);
153			dir.r = nil;
154			dir.nr = 0;
155		}
156		if(dir.nr == 0)
157			m->wdir = estrdup(wdir);
158		else
159			m->wdir = runetobyte(dir.r, dir.nr);
160		free(dir.r);
161		m->type = estrdup("text");
162		m->attr = nil;
163		buf[0] = '\0';
164		if(q1 == q0){
165			if(t->q1>t->q0 && t->q0<=q0 && q0<=t->q1){
166				q0 = t->q0;
167				q1 = t->q1;
168			}else{
169				p = q0;
170				while(q0>0 && (c=tgetc(t, q0-1))!=' ' && c!='\t' && c!='\n')
171					q0--;
172				while(q1<t->file->b.nc && (c=tgetc(t, q1))!=' ' && c!='\t' && c!='\n')
173					q1++;
174				if(q1 == q0){
175					plumbfree(m);
176					goto Return;
177				}
178				sprint(buf, "click=%d", p-q0);
179				m->attr = plumbunpackattr(buf);
180			}
181		}
182		r = runemalloc(q1-q0);
183		bufread(&t->file->b, q0, r, q1-q0);
184		m->data = runetobyte(r, q1-q0);
185		m->ndata = strlen(m->data);
186		free(r);
187		if(m->ndata<messagesize-1024 && plumbsendtofid(plumbsendfid, m) >= 0){
188			plumbfree(m);
189			goto Return;
190		}
191		plumbfree(m);
192		/* plumber failed to match; fall through */
193	}
194
195	/* interpret alphanumeric string ourselves */
196	if(expanded == FALSE)
197		return;
198	if(e.name || e.u.at)
199		openfile(t, &e);
200	else{
201		if(t->w == nil)
202			return;
203		ct = &t->w->body;
204		if(t->w != ct->w)
205			winlock(ct->w, 'M');
206		if(t == ct)
207			textsetselect(ct, e.q1, e.q1);
208		n = e.q1 - e.q0;
209		r = runemalloc(n);
210		bufread(&t->file->b, e.q0, r, n);
211		if(search(ct, r, n) && e.jump)
212			moveto(mousectl, addpt(frptofchar(&ct->fr, ct->fr.p0), Pt(4, ct->fr.font->height-4)));
213		if(t->w != ct->w)
214			winunlock(ct->w);
215		free(r);
216	}
217
218   Return:
219	free(e.name);
220	free(e.bname);
221}
222
223int
224plumbgetc(void *a, uint n)
225{
226	Rune *r;
227
228	r = a;
229	if(n>runestrlen(r))
230		return 0;
231	return r[n];
232}
233
234void
235plumblook(Plumbmsg *m)
236{
237	Expand e;
238	char *addr;
239
240	if(m->ndata >= BUFSIZE){
241		warning(nil, "insanely long file name (%d bytes) in plumb message (%.32s...)\n", m->ndata, m->data);
242		return;
243	}
244	e.q0 = 0;
245	e.q1 = 0;
246	if(m->data[0] == '\0')
247		return;
248	e.u.ar = nil;
249	e.bname = m->data;
250	e.name = bytetorune(e.bname, &e.nname);
251	e.jump = TRUE;
252	e.a0 = 0;
253	e.a1 = 0;
254	addr = plumblookup(m->attr, "addr");
255	if(addr != nil){
256		e.u.ar = bytetorune(addr, &e.a1);
257		e.agetc = plumbgetc;
258	}
259	drawtopwindow();
260	openfile(nil, &e);
261	free(e.name);
262	free(e.u.at);
263}
264
265void
266plumbshow(Plumbmsg *m)
267{
268	Window *w;
269	Rune rb[256], *r;
270	int nb, nr;
271	Runestr rs;
272	char *name, *p, namebuf[16];
273
274	drawtopwindow();
275	w = makenewwindow(nil);
276	name = plumblookup(m->attr, "filename");
277	if(name == nil){
278		name = namebuf;
279		nuntitled++;
280		snprint(namebuf, sizeof namebuf, "Untitled-%d", nuntitled);
281	}
282	p = nil;
283	if(name[0]!='/' && m->wdir!=nil && m->wdir[0]!='\0'){
284		nb = strlen(m->wdir) + 1 + strlen(name) + 1;
285		p = emalloc(nb);
286		snprint(p, nb, "%s/%s", m->wdir, name);
287		name = p;
288	}
289	cvttorunes(name, strlen(name), rb, &nb, &nr, nil);
290	free(p);
291	rs = cleanrname(runestr(rb, nr));
292	winsetname(w, rs.r, rs.nr);
293	r = runemalloc(m->ndata);
294	cvttorunes(m->data, m->ndata, r, &nb, &nr, nil);
295	textinsert(&w->body, 0, r, nr, TRUE);
296	free(r);
297	w->body.file->mod = FALSE;
298	w->dirty = FALSE;
299	winsettag(w);
300	textscrdraw(&w->body);
301	textsetselect(&w->tag, w->tag.file->b.nc, w->tag.file->b.nc);
302	xfidlog(w, "new");
303}
304
305int
306search(Text *ct, Rune *r, uint n)
307{
308	uint q, nb, maxn;
309	int around;
310	Rune *s, *b, *c;
311
312	if(n==0 || n>ct->file->b.nc)
313		return FALSE;
314	if(2*n > RBUFSIZE){
315		warning(nil, "string too long\n");
316		return FALSE;
317	}
318	maxn = max(2*n, RBUFSIZE);
319	s = fbufalloc();
320	b = s;
321	nb = 0;
322	b[nb] = 0;
323	around = 0;
324	q = ct->q1;
325	for(;;){
326		if(q >= ct->file->b.nc){
327			q = 0;
328			around = 1;
329			nb = 0;
330			b[nb] = 0;
331		}
332		if(nb > 0){
333			c = runestrchr(b, r[0]);
334			if(c == nil){
335				q += nb;
336				nb = 0;
337				b[nb] = 0;
338				if(around && q>=ct->q1)
339					break;
340				continue;
341			}
342			q += (c-b);
343			nb -= (c-b);
344			b = c;
345		}
346		/* reload if buffer covers neither string nor rest of file */
347		if(nb<n && nb!=ct->file->b.nc-q){
348			nb = ct->file->b.nc-q;
349			if(nb >= maxn)
350				nb = maxn-1;
351			bufread(&ct->file->b, q, s, nb);
352			b = s;
353			b[nb] = '\0';
354		}
355		/* this runeeq is fishy but the null at b[nb] makes it safe */
356		if(runeeq(b, n, r, n)==TRUE){
357			if(ct->w){
358				textshow(ct, q, q+n, 1);
359				winsettag(ct->w);
360			}else{
361				ct->q0 = q;
362				ct->q1 = q+n;
363			}
364			seltext = ct;
365			fbuffree(s);
366			return TRUE;
367		}
368		--nb;
369		b++;
370		q++;
371		if(around && q>=ct->q1)
372			break;
373	}
374	fbuffree(s);
375	return FALSE;
376}
377
378int
379isfilec(Rune r)
380{
381	static Rune Lx[] = { '.', '-', '+', '/', ':', 0 };
382	if(isalnum(r))
383		return TRUE;
384	if(runestrchr(Lx, r))
385		return TRUE;
386	return FALSE;
387}
388
389/* Runestr wrapper for cleanname */
390Runestr
391cleanrname(Runestr rs)
392{
393	char *s;
394	int nb, nulls;
395
396	s = runetobyte(rs.r, rs.nr);
397	cleanname(s);
398	cvttorunes(s, strlen(s), rs.r, &nb, &rs.nr, &nulls);
399	free(s);
400	return rs;
401}
402
403Runestr
404includefile(Rune *dir, Rune *file, int nfile)
405{
406	int m, n;
407	char *a;
408	Rune *r;
409	static Rune Lslash[] = { '/', 0 };
410
411	m = runestrlen(dir);
412	a = emalloc((m+1+nfile)*UTFmax+1);
413	sprint(a, "%S/%.*S", dir, nfile, file);
414	n = access(a, 0);
415	free(a);
416	if(n < 0)
417		return runestr(nil, 0);
418	r = runemalloc(m+1+nfile);
419	runemove(r, dir, m);
420	runemove(r+m, Lslash, 1);
421	runemove(r+m+1, file, nfile);
422	free(file);
423	return cleanrname(runestr(r, m+1+nfile));
424}
425
426static	Rune	*objdir;
427
428Runestr
429includename(Text *t, Rune *r, int n)
430{
431	Window *w;
432	char buf[128];
433	Rune Lsysinclude[] = { '/', 's', 'y', 's', '/', 'i', 'n', 'c', 'l', 'u', 'd', 'e', 0 };
434	Rune Lusrinclude[] = { '/', 'u', 's', 'r', '/', 'i', 'n', 'c', 'l', 'u', 'd', 'e', 0 };
435	Rune Lusrlocalinclude[] = { '/', 'u', 's', 'r', '/', 'l', 'o', 'c', 'a', 'l', 
436			'/', 'i', 'n', 'c', 'l', 'u', 'd', 'e', 0 };
437	Rune Lusrlocalplan9include[] = { '/', 'u', 's', 'r', '/', 'l', 'o', 'c', 'a', 'l', 
438			'/', 'p', 'l', 'a', 'n', '9', '/', 'i', 'n', 'c', 'l', 'u', 'd', 'e', 0 };
439	Runestr file;
440	int i;
441
442	if(objdir==nil && objtype!=nil){
443		sprint(buf, "/%s/include", objtype);
444		objdir = bytetorune(buf, &i);
445		objdir = runerealloc(objdir, i+1);
446		objdir[i] = '\0';	
447	}
448
449	w = t->w;
450	if(n==0 || r[0]=='/' || w==nil)
451		goto Rescue;
452	if(n>2 && r[0]=='.' && r[1]=='/')
453		goto Rescue;
454	file.r = nil;
455	file.nr = 0;
456	for(i=0; i<w->nincl && file.r==nil; i++)
457		file = includefile(w->incl[i], r, n);
458
459	if(file.r == nil)
460		file = includefile(Lsysinclude, r, n);
461	if(file.r == nil)
462		file = includefile(Lusrlocalplan9include, r, n);
463	if(file.r == nil)
464		file = includefile(Lusrlocalinclude, r, n);
465	if(file.r == nil)
466		file = includefile(Lusrinclude, r, n);
467	if(file.r==nil && objdir!=nil)
468		file = includefile(objdir, r, n);
469	if(file.r == nil)
470		goto Rescue;
471	return file;
472
473    Rescue:
474	return runestr(r, n);
475}
476
477Runestr
478dirname(Text *t, Rune *r, int n)
479{
480	Rune *b, c;
481	uint m, nt;
482	int slash;
483	Runestr tmp;
484
485	b = nil;
486	if(t==nil || t->w==nil)
487		goto Rescue;
488	nt = t->w->tag.file->b.nc;
489	if(nt == 0)
490		goto Rescue;
491	if(n>=1 && r[0]=='/')
492		goto Rescue;
493	b = runemalloc(nt+n+1);
494	bufread(&t->w->tag.file->b, 0, b, nt);
495	slash = -1;
496	for(m=0; m<nt; m++){
497		c = b[m];
498		if(c == '/')
499			slash = m;
500		if(c==' ' || c=='\t')
501			break;
502	}
503	if(slash < 0)
504		goto Rescue;
505	runemove(b+slash+1, r, n);
506	free(r);
507	return cleanrname(runestr(b, slash+1+n));
508
509    Rescue:
510	free(b);
511	tmp = runestr(r, n);
512	if(r)
513		return cleanrname(tmp);
514	return tmp;
515}
516
517int
518expandfile(Text *t, uint q0, uint q1, Expand *e)
519{
520	int i, n, nname, colon, eval;
521	uint amin, amax;
522	Rune *r, c;
523	Window *w;
524	Runestr rs;
525
526	amax = q1;
527	if(q1 == q0){
528		colon = -1;
529		while(q1<t->file->b.nc && isfilec(c=textreadc(t, q1))){
530			if(c == ':'){
531				colon = q1;
532				break;
533			}
534			q1++;
535		}
536		while(q0>0 && (isfilec(c=textreadc(t, q0-1)) || isaddrc(c) || isregexc(c))){
537			q0--;
538			if(colon<0 && c==':')
539				colon = q0;
540		}
541		/*
542		 * if it looks like it might begin file: , consume address chars after :
543		 * otherwise terminate expansion at :
544		 */
545		if(colon >= 0){
546			q1 = colon;
547			if(colon<t->file->b.nc-1 && isaddrc(textreadc(t, colon+1))){
548				q1 = colon+1;
549				while(q1<t->file->b.nc && isaddrc(textreadc(t, q1)))
550					q1++;
551			}
552		}
553		if(q1 > q0)
554			if(colon >= 0){	/* stop at white space */
555				for(amax=colon+1; amax<t->file->b.nc; amax++)
556					if((c=textreadc(t, amax))==' ' || c=='\t' || c=='\n')
557						break;
558			}else
559				amax = t->file->b.nc;
560	}
561	amin = amax;
562	e->q0 = q0;
563	e->q1 = q1;
564	n = q1-q0;
565	if(n == 0)
566		return FALSE;
567	/* see if it's a file name */
568	r = runemalloc(n);
569	bufread(&t->file->b, q0, r, n);
570	/* first, does it have bad chars? */
571	nname = -1;
572	for(i=0; i<n; i++){
573		c = r[i];
574		if(c==':' && nname<0){
575			if(q0+i+1<t->file->b.nc && (i==n-1 || isaddrc(textreadc(t, q0+i+1))))
576				amin = q0+i;
577			else
578				goto Isntfile;
579			nname = i;
580		}
581	}
582	if(nname == -1)
583		nname = n;
584	for(i=0; i<nname; i++)
585		if(!isfilec(r[i]))
586			goto Isntfile;
587	/*
588	 * See if it's a file name in <>, and turn that into an include
589	 * file name if so.  Should probably do it for "" too, but that's not
590	 * restrictive enough syntax and checking for a #include earlier on the
591	 * line would be silly.
592	 */
593	if(q0>0 && textreadc(t, q0-1)=='<' && q1<t->file->b.nc && textreadc(t, q1)=='>'){
594		rs = includename(t, r, nname);
595		r = rs.r;
596		nname = rs.nr;
597	}
598	else if(amin == q0)
599		goto Isfile;
600	else{
601		rs = dirname(t, r, nname);
602		r = rs.r;
603		nname = rs.nr;
604	}
605	e->bname = runetobyte(r, nname);
606	/* if it's already a window name, it's a file */
607	w = lookfile(r, nname);
608	if(w != nil)
609		goto Isfile;
610	/* if it's the name of a file, it's a file */
611	if(ismtpt(e->bname) || access(e->bname, 0) < 0){
612		free(e->bname);
613		e->bname = nil;
614		goto Isntfile;
615	}
616
617  Isfile:
618	e->name = r;
619	e->nname = nname;
620	e->u.at = t;
621	e->a0 = amin+1;
622	eval = FALSE;
623	address(TRUE, nil, range(-1,-1), range(0,0), t, e->a0, amax, tgetc, &eval, (uint*)&e->a1);
624	return TRUE;
625
626   Isntfile:
627	free(r);
628	return FALSE;
629}
630
631int
632expand(Text *t, uint q0, uint q1, Expand *e)
633{
634	memset(e, 0, sizeof *e);
635	e->agetc = tgetc;
636	/* if in selection, choose selection */
637	e->jump = TRUE;
638	if(q1==q0 && t->q1>t->q0 && t->q0<=q0 && q0<=t->q1){
639		q0 = t->q0;
640		q1 = t->q1;
641		if(t->what == Tag)
642			e->jump = FALSE;
643	}
644
645	if(expandfile(t, q0, q1, e))
646		return TRUE;
647
648	if(q0 == q1){
649		while(q1<t->file->b.nc && isalnum(textreadc(t, q1)))
650			q1++;
651		while(q0>0 && isalnum(textreadc(t, q0-1)))
652			q0--;
653	}
654	e->q0 = q0;
655	e->q1 = q1;
656	return q1 > q0;
657}
658
659Window*
660lookfile(Rune *s, int n)
661{
662	int i, j, k;
663	Window *w;
664	Column *c;
665	Text *t;
666
667	/* avoid terminal slash on directories */
668	if(n>1 && s[n-1] == '/')
669		--n;
670	for(j=0; j<row.ncol; j++){
671		c = row.col[j];
672		for(i=0; i<c->nw; i++){
673			w = c->w[i];
674			t = &w->body;
675			k = t->file->nname;
676			if(k>1 && t->file->name[k-1] == '/')
677				k--;
678			if(runeeq(t->file->name, k, s, n)){
679				w = w->body.file->curtext->w;
680				if(w->col != nil)	/* protect against race deleting w */
681					return w;
682			}
683		}
684	}
685	return nil;
686}
687
688Window*
689lookid(int id, int dump)
690{
691	int i, j;
692	Window *w;
693	Column *c;
694
695	for(j=0; j<row.ncol; j++){
696		c = row.col[j];
697		for(i=0; i<c->nw; i++){
698			w = c->w[i];
699			if(dump && w->dumpid == id)
700				return w;
701			if(!dump && w->id == id)
702				return w;
703		}
704	}
705	return nil;
706}
707
708
709Window*
710openfile(Text *t, Expand *e)
711{
712	Range r;
713	Window *w, *ow;
714	int eval, i, n;
715	Rune *rp;
716	Runestr rs;
717	uint dummy;
718
719	r.q0 = 0;
720	r.q1 = 0;
721	if(e->nname == 0){
722		w = t->w;
723		if(w == nil)
724			return nil;
725	}else{
726		w = lookfile(e->name, e->nname);
727		if(w == nil && e->name[0] != '/'){
728			/*
729			 * Unrooted path in new window.
730			 * This can happen if we type a pwd-relative path
731			 * in the topmost tag or the column tags.  
732			 * Most of the time plumber takes care of these,
733			 * but plumber might not be running or might not
734			 * be configured to accept plumbed directories.
735			 * Make the name a full path, just like we would if
736			 * opening via the plumber.
737			 */
738			n = utflen(wdir)+1+e->nname+1;
739			rp = runemalloc(n);
740			runesnprint(rp, n, "%s/%.*S", wdir, e->nname, e->name);
741			rs = cleanrname(runestr(rp, n-1));
742			free(e->name);
743			e->name = rs.r;
744			e->nname = rs.nr;
745			w = lookfile(e->name, e->nname);
746		}
747	}
748	if(w){
749		t = &w->body;
750		if(!t->col->safe && t->fr.maxlines==0) /* window is obscured by full-column window */
751			colgrow(t->col, t->col->w[0], 1);
752	}else{
753		ow = nil;
754		if(t)
755			ow = t->w;
756		w = makenewwindow(t);
757		t = &w->body;
758		winsetname(w, e->name, e->nname);
759		if(textload(t, 0, e->bname, 1) >= 0)
760			t->file->unread = FALSE;
761		t->file->mod = FALSE;
762		t->w->dirty = FALSE;
763		winsettag(t->w);
764		textsetselect(&t->w->tag, t->w->tag.file->b.nc, t->w->tag.file->b.nc);
765		if(ow != nil){
766			for(i=ow->nincl; --i>=0; ){
767				n = runestrlen(ow->incl[i]);
768				rp = runemalloc(n);
769				runemove(rp, ow->incl[i], n);
770				winaddincl(w, rp, n);
771			}
772			w->autoindent = ow->autoindent;
773		}else
774			w->autoindent = globalautoindent;
775		xfidlog(w, "new");
776	}
777	if(e->a1 == e->a0)
778		eval = FALSE;
779	else{
780		eval = TRUE;
781		r = address(TRUE, t, range(-1,-1), range(t->q0, t->q1), e->u.at, e->a0, e->a1, e->agetc, &eval, &dummy);
782		if(r.q0 > r.q1) {
783			eval = FALSE;
784			warning(nil, "addresses out of order\n");
785		}
786		if(eval == FALSE)
787			e->jump = FALSE;	/* don't jump if invalid address */
788	}
789	if(eval == FALSE){
790		r.q0 = t->q0;
791		r.q1 = t->q1;
792	}
793	textshow(t, r.q0, r.q1, 1);
794	winsettag(t->w);
795	seltext = t;
796	if(e->jump)
797		moveto(mousectl, addpt(frptofchar(&t->fr, t->fr.p0), Pt(4, font->height-4)));
798	return w;
799}
800
801void
802new(Text *et, Text *t, Text *argt, int flag1, int flag2, Rune *arg, int narg)
803{
804	int ndone;
805	Rune *a, *f;
806	int na, nf;
807	Expand e;
808	Runestr rs;
809	Window *w;
810
811	getarg(argt, FALSE, TRUE, &a, &na);
812	if(a){
813		new(et, t, nil, flag1, flag2, a, na);
814		if(narg == 0)
815			return;
816	}
817	/* loop condition: *arg is not a blank */
818	for(ndone=0; ; ndone++){
819		a = findbl(arg, narg, &na);
820		if(a == arg){
821			if(ndone==0 && et->col!=nil) {
822				w = coladd(et->col, nil, nil, -1);
823				winsettag(w);
824				xfidlog(w, "new");
825			}
826			break;
827		}
828		nf = narg-na;
829		f = runemalloc(nf);
830		runemove(f, arg, nf);
831		rs = dirname(et, f, nf);
832		memset(&e, 0, sizeof e);
833		e.name = rs.r;
834		e.nname = rs.nr;
835		e.bname = runetobyte(rs.r, rs.nr);
836		e.jump = TRUE;
837		openfile(et, &e);
838		free(e.name);
839		free(e.bname);
840		arg = skipbl(a, na, &narg);
841	}
842}