3 * This file is part of LyX, the document processor.
4 * License details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
9 * \author Alfredo Braunstein
10 * \author Tommaso Cucinotta
11 * \author Kornel Benko
13 * Full author contact details are available in file CREDITS.
21 #include "BufferList.h"
22 #include "BufferParams.h"
23 #include "BufferView.h"
26 #include "CutAndPaste.h"
27 #include "FuncRequest.h"
29 #include "output_latex.h"
30 #include "OutputParams.h"
31 #include "Paragraph.h"
36 #include "frontends/Application.h"
37 #include "frontends/alert.h"
39 #include "mathed/InsetMath.h"
40 #include "mathed/InsetMathHull.h"
41 #include "mathed/MathData.h"
42 #include "mathed/MathStream.h"
43 #include "mathed/MathSupport.h"
45 #include "support/debug.h"
46 #include "support/docstream.h"
47 #include "support/FileName.h"
48 #include "support/gettext.h"
49 #include "support/lassert.h"
50 #include "support/lstrings.h"
51 #include "support/textutils.h"
53 #include <unordered_map>
56 //#define ResultsDebug
57 #define USE_QT_FOR_SEARCH
58 #if defined(USE_QT_FOR_SEARCH)
59 #include <QtCore> // sets QT_VERSION
60 #if (QT_VERSION >= 0x050000)
61 #include <QRegularExpression>
71 using namespace lyx::support;
75 typedef unordered_map<string, string> AccentsMap;
76 typedef unordered_map<string,string>::const_iterator AccentsIterator;
77 static AccentsMap accents = unordered_map<string, string>();
79 // Helper class for deciding what should be ignored
83 IgnoreFormats() = default;
85 bool getFamily() const { return ignoreFamily_; }
87 bool getSeries() const { return ignoreSeries_; }
89 bool getShape() const { return ignoreShape_; }
91 bool getUnderline() const { return ignoreUnderline_; }
93 bool getMarkUp() const { return ignoreMarkUp_; }
95 bool getStrikeOut() const { return ignoreStrikeOut_; }
97 bool getSectioning() const { return ignoreSectioning_; }
99 bool getFrontMatter() const { return ignoreFrontMatter_; }
101 bool getColor() const { return ignoreColor_; }
103 bool getLanguage() const { return ignoreLanguage_; }
105 bool getDeleted() const { return ignoreDeleted_; }
107 void setIgnoreDeleted(bool value);
109 void setIgnoreFormat(string const & type, bool value, bool fromUser = true);
113 bool ignoreFamily_ = false;
115 bool ignoreSeries_ = false;
117 bool ignoreShape_ = false;
119 bool ignoreUnderline_ = false;
121 bool ignoreMarkUp_ = false;
123 bool ignoreStrikeOut_ = false;
125 bool ignoreSectioning_ = false;
127 bool ignoreFrontMatter_ = false;
129 bool ignoreColor_ = false;
131 bool ignoreLanguage_ = false;
132 bool userSelectedIgnoreLanguage_ = false;
134 bool ignoreDeleted_ = true;
137 void IgnoreFormats::setIgnoreFormat(string const & type, bool value, bool fromUser)
139 if (type == "color") {
140 ignoreColor_ = value;
142 else if (type == "language") {
144 userSelectedIgnoreLanguage_ = value;
145 ignoreLanguage_ = value;
148 ignoreLanguage_ = (value || userSelectedIgnoreLanguage_);
150 else if (type == "sectioning") {
151 ignoreSectioning_ = value;
152 ignoreFrontMatter_ = value;
154 else if (type == "font") {
155 ignoreSeries_ = value;
156 ignoreShape_ = value;
157 ignoreFamily_ = value;
159 else if (type == "series") {
160 ignoreSeries_ = value;
162 else if (type == "shape") {
163 ignoreShape_ = value;
165 else if (type == "family") {
166 ignoreFamily_ = value;
168 else if (type == "markup") {
169 ignoreMarkUp_ = value;
171 else if (type == "underline") {
172 ignoreUnderline_ = value;
174 else if (type == "strike") {
175 ignoreStrikeOut_ = value;
177 else if (type == "deleted") {
178 ignoreDeleted_ = value;
182 // The global variable that can be changed from outside
183 IgnoreFormats ignoreFormats;
186 void setIgnoreFormat(string const & type, bool value, bool fromUser)
188 ignoreFormats.setIgnoreFormat(type, value, fromUser);
194 bool parse_bool(docstring & howto, bool const defvalue = false)
199 howto = split(howto, var, ' ');
207 MatchString(docstring const & s, bool cs, bool mw)
208 : str(s), case_sens(cs), whole_words(mw)
211 // returns true if the specified string is at the specified position
212 // del specifies whether deleted strings in ct mode will be considered
213 int operator()(Paragraph const & par, pos_type pos, bool del = true) const
215 return par.find(str, case_sens, whole_words, pos, del);
223 // match whole words only
228 int findForward(DocIterator & cur, DocIterator const endcur,
229 MatchString const & match,
230 bool find_del = true, bool onlysel = false)
232 for (; cur; cur.forwardChar()) {
233 if (onlysel && endcur.pit() == cur.pit()
234 && endcur.idx() == cur.idx() && endcur.pos() < cur.pos())
236 if (cur.inTexted()) {
237 int len = match(cur.paragraph(), cur.pos(), find_del);
246 int findBackwards(DocIterator & cur, DocIterator const endcur,
247 MatchString const & match,
248 bool find_del = true, bool onlysel = false)
252 if (onlysel && endcur.pit() == cur.pit()
253 && endcur.idx() == cur.idx() && endcur.pos() > cur.pos())
255 if (cur.inTexted()) {
256 int len = match(cur.paragraph(), cur.pos(), find_del);
265 bool searchAllowed(docstring const & str)
268 frontend::Alert::error(_("Search error"), _("Search string is empty"));
277 bool findOne(BufferView * bv, docstring const & searchstr,
278 bool case_sens, bool whole, bool forward,
279 bool find_del, bool check_wrap, bool auto_wrap,
280 bool instant, bool onlysel)
282 if (!searchAllowed(searchstr))
285 DocIterator const endcur = forward ? bv->cursor().selectionEnd() : bv->cursor().selectionBegin();
287 if (onlysel && bv->cursor().selection()) {
288 docstring const matchstring = bv->cursor().selectionAsString(false);
289 docstring const lcmatchsting = support::lowercase(matchstring);
290 if (matchstring == searchstr || (!case_sens && lcmatchsting == lowercase(searchstr))) {
291 docstring q = _("The search string matches the selection, and search is limited to selection.\n"
292 "Continue search outside?");
293 int search_answer = frontend::Alert::prompt(_("Search outside selection?"),
294 q, 0, 1, _("&Yes"), _("&No"));
295 if (search_answer == 0) {
296 bv->clearSelection();
297 if (findOne(bv, searchstr, case_sens, whole, forward,
298 find_del, check_wrap, auto_wrap, false, false))
305 DocIterator cur = forward
306 ? ((instant || onlysel) ? bv->cursor().selectionBegin() : bv->cursor().selectionEnd())
307 : ((instant || onlysel) ? bv->cursor().selectionEnd() : bv->cursor().selectionBegin());
309 MatchString const match(searchstr, case_sens, whole);
311 int match_len = forward
312 ? findForward(cur, endcur, match, find_del, onlysel)
313 : findBackwards(cur, endcur, match, find_del, onlysel);
316 bv->putSelectionAt(cur, match_len, !forward);
318 docstring q = _("The search string was not found within the selection.\n"
319 "Continue search outside?");
320 int search_answer = frontend::Alert::prompt(_("Search outside selection?"),
321 q, 0, 1, _("&Yes"), _("&No"));
322 if (search_answer == 0) {
323 bv->clearSelection();
324 if (findOne(bv, searchstr, case_sens, whole, forward,
325 find_del, check_wrap, auto_wrap, false, false))
330 else if (check_wrap) {
331 DocIterator cur_orig(bv->cursor());
335 q = _("End of file reached while searching forward.\n"
336 "Continue searching from the beginning?");
338 q = _("Beginning of file reached while searching backward.\n"
339 "Continue searching from the end?");
340 int wrap_answer = frontend::Alert::prompt(_("Wrap search?"),
341 q, 0, 1, _("&Yes"), _("&No"));
342 auto_wrap = wrap_answer == 0;
346 bv->cursor().clear();
347 bv->cursor().push_back(CursorSlice(bv->buffer().inset()));
349 bv->cursor().setCursor(doc_iterator_end(&bv->buffer()));
350 bv->cursor().backwardPos();
352 bv->clearSelection();
353 if (findOne(bv, searchstr, case_sens, whole, forward,
354 find_del, false, false, false, false))
357 bv->cursor().setCursor(cur_orig);
361 return match_len > 0;
367 int replaceAll(BufferView * bv,
368 docstring const & searchstr, docstring const & replacestr,
369 bool case_sens, bool whole, bool onlysel)
371 Buffer & buf = bv->buffer();
373 if (!searchAllowed(searchstr) || buf.isReadonly())
376 DocIterator startcur = bv->cursor().selectionBegin();
377 DocIterator endcur = bv->cursor().selectionEnd();
378 bool const had_selection = bv->cursor().selection();
380 MatchString const match(searchstr, case_sens, whole);
383 int const rsize = replacestr.size();
384 int const ssize = searchstr.size();
387 cur.setCursor(doc_iterator_begin(&buf));
388 int match_len = findForward(cur, endcur, match, false, onlysel);
389 while (match_len > 0) {
390 // Backup current cursor position and font.
391 pos_type const pos = cur.pos();
392 Font const font = cur.paragraph().getFontSettings(buf.params(), pos);
395 cur.paragraph().eraseChars(pos, pos + match_len,
396 buf.params().track_changes);
397 cur.paragraph().insert(pos, replacestr, font,
398 Change(buf.params().track_changes
400 : Change::UNCHANGED));
401 for (int i = 0; i < rsize + struck; ++i)
403 if (onlysel && cur.pit() == endcur.pit() && cur.idx() == endcur.idx()) {
404 // Adjust end of selection for replace-all in selection
406 int const offset = rsize - ssize;
407 for (int i = 0; i < offset + struck; ++i)
410 int const offset = ssize - rsize;
411 for (int i = 0; i < offset + struck; ++i)
412 endcur.backwardPos();
416 match_len = findForward(cur, endcur, match, false, onlysel);
419 bv->putSelectionAt(doc_iterator_begin(&buf), 0, false);
421 startcur.fixIfBroken();
422 bv->setCursor(startcur);
424 // Reset selection, accounting for changes in selection
426 endcur.fixIfBroken();
427 bv->cursor().resetAnchor();
428 bv->setCursorSelectionTo(endcur);
435 // the idea here is that we are going to replace the string that
436 // is selected IF it is the search string.
437 // if there is a selection, but it is not the search string, then
438 // we basically ignore it. (FIXME We ought to replace only within
440 // if there is no selection, then:
441 // (i) if some search string has been provided, then we find it.
442 // (think of how the dialog works when you hit "replace" the
444 // (ii) if no search string has been provided, then we treat the
445 // word the cursor is in as the search string. (why? i have no
446 // idea.) but this only works in text?
448 // returns the number of replacements made (one, if any) and
449 // whether anything at all was done.
450 pair<bool, int> replaceOne(BufferView * bv, docstring searchstr,
451 docstring const & replacestr, bool case_sens,
452 bool whole, bool forward, bool findnext, bool wrap,
455 Cursor & cur = bv->cursor();
456 if (!cur.selection() || onlysel) {
457 // no selection, non-empty search string: find it
458 if (!searchstr.empty()) {
459 bool const found = findOne(bv, searchstr, case_sens, whole,
460 forward, true, findnext, wrap, false, onlysel);
461 return make_pair(found, 0);
463 // empty search string
466 return make_pair(false, 0);
467 // select current word and treat it as the search string.
468 // This causes a minor bug as undo will restore this selection,
469 // which the user did not create (#8986).
470 cur.innerText()->selectWord(cur, WHOLE_WORD);
471 searchstr = cur.selectionAsString(false, true);
474 // if we still don't have a search string, report the error
476 if (!searchAllowed(searchstr))
477 return make_pair(false, 0);
479 bool have_selection = cur.selection();
480 docstring const selected = cur.selectionAsString(false, true);
483 ? searchstr == selected
484 : compare_no_case(searchstr, selected) == 0;
486 // no selection or current selection is not search word:
487 // just find the search word
488 if (!have_selection || !match) {
489 bool const found = findOne(bv, searchstr, case_sens, whole, forward,
490 true, findnext, wrap, false, onlysel);
491 return make_pair(found, 0);
494 // we're now actually ready to replace. if the buffer is
495 // read-only, we can't, though.
496 if (bv->buffer().isReadonly())
497 return make_pair(false, 0);
499 cap::replaceSelectionWithString(cur, replacestr);
501 cur.pos() += replacestr.length();
502 LASSERT(cur.pos() <= cur.lastpos(),
503 cur.pos() = cur.lastpos());
506 findOne(bv, searchstr, case_sens, whole,
507 forward, false, findnext, wrap, false, onlysel);
509 return make_pair(true, 1);
515 docstring const find2string(docstring const & search,
516 bool casesensitive, bool matchword,
517 bool forward, bool wrap, bool instant,
522 << int(casesensitive) << ' '
523 << int(matchword) << ' '
524 << int(forward) << ' '
526 << int(instant) << ' '
532 docstring const replace2string(docstring const & replace,
533 docstring const & search,
534 bool casesensitive, bool matchword,
535 bool all, bool forward, bool findnext,
536 bool wrap, bool onlysel)
539 ss << replace << '\n'
541 << int(casesensitive) << ' '
542 << int(matchword) << ' '
544 << int(forward) << ' '
545 << int(findnext) << ' '
552 docstring const string2find(docstring const & argument,
560 // data is of the form
562 // <casesensitive> <matchword> <forward> <wrap> <onlysel>"
564 docstring howto = split(argument, search, '\n');
566 casesensitive = parse_bool(howto);
567 matchword = parse_bool(howto);
568 forward = parse_bool(howto, true);
569 wrap = parse_bool(howto);
570 instant = parse_bool(howto);
571 onlysel = parse_bool(howto);
577 bool lyxfind(BufferView * bv, FuncRequest const & ev)
579 if (!bv || ev.action() != LFUN_WORD_FIND)
582 //lyxerr << "find called, cmd: " << ev << endl;
590 docstring search = string2find(ev.argument(), casesensitive,
591 matchword, forward, wrap, instant, onlysel);
593 return findOne(bv, search, casesensitive, matchword, forward,
594 false, true, wrap, instant, onlysel);
598 bool lyxreplace(BufferView * bv, FuncRequest const & ev)
600 if (!bv || ev.action() != LFUN_WORD_REPLACE)
603 // data is of the form
606 // <casesensitive> <matchword> <all> <forward> <findnext> <wrap> <onlysel>"
609 docstring howto = split(ev.argument(), rplc, '\n');
610 howto = split(howto, search, '\n');
612 bool casesensitive = parse_bool(howto);
613 bool matchword = parse_bool(howto);
614 bool all = parse_bool(howto);
615 bool forward = parse_bool(howto, true);
616 bool findnext = parse_bool(howto, true);
617 bool wrap = parse_bool(howto);
618 bool onlysel = parse_bool(howto);
620 if (!bv->cursor().selection())
621 // only selection only makes sense with selection
626 int replace_count = 0;
628 replace_count = replaceAll(bv, search, rplc, casesensitive,
630 update = replace_count > 0;
633 replaceOne(bv, search, rplc, casesensitive, matchword,
634 forward, findnext, wrap, onlysel);
636 replace_count = rv.second;
639 Buffer const & buf = bv->buffer();
641 // emit message signal.
643 buf.message(_("String not found in selection."));
645 buf.message(_("String not found."));
647 if (replace_count == 0) {
648 buf.message(_("String found."));
649 } else if (replace_count == 1) {
650 buf.message(_("String has been replaced."));
652 docstring const str = onlysel
653 ? bformat(_("%1$d strings have been replaced in the selection."), replace_count)
654 : bformat(_("%1$d strings have been replaced."), replace_count);
662 bool findNextChange(BufferView * bv, Cursor & cur, bool const check_wrap)
664 for (; cur; cur.forwardPos())
665 if (cur.inTexted() && cur.paragraph().isChanged(cur.pos()))
669 DocIterator cur_orig(bv->cursor());
670 docstring q = _("End of file reached while searching forward.\n"
671 "Continue searching from the beginning?");
672 int wrap_answer = frontend::Alert::prompt(_("Wrap search?"),
673 q, 0, 1, _("&Yes"), _("&No"));
674 if (wrap_answer == 0) {
675 bv->cursor().clear();
676 bv->cursor().push_back(CursorSlice(bv->buffer().inset()));
677 bv->clearSelection();
678 cur.setCursor(bv->cursor().selectionBegin());
679 if (findNextChange(bv, cur, false))
682 bv->cursor().setCursor(cur_orig);
689 bool findPreviousChange(BufferView * bv, Cursor & cur, bool const check_wrap)
691 for (cur.backwardPos(); cur; cur.backwardPos()) {
692 if (cur.inTexted() && cur.paragraph().isChanged(cur.pos()))
697 DocIterator cur_orig(bv->cursor());
698 docstring q = _("Beginning of file reached while searching backward.\n"
699 "Continue searching from the end?");
700 int wrap_answer = frontend::Alert::prompt(_("Wrap search?"),
701 q, 0, 1, _("&Yes"), _("&No"));
702 if (wrap_answer == 0) {
703 bv->cursor().setCursor(doc_iterator_end(&bv->buffer()));
704 bv->cursor().backwardPos();
705 bv->clearSelection();
706 cur.setCursor(bv->cursor().selectionBegin());
707 if (findPreviousChange(bv, cur, false))
710 bv->cursor().setCursor(cur_orig);
717 bool selectChange(Cursor & cur, bool forward)
719 if (!cur.inTexted() || !cur.paragraph().isChanged(cur.pos()))
721 Change ch = cur.paragraph().lookupChange(cur.pos());
723 CursorSlice tip1 = cur.top();
724 for (; tip1.pit() < tip1.lastpit() || tip1.pos() < tip1.lastpos(); tip1.forwardPos()) {
725 Change ch2 = tip1.paragraph().lookupChange(tip1.pos());
726 if (!ch2.isSimilarTo(ch))
729 CursorSlice tip2 = cur.top();
730 for (; tip2.pit() > 0 || tip2.pos() > 0;) {
732 Change ch2 = tip2.paragraph().lookupChange(tip2.pos());
733 if (!ch2.isSimilarTo(ch)) {
734 // take a step forward to correctly set the selection
742 cur.bv().mouseSetCursor(cur, false);
744 cur.bv().mouseSetCursor(cur, true);
752 bool findChange(BufferView * bv, bool forward)
755 cur.setCursor(forward ? bv->cursor().selectionEnd()
756 : bv->cursor().selectionBegin());
757 forward ? findNextChange(bv, cur, true) : findPreviousChange(bv, cur, true);
758 return selectChange(cur, forward);
763 bool findNextChange(BufferView * bv)
765 return findChange(bv, true);
769 bool findPreviousChange(BufferView * bv)
771 return findChange(bv, false);
778 typedef vector<pair<string, string> > Escapes;
780 string string2regex(string in)
782 static std::regex specialChars { R"([-[\]{}()*+?.,\^$|#\s\$\\])" };
783 string temp = std::regex_replace(in, specialChars, R"(\$&)" );
788 while (fl_pos < temp.size()) {
789 fl_pos = temp.find("\\\\foreignlanguage", lastpos + offset);
790 if (fl_pos == string::npos)
793 temp2 += temp.substr(lastpos, fl_pos - lastpos);
799 if (lastpos < temp.size()) {
800 temp2 += temp.substr(lastpos, temp.size() - lastpos);
805 string correctRegex(string t, bool withformat)
807 /* Convert \backslash => \
808 * and \{, \}, \[, \] => {, }, [, ]
811 regex wordre("(\\\\)*(\\\\((backslash|mathcircumflex) ?|[\\[\\]\\{\\}]))");
814 bool backslashed = false;
815 for (sregex_iterator it(t.begin(), t.end(), wordre), end; it != end; ++it) {
818 if ((sub.position(2) - sub.position(0)) % 2 == 1) {
822 if (sub.str(4) == "backslash") {
825 // transforms '\backslash \{' into '\{'
827 string next = t.substr(sub.position(2) + sub.str(2).length(), 2);
828 if ((next == "\\{") || (next == "\\}")) {
834 else if (sub.str(4) == "mathcircumflex")
836 else if (backslashed) {
838 if (withformat && (sub.str(3) == "{"))
839 replace = accents["braceleft"];
840 else if (withformat && (sub.str(3) == "}"))
841 replace = accents["braceright"];
843 // else part should not exist
848 replace = sub.str(3);
850 if (lastpos < (size_t) sub.position(2))
851 s += t.substr(lastpos, sub.position(2) - lastpos);
853 lastpos = sub.position(2) + sub.length(2);
857 else if (lastpos < t.length())
858 s += t.substr(lastpos, t.length() - lastpos);
862 /// Within \regexp{} apply get_lyx_unescapes() only (i.e., preserve regexp semantics of the string),
863 /// while outside apply get_lyx_unescapes()+get_regexp_escapes().
864 /// If match_latex is true, then apply regexp_latex_escapes() to \regexp{} contents as well.
865 string escape_for_regex(string s, bool withformat)
869 while (lastpos < s.size()) {
870 size_t regex_pos = s.find("\\regexp{", lastpos);
871 if (regex_pos == string::npos) {
872 regex_pos = s.size();
874 if (regex_pos > lastpos) {
875 result += string2regex(s.substr(lastpos, regex_pos-lastpos));
877 if (lastpos == s.size())
880 size_t end_pos = s.find("\\endregexp{}}", regex_pos + 8);
881 result += correctRegex(s.substr(regex_pos + 8, end_pos -(regex_pos + 8)), withformat);
882 lastpos = end_pos + 13;
888 /// Wrapper for lyx::regex_replace with simpler interface
889 bool regex_replace(string const & s, string & t, string const & searchstr,
890 string const & replacestr)
892 regex e(searchstr, regex_constants::ECMAScript);
894 ostream_iterator<char, char> it(oss);
895 regex_replace(it, s.begin(), s.end(), e, replacestr);
896 // tolerate t and s be references to the same variable
897 bool rv = (s != oss.str());
916 vector <string> result = vector <string>();
917 MatchResult(int len = 0): match_len(len),match_prefix(0),match2end(0), pos(0),leadsize(0),pos_len(-1),searched_size(0) {};
920 static MatchResult::range interpretMatch(MatchResult &oldres, MatchResult &newres)
922 if (newres.match2end < oldres.match2end)
923 return MatchResult::newIsTooFar;
924 if (newres.match_len < oldres.match_len)
925 return MatchResult::newIsTooFar;
927 if (newres.match_len == oldres.match_len) {
928 if (newres.match2end == oldres.match2end)
929 return MatchResult::newIsBetter;
931 return MatchResult::newIsInvalid;
934 /** The class performing a match between a position in the document and the FindAdvOptions.
937 class MatchStringAdv {
939 MatchStringAdv(lyx::Buffer & buf, FindAndReplaceOptions & opt);
941 /** Tests if text starting at the supplied position matches with the one provided to the MatchStringAdv
942 ** constructor as opt.search, under the opt.* options settings.
945 ** If set, then match is searched only against beginning of text starting at cur.
946 ** If unset, then match is searched anywhere in text starting at cur.
949 ** The length of the matching text, or zero if no match was found.
951 MatchResult operator()(DocIterator const & cur, int len = -1, bool at_begin = true) const;
960 /// first buffer on which search was started
961 lyx::Buffer * const p_first_buf;
963 FindAndReplaceOptions const & opt;
966 /// Auxiliary find method (does not account for opt.matchword)
967 MatchResult findAux(DocIterator const & cur, int len = -1, bool at_begin = true) const;
968 void CreateRegexp(FindAndReplaceOptions const & opt, string regexp_str, string regexp2_str, string par_as_string = "");
970 /** Normalize a stringified or latexified LyX paragraph.
974 ** <li>if search is not casesensitive, then lowercase the string;
975 ** <li>remove any newline at begin or end of the string;
976 ** <li>replace any newline in the middle of the string with a simple space;
977 ** <li>remove stale empty styles and environments, like \emph{} and \textbf{}.
980 ** @todo Normalization should also expand macros, if the corresponding
981 ** search option was checked.
983 string normalize(docstring const & s) const;
984 // normalized string to search
985 string par_as_string;
986 // regular expression to use for searching
987 // regexp2 is same as regexp, but prefixed with a ".*?"
989 QRegularExpression regexp;
990 QRegularExpression regexp2;
995 // leading format material as string
996 string lead_as_string;
997 // par_as_string after removal of lead_as_string
998 string par_as_string_nolead;
999 // unmatched open braces in the search string/regexp
1001 // number of (.*?) subexpressions added at end of search regexp for closing
1002 // environments, math mode, styles, etc...
1003 int close_wildcards;
1005 // Are we searching with regular expressions ?
1007 static int valid_matches;
1008 static vector <string> matches;
1009 void FillResults(MatchResult &found_mr);
1012 int MatchStringAdv::valid_matches = 0;
1013 vector <string> MatchStringAdv::matches = vector <string>(10);
1015 void MatchStringAdv::FillResults(MatchResult &found_mr)
1017 if (found_mr.match_len > 0) {
1018 valid_matches = found_mr.result.size();
1019 for (size_t i = 0; i < found_mr.result.size(); i++)
1020 matches[i] = found_mr.result[i];
1026 static docstring buffer_to_latex(Buffer & buffer)
1028 //OutputParams runparams(&buffer.params().encoding());
1029 OutputParams runparams(encodings.fromLyXName("utf8"));
1030 odocstringstream ods;
1032 runparams.nice = true;
1033 runparams.flavor = Flavor::XeTeX;
1034 runparams.linelen = 10000; //lyxrc.plaintext_linelen;
1035 // No side effect of file copying and image conversion
1036 runparams.dryrun = true;
1037 if (ignoreFormats.getDeleted())
1038 runparams.for_searchAdv = OutputParams::SearchWithoutDeleted;
1040 runparams.for_searchAdv = OutputParams::SearchWithDeleted;
1041 pit_type const endpit = buffer.paragraphs().size();
1042 for (pit_type pit = 0; pit != endpit; ++pit) {
1043 TeXOnePar(buffer, buffer.text(), pit, os, runparams);
1044 LYXERR(Debug::FIND, "searchString up to here: " << ods.str());
1050 static docstring stringifySearchBuffer(Buffer & buffer, FindAndReplaceOptions const & opt)
1053 if (!opt.ignoreformat) {
1054 str = buffer_to_latex(buffer);
1056 // OutputParams runparams(&buffer.params().encoding());
1057 OutputParams runparams(encodings.fromLyXName("utf8"));
1058 runparams.nice = true;
1059 runparams.flavor = Flavor::XeTeX;
1060 runparams.linelen = 10000; //lyxrc.plaintext_linelen;
1061 runparams.dryrun = true;
1062 int option = AS_STR_INSETS |AS_STR_PLAINTEXT;
1063 if (ignoreFormats.getDeleted()) {
1064 option |= AS_STR_SKIPDELETE;
1065 runparams.for_searchAdv = OutputParams::SearchWithoutDeleted;
1068 runparams.for_searchAdv = OutputParams::SearchWithDeleted;
1070 for (pos_type pit = pos_type(0); pit < (pos_type)buffer.paragraphs().size(); ++pit) {
1071 Paragraph const & par = buffer.paragraphs().at(pit);
1072 LYXERR(Debug::FIND, "Adding to search string: '"
1073 << par.asString(pos_type(0), par.size(),
1077 str += par.asString(pos_type(0), par.size(),
1081 // Even in ignore-format we have to remove "\text{}, \lyxmathsym{}" parts
1082 string t = to_utf8(str);
1083 while (regex_replace(t, t, "\\\\(text|lyxmathsym|ensuremath)\\{([^\\}]*)\\}", "$2"));
1090 /// Return separation pos between the leading material and the rest
1091 static size_t identifyLeading(string const & s)
1094 // @TODO Support \item[text]
1095 // Kornel: Added textsl, textsf, textit, texttt and noun
1096 // + allow to search for colored text too
1097 while (regex_replace(t, t, "^\\\\(("
1098 "(author|title|subtitle|subject|publishers|dedication|uppertitleback|lowertitleback|extratitle|"
1099 "lyxaddress|lyxrightaddress|"
1100 "footnotesize|tiny|scriptsize|small|large|Large|LARGE|huge|Huge|"
1101 "emph|noun|minisec|text(bf|md|sl|sf|it|tt))|"
1102 "((textcolor|foreignlanguage|latexenvironment)\\{[a-z]+\\*?\\})|"
1103 "(u|uu)line|(s|x)out|uwave)|((sub)?(((sub)?section)|paragraph)|part|chapter)\\*?)\\{", "")
1104 || regex_replace(t, t, "^\\$", "")
1105 || regex_replace(t, t, "^\\\\\\[", "")
1106 || regex_replace(t, t, "^ ?\\\\item\\{[a-z]+\\}", "")
1107 || regex_replace(t, t, "^\\\\begin\\{[a-zA-Z_]*\\*?\\}", ""))
1109 LYXERR(Debug::FIND, " after removing leading $, \\[ , \\emph{, \\textbf{, etc.: '" << t << "'");
1114 * Given a latexified string, retrieve some handled features
1115 * The features of the regex will later be compared with the features
1116 * of the searched text. If the regex features are not a
1117 * subset of the analized, then, in not format ignoring search
1118 * we can early stop the search in the relevant inset.
1120 typedef map<string, bool> Features;
1122 static Features identifyFeatures(string const & s)
1124 static regex const feature("\\\\(([a-zA-Z]+(\\{([a-z]+\\*?)\\}|\\*)?))\\{");
1125 static regex const valid("^("
1127 "(footnotesize|tiny|scriptsize|small|large|Large|LARGE|huge|Huge|"
1128 "emph|noun|text(bf|md|sl|sf|it|tt)|"
1129 "(textcolor|foreignlanguage|item|listitem|latexenvironment)\\{[a-z]+\\*?\\})|"
1130 "(u|uu)line|(s|x)out|uwave|"
1131 "(sub|extra)?title|author|subject|publishers|dedication|(upper|lower)titleback|lyx(right)?address)|"
1132 "((sub)?(((sub)?section)|paragraph)|part|chapter|lyxslide)\\*?)$");
1137 for (sregex_iterator it(s.begin(), s.end(), feature), end; it != end; ++it) {
1140 if (sub.str(1).compare("regexp") == 0) {
1144 string token = sub.str(1);
1146 if (regex_match(token, sub2, valid)) {
1154 if (sub.str(1).compare("endregexp") == 0) {
1164 * defines values features of a key "\\[a-z]+{"
1169 /* Char type with content discarded
1170 * like \hspace{1cm} */
1172 /* Char, like \backslash */
1174 /* replace starting backslash with '#' */
1176 /* \part, \section*, ... */
1178 /* title, author etc */
1180 /* \foreignlanguage{ngerman}, ... */
1183 * to discard language in content */
1186 /* \begin{eqnarray}...\end{eqnarray}, ... $...$ */
1188 /* fonts, colors, markups, ... */
1190 /* footnotesize, ... large, ...
1191 * Ignore all of them */
1194 /* inputencoding, ...
1195 * Discard also content, because they do not help in search */
1198 * like remove, but also all arguments */
1200 /* item, listitem */
1202 /* tex, latex, ... like isChar */
1204 /* like \lettrine[lines=5]{}{} */
1206 // like isStandard, but always remove head
1208 /* End of arguments marker for lettrine,
1209 * so that they can be ignored */
1212 KeyInfo() = default;
1213 KeyInfo(KeyType type, int parcount, bool disable)
1215 parenthesiscount(parcount),
1216 disabled(disable) {}
1217 KeyType keytype = invalid;
1219 int _tokensize = -1;
1220 int _tokenstart = -1;
1221 int _dataStart = -1;
1223 int parenthesiscount = 1;
1224 bool disabled = false;
1225 bool used = false; /* by pattern */
1230 Border(int l=0, int u=0) : low(l), upper(u) {};
1235 #define MAXOPENED 30
1237 bool isPatternString_;
1239 explicit Intervall(bool isPattern, string const & p) :
1240 isPatternString_(isPattern), par(p), ignoreidx(-1), actualdeptindex(0),
1241 hasTitle(false), langcount(0)
1249 static vector<Border> borders;
1250 int depts[MAXOPENED];
1251 int closes[MAXOPENED];
1252 int actualdeptindex;
1253 int previousNotIgnored(int) const;
1254 int nextNotIgnored(int) const;
1255 void handleOpenP(int i);
1256 void handleCloseP(int i, bool closingAllowed);
1257 void resetOpenedP(int openPos);
1258 void addIntervall(int upper);
1259 void addIntervall(int low, int upper); /* if explicit */
1260 void removeAccents();
1261 void setForDefaultLang(KeyInfo const & defLang) const;
1262 int findclosing(int start, int end, char up, char down, int repeat);
1263 void handleParentheses(int lastpos, bool closingAllowed);
1265 int langcount; // Number of disabled language specs up to current position in actual interval
1266 int isOpeningPar(int pos) const;
1268 void output(ostringstream &os, int lastpos);
1269 // string show(int lastpos);
1272 vector<Border> Intervall::borders = vector<Border>(30);
1274 int Intervall::isOpeningPar(int pos) const
1276 if ((pos < 0) || (size_t(pos) >= par.size()))
1278 if (par[pos] != '{')
1280 if (size_t(pos) + 2 >= par.size())
1282 if (par[pos+2] != '}')
1284 if (par[pos+1] == '[' || par[pos+1] == ']')
1289 void Intervall::setForDefaultLang(KeyInfo const & defLang) const
1291 // Enable the use of first token again
1292 if (ignoreidx >= 0) {
1293 int value = defLang._tokenstart + defLang._tokensize;
1299 if (borders[borderidx].low < value)
1300 borders[borderidx].low = value;
1301 if (borders[borderidx].upper < value)
1302 borders[borderidx].upper = value;
1307 static void checkDepthIndex(int val)
1309 static int maxdepthidx = MAXOPENED-2;
1310 static int lastmaxdepth = 0;
1311 if (val > lastmaxdepth) {
1312 LYXERR(Debug::INFO, "Depth reached " << val);
1315 if (val > maxdepthidx) {
1317 LYXERR(Debug::INFO, "maxdepthidx now " << val);
1322 // Not needed, because borders are now dynamically expanded
1323 static void checkIgnoreIdx(int val)
1325 static int lastmaxignore = -1;
1326 if ((lastmaxignore < val) && (size_t(val+1) >= borders.size())) {
1327 LYXERR(Debug::INFO, "IgnoreIdx reached " << val);
1328 lastmaxignore = val;
1334 * Expand the region of ignored parts of the input latex string
1335 * The region is only relevant in output()
1337 void Intervall::addIntervall(int low, int upper)
1340 if (low == upper) return;
1341 for (idx = ignoreidx+1; idx > 0; --idx) {
1342 if (low > borders[idx-1].upper) {
1346 Border br(low, upper);
1347 if (idx > ignoreidx) {
1348 if (borders.size() <= size_t(idx)) {
1349 borders.push_back(br);
1355 // checkIgnoreIdx(ignoreidx);
1359 // Expand only if one of the new bound is inside the interwall
1360 // We know here that br.low > borders[idx-1].upper
1361 if (br.upper < borders[idx].low) {
1362 // We have to insert at this pos
1363 if (size_t(ignoreidx+1) >= borders.size()) {
1364 borders.push_back(borders[ignoreidx]);
1367 borders[ignoreidx+1] = borders[ignoreidx];
1369 for (int i = ignoreidx; i > idx; --i) {
1370 borders[i] = borders[i-1];
1374 // checkIgnoreIdx(ignoreidx);
1377 // Here we know, that we are overlapping
1378 if (br.low > borders[idx].low)
1379 br.low = borders[idx].low;
1380 // check what has to be concatenated
1382 for (int i = idx; i <= ignoreidx; i++) {
1383 if (br.upper >= borders[i].low) {
1385 if (br.upper < borders[i].upper)
1386 br.upper = borders[i].upper;
1392 // count should be >= 1 here
1395 for (int i = idx + count; i <= ignoreidx; i++) {
1396 borders[i-count+1] = borders[i];
1398 ignoreidx -= count - 1;
1404 static void buildaccent(string n, string param, string values)
1408 const char delim = '|';
1409 while (getline(s, name, delim)) {
1411 for (char c : param) {
1412 string key = name + "{" + c + "}";
1413 // get the corresponding utf8-value
1414 if ((values[start] & 0xc0) != 0xc0) {
1415 // should not happen, utf8 encoding starts at least with 11xxxxxx
1416 // but value for '\dot{i}' is 'i', which is ascii
1417 if ((values[start] & 0x80) == 0) {
1419 accents[key] = values.substr(start, 1);
1420 // LYXERR(Debug::INFO, "" << key << "=" << accents[key]);
1425 for (int j = 1; ;j++) {
1426 if (start + j >= values.size()) {
1427 accents[key] = values.substr(start, j);
1428 start = values.size() - 1;
1431 else if ((values[start+j] & 0xc0) != 0x80) {
1432 // This is the first byte of following utf8 char
1433 accents[key] = values.substr(start, j);
1435 // LYXERR(Debug::INFO, "" << key << "=" << accents[key]);
1444 static string getutf8(unsigned uchar)
1447 string ret = string();
1449 if (uchar <= 0x7f) {
1450 c[maxc-1] = uchar & 0x7f;
1453 unsigned char rest = 0x40;
1454 unsigned char first = 0x80;
1456 for (int i = start; i >=0; --i) {
1458 c[i] = first + uchar;
1461 c[i] = 0x80 | (uchar & 0x3f);
1468 for (int i = 0; i < maxc; i++) {
1469 if (c[i] == 0) continue;
1475 static void buildAccentsMap()
1477 accents["imath"] = "ı";
1479 accents["jmath"] = "ȷ";
1480 accents["cdot"] = "·";
1481 accents["textasciicircum"] = "^";
1482 accents["mathcircumflex"] = "^";
1483 accents["sim"] = "~";
1484 accents["guillemotright"] = "»";
1485 accents["guillemotleft"] = "«";
1486 accents["hairspace"] = getutf8(0xf0000); // select from free unicode plane 15
1487 accents["thinspace"] = getutf8(0xf0002); // and used _only_ by findadv
1488 accents["negthinspace"] = getutf8(0xf0003); // to omit backslashed latex macros
1489 accents["medspace"] = getutf8(0xf0004); // See https://en.wikipedia.org/wiki/Private_Use_Areas
1490 accents["negmedspace"] = getutf8(0xf0005);
1491 accents["thickspace"] = getutf8(0xf0006);
1492 accents["negthickspace"] = getutf8(0xf0007);
1493 accents["lyx"] = getutf8(0xf0010); // Used logos
1494 accents["LyX"] = getutf8(0xf0010);
1495 accents["tex"] = getutf8(0xf0011);
1496 accents["TeX"] = getutf8(0xf0011);
1497 accents["latex"] = getutf8(0xf0012);
1498 accents["LaTeX"] = getutf8(0xf0012);
1499 accents["latexe"] = getutf8(0xf0013);
1500 accents["LaTeXe"] = getutf8(0xf0013);
1501 accents["lyxarrow"] = getutf8(0xf0020);
1502 accents["braceleft"] = getutf8(0xf0030);
1503 accents["braceright"] = getutf8(0xf0031);
1504 accents["backslash lyx"] = getutf8(0xf0010); // Used logos inserted with starting \backslash
1505 accents["backslash LyX"] = getutf8(0xf0010);
1506 accents["backslash tex"] = getutf8(0xf0011);
1507 accents["backslash TeX"] = getutf8(0xf0011);
1508 accents["backslash latex"] = getutf8(0xf0012);
1509 accents["backslash LaTeX"] = getutf8(0xf0012);
1510 accents["backslash latexe"] = getutf8(0xf0013);
1511 accents["backslash LaTeXe"] = getutf8(0xf0013);
1512 accents["backslash lyxarrow"] = getutf8(0xf0020);
1513 accents["ddot{\\imath}"] = "ï";
1514 buildaccent("ddot", "aAeEhHiIioOtuUwWxXyY",
1515 "äÄëËḧḦïÏïöÖẗüÜẅẄẍẌÿŸ"); // umlaut
1516 buildaccent("dot|.", "aAbBcCdDeEfFGghHIimMnNoOpPrRsStTwWxXyYzZ",
1517 "ȧȦḃḂċĊḋḊėĖḟḞĠġḣḢİİṁṀṅṄȯȮṗṖṙṘṡṠṫṪẇẆẋẊẏẎżŻ"); // dot{i} can only happen if ignoring case, but there is no lowercase of 'İ'
1518 accents["acute{\\imath}"] = "í";
1519 buildaccent("acute", "aAcCeEgGkKlLmMoOnNpPrRsSuUwWyYzZiI",
1520 "áÁćĆéÉǵǴḱḰĺĹḿḾóÓńŃṕṔŕŔśŚúÚẃẂýÝźŹíÍ");
1521 buildaccent("dacute|H|h", "oOuU", "őŐűŰ"); // double acute
1522 buildaccent("mathring|r", "aAuUwy",
1524 accents["check{\\imath}"] = "ǐ";
1525 accents["check{\\jmath}"] = "ǰ";
1526 buildaccent("check|v", "cCdDaAeEiIoOuUgGkKhHlLnNrRsSTtzZ",
1527 "čČďĎǎǍěĚǐǏǒǑǔǓǧǦǩǨȟȞľĽňŇřŘšŠŤťžŽ"); // caron
1528 accents["hat{\\imath}"] = "î";
1529 accents["hat{\\jmath}"] = "ĵ";
1530 buildaccent("hat|^", "aAcCeEgGhHiIjJoOsSuUwWyYzZ",
1531 "âÂĉĈêÊĝĜĥĤîÎĵĴôÔŝŜûÛŵŴŷŶẑẐ"); // circ
1532 accents["bar{\\imath}"] = "ī";
1533 buildaccent("bar|=", "aAeEiIoOuUyY",
1534 "āĀēĒīĪōŌūŪȳȲ"); // macron
1535 accents["tilde{\\imath}"] = "ĩ";
1536 buildaccent("tilde", "aAeEiInNoOuUvVyY",
1537 "ãÃẽẼĩĨñÑõÕũŨṽṼỹỸ"); // tilde
1538 accents["breve{\\imath}"] = "ĭ";
1539 buildaccent("breve|u", "aAeEgGiIoOuU",
1540 "ăĂĕĔğĞĭĬŏŎŭŬ"); // breve
1541 accents["grave{\\imath}"] = "ì";
1542 buildaccent("grave|`", "aAeEiIoOuUnNwWyY",
1543 "àÀèÈìÌòÒùÙǹǸẁẀỳỲ"); // grave
1544 buildaccent("subdot|d", "BbDdHhKkLlMmNnRrSsTtVvWwZzAaEeIiOoUuYy",
1545 "ḄḅḌḍḤḥḲḳḶḷṂṃṆṇṚṛṢṣṬṭṾṿẈẉẒẓẠạẸẹỊịỌọỤụỴỵ"); // dot below
1546 buildaccent("ogonek|k", "AaEeIiUuOo",
1547 "ĄąĘęĮįŲųǪǫ"); // ogonek
1548 buildaccent("cedilla|c", "CcGgKkLlNnRrSsTtEeDdHh",
1549 "ÇçĢģĶķĻļŅņŖŗŞşŢţȨȩḐḑḨḩ"); // cedilla
1550 buildaccent("subring|textsubring", "Aa",
1552 buildaccent("subhat|textsubcircum", "DdEeLlNnTtUu",
1553 "ḒḓḘḙḼḽṊṋṰṱṶṷ"); // subcircum
1554 buildaccent("subtilde|textsubtilde", "EeIiUu",
1555 "ḚḛḬḭṴṵ"); // subtilde
1556 accents["dgrave{\\imath}"] = "ȉ";
1557 accents["textdoublegrave{\\i}"] = "ȉ";
1558 buildaccent("dgrave|textdoublegrave", "AaEeIiOoRrUu",
1559 "ȀȁȄȅȈȉȌȍȐȑȔȕ"); // double grave
1560 accents["rcap{\\imath}"] = "ȉ";
1561 accents["textroundcap{\\i}"] = "ȉ";
1562 buildaccent("rcap|textroundcap", "AaEeIiOoRrUu",
1563 "ȂȃȆȇȊȋȎȏȒȓȖȗ"); // inverted breve
1564 buildaccent("slashed", "oO",
1569 * Created accents in math or regexp environment
1570 * are macros, but we need the utf8 equivalent
1572 void Intervall::removeAccents()
1574 if (accents.empty())
1576 static regex const accre("\\\\(([\\S]|grave|breve|ddot|dot|acute|dacute|mathring|check|hat|bar|tilde|subdot|ogonek|"
1577 "cedilla|subring|textsubring|subhat|textsubcircum|subtilde|textsubtilde|dgrave|textdoublegrave|rcap|textroundcap|slashed)\\{[^\\{\\}]+\\}"
1578 "|((i|imath|jmath|cdot|[a-z]+space)|((backslash )?([lL]y[xX]|[tT]e[xX]|[lL]a[tT]e[xX]e?|lyxarrow))|(brace|guillemot)(left|right)|textasciicircum|mathcircumflex|sim)(?![a-zA-Z]))");
1580 for (sregex_iterator itacc(par.begin(), par.end(), accre), end; itacc != end; ++itacc) {
1582 string key = sub.str(1);
1583 AccentsIterator it_ac = accents.find(key);
1584 if (it_ac != accents.end()) {
1585 string val = it_ac->second;
1586 size_t pos = sub.position(size_t(0));
1587 for (size_t i = 0; i < val.size(); i++) {
1588 par[pos+i] = val[i];
1590 // Remove possibly following space too
1591 if (par[pos+sub.str(0).size()] == ' ')
1592 addIntervall(pos+val.size(), pos + sub.str(0).size()+1);
1594 addIntervall(pos+val.size(), pos + sub.str(0).size());
1595 for (size_t i = pos+val.size(); i < pos + sub.str(0).size(); i++) {
1596 // remove traces of any remaining chars
1601 LYXERR(Debug::INFO, "Not added accent for \"" << key << "\"");
1606 void Intervall::handleOpenP(int i)
1609 depts[actualdeptindex] = i+1;
1610 closes[actualdeptindex] = -1;
1611 checkDepthIndex(actualdeptindex);
1614 void Intervall::handleCloseP(int i, bool closingAllowed)
1616 if (actualdeptindex <= 0) {
1617 if (! closingAllowed)
1618 LYXERR(Debug::FIND, "Bad closing parenthesis in latex"); /* should not happen, but the latex input may be wrong */
1619 // if we are at the very end
1620 addIntervall(i, i+1);
1623 closes[actualdeptindex] = i+1;
1628 void Intervall::resetOpenedP(int openPos)
1630 // Used as initializer for foreignlanguage entry
1631 actualdeptindex = 1;
1632 depts[1] = openPos+1;
1636 int Intervall::previousNotIgnored(int start) const
1638 int idx = 0; /* int intervalls */
1639 for (idx = ignoreidx; idx >= 0; --idx) {
1640 if (start > borders[idx].upper)
1642 if (start >= borders[idx].low)
1643 start = borders[idx].low-1;
1648 int Intervall::nextNotIgnored(int start) const
1650 int idx = 0; /* int intervalls */
1651 for (idx = 0; idx <= ignoreidx; idx++) {
1652 if (start < borders[idx].low)
1654 if (start < borders[idx].upper)
1655 start = borders[idx].upper;
1660 typedef unordered_map<string, KeyInfo> KeysMap;
1661 typedef unordered_map<string, KeyInfo>::const_iterator KeysIterator;
1662 typedef vector< KeyInfo> Entries;
1663 static KeysMap keys = unordered_map<string, KeyInfo>();
1669 Intervall interval_;
1670 void buildKeys(bool);
1671 void buildEntries(bool);
1672 void makeKey(const string &, KeyInfo, bool isPatternString);
1673 void processRegion(int start, int region_end); /* remove {} parts */
1674 void removeHead(KeyInfo const &, int count=0);
1677 LatexInfo(string const & par, bool isPatternString)
1678 : entidx_(-1), interval_(isPatternString, par)
1680 buildKeys(isPatternString);
1681 entries_ = vector<KeyInfo>();
1682 buildEntries(isPatternString);
1686 if (entries_.empty()) {
1689 if (entries_[0].keytype == KeyInfo::isTitle) {
1690 interval_.hasTitle = true;
1691 if (! entries_[0].disabled) {
1692 interval_.titleValue = entries_[0].head;
1695 interval_.titleValue = "";
1697 removeHead(entries_[0]);
1698 if (entries_.size() > 1)
1707 if (int(entries_.size()) > entidx_) {
1714 bool setNextKey(int idx) {
1715 if ((idx == entidx_) && (entidx_ >= 0)) {
1722 int find(int start, KeyInfo::KeyType keytype) const {
1726 while (tmpIdx < int(entries_.size())) {
1727 if (entries_[tmpIdx].keytype == keytype)
1733 int process(ostringstream & os, KeyInfo const & actual);
1734 int dispatch(ostringstream & os, int previousStart, KeyInfo & actual);
1735 // string show(int lastpos) { return interval.show(lastpos);};
1736 int nextNotIgnored(int start) { return interval_.nextNotIgnored(start);};
1737 KeyInfo &getKeyInfo(int keyinfo) {
1738 static KeyInfo invalidInfo = KeyInfo();
1739 if ((keyinfo < 0) || ( keyinfo >= int(entries_.size())))
1742 return entries_[keyinfo];
1744 void setForDefaultLang(KeyInfo const & defLang) {interval_.setForDefaultLang(defLang);};
1745 void addIntervall(int low, int up) { interval_.addIntervall(low, up); };
1749 int Intervall::findclosing(int start, int end, char up = '{', char down = '}', int repeat = 1)
1753 for (int i = start; i < end; i += 1 + skip) {
1757 if (c == '\\') skip = 1;
1761 else if (c == down) {
1764 if ((repeat <= 0) || (par[i+1] != up))
1778 size_t mathpostfixsize;
1780 size_t mathprefixsize;
1784 vector<MathEntry> entries_;
1789 void insert(string const & wait, size_t start, size_t prefixsize, size_t end, size_t postfixsize) {
1790 MathEntry m = MathEntry();
1792 m.mathStart = start;
1793 m.mathprefixsize = prefixsize;
1794 m.mathEnd = end + postfixsize;
1795 m.mathpostfixsize = postfixsize;
1796 m.mathSize = m.mathEnd - m.mathStart;
1797 entries_.push_back(m);
1799 bool empty() const { return entries_.empty(); };
1800 size_t getEndPos() const {
1801 if (entries_.empty() || (actualIdx_ >= entries_.size())) {
1804 return entries_[actualIdx_].mathEnd;
1806 size_t getStartPos() const {
1807 if (entries_.empty() || (actualIdx_ >= entries_.size())) {
1808 return 100000; /* definitely enough? */
1810 return entries_[actualIdx_].mathStart;
1812 size_t getPrefixSize() const {
1813 if (entries_.empty() || (actualIdx_ >= entries_.size())) {
1816 return entries_[actualIdx_].mathprefixsize;
1818 size_t getPostfixSize() const {
1819 if (entries_.empty() || (actualIdx_ >= entries_.size())) {
1822 return entries_[actualIdx_].mathpostfixsize;
1824 size_t getFirstPos() {
1826 return getStartPos();
1828 size_t getSize() const {
1829 if (entries_.empty() || (actualIdx_ >= entries_.size())) {
1832 return entries_[actualIdx_].mathSize;
1834 void incrEntry() { actualIdx_++; };
1837 void LatexInfo::buildEntries(bool isPatternString)
1839 static regex const rmath("(\\\\)*(\\$|\\\\\\[|\\\\\\]|\\\\(begin|end)\\{((eqnarray|equation|flalign|gather|multline|align|alignat)\\*?)\\})");
1840 static regex const rkeys("(\\\\)*(\\$|\\\\\\[|\\\\\\]|\\\\((([a-zA-Z]+\\*?)(\\{([a-z]+\\*?)\\}|=[0-9]+[a-z]+)?)))");
1841 static bool disableLanguageOverride = false;
1842 smatch sub, submath;
1843 bool evaluatingRegexp = false;
1845 bool evaluatingMath = false;
1846 bool evaluatingCode = false;
1848 bool evaluatingOptional = false;
1849 size_t optionalEnd = 0;
1852 bool math_end_waiting = false;
1853 size_t math_pos = 10000;
1854 size_t math_prefix_size = 1;
1856 static vector<string> usedText = vector<string>();
1857 static bool removeMathHull = false;
1859 interval_.removeAccents();
1861 for (sregex_iterator itmath(interval_.par.begin(), interval_.par.end(), rmath), end; itmath != end; ++itmath) {
1863 if ((submath.position(2) - submath.position(0)) %2 == 1) {
1864 // prefixed by odd count of '\\'
1867 if (math_end_waiting) {
1868 size_t pos = submath.position(size_t(2));
1869 if ((math_end == "$") &&
1870 (submath.str(2) == "$")) {
1871 mi.insert("$", math_pos, 1, pos, 1);
1872 math_end_waiting = false;
1874 else if ((math_end == "\\]") &&
1875 (submath.str(2) == "\\]")) {
1876 mi.insert("\\]", math_pos, 2, pos, 2);
1877 math_end_waiting = false;
1879 else if ((submath.str(3).compare("end") == 0) &&
1880 (submath.str(4).compare(math_end) == 0)) {
1881 mi.insert(math_end, math_pos, math_prefix_size, pos, submath.str(2).length());
1882 math_end_waiting = false;
1888 if (submath.str(3).compare("begin") == 0) {
1889 math_end_waiting = true;
1890 math_end = submath.str(4);
1891 math_pos = submath.position(size_t(2));
1892 math_prefix_size = submath.str(2).length();
1894 else if (submath.str(2).compare("\\[") == 0) {
1895 math_end_waiting = true;
1897 math_pos = submath.position(size_t(2));
1899 else if (submath.str(2) == "$") {
1900 size_t pos = submath.position(size_t(2));
1901 math_end_waiting = true;
1907 // Ignore language if there is math somewhere in pattern-string
1908 if (isPatternString) {
1909 for (auto s: usedText) {
1910 // Remove entries created in previous search runs
1913 usedText = vector<string>();
1916 keys["foreignlanguage"].disabled = true;
1917 disableLanguageOverride = true;
1918 removeMathHull = false;
1921 removeMathHull = true; // used later if not isPatternString
1922 disableLanguageOverride = false;
1926 if (disableLanguageOverride) {
1927 keys["foreignlanguage"].disabled = true;
1930 math_pos = mi.getFirstPos();
1931 for (sregex_iterator it(interval_.par.begin(), interval_.par.end(), rkeys), end; it != end; ++it) {
1933 if ((sub.position(2) - sub.position(0)) %2 == 1) {
1934 // prefixed by odd count of '\\'
1937 string key = sub.str(5);
1939 if (sub.str(2)[0] == '\\')
1940 key = sub.str(2)[1];
1945 KeysIterator it_key = keys.find(key);
1946 if (it_key != keys.end()) {
1947 if (it_key->second.keytype == KeyInfo::headRemove) {
1948 KeyInfo found1 = it_key->second;
1949 found1.disabled = true;
1950 found1.head = "\\" + key + "{";
1951 found1._tokenstart = sub.position(size_t(2));
1952 found1._tokensize = found1.head.length();
1953 found1._dataStart = found1._tokenstart + found1.head.length();
1954 int endpos = interval_.findclosing(found1._dataStart, interval_.par.length(), '{', '}', 1);
1955 found1._dataEnd = endpos;
1960 if (evaluatingRegexp) {
1961 if (sub.str(3).compare("endregexp") == 0) {
1962 evaluatingRegexp = false;
1963 // found._tokenstart already set
1964 found._dataEnd = sub.position(size_t(2)) + 13;
1965 found._dataStart = found._dataEnd;
1966 found._tokensize = found._dataEnd - found._tokenstart;
1967 found.parenthesiscount = 0;
1968 found.head = interval_.par.substr(found._tokenstart, found._tokensize);
1975 if (evaluatingMath) {
1976 if (size_t(sub.position(size_t(2))) < mi.getEndPos())
1978 evaluatingMath = false;
1980 math_pos = mi.getStartPos();
1982 if (it_key == keys.end()) {
1983 found = KeyInfo(KeyInfo::isStandard, 0, true);
1984 LYXERR(Debug::INFO, "Undefined key " << key << " ==> will be used as text");
1985 found = KeyInfo(KeyInfo::isText, 0, false);
1986 if (isPatternString) {
1987 found.keytype = KeyInfo::isChar;
1988 found.disabled = false;
1992 usedText.push_back(key);
1996 if (key.compare("regexp") == 0) {
1997 evaluatingRegexp = true;
1998 found._tokenstart = sub.position(size_t(2));
1999 found._tokensize = 0;
2003 // Handle the other params of key
2004 if (found.keytype == KeyInfo::isIgnored)
2006 else if (found.keytype == KeyInfo::isMath) {
2007 if (size_t(sub.position(size_t(2))) == math_pos) {
2009 found._tokenstart = sub.position(size_t(2));
2010 found._tokensize = mi.getSize();
2011 found._dataEnd = found._tokenstart + found._tokensize;
2012 found._dataStart = found._dataEnd;
2013 found.parenthesiscount = 0;
2014 found.head = interval_.par.substr(found._tokenstart, found._tokensize);
2015 if (removeMathHull) {
2016 interval_.addIntervall(found._tokenstart, found._tokenstart + mi.getPrefixSize());
2017 interval_.addIntervall(found._dataEnd - mi.getPostfixSize(), found._dataEnd);
2019 evaluatingMath = true;
2022 // begin|end of unknown env, discard
2023 // First handle tables
2024 // longtable|tabular
2025 bool discardComment;
2027 found.keytype = KeyInfo::doRemove;
2028 if ((sub.str(7).compare("longtable") == 0) ||
2029 (sub.str(7).compare("tabular") == 0)) {
2030 discardComment = true; /* '%' */
2033 discardComment = false;
2034 static regex const removeArgs("^(multicols|multipar|sectionbox|subsectionbox|tcolorbox)$");
2036 string token = sub.str(7);
2037 if (regex_match(token, sub2, removeArgs)) {
2038 found.keytype = KeyInfo::removeWithArg;
2041 // discard spaces before pos(2)
2042 int pos = sub.position(size_t(2));
2044 for (count = 0; pos - count > 0; count++) {
2045 char c = interval_.par[pos-count-1];
2046 if (discardComment) {
2047 if ((c != ' ') && (c != '%'))
2053 found._tokenstart = pos - count;
2054 if (sub.str(3).compare(0, 5, "begin") == 0) {
2055 size_t pos1 = pos + sub.str(2).length();
2056 if (sub.str(7).compare("cjk") == 0) {
2057 pos1 = interval_.findclosing(pos1+1, interval_.par.length()) + 1;
2058 if ((interval_.par[pos1] == '{') && (interval_.par[pos1+1] == '}'))
2060 found.keytype = KeyInfo::isMain;
2061 found._dataStart = pos1;
2062 found._dataEnd = interval_.par.length();
2063 found.disabled = keys["foreignlanguage"].disabled;
2064 found.used = keys["foreignlanguage"].used;
2065 found._tokensize = pos1 - found._tokenstart;
2066 found.head = interval_.par.substr(found._tokenstart, found._tokensize);
2069 // Swallow possible optional params
2070 while (interval_.par[pos1] == '[') {
2071 pos1 = interval_.findclosing(pos1+1, interval_.par.length(), '[', ']')+1;
2073 // Swallow also the eventual parameter
2074 if (interval_.par[pos1] == '{') {
2075 found._dataEnd = interval_.findclosing(pos1+1, interval_.par.length()) + 1;
2078 found._dataEnd = pos1;
2080 found._dataStart = found._dataEnd;
2081 found._tokensize = count + found._dataEnd - pos;
2082 found.parenthesiscount = 0;
2083 found.head = interval_.par.substr(found._tokenstart, found._tokensize);
2084 found.disabled = true;
2088 // Handle "\end{...}"
2089 found._dataStart = pos + sub.str(2).length();
2090 found._dataEnd = found._dataStart;
2091 found._tokensize = count + found._dataEnd - pos;
2092 found.parenthesiscount = 0;
2093 found.head = interval_.par.substr(found._tokenstart, found._tokensize);
2094 found.disabled = true;
2098 else if (found.keytype != KeyInfo::isRegex) {
2099 found._tokenstart = sub.position(size_t(2));
2100 if (found.parenthesiscount == 0) {
2101 // Probably to be discarded
2102 size_t following_pos = sub.position(size_t(2)) + sub.str(5).length() + 1;
2103 char following = interval_.par[following_pos];
2104 if (following == ' ')
2105 found.head = "\\" + sub.str(5) + " ";
2106 else if (following == '=') {
2107 // like \uldepth=1000pt
2108 found.head = sub.str(2);
2111 found.head = "\\" + key;
2112 found._tokensize = found.head.length();
2113 found._dataEnd = found._tokenstart + found._tokensize;
2114 found._dataStart = found._dataEnd;
2117 int params = found._tokenstart + key.length() + 1;
2118 if (evaluatingOptional) {
2119 if (size_t(found._tokenstart) > optionalEnd) {
2120 evaluatingOptional = false;
2123 found.disabled = true;
2126 int optend = params;
2127 while (interval_.par[optend] == '[') {
2128 // discard optional parameters
2129 optend = interval_.findclosing(optend+1, interval_.par.length(), '[', ']') + 1;
2131 if (optend > params) {
2132 key += interval_.par.substr(params, optend-params);
2133 evaluatingOptional = true;
2134 optionalEnd = optend;
2135 if (found.keytype == KeyInfo::isSectioning) {
2136 // Remove optional values (but still keep in header)
2137 interval_.addIntervall(params, optend);
2140 string token = sub.str(7);
2142 if (interval_.par[optend] != '{') {
2144 found.parenthesiscount = 0;
2145 found.head = "\\" + key;
2148 closings = found.parenthesiscount;
2149 if (found.parenthesiscount == 1) {
2150 found.head = "\\" + key + "{";
2152 else if (found.parenthesiscount > 1) {
2154 found.head = sub.str(2) + "{";
2155 closings = found.parenthesiscount - 1;
2158 found.head = "\\" + key + "{";
2161 found._tokensize = found.head.length();
2162 found._dataStart = found._tokenstart + found.head.length();
2163 if (found.keytype == KeyInfo::doRemove) {
2165 size_t endpar = 2 + interval_.findclosing(found._dataStart, interval_.par.length(), '{', '}', closings);
2166 if (endpar >= interval_.par.length())
2167 found._dataStart = interval_.par.length();
2169 found._dataStart = endpar;
2170 found._tokensize = found._dataStart - found._tokenstart;
2173 found._dataStart = found._tokenstart + found._tokensize;
2177 if (interval_.par.substr(found._dataStart, 15).compare("\\endarguments{}") == 0) {
2178 found._dataStart += 15;
2182 endpos = found._dataStart - 1;
2184 endpos = interval_.findclosing(found._dataStart, interval_.par.length(), '{', '}', closings);
2185 if (found.keytype == KeyInfo::isList) {
2186 // Check if it really is list env
2187 static regex const listre("^([a-z]+)$");
2189 if (!regex_match(token, sub2, listre)) {
2190 // Change the key of this entry. It is not in a list/item environment
2191 found.keytype = KeyInfo::endArguments;
2194 if (found.keytype == KeyInfo::noMain) {
2195 evaluatingCode = true;
2197 codeStart = found._dataStart;
2199 else if (evaluatingCode) {
2200 if (size_t(found._dataStart) > codeEnd)
2201 evaluatingCode = false;
2202 else if (found.keytype == KeyInfo::isMain) {
2203 // Disable this key, treate it as standard
2204 found.keytype = KeyInfo::isStandard;
2205 found.disabled = true;
2206 if ((codeEnd +1 >= interval_.par.length()) &&
2207 (found._tokenstart == codeStart)) {
2208 // trickery, because the code inset starts
2209 // with \selectlanguage ...
2211 if (entries_.size() > 1) {
2212 entries_[entries_.size()-1]._dataEnd = codeEnd;
2217 if ((endpos == interval_.par.length()) &&
2218 (found.keytype == KeyInfo::doRemove)) {
2219 // Missing closing => error in latex-input?
2220 // therefore do not delete remaining data
2221 found._dataStart -= 1;
2222 found._dataEnd = found._dataStart;
2225 found._dataEnd = endpos;
2227 if (isPatternString) {
2228 keys[key].used = true;
2231 entries_.push_back(found);
2235 void LatexInfo::makeKey(const string &keysstring, KeyInfo keyI, bool isPatternString)
2237 stringstream s(keysstring);
2239 const char delim = '|';
2240 while (getline(s, key, delim)) {
2241 KeyInfo keyII(keyI);
2242 if (isPatternString) {
2245 else if ( !keys[key].used)
2246 keyII.disabled = true;
2251 void LatexInfo::buildKeys(bool isPatternString)
2254 static bool keysBuilt = false;
2255 if (keysBuilt && !isPatternString) return;
2257 // Keys to ignore in any case
2258 makeKey("text|textcyrillic|lyxmathsym|ensuremath", KeyInfo(KeyInfo::headRemove, 1, true), true);
2259 // Known standard keys with 1 parameter.
2260 // Split is done, if not at start of region
2261 makeKey("textsf|textss|texttt", KeyInfo(KeyInfo::isStandard, 1, ignoreFormats.getFamily()), isPatternString);
2262 makeKey("textbf", KeyInfo(KeyInfo::isStandard, 1, ignoreFormats.getSeries()), isPatternString);
2263 makeKey("textit|textsc|textsl", KeyInfo(KeyInfo::isStandard, 1, ignoreFormats.getShape()), isPatternString);
2264 makeKey("uuline|uline|uwave", KeyInfo(KeyInfo::isStandard, 1, ignoreFormats.getUnderline()), isPatternString);
2265 makeKey("emph|noun", KeyInfo(KeyInfo::isStandard, 1, ignoreFormats.getMarkUp()), isPatternString);
2266 makeKey("sout|xout", KeyInfo(KeyInfo::isStandard, 1, ignoreFormats.getStrikeOut()), isPatternString);
2268 makeKey("section|subsection|subsubsection|paragraph|subparagraph|minisec",
2269 KeyInfo(KeyInfo::isSectioning, 1, ignoreFormats.getSectioning()), isPatternString);
2270 makeKey("section*|subsection*|subsubsection*|paragraph*",
2271 KeyInfo(KeyInfo::isSectioning, 1, ignoreFormats.getSectioning()), isPatternString);
2272 makeKey("part|part*|chapter|chapter*", KeyInfo(KeyInfo::isSectioning, 1, ignoreFormats.getSectioning()), isPatternString);
2273 makeKey("title|subtitle|author|subject|publishers|dedication|uppertitleback|lowertitleback|extratitle|lyxaddress|lyxrightaddress", KeyInfo(KeyInfo::isTitle, 1, ignoreFormats.getFrontMatter()), isPatternString);
2275 makeKey("regexp", KeyInfo(KeyInfo::isRegex, 1, false), isPatternString);
2277 // Split is done, if not at start of region
2278 makeKey("textcolor", KeyInfo(KeyInfo::isStandard, 2, ignoreFormats.getColor()), isPatternString);
2279 makeKey("latexenvironment", KeyInfo(KeyInfo::isStandard, 2, false), isPatternString);
2281 // Split is done always.
2282 makeKey("foreignlanguage", KeyInfo(KeyInfo::isMain, 2, ignoreFormats.getLanguage()), isPatternString);
2286 makeKey("backslash|textbackslash|slash", KeyInfo(KeyInfo::isChar, 0, false), isPatternString);
2287 makeKey("textasciicircum|textasciitilde", KeyInfo(KeyInfo::isChar, 0, false), isPatternString);
2288 makeKey("textasciiacute|texemdash", KeyInfo(KeyInfo::isChar, 0, false), isPatternString);
2289 makeKey("dots|ldots", KeyInfo(KeyInfo::isChar, 0, false), isPatternString);
2291 makeKey("quad|qquad|hfill|dotfill", KeyInfo(KeyInfo::isChar, 0, false), isPatternString);
2292 makeKey("textvisiblespace|nobreakspace", KeyInfo(KeyInfo::isChar, 0, false), isPatternString);
2293 makeKey("negthickspace|negmedspace|negthinspace", KeyInfo(KeyInfo::isChar, 0, false), isPatternString);
2294 makeKey("thickspace|medspace|thinspace", KeyInfo(KeyInfo::isChar, 0, false), isPatternString);
2296 // makeKey("enskip|smallskip|medskip|bigskip|vfill", KeyInfo(KeyInfo::isChar, 0, false), isPatternString);
2297 // Custom space/skip, remove the content (== length value)
2298 makeKey("vspace|vspace*|hspace|hspace*|mspace", KeyInfo(KeyInfo::noContent, 1, false), isPatternString);
2299 // Found in fr/UserGuide.lyx
2300 makeKey("og|fg", KeyInfo(KeyInfo::isChar, 0, false), isPatternString);
2302 makeKey("textquotedbl|quotesinglbase|lyxarrow", KeyInfo(KeyInfo::isChar, 0, false), isPatternString);
2303 makeKey("textquotedblleft|textquotedblright", KeyInfo(KeyInfo::isChar, 0, false), isPatternString);
2304 // Known macros to remove (including their parameter)
2306 makeKey("input|inputencoding|label|ref|index|bibitem", KeyInfo(KeyInfo::doRemove, 1, false), isPatternString);
2307 makeKey("addtocounter|setlength", KeyInfo(KeyInfo::noContent, 2, true), isPatternString);
2308 // handle like standard keys with 1 parameter.
2309 makeKey("url|href|vref|thanks", KeyInfo(KeyInfo::isStandard, 1, false), isPatternString);
2311 // Ignore deleted text
2312 makeKey("lyxdeleted", KeyInfo(KeyInfo::doRemove, 3, false), isPatternString);
2313 // but preserve added text
2314 makeKey("lyxadded", KeyInfo(KeyInfo::doRemove, 2, false), isPatternString);
2316 // Macros to remove, but let the parameter survive
2318 makeKey("menuitem|textmd|textrm", KeyInfo(KeyInfo::isStandard, 1, true), isPatternString);
2320 // Remove language spec from content of these insets
2321 makeKey("code", KeyInfo(KeyInfo::noMain, 1, false), isPatternString);
2323 // Same effect as previous, parameter will survive (because there is no one anyway)
2325 makeKey("noindent|textcompwordmark|maketitle", KeyInfo(KeyInfo::isStandard, 0, true), isPatternString);
2326 // Remove table decorations
2327 makeKey("hline|tabularnewline|toprule|bottomrule|midrule", KeyInfo(KeyInfo::doRemove, 0, true), isPatternString);
2328 // Discard shape-header.
2329 // For footnote or shortcut too, because of lang settings
2330 // and wrong handling if used 'KeyInfo::noMain'
2331 makeKey("circlepar|diamondpar|heartpar|nutpar", KeyInfo(KeyInfo::isStandard, 1, true), isPatternString);
2332 makeKey("trianglerightpar|hexagonpar|starpar", KeyInfo(KeyInfo::isStandard, 1, true), isPatternString);
2333 makeKey("triangleuppar|triangledownpar|droppar", KeyInfo(KeyInfo::isStandard, 1, true), isPatternString);
2334 makeKey("triangleleftpar|shapepar|dropuppar", KeyInfo(KeyInfo::isStandard, 1, true), isPatternString);
2335 makeKey("hphantom|vphantom|footnote|shortcut|include|includegraphics", KeyInfo(KeyInfo::isStandard, 1, true), isPatternString);
2336 makeKey("parbox", KeyInfo(KeyInfo::doRemove, 1, true), isPatternString);
2337 // like ('tiny{}' or '\tiny ' ... )
2338 makeKey("footnotesize|tiny|scriptsize|small|large|Large|LARGE|huge|Huge", KeyInfo(KeyInfo::isSize, 0, false), isPatternString);
2340 // Survives, like known character
2341 // makeKey("lyx|LyX|latex|LaTeX|latexe|LaTeXe|tex|TeX", KeyInfo(KeyInfo::isChar, 0, false), isPatternString);
2342 makeKey("tableofcontents", KeyInfo(KeyInfo::isChar, 0, false), isPatternString);
2343 makeKey("item|listitem", KeyInfo(KeyInfo::isList, 1, false), isPatternString);
2345 makeKey("begin|end", KeyInfo(KeyInfo::isMath, 1, false), isPatternString);
2346 makeKey("[|]", KeyInfo(KeyInfo::isMath, 1, false), isPatternString);
2347 makeKey("$", KeyInfo(KeyInfo::isMath, 1, false), isPatternString);
2349 makeKey("par|uldepth|ULdepth|protect|nobreakdash|medskip|relax", KeyInfo(KeyInfo::isStandard, 0, true), isPatternString);
2350 // Remove RTL/LTR marker
2351 makeKey("l|r|textlr|textfr|textar|beginl|endl", KeyInfo(KeyInfo::isStandard, 0, true), isPatternString);
2352 makeKey("lettrine", KeyInfo(KeyInfo::cleanToStart, 0, true), isPatternString);
2353 makeKey("lyxslide", KeyInfo(KeyInfo::isSectioning, 1, true), isPatternString);
2354 makeKey("endarguments", KeyInfo(KeyInfo::endArguments, 0, true), isPatternString);
2355 makeKey("twocolumn", KeyInfo(KeyInfo::removeWithArg, 2, true), isPatternString);
2356 makeKey("tnotetext|ead|fntext|cortext|address", KeyInfo(KeyInfo::removeWithArg, 0, true), isPatternString);
2357 makeKey("lyxend", KeyInfo(KeyInfo::isStandard, 0, true), isPatternString);
2358 if (isPatternString) {
2359 // Allow the first searched string to rebuild the keys too
2363 // no need to rebuild again
2369 * Keep the list of actual opened parentheses actual
2370 * (e.g. depth == 4 means there are 4 '{' not processed yet)
2372 void Intervall::handleParentheses(int lastpos, bool closingAllowed)
2375 for (int i = depts[actualdeptindex]; i < lastpos; i+= 1 + skip) {
2379 if (c == '\\') skip = 1;
2380 else if (c == '{') {
2383 else if (c == '}') {
2384 handleCloseP(i, closingAllowed);
2390 string Intervall::show(int lastpos)
2392 int idx = 0; /* int intervalls */
2395 for (idx = 0; idx <= ignoreidx; idx++) {
2396 while (i < lastpos) {
2398 if (i <= borders[idx].low) {
2399 if (borders[idx].low > lastpos)
2400 printsize = lastpos - i;
2402 printsize = borders[idx].low - i;
2403 s += par.substr(i, printsize);
2405 if (i >= borders[idx].low)
2406 i = borders[idx].upper;
2409 i = borders[idx].upper;
2415 s += par.substr(i, lastpos-i);
2421 void Intervall::output(ostringstream &os, int lastpos)
2423 // get number of chars to output
2424 int idx = 0; /* int intervalls */
2427 string startTitle = titleValue;
2428 for (idx = 0; idx <= ignoreidx; idx++) {
2430 if (i <= borders[idx].low) {
2432 if (borders[idx].low > lastpos)
2433 printsize = lastpos - i;
2435 printsize = borders[idx].low - i;
2436 if (printsize > 0) {
2437 os << startTitle << par.substr(i, printsize);
2439 printed += printsize;
2442 handleParentheses(i, false);
2443 if (i >= borders[idx].low)
2444 i = borders[idx].upper;
2447 i = borders[idx].upper;
2454 os << startTitle << par.substr(i, lastpos-i);
2455 printed += lastpos-i;
2457 handleParentheses(lastpos, false);
2459 if (keys["foreignlanguage"].disabled)
2460 startindex = actualdeptindex-langcount;
2462 startindex = actualdeptindex;
2463 for (int i = startindex; i > 0; --i) {
2466 if (hasTitle && (printed > 0))
2468 if (! isPatternString_)
2470 handleParentheses(lastpos, true); /* extra closings '}' allowed here */
2473 void LatexInfo::processRegion(int start, int region_end)
2475 while (start < region_end) { /* Let {[} and {]} survive */
2476 int cnt = interval_.isOpeningPar(start);
2478 // Closing is allowed past the region
2479 int closing = interval_.findclosing(start+1, interval_.par.length());
2480 interval_.addIntervall(start, start+1);
2481 interval_.addIntervall(closing, closing+1);
2485 start = interval_.nextNotIgnored(start+1);
2489 void LatexInfo::removeHead(KeyInfo const & actual, int count)
2491 if (actual.parenthesiscount == 0) {
2492 // "{\tiny{} ...}" ==> "{{} ...}"
2493 interval_.addIntervall(actual._tokenstart-count, actual._tokenstart + actual._tokensize);
2496 // Remove header hull, that is "\url{abcd}" ==> "abcd"
2497 interval_.addIntervall(actual._tokenstart - count, actual._dataStart);
2498 interval_.addIntervall(actual._dataEnd, actual._dataEnd+1);
2502 int LatexInfo::dispatch(ostringstream &os, int previousStart, KeyInfo &actual)
2505 switch (actual.keytype)
2507 case KeyInfo::isTitle: {
2509 nextKeyIdx = getNextKey();
2512 case KeyInfo::cleanToStart: {
2513 actual._dataEnd = actual._dataStart;
2514 nextKeyIdx = getNextKey();
2515 // Search for end of arguments
2516 int tmpIdx = find(nextKeyIdx, KeyInfo::endArguments);
2518 for (int i = nextKeyIdx; i <= tmpIdx; i++) {
2519 entries_[i].disabled = true;
2521 actual._dataEnd = entries_[tmpIdx]._dataEnd;
2523 while (interval_.par[actual._dataEnd] == ' ')
2525 interval_.addIntervall(0, actual._dataEnd+1);
2526 interval_.actualdeptindex = 0;
2527 interval_.depts[0] = actual._dataEnd+1;
2528 interval_.closes[0] = -1;
2531 case KeyInfo::isText:
2532 interval_.par[actual._tokenstart] = '#';
2533 //interval_.addIntervall(actual._tokenstart, actual._tokenstart+1);
2534 nextKeyIdx = getNextKey();
2536 case KeyInfo::noContent: { /* char like "\hspace{2cm}" */
2537 if (actual.disabled)
2538 interval_.addIntervall(actual._tokenstart, actual._dataEnd);
2540 interval_.addIntervall(actual._dataStart, actual._dataEnd);
2543 case KeyInfo::isChar: {
2544 nextKeyIdx = getNextKey();
2547 case KeyInfo::isSize: {
2548 if (actual.disabled || (interval_.par[actual._dataStart] != '{') || (interval_.par[actual._dataStart-1] == ' ')) {
2549 if (actual.parenthesiscount == 0)
2550 interval_.addIntervall(actual._tokenstart, actual._dataEnd);
2552 interval_.addIntervall(actual._tokenstart, actual._dataEnd+1);
2554 nextKeyIdx = getNextKey();
2556 // Here _dataStart points to '{', so correct it
2557 actual._dataStart += 1;
2558 actual._tokensize += 1;
2559 actual.parenthesiscount = 1;
2560 if (interval_.par[actual._dataStart] == '}') {
2561 // Determine the end if used like '{\tiny{}...}'
2562 actual._dataEnd = interval_.findclosing(actual._dataStart+1, interval_.par.length()) + 1;
2563 interval_.addIntervall(actual._dataStart, actual._dataStart+1);
2566 // Determine the end if used like '\tiny{...}'
2567 actual._dataEnd = interval_.findclosing(actual._dataStart, interval_.par.length()) + 1;
2569 // Split on this key if not at start
2570 int start = interval_.nextNotIgnored(previousStart);
2571 if (start < actual._tokenstart) {
2572 interval_.output(os, actual._tokenstart);
2573 interval_.addIntervall(start, actual._tokenstart);
2575 // discard entry if at end of actual
2576 nextKeyIdx = process(os, actual);
2580 case KeyInfo::endArguments: {
2581 // Remove trailing '{}' too
2582 actual._dataStart += 1;
2583 actual._dataEnd += 1;
2584 interval_.addIntervall(actual._tokenstart, actual._dataEnd+1);
2585 nextKeyIdx = getNextKey();
2588 case KeyInfo::noMain:
2590 case KeyInfo::isStandard: {
2591 if (actual.disabled) {
2593 processRegion(actual._dataStart, actual._dataStart+1);
2594 nextKeyIdx = getNextKey();
2596 // Split on this key if not at datastart of calling entry
2597 int start = interval_.nextNotIgnored(previousStart);
2598 if (start < actual._tokenstart) {
2599 interval_.output(os, actual._tokenstart);
2600 interval_.addIntervall(start, actual._tokenstart);
2602 // discard entry if at end of actual
2603 nextKeyIdx = process(os, actual);
2607 case KeyInfo::removeWithArg: {
2608 nextKeyIdx = getNextKey();
2609 // Search for end of arguments
2610 int tmpIdx = find(nextKeyIdx, KeyInfo::endArguments);
2612 for (int i = nextKeyIdx; i <= tmpIdx; i++) {
2613 entries_[i].disabled = true;
2615 actual._dataEnd = entries_[tmpIdx]._dataEnd;
2617 interval_.addIntervall(actual._tokenstart, actual._dataEnd+1);
2620 case KeyInfo::doRemove: {
2621 // Remove the key with all parameters and following spaces
2624 if (interval_.par[actual._dataEnd-1] == ' ')
2625 start = actual._dataEnd;
2627 start = actual._dataEnd+1;
2628 for (pos = start; pos < interval_.par.length(); pos++) {
2629 if ((interval_.par[pos] != ' ') && (interval_.par[pos] != '%'))
2632 // Remove also enclosing parentheses [] and {}
2635 while (actual._tokenstart > numpars) {
2636 if (pos+numpars >= interval_.par.size())
2638 else if (interval_.par[pos+numpars] == ']' && interval_.par[actual._tokenstart-numpars-1] == '[')
2640 else if (interval_.par[pos+numpars] == '}' && interval_.par[actual._tokenstart-numpars-1] == '{')
2646 if (interval_.par[pos+numpars] == ' ')
2650 interval_.addIntervall(actual._tokenstart-numpars, pos+numpars+spaces);
2651 nextKeyIdx = getNextKey();
2654 case KeyInfo::isList: {
2655 // Discard space before _tokenstart
2657 for (count = 0; count < actual._tokenstart; count++) {
2658 if (interval_.par[actual._tokenstart-count-1] != ' ')
2661 nextKeyIdx = getNextKey();
2662 int tmpIdx = find(nextKeyIdx, KeyInfo::endArguments);
2664 // Special case: \item is not a list, but a command (like in Style Author_Biography in maa-monthly.layout)
2666 // How else can we catch this one?
2667 for (int i = nextKeyIdx; i <= tmpIdx; i++) {
2668 entries_[i].disabled = true;
2670 actual._dataEnd = entries_[tmpIdx]._dataEnd;
2672 else if (nextKeyIdx > 0) {
2673 // Ignore any lang entries inside data region
2674 for (int i = nextKeyIdx; i < int(entries_.size()) && entries_[i]._tokenstart < actual._dataEnd; i++) {
2675 if (entries_[i].keytype == KeyInfo::isMain)
2676 entries_[i].disabled = true;
2679 if (actual.disabled) {
2680 interval_.addIntervall(actual._tokenstart-count, actual._dataEnd+1);
2683 interval_.addIntervall(actual._tokenstart-count, actual._tokenstart);
2685 if (interval_.par[actual._dataEnd+1] == '[') {
2686 int posdown = interval_.findclosing(actual._dataEnd+2, interval_.par.length(), '[', ']');
2687 if ((interval_.par[actual._dataEnd+2] == '{') &&
2688 (interval_.par[posdown-1] == '}')) {
2689 interval_.addIntervall(actual._dataEnd+1,actual._dataEnd+3);
2690 interval_.addIntervall(posdown-1, posdown+1);
2693 interval_.addIntervall(actual._dataEnd+1, actual._dataEnd+2);
2694 interval_.addIntervall(posdown, posdown+1);
2696 int blk = interval_.nextNotIgnored(actual._dataEnd+1);
2697 if (blk > posdown) {
2698 // Discard at most 1 space after empty item
2700 for (count = 0; count < 1; count++) {
2701 if (interval_.par[blk+count] != ' ')
2705 interval_.addIntervall(blk, blk+count);
2710 case KeyInfo::isSectioning: {
2711 // Discard spaces before _tokenstart
2713 int val = actual._tokenstart;
2714 for (count = 0; count < actual._tokenstart;) {
2715 val = interval_.previousNotIgnored(val-1);
2716 if (val < 0 || interval_.par[val] != ' ')
2719 count = actual._tokenstart - val;
2722 if (actual.disabled) {
2723 removeHead(actual, count);
2724 nextKeyIdx = getNextKey();
2726 interval_.addIntervall(actual._tokenstart-count, actual._tokenstart);
2727 nextKeyIdx = process(os, actual);
2731 case KeyInfo::isMath: {
2732 // Same as regex, use the content unchanged
2733 nextKeyIdx = getNextKey();
2736 case KeyInfo::isRegex: {
2737 // DO NOT SPLIT ON REGEX
2739 nextKeyIdx = getNextKey();
2742 case KeyInfo::isIgnored: {
2743 // Treat like a character for now
2744 nextKeyIdx = getNextKey();
2747 case KeyInfo::isMain: {
2748 if (interval_.par.substr(actual._dataStart, 2) == "% ")
2749 interval_.addIntervall(actual._dataStart, actual._dataStart+2);
2750 if (actual._tokenstart > 0) {
2751 int prev = interval_.previousNotIgnored(actual._tokenstart - 1);
2752 if ((prev >= 0) && interval_.par[prev] == '%')
2753 interval_.addIntervall(prev, prev+1);
2755 if (actual.disabled) {
2757 interval_.langcount++;
2758 if ((interval_.par.substr(actual._dataStart, 3) == " \\[") ||
2759 (interval_.par.substr(actual._dataStart, 8) == " \\begin{")) {
2760 // Discard also the space before math-equation
2761 interval_.addIntervall(actual._dataStart, actual._dataStart+1);
2763 nextKeyIdx = getNextKey();
2764 // interval.resetOpenedP(actual._dataStart-1);
2767 if (actual._tokenstart < 26) {
2768 // for the first (and maybe dummy) language
2769 interval_.setForDefaultLang(actual);
2771 interval_.resetOpenedP(actual._dataStart-1);
2775 case KeyInfo::invalid:
2776 case KeyInfo::headRemove:
2777 // These two cases cannot happen, already handled
2780 // LYXERR(Debug::INFO, "Unhandled keytype");
2781 nextKeyIdx = getNextKey();
2788 int LatexInfo::process(ostringstream & os, KeyInfo const & actual )
2790 int end = interval_.nextNotIgnored(actual._dataEnd);
2791 int oldStart = actual._dataStart;
2792 int nextKeyIdx = getNextKey();
2794 if ((nextKeyIdx < 0) ||
2795 (entries_[nextKeyIdx]._tokenstart >= actual._dataEnd) ||
2796 (entries_[nextKeyIdx].keytype == KeyInfo::invalid)) {
2797 if (oldStart <= end) {
2798 processRegion(oldStart, end);
2803 KeyInfo &nextKey = getKeyInfo(nextKeyIdx);
2805 if ((nextKey.keytype == KeyInfo::isMain) && !nextKey.disabled) {
2806 (void) dispatch(os, actual._dataStart, nextKey);
2807 end = nextKey._tokenstart;
2810 processRegion(oldStart, nextKey._tokenstart);
2811 nextKeyIdx = dispatch(os, actual._dataStart, nextKey);
2813 oldStart = nextKey._dataEnd+1;
2815 // now nextKey is either invalid or is outside of actual._dataEnd
2816 // output the remaining and discard myself
2817 if (oldStart <= end) {
2818 processRegion(oldStart, end);
2820 if (interval_.par.size() > (size_t) end && interval_.par[end] == '}') {
2822 // This is the normal case.
2823 // But if using the firstlanguage, the closing may be missing
2825 // get minimum of 'end' and 'actual._dataEnd' in case that the nextKey.keytype was 'KeyInfo::isMain'
2827 if (actual._dataEnd < end)
2828 output_end = interval_.nextNotIgnored(actual._dataEnd);
2829 else if (interval_.par.size() > (size_t) end)
2830 output_end = interval_.nextNotIgnored(end);
2832 output_end = interval_.par.size();
2833 if ((actual.keytype == KeyInfo::isMain) && actual.disabled) {
2834 interval_.addIntervall(actual._tokenstart, actual._tokenstart+actual._tokensize);
2836 // Remove possible empty data
2837 int dstart = interval_.nextNotIgnored(actual._dataStart);
2838 while (interval_.isOpeningPar(dstart) == 1) {
2839 interval_.addIntervall(dstart, dstart+1);
2840 int dend = interval_.findclosing(dstart+1, output_end);
2841 interval_.addIntervall(dend, dend+1);
2842 dstart = interval_.nextNotIgnored(dstart+1);
2844 if (dstart < output_end)
2845 interval_.output(os, output_end);
2847 interval_.addIntervall(0, end);
2849 interval_.addIntervall(actual._tokenstart, end);
2853 string splitOnKnownMacros(string par, bool isPatternString)
2856 LatexInfo li(par, isPatternString);
2857 // LYXERR(Debug::INFO, "Berfore split: " << par);
2858 KeyInfo DummyKey = KeyInfo(KeyInfo::KeyType::isMain, 2, true);
2860 DummyKey._tokensize = 0;
2861 DummyKey._dataStart = 0;
2862 DummyKey._dataEnd = par.length();
2863 DummyKey.disabled = true;
2864 int firstkeyIdx = li.getFirstKey();
2866 if (firstkeyIdx >= 0) {
2867 KeyInfo firstKey = li.getKeyInfo(firstkeyIdx);
2868 DummyKey._tokenstart = firstKey._tokenstart;
2870 if ((firstKey.keytype != KeyInfo::isMain) || firstKey.disabled) {
2871 // Use dummy firstKey
2872 firstKey = DummyKey;
2873 (void) li.setNextKey(firstkeyIdx);
2876 if (par.substr(firstKey._dataStart, 2) == "% ")
2877 li.addIntervall(firstKey._dataStart, firstKey._dataStart+2);
2879 nextkeyIdx = li.process(os, firstKey);
2880 while (nextkeyIdx >= 0) {
2881 // Check for a possible gap between the last
2882 // entry and this one
2883 int datastart = li.nextNotIgnored(firstKey._dataStart);
2884 KeyInfo &nextKey = li.getKeyInfo(nextkeyIdx);
2885 if ((nextKey._tokenstart > datastart)) {
2887 firstKey._dataStart = datastart;
2888 firstKey._dataEnd = par.length();
2889 (void) li.setNextKey(nextkeyIdx);
2890 // Fake the last opened parenthesis
2891 li.setForDefaultLang(firstKey);
2892 nextkeyIdx = li.process(os, firstKey);
2895 if (nextKey.keytype != KeyInfo::isMain) {
2896 firstKey._dataStart = datastart;
2897 firstKey._dataEnd = nextKey._dataEnd+1;
2898 (void) li.setNextKey(nextkeyIdx);
2899 li.setForDefaultLang(firstKey);
2900 nextkeyIdx = li.process(os, firstKey);
2903 nextkeyIdx = li.process(os, nextKey);
2907 // Handle the remaining
2908 firstKey._dataStart = li.nextNotIgnored(firstKey._dataStart);
2909 firstKey._dataEnd = par.length();
2911 if ((firstKey._dataStart < firstKey._dataEnd) &&
2912 (par[firstKey._dataStart] != '}')) {
2913 li.setForDefaultLang(firstKey);
2914 (void) li.process(os, firstKey);
2918 // return string definitelly impossible to match
2919 s = "\\foreignlanguage{ignore}{ }";
2923 s = par; /* no known macros found */
2924 // LYXERR(Debug::INFO, "After split: " << s);
2929 * Try to unify the language specs in the latexified text.
2930 * Resulting modified string is set to "", if
2931 * the searched tex does not contain all the features in the search pattern
2933 static string correctlanguagesetting(string par, bool isPatternString, bool withformat, lyx::Buffer *pbuf = nullptr)
2935 static Features regex_f;
2936 static int missed = 0;
2937 static bool regex_with_format = false;
2939 int parlen = par.length();
2941 while ((parlen > 0) && (par[parlen-1] == '\n')) {
2944 if (isPatternString && (parlen > 0) && (par[parlen-1] == '~')) {
2945 // Happens to be there in case of description or labeling environment
2950 // Split the latex input into pieces which
2951 // can be digested by our search engine
2952 LYXERR(Debug::FIND, "input: \"" << par << "\"");
2953 if (isPatternString && (pbuf != nullptr)) { // Check if we should disable/enable test for language
2954 // We check for polyglossia, because in runparams.flavor we use Flavor::XeTeX
2955 string doclang = pbuf->params().language->polyglossia();
2956 static regex langre("\\\\(foreignlanguage)\\{([^\\}]+)\\}");
2958 bool toIgnoreLang = true;
2959 for (sregex_iterator it(par.begin(), par.end(), langre), end; it != end; ++it) {
2961 if (sub.str(2) != doclang) {
2962 toIgnoreLang = false;
2966 setIgnoreFormat("language", toIgnoreLang, false);
2969 result = splitOnKnownMacros(par.substr(0,parlen), isPatternString);
2970 LYXERR(Debug::FIND, "After splitOnKnownMacros:\n\"" << result << "\"");
2973 result = par.substr(0, parlen);
2974 if (isPatternString) {
2977 regex_f = identifyFeatures(result);
2978 string features = "";
2979 for (auto it = regex_f.cbegin(); it != regex_f.cend(); ++it) {
2980 string a = it->first;
2981 regex_with_format = true;
2982 features += " " + a;
2983 // LYXERR(Debug::INFO, "Identified regex format:" << a);
2985 LYXERR(Debug::FIND, "Identified Features" << features);
2988 } else if (regex_with_format) {
2989 Features info = identifyFeatures(result);
2990 for (auto it = regex_f.cbegin(); it != regex_f.cend(); ++it) {
2991 string a = it->first;
2992 bool b = it->second;
2993 if (b && ! info[a]) {
2995 LYXERR(Debug::FIND, "Missed(" << missed << " " << a <<", srclen = " << parlen );
3002 // LYXERR(Debug::INFO, "No regex formats");
3008 // Remove trailing closure of math, macros and environments, so to catch parts of them.
3009 static int identifyClosing(string & t)
3011 int open_braces = 0;
3013 LYXERR(Debug::FIND, "identifyClosing(): t now is '" << t << "'");
3014 if (regex_replace(t, t, "(.*[^\\\\])\\$$", "$1"))
3016 if (regex_replace(t, t, "(.*[^\\\\])\\\\\\]$", "$1"))
3018 if (regex_replace(t, t, "(.*[^\\\\])\\\\end\\{[a-zA-Z_]*\\*?\\}$", "$1"))
3020 if (regex_replace(t, t, "(.*[^\\\\])\\}$", "$1")) {
3029 static int num_replaced = 0;
3030 static bool previous_single_replace = true;
3032 void MatchStringAdv::CreateRegexp(FindAndReplaceOptions const & opt, string regexp_str, string regexp2_str, string par_as_string)
3035 // Handle \w properly
3036 QRegularExpression::PatternOptions popts = QRegularExpression::UseUnicodePropertiesOption | QRegularExpression::MultilineOption;
3037 if (! opt.casesensitive) {
3038 popts |= QRegularExpression::CaseInsensitiveOption;
3040 regexp = QRegularExpression(QString::fromStdString(regexp_str), popts);
3041 regexp2 = QRegularExpression(QString::fromStdString(regexp2_str), popts);
3043 if (regexp.isValid() && regexp2.isValid()) {
3044 regexIsValid = true;
3045 // Check '{', '}' pairs inside the regex
3048 for (unsigned i = 0; i < par_as_string.size(); i+= skip) {
3049 char c = par_as_string[i];
3056 else if (c == '}') {
3063 if (balanced != 0) {
3064 regexIsValid = false;
3065 regexError = "Unbalanced curly brackets in regexp \"" + regexp_str + "\"";
3069 regexIsValid = false;
3070 if (!regexp.isValid())
3071 regexError += "Invalid regexp \"" + regexp_str + "\", error = " + regexp.errorString().toStdString();
3073 regexError += "Invalid regexp2 \"" + regexp2_str + "\", error = " + regexp2.errorString().toStdString();
3076 if (opt.casesensitive) {
3077 regexp = regex(regexp_str);
3078 regexp2 = regex(regexp2_str);
3081 regexp = regex(regexp_str, std::regex_constants::icase);
3082 regexp2 = regex(regexp2_str, std::regex_constants::icase);
3087 static void modifyRegexForMatchWord(string &t)
3090 regex wordre("(\\\\)*((\\.|\\\\b))");
3093 for (sregex_iterator it(t.begin(), t.end(), wordre), end; it != end; ++it) {
3095 if ((sub.position(2) - sub.position(0)) % 2 == 1) {
3098 else if (sub.str(2) == "\\\\b")
3100 if (lastpos < (size_t) sub.position(2))
3101 s += t.substr(lastpos, sub.position(2) - lastpos);
3103 lastpos = sub.position(2) + sub.length(2);
3106 s = "\\b" + t + "\\b";
3110 else if (lastpos < t.length())
3111 s += t.substr(lastpos, t.length() - lastpos);
3112 t = "\\b" + s + "\\b";
3115 MatchStringAdv::MatchStringAdv(lyx::Buffer & buf, FindAndReplaceOptions & opt)
3116 : p_buf(&buf), p_first_buf(&buf), opt(opt)
3118 Buffer & find_buf = *theBufferList().getBuffer(FileName(to_utf8(opt.find_buf_name)), true);
3119 docstring const & ds = stringifySearchBuffer(find_buf, opt);
3120 use_regexp = lyx::to_utf8(ds).find("\\regexp{") != std::string::npos;
3121 if (opt.replace_all && previous_single_replace) {
3122 previous_single_replace = false;
3125 else if (!opt.replace_all) {
3126 num_replaced = 0; // count number of replaced strings
3127 previous_single_replace = true;
3129 // When using regexp, braces are hacked already by escape_for_regex()
3130 par_as_string = normalize(ds);
3132 close_wildcards = 0;
3134 size_t lead_size = 0;
3135 // correct the language settings
3136 par_as_string = correctlanguagesetting(par_as_string, true, !opt.ignoreformat, &buf);
3137 opt.matchAtStart = false;
3139 identifyClosing(par_as_string); // Removes math closings ($, ], ...) at end of string
3140 if (opt.ignoreformat) {
3144 lead_size = identifyLeading(par_as_string);
3146 lead_as_string = par_as_string.substr(0, lead_size);
3147 string lead_as_regex_string = string2regex(lead_as_string);
3148 par_as_string_nolead = par_as_string.substr(lead_size, par_as_string.size() - lead_size);
3149 string par_as_regex_string_nolead = string2regex(par_as_string_nolead);
3150 /* Handle whole words too in this case
3152 if (opt.matchword) {
3153 par_as_regex_string_nolead = "\\b" + par_as_regex_string_nolead + "\\b";
3154 opt.matchword = false;
3156 string regexp_str = "(" + lead_as_regex_string + ")()" + par_as_regex_string_nolead;
3157 string regexp2_str = "(" + lead_as_regex_string + ")(.*?)" + par_as_regex_string_nolead;
3158 CreateRegexp(opt, regexp_str, regexp2_str);
3160 LYXERR(Debug::FIND, "Setting regexp to : '" << regexp_str << "'");
3161 LYXERR(Debug::FIND, "Setting regexp2 to: '" << regexp2_str << "'");
3165 if (!opt.ignoreformat) {
3166 lead_size = identifyLeading(par_as_string);
3167 LYXERR(Debug::FIND, "Lead_size: " << lead_size);
3168 lead_as_string = par_as_string.substr(0, lead_size);
3169 par_as_string_nolead = par_as_string.substr(lead_size, par_as_string.size() - lead_size);
3172 // Here we are using regexp
3173 LASSERT(use_regexp, /**/);
3175 string lead_as_regexp;
3176 if (lead_size > 0) {
3177 lead_as_regexp = string2regex(par_as_string.substr(0, lead_size));
3178 regex_replace(par_as_string_nolead, par_as_string_nolead, "}$", "");
3179 par_as_string = par_as_string_nolead;
3180 LYXERR(Debug::FIND, "lead_as_regexp is '" << lead_as_regexp << "'");
3181 LYXERR(Debug::FIND, "par_as_string now is '" << par_as_string << "'");
3183 // LYXERR(Debug::FIND, "par_as_string before escape_for_regex() is '" << par_as_string << "'");
3184 par_as_string = escape_for_regex(par_as_string, !opt.ignoreformat);
3185 // Insert (.*?) before trailing closure of math, macros and environments, so to catch parts of them.
3186 // LYXERR(Debug::FIND, "par_as_string now is '" << par_as_string << "'");
3188 size_t lng = par_as_string.size();
3189 if (!opt.ignoreformat) {
3190 // Remove extra '\}' at end if not part of \{\.\}
3192 if (par_as_string.substr(lng-2, 2).compare("\\}") == 0) {
3194 if (par_as_string.substr(lng-6,3).compare("\\{\\") == 0)
3203 if (lng < par_as_string.size())
3204 par_as_string = par_as_string.substr(0,lng);
3206 LYXERR(Debug::FIND, "par_as_string after correctRegex is '" << par_as_string << "'");
3207 if ((lng > 0) && (par_as_string[0] == '^')) {
3208 par_as_string = par_as_string.substr(1);
3210 opt.matchAtStart = true;
3212 // LYXERR(Debug::FIND, "par_as_string now is '" << par_as_string << "'");
3213 // LYXERR(Debug::FIND, "Open braces: " << open_braces);
3214 // LYXERR(Debug::FIND, "Replaced text (to be used as regex): " << par_as_string);
3216 // If entered regexp must match at begin of searched string buffer
3217 // Kornel: Added parentheses to use $1 for size of the leading string
3221 // TODO: Adapt '\[12345678]' in par_as_string to acount for the first '()
3222 // Unfortunately is '\1', '\2', etc not working for strings with extra format
3223 // so the convert has no effect in that case
3224 for (int i = 7; i > 0; --i) {
3225 string orig = "\\\\" + std::to_string(i);
3226 string dest = "\\" + std::to_string(i+2);
3227 while (regex_replace(par_as_string, par_as_string, orig, dest));
3229 if (opt.matchword) {
3230 modifyRegexForMatchWord(par_as_string);
3231 opt.matchword = false;
3233 regexp_str = "(" + lead_as_regexp + ")()" + par_as_string;
3234 regexp2_str = "(" + lead_as_regexp + ")(.*?)" + par_as_string;
3236 LYXERR(Debug::FIND, "Setting regexp to : '" << regexp_str << "'");
3237 LYXERR(Debug::FIND, "Setting regexp2 to: '" << regexp2_str << "'");
3238 CreateRegexp(opt, regexp_str, regexp2_str, par_as_string);
3242 MatchResult MatchStringAdv::findAux(DocIterator const & cur, int len, bool at_begin) const
3246 mres.searched_size = len;
3248 (opt.restr == FindAndReplaceOptions::R_ONLY_MATHS && !cur.inMathed()) )
3251 docstring docstr = stringifyFromForSearch(opt, cur, len);
3253 str = normalize(docstr);
3254 if (!opt.ignoreformat) {
3255 str = correctlanguagesetting(str, false, !opt.ignoreformat);
3256 // remove closing '}' and '\n' to allow for use of '$' in regex
3257 size_t lng = str.size();
3258 while ((lng > 1) && ((str[lng -1] == '}') || (str[lng -1] == '\n')))
3260 if (lng != str.size()) {
3261 str = str.substr(0, lng);
3265 mres.match_len = -1;
3268 LYXERR(Debug::FIND, "After normalization: Matching against:\n'" << str << "'");
3270 LASSERT(use_regexp, /**/);
3272 // use_regexp always true
3273 LYXERR(Debug::FIND, "Searching in regexp mode: at_begin=" << at_begin);
3275 QString qstr = QString::fromStdString(str);
3276 QRegularExpression const *p_regexp;
3277 QRegularExpression::MatchType flags = QRegularExpression::NormalMatch;
3281 p_regexp = ®exp2;
3283 QRegularExpressionMatch match = p_regexp->match(qstr, 0, flags);
3284 if (!match.hasMatch())
3287 regex const *p_regexp;
3288 regex_constants::match_flag_type flags;
3290 flags = regex_constants::match_continuous;
3293 flags = regex_constants::match_default;
3294 p_regexp = ®exp2;
3296 sregex_iterator re_it(str.begin(), str.end(), *p_regexp, flags);
3297 if (re_it == sregex_iterator())
3299 match_results<string::const_iterator> const & m = *re_it;
3301 // Whole found string, including the leading
3302 // std: m[0].second - m[0].first
3303 // Qt: match.capturedEnd(0) - match.capturedStart(0)
3305 // Size of the leading string
3306 // std: m[1].second - m[1].first
3307 // Qt: match.capturedEnd(1) - match.capturedStart(1)
3308 int leadingsize = 0;
3310 if (match.lastCapturedIndex() > 0) {
3311 leadingsize = match.capturedEnd(1) - match.capturedStart(1);
3316 leadingsize = m[1].second - m[1].first;
3320 mres.match_prefix = match.capturedEnd(2) - match.capturedStart(2);
3321 mres.match_len = match.capturedEnd(0) - match.capturedEnd(2);
3322 // because of different number of closing at end of string
3323 // we have to 'unify' the length of the post-match.
3324 // Done by ignoring closing parenthesis and linefeeds at string end
3325 int matchend = match.capturedEnd(0);
3326 size_t strsize = qstr.size();
3327 if (!opt.ignoreformat) {
3328 while (mres.match_len > 0) {
3329 QChar c = qstr.at(matchend - 1);
3330 if ((c == '\n') || (c == '}') || (c == '{')) {
3337 while (strsize > (size_t) match.capturedEnd(0)) {
3338 QChar c = qstr.at(strsize-1);
3339 if ((c == '\n') || (c == '}')) {
3346 // LYXERR0(qstr.toStdString());
3347 mres.match2end = strsize - matchend;
3348 mres.pos = match.capturedStart(2);
3350 mres.match_prefix = m[2].second - m[2].first;
3351 mres.match_len = m[0].second - m[2].second;
3352 // ignore closing parenthesis and linefeeds at string end
3353 size_t strend = m[0].second - m[0].first;
3354 int matchend = strend;
3355 size_t strsize = str.size();
3356 if (!opt.ignoreformat) {
3357 while (mres.match_len > 0) {
3358 char c = str.at(matchend - 1);
3359 if ((c == '\n') || (c == '}') || (c == '{')) {
3366 while (strsize > strend) {
3367 if ((str.at(strsize-1) == '}') || (str.at(strsize-1) == '\n')) {
3375 mres.match2end = strsize - matchend;
3376 mres.pos = m[2].first - m[0].first;;
3378 if (mres.match2end < 0)
3380 mres.leadsize = leadingsize;
3382 if (mres.match_len > 0) {
3383 string a0 = match.captured(0).mid(mres.pos + mres.match_prefix, mres.match_len).toStdString();
3384 mres.result.push_back(a0);
3385 for (int i = 3; i <= match.lastCapturedIndex(); i++) {
3386 mres.result.push_back(match.captured(i).toStdString());
3390 if (mres.match_len > 0) {
3391 string a0 = m[0].str().substr(mres.pos + mres.match_prefix, mres.match_len);
3392 mres.result.push_back(a0);
3393 for (size_t i = 3; i < m.size(); i++) {
3394 mres.result.push_back(m[i]);
3403 MatchResult MatchStringAdv::operator()(DocIterator const & cur, int len, bool at_begin) const
3405 MatchResult mres = findAux(cur, len, at_begin);
3406 int res = mres.match_len;
3408 "res=" << res << ", at_begin=" << at_begin
3409 << ", matchAtStart=" << opt.matchAtStart
3410 << ", inTexted=" << cur.inTexted());
3411 if (opt.matchAtStart) {
3414 else if (mres.match_prefix > 0)
3423 static bool simple_replace(string &t, string from, string to)
3425 regex repl("(\\\\)*(" + from + ")");
3429 for (sregex_iterator it(t.begin(), t.end(), repl), end; it != end; ++it) {
3431 if ((sub.position(2) - sub.position(0)) % 2 == 1)
3433 if (lastpos < (size_t) sub.position(2))
3434 s += t.substr(lastpos, sub.position(2) - lastpos);
3436 lastpos = sub.position(2) + sub.length(2);
3440 else if (lastpos < t.length())
3441 s += t.substr(lastpos, t.length() - lastpos);
3447 string MatchStringAdv::normalize(docstring const & s) const
3450 t = lyx::to_utf8(s);
3451 // Remove \n at begin
3452 while (!t.empty() && t[0] == '\n')
3455 while (!t.empty() && t[t.size() - 1] == '\n')
3456 t = t.substr(0, t.size() - 1);
3458 // Handle all other '\n'
3459 while ((pos = t.find("\n")) != string::npos) {
3460 if (pos > 1 && t[pos-1] == '\\' && t[pos-2] == '\\' ) {
3462 if (isAlnumASCII(t[pos+1])) {
3463 t.replace(pos-2, 3, " ");
3466 t.replace(pos-2, 3, "");
3469 else if (!isAlnumASCII(t[pos+1]) || !isAlnumASCII(t[pos-1])) {
3470 // '\n' adjacent to non-alpha-numerics, discard
3471 t.replace(pos, 1, "");
3474 // Replace all other \n with spaces
3475 t.replace(pos, 1, " ");
3478 // Remove stale empty \emph{}, \textbf{} and similar blocks from latexify
3479 // Kornel: Added textsl, textsf, textit, texttt and noun
3480 // + allow to seach for colored text too
3481 LYXERR(Debug::FIND, "Removing stale empty macros from: " << t);
3482 while (regex_replace(t, t, "\\\\(emph|noun|text(bf|sl|sf|it|tt)|(u|uu)line|(s|x)out|uwave)(\\{(\\{\\})?\\})+", ""))
3483 LYXERR(Debug::FIND, " further removing stale empty \\emph{}, \\textbf{} macros from: " << t);
3484 while (regex_replace(t, t, "\\\\((sub)?(((sub)?section)|paragraph)|part)\\*?(\\{(\\{\\})?\\})+", ""))
3485 LYXERR(Debug::FIND, " further removing stale empty \\emph{}, \\textbf{} macros from: " << t);
3486 while (regex_replace(t, t, "\\\\(foreignlanguage|textcolor|item)\\{[a-z]+\\}(\\{(\\{\\})?\\})+", ""));
3492 docstring stringifyFromCursor(DocIterator const & cur, int len)
3494 LYXERR(Debug::FIND, "Stringifying with len=" << len << " from cursor at pos: " << cur);
3495 if (cur.inTexted()) {
3496 Paragraph const & par = cur.paragraph();
3497 // TODO what about searching beyond/across paragraph breaks ?
3498 // TODO Try adding a AS_STR_INSERTS as last arg
3499 pos_type end = ( len == -1 || cur.pos() + len > int(par.size()) ) ?
3500 int(par.size()) : cur.pos() + len;
3501 // OutputParams runparams(&cur.buffer()->params().encoding());
3502 OutputParams runparams(encodings.fromLyXName("utf8"));
3503 runparams.nice = true;
3504 runparams.flavor = Flavor::XeTeX;
3505 runparams.linelen = 10000; //lyxrc.plaintext_linelen;
3506 // No side effect of file copying and image conversion
3507 runparams.dryrun = true;
3508 int option = AS_STR_INSETS | AS_STR_PLAINTEXT;
3509 if (ignoreFormats.getDeleted()) {
3510 option |= AS_STR_SKIPDELETE;
3511 runparams.for_searchAdv = OutputParams::SearchWithoutDeleted;
3514 runparams.for_searchAdv = OutputParams::SearchWithDeleted;
3516 LYXERR(Debug::FIND, "Stringifying with cur: "
3517 << cur << ", from pos: " << cur.pos() << ", end: " << end);
3518 return par.asString(cur.pos(), end,
3521 } else if (cur.inMathed()) {
3522 CursorSlice cs = cur.top();
3523 MathData md = cs.cell();
3524 MathData::const_iterator it_end =
3525 (( len == -1 || cs.pos() + len > int(md.size()))
3527 : md.begin() + cs.pos() + len );
3529 for (MathData::const_iterator it = md.begin() + cs.pos();
3532 docstring s = asString(md2);
3533 LYXERR(Debug::FIND, "Stringified math: '" << s << "'");
3536 LYXERR(Debug::FIND, "Don't know how to stringify from here: " << cur);
3541 /** Computes the LaTeX export of buf starting from cur and ending len positions
3542 * after cur, if len is positive, or at the paragraph or innermost inset end
3545 docstring latexifyFromCursor(DocIterator const & cur, int len)
3548 LYXERR(Debug::FIND, "Latexifying with len=" << len << " from cursor at pos: " << cur);
3549 LYXERR(Debug::FIND, " with cur.lastpost=" << cur.lastpos() << ", cur.lastrow="
3550 << cur.lastrow() << ", cur.lastcol=" << cur.lastcol());
3552 Buffer const & buf = *cur.buffer();
3554 odocstringstream ods;
3556 //OutputParams runparams(&buf.params().encoding());
3557 OutputParams runparams(encodings.fromLyXName("utf8"));
3558 runparams.nice = false;
3559 runparams.flavor = Flavor::XeTeX;
3560 runparams.linelen = 8000; //lyxrc.plaintext_linelen;
3561 // No side effect of file copying and image conversion
3562 runparams.dryrun = true;
3563 if (ignoreFormats.getDeleted()) {
3564 runparams.for_searchAdv = OutputParams::SearchWithoutDeleted;
3567 runparams.for_searchAdv = OutputParams::SearchWithDeleted;
3570 if (cur.inTexted()) {
3571 // @TODO what about searching beyond/across paragraph breaks ?
3572 pos_type endpos = cur.paragraph().size();
3573 if (len != -1 && endpos > cur.pos() + len)
3574 endpos = cur.pos() + len;
3575 TeXOnePar(buf, *cur.innerText(), cur.pit(), os, runparams,
3576 string(), cur.pos(), endpos);
3577 string s = lyx::to_utf8(ods.str());
3578 LYXERR(Debug::FIND, "Latexified +modified text: '" << s << "'");
3579 return(lyx::from_utf8(s));
3580 } else if (cur.inMathed()) {
3581 // Retrieve the math environment type, and add '$' or '$[' or others (\begin{equation}) accordingly
3582 for (int s = cur.depth() - 1; s >= 0; --s) {
3583 CursorSlice const & cs = cur[s];
3584 if (cs.asInsetMath() && cs.asInsetMath()->asHullInset()) {
3585 TeXMathStream ws(os);
3586 cs.asInsetMath()->asHullInset()->header_write(ws);
3591 CursorSlice const & cs = cur.top();
3592 MathData md = cs.cell();
3593 MathData::const_iterator it_end =
3594 ((len == -1 || cs.pos() + len > int(md.size()))
3596 : md.begin() + cs.pos() + len);
3598 for (MathData::const_iterator it = md.begin() + cs.pos();
3602 ods << asString(md2);
3603 // Retrieve the math environment type, and add '$' or '$]'
3604 // or others (\end{equation}) accordingly
3605 for (int s = cur.depth() - 1; s >= 0; --s) {
3606 CursorSlice const & cs2 = cur[s];
3607 InsetMath * inset = cs2.asInsetMath();
3608 if (inset && inset->asHullInset()) {
3609 TeXMathStream ws(os);
3610 inset->asHullInset()->footer_write(ws);
3614 LYXERR(Debug::FIND, "Latexified math: '" << lyx::to_utf8(ods.str()) << "'");
3616 LYXERR(Debug::FIND, "Don't know how to stringify from here: " << cur);
3621 #if defined(ResultsDebug)
3623 static void displayMResult(MatchResult &mres, string from, DocIterator & cur)
3625 LYXERR0( "from:\t\t\t" << from);
3627 if (mres.pos_len > 0) {
3629 status = "FINALSEARCH";
3632 if (mres.match_len > 0) {
3633 if ((mres.match_prefix == 0) && (mres.pos == mres.leadsize))
3634 status = "Good Match";
3636 status = "Matched in";
3639 status = "MissedSearch";
3642 LYXERR0( status << "(" << cur.pos() << " ... " << mres.searched_size + cur.pos() << ") cur.lastpos(" << cur.lastpos() << ")");
3643 if ((mres.leadsize > 0) || (mres.match_len > 0) || (mres.match2end > 0))
3644 LYXERR0( "leadsize(" << mres.leadsize << ") match_len(" << mres.match_len << ") match2end(" << mres.match2end << ")");
3645 if ((mres.pos > 0) || (mres.match_prefix > 0))
3646 LYXERR0( "pos(" << mres.pos << ") match_prefix(" << mres.match_prefix << ")");
3647 for (size_t i = 0; i < mres.result.size(); i++)
3648 LYXERR0( "Match " << i << " = \"" << mres.result[i] << "\"");
3650 #define displayMres(s, txt, cur) displayMResult(s, txt, cur);
3652 #define displayMres(s, txt, cur)
3655 /** Finalize an advanced find operation, advancing the cursor to the innermost
3656 ** position that matches, plus computing the length of the matching text to
3658 ** Return the cur.pos() difference between start and end of found match
3660 MatchResult findAdvFinalize(DocIterator & cur, MatchStringAdv const & match, MatchResult const & expected = MatchResult(-1))
3662 // Search the foremost position that matches (avoids find of entire math
3663 // inset when match at start of it)
3664 DocIterator old_cur(cur.buffer());
3666 static MatchResult fail = MatchResult();
3667 MatchResult max_match;
3668 // If (prefix_len > 0) means that forwarding 1 position will remove the complete entry
3669 // Happens with e.g. hyperlinks
3670 // either one sees "http://www.bla.bla" or nothing
3671 // so the search for "www" gives prefix_len = 7 (== sizeof("http://")
3672 // and although we search for only 3 chars, we find the whole hyperlink inset
3673 bool at_begin = (expected.match_prefix == 0);
3674 if (!match.opt.forward && match.opt.ignoreformat) {
3675 if (expected.pos > 0)
3678 LASSERT(at_begin, /**/);
3679 if (expected.match_len > 0 && at_begin) {
3680 // Search for deepest match
3682 max_match = expected;
3684 size_t d = cur.depth();
3688 if (cur.depth() < d)
3690 if (cur.depth() == d)
3693 while (cur && cur.depth() > lastd) {
3694 lastd = cur.depth();
3695 mres = match(cur, -1, at_begin);
3696 displayMres(mres, "Checking innermost", cur);
3697 if (mres.match_len > 0)
3702 if (mres.match_len < expected.match_len)
3710 // (expected.match_len <= 0)
3711 mres = match(cur); /* match valid only if not searching whole words */
3712 displayMres(mres, "Start with negative match", cur);
3715 if (max_match.match_len <= 0) return fail;
3716 LYXERR(Debug::FIND, "Ok");
3718 // Compute the match length
3720 if (cur.pos() + len > cur.lastpos())
3723 LASSERT(match.use_regexp, /**/);
3726 int maxl = cur.lastpos() - cur.pos();
3727 // Greedy behaviour while matching regexps
3728 while (maxl > minl) {
3730 mres2 = match(cur, len, at_begin);
3731 displayMres(mres2, "Finalize loop", cur);
3732 int actual_match_len = mres2.match_len;
3733 if (actual_match_len >= max_match.match_len) {
3734 // actual_match_len > max_match _can_ happen,
3735 // if the search area splits
3736 // some following word so that the regex
3737 // (e.g. 'r.*r\b' matches 'r' from the middle of the
3739 // This means, the len value is too big
3740 actual_match_len = max_match.match_len;
3742 max_match.match_len = actual_match_len;
3744 if (maxl - minl < 4)
3745 len = (int)((maxl + minl)/2);
3747 len = (int)(minl + (maxl - minl + 3)/4);
3750 // (actual_match_len < max_match.match_len)
3752 len = (int)((maxl + minl)/2);
3757 // Search for real start of matched characters
3759 MatchResult actual_match;
3762 } while (cur.depth() > old_cur.depth()); /* Skip inner insets */
3763 if (cur.depth() < old_cur.depth()) {
3765 LYXERR(Debug::INFO, "cur.depth() < old_cur.depth(), this should never happen");
3768 if (cur.pos() != old_cur.pos()) {
3769 // OK, forwarded 1 pos in actual inset
3770 actual_match = match(cur, len-1, at_begin);
3771 if (actual_match.match_len == max_match.match_len) {
3772 // Ha, got it! The shorter selection has the same match length
3775 max_match = actual_match;
3778 // OK, the shorter selection matches less chars, revert to previous value
3784 LYXERR(Debug::INFO, "cur.pos() == old_cur.pos(), this should never happen");
3785 actual_match = match(cur, len, at_begin);
3786 if (actual_match.match_len == max_match.match_len) {
3788 max_match = actual_match;
3795 max_match.pos_len = len;
3796 displayMres(max_match, "SEARCH RESULT", cur)
3803 int findForwardAdv(DocIterator & cur, MatchStringAdv & match)
3807 bool repeat = false;
3808 DocIterator orig_cur; // to be used if repeat not successful
3809 MatchResult orig_mres;
3810 while (!theApp()->longOperationCancelled() && cur) {
3811 //(void) findAdvForwardInnermost(cur);
3812 LYXERR(Debug::FIND, "findForwardAdv() cur: " << cur);
3813 MatchResult mres = match(cur, -1, false);
3814 string msg = "Starting";
3817 displayMres(mres, msg + " findForwardAdv", cur)
3818 int match_len = mres.match_len;
3819 if ((mres.pos > 100000) || (mres.match2end > 100000) || (match_len > 100000)) {
3820 LYXERR(Debug::INFO, "BIG LENGTHS: " << mres.pos << ", " << match_len << ", " << mres.match2end);
3823 if (match_len <= 0) {
3824 // This should exit nested insets, if any, or otherwise undefine the currsor.
3825 cur.pos() = cur.lastpos();
3826 LYXERR(Debug::FIND, "Advancing pos: cur=" << cur);
3829 else { // match_len > 0
3830 // Try to find the begin of searched string
3832 int firstInvalid = cur.lastpos() - cur.pos();
3834 int incrmatch = (mres.match_prefix + mres.pos - mres.leadsize + 1)*3/4;
3835 int incrcur = (firstInvalid + 1 )*3/4;
3836 if (incrcur < incrmatch)
3837 increment = incrcur;
3839 increment = incrmatch;
3843 LYXERR(Debug::FIND, "Set increment to " << increment);
3844 while (increment > 0) {
3845 DocIterator old_cur = cur;
3846 if (cur.pos() + increment >= cur.lastpos()) {
3850 cur.pos() = cur.pos() + increment;
3851 MatchResult mres2 = match(cur, -1, false);
3852 displayMres(mres2, "findForwardAdv loop", cur)
3853 switch (interpretMatch(mres, mres2)) {
3854 case MatchResult::newIsTooFar:
3855 // behind the expected match
3856 firstInvalid = increment;
3860 case MatchResult::newIsBetter:
3861 // not reached yet, but cur.pos()+increment is bettert
3863 firstInvalid -= increment;
3864 if (increment > firstInvalid*3/4)
3865 increment = firstInvalid*3/4;
3866 if ((mres2.pos == mres2.leadsize) && (increment >= mres2.match_prefix)) {
3867 if (increment >= mres2.match_prefix)
3868 increment = (mres2.match_prefix+1)*3/4;
3873 // Handle not like MatchResult::newIsTooFar
3874 LYXERR0( "Probably too far: Increment = " << increment << " match_prefix = " << mres.match_prefix);
3876 increment = increment*3/4;
3881 if (mres.match_len > 0) {
3882 if (mres.match_prefix + mres.pos - mres.leadsize > 0) {
3883 // The match seems to indicate some deeper level
3892 // should never be reached.
3896 // LYXERR0("Leaving first loop");
3897 LYXERR(Debug::FIND, "Finalizing 1");
3898 MatchResult found_match = findAdvFinalize(cur, match, mres);
3899 if (found_match.match_len > 0) {
3900 LASSERT(found_match.pos_len > 0, /**/);
3901 match.FillResults(found_match);
3902 return found_match.pos_len;
3905 // try next possible match
3916 /// Find the most backward consecutive match within same paragraph while searching backwards.
3917 MatchResult findMostBackwards(DocIterator & cur, MatchStringAdv const & match, MatchResult &expected)
3919 DocIterator cur_begin = cur;
3920 cur_begin.pos() = 0;
3921 DocIterator tmp_cur = cur;
3922 MatchResult mr = findAdvFinalize(tmp_cur, match, expected);
3923 Inset & inset = cur.inset();
3924 for (; cur != cur_begin; cur.backwardPos()) {
3925 LYXERR(Debug::FIND, "findMostBackwards(): cur=" << cur);
3926 DocIterator new_cur = cur;
3927 new_cur.backwardPos();
3928 if (new_cur == cur || &new_cur.inset() != &inset || !match(new_cur).match_len)
3930 MatchResult new_mr = findAdvFinalize(new_cur, match, expected);
3931 if (new_mr.match_len == mr.match_len)
3935 LYXERR(Debug::FIND, "findMostBackwards(): exiting with cur=" << cur);
3941 int findBackwardsAdv(DocIterator & cur, MatchStringAdv & match)
3945 // Backup of original position
3946 DocIterator cur_begin = doc_iterator_begin(cur.buffer());
3947 if (cur == cur_begin)
3950 DocIterator cur_orig(cur);
3951 bool pit_changed = false;
3954 MatchResult found_match = match(cur, -1, false);
3956 if (found_match.match_len > 0) {
3958 cur.pos() = cur.lastpos();
3960 cur.pos() = cur_orig.pos();
3961 LYXERR(Debug::FIND, "findBackAdv2: cur: " << cur);
3962 DocIterator cur_prev_iter;
3964 found_match = match(cur);
3965 LYXERR(Debug::FIND, "findBackAdv3: found_match="
3966 << (found_match.match_len > 0) << ", cur: " << cur);
3967 if (found_match.match_len > 0) {
3968 MatchResult found_mr = findMostBackwards(cur, match, found_match);
3969 if (found_mr.pos_len > 0) {
3970 match.FillResults(found_mr);
3971 return found_mr.pos_len;
3975 // Stop if begin of document reached
3976 if (cur == cur_begin)
3978 cur_prev_iter = cur;
3982 if (cur == cur_begin)
3989 } while (!theApp()->longOperationCancelled());
3997 docstring stringifyFromForSearch(FindAndReplaceOptions const & opt,
3998 DocIterator const & cur, int len)
4000 if (cur.pos() < 0 || cur.pos() > cur.lastpos())
4002 if (!opt.ignoreformat)
4003 return latexifyFromCursor(cur, len);
4005 return stringifyFromCursor(cur, len);
4009 FindAndReplaceOptions::FindAndReplaceOptions(
4010 docstring const & _find_buf_name, bool _casesensitive,
4011 bool _matchword, bool _forward, bool _expandmacros, bool _ignoreformat,
4012 docstring const & _repl_buf_name, bool _keep_case,
4013 SearchScope _scope, SearchRestriction _restr, bool _replace_all)
4014 : find_buf_name(_find_buf_name), casesensitive(_casesensitive), matchword(_matchword),
4015 forward(_forward), expandmacros(_expandmacros), ignoreformat(_ignoreformat),
4016 repl_buf_name(_repl_buf_name), keep_case(_keep_case), scope(_scope), restr(_restr), replace_all(_replace_all)
4024 /** Check if 'len' letters following cursor are all non-lowercase */
4025 static bool allNonLowercase(Cursor const & cur, int len)
4027 pos_type beg_pos = cur.selectionBegin().pos();
4028 pos_type end_pos = cur.selectionBegin().pos() + len;
4029 if (len > cur.lastpos() + 1 - beg_pos) {
4030 LYXERR(Debug::FIND, "This should not happen, more debug needed");
4031 len = cur.lastpos() + 1 - beg_pos;
4032 end_pos = beg_pos + len;
4034 for (pos_type pos = beg_pos; pos != end_pos; ++pos)
4035 if (isLowerCase(cur.paragraph().getChar(pos)))
4041 /** Check if first letter is upper case and second one is lower case */
4042 static bool firstUppercase(Cursor const & cur)
4045 pos_type pos = cur.selectionBegin().pos();
4046 if (pos >= cur.lastpos() - 1) {
4047 LYXERR(Debug::FIND, "No upper-case at cur: " << cur);
4050 ch1 = cur.paragraph().getChar(pos);
4051 ch2 = cur.paragraph().getChar(pos + 1);
4052 bool result = isUpperCase(ch1) && isLowerCase(ch2);
4053 LYXERR(Debug::FIND, "firstUppercase(): "
4054 << "ch1=" << ch1 << "(" << char(ch1) << "), ch2="
4055 << ch2 << "(" << char(ch2) << ")"
4056 << ", result=" << result << ", cur=" << cur);
4061 /** Make first letter of supplied buffer upper-case, and the rest lower-case.
4063 ** \fixme What to do with possible further paragraphs in replace buffer ?
4065 static void changeFirstCase(Buffer & buffer, TextCase first_case, TextCase others_case)
4067 ParagraphList::iterator pit = buffer.paragraphs().begin();
4068 LASSERT(!pit->empty(), /**/);
4069 pos_type right = pos_type(1);
4070 pit->changeCase(buffer.params(), pos_type(0), right, first_case);
4071 right = pit->size();
4072 pit->changeCase(buffer.params(), pos_type(1), right, others_case);
4076 static bool replaceMatches(string &t, int maxmatchnum, vector <string> const & replacements)
4078 // Should replace the string "$" + std::to_string(matchnum) with replacement
4079 // if the char '$' is not prefixed with odd number of char '\\'
4080 static regex const rematch("(\\\\)*(\\$\\$([0-9]))");
4084 for (sregex_iterator it(t.begin(), t.end(), rematch), end; it != end; ++it) {
4086 if ((sub.position(2) - sub.position(0)) % 2 == 1)
4088 int num = stoi(sub.str(3), nullptr, 10);
4089 if (num >= maxmatchnum)
4091 if (lastpos < (size_t) sub.position(2))
4092 s += t.substr(lastpos, sub.position(2) - lastpos);
4093 s += replacements[num];
4094 lastpos = sub.position(2) + sub.length(2);
4098 else if (lastpos < t.length())
4099 s += t.substr(lastpos, t.length() - lastpos);
4105 static int findAdvReplace(BufferView * bv, FindAndReplaceOptions const & opt, MatchStringAdv & matchAdv)
4107 Cursor & cur = bv->cursor();
4108 if (opt.repl_buf_name.empty()
4109 || theBufferList().getBuffer(FileName(to_utf8(opt.repl_buf_name)), true) == 0
4110 || theBufferList().getBuffer(FileName(to_utf8(opt.find_buf_name)), true) == 0)
4113 DocIterator sel_beg = cur.selectionBegin();
4114 DocIterator sel_end = cur.selectionEnd();
4115 if (&sel_beg.inset() != &sel_end.inset()
4116 || sel_beg.pit() != sel_end.pit()
4117 || sel_beg.idx() != sel_end.idx())
4119 int sel_len = sel_end.pos() - sel_beg.pos();
4120 LYXERR(Debug::FIND, "sel_beg: " << sel_beg << ", sel_end: " << sel_end
4121 << ", sel_len: " << sel_len << endl);
4124 LASSERT(sel_len > 0, return 0);
4126 if (!matchAdv(sel_beg, sel_len).match_len)
4129 // Build a copy of the replace buffer, adapted to the KeepCase option
4130 Buffer const & repl_buffer_orig = *theBufferList().getBuffer(FileName(to_utf8(opt.repl_buf_name)), true);
4132 repl_buffer_orig.write(oss);
4133 string lyx = oss.str();
4134 if (matchAdv.valid_matches > 0) {
4135 replaceMatches(lyx, matchAdv.valid_matches, matchAdv.matches);
4137 Buffer repl_buffer("", false);
4138 repl_buffer.setUnnamed(true);
4139 LASSERT(repl_buffer.readString(lyx), return 0);
4140 if (opt.keep_case && sel_len >= 2) {
4141 LYXERR(Debug::FIND, "keep_case true: cur.pos()=" << cur.pos() << ", sel_len=" << sel_len);
4142 if (cur.inTexted()) {
4143 if (firstUppercase(cur))
4144 changeFirstCase(repl_buffer, text_uppercase, text_lowercase);
4145 else if (allNonLowercase(cur, sel_len))
4146 changeFirstCase(repl_buffer, text_uppercase, text_uppercase);
4149 cap::cutSelection(cur, false);
4150 if (cur.inTexted()) {
4151 repl_buffer.changeLanguage(
4152 repl_buffer.language(),
4153 cur.getFont().language());
4154 LYXERR(Debug::FIND, "Replacing by pasteParagraphList()ing repl_buffer");
4155 LYXERR(Debug::FIND, "Before pasteParagraphList() cur=" << cur << endl);
4156 cap::pasteParagraphList(cur, repl_buffer.paragraphs(),
4157 repl_buffer.params().documentClassPtr(),
4158 repl_buffer.params().authors(),
4159 bv->buffer().errorList("Paste"));
4160 LYXERR(Debug::FIND, "After pasteParagraphList() cur=" << cur << endl);
4161 sel_len = repl_buffer.paragraphs().begin()->size();
4162 } else if (cur.inMathed()) {
4163 odocstringstream ods;
4165 // OutputParams runparams(&repl_buffer.params().encoding());
4166 OutputParams runparams(encodings.fromLyXName("utf8"));
4167 runparams.nice = false;
4168 runparams.flavor = Flavor::XeTeX;
4169 runparams.linelen = 8000; //lyxrc.plaintext_linelen;
4170 runparams.dryrun = true;
4171 TeXOnePar(repl_buffer, repl_buffer.text(), 0, os, runparams);
4172 //repl_buffer.getSourceCode(ods, 0, repl_buffer.paragraphs().size(), false);
4173 docstring repl_latex = ods.str();
4174 LYXERR(Debug::FIND, "Latexified replace_buffer: '" << repl_latex << "'");
4176 (void)regex_replace(to_utf8(repl_latex), s, "\\$(.*)\\$", "$1");
4177 (void)regex_replace(s, s, "\\\\\\[(.*)\\\\\\]", "$1");
4178 repl_latex = from_utf8(s);
4179 LYXERR(Debug::FIND, "Replacing by insert()ing latex: '" << repl_latex << "' cur=" << cur << " with depth=" << cur.depth());
4180 MathData ar(cur.buffer());
4181 asArray(repl_latex, ar, Parse::NORMAL);
4183 sel_len = ar.size();
4184 LYXERR(Debug::FIND, "After insert() cur=" << cur << " with depth: " << cur.depth() << " and len: " << sel_len);
4186 if (cur.pos() >= sel_len)
4187 cur.pos() -= sel_len;
4190 LYXERR(Debug::FIND, "After pos adj cur=" << cur << " with depth: " << cur.depth() << " and len: " << sel_len);
4191 bv->putSelectionAt(DocIterator(cur), sel_len, !opt.forward);
4192 bv->processUpdateFlags(Update::Force);
4197 /// Perform a FindAdv operation.
4198 bool findAdv(BufferView * bv, FindAndReplaceOptions & opt)
4203 // e.g., when invoking word-findadv from mini-buffer wither with
4204 // wrong options syntax or before ever opening advanced F&R pane
4205 if (theBufferList().getBuffer(FileName(to_utf8(opt.find_buf_name)), true) == 0)
4209 MatchStringAdv matchAdv(bv->buffer(), opt);
4211 if (!matchAdv.regexIsValid) {
4212 bv->message(lyx::from_utf8(matchAdv.regexError));
4216 int length = bv->cursor().selectionEnd().pos() - bv->cursor().selectionBegin().pos();
4218 bv->putSelectionAt(bv->cursor().selectionBegin(), length, !opt.forward);
4219 num_replaced += findAdvReplace(bv, opt, matchAdv);
4222 pos_len = findForwardAdv(cur, matchAdv);
4224 pos_len = findBackwardsAdv(cur, matchAdv);
4225 } catch (exception & ex) {
4226 bv->message(from_utf8(ex.what()));
4231 if (num_replaced > 0) {
4232 switch (num_replaced)
4235 bv->message(_("One match has been replaced."));
4238 bv->message(_("Two matches have been replaced."));
4241 bv->message(bformat(_("%1$d matches have been replaced."), num_replaced));
4247 bv->message(_("Match not found."));
4252 if (num_replaced > 0)
4253 bv->message(_("Match has been replaced."));
4255 bv->message(_("Match found."));
4257 if (cur.pos() + pos_len > cur.lastpos()) {
4258 // Prevent crash in bv->putSelectionAt()
4259 // Should never happen, maybe LASSERT() here?
4260 pos_len = cur.lastpos() - cur.pos();
4262 LYXERR(Debug::FIND, "Putting selection at cur=" << cur << " with len: " << pos_len);
4263 bv->putSelectionAt(cur, pos_len, !opt.forward);
4269 ostringstream & operator<<(ostringstream & os, FindAndReplaceOptions const & opt)
4271 os << to_utf8(opt.find_buf_name) << "\nEOSS\n"
4272 << opt.casesensitive << ' '
4273 << opt.matchword << ' '
4274 << opt.forward << ' '
4275 << opt.expandmacros << ' '
4276 << opt.ignoreformat << ' '
4277 << opt.replace_all << ' '
4278 << to_utf8(opt.repl_buf_name) << "\nEOSS\n"
4279 << opt.keep_case << ' '
4280 << int(opt.scope) << ' '
4283 LYXERR(Debug::FIND, "built: " << os.str());
4289 istringstream & operator>>(istringstream & is, FindAndReplaceOptions & opt)
4291 // LYXERR(Debug::FIND, "parsing");
4295 while (line != "EOSS") {
4299 if (is.eof()) // Tolerate malformed request
4303 // LYXERR(Debug::FIND, "file_buf_name: '" << s << "'");
4304 opt.find_buf_name = from_utf8(s);
4305 is >> opt.casesensitive >> opt.matchword >> opt.forward >> opt.expandmacros >> opt.ignoreformat >> opt.replace_all;
4306 is.get(); // Waste space before replace string
4309 while (line != "EOSS") {
4313 if (is.eof()) // Tolerate malformed request
4317 // LYXERR(Debug::FIND, "repl_buf_name: '" << s << "'");
4318 opt.repl_buf_name = from_utf8(s);
4319 is >> opt.keep_case;
4322 opt.scope = FindAndReplaceOptions::SearchScope(i);
4324 opt.restr = FindAndReplaceOptions::SearchRestriction(i);
4327 LYXERR(Debug::FIND, "parsed: " << opt.casesensitive << ' ' << opt.matchword << ' ' << opt.forward << ' '
4328 << opt.expandmacros << ' ' << opt.ignoreformat << ' ' << opt.keep_case << ' '
4329 << opt.scope << ' ' << opt.restr);