15 static char linex[]="\n";
16 static char wordx[]=" \t\n";
17 struct cmdtab cmdtab[]={
18 /* cmdc text regexp addr defcmd defaddr count token fn */
19 '\n', 0, 0, 0, 0, aDot, 0, 0, nl_cmd,
20 'a', 1, 0, 0, 0, aDot, 0, 0, a_cmd,
21 'b', 0, 0, 0, 0, aNo, 0, linex, b_cmd,
22 'c', 1, 0, 0, 0, aDot, 0, 0, c_cmd,
23 'd', 0, 0, 0, 0, aDot, 0, 0, d_cmd,
24 'e', 0, 0, 0, 0, aNo, 0, wordx, e_cmd,
25 'f', 0, 0, 0, 0, aNo, 0, wordx, f_cmd,
26 'g', 0, 1, 0, 'p', aDot, 0, 0, g_cmd,
27 'i', 1, 0, 0, 0, aDot, 0, 0, i_cmd,
28 'm', 0, 0, 1, 0, aDot, 0, 0, m_cmd,
29 'p', 0, 0, 0, 0, aDot, 0, 0, p_cmd,
30 'r', 0, 0, 0, 0, aDot, 0, wordx, e_cmd,
31 's', 0, 1, 0, 0, aDot, 1, 0, s_cmd,
32 't', 0, 0, 1, 0, aDot, 0, 0, m_cmd,
33 'u', 0, 0, 0, 0, aNo, 2, 0, u_cmd,
34 'v', 0, 1, 0, 'p', aDot, 0, 0, g_cmd,
35 'w', 0, 0, 0, 0, aAll, 0, wordx, w_cmd,
36 'x', 0, 1, 0, 'p', aDot, 0, 0, x_cmd,
37 'y', 0, 1, 0, 'p', aDot, 0, 0, x_cmd,
38 '=', 0, 0, 0, 0, aDot, 0, linex, eq_cmd,
39 'B', 0, 0, 0, 0, aNo, 0, linex, B_cmd,
40 'D', 0, 0, 0, 0, aNo, 0, linex, D_cmd,
41 'X', 0, 1, 0, 'f', aNo, 0, 0, X_cmd,
42 'Y', 0, 1, 0, 'f', aNo, 0, 0, X_cmd,
43 '<', 0, 0, 0, 0, aDot, 0, linex, pipe_cmd,
44 '|', 0, 0, 0, 0, aDot, 0, linex, pipe_cmd,
45 '>', 0, 0, 0, 0, aDot, 0, linex, pipe_cmd,
46 /* deliberately unimplemented:
47 'k', 0, 0, 0, 0, aDot, 0, 0, k_cmd,
48 'n', 0, 0, 0, 0, aNo, 0, 0, n_cmd,
49 'q', 0, 0, 0, 0, aNo, 0, 0, q_cmd,
50 '!', 0, 0, 0, 0, aNo, 0, linex, plan9_cmd,
52 0, 0, 0, 0, 0, 0, 0, 0
56 Addr *compoundaddr(void);
57 Addr *simpleaddr(void);
73 int editing = Inactive;
75 String* newstring(int);
83 threadsetname("editthread");
84 while((cmdp=parsecmd(0)) != 0){
85 if(cmdexec(curtext, cmdp) == 0)
93 allelogterm(Window *w, void *x)
96 elogterm(w->body.file);
100 alleditinit(Window *w, void *x)
103 textcommit(&w->tag, TRUE);
104 textcommit(&w->body, TRUE);
105 w->body.file->editclean = FALSE;
109 allupdate(Window *w, void *x)
118 if(f->curtext != t) /* do curtext only */
120 if(f->elog.type == Null)
122 else if(f->elog.type != Empty){
126 for(i=0; i<f->ntext; i++)
127 f->text[i]->w->dirty = FALSE;
130 textsetselect(t, t->q0, t->q1);
136 editerror(char *fmt, ...)
142 s = vsmprint(fmt, arg);
145 allwindows(allelogterm, nil); /* truncate the edit logs */
151 editcmd(Text *ct, Rune *r, uint n)
158 warning(nil, "string too long\n");
162 allwindows(alleditinit, nil);
165 cmdstartp = runemalloc(n+2);
166 runemove(cmdstartp, r, n);
168 cmdstartp[n++] = '\n';
170 cmdendp = cmdstartp+n;
175 curtext = &ct->w->body;
178 editerrc = chancreate(sizeof(char*), 0);
179 chansetname(editerrc, "editerrc");
180 lastpat = allocstring(0);
182 threadcreate(editthread, nil, STACK);
183 err = recvp(editerrc);
187 warning(nil, "Edit: %s\n", err);
191 /* update everyone whose edit log has data */
192 allwindows(allupdate, nil);
214 if(--cmdp < cmdstartp)
226 if(signok>1 && nextc()=='-'){
230 if((c=nextc())<'0' || '9'<c) /* no number defaults to 1 */
232 while('0'<=(c=getch()) && c<='9')
244 while(c==' ' || c=='\t');
251 * Check that list has room for one more element.
256 if(l->u.listptr==0 || l->nalloc==0){
258 l->u.listptr = emalloc(INCR*sizeof(void*));
260 }else if(l->nused == l->nalloc){
261 l->u.listptr = erealloc(l->u.listptr, (l->nalloc+INCR)*sizeof(void*));
262 memset(l->u.ptr+l->nalloc, 0, INCR*sizeof(void*));
268 * Remove the ith element from the list
271 dellist(List *l, int i)
273 memmove(&l->u.ptr[i], &l->u.ptr[i+1], (l->nused-(i+1))*sizeof(void*));
278 * Add a new element, whose position is i, to the list
281 inslist(List *l, int i, void *v)
284 memmove(&l->u.ptr[i+1], &l->u.ptr[i], (l->nused-i)*sizeof(void*));
301 s = emalloc(sizeof(String));
304 s->r = emalloc(s->nalloc*sizeof(Rune));
310 freestring(String *s)
320 p = emalloc(sizeof(Cmd));
321 inslist(&cmdlist, cmdlist.nused, p);
331 inslist(&stringlist, stringlist.nused, p);
340 p = emalloc(sizeof(Addr));
341 inslist(&addrlist, addrlist.nused, p);
350 while(cmdlist.nused > 0)
351 free(cmdlist.u.ucharptr[--cmdlist.nused]);
352 while(addrlist.nused > 0)
353 free(addrlist.u.ucharptr[--addrlist.nused]);
354 while(stringlist.nused>0){
355 i = --stringlist.nused;
356 freestring(stringlist.u.stringptr[i]);
363 if(c=='\\' || ('a'<=c && c<='z')
364 || ('A'<=c && c<='Z') || ('0'<=c && c<='9'))
365 editerror("bad delimiter %c\n", c);
376 editerror("newline expected (saw %C)", c);
380 Straddc(String *s, int c)
382 if(s->n+1 >= s->nalloc){
384 s->r = erealloc(s->r, s->nalloc*sizeof(Rune));
391 getrhs(String *s, int delim, int cmd)
395 while((c = getch())>0 && c!=delim && c!='\n'){
398 error("bad right hand side");
404 else if(c!=delim && (cmd=='s' || c!='\\')) /* s does its own */
409 ungetch(); /* let client read whether delimiter, '\n' or whatever */
413 collecttoken(char *end)
415 String *s = newstring(0);
418 while((c=nextc())==' ' || c=='\t')
419 Straddc(s, getch()); /* blanks significant for getname() */
420 while((c=getch())>0 && utfrune(end, c)==0)
431 int begline, i, c, delim;
434 if(cmdskipbl()=='\n'){
439 while((c = getch())>0 && c!='\n')
441 i++, Straddc(s, '\n');
444 }while(s->r[begline]!='.' || s->r[begline+1]!='\n');
448 okdelim(delim = getch());
449 getrhs(s, delim, 'a');
463 for(i=0; cmdtab[i].cmdc; i++)
464 if(cmdtab[i].cmdc == c)
477 cmd.next = cmd.u.cmd = 0;
479 cmd.flag = cmd.num = 0;
480 cmd.addr = compoundaddr();
481 if(cmdskipbl() == -1)
486 if(cmd.cmdc=='c' && nextc()=='d'){ /* sleazy two-character case */
487 getch(); /* the 'd' */
490 i = cmdlookup(cmd.cmdc);
493 goto Return; /* let nl_cmd work it all out */
495 if(ct->defaddr==aNo && cmd.addr)
496 editerror("command takes no address");
498 cmd.num = getnum(ct->count);
500 /* x without pattern -> .*\n, indicated by cmd.re==0 */
501 /* X without pattern is all files */
502 if((ct->cmdc!='x' && ct->cmdc!='X') ||
503 ((c = nextc())!=' ' && c!='\t' && c!='\n')){
505 if((c = getch())=='\n' || c<0)
506 editerror("no address");
508 cmd.re = getregexp(c);
510 cmd.u.text = newstring(0);
511 getrhs(cmd.u.text, c, 's');
521 if(ct->addr && (cmd.u.mtaddr=simpleaddr())==0)
522 editerror("bad address");
524 if(cmdskipbl() == '\n'){
526 cmd.u.cmd = newcmd();
527 cmd.u.cmd->cmdc = ct->defcmd;
528 }else if((cmd.u.cmd = parsecmd(nest))==0)
531 cmd.u.text = collecttext();
533 cmd.u.text = collecttoken(ct->token);
541 if(cmdskipbl()=='\n')
543 ncp = parsecmd(nest+1);
553 editerror("right brace with no left brace");
556 editerror("unknown command %c", cmd.cmdc);
570 buf = allocstring(0);
572 if((c = getch())=='\\'){
575 else if(nextc()=='\\'){
579 }else if(c==delim || c=='\n')
582 editerror("regular expression too long");
594 editerror("no regular expression defined");
595 r = newstring(lastpat->n);
596 runemove(r->r, lastpat->r, lastpat->n); /* newstring put \0 at end */
612 addr.num = getnum(1);
614 case '0': case '1': case '2': case '3': case '4':
615 case '5': case '6': case '7': case '8': case '9':
616 addr.num = getnum(1);
619 case '/': case '?': case '"':
620 addr.u.re = getregexp(addr.type = getch());
632 if(addr.next = simpleaddr())
633 switch(addr.next->type){
639 editerror("bad address syntax");
648 if(addr.type!='+' && addr.type!='-'){
649 /* insert the missing '+' */
652 nap->next = addr.next;
673 addr.u.left = simpleaddr();
674 if((addr.type = cmdskipbl())!=',' && addr.type!=';')
677 next = addr.next = compoundaddr();
678 if(next && (next->type==',' || next->type==';') && next->u.left==0)
679 editerror("bad address syntax");