From 2d477c5e0a2a402ec77ed8447bb93806daa3746a Mon Sep 17 00:00:00 2001 From: Kornel Benko Date: Sun, 28 Oct 2018 19:40:14 +0100 Subject: [PATCH] Changes to match math equations Now tests findadv-01 ... findadv-20 pass too. keytest.py: Expanded time for controll keys (like \[Return]) findadv*: expanded time for normal keys lyxfind.cpp: Handle math equations --- development/autotests/findadv-01-in.txt | 8 ++- development/autotests/findadv-02-in.txt | 9 ++- development/autotests/findadv-05-in.txt | 1 + development/autotests/findadv-06-in.txt | 1 + development/autotests/findadv-07-in.txt | 3 + development/autotests/findadv-08-in.txt | 1 + development/autotests/findadv-11-in.txt | 1 + development/autotests/findadv-14-in.txt | 1 + development/autotests/findadv-19-in.txt | 1 + development/autotests/keytest.py | 2 +- src/lyxfind.cpp | 83 ++++++++++++++++++++----- src/lyxfind.h | 4 ++ 12 files changed, 98 insertions(+), 17 deletions(-) diff --git a/development/autotests/findadv-01-in.txt b/development/autotests/findadv-01-in.txt index 63933d5a7c..c480e82053 100644 --- a/development/autotests/findadv-01-in.txt +++ b/development/autotests/findadv-01-in.txt @@ -3,11 +3,17 @@ # Lang it_IT.utf8 CO: findadv-01.ctrl +KD: 100 TestBegin test.lyx -dbg key,find > findadv-01.loga.txt 2>&1 KK: aaa\[Home] KK: \CF +Sleep 1 KK: a\[Tab] -KK: aba\[Return]\[Return]\[Return] +Sleep 1 +KK: aba +KK: \[Return] +Sleep 1 +KK: \[Return]\[Return] KK: \Cs KK: \Axbuffer-export latex\[Return] CR: ^abaabaa$ diff --git a/development/autotests/findadv-02-in.txt b/development/autotests/findadv-02-in.txt index 8fec5b9f2e..5b3bdb521b 100644 --- a/development/autotests/findadv-02-in.txt +++ b/development/autotests/findadv-02-in.txt @@ -3,13 +3,20 @@ Lang it_IT.utf8 CO: findadv-02.ctrl TestBegin test.lyx -dbg key,find > findadv-02.loga.txt 2>&1 +KD: 50 KK: \Cm KK: x^(a) +\\frac 1+x^(a) \[Down]1-x^(a) \C\[Home] KK: \CF KK: \Cmx^(a) \[Tab] +Sleep 1 KK: \Cmx_a -KK: \[Return]\[Return]\[Return] +KK: \[Return] +Sleep 1 +KK: \[Return] +Sleep 1 +KK: \[Return] KK: \Cs +KD: 10 KK: \Axbuffer-export latex\[Return] CR: ^\$x_\{a\}\+\\frac\{1\+x_\{a\}\}\{1-x\^\{\(a\)\}\}\$$ CC: diff --git a/development/autotests/findadv-05-in.txt b/development/autotests/findadv-05-in.txt index a3ea5e3257..0fcf149af5 100644 --- a/development/autotests/findadv-05-in.txt +++ b/development/autotests/findadv-05-in.txt @@ -3,6 +3,7 @@ Lang sk_SK.utf8 CO: findadv-05.ctrl TestBegin test.lyx -dbg key,find > findadv-05.loga.txt 2>&1 +KD: 50 CN: Part 1 KK: foo \Cefoo foo\Ce foo\C\[Home] KK: \Cs diff --git a/development/autotests/findadv-06-in.txt b/development/autotests/findadv-06-in.txt index f3c16fbfca..3fb38b61f6 100644 --- a/development/autotests/findadv-06-in.txt +++ b/development/autotests/findadv-06-in.txt @@ -3,6 +3,7 @@ Lang sk_SK.utf8 CO: findadv-06.ctrl TestBegin test.lyx -dbg key,find > findadv-06.loga.txt 2>&1 +KD: 50 CN: Part 1 KK: foo\[Return] KK: \Ao2 diff --git a/development/autotests/findadv-07-in.txt b/development/autotests/findadv-07-in.txt index 3d60933f57..37c5b7623a 100644 --- a/development/autotests/findadv-07-in.txt +++ b/development/autotests/findadv-07-in.txt @@ -3,6 +3,7 @@ Lang sk_SK.utf8 CO: findadv-07.ctrl TestBegin test.lyx -dbg key,find > findadv-07.loga.txt 2>&1 +KD: 100 CN: Part 1 KK: foo bar\[Return] #select itemized @@ -15,6 +16,7 @@ KK: \Axdialog-show findreplaceadv\[Return] # Uncheck ignore format KK: \At\Ai\Ah KK: \Aob +Sleep 1 KK: foo\[Return] CP: Putting selection at .*idx: 0 par: 1 pos: 0\n with len: 3 # @@ -26,6 +28,7 @@ KK: \[Delete] # Uncheck ignore format #KK: \At\Ai\Ah KK: \Aob +Sleep 1 KK: bar\[Return] CP: Putting selection at .*idx: 0 par: 1 pos: 4\n with len: 3 TestEnd diff --git a/development/autotests/findadv-08-in.txt b/development/autotests/findadv-08-in.txt index 62e21039dc..0dc93059c4 100644 --- a/development/autotests/findadv-08-in.txt +++ b/development/autotests/findadv-08-in.txt @@ -6,6 +6,7 @@ PrepareShortcuts Lang sk_SK.utf8 CO: findadv-08.ctrl TestBegin test.lyx -dbg key,find > findadv-08.loga.txt 2>&1 +KD: 100 CN: Part 1 Cr: ^.*Putting CP: Key (queried) [action=buffer-begin][Ctrl+Home] diff --git a/development/autotests/findadv-11-in.txt b/development/autotests/findadv-11-in.txt index 3832475f9c..a13d81c8ac 100644 --- a/development/autotests/findadv-11-in.txt +++ b/development/autotests/findadv-11-in.txt @@ -3,6 +3,7 @@ Lang it_IT.utf8 CO: findadv-11.ctrl TestBegin test.lyx -dbg key,find > findadv-11.loga.txt 2>&1 +KD: 200 CN: Part 1 KK: \Cm KK: x_v \C\[Home] diff --git a/development/autotests/findadv-14-in.txt b/development/autotests/findadv-14-in.txt index fb8fd4cadf..022238a434 100644 --- a/development/autotests/findadv-14-in.txt +++ b/development/autotests/findadv-14-in.txt @@ -4,6 +4,7 @@ Lang sk_SK.utf8 CO: findadv-14.ctrl TestBegin test.lyx -dbg key,find > findadv-14.loga.txt 2>&1 +KD: 100 CN: Part 1 KK: \Amd KK: x^2 +x^2\C\[Home] diff --git a/development/autotests/findadv-19-in.txt b/development/autotests/findadv-19-in.txt index 5a49b3f8bf..a4b29926b8 100644 --- a/development/autotests/findadv-19-in.txt +++ b/development/autotests/findadv-19-in.txt @@ -3,6 +3,7 @@ Lang it_IT.utf8 CO: findadv-19.ctrl TestBegin test.lyx -dbg key,find > findadv-19.loga.txt 2>&1 +KD: 50 KK: Hello world world!\C\[Home] KK: \Cs KK: \CF diff --git a/development/autotests/keytest.py b/development/autotests/keytest.py index 404f2c540f..dab92a8ca0 100755 --- a/development/autotests/keytest.py +++ b/development/autotests/keytest.py @@ -593,7 +593,7 @@ if qt_frontend is None: qt_frontend = 'QT4' if qt_frontend == 'QT5': # Some tests sometimes failed with value 0.01 on Qt5.8 - controlkey_delay = 0.02 + controlkey_delay = 0.4 else: controlkey_delay = 0.4 diff --git a/src/lyxfind.cpp b/src/lyxfind.cpp index 87a57b8b56..68399929c5 100644 --- a/src/lyxfind.cpp +++ b/src/lyxfind.cpp @@ -1279,8 +1279,9 @@ int Intervall::findclosing(int start, int end) void LatexInfo::buildEntries(bool isPatternString) { - static regex const rmath("\\\\(begin|end)\\{((eqnarray|equation|flalign|gather|multiline|align)\\*?)\\}"); - static regex const rkeys("\\\\((([a-zA-Z]+\\*?)(\\{([a-z]+\\*?)\\}|=[0-9]+[a-z]+)?))"); + static regex const rmath("\\$|\\\\\\[|\\\\\\]|\\\\(begin|end)\\{((eqnarray|equation|flalign|gather|multiline|align)\\*?)\\}"); + static regex const rkeys("\\$|\\\\\\[|\\\\\\]|\\\\((([a-zA-Z]+\\*?)(\\{([a-z]+\\*?)\\}|=[0-9]+[a-z]+)?))"); + static bool disableLanguageOverride = false; smatch sub, submath; bool evaluatingRegexp = false; KeyInfo found; @@ -1293,9 +1294,22 @@ void LatexInfo::buildEntries(bool isPatternString) for (sregex_iterator itmath(interval.par.begin(), interval.par.end(), rmath), end; itmath != end; ++itmath) { submath = *itmath; if (math_end_waiting) { - if ((submath.str(1).compare("end") == 0) && + size_t pos = submath.position(size_t(0)); + if (math_end == "$") { + if ((submath.str(0) == "$") && (interval.par[pos-1] != '\\')) { + math_size = pos + 1 - math_pos; + math_end_waiting = false; + } + } + else if (math_end == "\\]") { + if (submath.str(0) == "\\]") { + math_size = pos + 2 - math_pos; + math_end_waiting = false; + } + } + else if ((submath.str(1).compare("end") == 0) && (submath.str(2).compare(math_end) == 0)) { - math_size = submath.position(size_t(0)) + submath.str(0).length() - math_pos; + math_size = pos + submath.str(0).length() - math_pos; math_end_waiting = false; } } @@ -1305,10 +1319,44 @@ void LatexInfo::buildEntries(bool isPatternString) math_end = submath.str(2); math_pos = submath.position(size_t(0)); } + else if (submath.str(0).compare("\\[") == 0) { + math_end_waiting = true; + math_end = "\\]"; + math_pos = submath.position(size_t(0)); + } + else if (submath.str(0) == "$") { + size_t pos = submath.position(size_t(0)); + if ((pos == 0) || (interval.par[pos-1] != '\\')) { + math_end_waiting = true; + math_end = "$"; + math_pos = pos; + } + } + } + } + if (isPatternString) { + if (math_pos < interval.par.length()) { + // Disable language + keys["foreignlanguage"].disabled = true; + disableLanguageOverride = true; + } + else + disableLanguageOverride = false; + } + else { + if (disableLanguageOverride) { + keys["foreignlanguage"].disabled = true; } } for (sregex_iterator it(interval.par.begin(), interval.par.end(), rkeys), end; it != end; ++it) { sub = *it; + string key = sub.str(3); + if (key == "") { + if (sub.str(0)[0] == '\\') + key = sub.str(0)[1]; + else + key = sub.str(0); + }; if (evaluatingRegexp) { if (sub.str(1).compare("endregexp") == 0) { evaluatingRegexp = false; @@ -1320,12 +1368,12 @@ void LatexInfo::buildEntries(bool isPatternString) } } else { - if (keys.find(sub.str(3)) == keys.end()) { + if (keys.find(key) == keys.end()) { LYXERR(Debug::FIND, "Found unknown key " << sub.str(0)); continue; } - found = keys[sub.str(3)]; - if (sub.str(3).compare("regexp") == 0) { + found = keys[key]; + if (key.compare("regexp") == 0) { evaluatingRegexp = true; found._tokenstart = sub.position(size_t(0)); found._tokensize = 0; @@ -1337,7 +1385,7 @@ void LatexInfo::buildEntries(bool isPatternString) continue; else if (found.keytype == KeyInfo::isMath) { if (size_t(sub.position(size_t(0))) == math_pos) { - found = keys[sub.str(3)]; + found = keys[key]; found._tokenstart = sub.position(size_t(0)); found._tokensize = math_size; found._dataEnd = found._tokenstart + found._tokensize; @@ -1364,14 +1412,14 @@ void LatexInfo::buildEntries(bool isPatternString) found.head = sub.str(0); } else - found.head = "\\" + sub.str(3); + found.head = "\\" + key; found._tokensize = found.head.length(); found._dataEnd = found._tokenstart + found._tokensize; found._dataStart = found._dataEnd; } else { if (found.parenthesiscount == 1) { - found.head = "\\" + sub.str(3) + "{"; + found.head = "\\" + key + "{"; } else if (found.parenthesiscount == 2) { found.head = sub.str(0) + "{"; @@ -1381,7 +1429,7 @@ void LatexInfo::buildEntries(bool isPatternString) found._dataStart = found._tokenstart + found.head.length(); found._dataEnd = interval.findclosing(found._dataStart, interval.par.length()); if (isPatternString) { - keys[sub.str(3)].used = true; + keys[key].used = true; } } } @@ -1455,9 +1503,11 @@ void LatexInfo::buildKeys(bool isPatternString) // Survives, like known character makeKey("lyx", KeyInfo(KeyInfo::isIgnored, 0, false), isPatternString); + makeKey("item", KeyInfo(KeyInfo::isChar, 0, false), isPatternString); - makeKey("begin", KeyInfo(KeyInfo::isMath, 1, false), isPatternString); - makeKey("end", KeyInfo(KeyInfo::isMath, 1, false), isPatternString); + makeKey("begin|end", KeyInfo(KeyInfo::isMath, 1, false), isPatternString); + makeKey("[|]", KeyInfo(KeyInfo::isMath, 1, false), isPatternString); + makeKey("$", KeyInfo(KeyInfo::isMath, 1, false), isPatternString); makeKey("par|uldepth|ULdepth", KeyInfo(KeyInfo::doRemove, 0, true), isPatternString); @@ -1672,6 +1722,11 @@ int LatexInfo::dispatch(ostringstream &os, int previousStart, KeyInfo &actual) case KeyInfo::isMain: { if (actual.disabled) { removeHead(actual); + if ((interval.par.substr(actual._dataStart, 3) == " \\[") || + (interval.par.substr(actual._dataStart, 8) == " \\begin{")) { + // Discard also the space before math-equation + interval.addIntervall(actual._dataStart, actual._dataStart+1); + } interval.resetOpenedP(actual._dataStart-1); } else { @@ -1802,7 +1857,7 @@ string splitOnKnownMacros(string par, bool isPatternString) { s = os.str(); } else - s = ""; /* found end */ + s = par; /* no known macros found */ return s; } diff --git a/src/lyxfind.h b/src/lyxfind.h index 61f44366ec..a761725183 100644 --- a/src/lyxfind.h +++ b/src/lyxfind.h @@ -138,6 +138,10 @@ docstring stringifyFromForSearch( DocIterator const & cur, int len = -1); +/** Set format type to be ignore by search + **/ +void setIgnoreFormat(std::string type, bool value); + } // namespace lyx #endif // LYXFIND_H