20 * These functions get called as:
22 * fn(et, t, argt, flag1, flag1, flag2, s, n);
24 * Where the arguments are:
26 * et: the Text* in which the executing event (click) occurred
27 * t: the Text* containing the current selection (Edit, Cut, Snarf, Paste)
28 * argt: the Text* containing the argument for a 2-1 click.
29 * e->flag1: from Exectab entry
30 * e->flag2: from Exectab entry
31 * s: the command line remainder (e.g., "x" if executing "Dump x")
32 * n: length of s (s is *not* NUL-terminated)
35 void doabort(Text*, Text*, Text*, int, int, Rune*, int);
36 void del(Text*, Text*, Text*, int, int, Rune*, int);
37 void delcol(Text*, Text*, Text*, int, int, Rune*, int);
38 void dotfiles(Text*, Text*, Text*, int, int, Rune*, int);
39 void dump(Text*, Text*, Text*, int, int, Rune*, int);
40 void edit(Text*, Text*, Text*, int, int, Rune*, int);
41 void xexit(Text*, Text*, Text*, int, int, Rune*, int);
42 void fontx(Text*, Text*, Text*, int, int, Rune*, int);
43 void get(Text*, Text*, Text*, int, int, Rune*, int);
44 void id(Text*, Text*, Text*, int, int, Rune*, int);
45 void incl(Text*, Text*, Text*, int, int, Rune*, int);
46 void indent(Text*, Text*, Text*, int, int, Rune*, int);
47 void xkill(Text*, Text*, Text*, int, int, Rune*, int);
48 void local(Text*, Text*, Text*, int, int, Rune*, int);
49 void look(Text*, Text*, Text*, int, int, Rune*, int);
50 void newcol(Text*, Text*, Text*, int, int, Rune*, int);
51 void paste(Text*, Text*, Text*, int, int, Rune*, int);
52 void put(Text*, Text*, Text*, int, int, Rune*, int);
53 void putall(Text*, Text*, Text*, int, int, Rune*, int);
54 void sendx(Text*, Text*, Text*, int, int, Rune*, int);
55 void sort(Text*, Text*, Text*, int, int, Rune*, int);
56 void tab(Text*, Text*, Text*, int, int, Rune*, int);
57 void zeroxx(Text*, Text*, Text*, int, int, Rune*, int);
59 typedef struct Exectab Exectab;
63 void (*fn)(Text*, Text*, Text*, int, int, Rune*, int);
69 static Rune LAbort[] = { 'A', 'b', 'o', 'r', 't', 0 };
70 static Rune LCut[] = { 'C', 'u', 't', 0 };
71 static Rune LDel[] = { 'D', 'e', 'l', 0 };
72 static Rune LDelcol[] = { 'D', 'e', 'l', 'c', 'o', 'l', 0 };
73 static Rune LDelete[] = { 'D', 'e', 'l', 'e', 't', 'e', 0 };
74 static Rune LDump[] = { 'D', 'u', 'm', 'p', 0 };
75 static Rune LEdit[] = { 'E', 'd', 'i', 't', 0 };
76 static Rune LExit[] = { 'E', 'x', 'i', 't', 0 };
77 static Rune LFont[] = { 'F', 'o', 'n', 't', 0 };
78 static Rune LGet[] = { 'G', 'e', 't', 0 };
79 static Rune LID[] = { 'I', 'D', 0 };
80 static Rune LIncl[] = { 'I', 'n', 'c', 'l', 0 };
81 static Rune LIndent[] = { 'I', 'n', 'd', 'e', 'n', 't', 0 };
82 static Rune LKill[] = { 'K', 'i', 'l', 'l', 0 };
83 static Rune LLoad[] = { 'L', 'o', 'a', 'd', 0 };
84 static Rune LLocal[] = { 'L', 'o', 'c', 'a', 'l', 0 };
85 static Rune LLook[] = { 'L', 'o', 'o', 'k', 0 };
86 static Rune LNew[] = { 'N', 'e', 'w', 0 };
87 static Rune LNewcol[] = { 'N', 'e', 'w', 'c', 'o', 'l', 0 };
88 static Rune LPaste[] = { 'P', 'a', 's', 't', 'e', 0 };
89 static Rune LPut[] = { 'P', 'u', 't', 0 };
90 static Rune LPutall[] = { 'P', 'u', 't', 'a', 'l', 'l', 0 };
91 static Rune LRedo[] = { 'R', 'e', 'd', 'o', 0 };
92 static Rune LSend[] = { 'S', 'e', 'n', 'd', 0 };
93 static Rune LSnarf[] = { 'S', 'n', 'a', 'r', 'f', 0 };
94 static Rune LSort[] = { 'S', 'o', 'r', 't', 0 };
95 static Rune LTab[] = { 'T', 'a', 'b', 0 };
96 static Rune LUndo[] = { 'U', 'n', 'd', 'o', 0 };
97 static Rune LZerox[] = { 'Z', 'e', 'r', 'o', 'x', 0 };
100 { LAbort, doabort, FALSE, XXX, XXX, },
101 { LCut, cut, TRUE, TRUE, TRUE },
102 { LDel, del, FALSE, FALSE, XXX },
103 { LDelcol, delcol, FALSE, XXX, XXX },
104 { LDelete, del, FALSE, TRUE, XXX },
105 { LDump, dump, FALSE, TRUE, XXX },
106 { LEdit, edit, FALSE, XXX, XXX },
107 { LExit, xexit, FALSE, XXX, XXX },
108 { LFont, fontx, FALSE, XXX, XXX },
109 { LGet, get, FALSE, TRUE, XXX },
110 { LID, id, FALSE, XXX, XXX },
111 { LIncl, incl, FALSE, XXX, XXX },
112 { LIndent, indent, FALSE, XXX, XXX },
113 { LKill, xkill, FALSE, XXX, XXX },
114 { LLoad, dump, FALSE, FALSE, XXX },
115 { LLocal, local, FALSE, XXX, XXX },
116 { LLook, look, FALSE, XXX, XXX },
117 { LNew, new, FALSE, XXX, XXX },
118 { LNewcol, newcol, FALSE, XXX, XXX },
119 { LPaste, paste, TRUE, TRUE, XXX },
120 { LPut, put, FALSE, XXX, XXX },
121 { LPutall, putall, FALSE, XXX, XXX },
122 { LRedo, undo, FALSE, FALSE, XXX },
123 { LSend, sendx, TRUE, XXX, XXX },
124 { LSnarf, cut, FALSE, TRUE, FALSE },
125 { LSort, sort, FALSE, XXX, XXX },
126 { LTab, tab, FALSE, XXX, XXX },
127 { LUndo, undo, FALSE, TRUE, XXX },
128 { LZerox, zeroxx, FALSE, XXX, XXX },
133 lookup(Rune *r, int n)
138 r = skipbl(r, n, &n);
143 for(e=exectab; e->name; e++)
144 if(runeeq(r, nr, e->name, runestrlen(e->name)) == TRUE)
154 return c=='<' || c=='|' || c=='>';
158 execute(Text *t, uint aq0, uint aq1, int external, Text *argt)
169 if(q1 == q0){ /* expand to find word (actually file name) */
170 /* if in selection, choose selection */
171 if(t->q1>t->q0 && t->q0<=q0 && q0<=t->q1){
175 while(q1<t->file->b.nc && isexecc(c=textreadc(t, q1)) && c!=':')
177 while(q0>0 && isexecc(c=textreadc(t, q0-1)) && c!=':')
183 r = runemalloc(q1-q0);
184 bufread(&t->file->b, q0, r, q1-q0);
185 e = lookup(r, q1-q0);
186 if(!external && t->w!=nil && t->w->nopen[QWevent]>0){
190 if(q0!=aq0 || q1!=aq1){
191 bufread(&t->file->b, aq0, r, aq1-aq0);
194 aa = getbytearg(argt, TRUE, TRUE, &a);
196 if(strlen(a) > EVENTSIZE){ /* too big; too bad */
199 warning(nil, "argument string too long\n");
209 winevent(t->w, "%c%d %d %d %d %.*S\n", c, aq0, aq1, f, n, n, r);
211 winevent(t->w, "%c%d %d %d 0 \n", c, aq0, aq1, f, n);
212 if(q0!=aq0 || q1!=aq1){
214 bufread(&t->file->b, q0, r, n);
216 winevent(t->w, "%c%d %d 0 %d %.*S\n", c, q0, q1, n, n, r);
218 winevent(t->w, "%c%d %d 0 0 \n", c, q0, q1, n);
221 winevent(t->w, "%c0 0 0 %d %s\n", c, utflen(a), a);
223 winevent(t->w, "%c0 0 0 %d %s\n", c, utflen(aa), aa);
225 winevent(t->w, "%c0 0 0 0 \n", c);
233 if(e->mark && seltext!=nil)
234 if(seltext->what == Body){
236 filemark(seltext->w->body.file);
238 s = skipbl(r, q1-q0, &n);
239 s = findbl(s, n, &n);
240 s = skipbl(s, n, &n);
241 (*e->fn)(t, seltext, argt, e->flag1, e->flag2, s, n);
246 b = runetobyte(r, q1-q0);
248 dir = dirname(t, nil, 0);
249 if(dir.nr==1 && dir.r[0]=='.'){ /* sigh */
254 aa = getbytearg(argt, TRUE, TRUE, &a);
257 run(t->w, b, dir.r, dir.nr, TRUE, aa, a, FALSE);
261 printarg(Text *argt, uint q0, uint q1)
265 if(argt->what!=Body || argt->file->name==nil)
267 buf = emalloc(argt->file->nname+32);
269 sprint(buf, "%.*S:#%d", argt->file->nname, argt->file->name, q0);
271 sprint(buf, "%.*S:#%d,#%d", argt->file->nname, argt->file->name, q0, q1);
276 getarg(Text *argt, int doaddr, int dofile, Rune **rp, int *nrp)
287 textcommit(argt, TRUE);
288 if(expand(argt, argt->q0, argt->q1, &e)){
290 if(e.nname && dofile){
291 e.name = runerealloc(e.name, e.nname+1);
293 a = printarg(argt, e.q0, e.q1);
304 *rp = runemalloc(n+1);
305 bufread(&argt->file->b, e.q0, *rp, n);
307 a = printarg(argt, e.q0, e.q1);
313 getbytearg(Text *argt, int doaddr, int dofile, char **bp)
320 aa = getarg(argt, doaddr, dofile, &r, &n);
323 *bp = runetobyte(r, n);
329 doabort(Text *__0, Text *_0, Text *_1, int _2, int _3, Rune *_4, int _5)
342 warning(nil, "executing Abort again will call abort()\n");
348 newcol(Text *et, Text *_0, Text *_1, int _2, int _3, Rune *_4, int _5)
360 c = rowadd(et->row, nil, -1);
362 w = coladd(c, nil, nil, -1);
369 delcol(Text *et, Text *_0, Text *_1, int _2, int _3, Rune *_4, int _5)
383 if(c==nil || colclean(c)==0)
385 for(i=0; i<c->nw; i++){
387 if(w->nopen[QWevent]+w->nopen[QWaddr]+w->nopen[QWdata]+w->nopen[QWxdata] > 0){
388 warning(nil, "can't delete column; %.*S is running an external command\n", w->body.file->nname, w->body.file->name);
392 rowclose(et->col->row, et->col, TRUE);
396 del(Text *et, Text *_0, Text *_1, int flag1, int _2, Rune *_3, int _4)
404 if(et->col==nil || et->w == nil)
406 if(flag1 || et->w->body.file->ntext>1 || winclean(et->w, FALSE))
407 colclose(et->col, et->w, TRUE);
411 sort(Text *et, Text *_0, Text *_1, int _2, int _3, Rune *_4, int _5)
425 seqof(Window *w, int isundo)
427 /* if it's undo, see who changed with us */
429 return w->body.file->seq;
430 /* if it's redo, see who we'll be sync'ed up with */
431 return fileredoseq(w->body.file);
435 undo(Text *et, Text *_0, Text *_1, int flag1, int _2, Rune *_3, int _4)
448 if(et==nil || et->w== nil)
450 seq = seqof(et->w, flag1);
452 /* nothing to undo */
456 * Undo the executing window first. Its display will update. other windows
457 * in the same file will not call show() and jump to a different location in the file.
458 * Simultaneous changes to other files will be chaotic, however.
460 winundo(et->w, flag1);
461 for(i=0; i<row.ncol; i++){
463 for(j=0; j<c->nw; j++){
467 if(seqof(w, flag1) == seq)
474 getname(Text *t, Text *argt, Rune *arg, int narg, int isput)
481 getarg(argt, FALSE, TRUE, &r, &n);
486 /* if are doing a Put, want to synthesize name even for non-existent file */
487 /* best guess is that file name doesn't contain a slash */
503 s = runetobyte(t->file->name, t->file->nname);
506 /* prefix with directory name if necessary */
509 if(n>0 && arg[0]!='/'){
510 dir = dirname(t, nil, 0);
511 if(dir.nr==1 && dir.r[0]=='.'){ /* sigh */
518 r = runemalloc(dir.nr+n+1);
519 runemove(r, dir.r, dir.nr);
521 if(dir.nr>0 && r[dir.nr]!='/' && n>0 && arg[0]!='/')
523 runemove(r+dir.nr, arg, n);
530 s = runetobyte(r, n);
540 zeroxx(Text *et, Text *t, Text *_1, int _2, int _3, Rune *_4, int _5)
552 if(t!=nil && t->w!=nil && t->w!=et->w){
561 if(t==nil || t->w==nil)
565 warning(nil, "%.*S is a directory; Zerox illegal\n", t->file->nname, t->file->name);
567 nw = coladd(t->w->col, nil, t->w, -1);
568 /* ugly: fix locks so w->unlock works */
569 winlock1(nw, t->w->owner);
570 xfidlog(nw, "zerox");
576 typedef struct TextAddr TextAddr;
578 long lorigin; // line+rune for origin
580 long lq0; // line+rune for q0
582 long lq1; // line+rune for q1
587 get(Text *et, Text *t, Text *argt, int flag1, int _0, Rune *arg, int narg)
591 int i, n, dirty, samename, isdir;
601 if(et==nil || et->w==nil)
603 if(!et->w->isdir && (et->w->body.file->b.nc>0 && !winclean(et->w, TRUE)))
607 name = getname(t, argt, arg, narg, FALSE);
609 warning(nil, "no file name\n");
612 if(t->file->ntext>1){
614 isdir = (d!=nil && (d->qid.type & QTDIR));
617 warning(nil, "%s is a directory; can't read with multiple windows on it\n", name);
621 addr = emalloc((t->file->ntext)*sizeof(TextAddr));
622 for(i=0; i<t->file->ntext; i++) {
624 u = t->file->text[i];
625 a->lorigin = nlcount(u, 0, u->org, &a->rorigin);
626 a->lq0 = nlcount(u, 0, u->q0, &a->rq0);
627 a->lq1 = nlcount(u, u->q0, u->q1, &a->rq1);
629 r = bytetorune(name, &n);
630 for(i=0; i<t->file->ntext; i++){
631 u = t->file->text[i];
632 /* second and subsequent calls with zero an already empty buffer, but OK */
636 samename = runeeq(r, n, t->file->name, t->file->nname);
637 textload(t, 0, name, samename);
639 t->file->mod = FALSE;
645 for(i=0; i<t->file->ntext; i++)
646 t->file->text[i]->w->dirty = dirty;
650 t->file->unread = FALSE;
651 for(i=0; i<t->file->ntext; i++){
652 u = t->file->text[i];
653 textsetselect(&u->w->tag, u->w->tag.file->b.nc, u->w->tag.file->b.nc);
656 // warning(nil, "%d %d %d %d %d %d\n", a->lorigin, a->rorigin, a->lq0, a->rq0, a->lq1, a->rq1);
657 q0 = nlcounttopos(u, 0, a->lq0, a->rq0);
658 q1 = nlcounttopos(u, q0, a->lq1, a->rq1);
659 textsetselect(u, q0, q1);
660 q0 = nlcounttopos(u, 0, a->lorigin, a->rorigin);
661 textsetorigin(u, q0, FALSE);
670 checksha1(char *name, File *f, Dir *d)
677 fd = open(name, OREAD);
680 h = sha1(nil, 0, nil, nil);
682 while((n = read(fd, buf, 8192)) > 0)
683 sha1(buf, n, nil, h);
686 sha1(nil, 0, out, h);
687 if(memcmp(out, f->sha1, sizeof out) == 0) {
689 f->qidpath = d->qid.path;
695 putfile(File *f, int q0, int q1, Rune *namer, int nname)
708 name = runetobyte(namer, nname);
710 if(d!=nil && runeeq(namer, nname, f->name, f->nname)){
711 if(f->dev!=d->dev || f->qidpath!=d->qid.path || f->mtime != d->mtime)
712 checksha1(name, f, d);
713 if(f->dev!=d->dev || f->qidpath!=d->qid.path || f->mtime != d->mtime) {
715 warning(nil, "%s not written; file already exists\n", name);
717 warning(nil, "%s modified%s%s since last read\n\twas %t; now %t\n", name, d->muid[0]?" by ":"", d->muid, f->mtime, d->mtime);
719 f->qidpath = d->qid.path;
724 fd = create(name, OWRITE, 0666);
726 warning(nil, "can't create file %s: %r\n", name);
729 // Use bio in order to force the writes to be large and
730 // block-aligned (bio's default is 8K). This is not strictly
731 // necessary; it works around some buggy underlying
732 // file systems that mishandle unaligned writes.
733 // https://codereview.appspot.com/89550043/
734 b = emalloc(sizeof *b);
735 Binit(b, fd, OWRITE);
740 h = sha1(nil, 0, nil, nil);
741 isapp = (d!=nil && d->length>0 && (d->qid.type&QTAPPEND));
743 warning(nil, "%s not written; file is append only\n", name);
747 for(q=q0; q<q1; q+=n){
749 if(n > BUFSIZE/UTFmax)
751 bufread(&f->b, q, r, n);
752 m = snprint(s, BUFSIZE+1, "%.*S", n, r);
753 sha1((uchar*)s, m, nil, h);
754 if(Bwrite(b, s, m) != m){
755 warning(nil, "can't write file %s: %r\n", name);
760 warning(nil, "can't write file %s: %r\n", name);
766 if(runeeq(namer, nname, f->name, f->nname)){
767 if(q0!=0 || q1!=f->b.nc){
772 // In case the file is on NFS, reopen the fd
773 // before dirfstat to cause the attribute cache
774 // to be updated (otherwise the mtime in the
775 // dirfstat below will be stale and not match
776 // what NFS sees). The file is already written,
777 // so this should be a no-op when not on NFS.
778 // Opening for OWRITE (but no truncation)
779 // in case we don't have read permission.
780 // (The create above worked, so we probably
781 // still have write permission.)
783 fd = open(name, OWRITE);
790 f->qidpath = d->qid.path;
793 sha1(nil, 0, f->sha1, h);
799 for(i=0; i<f->ntext; i++){
800 f->text[i]->w->putseq = f->seq;
801 f->text[i]->w->dirty = w->dirty;
832 put(Text *et, Text *_0, Text *argt, int _1, int _2, Rune *arg, int narg)
844 if(et==nil || et->w==nil || et->w->isdir)
848 name = getname(&w->body, argt, arg, narg, TRUE);
850 warning(nil, "no file name\n");
853 namer = bytetorune(name, &nname);
854 putfile(f, 0, f->b.nc, namer, nname);
860 dump(Text *_0, Text *_1, Text *argt, int isdump, int _2, Rune *arg, int narg)
869 name = runetobyte(arg, narg);
871 getbytearg(argt, FALSE, TRUE, &name);
875 rowload(&row, name, FALSE);
880 cut(Text *et, Text *t, Text *_0, int dosnarf, int docut, Rune *_2, int _3)
882 uint q0, q1, n, locked, c;
890 * if not executing a mouse chord (et != t) and snarfing (dosnarf)
891 * and executed Cut or Snarf in window tag (et->w != nil),
892 * then use the window body selection or the tag selection
893 * or do nothing at all.
895 if(et!=t && dosnarf && et->w!=nil){
896 if(et->w->body.q1>et->w->body.q0){
899 filemark(t->file); /* seq has been incremented by execute */
900 }else if(et->w->tag.q1>et->w->tag.q0)
905 if(t == nil) /* no selection */
909 if(t->w!=nil && et->w!=t->w){
924 bufdelete(&snarfbuf, 0, snarfbuf.nc);
930 bufread(&t->file->b, q0, r, n);
931 bufinsert(&snarfbuf, snarfbuf.nc, r, n);
938 textdelete(t, t->q0, t->q1, TRUE);
939 textsetselect(t, t->q0, t->q0);
944 }else if(dosnarf) /* Snarf command */
951 paste(Text *et, Text *t, Text *_0, int selectall, int tobody, Rune *_1, int _2)
961 /* if(tobody), use body of executing window (Paste or Send command) */
962 if(tobody && et!=nil && et->w!=nil){
964 filemark(t->file); /* seq has been incremented by execute */
970 if(t==nil || snarfbuf.nc==0)
972 if(t->w!=nil && et->w!=t->w){
978 cut(t, t, nil, FALSE, TRUE, nil, 0);
981 q1 = t->q0+snarfbuf.nc;
989 bufread(&snarfbuf, q, r, n);
990 textinsert(t, q0, r, n, TRUE);
996 textsetselect(t, t->q0, q1);
998 textsetselect(t, q1, q1);
1003 if(t->w!=nil && et->w!=t->w)
1008 look(Text *et, Text *t, Text *argt, int _0, int _1, Rune *arg, int narg)
1019 search(t, arg, narg);
1022 getarg(argt, FALSE, FALSE, &r, &n);
1026 bufread(&t->file->b, t->q0, r, n);
1033 static Rune Lnl[] = { '\n', 0 };
1036 sendx(Text *et, Text *t, Text *_0, int _1, int _2, Rune *_3, int _4)
1048 cut(t, t, nil, TRUE, FALSE, nil, 0);
1049 textsetselect(t, t->file->b.nc, t->file->b.nc);
1050 paste(t, t, nil, TRUE, TRUE, nil, 0);
1051 if(textreadc(t, t->file->b.nc-1) != '\n'){
1052 textinsert(t, t->file->b.nc, Lnl, 1, TRUE);
1053 textsetselect(t, t->file->b.nc, t->file->b.nc);
1056 textshow(t, t->q1, t->q1, 1);
1060 edit(Text *et, Text *_0, Text *argt, int _1, int _2, Rune *arg, int narg)
1071 getarg(argt, FALSE, TRUE, &r, &len);
1074 editcmd(et, r, len);
1077 editcmd(et, arg, narg);
1081 xexit(Text *et, Text *_0, Text *_1, int _2, int _3, Rune *_4, int _5)
1098 putall(Text *et, Text *_0, Text *_1, int _2, int _3, Rune *_4, int _5)
1113 for(i=0; i<row.ncol; i++){
1115 for(j=0; j<c->nw; j++){
1117 if(w->isscratch || w->isdir || w->body.file->nname==0)
1119 if(w->nopen[QWevent] > 0)
1121 a = runetobyte(w->body.file->name, w->body.file->nname);
1123 if(w->body.file->mod || w->body.ncache)
1125 warning(nil, "no auto-Put of %s: %r\n", a);
1127 wincommit(w, &w->body);
1128 put(&w->body, nil, nil, XXX, XXX, nil, 0);
1137 id(Text *et, Text *_0, Text *_1, int _2, int _3, Rune *_4, int _5)
1147 warning(nil, "/mnt/acme/%d/\n", et->w->id);
1151 local(Text *et, Text *_0, Text *argt, int _1, int _2, Rune *arg, int narg)
1160 aa = getbytearg(argt, TRUE, TRUE, &a);
1162 dir = dirname(et, nil, 0);
1163 if(dir.nr==1 && dir.r[0]=='.'){ /* sigh */
1168 run(nil, runetobyte(arg, narg), dir.r, dir.nr, FALSE, aa, a, FALSE);
1172 xkill(Text *_0, Text *_1, Text *argt, int _2, int _3, Rune *arg, int narg)
1182 getarg(argt, FALSE, FALSE, &r, &na);
1184 xkill(nil, nil, nil, 0, 0, r, na);
1185 /* loop condition: *arg is not a blank */
1187 a = findbl(arg, narg, &na);
1190 cmd = runemalloc(narg-na+1);
1191 runemove(cmd, arg, narg-na);
1193 arg = skipbl(a, na, &narg);
1197 static Rune Lfix[] = { 'f', 'i', 'x', 0 };
1198 static Rune Lvar[] = { 'v', 'a', 'r', 0 };
1201 fontx(Text *et, Text *t, Text *argt, int _0, int _1, Rune *arg, int narg)
1203 Rune *a, *r, *flag, *file;
1213 if(et==nil || et->w==nil)
1218 /* loop condition: *arg is not a blank */
1221 a = findbl(arg, narg, &na);
1224 r = runemalloc(narg-na+1);
1225 runemove(r, arg, narg-na);
1226 if(runeeq(r, narg-na, Lfix, 3) || runeeq(r, narg-na, Lvar, 3)){
1234 arg = skipbl(a, na, &narg);
1236 getarg(argt, FALSE, TRUE, &r, &na);
1238 if(runeeq(r, na, Lfix, 3) || runeeq(r, na, Lvar, 3)){
1248 fix = runeeq(flag, runestrlen(flag), Lfix, 3);
1249 else if(file == nil){
1250 newfont = rfget(FALSE, FALSE, FALSE, nil);
1252 fix = strcmp(newfont->f->name, t->fr.font->name)==0;
1255 aa = runetobyte(file, nf);
1256 newfont = rfget(fix, flag!=nil, FALSE, aa);
1259 newfont = rfget(fix, FALSE, FALSE, nil);
1261 draw(screen, t->w->r, textcols[BACK], nil, ZP);
1262 rfclose(t->reffont);
1263 t->reffont = newfont;
1264 t->fr.font = newfont->f;
1267 t->all.min.x++; /* force recolumnation; disgusting! */
1268 for(i=0; i<t->w->ndl; i++){
1270 aa = runetobyte(dp->r, dp->nr);
1271 dp->wid = stringwidth(newfont->f, aa);
1275 /* avoid shrinking of window due to quantization */
1276 colgrow(t->w->col, t->w, -1);
1283 incl(Text *et, Text *_0, Text *argt, int _1, int _2, Rune *arg, int narg)
1293 if(et==nil || et->w==nil)
1297 getarg(argt, FALSE, TRUE, &r, &len);
1300 winaddincl(w, r, len);
1302 /* loop condition: *arg is not a blank */
1304 a = findbl(arg, narg, &na);
1307 r = runemalloc(narg-na+1);
1308 runemove(r, arg, narg-na);
1310 winaddincl(w, r, narg-na);
1311 arg = skipbl(a, na, &narg);
1313 if(n==0 && w->nincl){
1314 for(n=w->nincl; --n>=0; )
1315 warning(nil, "%S ", w->incl[n]);
1320 static Rune LON[] = { 'O', 'N', 0 };
1321 static Rune LOFF[] = { 'O', 'F', 'F', 0 };
1322 static Rune Lon[] = { 'o', 'n', 0 };
1332 indentval(Rune *s, int n)
1336 if(runestrncmp(s, LON, n) == 0){
1337 globalautoindent = TRUE;
1338 warning(nil, "Indent ON\n");
1341 if(runestrncmp(s, LOFF, n) == 0){
1342 globalautoindent = FALSE;
1343 warning(nil, "Indent OFF\n");
1346 return runestrncmp(s, Lon, n) == 0;
1350 fixindent(Window *w, void *arg)
1353 w->autoindent = globalautoindent;
1357 indent(Text *et, Text *_0, Text *argt, int _1, int _2, Rune *arg, int narg)
1361 int na, len, autoindent;
1368 if(et!=nil && et->w!=nil)
1370 autoindent = IError;
1371 getarg(argt, FALSE, TRUE, &r, &len);
1373 autoindent = indentval(r, len);
1375 a = findbl(arg, narg, &na);
1377 autoindent = indentval(arg, narg-na);
1379 if(autoindent == IGlobal)
1380 allwindows(fixindent, nil);
1381 else if(w != nil && autoindent >= 0)
1382 w->autoindent = autoindent;
1386 tab(Text *et, Text *_0, Text *argt, int _1, int _2, Rune *arg, int narg)
1397 if(et==nil || et->w==nil)
1400 getarg(argt, FALSE, TRUE, &r, &len);
1402 if(r!=nil && len>0){
1403 p = runetobyte(r, len);
1404 if('0'<=p[0] && p[0]<='9')
1408 a = findbl(arg, narg, &na);
1410 p = runetobyte(arg, narg-na);
1411 if('0'<=p[0] && p[0]<='9')
1417 if(w->body.tabstop != tab){
1418 w->body.tabstop = tab;
1419 winresize(w, w->r, FALSE, TRUE);
1422 warning(nil, "%.*S: Tab %d\n", w->body.file->nname, w->body.file->name, w->body.tabstop);
1426 runproc(void *argvp)
1440 char *e, *t, *name, *filename, *dir, **av, *news;
1442 int ac, w, inarg, i, n, fd, nincl, winid;
1447 /*static void *parg[2]; */
1453 threadsetname("runproc");
1459 ndir = (uintptr)argv[3];
1460 newns = (uintptr)argv[4];
1465 iseditcmd = (uintptr)argv[9];
1469 while(*t==' ' || *t=='\n' || *t=='\t')
1472 if(*e==' ' || *e=='\n' || *e=='\t' )
1474 name = emalloc((e-t)+2);
1475 memmove(name, t, e-t);
1477 e = utfrrune(name, '/');
1479 memmove(name, e+1, strlen(e+1)+1); /* strcpy but overlaps */
1480 strcat(name, " "); /* add blank here for ease in waittask */
1481 c->name = bytetorune(name, &c->nname);
1484 if(*t=='<' || *t=='|' || *t=='>')
1486 c->iseditcmd = iseditcmd;
1492 filename = smprint("%.*S", win->body.file->nname, win->body.file->name);
1495 incl = emalloc(nincl*sizeof(Rune*));
1496 for(i=0; i<nincl; i++){
1497 n = runestrlen(win->incl[i]);
1498 incl[i] = runemalloc(n+1);
1499 runemove(incl[i], win->incl[i], n);
1507 winid = activewin->id;
1509 rfork(RFNAMEG|RFENVG|RFFDG|RFNOTEG);
1510 sprint(buf, "%d", winid);
1511 putenv("winid", buf);
1514 putenv("%", filename);
1515 putenv("samfile", filename);
1518 c->md = fsysmount(rdir, ndir, incl, nincl);
1520 fprint(2, "child: can't allocate mntdir: %r\n");
1521 threadexits("fsysmount");
1523 sprint(buf, "%d", c->md->id);
1524 if((fs = nsmount("acme", buf)) == nil){
1525 fprint(2, "child: can't mount acme: %r\n");
1528 threadexits("nsmount");
1530 if(winid>0 && (pipechar=='|' || pipechar=='>')){
1531 sprint(buf, "%d/rdsel", winid);
1532 sfd[0] = fsopenfd(fs, buf, OREAD);
1534 sfd[0] = open("/dev/null", OREAD);
1535 if((winid>0 || iseditcmd) && (pipechar=='|' || pipechar=='<')){
1538 sprint(buf, "%d/editout", winid);
1540 sprint(buf, "editout");
1542 sprint(buf, "%d/wrsel", winid);
1543 sfd[1] = fsopenfd(fs, buf, OWRITE);
1544 sfd[2] = fsopenfd(fs, "cons", OWRITE);
1546 sfd[1] = fsopenfd(fs, "cons", OWRITE);
1551 rfork(RFFDG|RFNOTEG);
1553 sfd[0] = open("/dev/null", OREAD);
1554 sfd[1] = open("/dev/null", OWRITE);
1555 sfd[2] = dup(erroutfd, -1);
1561 putenv("acmeaddr", argaddr);
1562 if(acmeshell != nil)
1564 if(strlen(t) > sizeof buf-10) /* may need to print into stack */
1568 w = chartorune(&r, e);
1569 if(r==' ' || r=='\t')
1573 if(utfrune("#;&|^$=`'{}()<>[]*?^~`/", r))
1584 w = chartorune(&r, e);
1585 if(r==' ' || r=='\t'){
1592 av = realloc(av, (ac+1)*sizeof(char**));
1596 av = realloc(av, (ac+2)*sizeof(char**));
1603 dir = runetobyte(rdir, ndir);
1604 ret = threadspawnd(sfd, av[0], av, dir);
1611 /* libthread uses execvp so no need to do this */
1614 if(e[0]=='/' || (e[0]=='.' && e[1]=='/'))
1617 sprint(buf, "%s/%s", cputype, av[0]);
1618 procexec(cpid, sfd, buf, av);
1620 sprint(buf, "/bin/%s", av[0]);
1621 procexec(cpid, sfd, buf, av);
1627 * ugly: set path = (. $cputype /bin)
1628 * should honor $path if unusual.
1632 memmove(buf+n, ".", 2);
1634 i = strlen(cputype)+1;
1635 memmove(buf+n, cputype, i);
1637 memmove(buf+n, "/bin", 5);
1639 fd = create("/env/path", OWRITE, 0666);
1645 news = emalloc(strlen(t) + 1 + 1 + strlen(arg) + 1 + 1);
1647 sprint(news, "%s '%s'", t, arg); /* BUG: what if quote in arg? */
1655 dir = runetobyte(rdir, ndir);
1663 ret = threadspawnd(sfd, rcarg[0], rcarg, dir);
1670 warning(nil, "exec %s: %r\n", shell);
1673 /* threadexec hasn't happened, so send a zero */
1676 if(sfd[2] != sfd[1])
1683 runwaittask(void *v)
1689 threadsetname("runwaittask");
1695 c->pid = recvul(cpid);
1696 while(c->pid == ~0);
1698 if(c->pid != 0) /* successful exec */
1711 run(Window *win, char *s, Rune *rdir, int ndir, int newns, char *argaddr, char *xarg, int iseditcmd)
1720 arg = emalloc(10*sizeof(void*));
1721 c = emalloc(sizeof *c);
1722 cpid = chancreate(sizeof(ulong), 0);
1723 chansetname(cpid, "cpid %s", s);
1727 arg[3] = (void*)(uintptr)ndir;
1728 arg[4] = (void*)(uintptr)newns;
1733 arg[9] = (void*)(uintptr)iseditcmd;
1734 threadcreate(runproc, arg, STACK);
1735 /* mustn't block here because must be ready to answer mount() call in run() */
1736 arg = emalloc(2*sizeof(void*));
1739 threadcreate(runwaittask, arg, STACK);