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)
596 r = skipbl(cr, ncr, &n);
598 editerror("no command specified for %c", cmd);
600 if(state == Inserting){
604 if(cmd == '<' || cmd=='|')
605 elogdelete(t->file, t->q0, t->q1);
614 dir = dirname(t, nil, 0);
615 if(dir.nr==1 && dir.r[0]=='.'){ /* sigh */
621 if(t!=nil && t->w!=nil)
622 incref(&t->w->ref); /* run will decref */
623 run(w, runetobyte(s, n), dir.r, dir.nr, TRUE, nil, nil, TRUE);
625 if(t!=nil && t->w!=nil)
631 if(t!=nil && t->w!=nil)
636 pipe_cmd(Text *t, Cmd *cp)
638 runpipe(t, cp->cmdc, cp->u.text->r, cp->u.text->n, Inserting);
643 nlcount(Text *t, long q0, long q1)
657 bufread(&t->file->b, q0, buf, nbuf);
669 printposn(Text *t, int charsonly)
673 if (t != nil && t->file != nil && t->file->name != nil)
674 warning(nil, "%.*S:", t->file->nname, t->file->name);
676 l1 = 1+nlcount(t, 0, addr.r.q0);
677 l2 = l1+nlcount(t, addr.r.q0, addr.r.q1);
678 /* check if addr ends with '\n' */
679 if(addr.r.q1>0 && addr.r.q1>addr.r.q0 && textreadc(t, addr.r.q1-1)=='\n')
681 warning(nil, "%lud", l1);
683 warning(nil, ",%lud", l2);
687 warning(nil, "#%d", addr.r.q0);
688 if(addr.r.q1 != addr.r.q0)
689 warning(nil, ",#%d", addr.r.q1);
694 eq_cmd(Text *t, Cmd *cp)
698 switch(cp->u.text->n){
703 if(cp->u.text->r[0] == '#'){
709 editerror("newline expected");
711 printposn(t, charsonly);
716 nl_cmd(Text *t, Cmd *cp)
723 /* First put it on newline boundaries */
725 addr = lineaddr(0, a, -1);
726 a = lineaddr(0, a, 1);
728 if(addr.r.q0==t->q0 && addr.r.q1==t->q1){
730 addr = lineaddr(1, a, 1);
733 textshow(t, addr.r.q0, addr.r.q1, 1);
738 append(File *f, Cmd *cp, long p)
740 if(cp->u.text->n > 0)
741 eloginsert(f, p, cp->u.text->r, cp->u.text->n);
763 bufread(&f->b, p1, buf, np);
765 warning(nil, "%S", buf);
769 f->curtext->q0 = addr.r.q0;
770 f->curtext->q1 = addr.r.q1;
781 /* same check for dirty as in settag, but we know ncache==0 */
782 dirty = !w->isdir && !w->isscratch && f->mod;
783 warning(nil, "%c%c%c %.*S\n", " '"[dirty],
784 '+', " ."[curtext!=nil && curtext->file==f], f->nname, f->name);
788 loopcmd(File *f, Cmd *cp, Range *rp, long nrp)
792 for(i=0; i<nrp; i++){
793 f->curtext->q0 = rp[i].q0;
794 f->curtext->q1 = rp[i].q1;
795 cmdexec(f->curtext, cp);
800 looper(File *f, Cmd *cp, int xy)
809 if(rxcompile(cp->re->r) == FALSE)
810 editerror("bad regexp in %c command", cp->cmdc);
813 for(p = r.q0; p<=r.q1; ){
814 if(!rxexecute(f->curtext, nil, p, r.q1, &sel)){ /* no match, but y should still run */
817 tr.q0 = op, tr.q1 = r.q1;
818 p = r.q1+1; /* exit next loop */
820 if(sel.r[0].q0==sel.r[0].q1){ /* empty match? */
831 tr.q0 = op, tr.q1 = sel.r[0].q0;
835 rp = erealloc(rp, nrp*sizeof(Range));
838 loopcmd(f, cp->u.cmd, rp, nrp);
844 linelooper(File *f, Cmd *cp)
856 a3.r.q0 = a3.r.q1 = r.q0;
857 a = lineaddr(0, a3, 1);
859 for(p = r.q0; p<r.q1; p = a3.r.q1){
861 if(p!=r.q0 || linesel.q1==p){
862 a = lineaddr(1, a3, 1);
865 if(linesel.q0 >= r.q1)
867 if(linesel.q1 >= r.q1)
869 if(linesel.q1 > linesel.q0)
870 if(linesel.q0>=a3.r.q1 && linesel.q1>a3.r.q1){
873 rp = erealloc(rp, nrp*sizeof(Range));
879 loopcmd(f, cp->u.cmd, rp, nrp);
890 } loopstruct; /* only one; X and Y can't nest */
893 alllooper(Window *w, void *v)
901 // if(w->isscratch || w->isdir)
904 /* only use this window if it's the current window for the file */
905 if(t->file->curtext != t)
907 // if(w->nopen[QWevent] > 0)
909 /* no auto-execute on files without names */
910 if(cp->re==nil && t->file->nname==0)
912 if(cp->re==nil || filematch(t->file, cp->re)==lp->XY){
913 lp->w = erealloc(lp->w, (lp->nw+1)*sizeof(Window*));
919 alllocker(Window *w, void *v)
928 filelooper(Cmd *cp, int XY)
933 editerror("can't nest %c command", "YX"[XY]);
938 if(loopstruct.w) /* error'ed out last time */
942 allwindows(alllooper, &loopstruct);
944 * add a ref to all windows to keep safe windows accessed by X
945 * that would not otherwise have a ref to hold them up during
946 * the shenanigans. note this with globalincref so that any
947 * newly created windows start with an extra reference.
949 allwindows(alllocker, (void*)1);
951 for(i=0; i<loopstruct.nw; i++)
952 cmdexec(&loopstruct.w[i]->body, cp->u.cmd);
953 allwindows(alllocker, (void*)0);
963 nextmatch(File *f, String *r, long p, int sign)
965 if(rxcompile(r->r) == FALSE)
966 editerror("bad regexp in command address");
968 if(!rxexecute(f->curtext, nil, p, 0x7FFFFFFFL, &sel))
969 editerror("no match for regexp");
970 if(sel.r[0].q0==sel.r[0].q1 && sel.r[0].q0==p){
973 if(!rxexecute(f->curtext, nil, p, 0x7FFFFFFFL, &sel))
974 editerror("address");
977 if(!rxbexecute(f->curtext, p, &sel))
978 editerror("no match for regexp");
979 if(sel.r[0].q0==sel.r[0].q1 && sel.r[0].q1==p){
982 if(!rxbexecute(f->curtext, p, &sel))
983 editerror("address");
988 File *matchfile(String*);
989 Address charaddr(long, Address, int);
990 Address lineaddr(long, Address, int);
993 cmdaddress(Addr *ap, Address a, int sign)
1002 a = (*(ap->type=='#'?charaddr:lineaddr))(ap->num, a, sign);
1010 a.r.q0 = a.r.q1 = f->b.nc;
1014 editerror("can't handle '");
1024 nextmatch(f, ap->u.re, sign>=0? a.r.q1 : a.r.q0, sign);
1029 f = matchfile(ap->u.re);
1034 a.r.q0 = 0, a.r.q1 = f->b.nc;
1040 a1 = cmdaddress(ap->u.left, a, 0);
1042 a1.f = a.f, a1.r.q0 = a1.r.q1 = 0;
1043 if(ap->type == ';'){
1046 f->curtext->q0 = a1.r.q0;
1047 f->curtext->q1 = a1.r.q1;
1050 a2 = cmdaddress(ap->next, a, 0);
1052 a2.f = a.f, a2.r.q0 = a2.r.q1 = f->b.nc;
1054 editerror("addresses in different files");
1055 a.f = a1.f, a.r.q0 = a1.r.q0, a.r.q1 = a2.r.q1;
1057 editerror("addresses out of order");
1065 if(ap->next==0 || ap->next->type=='+' || ap->next->type=='-')
1066 a = lineaddr(1L, a, sign);
1069 error("cmdaddress");
1072 }while(ap = ap->next); /* assign = */
1082 alltofile(Window *w, void *v)
1090 if(w->isscratch || w->isdir)
1093 /* only use this window if it's the current window for the file */
1094 if(t->file->curtext != t)
1096 // if(w->nopen[QWevent] > 0)
1098 if(runeeq(tp->r->r, tp->r->n, t->file->name, t->file->nname))
1108 rr.r = skipbl(r->r, r->n, &rr.n);
1111 allwindows(alltofile, &t);
1113 editerror("no such file\"%S\"", rr.r);
1118 allmatchfile(Window *w, void *v)
1124 if(w->isscratch || w->isdir)
1127 /* only use this window if it's the current window for the file */
1128 if(t->file->curtext != t)
1130 // if(w->nopen[QWevent] > 0)
1132 if(filematch(w->body.file, tp->r)){
1134 editerror("too many files match \"%S\"", tp->r->r);
1135 tp->f = w->body.file;
1140 matchfile(String *r)
1146 allwindows(allmatchfile, &tf);
1149 editerror("no file matches \"%S\"", r->r);
1154 filematch(File *f, String *r)
1159 int match, i, dirty;
1162 /* compile expr first so if we get an error, we haven't allocated anything */
1163 if(rxcompile(r->r) == FALSE)
1164 editerror("bad regexp in file match");
1167 /* same check for dirty as in settag, but we know ncache==0 */
1168 dirty = !w->isdir && !w->isscratch && f->mod;
1169 snprint(buf, BUFSIZE, "%c%c%c %.*S\n", " '"[dirty],
1170 '+', " ."[curtext!=nil && curtext->file==f], f->nname, f->name);
1171 rbuf = bytetorune(buf, &i);
1173 match = rxexecute(nil, rbuf, 0, i, &s);
1179 charaddr(long l, Address addr, int sign)
1182 addr.r.q0 = addr.r.q1 = l;
1184 addr.r.q1 = addr.r.q0 -= l;
1186 addr.r.q0 = addr.r.q1 += l;
1187 if(addr.r.q0<0 || addr.r.q1>addr.f->b.nc)
1188 editerror("address out of range");
1193 lineaddr(long l, Address addr, int sign)
1204 if(sign==0 || addr.r.q1==0){
1205 a.r.q0 = a.r.q1 = 0;
1211 if(sign==0 || addr.r.q1==0){
1216 n = textreadc(f->curtext, p++)=='\n';
1220 editerror("address out of range");
1221 if(textreadc(f->curtext, p++) == '\n')
1226 while(p < f->b.nc && textreadc(f->curtext, p++)!='\n')
1234 for(n = 0; n<l; ){ /* always runs once */
1237 editerror("address out of range");
1239 c = textreadc(f->curtext, p-1);
1240 if(c != '\n' || ++n != l)
1248 while(p > 0 && textreadc(f->curtext, p-1)!='\n') /* lines start after a newline */
1263 allfilecheck(Window *w, void *v)
1265 struct Filecheck *fp;
1270 if(w->body.file == fp->f)
1272 if(runeeq(fp->r, fp->nr, f->name, f->nname))
1273 warning(nil, "warning: duplicate file name \"%.*S\"\n", fp->nr, fp->r);
1277 cmdname(File *f, String *str, int set)
1281 struct Filecheck fc;
1288 /* no name; use existing */
1291 r = runemalloc(f->nname+1);
1292 runemove(r, f->name, f->nname);
1295 s = skipbl(s, n, &n);
1300 r = runemalloc(n+1);
1303 newname = dirname(f->curtext, runestrdup(s), n);
1305 r = runemalloc(n+1);
1306 runemove(r, newname.r, n);
1312 allwindows(allfilecheck, &fc);
1317 if(set && !runeeq(r, n, f->name, f->nname)){
1320 f->curtext->w->dirty = TRUE;
1321 winsetname(f->curtext->w, r, n);