shithub: neatroff

Download patch

ref: e178446fb18669de84e5ead2590cb53ad9efff6f
parent: 915a5bc3e66f4462cc28e68361c6de4e7a8f34e1
author: Ali Gholami Rudi <ali@rudi.ir>
date: Sat Aug 9 07:22:13 EDT 2014

reg: extract num_inc() from num_get()

Also \gx interpolates to nothing for unused registers.

--- a/cp.c
+++ b/cp.c
@@ -43,7 +43,7 @@
 		cp_back(c);
 	id = regid();
 	if (c == '-' || c == '+')
-		num_get(id, c == '+' ? 1 : -1);
+		num_inc(id, c == '+');
 	if (num_str(id))
 		in_push(num_str(id), NULL);
 }
@@ -110,7 +110,7 @@
 	if (!*s)
 		return;
 	*s++ = '\0';
-	num_set(map(arg), eval_re(s, num_get(map(arg), 0), 'u'));
+	num_set(map(arg), eval_re(s, num_get(map(arg)), 'u'));
 }
 
 /* conditional interpolation as \?'cond@expr1@expr2@' */
--- a/reg.c
+++ b/reg.c
@@ -57,6 +57,8 @@
 {
 	static char numbuf[128];
 	char *s = map_name(id);
+	if (!nregs_fmt[id])
+		nregs_fmt[id] = '0';
 	numbuf[0] = '\0';
 	if (s[0] == '.' && !s[2]) {
 		switch (s[1]) {
@@ -97,7 +99,7 @@
 		return numbuf;
 	}
 	if (s[0] == 'y' && s[1] == 'r' && !s[2]) {
-		sprintf(numbuf, "%02d", nregs[id]);
+		sprintf(numbuf, "%02d", *nreg(id));
 		return numbuf;
 	}
 	if (!nregs_fmt[id] || num_fmt(numbuf, *nreg(id), nregs_fmt[id]))
@@ -107,14 +109,21 @@
 
 void num_set(int id, int val)
 {
+	if (!nregs_fmt[id])
+		nregs_fmt[id] = '0';
 	*nreg(id) = val;
 }
 
-void num_inc(int id, int val)
+void num_setinc(int id, int val)
 {
 	nregs_inc[id] = val;
 }
 
+void num_inc(int id, int pos)
+{
+	*nreg(id) += pos > 0 ? nregs_inc[id] : -nregs_inc[id];
+}
+
 void num_del(int id)
 {
 	*nreg(id) = 0;
@@ -122,13 +131,6 @@
 	nregs_fmt[id] = 0;
 }
 
-int num_get(int id, int inc)
-{
-	if (inc)
-		*nreg(id) += inc > 0 ? nregs_inc[id] : -nregs_inc[id];
-	return *nreg(id);
-}
-
 void str_set(int id, char *s)
 {
 	int len = strlen(s) + 1;
@@ -376,12 +378,12 @@
 	static char fmtbuf[128];
 	char *s = fmtbuf;
 	int i;
-	if (!nregs_fmt[id] || (nregs_fmt[id] & NF_FMT) == '0') {
+	if ((nregs_fmt[id] & NF_FMT) == '0') {
 		*s++ = '0';
 		i = nregs_fmt[id] >> NF_LSH;
 		while (i-- > 1)
 			*s++ = '0';
-	} else {
+	} else if (nregs_fmt[id]) {
 		*s++ = nregs_fmt[id] & NF_FMT;
 	}
 	*s = '\0';
--- a/roff.h
+++ b/roff.h
@@ -77,13 +77,14 @@
 #define c_bp	"\\:"	/* zero-width word break point */
 
 /* number registers */
-int num_get(int id, int inc);
+#define num_get(id)	(*nreg(id))
 void num_set(int id, int val);
-void num_inc(int id, int val);
+void num_inc(int id, int pos);
 void num_del(int id);
 char *num_str(int id);
 char *num_getfmt(int id);
 void num_setfmt(int id, char *fmt);
+void num_setinc(int id, int val);
 int *nreg(int id);
 int eval(char *s, int unit);
 int eval_up(char **s, int unit);
--- a/tr.c
+++ b/tr.c
@@ -46,8 +46,8 @@
 	if (!args[2])
 		return;
 	id = map(args[1]);
-	num_set(id, eval_re(args[2], num_get(id, 0), 'u'));
-	num_inc(id, args[3] ? eval(args[3], 'u') : 0);
+	num_set(id, eval_re(args[2], num_get(id), 'u'));
+	num_setinc(id, args[3] ? eval(args[3], 'u') : 0);
 }
 
 static void tr_rr(char **args)