From 92b3588a465c9d37b5c183912b05d46120a88216 Mon Sep 17 00:00:00 2001
From: ezsh <ezsh.junk@gmail.com>
Date: Thu, 23 May 2019 19:52:04 +0200
Subject: [PATCH] Convert setLocalPronouns to a function

---
 src/js/assayJS.js                     |  4 ++
 src/js/utilJS.js                      | 29 ++++++++++
 src/pregmod/widgets/pregmodWidgets.tw | 79 +--------------------------
 3 files changed, 34 insertions(+), 78 deletions(-)

diff --git a/src/js/assayJS.js b/src/js/assayJS.js
index 12d66183801..7b07e11675e 100644
--- a/src/js/assayJS.js
+++ b/src/js/assayJS.js
@@ -391,6 +391,10 @@ App.Utils.Pronouns = class {
 	get Hers() { return this.Possessive; }
 	get Herself() { return this.ObjectReflexive; }
 	get Girl() { return this.Noun; }
+
+	get woman() { return this.noun === "girl" ? "woman" : "man"; }
+	get women() { return this.noun === "girl" ? "women" : "men"; }
+	get loli() { return this.noun === "girl" ? "loli" : "shota"; }
 };
 
 /**
diff --git a/src/js/utilJS.js b/src/js/utilJS.js
index 658f0f6f634..2511d27d988 100644
--- a/src/js/utilJS.js
+++ b/src/js/utilJS.js
@@ -2555,3 +2555,32 @@ App.Utils.setActiveSlaveByIndex = function(index) {
 App.Utils.slaveIndexForId = function(id) {
 	return State.variables.slaveIndices[id];
 };
+
+/**
+ * Sets temporary variables named by the scheme, dscribed below, to pronouns for the given slave
+ * @param {App.Entity.SlaveState} slave
+ * @param {any} [suffix] pronounsSuffix. Anything that can be converted to string.
+ * @param {string[]} [pronouns] requested pronouns. Defaults to all pronoun forms.
+ *
+ * The variables naming scheme is the pronoun name (he, his, etc.) and optional suffix. If the suffix is empty, the variables
+ * will be set as story variables, otherwise as temporary variables.
+ * This way for a call App.Utils.setLocalPronouns(slave) there will be story variables "$he", "$his", for
+ * App.Utils.setLocalPronouns(slave, 1): _he1, _his1 and so on.
+ */
+App.Utils.setLocalPronouns = function(slave, suffix, pronouns) {
+	const ps = getPronouns(slave);
+	/** @type {string} */
+	const pSuffix = suffix !== undefined ? suffix.toString() : '';
+	pronouns = pronouns || [ // Object.getOwnPropertyNames(ps) ?
+		'he', 'him', 'his', 'himself', 'boy',
+		'He', 'Him', 'His', 'Himself', 'Boy',
+		'she', 'her', 'hers', 'herself', 'girl',
+		'She', 'Her', 'Hers', 'Herself', 'Girl',
+		'woman', 'women', 'loli'
+	];
+
+	const scope = pSuffix.length === 0 ? State.variables : State.temporary;
+	pronouns.forEach(p => {
+		scope[p + pSuffix] = ps[p];
+	});
+}
diff --git a/src/pregmod/widgets/pregmodWidgets.tw b/src/pregmod/widgets/pregmodWidgets.tw
index f1229a97ba3..0c2db4e4db7 100644
--- a/src/pregmod/widgets/pregmodWidgets.tw
+++ b/src/pregmod/widgets/pregmodWidgets.tw
@@ -185,84 +185,7 @@
 
 	2. Her/hers vs his/his. Here, the opposite problem. In this one case and this alone, where you would use "hers," (the possessive pronoun) the variable is female, since the distinction is only important there. I am very sorry for English.
 */
