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 /* ocurfile = curfile; */
86 /* loaded = curfile && !curfile->unread; */
87 if(cmdexec(curtext, cmdp) == 0)
95 allelogterm(Window *w, void *x)
98 elogterm(w->body.file);
102 alleditinit(Window *w, void *x)
105 textcommit(&w->tag, TRUE);
106 textcommit(&w->body, TRUE);
107 w->body.file->editclean = FALSE;
111 allupdate(Window *w, void *x)
120 if(f->curtext != t) /* do curtext only */
122 if(f->elog.type == Null)
124 else if(f->elog.type != Empty){
128 for(i=0; i<f->ntext; i++)
129 f->text[i]->w->dirty = FALSE;
132 textsetselect(t, t->q0, t->q1);
138 editerror(char *fmt, ...)
144 s = vsmprint(fmt, arg);
147 allwindows(allelogterm, nil); /* truncate the edit logs */
153 editcmd(Text *ct, Rune *r, uint n)
160 warning(nil, "string too long\n");
164 allwindows(alleditinit, nil);
167 cmdstartp = runemalloc(n+2);
168 runemove(cmdstartp, r, n);
170 cmdstartp[n++] = '\n';
172 cmdendp = cmdstartp+n;
177 curtext = &ct->w->body;
180 editerrc = chancreate(sizeof(char*), 0);
181 chansetname(editerrc, "editerrc");
182 lastpat = allocstring(0);
184 threadcreate(editthread, nil, STACK);
185 err = recvp(editerrc);
189 warning(nil, "Edit: %s\n", err);
193 /* update everyone whose edit log has data */
194 allwindows(allupdate, nil);
200 if(*cmdp == *cmdendp)
208 if(*cmdp == *cmdendp)
216 if(--cmdp < cmdstartp)
228 if(signok>1 && nextc()=='-'){
232 if((c=nextc())<'0' || '9'<c) /* no number defaults to 1 */
234 while('0'<=(c=getch()) && c<='9')
246 while(c==' ' || c=='\t');
253 * Check that list has room for one more element.
258 if(l->u.listptr==0 || l->nalloc==0){
260 l->u.listptr = emalloc(INCR*sizeof(long));
262 }else if(l->nused == l->nalloc){
263 l->u.listptr = erealloc(l->u.listptr, (l->nalloc+INCR)*sizeof(long));
264 memset((void*)(l->u.longptr+l->nalloc), 0, INCR*sizeof(long));
270 * Remove the ith element from the list
273 dellist(List *l, int i)
275 memmove(&l->u.longptr[i], &l->u.longptr[i+1], (l->nused-(i+1))*sizeof(long));
280 * Add a new element, whose position is i, to the list
283 inslist(List *l, int i, long val)
286 memmove(&l->u.longptr[i+1], &l->u.longptr[i], (l->nused-i)*sizeof(long));
287 l->u.longptr[i] = val;
303 s = emalloc(sizeof(String));
306 s->r = emalloc(s->nalloc*sizeof(Rune));
312 freestring(String *s)
322 p = emalloc(sizeof(Cmd));
323 inslist(&cmdlist, cmdlist.nused, (long)p);
333 inslist(&stringlist, stringlist.nused, (long)p);
342 p = emalloc(sizeof(Addr));
343 inslist(&addrlist, addrlist.nused, (long)p);
352 while(cmdlist.nused > 0)
353 free(cmdlist.u.ucharptr[--cmdlist.nused]);
354 while(addrlist.nused > 0)
355 free(addrlist.u.ucharptr[--addrlist.nused]);
356 while(stringlist.nused>0){
357 i = --stringlist.nused;
358 freestring(stringlist.u.stringptr[i]);
365 if(c=='\\' || ('a'<=c && c<='z')
366 || ('A'<=c && c<='Z') || ('0'<=c && c<='9'))
367 editerror("bad delimiter %c\n", c);
378 editerror("newline expected (saw %C)", c);
382 Straddc(String *s, int c)
384 if(s->n+1 >= s->nalloc){
386 s->r = erealloc(s->r, s->nalloc*sizeof(Rune));
393 getrhs(String *s, int delim, int cmd)
397 while((c = getch())>0 && c!=delim && c!='\n'){
400 error("bad right hand side");
406 else if(c!=delim && (cmd=='s' || c!='\\')) /* s does its own */
411 ungetch(); /* let client read whether delimiter, '\n' or whatever */
415 collecttoken(char *end)
417 String *s = newstring(0);
420 while((c=nextc())==' ' || c=='\t')
421 Straddc(s, getch()); /* blanks significant for getname() */
422 while((c=getch())>0 && utfrune(end, c)==0)
433 int begline, i, c, delim;
436 if(cmdskipbl()=='\n'){
441 while((c = getch())>0 && c!='\n')
443 i++, Straddc(s, '\n');
446 }while(s->r[begline]!='.' || s->r[begline+1]!='\n');
450 okdelim(delim = getch());
451 getrhs(s, delim, 'a');
465 for(i=0; cmdtab[i].cmdc; i++)
466 if(cmdtab[i].cmdc == c)
479 cmd.next = cmd.u.cmd = 0;
481 cmd.flag = cmd.num = 0;
482 cmd.addr = compoundaddr();
483 if(cmdskipbl() == -1)
488 if(cmd.cmdc=='c' && nextc()=='d'){ /* sleazy two-character case */
489 getch(); /* the 'd' */
492 i = cmdlookup(cmd.cmdc);
495 goto Return; /* let nl_cmd work it all out */
497 if(ct->defaddr==aNo && cmd.addr)
498 editerror("command takes no address");
500 cmd.num = getnum(ct->count);
502 /* x without pattern -> .*\n, indicated by cmd.re==0 */
503 /* X without pattern is all files */
504 if((ct->cmdc!='x' && ct->cmdc!='X') ||
505 ((c = nextc())!=' ' && c!='\t' && c!='\n')){
507 if((c = getch())=='\n' || c<0)
508 editerror("no address");
510 cmd.re = getregexp(c);
512 cmd.u.text = newstring(0);
513 getrhs(cmd.u.text, c, 's');
523 if(ct->addr && (cmd.u.mtaddr=simpleaddr())==0)
524 editerror("bad address");
526 if(cmdskipbl() == '\n'){
528 cmd.u.cmd = newcmd();
529 cmd.u.cmd->cmdc = ct->defcmd;
530 }else if((cmd.u.cmd = parsecmd(nest))==0)
533 cmd.u.text = collecttext();
535 cmd.u.text = collecttoken(ct->token);
543 if(cmdskipbl()=='\n')
545 ncp = parsecmd(nest+1);
555 editerror("right brace with no left brace");
558 editerror("unknown command %c", cmd.cmdc);
572 buf = allocstring(0);
574 if((c = getch())=='\\'){
577 else if(nextc()=='\\'){
581 }else if(c==delim || c=='\n')
584 editerror("regular expression too long");
596 editerror("no regular expression defined");
597 r = newstring(lastpat->n);
598 runemove(r->r, lastpat->r, lastpat->n); /* newstring put \0 at end */
614 addr.num = getnum(1);
616 case '0': case '1': case '2': case '3': case '4':
617 case '5': case '6': case '7': case '8': case '9':
618 addr.num = getnum(1);
621 case '/': case '?': case '"':
622 addr.u.re = getregexp(addr.type = getch());
634 if(addr.next = simpleaddr())
635 switch(addr.next->type){
641 editerror("bad address syntax");
650 if(addr.type!='+' && addr.type!='-'){
651 /* insert the missing '+' */
654 nap->next = addr.next;
675 addr.u.left = simpleaddr();
676 if((addr.type = cmdskipbl())!=',' && addr.type!=';')
679 next = addr.next = compoundaddr();
680 if(next && (next->type==',' || next->type==';') && next->u.left==0)
681 editerror("bad address syntax");