diff --git a/src/modal.c b/src/modal.c index d718473..2dcd110 100644 --- a/src/modal.c +++ b/src/modal.c @@ -194,7 +194,7 @@ static int apply_rule(Rule *r, char *s) { unsigned char rid; - char c, *a = r->a, *b = r->b, *origin = dst_, *reg; + char c, *a = r->a, *b = r->b, *origin = dst_, *reg, last = 0; /* phase: clean regs */ while(stack_ != stack) regs[(int)*(--stack_)] = 0; /* phase: match rule */ @@ -216,10 +216,15 @@ apply_rule(Rule *r, char *s) if(!spacer(c)) return 0; /* phase: write rule */ while((c = *b++)) - if(c == '?' && (rid = *b) && (reg = regs[rid])) + if(c == '?' && (rid = *b) && (reg = regs[rid])){ + char *ori = dst_; write_reg(rid, reg), b++; - else - *dst_++ = c; + if(dst_ == ori){ + if(b[0] == ' ') b++; + else if(last == ' ') dst_--; + } + } else + *dst_++ = last = c; if(dst_ == origin) { while(*s == ' ') s++; if(*s == ')' && *(dst_ - 1) == ' ') dst_--;