~rabbits/modal

89f393e3492381e2f1115a6c33cf576624305ffa — Devine Lu Linvega a month ago 13ec84d
Handle empty register writes at the register level
1 files changed, 8 insertions(+), 14 deletions(-)

M src/modal.c
M src/modal.c => src/modal.c +8 -14
@@ 123,16 123,17 @@ file_import(char *path, char *ptr)
	return copy("NAF", ptr, 3);
}

static int
static void
write_reg(char r, char *reg)
{
	char *origin = dst_;
	switch(r) {
	case ':': device_write(reg); break;
	case '~': {
		unsigned char c;
		char *origin = dst_;
		while(fread(&c, 1, 1, stdin) && c >= ' ') *dst_++ = c;
		if(feof(stdin)) dst_ = copy("EOF", dst_, 3);
		if(origin == dst_) dst_--;
		break;
	}
	case '_': {


@@ 172,7 173,6 @@ write_reg(char r, char *reg)
	}
	default: dst_ = copy(reg, dst_, walk(reg) - reg);
	}
	return dst_ - origin;
}

static int


@@ 193,7 193,7 @@ static int
apply_rule(Rule *r, char *s)
{
	unsigned char rid;
	char c, *a = r->a, *b = r->b, *origin = dst_, *reg, last = 0;
	char c, *a = r->a, *b = r->b, *origin = dst_, *reg;
	/* phase: clean regs */
	while(stack_ != stack) regs[(int)*(--stack_)] = 0;
	/* phase: match rule */


@@ 215,16 215,10 @@ apply_rule(Rule *r, char *s)
	if(!spacer(c)) return 0;
	/* phase: write rule */
	while((c = *b++))
		if(c == '?' && (rid = *b) && (reg = regs[rid])) {
			b++;
			if(!write_reg(rid, reg)) {
				if(*b == ' ')
					b++;
				else if(last == ' ')
					dst_--;
			}
		} else
			*dst_++ = last = c;
		if(c == '?' && (rid = *b) && (reg = regs[rid]))
			write_reg(rid, reg), b++;
		else
			*dst_++ = c;
	if(dst_ == origin) {
		while(*s == ' ') s++;
		if(*s == ')' && *(dst_ - 1) == ' ') dst_--;