-	/* TODO: set widget to autogen variable names based on args[1] number if possible */
-	<<if $args[1] == 4>>
-		<<set _he4 = $args[0].pronoun>>
-		<<set _his4 = $args[0].possessive>>
-		<<set _hers4 = $args[0].possessivePronoun>>
-		<<set _him4 = $args[0].object>>
-		<<set _himself4 = $args[0].objectReflexive>>
-		<<set _girl4 = $args[0].noun>>
-		<<if _girl4 == "girl">>
-			<<set _woman4 = "woman", _loli4 = "loli", _women4 = "women">>
-		<<else>>
-			<<set _woman4 = "man", _loli4 = "shota", _women4 = "men">>
-		<</if>>
-
-		<<set _He4 = capFirstChar(_he4)>>
-		<<set _His4 = capFirstChar(_his4)>>
-		<<set _Hers4 = capFirstChar(_hers4)>>
-		<<set _Him4 = capFirstChar(_him4)>>
-		<<set _Himself4 = capFirstChar(_himself4)>>
-		<<set _Girl4 = capFirstChar(_girl4)>>
-	<<elseif $args[1] == 3>>
-		<<set _he3 = $args[0].pronoun>>
-		<<set _his3 = $args[0].possessive>>
-		<<set _hers3 = $args[0].possessivePronoun>>
-		<<set _him3 = $args[0].object>>
-		<<set _himself3 = $args[0].objectReflexive>>
-		<<set _girl3 = $args[0].noun>>
-		<<if _girl3 == "girl">>
-			<<set _woman3 = "woman", _loli3 = "loli", _women3 = "women">>
-		<<else>>
-			<<set _woman3 = "man", _loli3 = "shota", _women3 = "men">>
-		<</if>>
-
-		<<set _He3 = capFirstChar(_he3)>>
-		<<set _His3 = capFirstChar(_his3)>>
-		<<set _Hers3 = capFirstChar(_hers3)>>
-		<<set _Him3 = capFirstChar(_him3)>>
-		<<set _Himself3 = capFirstChar(_himself3)>>
-		<<set _Girl3 = capFirstChar(_girl3)>>
-	<<elseif $args[1] == 2>>
-		<<set _he2 = $args[0].pronoun>>
-		<<set _his2 = $args[0].possessive>>
-		<<set _hers2 = $args[0].possessivePronoun>>
-		<<set _him2 = $args[0].object>>
-		<<set _himself2 = $args[0].objectReflexive>>
-		<<set _girl2 = $args[0].noun>>
-		<<if _girl2 == "girl">>
-			<<set _woman2 = "woman", _loli2 = "loli", _women2 = "women">>
-		<<else>>
-			<<set _woman2 = "man", _loli2 = "shota", _women2 = "men">>
-		<</if>>
-
-		<<set _He2 = capFirstChar(_he2)>>
-		<<set _His2 = capFirstChar(_his2)>>
-		<<set _Hers2 = capFirstChar(_hers2)>>
-		<<set _Him2 = capFirstChar(_him2)>>
-		<<set _Himself2 = capFirstChar(_himself2)>>
-		<<set _Girl2 = capFirstChar(_girl2)>>
-	<<else>>
-		<<set $he = $args[0].pronoun>>
-		<<set $his = $args[0].possessive>>
-		<<set $hers = $args[0].possessivePronoun>>
-		<<set $him = $args[0].object>>
-		<<set $himself = $args[0].objectReflexive>>
-		<<set $girl = $args[0].noun>>
-		<<if $girl == "girl">>
-			<<set $woman = "woman", $loli = "loli", $women = "women">>
-		<<else>>
-			<<set $woman = "man", $loli = "shota", $women = "men">>
-		<</if>>
-
-		<<set $He = capFirstChar($he)>>
-		<<set $His = capFirstChar($his)>>
-		<<set $Hers = capFirstChar($hers)>>
-		<<set $Him = capFirstChar($him)>>
-		<<set $Himself = capFirstChar($himself)>>
-		<<set $Girl = capFirstChar($girl)>>
-	<</if>>
+	<<run App.Utils.setLocalPronouns($args[0], $args[1])>>
 <</widget>>
 
 <<widget "setNonlocalPronouns">>
-- 
GitLab