From 074ff128164bbdc4d5255a79dd4396eb1d437105 Mon Sep 17 00:00:00 2001 From: twanvl Date: Sun, 18 Mar 2007 01:05:43 +0000 Subject: [PATCH] Keyword parameters in the text are processed, placeholders are inserted git-svn-id: svn://svn.code.sf.net/p/magicseteditor/code/trunk@211 0fc631ac-6414-0410-93d0-97cfa31319b6 --- data/magic.mse-game/game | 1 + src/data/keyword.cpp | 52 ++++++++++++++++++++++----------- src/data/keyword.hpp | 10 +++++-- src/gui/control/card_editor.cpp | 9 +++--- 4 files changed, 48 insertions(+), 24 deletions(-) diff --git a/data/magic.mse-game/game b/data/magic.mse-game/game index 66dfed6a..2b03e5d0 100644 --- a/data/magic.mse-game/game +++ b/data/magic.mse-game/game @@ -807,6 +807,7 @@ keyword parameter type: name: cost #insert as: word match: [XYZ0-9WUBRGS/]+|[^(.,\n]|([XYZ0-9WUBRGS/]+,)?[^(.,\n]* + script: "{input}" # TODO : DEBUG keyword parameter type: name: number match: [XYZ0-9]+ diff --git a/src/data/keyword.cpp b/src/data/keyword.cpp index 5c0f5ea7..7825f822 100644 --- a/src/data/keyword.cpp +++ b/src/data/keyword.cpp @@ -116,7 +116,7 @@ void KeywordExpansion::prepare(const vector& param_types, bool fo if (!force && matchRe.IsValid()) return; parameters.clear(); // Prepare regex -// String regex; + String regex = _("("); vector::const_iterator param = parameters.begin(); // Parse the 'match' string for (size_t i = 0 ; i < match.size() ;) { @@ -126,24 +126,26 @@ void KeywordExpansion::prepare(const vector& param_types, bool fo size_t start = skip_tag(match, i), end = match_close_tag(match, i); String type = match.substr(start, end-start); // find parameter type 'type' - KeywordParam* p = nullptr; + KeywordParamP p; FOR_EACH_CONST(pt, param_types) { if (pt->name == type) { - p = pt.get(); + p = pt; break; } } if (!p) { throw InternalError(_("Unknown keyword parameter type: ") + type); } + parameters.push_back(p); // modify regex - regex += _("(") + make_non_capturing(p->match) + _(")"); + regex += _(")(") + make_non_capturing(p->match) + _(")?("); i = skip_tag(match, end); } else { regex += regex_escape(c); i++; } } + regex += _(")"); if (!matchRe.Compile(regex, wxRE_ADVANCED)) { throw InternalError(_("Error creating match regex")); } @@ -275,7 +277,7 @@ void closure(vector& state) { } } -//DEBUG +#ifdef _DEBUG void dump(int i, KeywordTrie* t) { FOR_EACH(c, t->children) { wxLogDebug(String(i,_(' ')) + c.first + _(" ") + String::Format(_("%p"),c.second)); @@ -286,19 +288,18 @@ void dump(int i, KeywordTrie* t) { if (t->on_any_star != t) dump(i+2, t->on_any_star); } } +#endif String KeywordDatabase::expand(const String& text, const ScriptValueP& expand_default, const ScriptValueP& combine_script, Context& ctx) const { - // DEBUG: dump db - //dump(0, root); - assert(combine_script); // Remove all old reminder texts String s = remove_tag_contents(text, _("")); s = remove_tag_contents(s, _("")); // OLD, TODO: REMOVEME + s = remove_tag_contents(s, _("")); s = remove_tag(s, _("::const_iterator it = kt->children.find(c); if (it != kt->children.end()) { next.push_back(it->second); - wxLogDebug(c + String(_(" -> ")) + String::Format(_("%p"), it->second)); } } // next becomes current @@ -369,17 +369,35 @@ String KeywordDatabase::expand(const String& text, end = untagged_to_index(s, start_u + len_u, true); result += s.substr(0, start); - // Set parameters in context + // Split the keyword, set parameters in context + String total; // the total keyword size_t match_count = kw->matchRe.GetMatchCount(); + assert(match_count - 1 == 1 + 2 * kw->parameters.size()); for (size_t j = 1 ; j < match_count ; ++j) { + // j = odd -> text + // j = even -> parameter #(j/2) size_t start_u, len_u; kw->matchRe.GetMatch(&start_u, &len_u, j); - String param = untagged.substr(start_u, len_u); - if (j-1 < kw->parameters.size() && kw->parameters[j-1]->script) { - // apply parameter script - param = kw->parameters[j-1]->script.invoke(ctx)->toString(); + size_t part_end = untagged_to_index(s, start_u + len_u, true); + String part = s.substr(start, part_end - start); + if ((j % 2) == 0) { + // parameter + String param = untagged.substr(start_u, len_u); // untagged version + if (param.empty()) { + // placeholder + param = _("") + kw->parameters[j/2-1]->name + _(""); + part = part + param; // keep tags + } else if (kw->parameters[j/2-1]->script) { + // apply parameter script + ctx.setVariable(_("input"), toScript(part)); + part = kw->parameters[j/2-1]->script.invoke(ctx)->toString(); + ctx.setVariable(_("input"), toScript(part)); + param = kw->parameters[j/2-1]->script.invoke(ctx)->toString(); + } + ctx.setVariable(String(_("param")) << (int)(j/2), toScript(param)); } - ctx.setVariable(String(_("param")) << (int)j, toScript(param)); + total += part; + start = part_end; } ctx.setVariable(_("mode"), toScript(kw->mode)); @@ -394,14 +412,14 @@ String KeywordDatabase::expand(const String& text, // Combine keyword & reminder with result if (expand) { String reminder = kw->reminder.invoke(ctx)->toString(); - ctx.setVariable(_("keyword"), toScript(s.substr(start, end - start))); + ctx.setVariable(_("keyword"), toScript(total)); ctx.setVariable(_("reminder"), toScript(reminder)); result += _(""); result += combine_script->eval(ctx)->toString(); result += _(""); } else { result += _(""); - result += s.substr(start, end - start); + result += total; result += _(""); } diff --git a/src/data/keyword.hpp b/src/data/keyword.hpp index aa5fd187..5ca9f95d 100644 --- a/src/data/keyword.hpp +++ b/src/data/keyword.hpp @@ -50,11 +50,15 @@ class KeywordExpansion { public: String match; ///< String to match, tags are used for parameters vector parameters; ///< The types of parameters - wxRegEx matchRe; ///< Regular expression to match and split parameters, automatically generated StringScript reminder; ///< Reminder text of the keyword String mode; ///< Mode of use, can be used by scripts (only gives the name) -// . Default is the mode of the Keyword. - String regex;//TODO REMOVE + /// Regular expression to match and split parameters, automatically generated. + /** The regex has exactly 2 * parameters.size() + 1 captures (excluding the entire match, caputure 0), + * captures 1,3,... capture the plain text of the match string + * captures 2,4,... capture the parameters + */ + wxRegEx matchRe; +//% . Default is the mode of the Keyword. /// Prepare the expansion: (re)generate matchRe and the list of parameters. /** Throws when there is an error in the input diff --git a/src/gui/control/card_editor.cpp b/src/gui/control/card_editor.cpp index 27ff0b6c..4e0f871e 100644 --- a/src/gui/control/card_editor.cpp +++ b/src/gui/control/card_editor.cpp @@ -35,10 +35,11 @@ ValueViewerP DataEditor::makeViewer(const StyleP& style) { // ----------------------------------------------------------------------------- : Utility for ValueViewers bool DataEditor::drawBorders() const { - return !nativeLook() && settings.stylesheetSettingsFor(*set->stylesheetFor(card)).card_borders(); + return !nativeLook() && + settings.stylesheetSettingsFor(*set->stylesheetFor(card)).card_borders(); } bool DataEditor::drawEditing() const { - return true; + return FindFocus() == this; } wxPen DataEditor::borderPen(bool active) const { @@ -47,7 +48,7 @@ wxPen DataEditor::borderPen(bool active) const { } ValueViewer* DataEditor::focusedViewer() const { - return current_viewer; + return FindFocus() == this ? current_viewer : nullptr; } // ----------------------------------------------------------------------------- : Selection @@ -276,7 +277,7 @@ void DataEditor::onContextMenu(wxContextMenuEvent& ev) { if (current_editor) { IconMenu m; m.Append(wxID_CUT, _("cut"), _("Cu&t"), _("Move the selected text to the clipboard")); - m.Append(wxID_COPY, _("copy"), _("&Copy"), _("Place the selected text on the clipboard")); + m.Append(wxID_COPY, _("copy"), _("&Copy"), _("Place the selected text on the clipboard")); m.Append(wxID_PASTE, _("paste"), _("&Paste"), _("Inserts the text from the clipboard")); m.Enable(wxID_CUT, canCut()); m.Enable(wxID_COPY, canCopy());