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";
141 eloginsert(f, q, r, nr);
144 collection = runerealloc(collection, ncollection+nr+1);
145 runemove(collection+ncollection, r, nr);
147 collection[ncollection] = '\0';
150 return "unknown state in edittext";
154 /* string is known to be NUL-terminated */
156 filelist(Text *t, Rune *r, int nr)
160 r = skipbl(r, nr, &nr);
162 return runestrdup(r);
163 /* use < command to collect text */
165 runpipe(t, '<', r+1, nr-1, Collecting);
170 a_cmd(Text *t, Cmd *cp)
172 return append(t->file, cp, addr.r.q1);
176 b_cmd(Text *t, Cmd *cp)
181 f = tofile(cp->u.text);
184 curtext = f->curtext;
189 B_cmd(Text *t, Cmd *cp)
194 list = filelist(t, cp->u.text->r, cp->u.text->n);
199 r = skipbl(r, nr, &nr);
201 new(t, t, nil, 0, 0, r, 0);
203 s = findbl(r, nr, &nr);
205 new(t, t, nil, 0, 0, r, runestrlen(r));
207 r = skipbl(s+1, nr-1, &nr);
214 c_cmd(Text *t, Cmd *cp)
216 elogreplace(t->file, addr.r.q0, addr.r.q1, cp->u.text->r, cp->u.text->n);
218 t->q1 = addr.r.q0+cp->u.text->n;
223 d_cmd(Text *t, Cmd *cp)
226 if(addr.r.q1 > addr.r.q0)
227 elogdelete(t->file, addr.r.q0, addr.r.q1);
236 if(t->w->body.file->ntext>1 || winclean(t->w, FALSE))
237 colclose(t->col, t->w, TRUE);
241 D_cmd(Text *t, Cmd *cp)
243 Rune *list, *r, *s, *n;
249 list = filelist(t, cp->u.text->r, cp->u.text->n);
254 dir = dirname(t, nil, 0);
257 r = skipbl(r, nr, &nr);
259 s = findbl(r, nr, &nr);
261 /* first time through, could be empty string, meaning delete file empty name */
263 if(r[0]=='/' || nn==0 || dir.nr==0){
264 rs.r = runestrdup(r);
267 n = runemalloc(dir.nr+1+nn);
268 runemove(n, dir.r, dir.nr);
270 runemove(n+dir.nr+1, r, nn);
271 rs = cleanrname(runestr(n, dir.nr+1+nn));
273 w = lookfile(rs.r, rs.nr);
275 snprint(buf, sizeof buf, "no such file %.*S", rs.nr, rs.r);
282 r = skipbl(s+1, nr-1, &nr);
290 readloader(void *v, uint q0, Rune *r, int nr)
293 eloginsert(v, q0, r, nr);
298 e_cmd(Text *t, Cmd *cp)
302 int i, isdir, q0, q1, fd, nulls, samename, allreplaced;
310 if(winclean(t->w, TRUE)==FALSE)
311 editerror(""); /* winclean generated message already */
315 allreplaced = (q0==0 && q1==f->b.nc);
316 name = cmdname(f, cp->u.text, cp->cmdc=='e');
319 i = runestrlen(name);
320 samename = runeeq(name, i, t->file->name, t->file->nname);
321 s = runetobyte(name, i);
325 snprint(tmp, sizeof tmp, "can't open %s: %r", s);
330 isdir = (d!=nil && (d->qid.type&QTDIR));
334 snprint(tmp, sizeof tmp, "%s is a directory", s);
338 elogdelete(f, q0, q1);
340 loadfile(fd, q1, &nulls, readloader, f);
344 warning(nil, "%s: NUL bytes elided\n", s);
345 else if(allreplaced && samename)
350 static Rune Lempty[] = { 0 };
352 f_cmd(Text *t, Cmd *cp)
358 if(cp->u.text == nil){
364 name = cmdname(t->file, str, TRUE);
371 g_cmd(Text *t, Cmd *cp)
373 if(t->file != addr.f){
374 warning(nil, "internal error: g_cmd f!=addr.f\n");
377 if(rxcompile(cp->re->r) == FALSE)
378 editerror("bad regexp in g command");
379 if(rxexecute(t, nil, addr.r.q0, addr.r.q1, &sel) ^ cp->cmdc=='v'){
382 return cmdexec(t, cp->u.cmd);
388 i_cmd(Text *t, Cmd *cp)
390 return append(t->file, cp, addr.r.q0);
394 copy(File *f, Address addr2)
401 for(p=addr.r.q0; p<addr.r.q1; p+=ni){
405 bufread(&f->b, p, buf, ni);
406 eloginsert(addr2.f, addr2.r.q1, buf, ni);
412 move(File *f, Address addr2)
414 if(addr.f!=addr2.f || addr.r.q1<=addr2.r.q0){
415 elogdelete(f, addr.r.q0, addr.r.q1);
417 }else if(addr.r.q0 >= addr2.r.q1){
419 elogdelete(f, addr.r.q0, addr.r.q1);
421 error("move overlaps itself");
425 m_cmd(Text *t, Cmd *cp)
429 mkaddr(&dot, t->file);
430 addr2 = cmdaddress(cp->u.mtaddr, dot, 0);
432 move(t->file, addr2);
434 copy(t->file, addr2);
439 p_cmd(Text *t, Cmd *cp)
442 return pdisplay(t->file);
446 s_cmd(Text *t, Cmd *cp)
448 int i, j, k, c, m, n, nrp, didsub;
457 if(rxcompile(cp->re->r) == FALSE)
458 editerror("bad regexp in s command");
463 for(p1 = addr.r.q0; p1<=addr.r.q1 && rxexecute(t, nil, p1, addr.r.q1, &sel); ){
464 if(sel.r[0].q0 == sel.r[0].q1){ /* empty match? */
465 if(sel.r[0].q0 == op){
476 rp = erealloc(rp, nrp*sizeof(Rangeset));
480 buf = allocstring(0);
481 for(m=0; m<nrp; m++){
485 for(i = 0; i<cp->u.text->n; i++)
486 if((c = cp->u.text->r[i])=='\\' && i<cp->u.text->n-1){
487 c = cp->u.text->r[++i];
488 if('1'<=c && c<='9') {
490 if(sel.r[j].q1-sel.r[j].q0>RBUFSIZE){
491 err = "replacement string too long";
494 bufread(&t->file->b, sel.r[j].q0, rbuf, sel.r[j].q1-sel.r[j].q0);
495 for(k=0; k<sel.r[j].q1-sel.r[j].q0; k++)
496 Straddc(buf, rbuf[k]);
502 if(sel.r[0].q1-sel.r[0].q0>RBUFSIZE){
503 err = "right hand side too long in substitution";
506 bufread(&t->file->b, sel.r[0].q0, rbuf, sel.r[0].q1-sel.r[0].q0);
507 for(k=0; k<sel.r[0].q1-sel.r[0].q0; k++)
508 Straddc(buf, rbuf[k]);
510 elogreplace(t->file, sel.r[0].q0, sel.r[0].q1, buf->r, buf->n);
511 delta -= sel.r[0].q1-sel.r[0].q0;
520 if(!didsub && nest==0)
521 editerror("no substitution");
523 t->q1 = addr.r.q1+delta;
535 u_cmd(Text *t, Cmd *cp)
546 while(n-->0 && t->file->seq!=0 && t->file->seq!=oseq){
548 undo(t, nil, nil, flag, 0, nil, 0);
554 w_cmd(Text *t, Cmd *cp)
561 editerror("can't write file with pending modifications");
562 r = cmdname(f, cp->u.text, FALSE);
564 editerror("no name specified for 'w' command");
565 putfile(f, addr.r.q0, addr.r.q1, r, runestrlen(r));
566 /* r is freed by putfile */
571 x_cmd(Text *t, Cmd *cp)
574 looper(t->file, cp, cp->cmdc=='x');
576 linelooper(t->file, cp);
581 X_cmd(Text *t, Cmd *cp)
585 filelooper(cp, cp->cmdc=='X');
590 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){
606 if(cmd == '<' || cmd=='|')
607 elogdelete(t->file, t->q0, t->q1);
616 dir = dirname(t, nil, 0);
617 if(dir.nr==1 && dir.r[0]=='.'){ /* sigh */
623 if(t!=nil && t->w!=nil)
624 incref(&t->w->ref); /* run will decref */
625 run(w, runetobyte(s, n), dir.r, dir.nr, TRUE, nil, nil, TRUE);
627 if(t!=nil && t->w!=nil)
633 if(t!=nil && t->w!=nil)
635 if(state == Inserting){
637 t->q1 = addr.r.q0 + t->w->neditwrsel;
642 pipe_cmd(Text *t, Cmd *cp)
644 runpipe(t, cp->cmdc, cp->u.text->r, cp->u.text->n, Inserting);
649 nlcount(Text *t, long q0, long q1)
663 bufread(&t->file->b, q0, buf, nbuf);
675 printposn(Text *t, int charsonly)
679 if (t != nil && t->file != nil && t->file->name != nil)
680 warning(nil, "%.*S:", t->file->nname, t->file->name);
682 l1 = 1+nlcount(t, 0, addr.r.q0);
683 l2 = l1+nlcount(t, addr.r.q0, addr.r.q1);
684 /* check if addr ends with '\n' */
685 if(addr.r.q1>0 && addr.r.q1>addr.r.q0 && textreadc(t, addr.r.q1-1)=='\n')
687 warning(nil, "%lud", l1);
689 warning(nil, ",%lud", l2);
693 warning(nil, "#%d", addr.r.q0);
694 if(addr.r.q1 != addr.r.q0)
695 warning(nil, ",#%d", addr.r.q1);
700 eq_cmd(Text *t, Cmd *cp)
704 switch(cp->u.text->n){
709 if(cp->u.text->r[0] == '#'){
715 editerror("newline expected");
717 printposn(t, charsonly);
722 nl_cmd(Text *t, Cmd *cp)
729 /* First put it on newline boundaries */
731 addr = lineaddr(0, a, -1);
732 a = lineaddr(0, a, 1);
734 if(addr.r.q0==t->q0 && addr.r.q1==t->q1){
736 addr = lineaddr(1, a, 1);
739 textshow(t, addr.r.q0, addr.r.q1, 1);
744 append(File *f, Cmd *cp, long p)
746 if(cp->u.text->n > 0)
747 eloginsert(f, p, cp->u.text->r, cp->u.text->n);
749 f->curtext->q1 = p+cp->u.text->n;
769 bufread(&f->b, p1, buf, np);
771 warning(nil, "%S", buf);
775 f->curtext->q0 = addr.r.q0;
776 f->curtext->q1 = addr.r.q1;
787 /* same check for dirty as in settag, but we know ncache==0 */
788 dirty = !w->isdir && !w->isscratch && f->mod;
789 warning(nil, "%c%c%c %.*S\n", " '"[dirty],
790 '+', " ."[curtext!=nil && curtext->file==f], f->nname, f->name);
794 loopcmd(File *f, Cmd *cp, Range *rp, long nrp)
798 for(i=0; i<nrp; i++){
799 f->curtext->q0 = rp[i].q0;
800 f->curtext->q1 = rp[i].q1;
801 cmdexec(f->curtext, cp);
806 looper(File *f, Cmd *cp, int xy)
815 if(rxcompile(cp->re->r) == FALSE)
816 editerror("bad regexp in %c command", cp->cmdc);
819 for(p = r.q0; p<=r.q1; ){
820 if(!rxexecute(f->curtext, nil, p, r.q1, &sel)){ /* no match, but y should still run */
823 tr.q0 = op, tr.q1 = r.q1;
824 p = r.q1+1; /* exit next loop */
826 if(sel.r[0].q0==sel.r[0].q1){ /* empty match? */
837 tr.q0 = op, tr.q1 = sel.r[0].q0;
841 rp = erealloc(rp, nrp*sizeof(Range));
844 loopcmd(f, cp->u.cmd, rp, nrp);
850 linelooper(File *f, Cmd *cp)
862 a3.r.q0 = a3.r.q1 = r.q0;
863 a = lineaddr(0, a3, 1);
865 for(p = r.q0; p<r.q1; p = a3.r.q1){
867 if(p!=r.q0 || linesel.q1==p){
868 a = lineaddr(1, a3, 1);
871 if(linesel.q0 >= r.q1)
873 if(linesel.q1 >= r.q1)
875 if(linesel.q1 > linesel.q0)
876 if(linesel.q0>=a3.r.q1 && linesel.q1>a3.r.q1){
879 rp = erealloc(rp, nrp*sizeof(Range));
885 loopcmd(f, cp->u.cmd, rp, nrp);
896 } loopstruct; /* only one; X and Y can't nest */
899 alllooper(Window *w, void *v)
907 // if(w->isscratch || w->isdir)
910 /* only use this window if it's the current window for the file */
911 if(t->file->curtext != t)
913 // if(w->nopen[QWevent] > 0)
915 /* no auto-execute on files without names */
916 if(cp->re==nil && t->file->nname==0)
918 if(cp->re==nil || filematch(t->file, cp->re)==lp->XY){
919 lp->w = erealloc(lp->w, (lp->nw+1)*sizeof(Window*));
925 alllocker(Window *w, void *v)
934 filelooper(Cmd *cp, int XY)
939 editerror("can't nest %c command", "YX"[XY]);
944 if(loopstruct.w) /* error'ed out last time */
948 allwindows(alllooper, &loopstruct);
950 * add a ref to all windows to keep safe windows accessed by X
951 * that would not otherwise have a ref to hold them up during
952 * the shenanigans. note this with globalincref so that any
953 * newly created windows start with an extra reference.
955 allwindows(alllocker, (void*)1);
957 for(i=0; i<loopstruct.nw; i++)
958 cmdexec(&loopstruct.w[i]->body, cp->u.cmd);
959 allwindows(alllocker, (void*)0);
969 nextmatch(File *f, String *r, long p, int sign)
971 if(rxcompile(r->r) == FALSE)
972 editerror("bad regexp in command address");
974 if(!rxexecute(f->curtext, nil, p, 0x7FFFFFFFL, &sel))
975 editerror("no match for regexp");
976 if(sel.r[0].q0==sel.r[0].q1 && sel.r[0].q0==p){
979 if(!rxexecute(f->curtext, nil, p, 0x7FFFFFFFL, &sel))
980 editerror("address");
983 if(!rxbexecute(f->curtext, p, &sel))
984 editerror("no match for regexp");
985 if(sel.r[0].q0==sel.r[0].q1 && sel.r[0].q1==p){
988 if(!rxbexecute(f->curtext, p, &sel))
989 editerror("address");
994 File *matchfile(String*);
995 Address charaddr(long, Address, int);
996 Address lineaddr(long, Address, int);
999 cmdaddress(Addr *ap, Address a, int sign)
1008 a = (*(ap->type=='#'?charaddr:lineaddr))(ap->num, a, sign);
1016 a.r.q0 = a.r.q1 = f->b.nc;
1020 editerror("can't handle '");
1030 nextmatch(f, ap->u.re, sign>=0? a.r.q1 : a.r.q0, sign);
1035 f = matchfile(ap->u.re);
1040 a.r.q0 = 0, a.r.q1 = f->b.nc;
1046 a1 = cmdaddress(ap->u.left, a, 0);
1048 a1.f = a.f, a1.r.q0 = a1.r.q1 = 0;
1049 if(ap->type == ';'){
1052 f->curtext->q0 = a1.r.q0;
1053 f->curtext->q1 = a1.r.q1;
1056 a2 = cmdaddress(ap->next, a, 0);
1058 a2.f = a.f, a2.r.q0 = a2.r.q1 = f->b.nc;
1060 editerror("addresses in different files");
1061 a.f = a1.f, a.r.q0 = a1.r.q0, a.r.q1 = a2.r.q1;
1063 editerror("addresses out of order");
1071 if(ap->next==0 || ap->next->type=='+' || ap->next->type=='-')
1072 a = lineaddr(1L, a, sign);
1075 error("cmdaddress");
1078 }while(ap = ap->next); /* assign = */
1088 alltofile(Window *w, void *v)
1096 if(w->isscratch || w->isdir)
1099 /* only use this window if it's the current window for the file */
1100 if(t->file->curtext != t)
1102 // if(w->nopen[QWevent] > 0)
1104 if(runeeq(tp->r->r, tp->r->n, t->file->name, t->file->nname))
1114 rr.r = skipbl(r->r, r->n, &rr.n);
1117 allwindows(alltofile, &t);
1119 editerror("no such file\"%S\"", rr.r);
1124 allmatchfile(Window *w, void *v)
1130 if(w->isscratch || w->isdir)
1133 /* only use this window if it's the current window for the file */
1134 if(t->file->curtext != t)
1136 // if(w->nopen[QWevent] > 0)
1138 if(filematch(w->body.file, tp->r)){
1140 editerror("too many files match \"%S\"", tp->r->r);
1141 tp->f = w->body.file;
1146 matchfile(String *r)
1152 allwindows(allmatchfile, &tf);
1155 editerror("no file matches \"%S\"", r->r);
1160 filematch(File *f, String *r)
1165 int match, i, dirty;
1168 /* compile expr first so if we get an error, we haven't allocated anything */
1169 if(rxcompile(r->r) == FALSE)
1170 editerror("bad regexp in file match");
1173 /* same check for dirty as in settag, but we know ncache==0 */
1174 dirty = !w->isdir && !w->isscratch && f->mod;
1175 snprint(buf, BUFSIZE, "%c%c%c %.*S\n", " '"[dirty],
1176 '+', " ."[curtext!=nil && curtext->file==f], f->nname, f->name);
1177 rbuf = bytetorune(buf, &i);
1179 match = rxexecute(nil, rbuf, 0, i, &s);
1185 charaddr(long l, Address addr, int sign)
1188 addr.r.q0 = addr.r.q1 = l;
1190 addr.r.q1 = addr.r.q0 -= l;
1192 addr.r.q0 = addr.r.q1 += l;
1193 if(addr.r.q0<0 || addr.r.q1>addr.f->b.nc)
1194 editerror("address out of range");
1199 lineaddr(long l, Address addr, int sign)
1210 if(sign==0 || addr.r.q1==0){
1211 a.r.q0 = a.r.q1 = 0;
1217 if(sign==0 || addr.r.q1==0){
1222 n = textreadc(f->curtext, p++)=='\n';
1226 editerror("address out of range");
1227 if(textreadc(f->curtext, p++) == '\n')
1232 while(p < f->b.nc && textreadc(f->curtext, p++)!='\n')
1240 for(n = 0; n<l; ){ /* always runs once */
1243 editerror("address out of range");
1245 c = textreadc(f->curtext, p-1);
1246 if(c != '\n' || ++n != l)
1254 while(p > 0 && textreadc(f->curtext, p-1)!='\n') /* lines start after a newline */
1269 allfilecheck(Window *w, void *v)
1271 struct Filecheck *fp;
1276 if(w->body.file == fp->f)
1278 if(runeeq(fp->r, fp->nr, f->name, f->nname))
1279 warning(nil, "warning: duplicate file name \"%.*S\"\n", fp->nr, fp->r);
1283 cmdname(File *f, String *str, int set)
1287 struct Filecheck fc;
1294 /* no name; use existing */
1297 r = runemalloc(f->nname+1);
1298 runemove(r, f->name, f->nname);
1301 s = skipbl(s, n, &n);
1306 r = runemalloc(n+1);
1309 newname = dirname(f->curtext, runestrdup(s), n);
1316 allwindows(allfilecheck, &fc);
1321 if(set && !runeeq(r, n, f->name, f->nname)){
1324 f->curtext->w->dirty = TRUE;
1325 winsetname(f->curtext->w, r, n);