17 char Enoname[] = "no file name given";
26 int append(File*, Cmd*, long);
28 void pfilename(File*);
29 void looper(File*, Cmd*, int);
30 void filelooper(Cmd*, int);
31 void linelooper(File*, Cmd*);
32 Address lineaddr(long, Address, int);
33 int filematch(File*, String*);
34 File *tofile(String*);
35 Rune* cmdname(File *f, String *s, int);
36 void runpipe(Text*, int, Rune*, int, int);
54 mkaddr(Address *a, File *f)
56 a->r.q0 = f->curtext->q0;
57 a->r.q1 = f->curtext->q1;
62 cmdexec(Text *t, Cmd *cp)
74 if(w==nil && (cp->addr==0 || cp->addr->type!='"') &&
75 !utfrune("bBnqUXY!", cp->cmdc) &&
76 !(cp->cmdc=='D' && cp->u.text))
77 editerror("no current window");
78 i = cmdlookup(cp->cmdc); /* will be -1 for '{' */
85 if(i>=0 && cmdtab[i].defaddr != aNo){
86 if((ap=cp->addr)==0 && cp->cmdc!='\n'){
87 cp->addr = ap = newaddr();
89 if(cmdtab[i].defaddr == aAll)
91 }else if(ap && ap->type=='"' && ap->next==0 && cp->cmdc!='\n'){
94 if(cmdtab[i].defaddr == aAll)
97 if(cp->addr){ /* may be false for '\n' (only) */
98 static Address none = {0,0,nil};
101 addr = cmdaddress(ap, dot, 0);
103 addr = cmdaddress(ap, none, 0);
112 dot = cmdaddress(cp->addr, dot, 0);
113 for(cp = cp->u.cmd; cp; cp = cp->next){
114 if(dot.r.q1 > t->file->b.nc)
115 editerror("dot extends past end of buffer during { command");
123 editerror("unknown command %c in cmdexec", cp->cmdc);
124 i = (*cmdtab[i].fn)(t, cp);
131 edittext(Window *w, int q, Rune *r, int nr)
138 return "permission denied";
140 eloginsert(f, q, r, nr);
143 collection = runerealloc(collection, ncollection+nr+1);
144 runemove(collection+ncollection, r, nr);
146 collection[ncollection] = '\0';
149 return "unknown state in edittext";
153 /* string is known to be NUL-terminated */
155 filelist(Text *t, Rune *r, int nr)
159 r = skipbl(r, nr, &nr);
161 return runestrdup(r);
162 /* use < command to collect text */
164 runpipe(t, '<', r+1, nr-1, Collecting);
169 a_cmd(Text *t, Cmd *cp)
171 return append(t->file, cp, addr.r.q1);
175 b_cmd(Text *t, Cmd *cp)
180 f = tofile(cp->u.text);
183 curtext = f->curtext;
188 B_cmd(Text *t, Cmd *cp)
193 list = filelist(t, cp->u.text->r, cp->u.text->n);
198 r = skipbl(r, nr, &nr);
200 new(t, t, nil, 0, 0, r, 0);
202 s = findbl(r, nr, &nr);
204 new(t, t, nil, 0, 0, r, runestrlen(r));
206 r = skipbl(s+1, nr-1, &nr);
213 c_cmd(Text *t, Cmd *cp)
215 elogreplace(t->file, addr.r.q0, addr.r.q1, cp->u.text->r, cp->u.text->n);
222 d_cmd(Text *t, Cmd *cp)
225 if(addr.r.q1 > addr.r.q0)
226 elogdelete(t->file, addr.r.q0, addr.r.q1);
235 if(t->w->body.file->ntext>1 || winclean(t->w, FALSE))
236 colclose(t->col, t->w, TRUE);
240 D_cmd(Text *t, Cmd *cp)
242 Rune *list, *r, *s, *n;
248 list = filelist(t, cp->u.text->r, cp->u.text->n);
253 dir = dirname(t, nil, 0);
256 r = skipbl(r, nr, &nr);
258 s = findbl(r, nr, &nr);
260 /* first time through, could be empty string, meaning delete file empty name */
262 if(r[0]=='/' || nn==0 || dir.nr==0){
263 rs.r = runestrdup(r);
266 n = runemalloc(dir.nr+1+nn);
267 runemove(n, dir.r, dir.nr);
269 runemove(n+dir.nr+1, r, nn);
270 rs = cleanrname(runestr(n, dir.nr+1+nn));
272 w = lookfile(rs.r, rs.nr);
274 snprint(buf, sizeof buf, "no such file %.*S", rs.nr, rs.r);
281 r = skipbl(s+1, nr-1, &nr);
289 readloader(void *v, uint q0, Rune *r, int nr)
292 eloginsert(v, q0, r, nr);
297 e_cmd(Text *t, Cmd *cp)
301 int i, isdir, q0, q1, fd, nulls, samename, allreplaced;
309 if(winclean(t->w, TRUE)==FALSE)
310 editerror(""); /* winclean generated message already */
314 allreplaced = (q0==0 && q1==f->b.nc);
315 name = cmdname(f, cp->u.text, cp->cmdc=='e');
318 i = runestrlen(name);
319 samename = runeeq(name, i, t->file->name, t->file->nname);
320 s = runetobyte(name, i);
324 snprint(tmp, sizeof tmp, "can't open %s: %r", s);
329 isdir = (d!=nil && (d->qid.type&QTDIR));
333 snprint(tmp, sizeof tmp, "%s is a directory", s);
337 elogdelete(f, q0, q1);
339 loadfile(fd, q1, &nulls, readloader, f);
343 warning(nil, "%s: NUL bytes elided\n", s);
344 else if(allreplaced && samename)
349 static Rune Lempty[] = { 0 };
351 f_cmd(Text *t, Cmd *cp)
357 if(cp->u.text == nil){
363 name = cmdname(t->file, str, TRUE);
370 g_cmd(Text *t, Cmd *cp)
372 if(t->file != addr.f){
373 warning(nil, "internal error: g_cmd f!=addr.f\n");
376 if(rxcompile(cp->re->r) == FALSE)
377 editerror("bad regexp in g command");
378 if(rxexecute(t, nil, addr.r.q0, addr.r.q1, &sel) ^ cp->cmdc=='v'){
381 return cmdexec(t, cp->u.cmd);
387 i_cmd(Text *t, Cmd *cp)
389 return append(t->file, cp, addr.r.q0);
393 copy(File *f, Address addr2)
400 for(p=addr.r.q0; p<addr.r.q1; p+=ni){
404 bufread(&f->b, p, buf, ni);
405 eloginsert(addr2.f, addr2.r.q1, buf, ni);
411 move(File *f, Address addr2)
413 if(addr.f!=addr2.f || addr.r.q1<=addr2.r.q0){
414 elogdelete(f, addr.r.q0, addr.r.q1);
416 }else if(addr.r.q0 >= addr2.r.q1){
418 elogdelete(f, addr.r.q0, addr.r.q1);
420 error("move overlaps itself");
424 m_cmd(Text *t, Cmd *cp)
428 mkaddr(&dot, t->file);
429 addr2 = cmdaddress(cp->u.mtaddr, dot, 0);
431 move(t->file, addr2);
433 copy(t->file, addr2);
438 p_cmd(Text *t, Cmd *cp)
441 return pdisplay(t->file);
445 s_cmd(Text *t, Cmd *cp)
447 int i, j, k, c, m, n, nrp, didsub;
456 if(rxcompile(cp->re->r) == FALSE)
457 editerror("bad regexp in s command");
462 for(p1 = addr.r.q0; p1<=addr.r.q1 && rxexecute(t, nil, p1, addr.r.q1, &sel); ){
463 if(sel.r[0].q0 == sel.r[0].q1){ /* empty match? */
464 if(sel.r[0].q0 == op){
475 rp = erealloc(rp, nrp*sizeof(Rangeset));
479 buf = allocstring(0);
480 for(m=0; m<nrp; m++){
484 for(i = 0; i<cp->u.text->n; i++)
485 if((c = cp->u.text->r[i])=='\\' && i<cp->u.text->n-1){
486 c = cp->u.text->r[++i];
487 if('1'<=c && c<='9') {
489 if(sel.r[j].q1-sel.r[j].q0>RBUFSIZE){
490 err = "replacement string too long";
493 bufread(&t->file->b, sel.r[j].q0, rbuf, sel.r[j].q1-sel.r[j].q0);
494 for(k=0; k<sel.r[j].q1-sel.r[j].q0; k++)
495 Straddc(buf, rbuf[k]);
501 if(sel.r[0].q1-sel.r[0].q0>RBUFSIZE){
502 err = "right hand side too long in substitution";
505 bufread(&t->file->b, sel.r[0].q0, rbuf, sel.r[0].q1-sel.r[0].q0);
506 for(k=0; k<sel.r[0].q1-sel.r[0].q0; k++)
507 Straddc(buf, rbuf[k]);
509 elogreplace(t->file, sel.r[0].q0, sel.r[0].q1, buf->r, buf->n);
510 delta -= sel.r[0].q1-sel.r[0].q0;
519 if(!didsub && nest==0)
520 editerror("no substitution");
534 u_cmd(Text *t, Cmd *cp)
545 while(n-->0 && t->file->seq!=0 && t->file->seq!=oseq){
547 undo(t, nil, nil, flag, 0, nil, 0);
553 w_cmd(Text *t, Cmd *cp)
560 editerror("can't write file with pending modifications");
561 r = cmdname(f, cp->u.text, FALSE);
563 editerror("no name specified for 'w' command");
564 putfile(f, addr.r.q0, addr.r.q1, r, runestrlen(r));
565 /* r is freed by putfile */
570 x_cmd(Text *t, Cmd *cp)
573 looper(t->file, cp, cp->cmdc=='x');
575 linelooper(t->file, cp);
580 X_cmd(Text *t, Cmd *cp)
584 filelooper(cp, cp->cmdc=='X');
589 runpipe(Text *t, int cmd, Rune *cr, int ncr, int state)
597 r = skipbl(cr, ncr, &n);
599 editerror("no command specified for %c", cmd);
601 if(state == Inserting){
605 if(cmd == '<' || cmd=='|')
606 elogdelete(t->file, t->q0, t->q1);
615 dir = dirname(t, nil, 0);
616 if(dir.nr==1 && dir.r[0]=='.'){ /* sigh */
622 if(t!=nil && t->w!=nil)
623 incref(&t->w->ref); /* run will decref */
624 run(w, runetobyte(s, n), dir.r, dir.nr, TRUE, nil, nil, TRUE);
626 if(t!=nil && t->w!=nil)
631 * The editoutlk exists only so that we can tell when
632 * the editout file has been closed. It can get closed *after*
633 * the process exits because, since the process cannot be
634 * connected directly to editout (no 9P kernel support),
635 * the process is actually connected to a pipe to another
636 * process (arranged via 9pserve) that reads from the pipe
637 * and then writes the data in the pipe to editout using
638 * 9P transactions. This process might still have a couple
639 * writes left to copy after the original process has exited.
645 qlock(q); /* wait for file to close */
649 if(t!=nil && t->w!=nil)
654 pipe_cmd(Text *t, Cmd *cp)
656 runpipe(t, cp->cmdc, cp->u.text->r, cp->u.text->n, Inserting);
661 nlcount(Text *t, long q0, long q1)
675 bufread(&t->file->b, q0, buf, nbuf);
687 printposn(Text *t, int charsonly)
691 if (t != nil && t->file != nil && t->file->name != nil)
692 warning(nil, "%.*S:", t->file->nname, t->file->name);
694 l1 = 1+nlcount(t, 0, addr.r.q0);
695 l2 = l1+nlcount(t, addr.r.q0, addr.r.q1);
696 /* check if addr ends with '\n' */
697 if(addr.r.q1>0 && addr.r.q1>addr.r.q0 && textreadc(t, addr.r.q1-1)=='\n')
699 warning(nil, "%lud", l1);
701 warning(nil, ",%lud", l2);
705 warning(nil, "#%d", addr.r.q0);
706 if(addr.r.q1 != addr.r.q0)
707 warning(nil, ",#%d", addr.r.q1);
712 eq_cmd(Text *t, Cmd *cp)
716 switch(cp->u.text->n){
721 if(cp->u.text->r[0] == '#'){
727 editerror("newline expected");
729 printposn(t, charsonly);
734 nl_cmd(Text *t, Cmd *cp)
741 /* First put it on newline boundaries */
743 addr = lineaddr(0, a, -1);
744 a = lineaddr(0, a, 1);
746 if(addr.r.q0==t->q0 && addr.r.q1==t->q1){
748 addr = lineaddr(1, a, 1);
751 textshow(t, addr.r.q0, addr.r.q1, 1);
756 append(File *f, Cmd *cp, long p)
758 if(cp->u.text->n > 0)
759 eloginsert(f, p, cp->u.text->r, cp->u.text->n);
781 bufread(&f->b, p1, buf, np);
783 warning(nil, "%S", buf);
787 f->curtext->q0 = addr.r.q0;
788 f->curtext->q1 = addr.r.q1;
799 /* same check for dirty as in settag, but we know ncache==0 */
800 dirty = !w->isdir && !w->isscratch && f->mod;
801 warning(nil, "%c%c%c %.*S\n", " '"[dirty],
802 '+', " ."[curtext!=nil && curtext->file==f], f->nname, f->name);
806 loopcmd(File *f, Cmd *cp, Range *rp, long nrp)
810 for(i=0; i<nrp; i++){
811 f->curtext->q0 = rp[i].q0;
812 f->curtext->q1 = rp[i].q1;
813 cmdexec(f->curtext, cp);
818 looper(File *f, Cmd *cp, int xy)
827 if(rxcompile(cp->re->r) == FALSE)
828 editerror("bad regexp in %c command", cp->cmdc);
831 for(p = r.q0; p<=r.q1; ){
832 if(!rxexecute(f->curtext, nil, p, r.q1, &sel)){ /* no match, but y should still run */
835 tr.q0 = op, tr.q1 = r.q1;
836 p = r.q1+1; /* exit next loop */
838 if(sel.r[0].q0==sel.r[0].q1){ /* empty match? */
849 tr.q0 = op, tr.q1 = sel.r[0].q0;
853 rp = erealloc(rp, nrp*sizeof(Range));
856 loopcmd(f, cp->u.cmd, rp, nrp);
862 linelooper(File *f, Cmd *cp)
874 a3.r.q0 = a3.r.q1 = r.q0;
875 a = lineaddr(0, a3, 1);
877 for(p = r.q0; p<r.q1; p = a3.r.q1){
879 if(p!=r.q0 || linesel.q1==p){
880 a = lineaddr(1, a3, 1);
883 if(linesel.q0 >= r.q1)
885 if(linesel.q1 >= r.q1)
887 if(linesel.q1 > linesel.q0)
888 if(linesel.q0>=a3.r.q1 && linesel.q1>a3.r.q1){
891 rp = erealloc(rp, nrp*sizeof(Range));
897 loopcmd(f, cp->u.cmd, rp, nrp);
908 } loopstruct; /* only one; X and Y can't nest */
911 alllooper(Window *w, void *v)
919 /* if(w->isscratch || w->isdir) */
922 /* only use this window if it's the current window for the file */
923 if(t->file->curtext != t)
925 /* if(w->nopen[QWevent] > 0) */
927 /* no auto-execute on files without names */
928 if(cp->re==nil && t->file->nname==0)
930 if(cp->re==nil || filematch(t->file, cp->re)==lp->XY){
931 lp->w = erealloc(lp->w, (lp->nw+1)*sizeof(Window*));
937 alllocker(Window *w, void *v)
946 filelooper(Cmd *cp, int XY)
951 editerror("can't nest %c command", "YX"[XY]);
956 if(loopstruct.w) /* error'ed out last time */
960 allwindows(alllooper, &loopstruct);
962 * add a ref to all windows to keep safe windows accessed by X
963 * that would not otherwise have a ref to hold them up during
964 * the shenanigans. note this with globalincref so that any
965 * newly created windows start with an extra reference.
967 allwindows(alllocker, (void*)1);
969 for(i=0; i<loopstruct.nw; i++)
970 cmdexec(&loopstruct.w[i]->body, cp->u.cmd);
971 allwindows(alllocker, (void*)0);
981 nextmatch(File *f, String *r, long p, int sign)
983 if(rxcompile(r->r) == FALSE)
984 editerror("bad regexp in command address");
986 if(!rxexecute(f->curtext, nil, p, 0x7FFFFFFFL, &sel))
987 editerror("no match for regexp");
988 if(sel.r[0].q0==sel.r[0].q1 && sel.r[0].q0==p){
991 if(!rxexecute(f->curtext, nil, p, 0x7FFFFFFFL, &sel))
992 editerror("address");
995 if(!rxbexecute(f->curtext, p, &sel))
996 editerror("no match for regexp");
997 if(sel.r[0].q0==sel.r[0].q1 && sel.r[0].q1==p){
1000 if(!rxbexecute(f->curtext, p, &sel))
1001 editerror("address");
1006 File *matchfile(String*);
1007 Address charaddr(long, Address, int);
1008 Address lineaddr(long, Address, int);
1011 cmdaddress(Addr *ap, Address a, int sign)
1020 a = (*(ap->type=='#'?charaddr:lineaddr))(ap->num, a, sign);
1028 a.r.q0 = a.r.q1 = f->b.nc;
1032 editerror("can't handle '");
1033 /* a.r = f->mark; */
1042 nextmatch(f, ap->u.re, sign>=0? a.r.q1 : a.r.q0, sign);
1047 f = matchfile(ap->u.re);
1052 a.r.q0 = 0, a.r.q1 = f->b.nc;
1058 a1 = cmdaddress(ap->u.left, a, 0);
1060 a1.f = a.f, a1.r.q0 = a1.r.q1 = 0;
1061 if(ap->type == ';'){
1064 f->curtext->q0 = a1.r.q0;
1065 f->curtext->q1 = a1.r.q1;
1068 a2 = cmdaddress(ap->next, a, 0);
1070 a2.f = a.f, a2.r.q0 = a2.r.q1 = f->b.nc;
1072 editerror("addresses in different files");
1073 a.f = a1.f, a.r.q0 = a1.r.q0, a.r.q1 = a2.r.q1;
1075 editerror("addresses out of order");
1083 if(ap->next==0 || ap->next->type=='+' || ap->next->type=='-')
1084 a = lineaddr(1L, a, sign);
1087 error("cmdaddress");
1090 }while(ap = ap->next); /* assign = */
1100 alltofile(Window *w, void *v)
1108 if(w->isscratch || w->isdir)
1111 /* only use this window if it's the current window for the file */
1112 if(t->file->curtext != t)
1114 /* if(w->nopen[QWevent] > 0) */
1116 if(runeeq(tp->r->r, tp->r->n, t->file->name, t->file->nname))
1126 rr.r = skipbl(r->r, r->n, &rr.n);
1129 allwindows(alltofile, &t);
1131 editerror("no such file\"%S\"", rr.r);
1136 allmatchfile(Window *w, void *v)
1142 if(w->isscratch || w->isdir)
1145 /* only use this window if it's the current window for the file */
1146 if(t->file->curtext != t)
1148 /* if(w->nopen[QWevent] > 0) */
1150 if(filematch(w->body.file, tp->r)){
1152 editerror("too many files match \"%S\"", tp->r->r);
1153 tp->f = w->body.file;
1158 matchfile(String *r)
1164 allwindows(allmatchfile, &tf);
1167 editerror("no file matches \"%S\"", r->r);
1172 filematch(File *f, String *r)
1177 int match, i, dirty;
1180 /* compile expr first so if we get an error, we haven't allocated anything */
1181 if(rxcompile(r->r) == FALSE)
1182 editerror("bad regexp in file match");
1185 /* same check for dirty as in settag, but we know ncache==0 */
1186 dirty = !w->isdir && !w->isscratch && f->mod;
1187 snprint(buf, BUFSIZE, "%c%c%c %.*S\n", " '"[dirty],
1188 '+', " ."[curtext!=nil && curtext->file==f], f->nname, f->name);
1189 rbuf = bytetorune(buf, &i);
1191 match = rxexecute(nil, rbuf, 0, i, &s);
1197 charaddr(long l, Address addr, int sign)
1200 addr.r.q0 = addr.r.q1 = l;
1202 addr.r.q1 = addr.r.q0 -= l;
1204 addr.r.q0 = addr.r.q1 += l;
1205 if(addr.r.q0<0 || addr.r.q1>addr.f->b.nc)
1206 editerror("address out of range");
1211 lineaddr(long l, Address addr, int sign)
1222 if(sign==0 || addr.r.q1==0){
1223 a.r.q0 = a.r.q1 = 0;
1229 if(sign==0 || addr.r.q1==0){
1234 n = textreadc(f->curtext, p++)=='\n';
1238 editerror("address out of range");
1239 if(textreadc(f->curtext, p++) == '\n')
1244 while(p < f->b.nc && textreadc(f->curtext, p++)!='\n')
1252 for(n = 0; n<l; ){ /* always runs once */
1255 editerror("address out of range");
1257 c = textreadc(f->curtext, p-1);
1258 if(c != '\n' || ++n != l)
1266 while(p > 0 && textreadc(f->curtext, p-1)!='\n') /* lines start after a newline */
1281 allfilecheck(Window *w, void *v)
1283 struct Filecheck *fp;
1288 if(w->body.file == fp->f)
1290 if(runeeq(fp->r, fp->nr, f->name, f->nname))
1291 warning(nil, "warning: duplicate file name \"%.*S\"\n", fp->nr, fp->r);
1295 cmdname(File *f, String *str, int set)
1299 struct Filecheck fc;
1306 /* no name; use existing */
1309 r = runemalloc(f->nname+1);
1310 runemove(r, f->name, f->nname);
1313 s = skipbl(s, n, &n);
1318 r = runemalloc(n+1);
1321 newname = dirname(f->curtext, runestrdup(s), n);
1323 r = runemalloc(n+1);
1324 runemove(r, newname.r, n);
1330 allwindows(allfilecheck, &fc);
1335 if(set && !runeeq(r, n, f->name, f->nname)){
1338 f->curtext->w->dirty = TRUE;
1339 winsetname(f->curtext->w, r, n);