18 * These functions get called as:
20 * fn(et, t, argt, flag1, flag1, flag2, s, n);
22 * Where the arguments are:
24 * et: the Text* in which the executing event (click) occurred
25 * t: the Text* containing the current selection (Edit, Cut, Snarf, Paste)
26 * argt: the Text* containing the argument for a 2-1 click.
27 * e->flag1: from Exectab entry
28 * e->flag2: from Exectab entry
29 * s: the command line remainder (e.g., "x" if executing "Dump x")
30 * n: length of s (s is *not* NUL-terminated)
33 void doabort(Text*, Text*, Text*, int, int, Rune*, int);
34 void del(Text*, Text*, Text*, int, int, Rune*, int);
35 void delcol(Text*, Text*, Text*, int, int, Rune*, int);
36 void dotfiles(Text*, Text*, Text*, int, int, Rune*, int);
37 void dump(Text*, Text*, Text*, int, int, Rune*, int);
38 void edit(Text*, Text*, Text*, int, int, Rune*, int);
39 void xexit(Text*, Text*, Text*, int, int, Rune*, int);
40 void fontx(Text*, Text*, Text*, int, int, Rune*, int);
41 void get(Text*, Text*, Text*, int, int, Rune*, int);
42 void id(Text*, Text*, Text*, int, int, Rune*, int);
43 void incl(Text*, Text*, Text*, int, int, Rune*, int);
44 void indent(Text*, Text*, Text*, int, int, Rune*, int);
45 void xkill(Text*, Text*, Text*, int, int, Rune*, int);
46 void local(Text*, Text*, Text*, int, int, Rune*, int);
47 void look(Text*, Text*, Text*, int, int, Rune*, int);
48 void newcol(Text*, Text*, Text*, int, int, Rune*, int);
49 void paste(Text*, Text*, Text*, int, int, Rune*, int);
50 void put(Text*, Text*, Text*, int, int, Rune*, int);
51 void putall(Text*, Text*, Text*, int, int, Rune*, int);
52 void sendx(Text*, Text*, Text*, int, int, Rune*, int);
53 void sort(Text*, Text*, Text*, int, int, Rune*, int);
54 void tab(Text*, Text*, Text*, int, int, Rune*, int);
55 void zeroxx(Text*, Text*, Text*, int, int, Rune*, int);
57 typedef struct Exectab Exectab;
61 void (*fn)(Text*, Text*, Text*, int, int, Rune*, int);
67 static Rune LAbort[] = { 'A', 'b', 'o', 'r', 't', 0 };
68 static Rune LCut[] = { 'C', 'u', 't', 0 };
69 static Rune LDel[] = { 'D', 'e', 'l', 0 };
70 static Rune LDelcol[] = { 'D', 'e', 'l', 'c', 'o', 'l', 0 };
71 static Rune LDelete[] = { 'D', 'e', 'l', 'e', 't', 'e', 0 };
72 static Rune LDump[] = { 'D', 'u', 'm', 'p', 0 };
73 static Rune LEdit[] = { 'E', 'd', 'i', 't', 0 };
74 static Rune LExit[] = { 'E', 'x', 'i', 't', 0 };
75 static Rune LFont[] = { 'F', 'o', 'n', 't', 0 };
76 static Rune LGet[] = { 'G', 'e', 't', 0 };
77 static Rune LID[] = { 'I', 'D', 0 };
78 static Rune LIncl[] = { 'I', 'n', 'c', 'l', 0 };
79 static Rune LIndent[] = { 'I', 'n', 'd', 'e', 'n', 't', 0 };
80 static Rune LKill[] = { 'K', 'i', 'l', 'l', 0 };
81 static Rune LLoad[] = { 'L', 'o', 'a', 'd', 0 };
82 static Rune LLocal[] = { 'L', 'o', 'c', 'a', 'l', 0 };
83 static Rune LLook[] = { 'L', 'o', 'o', 'k', 0 };
84 static Rune LNew[] = { 'N', 'e', 'w', 0 };
85 static Rune LNewcol[] = { 'N', 'e', 'w', 'c', 'o', 'l', 0 };
86 static Rune LPaste[] = { 'P', 'a', 's', 't', 'e', 0 };
87 static Rune LPut[] = { 'P', 'u', 't', 0 };
88 static Rune LPutall[] = { 'P', 'u', 't', 'a', 'l', 'l', 0 };
89 static Rune LRedo[] = { 'R', 'e', 'd', 'o', 0 };
90 static Rune LSend[] = { 'S', 'e', 'n', 'd', 0 };
91 static Rune LSnarf[] = { 'S', 'n', 'a', 'r', 'f', 0 };
92 static Rune LSort[] = { 'S', 'o', 'r', 't', 0 };
93 static Rune LTab[] = { 'T', 'a', 'b', 0 };
94 static Rune LUndo[] = { 'U', 'n', 'd', 'o', 0 };
95 static Rune LZerox[] = { 'Z', 'e', 'r', 'o', 'x', 0 };
98 { LAbort, doabort, FALSE, XXX, XXX, },
99 { LCut, cut, TRUE, TRUE, TRUE },
100 { LDel, del, FALSE, FALSE, XXX },
101 { LDelcol, delcol, FALSE, XXX, XXX },
102 { LDelete, del, FALSE, TRUE, XXX },
103 { LDump, dump, FALSE, TRUE, XXX },
104 { LEdit, edit, FALSE, XXX, XXX },
105 { LExit, xexit, FALSE, XXX, XXX },
106 { LFont, fontx, FALSE, XXX, XXX },
107 { LGet, get, FALSE, TRUE, XXX },
108 { LID, id, FALSE, XXX, XXX },
109 { LIncl, incl, FALSE, XXX, XXX },
110 { LIndent, indent, FALSE, XXX, XXX },
111 { LKill, xkill, FALSE, XXX, XXX },
112 { LLoad, dump, FALSE, FALSE, XXX },
113 { LLocal, local, FALSE, XXX, XXX },
114 { LLook, look, FALSE, XXX, XXX },
115 { LNew, new, FALSE, XXX, XXX },
116 { LNewcol, newcol, FALSE, XXX, XXX },
117 { LPaste, paste, TRUE, TRUE, XXX },
118 { LPut, put, FALSE, XXX, XXX },
119 { LPutall, putall, FALSE, XXX, XXX },
120 { LRedo, undo, FALSE, FALSE, XXX },
121 { LSend, sendx, TRUE, XXX, XXX },
122 { LSnarf, cut, FALSE, TRUE, FALSE },
123 { LSort, sort, FALSE, XXX, XXX },
124 { LTab, tab, FALSE, XXX, XXX },
125 { LUndo, undo, FALSE, TRUE, XXX },
126 { LZerox, zeroxx, FALSE, XXX, XXX },
131 lookup(Rune *r, int n)
136 r = skipbl(r, n, &n);
141 for(e=exectab; e->name; e++)
142 if(runeeq(r, nr, e->name, runestrlen(e->name)) == TRUE)
152 return c=='<' || c=='|' || c=='>';
156 execute(Text *t, uint aq0, uint aq1, int external, Text *argt)
167 if(q1 == q0){ /* expand to find word (actually file name) */
168 /* if in selection, choose selection */
169 if(t->q1>t->q0 && t->q0<=q0 && q0<=t->q1){
173 while(q1<t->file->b.nc && isexecc(c=textreadc(t, q1)) && c!=':')
175 while(q0>0 && isexecc(c=textreadc(t, q0-1)) && c!=':')
181 r = runemalloc(q1-q0);
182 bufread(&t->file->b, q0, r, q1-q0);
183 e = lookup(r, q1-q0);
184 if(!external && t->w!=nil && t->w->nopen[QWevent]>0){
188 if(q0!=aq0 || q1!=aq1){
189 bufread(&t->file->b, aq0, r, aq1-aq0);
192 aa = getbytearg(argt, TRUE, TRUE, &a);
194 if(strlen(a) > EVENTSIZE){ /* too big; too bad */
197 warning(nil, "`argument string too long\n");
207 winevent(t->w, "%c%d %d %d %d %.*S\n", c, aq0, aq1, f, n, n, r);
209 winevent(t->w, "%c%d %d %d 0 \n", c, aq0, aq1, f, n);
210 if(q0!=aq0 || q1!=aq1){
212 bufread(&t->file->b, q0, r, n);
214 winevent(t->w, "%c%d %d 0 %d %.*S\n", c, q0, q1, n, n, r);
216 winevent(t->w, "%c%d %d 0 0 \n", c, q0, q1, n);
219 winevent(t->w, "%c0 0 0 %d %s\n", c, utflen(a), a);
221 winevent(t->w, "%c0 0 0 %d %s\n", c, utflen(aa), aa);
223 winevent(t->w, "%c0 0 0 0 \n", c);
231 if(e->mark && seltext!=nil)
232 if(seltext->what == Body){
234 filemark(seltext->w->body.file);
236 s = skipbl(r, q1-q0, &n);
237 s = findbl(s, n, &n);
238 s = skipbl(s, n, &n);
239 (*e->fn)(t, seltext, argt, e->flag1, e->flag2, s, n);
244 b = runetobyte(r, q1-q0);
246 dir = dirname(t, nil, 0);
247 if(dir.nr==1 && dir.r[0]=='.'){ /* sigh */
252 aa = getbytearg(argt, TRUE, TRUE, &a);
255 run(t->w, b, dir.r, dir.nr, TRUE, aa, a, FALSE);
259 printarg(Text *argt, uint q0, uint q1)
263 if(argt->what!=Body || argt->file->name==nil)
265 buf = emalloc(argt->file->nname+32);
267 sprint(buf, "%.*S:#%d", argt->file->nname, argt->file->name, q0);
269 sprint(buf, "%.*S:#%d,#%d", argt->file->nname, argt->file->name, q0, q1);
274 getarg(Text *argt, int doaddr, int dofile, Rune **rp, int *nrp)
285 textcommit(argt, TRUE);
286 if(expand(argt, argt->q0, argt->q1, &e)){
288 if(e.nname && dofile){
289 e.name = runerealloc(e.name, e.nname+1);
291 a = printarg(argt, e.q0, e.q1);
302 *rp = runemalloc(n+1);
303 bufread(&argt->file->b, e.q0, *rp, n);
305 a = printarg(argt, e.q0, e.q1);
311 getbytearg(Text *argt, int doaddr, int dofile, char **bp)
318 aa = getarg(argt, doaddr, dofile, &r, &n);
321 *bp = runetobyte(r, n);
327 doabort(Text *__0, Text *_0, Text *_1, int _2, int _3, Rune *_4, int _5)
340 warning(nil, "executing Abort again will call abort()\n");
346 newcol(Text *et, Text *_0, Text *_1, int _2, int _3, Rune *_4, int _5)
357 c = rowadd(et->row, nil, -1);
359 winsettag(coladd(c, nil, nil, -1));
363 delcol(Text *et, Text *_0, Text *_1, int _2, int _3, Rune *_4, int _5)
377 if(c==nil || colclean(c)==0)
379 for(i=0; i<c->nw; i++){
381 if(w->nopen[QWevent]+w->nopen[QWaddr]+w->nopen[QWdata]+w->nopen[QWxdata] > 0){
382 warning(nil, "can't delete column; %.*S is running an external command\n", w->body.file->nname, w->body.file->name);
386 rowclose(et->col->row, et->col, TRUE);
390 del(Text *et, Text *_0, Text *_1, int flag1, int _2, Rune *_3, int _4)
398 if(et->col==nil || et->w == nil)
400 if(flag1 || et->w->body.file->ntext>1 || winclean(et->w, FALSE))
401 colclose(et->col, et->w, TRUE);
405 sort(Text *et, Text *_0, Text *_1, int _2, int _3, Rune *_4, int _5)
419 seqof(Window *w, int isundo)
421 /* if it's undo, see who changed with us */
423 return w->body.file->seq;
424 /* if it's redo, see who we'll be sync'ed up with */
425 return fileredoseq(w->body.file);
429 undo(Text *et, Text *_0, Text *_1, int flag1, int _2, Rune *_3, int _4)
442 if(et==nil || et->w== nil)
444 seq = seqof(et->w, flag1);
446 /* nothing to undo */
450 * Undo the executing window first. Its display will update. other windows
451 * in the same file will not call show() and jump to a different location in the file.
452 * Simultaneous changes to other files will be chaotic, however.
454 winundo(et->w, flag1);
455 for(i=0; i<row.ncol; i++){
457 for(j=0; j<c->nw; j++){
461 if(seqof(w, flag1) == seq)
468 getname(Text *t, Text *argt, Rune *arg, int narg, int isput)
475 getarg(argt, FALSE, TRUE, &r, &n);
480 /* if are doing a Put, want to synthesize name even for non-existent file */
481 /* best guess is that file name doesn't contain a slash */
497 s = runetobyte(t->file->name, t->file->nname);
500 /* prefix with directory name if necessary */
503 if(n>0 && arg[0]!='/'){
504 dir = dirname(t, nil, 0);
505 if(dir.nr==1 && dir.r[0]=='.'){ /* sigh */
512 r = runemalloc(dir.nr+n+1);
513 runemove(r, dir.r, dir.nr);
515 if(dir.nr>0 && r[dir.nr]!='/' && n>0 && arg[0]!='/')
517 runemove(r+dir.nr, arg, n);
524 s = runetobyte(r, n);
534 zeroxx(Text *et, Text *t, Text *_1, int _2, int _3, Rune *_4, int _5)
546 if(t!=nil && t->w!=nil && t->w!=et->w){
555 if(t==nil || t->w==nil)
559 warning(nil, "%.*S is a directory; Zerox illegal\n", t->file->nname, t->file->name);
561 nw = coladd(t->w->col, nil, t->w, -1);
562 /* ugly: fix locks so w->unlock works */
563 winlock1(nw, t->w->owner);
570 get(Text *et, Text *t, Text *argt, int flag1, int _0, Rune *arg, int narg)
574 int i, n, dirty, samename, isdir;
582 if(et==nil || et->w==nil)
584 if(!et->w->isdir && (et->w->body.file->b.nc>0 && !winclean(et->w, TRUE)))
588 name = getname(t, argt, arg, narg, FALSE);
590 warning(nil, "no file name\n");
593 if(t->file->ntext>1){
595 isdir = (d!=nil && (d->qid.type & QTDIR));
598 warning(nil, "%s is a directory; can't read with multiple windows on it\n", name);
602 r = bytetorune(name, &n);
603 for(i=0; i<t->file->ntext; i++){
604 u = t->file->text[i];
605 /* second and subsequent calls with zero an already empty buffer, but OK */
609 samename = runeeq(r, n, t->file->name, t->file->nname);
610 textload(t, 0, name, samename);
612 t->file->mod = FALSE;
618 for(i=0; i<t->file->ntext; i++)
619 t->file->text[i]->w->dirty = dirty;
623 t->file->unread = FALSE;
624 for(i=0; i<t->file->ntext; i++){
625 u = t->file->text[i];
626 textsetselect(&u->w->tag, u->w->tag.file->b.nc, u->w->tag.file->b.nc);
632 putfile(File *f, int q0, int q1, Rune *namer, int nname)
643 name = runetobyte(namer, nname);
645 if(d!=nil && runeeq(namer, nname, f->name, f->nname)){
646 /* f->mtime+1 because when talking over NFS it's often off by a second */
647 if(f->dev!=d->dev || f->qidpath!=d->qid.path || f->mtime+1<d->mtime){
649 f->qidpath = d->qid.path;
652 warning(nil, "%s not written; file already exists\n", name);
654 warning(nil, "%s modified%s%s since last read\n", name, d->muid[0]?" by ":"", d->muid);
658 fd = create(name, OWRITE, 0666);
660 warning(nil, "can't create file %s: %r\n", name);
667 isapp = (d!=nil && d->length>0 && (d->qid.type&QTAPPEND));
669 warning(nil, "%s not written; file is append only\n", name);
673 for(q=q0; q<q1; q+=n){
675 if(n > BUFSIZE/UTFmax)
677 bufread(&f->b, q, r, n);
678 m = snprint(s, BUFSIZE+1, "%.*S", n, r);
679 if(write(fd, s, m) != m){
680 warning(nil, "can't write file %s: %r\n", name);
684 if(runeeq(namer, nname, f->name, f->nname)){
685 if(q0!=0 || q1!=f->b.nc){
695 f->qidpath = d->qid.path;
702 for(i=0; i<f->ntext; i++){
703 f->text[i]->w->putseq = f->seq;
704 f->text[i]->w->dirty = w->dirty;
729 put(Text *et, Text *_0, Text *argt, int _1, int _2, Rune *arg, int narg)
741 if(et==nil || et->w==nil || et->w->isdir)
745 name = getname(&w->body, argt, arg, narg, TRUE);
747 warning(nil, "no file name\n");
750 namer = bytetorune(name, &nname);
751 putfile(f, 0, f->b.nc, namer, nname);
756 dump(Text *_0, Text *_1, Text *argt, int isdump, int _2, Rune *arg, int narg)
765 name = runetobyte(arg, narg);
767 getbytearg(argt, FALSE, TRUE, &name);
771 rowload(&row, name, FALSE);
776 cut(Text *et, Text *t, Text *_0, int dosnarf, int docut, Rune *_2, int _3)
778 uint q0, q1, n, locked, c;
786 * if not executing a mouse chord (et != t) and snarfing (dosnarf)
787 * and executed Cut or Snarf in window tag (et->w != nil),
788 * then use the window body selection or the tag selection
789 * or do nothing at all.
791 if(et!=t && dosnarf && et->w!=nil){
792 if(et->w->body.q1>et->w->body.q0){
795 filemark(t->file); /* seq has been incremented by execute */
796 }else if(et->w->tag.q1>et->w->tag.q0)
801 if(t == nil) /* no selection */
805 if(t->w!=nil && et->w!=t->w){
820 bufdelete(&snarfbuf, 0, snarfbuf.nc);
826 bufread(&t->file->b, q0, r, n);
827 bufinsert(&snarfbuf, snarfbuf.nc, r, n);
834 textdelete(t, t->q0, t->q1, TRUE);
835 textsetselect(t, t->q0, t->q0);
840 }else if(dosnarf) /* Snarf command */
847 paste(Text *et, Text *t, Text *_0, int selectall, int tobody, Rune *_1, int _2)
857 /* if(tobody), use body of executing window (Paste or Send command) */
858 if(tobody && et!=nil && et->w!=nil){
860 filemark(t->file); /* seq has been incremented by execute */
866 if(t==nil || snarfbuf.nc==0)
868 if(t->w!=nil && et->w!=t->w){
874 cut(t, t, nil, FALSE, TRUE, nil, 0);
877 q1 = t->q0+snarfbuf.nc;
885 bufread(&snarfbuf, q, r, n);
886 textinsert(t, q0, r, n, TRUE);
892 textsetselect(t, t->q0, q1);
894 textsetselect(t, q1, q1);
899 if(t->w!=nil && et->w!=t->w)
904 look(Text *et, Text *t, Text *argt, int _0, int _1, Rune *arg, int narg)
915 search(t, arg, narg);
918 getarg(argt, FALSE, FALSE, &r, &n);
922 bufread(&t->file->b, t->q0, r, n);
929 static Rune Lnl[] = { '\n', 0 };
932 sendx(Text *et, Text *t, Text *_0, int _1, int _2, Rune *_3, int _4)
944 cut(t, t, nil, TRUE, FALSE, nil, 0);
945 textsetselect(t, t->file->b.nc, t->file->b.nc);
946 paste(t, t, nil, TRUE, TRUE, nil, 0);
947 if(textreadc(t, t->file->b.nc-1) != '\n'){
948 textinsert(t, t->file->b.nc, Lnl, 1, TRUE);
949 textsetselect(t, t->file->b.nc, t->file->b.nc);
954 edit(Text *et, Text *_0, Text *argt, int _1, int _2, Rune *arg, int narg)
965 getarg(argt, FALSE, TRUE, &r, &len);
971 editcmd(et, arg, narg);
975 xexit(Text *et, Text *_0, Text *_1, int _2, int _3, Rune *_4, int _5)
992 putall(Text *et, Text *_0, Text *_1, int _2, int _3, Rune *_4, int _5)
1007 for(i=0; i<row.ncol; i++){
1009 for(j=0; j<c->nw; j++){
1011 if(w->isscratch || w->isdir || w->body.file->nname==0)
1013 if(w->nopen[QWevent] > 0)
1015 a = runetobyte(w->body.file->name, w->body.file->nname);
1017 if(w->body.file->mod || w->body.ncache)
1019 warning(nil, "no auto-Put of %s: %r\n", a);
1021 wincommit(w, &w->body);
1022 put(&w->body, nil, nil, XXX, XXX, nil, 0);
1031 id(Text *et, Text *_0, Text *_1, int _2, int _3, Rune *_4, int _5)
1041 warning(nil, "/mnt/acme/%d/\n", et->w->id);
1045 local(Text *et, Text *_0, Text *argt, int _1, int _2, Rune *arg, int narg)
1054 aa = getbytearg(argt, TRUE, TRUE, &a);
1056 dir = dirname(et, nil, 0);
1057 if(dir.nr==1 && dir.r[0]=='.'){ /* sigh */
1062 run(nil, runetobyte(arg, narg), dir.r, dir.nr, FALSE, aa, a, FALSE);
1066 xkill(Text *_0, Text *_1, Text *argt, int _2, int _3, Rune *arg, int narg)
1076 getarg(argt, FALSE, FALSE, &r, &na);
1078 xkill(nil, nil, nil, 0, 0, r, na);
1079 /* loop condition: *arg is not a blank */
1081 a = findbl(arg, narg, &na);
1084 cmd = runemalloc(narg-na+1);
1085 runemove(cmd, arg, narg-na);
1087 arg = skipbl(a, na, &narg);
1091 static Rune Lfix[] = { 'f', 'i', 'x', 0 };
1092 static Rune Lvar[] = { 'v', 'a', 'r', 0 };
1095 fontx(Text *et, Text *t, Text *argt, int _0, int _1, Rune *arg, int narg)
1097 Rune *a, *r, *flag, *file;
1107 if(et==nil || et->w==nil)
1112 /* loop condition: *arg is not a blank */
1115 a = findbl(arg, narg, &na);
1118 r = runemalloc(narg-na+1);
1119 runemove(r, arg, narg-na);
1120 if(runeeq(r, narg-na, Lfix, 3) || runeeq(r, narg-na, Lvar, 3)){
1128 arg = skipbl(a, na, &narg);
1130 getarg(argt, FALSE, TRUE, &r, &na);
1132 if(runeeq(r, na, Lfix, 3) || runeeq(r, na, Lvar, 3)){
1142 fix = runeeq(flag, runestrlen(flag), Lfix, 3);
1143 else if(file == nil){
1144 newfont = rfget(FALSE, FALSE, FALSE, nil);
1146 fix = strcmp(newfont->f->name, t->fr.font->name)==0;
1149 aa = runetobyte(file, nf);
1150 newfont = rfget(fix, flag!=nil, FALSE, aa);
1153 newfont = rfget(fix, FALSE, FALSE, nil);
1155 draw(screen, t->w->r, textcols[BACK], nil, ZP);
1156 rfclose(t->reffont);
1157 t->reffont = newfont;
1158 t->fr.font = newfont->f;
1161 t->all.min.x++; /* force recolumnation; disgusting! */
1162 for(i=0; i<t->w->ndl; i++){
1164 aa = runetobyte(dp->r, dp->nr);
1165 dp->wid = stringwidth(newfont->f, aa);
1169 /* avoid shrinking of window due to quantization */
1170 colgrow(t->w->col, t->w, -1);
1177 incl(Text *et, Text *_0, Text *argt, int _1, int _2, Rune *arg, int narg)
1187 if(et==nil || et->w==nil)
1191 getarg(argt, FALSE, TRUE, &r, &len);
1194 winaddincl(w, r, len);
1196 /* loop condition: *arg is not a blank */
1198 a = findbl(arg, narg, &na);
1201 r = runemalloc(narg-na+1);
1202 runemove(r, arg, narg-na);
1204 winaddincl(w, r, narg-na);
1205 arg = skipbl(a, na, &narg);
1207 if(n==0 && w->nincl){
1208 for(n=w->nincl; --n>=0; )
1209 warning(nil, "%S ", w->incl[n]);
1214 static Rune LON[] = { 'O', 'N', 0 };
1215 static Rune LOFF[] = { 'O', 'F', 'F', 0 };
1216 static Rune Lon[] = { 'o', 'n', 0 };
1226 indentval(Rune *s, int n)
1230 if(runestrncmp(s, LON, n) == 0){
1231 globalautoindent = TRUE;
1232 warning(nil, "Indent ON\n");
1235 if(runestrncmp(s, LOFF, n) == 0){
1236 globalautoindent = FALSE;
1237 warning(nil, "Indent OFF\n");
1240 return runestrncmp(s, Lon, n) == 0;
1244 fixindent(Window *w, void *arg)
1247 w->autoindent = globalautoindent;
1251 indent(Text *et, Text *_0, Text *argt, int _1, int _2, Rune *arg, int narg)
1255 int na, len, autoindent;
1262 if(et!=nil && et->w!=nil)
1264 autoindent = IError;
1265 getarg(argt, FALSE, TRUE, &r, &len);
1267 autoindent = indentval(r, len);
1269 a = findbl(arg, narg, &na);
1271 autoindent = indentval(arg, narg-na);
1273 if(autoindent == IGlobal)
1274 allwindows(fixindent, nil);
1275 else if(w != nil && autoindent >= 0)
1276 w->autoindent = autoindent;
1280 tab(Text *et, Text *_0, Text *argt, int _1, int _2, Rune *arg, int narg)
1291 if(et==nil || et->w==nil)
1294 getarg(argt, FALSE, TRUE, &r, &len);
1296 if(r!=nil && len>0){
1297 p = runetobyte(r, len);
1298 if('0'<=p[0] && p[0]<='9')
1302 a = findbl(arg, narg, &na);
1304 p = runetobyte(arg, narg-na);
1305 if('0'<=p[0] && p[0]<='9')
1311 if(w->body.tabstop != tab){
1312 w->body.tabstop = tab;
1313 winresize(w, w->r, FALSE, TRUE);
1316 warning(nil, "%.*S: Tab %d\n", w->body.file->nname, w->body.file->name, w->body.tabstop);
1320 runproc(void *argvp)
1334 char *e, *t, *name, *filename, *dir, **av, *news;
1336 int ac, w, inarg, i, n, fd, nincl, winid;
1342 /*static void *parg[2]; */
1347 threadsetname("runproc");
1353 ndir = (uintptr)argv[3];
1354 newns = (uintptr)argv[4];
1359 iseditcmd = (uintptr)argv[9];
1363 while(*t==' ' || *t=='\n' || *t=='\t')
1366 if(*e==' ' || *e=='\n' || *e=='\t' )
1368 name = emalloc((e-t)+2);
1369 memmove(name, t, e-t);
1371 e = utfrrune(name, '/');
1373 memmove(name, e+1, strlen(e+1)+1); /* strcpy but overlaps */
1374 strcat(name, " "); /* add blank here for ease in waittask */
1375 c->name = bytetorune(name, &c->nname);
1378 if(*t=='<' || *t=='|' || *t=='>')
1380 c->iseditcmd = iseditcmd;
1386 filename = smprint("%.*S", win->body.file->nname, win->body.file->name);
1389 incl = emalloc(nincl*sizeof(Rune*));
1390 for(i=0; i<nincl; i++){
1391 n = runestrlen(win->incl[i]);
1392 incl[i] = runemalloc(n+1);
1393 runemove(incl[i], win->incl[i], n);
1401 winid = activewin->id;
1403 rfork(RFNAMEG|RFENVG|RFFDG|RFNOTEG);
1404 sprint(buf, "%d", winid);
1405 putenv("winid", buf);
1408 putenv("%", filename);
1411 c->md = fsysmount(rdir, ndir, incl, nincl);
1413 fprint(2, "child: can't allocate mntdir: %r\n");
1414 threadexits("fsysmount");
1416 sprint(buf, "%d", c->md->id);
1417 if((fs = nsmount("acme", buf)) == nil){
1418 fprint(2, "child: can't mount acme: %r\n");
1421 threadexits("nsmount");
1423 if(winid>0 && (pipechar=='|' || pipechar=='>')){
1424 sprint(buf, "%d/rdsel", winid);
1425 sfd[0] = fsopenfd(fs, buf, OREAD);
1427 sfd[0] = open("/dev/null", OREAD);
1428 if((winid>0 || iseditcmd) && (pipechar=='|' || pipechar=='<')){
1431 sprint(buf, "%d/editout", winid);
1433 sprint(buf, "editout");
1435 sprint(buf, "%d/wrsel", winid);
1436 sfd[1] = fsopenfd(fs, buf, OWRITE);
1437 sfd[2] = fsopenfd(fs, "cons", OWRITE);
1439 sfd[1] = fsopenfd(fs, "cons", OWRITE);
1444 rfork(RFFDG|RFNOTEG);
1446 sfd[0] = open("/dev/null", OREAD);
1447 sfd[1] = open("/dev/null", OWRITE);
1448 sfd[2] = dup(erroutfd, -1);
1454 putenv("acmeaddr", argaddr);
1455 if(strlen(t) > sizeof buf-10) /* may need to print into stack */
1459 w = chartorune(&r, e);
1460 if(r==' ' || r=='\t')
1464 if(utfrune("#;&|^$=`'{}()<>[]*?^~`", r))
1475 w = chartorune(&r, e);
1476 if(r==' ' || r=='\t'){
1483 av = realloc(av, (ac+1)*sizeof(char**));
1487 av = realloc(av, (ac+2)*sizeof(char**));
1493 * clumsy -- we're not running in a separate thread
1494 * so we have to save the current directory and put
1495 * it back when we're done. if this gets to be a regular
1496 * thing we could change threadexec to take a directory too.
1500 olddir = open(".", OREAD);
1501 dir = runetobyte(rdir, ndir);
1502 chdir(dir); /* ignore error: probably app. window */
1505 ret = threadspawn(sfd, av[0], av);
1515 /* libthread uses execvp so no need to do this */
1518 if(e[0]=='/' || (e[0]=='.' && e[1]=='/'))
1521 sprint(buf, "%s/%s", cputype, av[0]);
1522 procexec(cpid, sfd, buf, av);
1524 sprint(buf, "/bin/%s", av[0]);
1525 procexec(cpid, sfd, buf, av);
1531 * ugly: set path = (. $cputype /bin)
1532 * should honor $path if unusual.
1536 memmove(buf+n, ".", 2);
1538 i = strlen(cputype)+1;
1539 memmove(buf+n, cputype, i);
1541 memmove(buf+n, "/bin", 5);
1543 fd = create("/env/path", OWRITE, 0666);
1549 news = emalloc(strlen(t) + 1 + 1 + strlen(arg) + 1 + 1);
1551 sprint(news, "%s '%s'", t, arg); /* BUG: what if quote in arg? */
1559 olddir = open(".", OREAD);
1560 dir = runetobyte(rdir, ndir);
1561 chdir(dir); /* ignore error: probably app. window */
1568 ret = threadspawn(sfd, rcarg[0], rcarg);
1578 warning(nil, "exec rc: %r\n");
1581 /* threadexec hasn't happened, so send a zero */
1584 if(sfd[2] != sfd[1])
1591 runwaittask(void *v)
1597 threadsetname("runwaittask");
1603 c->pid = recvul(cpid);
1604 while(c->pid == ~0);
1606 if(c->pid != 0) /* successful exec */
1619 run(Window *win, char *s, Rune *rdir, int ndir, int newns, char *argaddr, char *xarg, int iseditcmd)
1628 arg = emalloc(10*sizeof(void*));
1629 c = emalloc(sizeof *c);
1630 cpid = chancreate(sizeof(ulong), 0);
1631 chansetname(cpid, "cpid %s", s);
1635 arg[3] = (void*)(uintptr)ndir;
1636 arg[4] = (void*)(uintptr)newns;
1641 arg[9] = (void*)(uintptr)iseditcmd;
1642 threadcreate(runproc, arg, STACK);
1643 /* mustn't block here because must be ready to answer mount() call in run() */
1644 arg = emalloc(2*sizeof(void*));
1647 threadcreate(runwaittask, arg, STACK);