shithub: neatroff

Download patch

ref: fa8a3c6bfa5bfa31e159bc681cbb79b83d89428e
parent: 6e3ecea216b6fee1ec2f1e51c8984152f2d25c83
author: Ali Gholami Rudi <ali@rudi.ir>
date: Wed Mar 27 14:49:40 EDT 2013

reg: restore registers after diverted lines

--- a/reg.c
+++ b/reg.c
@@ -141,3 +141,31 @@
 {
 	return env->adj;
 }
+
+/* saving and restoring registers around diverted lines */
+struct odiv {
+	int f, s, f0, s0;
+};
+
+static struct odiv odivs[NPREV];	/* state before diverted text */
+static int nodivs;
+
+/* begin outputting diverted line */
+void odiv_beg(void)
+{
+	struct odiv *o = &odivs[nodivs++];
+	o->f = n_f;
+	o->s = n_s;
+	o->f0 = n_f0;
+	o->s0 = n_s0;
+}
+
+/* end outputting diverted line */
+void odiv_end(void)
+{
+	struct odiv *o = &odivs[--nodivs];
+	n_f = o->f;
+	n_s = o->s;
+	n_f0 = o->f0;
+	n_s0 = o->s0;
+}
--- a/ren.c
+++ b/ren.c
@@ -8,15 +8,9 @@
 #define ADJ_MODE	(n_u ? n_j : ADJ_N)
 #define adj		env_adj()	/* line buffer */
 
-/* diversion */
-struct div {
-	int f, s, f0, s0;		/* backup variables */
-};
-
 static int ren_backed = -1;		/* pushed back character */
 static int ren_div;			/* current diversion */
 static struct sbuf out_div;		/* current diversion output */
-static struct div cur_div;
 
 static int ren_next(void)
 {
@@ -55,6 +49,8 @@
 	if (args[1]) {
 		sbuf_init(&out_div);
 		ren_div = REG(args[1][0], args[1][1]);
+		if (args[0][2] == 'a')	/* .da */
+			sbuf_append(&out_div, str_get(ren_div));
 		n_d = 0;
 	} else if (ren_div) {
 		sbuf_putnl(&out_div);
@@ -64,24 +60,6 @@
 	}
 }
 
-/* begin outputting diverted line */
-static void div_beg(void)
-{
-	cur_div.f = n_f;
-	cur_div.s = n_s;
-	cur_div.f0 = n_f0;
-	cur_div.s0 = n_s0;
-}
-
-/* end outputting diverted line */
-static void div_end(void)
-{
-	n_f = cur_div.f;
-	n_s = cur_div.s;
-	n_f0 = cur_div.f0;
-	n_s0 = cur_div.s0;
-}
-
 /* vertical motion before rendering lines */
 static void down(int n)
 {
@@ -269,9 +247,9 @@
 			if (c[0] == DIV_BEG[1]) {
 				nextchar(c);
 				if (c[0] == DIV_BEG[2])
-					div_beg();
+					odiv_beg();
 				if (c[0] == DIV_END[2])
-					div_end();
+					odiv_end();
 				continue;
 			}
 			if (c[0] == '(') {
--- a/tr.c
+++ b/tr.c
@@ -290,6 +290,7 @@
 	{"am", tr_de, mkargs_reg1},
 	{"bp", tr_bp},
 	{"br", tr_br},
+	{"da", tr_di},
 	{"de", tr_de, mkargs_reg1},
 	{"di", tr_di},
 	{"ds", tr_ds, mkargs_ds},
--- a/xroff.h
+++ b/xroff.h
@@ -34,6 +34,10 @@
 void str_rm(int id);
 void str_rn(int src, int dst);
 
+/* saving and restoring registers before and after printing diverted lines */
+void odiv_beg(void);
+void odiv_end(void);
+
 /* enviroments */
 void env_init(void);
 void env_free(void);