From cac41d59b64a8ee9e63c953529a2da5a8db17bc3 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Deuc=D0=B5?= <shurd@sasktel.net>
Date: Thu, 4 Mar 2021 14:48:34 -0500
Subject: [PATCH] Deal with the three classes of variables...

1) ` vars which are always expanded.
2) & vars which are expanded "sometimes" (generally for display stuff).
3) "pure" vars which are only expanded when they are the entire string.

The main change here is in the & var handling... lw() no longer
expands these, so they can be displayed to the user... they are now
expanded in the following cases:

1) Anything that calculates the displayed length.  The assumption
   here is that the string will be passed to one of the following
   things.
2) Bar updates, either @quebar or @saybar
3) @do addlog
4) @do write
5) @moremap
6) @progname
7) @show
8) @writefile
---
 xtrn/lord2/l2lib.js | 11 +++++++++--
 xtrn/lord2/lord2.js | 14 +++++++-------
 2 files changed, 16 insertions(+), 9 deletions(-)

diff --git a/xtrn/lord2/l2lib.js b/xtrn/lord2/l2lib.js
index ff65183683..d5e4ecd125 100644
--- a/xtrn/lord2/l2lib.js
+++ b/xtrn/lord2/l2lib.js
@@ -802,7 +802,7 @@ function broken_displen(str)
 {
 	var i;
 
-	str = expand_ticks(replace_vars(str));
+	str = expand_ticks(replace_vars(replace_svars(str)));
 	return str.length;
 }
 
@@ -1304,7 +1304,6 @@ function replace_vars(str)
 {
 	if (typeof str !== 'string')
 		return str;
-	str = str.replace(/([Ss]?&[A-Za-z]+)/g, function(m, r1) { return getvar(r1, true); });
 	str = str.replace(/(`[Vv][0-4][0-9])/g, function(m, r1) { return getvar(r1, true); });
 	str = str.replace(/(`[Ss][0-1][0-9])/g, function(m, r1) { return getvar(r1, true); });
 	str = str.replace(/(`[Pp][0-9][0-9])/g, function(m, r1) { return getvar(r1, true); });
@@ -1316,6 +1315,14 @@ function replace_vars(str)
 	return str;
 }
 
+function replace_svars(str)
+{
+	if (typeof str !== 'string')
+		return str;
+	str = str.replace(/([Ss]?&[A-Za-z]+)/g, function(m, r1) { return getvar(r1, true); });
+	return str;
+}
+
 function getoffset(x, y) {
 	return (x * 20 + y);
 }
diff --git a/xtrn/lord2/lord2.js b/xtrn/lord2/lord2.js
index 6c107a072a..45114f1f82 100644
--- a/xtrn/lord2/lord2.js
+++ b/xtrn/lord2/lord2.js
@@ -79,7 +79,7 @@ function redraw_bar(updstatus)
 
 function update_bar(str, msg, timeout)
 {
-	str = replace_vars(str);
+	str = replace_vars(replace_svars(str));
 	var dl = displen(str);
 	var l;
 
@@ -261,7 +261,7 @@ function run_ref(sec, fname)
 				return;
 			if (f.open('ab')) {
 				cl = files[fname].lines[line];
-				f.write(replace_vars(cl)+'\r\n');
+				f.write(replace_vars(replace_svars(cl))+'\r\n');
 				f.close();
 			}
 		},
@@ -548,7 +548,7 @@ function run_ref(sec, fname)
 			if (line > files[fname].lines.length)
 				return;
 			cl = files[fname].lines[line];
-			lw(replace_vars(cl));
+			lw(replace_vars(replace_svars(cl)));
 		},
 	};
 	var handlers = {
@@ -1242,7 +1242,7 @@ function run_ref(sec, fname)
 			if (line > files[fname].lines.length)
 				return;
 			cl = files[fname].lines[line];
-			morestr = replace_vars(cl);
+			morestr = replace_vars(replace_svars(cl));
 		},
 		'nocheck':function(args) {
 			// We don't really support this because there's no need for it.
@@ -1269,7 +1269,7 @@ function run_ref(sec, fname)
 			if (line > files[fname].lines.length)
 				return;
 			cl = files[fname].lines[line];
-			progname = replace_vars(cl);
+			progname = replace_vars(replace_svars(cl));
 		},
 		'rank':function(args) {
 			// TODO: No real clue what the filename is for...
@@ -1447,7 +1447,7 @@ rescan:
 
 			if (args.length === 0) {
 				l.forEach(function(l) {
-					lln(replace_vars(l));
+					lln(replace_vars(replace_svars(l)));
 				});
 			}
 			else if (args[0].toLowerCase() === 'scroll') {
@@ -1543,7 +1543,7 @@ rescan:
 			if (!f.open('ab'))
 				throw new Error('Unable to open '+f.name+' at '+fname+':'+line);
 			getlines().forEach(function(l) {
-				f.write(replace_vars(l)+'\r\n');
+				f.write(replace_vars(replace_svars(l))+'\r\n');
 			});
 			f.close();
 		},
-- 
GitLab