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 lastpat = allocstring(0);
183 threadcreate(editthread, nil, STACK);
184 err = recvp(editerrc);
188 warning(nil, "Edit: %s\n", err);
192 /* update everyone whose edit log has data */
193 allwindows(allupdate, nil);
199 if(*cmdp == *cmdendp)
207 if(*cmdp == *cmdendp)
215 if(--cmdp < cmdstartp)
227 if(signok>1 && nextc()=='-'){
231 if((c=nextc())<'0' || '9'<c) /* no number defaults to 1 */
233 while('0'<=(c=getch()) && c<='9')
245 while(c==' ' || c=='\t');
252 * Check that list has room for one more element.
257 if(l->u.listptr==0 || l->nalloc==0){
259 l->u.listptr = emalloc(INCR*sizeof(long));
261 }else if(l->nused == l->nalloc){
262 l->u.listptr = erealloc(l->u.listptr, (l->nalloc+INCR)*sizeof(long));
263 memset((void*)(l->u.longptr+l->nalloc), 0, INCR*sizeof(long));
269 * Remove the ith element from the list
272 dellist(List *l, int i)
274 memmove(&l->u.longptr[i], &l->u.longptr[i+1], (l->nused-(i+1))*sizeof(long));
279 * Add a new element, whose position is i, to the list
282 inslist(List *l, int i, long val)
285 memmove(&l->u.longptr[i+1], &l->u.longptr[i], (l->nused-i)*sizeof(long));
286 l->u.longptr[i] = val;
302 s = emalloc(sizeof(String));
305 s->r = emalloc(s->nalloc*sizeof(Rune));
311 freestring(String *s)
321 p = emalloc(sizeof(Cmd));
322 inslist(&cmdlist, cmdlist.nused, (long)p);
332 inslist(&stringlist, stringlist.nused, (long)p);
341 p = emalloc(sizeof(Addr));
342 inslist(&addrlist, addrlist.nused, (long)p);
351 while(cmdlist.nused > 0)
352 free(cmdlist.u.ucharptr[--cmdlist.nused]);
353 while(addrlist.nused > 0)
354 free(addrlist.u.ucharptr[--addrlist.nused]);
355 while(stringlist.nused>0){
356 i = --stringlist.nused;
357 freestring(stringlist.u.stringptr[i]);
364 if(c=='\\' || ('a'<=c && c<='z')
365 || ('A'<=c && c<='Z') || ('0'<=c && c<='9'))
366 editerror("bad delimiter %c\n", c);
377 editerror("newline expected (saw %C)", c);
381 Straddc(String *s, int c)
383 if(s->n+1 >= s->nalloc){
385 s->r = erealloc(s->r, s->nalloc*sizeof(Rune));
392 getrhs(String *s, int delim, int cmd)
396 while((c = getch())>0 && c!=delim && c!='\n'){
399 error("bad right hand side");
405 else if(c!=delim && (cmd=='s' || c!='\\')) /* s does its own */
410 ungetch(); /* let client read whether delimiter, '\n' or whatever */
414 collecttoken(char *end)
416 String *s = newstring(0);
419 while((c=nextc())==' ' || c=='\t')
420 Straddc(s, getch()); /* blanks significant for getname() */
421 while((c=getch())>0 && utfrune(end, c)==0)
432 int begline, i, c, delim;
435 if(cmdskipbl()=='\n'){
440 while((c = getch())>0 && c!='\n')
442 i++, Straddc(s, '\n');
445 }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 */
611 addr.num = getnum(1);
613 case '0': case '1': case '2': case '3': case '4':
614 case '5': case '6': case '7': case '8': case '9':
615 addr.num = getnum(1);
618 case '/': case '?': case '"':
619 addr.u.re = getregexp(addr.type = getch());
631 if(addr.next = simpleaddr())
632 switch(addr.next->type){
638 editerror("bad address syntax");
647 if(addr.type!='+' && addr.type!='-'){
648 /* insert the missing '+' */
651 nap->next = addr.next;
672 addr.u.left = simpleaddr();
673 if((addr.type = cmdskipbl())!=',' && addr.type!=';')
676 next = addr.next = compoundaddr();
677 if(next && (next->type==',' || next->type==';') && next->u.left==0)
678 editerror("bad address syntax");