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