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: ...@@ -807,6 +807,7 @@ keyword parameter type:
name: cost name: cost
#insert as: word #insert as: word
match: [XYZ0-9WUBRGS/]+|[^(.,\n]|([XYZ0-9WUBRGS/]+,)?[^(.,\n]* match: [XYZ0-9WUBRGS/]+|[^(.,\n]|([XYZ0-9WUBRGS/]+,)?[^(.,\n]*
script: "<sym-auto>{input}</sym-auto>" # TODO : DEBUG
keyword parameter type: keyword parameter type:
name: number name: number
match: [XYZ0-9]+ match: [XYZ0-9]+
......
...@@ -116,7 +116,7 @@ void KeywordExpansion::prepare(const vector<KeywordParamP>& param_types, bool fo ...@@ -116,7 +116,7 @@ void KeywordExpansion::prepare(const vector<KeywordParamP>& param_types, bool fo
if (!force && matchRe.IsValid()) return; if (!force && matchRe.IsValid()) return;
parameters.clear(); parameters.clear();
// Prepare regex // Prepare regex
// String regex; String regex = _("(");
vector<KeywordParamP>::const_iterator param = parameters.begin(); vector<KeywordParamP>::const_iterator param = parameters.begin();
// Parse the 'match' string // Parse the 'match' string
for (size_t i = 0 ; i < match.size() ;) { for (size_t i = 0 ; i < match.size() ;) {
...@@ -126,24 +126,26 @@ void KeywordExpansion::prepare(const vector<KeywordParamP>& param_types, bool fo ...@@ -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); size_t start = skip_tag(match, i), end = match_close_tag(match, i);
String type = match.substr(start, end-start); String type = match.substr(start, end-start);
// find parameter type 'type' // find parameter type 'type'
KeywordParam* p = nullptr; KeywordParamP p;
FOR_EACH_CONST(pt, param_types) { FOR_EACH_CONST(pt, param_types) {
if (pt->name == type) { if (pt->name == type) {
p = pt.get(); p = pt;
break; break;
} }
} }
if (!p) { if (!p) {
throw InternalError(_("Unknown keyword parameter type: ") + type); throw InternalError(_("Unknown keyword parameter type: ") + type);
} }
parameters.push_back(p);
// modify regex // modify regex
regex += _("(") + make_non_capturing(p->match) + _(")"); regex += _(")(") + make_non_capturing(p->match) + _(")?(");
i = skip_tag(match, end); i = skip_tag(match, end);
} else { } else {
regex += regex_escape(c); regex += regex_escape(c);
i++; i++;
} }
} }
regex += _(")");
if (!matchRe.Compile(regex, wxRE_ADVANCED)) { if (!matchRe.Compile(regex, wxRE_ADVANCED)) {
throw InternalError(_("Error creating match regex")); throw InternalError(_("Error creating match regex"));
} }
...@@ -275,7 +277,7 @@ void closure(vector<KeywordTrie*>& state) { ...@@ -275,7 +277,7 @@ void closure(vector<KeywordTrie*>& state) {
} }
} }
//DEBUG #ifdef _DEBUG
void dump(int i, KeywordTrie* t) { void dump(int i, KeywordTrie* t) {
FOR_EACH(c, t->children) { FOR_EACH(c, t->children) {
wxLogDebug(String(i,_(' ')) + c.first + _(" ") + String::Format(_("%p"),c.second)); wxLogDebug(String(i,_(' ')) + c.first + _(" ") + String::Format(_("%p"),c.second));
...@@ -286,19 +288,18 @@ void dump(int i, KeywordTrie* t) { ...@@ -286,19 +288,18 @@ void dump(int i, KeywordTrie* t) {
if (t->on_any_star != t) dump(i+2, t->on_any_star); if (t->on_any_star != t) dump(i+2, t->on_any_star);
} }
} }
#endif
String KeywordDatabase::expand(const String& text, String KeywordDatabase::expand(const String& text,
const ScriptValueP& expand_default, const ScriptValueP& expand_default,
const ScriptValueP& combine_script, const ScriptValueP& combine_script,
Context& ctx) const { Context& ctx) const {
// DEBUG: dump db
//dump(0, root);
assert(combine_script); assert(combine_script);
// Remove all old reminder texts // Remove all old reminder texts
String s = remove_tag_contents(text, _("<atom-reminder>")); String s = remove_tag_contents(text, _("<atom-reminder>"));
s = remove_tag_contents(s, _("<atom-keyword>")); // OLD, TODO: REMOVEME s = remove_tag_contents(s, _("<atom-keyword>")); // OLD, TODO: REMOVEME
s = remove_tag_contents(s, _("<atom-kwpph>"));
s = remove_tag(s, _("<keyword-param")); s = remove_tag(s, _("<keyword-param"));
String untagged = untag_no_escape(s); String untagged = untag_no_escape(s);
...@@ -345,7 +346,6 @@ String KeywordDatabase::expand(const String& text, ...@@ -345,7 +346,6 @@ String KeywordDatabase::expand(const String& text,
map<Char,KeywordTrie*>::const_iterator it = kt->children.find(c); map<Char,KeywordTrie*>::const_iterator it = kt->children.find(c);
if (it != kt->children.end()) { if (it != kt->children.end()) {
next.push_back(it->second); next.push_back(it->second);
wxLogDebug(c + String(_(" -> ")) + String::Format(_("%p"), it->second));
} }
} }
// next becomes current // next becomes current
...@@ -369,17 +369,35 @@ String KeywordDatabase::expand(const String& text, ...@@ -369,17 +369,35 @@ String KeywordDatabase::expand(const String& text,
end = untagged_to_index(s, start_u + len_u, true); end = untagged_to_index(s, start_u + len_u, true);
result += s.substr(0, start); 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(); 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) { for (size_t j = 1 ; j < match_count ; ++j) {
// j = odd -> text
// j = even -> parameter #(j/2)
size_t start_u, len_u; size_t start_u, len_u;
kw->matchRe.GetMatch(&start_u, &len_u, j); kw->matchRe.GetMatch(&start_u, &len_u, j);
String param = untagged.substr(start_u, len_u); size_t part_end = untagged_to_index(s, start_u + len_u, true);
if (j-1 < kw->parameters.size() && kw->parameters[j-1]->script) { String part = s.substr(start, part_end - start);
// apply parameter script if ((j % 2) == 0) {
param = kw->parameters[j-1]->script.invoke(ctx)->toString(); // 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)); ctx.setVariable(_("mode"), toScript(kw->mode));
...@@ -394,14 +412,14 @@ String KeywordDatabase::expand(const String& text, ...@@ -394,14 +412,14 @@ String KeywordDatabase::expand(const String& text,
// Combine keyword & reminder with result // Combine keyword & reminder with result
if (expand) { if (expand) {
String reminder = kw->reminder.invoke(ctx)->toString(); 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)); ctx.setVariable(_("reminder"), toScript(reminder));
result += _("<kw-"); result += expand_type; result += _(">"); result += _("<kw-"); result += expand_type; result += _(">");
result += combine_script->eval(ctx)->toString(); result += combine_script->eval(ctx)->toString();
result += _("</kw-"); result += expand_type; result += _(">"); result += _("</kw-"); result += expand_type; result += _(">");
} else { } else {
result += _("<kw-"); result += expand_type; result += _(">"); result += _("<kw-"); result += expand_type; result += _(">");
result += s.substr(start, end - start); result += total;
result += _("</kw-"); result += expand_type; result += _(">"); result += _("</kw-"); result += expand_type; result += _(">");
} }
......
...@@ -50,11 +50,15 @@ class KeywordExpansion { ...@@ -50,11 +50,15 @@ class KeywordExpansion {
public: public:
String match; ///< String to match, <param> tags are used for parameters String match; ///< String to match, <param> tags are used for parameters
vector<KeywordParamP> parameters; ///< The types of 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 StringScript reminder; ///< Reminder text of the keyword
String mode; ///< Mode of use, can be used by scripts (only gives the name) String mode; ///< Mode of use, can be used by scripts (only gives the name)
// . Default is the mode of the Keyword. /// Regular expression to match and split parameters, automatically generated.
String regex;//TODO REMOVE /** 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. /// Prepare the expansion: (re)generate matchRe and the list of parameters.
/** Throws when there is an error in the input /** Throws when there is an error in the input
......
...@@ -35,10 +35,11 @@ ValueViewerP DataEditor::makeViewer(const StyleP& style) { ...@@ -35,10 +35,11 @@ ValueViewerP DataEditor::makeViewer(const StyleP& style) {
// ----------------------------------------------------------------------------- : Utility for ValueViewers // ----------------------------------------------------------------------------- : Utility for ValueViewers
bool DataEditor::drawBorders() const { 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 { bool DataEditor::drawEditing() const {
return true; return FindFocus() == this;
} }
wxPen DataEditor::borderPen(bool active) const { wxPen DataEditor::borderPen(bool active) const {
...@@ -47,7 +48,7 @@ wxPen DataEditor::borderPen(bool active) const { ...@@ -47,7 +48,7 @@ wxPen DataEditor::borderPen(bool active) const {
} }
ValueViewer* DataEditor::focusedViewer() const { ValueViewer* DataEditor::focusedViewer() const {
return current_viewer; return FindFocus() == this ? current_viewer : nullptr;
} }
// ----------------------------------------------------------------------------- : Selection // ----------------------------------------------------------------------------- : Selection
...@@ -276,7 +277,7 @@ void DataEditor::onContextMenu(wxContextMenuEvent& ev) { ...@@ -276,7 +277,7 @@ void DataEditor::onContextMenu(wxContextMenuEvent& ev) {
if (current_editor) { if (current_editor) {
IconMenu m; IconMenu m;
m.Append(wxID_CUT, _("cut"), _("Cu&t"), _("Move the selected text to the clipboard")); 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.Append(wxID_PASTE, _("paste"), _("&Paste"), _("Inserts the text from the clipboard"));
m.Enable(wxID_CUT, canCut()); m.Enable(wxID_CUT, canCut());
m.Enable(wxID_COPY, canCopy()); 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