Commit dbb4bcac authored by twanvl's avatar twanvl

Keyword parameters in the text are processed, placeholders are inserted

parent 6dfa6a4f
......@@ -807,6 +807,7 @@ keyword parameter type:
name: cost
#insert as: word
match: [XYZ0-9WUBRGS/]+|[^(.,\n]|([XYZ0-9WUBRGS/]+,)?[^(.,\n]*
script: "<sym-auto>{input}</sym-auto>" # TODO : DEBUG
keyword parameter type:
name: number
match: [XYZ0-9]+
......
......@@ -116,7 +116,7 @@ void KeywordExpansion::prepare(const vector<KeywordParamP>& param_types, bool fo
if (!force && matchRe.IsValid()) return;
parameters.clear();
// Prepare regex
// String regex;
String regex = _("(");
vector<KeywordParamP>::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<KeywordParamP>& 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<KeywordTrie*>& 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, _("<atom-reminder>"));
s = remove_tag_contents(s, _("<atom-keyword>")); // OLD, TODO: REMOVEME
s = remove_tag_contents(s, _("<atom-kwpph>"));
s = remove_tag(s, _("<keyword-param"));
String untagged = untag_no_escape(s);
......@@ -345,7 +346,6 @@ String KeywordDatabase::expand(const String& text,
map<Char,KeywordTrie*>::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 = _("<atom-kwpph>") + kw->parameters[j/2-1]->name + _("</atom-kwpph>");
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 += _("<kw-"); result += expand_type; result += _(">");
result += combine_script->eval(ctx)->toString();
result += _("</kw-"); result += expand_type; result += _(">");
} else {
result += _("<kw-"); result += expand_type; result += _(">");
result += s.substr(start, end - start);
result += total;
result += _("</kw-"); result += expand_type; result += _(">");
}
......
......@@ -50,11 +50,15 @@ class KeywordExpansion {
public:
String match; ///< String to match, <param> tags are used for parameters
vector<KeywordParamP> 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
......
......@@ -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());
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment