From c52dfe4f1d5b12001ca66a02a19774c2b358212c Mon Sep 17 00:00:00 2001 From: twanvl Date: Thu, 5 Jul 2007 14:44:24 +0000 Subject: [PATCH] Fixed font size of code in keywords tab; Added 'recursive' flag to replace_rule function git-svn-id: svn://svn.code.sf.net/p/magicseteditor/code/trunk@487 0fc631ac-6414-0410-93d0-97cfa31319b6 --- src/data/font.cpp | 7 +++- src/script/functions/basic.cpp | 76 +++++++++++++++++++--------------- 2 files changed, 48 insertions(+), 35 deletions(-) diff --git a/src/data/font.cpp b/src/data/font.cpp index 2dc9f09d..762fd454 100644 --- a/src/data/font.cpp +++ b/src/data/font.cpp @@ -76,8 +76,11 @@ wxFont Font::toWxFont(double scale) const { // make font wxFont font; if (flags & FONT_CODE) { - if (size_i < 2) size_i = wxNORMAL_FONT->GetPointSize(); - font = wxFont(size_i, wxFONTFAMILY_TELETYPE, wxFONTSTYLE_NORMAL, weight_i, underline(), _("Courier New")); + if (size_i < 2) { + return wxFont(wxNORMAL_FONT->GetPointSize(), wxFONTFAMILY_TELETYPE, wxFONTSTYLE_NORMAL, weight_i, underline(), _("Courier New")); + } else { + font = wxFont(size_i, wxFONTFAMILY_TELETYPE, wxFONTSTYLE_NORMAL, weight_i, underline(), _("Courier New")); + } } else if (name().empty()) { font = *wxNORMAL_FONT; font.SetPointSize(size > 1 ? size_i : scale * font.GetPointSize()); diff --git a/src/script/functions/basic.cpp b/src/script/functions/basic.cpp index 061bbbd7..762a6e4e 100644 --- a/src/script/functions/basic.cpp +++ b/src/script/functions/basic.cpp @@ -303,50 +303,58 @@ class ScriptReplaceRule : public ScriptValue { virtual String typeName() const { return _("replace_rule"); } virtual ScriptValueP eval(Context& ctx) const { SCRIPT_PARAM(String, input); - if (context.IsValid() || replacement_function) { - // match first, then check context of match - String ret; - while (regex.Matches(input)) { - // for each match ... - size_t start, len; - bool ok = regex.GetMatch(&start, &len, 0); - assert(ok); - ret += input.substr(0, start); // everything before the match position stays - String inside = input.substr(start, len); // inside the match - String next_input = input.substr(start + len); // next loop the input is after this match - String after_replace = ret + _("") + next_input; // after replacing, the resulting context would be - if (!context.IsValid() || context.Matches(after_replace)) { - // the context matches -> perform replacement - if (replacement_function) { - // set match results in context - for (UInt m = 0 ; m < regex.GetMatchCount() ; ++m) { - regex.GetMatch(&start, &len, m); - String name = m == 0 ? _("input") : String(_("_")) << m; - String value = input.substr(start, len); - ctx.setVariable(name, to_script(value)); - } - // call - inside = replacement_function->eval(ctx)->toString(); - } else { - regex.Replace(&inside, replacement, 1); // replace inside - } - } - ret += inside; - input = next_input; - } - ret += input; - SCRIPT_RETURN(ret); + if (context.IsValid() || replacement_function || recursive) { + SCRIPT_RETURN(apply(ctx, input)); } else { // dumb replacing regex.Replace(&input, replacement); SCRIPT_RETURN(input); } } + String apply(Context& ctx, String& input, int level = 0) const { + // match first, then check context of match + String ret; + while (regex.Matches(input)) { + // for each match ... + size_t start, len; + bool ok = regex.GetMatch(&start, &len, 0); + assert(ok); + ret += input.substr(0, start); // everything before the match position stays + String inside = input.substr(start, len); // inside the match + String next_input = input.substr(start + len); // next loop the input is after this match + String after_replace = ret + _("") + next_input; // after replacing, the resulting context would be + if (!context.IsValid() || context.Matches(after_replace)) { + // the context matches -> perform replacement + if (replacement_function) { + // set match results in context + for (UInt m = 0 ; m < regex.GetMatchCount() ; ++m) { + regex.GetMatch(&start, &len, m); + String name = m == 0 ? _("input") : String(_("_")) << m; + String value = input.substr(start, len); + ctx.setVariable(name, to_script(value)); + } + // call + inside = replacement_function->eval(ctx)->toString(); + } else { + regex.Replace(&inside, replacement, 1); // replace inside + } + } + if (recursive && level < 20) { + ret += apply(ctx, inside, level + 1); + } else { + ret += inside; + } + input = next_input; + } + ret += input; + return ret; + } wxRegEx regex; ///< Regex to match wxRegEx context; ///< Match only in a given context, optional String replacement; ///< Replacement ScriptValueP replacement_function; ///< Replacement function instead of a simple string, optional + bool recursive; ///< Recurse into the replacement }; // Create a regular expression rule for replacing in strings @@ -370,6 +378,8 @@ ScriptValueP replace_rule(Context& ctx) { throw ScriptError(_("Error while compiling regular expression: '")+in_context+_("'")); } } + SCRIPT_OPTIONAL_PARAM_(bool, recursive); + ret->recursive = recursive; return ret; }