shithub: neatroff

Download patch

ref: bb112510aa18ab3e4e703c34d0d010f2e1ee9bf7
parent: e178446fb18669de84e5ead2590cb53ad9efff6f
author: Ali Gholami Rudi <ali@rudi.ir>
date: Sat Aug 9 19:56:36 EDT 2014

cp: never pass on \{ and \}

--- a/cp.c
+++ b/cp.c
@@ -6,7 +6,8 @@
 
 static int cp_blkdep;		/* input block depth (text in \{ and \}) */
 static int cp_cpmode;		/* disable the interpretation \w and \E */
-static int cp_preblk;		/* prefix \{ with this character until an EOL */
+static int cp_reqln;		/* a request line; replace \{ with an space */
+static int cp_reqdep;		/* the block depth of current request line */
 
 static void cparg(char *d, int len)
 {
@@ -168,21 +169,19 @@
 			in_back('');
 			return c_ni;
 		}
-		if (c == '}' && cp_blkdep > 0)
+		if (c == '}' && !cp_cpmode) {
 			cp_blkdep--;
-		if (c == '{') {
+			return cp_raw();
+		}
+		if (c == '{' && !cp_cpmode) {
 			cp_blkdep++;
-			if (cp_preblk > 0) {
-				in_back(c);
-				in_back(c_ec);
-				return cp_preblk;
-			}
+			return cp_reqln ? ' ' : cp_raw();
 		}
 		in_back(c);
 		return c_ec;
 	}
 	if (c == '\n')
-		cp_preblk = 0;
+		cp_reqln = 0;
 	return c;
 }
 
@@ -230,18 +229,16 @@
 
 void cp_blk(int skip)
 {
-	int c;
-	do {
-		c = skip ? cp_raw() : cp_next();
-	} while (c == ' ' || c == '\t');
 	if (skip) {
-		int dep = c == c_ec && in_top() == '{' ? cp_blkdep - 1 : cp_blkdep;
-		while (c >= 0 && (c != '\n' || cp_blkdep > dep))
+		int c = cp_raw();
+		while (c >= 0 && (c != '\n' || cp_blkdep > cp_reqdep))
 			c = cp_raw();
 	} else {
-		if (c == c_ec && in_top() == '{')	/* a troff \{ \} block */
-			cp_raw();
-		else
+		int c = cp_next();
+		while ((c == ' ' || c == '\t') && cp_blkdep <= cp_reqdep)
+			c = cp_next();
+		/* push back if the space is not inserted because of cp_reqln */
+		if (c != ' ' && c != '\t')
 			cp_back(c);
 	}
 }
@@ -251,8 +248,9 @@
 	cp_cpmode = mode;
 }
 
-/* prefix \{ with c until an EOL; the main reason is handling .ie\{ */
-void cp_prefixblock(int c)
+/* beginning of a request line; replace \{ with a space until an EOL */
+void cp_reqline(void)
 {
-	cp_preblk = c;
+	cp_reqln = 1;
+	cp_reqdep = cp_blkdep;
 }
--- a/ren.c
+++ b/ren.c
@@ -688,9 +688,6 @@
 	case ',':
 		wb_italiccorrectionleft(wb);
 		break;
-	case '{':
-	case '}':
-		break;
 	}
 }
 
--- a/roff.h
+++ b/roff.h
@@ -208,7 +208,7 @@
 int in_lnum(void);		/* current line number */
 
 void cp_blk(int skip);		/* skip or read the next line or block */
-void cp_prefixblock(int c);	/* prefix \{ with c until an EOL */
+void cp_reqline(void);		/* beginning of a request line */
 void cp_copymode(int mode);	/* do not interpret \w and \E */
 #define cp_back		in_back	/* cp.c is stateless */
 int tr_nextreq(void);		/* read the next troff request */
--- a/tr.c
+++ b/tr.c
@@ -235,8 +235,7 @@
 	struct sbuf sbuf;
 	int ret;
 	sbuf_init(&sbuf);
-	if (!read_until(&sbuf, NULL, next, back))
-		back(' ');
+	read_until(&sbuf, NULL, next, back);
 	ret = eval(sbuf_buf(&sbuf), '\0') > 0;
 	sbuf_done(&sbuf);
 	return ret;
@@ -966,7 +965,7 @@
 	args[0] = cmd;
 	cmd[0] = c;
 	req = NULL;
-	cp_prefixblock(' ');
+	cp_reqline();
 	read_regname(cmd + 1);
 	sbuf_init(&sbuf);
 	req = str_dget(map(cmd + 1));