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
12 * Full author contact details are available in file CREDITS.
20 #include "buffer_funcs.h"
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"
32 #include "ParIterator.h"
36 #include "frontends/Application.h"
37 #include "frontends/alert.h"
39 #include "mathed/InsetMath.h"
40 #include "mathed/InsetMathGrid.h"
41 #include "mathed/InsetMathHull.h"
42 #include "mathed/MathStream.h"
43 #include "mathed/MathSupport.h"
45 #include "support/convert.h"
46 #include "support/debug.h"
47 #include "support/docstream.h"
48 #include "support/FileName.h"
49 #include "support/gettext.h"
50 #include "support/lassert.h"
51 #include "support/lstrings.h"
53 #include "support/regex.h"
54 #include <boost/next_prior.hpp>
57 using namespace lyx::support;
63 bool parse_bool(docstring & howto)
68 howto = split(howto, var, ' ');
73 class MatchString : public binary_function<Paragraph, pos_type, int>
76 MatchString(docstring const & str, bool cs, bool mw)
77 : str(str), case_sens(cs), whole_words(mw)
80 // returns true if the specified string is at the specified position
81 // del specifies whether deleted strings in ct mode will be considered
82 int operator()(Paragraph const & par, pos_type pos, bool del = true) const
84 return par.find(str, case_sens, whole_words, pos, del);
92 // match whole words only
97 int findForward(DocIterator & cur, MatchString const & match,
100 for (; cur; cur.forwardChar())
101 if (cur.inTexted()) {
102 int len = match(cur.paragraph(), cur.pos(), find_del);
110 int findBackwards(DocIterator & cur, MatchString const & match,
111 bool find_del = true)
115 if (cur.inTexted()) {
116 int len = match(cur.paragraph(), cur.pos(), find_del);
125 bool searchAllowed(docstring const & str)
128 frontend::Alert::error(_("Search error"), _("Search string is empty"));
135 bool findOne(BufferView * bv, docstring const & searchstr,
136 bool case_sens, bool whole, bool forward, bool find_del = true)
138 if (!searchAllowed(searchstr))
141 DocIterator cur = forward
142 ? bv->cursor().selectionEnd()
143 : bv->cursor().selectionBegin();
145 MatchString const match(searchstr, case_sens, whole);
147 int match_len = forward
148 ? findForward(cur, match, find_del)
149 : findBackwards(cur, match, find_del);
152 bv->putSelectionAt(cur, match_len, !forward);
154 return match_len > 0;
158 int replaceAll(BufferView * bv,
159 docstring const & searchstr, docstring const & replacestr,
160 bool case_sens, bool whole)
162 Buffer & buf = bv->buffer();
164 if (!searchAllowed(searchstr) || buf.isReadonly())
167 DocIterator cur_orig(bv->cursor());
169 MatchString const match(searchstr, case_sens, whole);
172 int const rsize = replacestr.size();
173 int const ssize = searchstr.size();
176 cur.setCursor(doc_iterator_begin(&buf));
177 int match_len = findForward(cur, match, false);
178 while (match_len > 0) {
179 // Backup current cursor position and font.
180 pos_type const pos = cur.pos();
181 Font const font = cur.paragraph().getFontSettings(buf.params(), pos);
183 int striked = ssize -
184 cur.paragraph().eraseChars(pos, pos + match_len,
185 buf.params().trackChanges);
186 cur.paragraph().insert(pos, replacestr, font,
187 Change(buf.params().trackChanges
189 : Change::UNCHANGED));
190 for (int i = 0; i < rsize + striked; ++i)
193 match_len = findForward(cur, match, false);
196 bv->putSelectionAt(doc_iterator_begin(&buf), 0, false);
198 cur_orig.fixIfBroken();
199 bv->setCursor(cur_orig);
205 // the idea here is that we are going to replace the string that
206 // is selected IF it is the search string.
207 // if there is a selection, but it is not the search string, then
208 // we basically ignore it. (FIXME We ought to replace only within
210 // if there is no selection, then:
211 // (i) if some search string has been provided, then we find it.
212 // (think of how the dialog works when you hit "replace" the
214 // (ii) if no search string has been provided, then we treat the
215 // word the cursor is in as the search string. (why? i have no
216 // idea.) but this only works in text?
218 // returns the number of replacements made (one, if any) and
219 // whether anything at all was done.
220 pair<bool, int> replaceOne(BufferView * bv, docstring searchstr,
221 docstring const & replacestr, bool case_sens,
222 bool whole, bool forward, bool findnext)
224 Cursor & cur = bv->cursor();
225 if (!cur.selection()) {
226 // no selection, non-empty search string: find it
227 if (!searchstr.empty()) {
228 findOne(bv, searchstr, case_sens, whole, forward);
229 return make_pair(true, 0);
231 // empty search string
234 return make_pair(false, 0);
235 // select current word and treat it as the search string.
236 // This causes a minor bug as undo will restore this selection,
237 // which the user did not create (#8986).
238 cur.innerText()->selectWord(cur, WHOLE_WORD);
239 searchstr = cur.selectionAsString(false);
242 // if we still don't have a search string, report the error
244 if (!searchAllowed(searchstr))
245 return make_pair(false, 0);
247 bool have_selection = cur.selection();
248 docstring const selected = cur.selectionAsString(false);
251 ? searchstr == selected
252 : compare_no_case(searchstr, selected) == 0;
254 // no selection or current selection is not search word:
255 // just find the search word
256 if (!have_selection || !match) {
257 findOne(bv, searchstr, case_sens, whole, forward);
258 return make_pair(true, 0);
261 // we're now actually ready to replace. if the buffer is
262 // read-only, we can't, though.
263 if (bv->buffer().isReadonly())
264 return make_pair(false, 0);
266 cap::replaceSelectionWithString(cur, replacestr);
268 cur.pos() += replacestr.length();
269 LASSERT(cur.pos() <= cur.lastpos(),
270 cur.pos() = cur.lastpos());
273 findOne(bv, searchstr, case_sens, whole, forward, false);
275 return make_pair(true, 1);
281 docstring const find2string(docstring const & search,
282 bool casesensitive, bool matchword, bool forward)
286 << int(casesensitive) << ' '
287 << int(matchword) << ' '
293 docstring const replace2string(docstring const & replace,
294 docstring const & search,
295 bool casesensitive, bool matchword,
296 bool all, bool forward, bool findnext)
299 ss << replace << '\n'
301 << int(casesensitive) << ' '
302 << int(matchword) << ' '
304 << int(forward) << ' '
310 bool lyxfind(BufferView * bv, FuncRequest const & ev)
312 if (!bv || ev.action() != LFUN_WORD_FIND)
315 //lyxerr << "find called, cmd: " << ev << endl;
317 // data is of the form
319 // <casesensitive> <matchword> <forward>"
321 docstring howto = split(ev.argument(), search, '\n');
323 bool casesensitive = parse_bool(howto);
324 bool matchword = parse_bool(howto);
325 bool forward = parse_bool(howto);
327 return findOne(bv, search, casesensitive, matchword, forward);
331 bool lyxreplace(BufferView * bv,
332 FuncRequest const & ev, bool has_deleted)
334 if (!bv || ev.action() != LFUN_WORD_REPLACE)
337 // data is of the form
340 // <casesensitive> <matchword> <all> <forward> <findnext>"
343 docstring howto = split(ev.argument(), rplc, '\n');
344 howto = split(howto, search, '\n');
346 bool casesensitive = parse_bool(howto);
347 bool matchword = parse_bool(howto);
348 bool all = parse_bool(howto);
349 bool forward = parse_bool(howto);
350 bool findnext = howto.empty() ? true : parse_bool(howto);
352 int replace_count = 0;
357 replace_count = replaceAll(bv, search, rplc, casesensitive, matchword);
358 update = replace_count > 0;
361 replaceOne(bv, search, rplc, casesensitive, matchword, forward, findnext);
363 replace_count = rv.second;
366 Buffer const & buf = bv->buffer();
368 // emit message signal.
369 buf.message(_("String not found."));
371 if (replace_count == 0) {
372 buf.message(_("String found."));
373 } else if (replace_count == 1) {
374 buf.message(_("String has been replaced."));
376 docstring const str =
377 bformat(_("%1$d strings have been replaced."), replace_count);
381 } else if (findnext) {
382 // if we have deleted characters, we do not replace at all, but
383 // rather search for the next occurence
384 if (findOne(bv, search, casesensitive, matchword, forward))
387 bv->message(_("String not found."));
394 bool findChange(DocIterator & cur, bool next)
398 for (; cur; next ? cur.forwardPos() : cur.backwardPos())
399 if (cur.inTexted() && cur.paragraph().isChanged(cur.pos())) {
401 // if we search backwards, take a step forward
402 // to correctly set the anchor
411 bool findChange(BufferView * bv, bool next)
414 cur.setCursor(next ? bv->cursor().selectionEnd()
415 : bv->cursor().selectionBegin());
417 // Are we within a change ? Then first search forward (backward),
418 // clear the selection and search the other way around (see the end
419 // of this function). This will avoid changes to be selected half.
420 bool search_both_sides = false;
422 // Find enclosing text cursor
423 while (tmpcur.inMathed())
425 Change change_next_pos
426 = tmpcur.paragraph().lookupChange(tmpcur.pos());
427 if (change_next_pos.changed()) {
428 if (cur.inMathed()) {
430 search_both_sides = true;
431 } else if (tmpcur.pos() > 0 && tmpcur.inTexted()) {
432 Change change_prev_pos
433 = tmpcur.paragraph().lookupChange(tmpcur.pos() - 1);
434 if (change_next_pos.isSimilarTo(change_prev_pos))
435 search_both_sides = true;
439 // find the next change
440 if (!findChange(cur, next))
443 bv->mouseSetCursor(cur, false);
445 CursorSlice & tip = cur.top();
447 if (!next && tip.pos() > 0)
448 // take a step into the change
451 Change orig_change = tip.paragraph().lookupChange(tip.pos());
454 for (; tip.pit() < tip.lastpit() || tip.pos() < tip.lastpos(); tip.forwardPos()) {
455 Change change = tip.paragraph().lookupChange(tip.pos());
456 if (!change.isSimilarTo(orig_change))
460 for (; tip.pit() > 0 || tip.pos() > 0;) {
462 Change change = tip.paragraph().lookupChange(tip.pos());
463 if (!change.isSimilarTo(orig_change)) {
464 // take a step forward to correctly set the selection
471 if (!search_both_sides) {
472 // Now set the selection.
473 bv->mouseSetCursor(cur, true);
475 bv->mouseSetCursor(cur, false);
476 findChange(bv, !next);
484 bool findNextChange(BufferView * bv)
486 return findChange(bv, true);
490 bool findPreviousChange(BufferView * bv)
492 return findChange(bv, false);
498 typedef vector<pair<string, string> > Escapes;
500 /// A map of symbols and their escaped equivalent needed within a regex.
501 /// @note Beware of order
502 Escapes const & get_regexp_escapes()
504 typedef std::pair<std::string, std::string> P;
506 static Escapes escape_map;
507 if (escape_map.empty()) {
508 escape_map.push_back(P("$", "_x_$"));
509 escape_map.push_back(P("{", "_x_{"));
510 escape_map.push_back(P("}", "_x_}"));
511 escape_map.push_back(P("[", "_x_["));
512 escape_map.push_back(P("]", "_x_]"));
513 escape_map.push_back(P("(", "_x_("));
514 escape_map.push_back(P(")", "_x_)"));
515 escape_map.push_back(P("+", "_x_+"));
516 escape_map.push_back(P("*", "_x_*"));
517 escape_map.push_back(P(".", "_x_."));
518 escape_map.push_back(P("\\", "(?:\\\\|\\\\backslash)"));
519 escape_map.push_back(P("~", "(?:\\\\textasciitilde|\\\\sim)"));
520 escape_map.push_back(P("^", "(?:\\^|\\\\textasciicircum\\{\\}|\\\\mathcircumflex)"));
521 escape_map.push_back(P("_x_", "\\"));
526 /// A map of lyx escaped strings and their unescaped equivalent.
527 Escapes const & get_lyx_unescapes()
529 typedef std::pair<std::string, std::string> P;
531 static Escapes escape_map;
532 if (escape_map.empty()) {
533 escape_map.push_back(P("\\%", "%"));
534 escape_map.push_back(P("\\mathcircumflex ", "^"));
535 escape_map.push_back(P("\\mathcircumflex", "^"));
536 escape_map.push_back(P("\\backslash ", "\\"));
537 escape_map.push_back(P("\\backslash", "\\"));
538 escape_map.push_back(P("\\\\{", "_x_<"));
539 escape_map.push_back(P("\\\\}", "_x_>"));
540 escape_map.push_back(P("\\sim ", "~"));
541 escape_map.push_back(P("\\sim", "~"));
546 /// A map of escapes turning a regexp matching text to one matching latex.
547 Escapes const & get_regexp_latex_escapes()
549 typedef std::pair<std::string, std::string> P;
551 static Escapes escape_map;
552 if (escape_map.empty()) {
553 escape_map.push_back(P("\\\\", "(?:\\\\\\\\|\\\\backslash|\\\\textbackslash\\{\\})"));
554 escape_map.push_back(P("(<?!\\\\\\\\textbackslash)\\{", "\\\\\\{"));
555 escape_map.push_back(P("(<?!\\\\\\\\textbackslash\\\\\\{)\\}", "\\\\\\}"));
556 escape_map.push_back(P("\\[", "\\{\\[\\}"));
557 escape_map.push_back(P("\\]", "\\{\\]\\}"));
558 escape_map.push_back(P("\\^", "(?:\\^|\\\\textasciicircum\\{\\}|\\\\mathcircumflex)"));
559 escape_map.push_back(P("%", "\\\\\\%"));
564 /** @todo Probably the maps need to be migrated to regexps, in order to distinguish if
565 ** the found occurrence were escaped.
567 string apply_escapes(string s, Escapes const & escape_map)
569 LYXERR(Debug::FIND, "Escaping: '" << s << "'");
570 Escapes::const_iterator it;
571 for (it = escape_map.begin(); it != escape_map.end(); ++it) {
572 // LYXERR(Debug::FIND, "Escaping " << it->first << " as " << it->second);
573 unsigned int pos = 0;
574 while (pos < s.length() && (pos = s.find(it->first, pos)) < s.length()) {
575 s.replace(pos, it->first.length(), it->second);
576 LYXERR(Debug::FIND, "After escape: " << s);
577 pos += it->second.length();
578 // LYXERR(Debug::FIND, "pos: " << pos);
581 LYXERR(Debug::FIND, "Escaped : '" << s << "'");
586 /// Within \regexp{} apply get_lyx_unescapes() only (i.e., preserve regexp semantics of the string),
587 /// while outside apply get_lyx_unescapes()+get_regexp_escapes().
588 /// If match_latex is true, then apply regexp_latex_escapes() to \regexp{} contents as well.
589 string escape_for_regex(string s, bool match_latex)
592 while (pos < s.size()) {
593 size_t new_pos = s.find("\\regexp{", pos);
594 if (new_pos == string::npos)
596 LYXERR(Debug::FIND, "new_pos: " << new_pos);
597 string t = apply_escapes(s.substr(pos, new_pos - pos), get_lyx_unescapes());
598 LYXERR(Debug::FIND, "t [lyx]: " << t);
599 t = apply_escapes(t, get_regexp_escapes());
600 LYXERR(Debug::FIND, "t [rxp]: " << t);
601 s.replace(pos, new_pos - pos, t);
602 new_pos = pos + t.size();
603 LYXERR(Debug::FIND, "Regexp after escaping: " << s);
604 LYXERR(Debug::FIND, "new_pos: " << new_pos);
605 if (new_pos == s.size())
607 // Might fail if \\endregexp{} is preceeded by unexpected stuff (weird escapes)
608 size_t end_pos = s.find("\\endregexp{}}", new_pos + 8);
609 LYXERR(Debug::FIND, "end_pos: " << end_pos);
610 t = s.substr(new_pos + 8, end_pos - (new_pos + 8));
611 LYXERR(Debug::FIND, "t in regexp : " << t);
612 t = apply_escapes(t, get_lyx_unescapes());
613 LYXERR(Debug::FIND, "t in regexp [lyx]: " << t);
615 t = apply_escapes(t, get_regexp_latex_escapes());
616 LYXERR(Debug::FIND, "t in regexp [ltx]: " << t);
618 if (end_pos == s.size()) {
619 s.replace(new_pos, end_pos - new_pos, t);
621 LYXERR(Debug::FIND, "Regexp after \\regexp{} removal: " << s);
624 s.replace(new_pos, end_pos + 13 - new_pos, t);
625 LYXERR(Debug::FIND, "Regexp after \\regexp{...\\endregexp{}} removal: " << s);
626 pos = new_pos + t.size();
627 LYXERR(Debug::FIND, "pos: " << pos);
633 /// Wrapper for lyx::regex_replace with simpler interface
634 bool regex_replace(string const & s, string & t, string const & searchstr,
635 string const & replacestr)
637 lyx::regex e(searchstr);
639 ostream_iterator<char, char> it(oss);
640 lyx::regex_replace(it, s.begin(), s.end(), e, replacestr);
641 // tolerate t and s be references to the same variable
642 bool rv = (s != oss.str());
648 /** Checks if supplied string segment is well-formed from the standpoint of matching open-closed braces.
650 ** Verify that closed braces exactly match open braces. This avoids that, for example,
651 ** \frac{.*}{x} matches \frac{x+\frac{y}{x}}{z} with .* being 'x+\frac{y'.
654 ** Number of open braces that must remain open at the end for the verification to succeed.
656 bool braces_match(string::const_iterator const & beg,
657 string::const_iterator const & end,
661 string::const_iterator it = beg;
662 LYXERR(Debug::FIND, "Checking " << unmatched << " unmatched braces in '" << string(beg, end) << "'");
663 for (; it != end; ++it) {
664 // Skip escaped braces in the count
669 } else if (*it == '{') {
671 } else if (*it == '}') {
672 if (open_pars == 0) {
673 LYXERR(Debug::FIND, "Found unmatched closed brace");
679 if (open_pars != unmatched) {
680 LYXERR(Debug::FIND, "Found " << open_pars
681 << " instead of " << unmatched
682 << " unmatched open braces at the end of count");
685 LYXERR(Debug::FIND, "Braces match as expected");
690 /** The class performing a match between a position in the document and the FindAdvOptions.
692 class MatchStringAdv {
694 MatchStringAdv(lyx::Buffer & buf, FindAndReplaceOptions const & opt);
696 /** Tests if text starting at the supplied position matches with the one provided to the MatchStringAdv
697 ** constructor as opt.search, under the opt.* options settings.
700 ** If set, then match is searched only against beginning of text starting at cur.
701 ** If unset, then match is searched anywhere in text starting at cur.
704 ** The length of the matching text, or zero if no match was found.
706 int operator()(DocIterator const & cur, int len = -1, bool at_begin = true) const;
711 /// first buffer on which search was started
712 lyx::Buffer * const p_first_buf;
714 FindAndReplaceOptions const & opt;
717 /// Auxiliary find method (does not account for opt.matchword)
718 int findAux(DocIterator const & cur, int len = -1, bool at_begin = true) const;
720 /** Normalize a stringified or latexified LyX paragraph.
724 ** <li>if search is not casesensitive, then lowercase the string;
725 ** <li>remove any newline at begin or end of the string;
726 ** <li>replace any newline in the middle of the string with a simple space;
727 ** <li>remove stale empty styles and environments, like \emph{} and \textbf{}.
730 ** @todo Normalization should also expand macros, if the corresponding
731 ** search option was checked.
733 string normalize(docstring const & s, bool hack_braces) const;
734 // normalized string to search
735 string par_as_string;
736 // regular expression to use for searching
738 // same as regexp, but prefixed with a ".*"
740 // leading format material as string
741 string lead_as_string;
742 // par_as_string after removal of lead_as_string
743 string par_as_string_nolead;
744 // unmatched open braces in the search string/regexp
746 // number of (.*?) subexpressions added at end of search regexp for closing
747 // environments, math mode, styles, etc...
749 // Are we searching with regular expressions ?
754 static docstring buffer_to_latex(Buffer & buffer)
756 OutputParams runparams(&buffer.params().encoding());
758 odocstringstream ods;
759 otexstream os(ods, texrow);
760 runparams.nice = true;
761 runparams.flavor = OutputParams::LATEX;
762 runparams.linelen = 80; //lyxrc.plaintext_linelen;
763 // No side effect of file copying and image conversion
764 runparams.dryrun = true;
765 pit_type const endpit = buffer.paragraphs().size();
766 for (pit_type pit = 0; pit != endpit; ++pit) {
767 TeXOnePar(buffer, buffer.text(), pit, os, runparams);
768 LYXERR(Debug::FIND, "searchString up to here: " << ods.str());
774 static docstring stringifySearchBuffer(Buffer & buffer, FindAndReplaceOptions const & opt)
777 if (!opt.ignoreformat) {
778 str = buffer_to_latex(buffer);
780 OutputParams runparams(&buffer.params().encoding());
781 runparams.nice = true;
782 runparams.flavor = OutputParams::LATEX;
783 runparams.linelen = 100000; //lyxrc.plaintext_linelen;
784 runparams.dryrun = true;
785 for (pos_type pit = pos_type(0); pit < (pos_type)buffer.paragraphs().size(); ++pit) {
786 Paragraph const & par = buffer.paragraphs().at(pit);
787 LYXERR(Debug::FIND, "Adding to search string: '"
788 << par.stringify(pos_type(0), par.size(),
789 AS_STR_INSETS, runparams)
791 str += par.stringify(pos_type(0), par.size(),
792 AS_STR_INSETS, runparams);
799 /// Return separation pos between the leading material and the rest
800 static size_t identifyLeading(string const & s)
803 // @TODO Support \item[text]
804 while (regex_replace(t, t, "^\\\\(emph|textbf|subsubsection|subsection|section|subparagraph|paragraph|part)\\*?\\{", "")
805 || regex_replace(t, t, "^\\$", "")
806 || regex_replace(t, t, "^\\\\\\[ ", "")
807 || regex_replace(t, t, "^\\\\item ", "")
808 || regex_replace(t, t, "^\\\\begin\\{[a-zA-Z_]*\\*?\\} ", ""))
809 LYXERR(Debug::FIND, " after removing leading $, \\[ , \\emph{, \\textbf{, etc.: '" << t << "'");
814 // Remove trailing closure of math, macros and environments, so to catch parts of them.
815 static int identifyClosing(string & t)
819 LYXERR(Debug::FIND, "identifyClosing(): t now is '" << t << "'");
820 if (regex_replace(t, t, "(.*[^\\\\])\\$\\'", "$1"))
822 if (regex_replace(t, t, "(.*[^\\\\]) \\\\\\]\\'", "$1"))
824 if (regex_replace(t, t, "(.*[^\\\\]) \\\\end\\{[a-zA-Z_]*\\*?\\}\\'", "$1"))
826 if (regex_replace(t, t, "(.*[^\\\\])\\}\\'", "$1")) {
836 MatchStringAdv::MatchStringAdv(lyx::Buffer & buf, FindAndReplaceOptions const & opt)
837 : p_buf(&buf), p_first_buf(&buf), opt(opt)
839 Buffer & find_buf = *theBufferList().getBuffer(FileName(to_utf8(opt.find_buf_name)), true);
840 docstring const & ds = stringifySearchBuffer(find_buf, opt);
841 use_regexp = lyx::to_utf8(ds).find("\\regexp{") != std::string::npos;
842 // When using regexp, braces are hacked already by escape_for_regex()
843 par_as_string = normalize(ds, !use_regexp);
847 size_t lead_size = 0;
848 if (opt.ignoreformat) {
850 // if par_as_string_nolead were emty,
851 // the following call to findAux will always *find* the string
852 // in the checked data, and thus always using the slow
853 // examining of the current text part.
854 par_as_string_nolead = par_as_string;
858 lead_size = identifyLeading(par_as_string);
859 lead_as_string = par_as_string.substr(0, lead_size);
860 par_as_string_nolead = par_as_string.substr(lead_size, par_as_string.size() - lead_size);
864 open_braces = identifyClosing(par_as_string);
865 identifyClosing(par_as_string_nolead);
866 LYXERR(Debug::FIND, "Open braces: " << open_braces);
867 LYXERR(Debug::FIND, "Built MatchStringAdv object: par_as_string = '" << par_as_string << "'");
869 string lead_as_regexp;
871 // @todo No need to search for \regexp{} insets in leading material
872 lead_as_regexp = escape_for_regex(par_as_string.substr(0, lead_size), !opt.ignoreformat);
873 par_as_string = par_as_string_nolead;
874 LYXERR(Debug::FIND, "lead_as_regexp is '" << lead_as_regexp << "'");
875 LYXERR(Debug::FIND, "par_as_string now is '" << par_as_string << "'");
877 par_as_string = escape_for_regex(par_as_string, !opt.ignoreformat);
878 // Insert (.*?) before trailing closure of math, macros and environments, so to catch parts of them.
879 LYXERR(Debug::FIND, "par_as_string now is '" << par_as_string << "'");
881 // Insert .* before trailing '\$' ('$' has been escaped by escape_for_regex)
882 regex_replace(par_as_string, par_as_string, "(.*[^\\\\])(\\\\\\$)\\'", "$1(.*?)$2")
883 // Insert .* before trailing '\\\]' ('\]' has been escaped by escape_for_regex)
884 || regex_replace(par_as_string, par_as_string, "(.*[^\\\\])( \\\\\\\\\\\\\\])\\'", "$1(.*?)$2")
885 // Insert .* before trailing '\\end\{...}' ('\end{...}' has been escaped by escape_for_regex)
886 || regex_replace(par_as_string, par_as_string,
887 "(.*[^\\\\])( \\\\\\\\end\\\\\\{[a-zA-Z_]*)(\\\\\\*)?(\\\\\\})\\'", "$1(.*?)$2$3$4")
888 // Insert .* before trailing '\}' ('}' has been escaped by escape_for_regex)
889 || regex_replace(par_as_string, par_as_string, "(.*[^\\\\])(\\\\\\})\\'", "$1(.*?)$2")
893 LYXERR(Debug::FIND, "par_as_string now is '" << par_as_string << "'");
894 LYXERR(Debug::FIND, "Open braces: " << open_braces);
895 LYXERR(Debug::FIND, "Close .*? : " << close_wildcards);
896 LYXERR(Debug::FIND, "Replaced text (to be used as regex): " << par_as_string);
897 // If entered regexp must match at begin of searched string buffer
898 string regexp_str = string("\\`") + lead_as_regexp + par_as_string;
899 LYXERR(Debug::FIND, "Setting regexp to : '" << regexp_str << "'");
900 regexp = lyx::regex(regexp_str);
902 // If entered regexp may match wherever in searched string buffer
903 string regexp2_str = string("\\`.*") + lead_as_regexp + ".*" + par_as_string;
904 LYXERR(Debug::FIND, "Setting regexp2 to: '" << regexp2_str << "'");
905 regexp2 = lyx::regex(regexp2_str);
910 int MatchStringAdv::findAux(DocIterator const & cur, int len, bool at_begin) const
913 (opt.restr == FindAndReplaceOptions::R_ONLY_MATHS && !cur.inMathed()) )
915 docstring docstr = stringifyFromForSearch(opt, cur, len);
916 LYXERR(Debug::FIND, "Matching against '" << lyx::to_utf8(docstr) << "'");
917 string str = normalize(docstr, true);
918 LYXERR(Debug::FIND, "After normalization: '" << str << "'");
920 LYXERR(Debug::FIND, "Searching in normal mode: par_as_string='" << par_as_string << "', str='" << str << "'");
921 LYXERR(Debug::FIND, "Searching in normal mode: lead_as_string='" << lead_as_string << "', par_as_string_nolead='" << par_as_string_nolead << "'");
923 LYXERR(Debug::FIND, "size=" << par_as_string.size() << ", substr='" << str.substr(0, par_as_string.size()) << "'");
924 if (str.substr(0, par_as_string.size()) == par_as_string)
925 return par_as_string.size();
927 size_t pos = str.find(par_as_string_nolead);
928 if (pos != string::npos)
929 return par_as_string.size();
932 LYXERR(Debug::FIND, "Searching in regexp mode: at_begin=" << at_begin);
933 // Try all possible regexp matches,
934 //until one that verifies the braces match test is found
935 regex const *p_regexp = at_begin ? ®exp : ®exp2;
936 sregex_iterator re_it(str.begin(), str.end(), *p_regexp);
937 sregex_iterator re_it_end;
938 for (; re_it != re_it_end; ++re_it) {
939 match_results<string::const_iterator> const & m = *re_it;
940 // Check braces on the segment that matched the entire regexp expression,
941 // plus the last subexpression, if a (.*?) was inserted in the constructor.
942 if (!braces_match(m[0].first, m[0].second, open_braces))
944 // Check braces on segments that matched all (.*?) subexpressions,
945 // except the last "padding" one inserted by lyx.
946 for (size_t i = 1; i < m.size() - 1; ++i)
947 if (!braces_match(m[i].first, m[i].second))
949 // Exclude from the returned match length any length
950 // due to close wildcards added at end of regexp
951 if (close_wildcards == 0)
952 return m[0].second - m[0].first;
954 return m[m.size() - close_wildcards].first - m[0].first;
961 int MatchStringAdv::operator()(DocIterator const & cur, int len, bool at_begin) const
963 int res = findAux(cur, len, at_begin);
965 "res=" << res << ", at_begin=" << at_begin
966 << ", matchword=" << opt.matchword
967 << ", inTexted=" << cur.inTexted());
968 if (res == 0 || !at_begin || !opt.matchword || !cur.inTexted())
970 Paragraph const & par = cur.paragraph();
971 bool ws_left = (cur.pos() > 0)
972 ? par.isWordSeparator(cur.pos() - 1)
974 bool ws_right = (cur.pos() + res < par.size())
975 ? par.isWordSeparator(cur.pos() + res)
978 "cur.pos()=" << cur.pos() << ", res=" << res
979 << ", separ: " << ws_left << ", " << ws_right
981 if (ws_left && ws_right)
987 string MatchStringAdv::normalize(docstring const & s, bool hack_braces) const
990 if (! opt.casesensitive)
991 t = lyx::to_utf8(lowercase(s));
994 // Remove \n at begin
995 while (!t.empty() && t[0] == '\n')
998 while (!t.empty() && t[t.size() - 1] == '\n')
999 t = t.substr(0, t.size() - 1);
1001 // Replace all other \n with spaces
1002 while ((pos = t.find("\n")) != string::npos)
1003 t.replace(pos, 1, " ");
1004 // Remove stale empty \emph{}, \textbf{} and similar blocks from latexify
1005 LYXERR(Debug::FIND, "Removing stale empty \\emph{}, \\textbf{}, \\*section{} macros from: " << t);
1006 while (regex_replace(t, t, "\\\\(emph|textbf|subsubsection|subsection|section|subparagraph|paragraph|part)(\\{\\})+", ""))
1007 LYXERR(Debug::FIND, " further removing stale empty \\emph{}, \\textbf{} macros from: " << t);
1009 // FIXME - check what preceeds the brace
1011 if (opt.ignoreformat)
1012 while (regex_replace(t, t, "\\{", "_x_<")
1013 || regex_replace(t, t, "\\}", "_x_>"))
1014 LYXERR(Debug::FIND, "After {} replacement: '" << t << "'");
1016 while (regex_replace(t, t, "\\\\\\{", "_x_<")
1017 || regex_replace(t, t, "\\\\\\}", "_x_>"))
1018 LYXERR(Debug::FIND, "After {} replacement: '" << t << "'");
1025 docstring stringifyFromCursor(DocIterator const & cur, int len)
1027 LYXERR(Debug::FIND, "Stringifying with len=" << len << " from cursor at pos: " << cur);
1028 if (cur.inTexted()) {
1029 Paragraph const & par = cur.paragraph();
1030 // TODO what about searching beyond/across paragraph breaks ?
1031 // TODO Try adding a AS_STR_INSERTS as last arg
1032 pos_type end = ( len == -1 || cur.pos() + len > int(par.size()) ) ?
1033 int(par.size()) : cur.pos() + len;
1034 OutputParams runparams(&cur.buffer()->params().encoding());
1035 odocstringstream os;
1036 runparams.nice = true;
1037 runparams.flavor = OutputParams::LATEX;
1038 runparams.linelen = 100000; //lyxrc.plaintext_linelen;
1039 // No side effect of file copying and image conversion
1040 runparams.dryrun = true;
1041 LYXERR(Debug::FIND, "Stringifying with cur: "
1042 << cur << ", from pos: " << cur.pos() << ", end: " << end);
1043 return par.stringify(cur.pos(), end, AS_STR_INSETS, runparams);
1044 } else if (cur.inMathed()) {
1046 CursorSlice cs = cur.top();
1047 MathData md = cs.cell();
1048 MathData::const_iterator it_end =
1049 (( len == -1 || cs.pos() + len > int(md.size()))
1051 : md.begin() + cs.pos() + len );
1052 for (MathData::const_iterator it = md.begin() + cs.pos();
1054 s = s + asString(*it);
1055 LYXERR(Debug::FIND, "Stringified math: '" << s << "'");
1058 LYXERR(Debug::FIND, "Don't know how to stringify from here: " << cur);
1063 /** Computes the LaTeX export of buf starting from cur and ending len positions
1064 * after cur, if len is positive, or at the paragraph or innermost inset end
1067 docstring latexifyFromCursor(DocIterator const & cur, int len)
1069 LYXERR(Debug::FIND, "Latexifying with len=" << len << " from cursor at pos: " << cur);
1070 LYXERR(Debug::FIND, " with cur.lastpost=" << cur.lastpos() << ", cur.lastrow="
1071 << cur.lastrow() << ", cur.lastcol=" << cur.lastcol());
1072 Buffer const & buf = *cur.buffer();
1073 LBUFERR(buf.params().isLatex());
1076 odocstringstream ods;
1077 otexstream os(ods, texrow);
1078 OutputParams runparams(&buf.params().encoding());
1079 runparams.nice = false;
1080 runparams.flavor = OutputParams::LATEX;
1081 runparams.linelen = 8000; //lyxrc.plaintext_linelen;
1082 // No side effect of file copying and image conversion
1083 runparams.dryrun = true;
1085 if (cur.inTexted()) {
1086 // @TODO what about searching beyond/across paragraph breaks ?
1087 pos_type endpos = cur.paragraph().size();
1088 if (len != -1 && endpos > cur.pos() + len)
1089 endpos = cur.pos() + len;
1090 TeXOnePar(buf, *cur.innerText(), cur.pit(), os, runparams,
1091 string(), cur.pos(), endpos);
1092 LYXERR(Debug::FIND, "Latexified text: '" << lyx::to_utf8(ods.str()) << "'");
1093 } else if (cur.inMathed()) {
1094 // Retrieve the math environment type, and add '$' or '$[' or others (\begin{equation}) accordingly
1095 for (int s = cur.depth() - 1; s >= 0; --s) {
1096 CursorSlice const & cs = cur[s];
1097 if (cs.asInsetMath() && cs.asInsetMath() && cs.asInsetMath()->asHullInset()) {
1098 WriteStream ws(ods);
1099 cs.asInsetMath()->asHullInset()->header_write(ws);
1104 CursorSlice const & cs = cur.top();
1105 MathData md = cs.cell();
1106 MathData::const_iterator it_end =
1107 ((len == -1 || cs.pos() + len > int(md.size()))
1109 : md.begin() + cs.pos() + len);
1110 for (MathData::const_iterator it = md.begin() + cs.pos();
1112 ods << asString(*it);
1114 // Retrieve the math environment type, and add '$' or '$]'
1115 // or others (\end{equation}) accordingly
1116 for (int s = cur.depth() - 1; s >= 0; --s) {
1117 CursorSlice const & cs = cur[s];
1118 InsetMath * inset = cs.asInsetMath();
1119 if (inset && inset->asHullInset()) {
1120 WriteStream ws(ods);
1121 inset->asHullInset()->footer_write(ws);
1125 LYXERR(Debug::FIND, "Latexified math: '" << lyx::to_utf8(ods.str()) << "'");
1127 LYXERR(Debug::FIND, "Don't know how to stringify from here: " << cur);
1133 /** Finalize an advanced find operation, advancing the cursor to the innermost
1134 ** position that matches, plus computing the length of the matching text to
1137 int findAdvFinalize(DocIterator & cur, MatchStringAdv const & match)
1139 // Search the foremost position that matches (avoids find of entire math
1140 // inset when match at start of it)
1142 DocIterator old_cur(cur.buffer());
1144 LYXERR(Debug::FIND, "Forwarding one step (searching for innermost match)");
1148 } while (cur && cur.depth() > d && match(cur) > 0);
1150 LASSERT(match(cur) > 0, return 0);
1151 LYXERR(Debug::FIND, "Ok");
1153 // Compute the match length
1155 if (cur.pos() + len > cur.lastpos())
1157 LYXERR(Debug::FIND, "verifying unmatch with len = " << len);
1158 while (cur.pos() + len <= cur.lastpos() && match(cur, len) == 0) {
1160 LYXERR(Debug::FIND, "verifying unmatch with len = " << len);
1162 // Length of matched text (different from len param)
1163 int old_len = match(cur, len);
1165 // Greedy behaviour while matching regexps
1166 while ((new_len = match(cur, len + 1)) > old_len) {
1169 LYXERR(Debug::FIND, "verifying match with len = " << len);
1176 int findForwardAdv(DocIterator & cur, MatchStringAdv & match)
1180 while (!theApp()->longOperationCancelled() && cur) {
1181 LYXERR(Debug::FIND, "findForwardAdv() cur: " << cur);
1182 int match_len = match(cur, -1, false);
1183 LYXERR(Debug::FIND, "match_len: " << match_len);
1185 for (; !theApp()->longOperationCancelled() && cur; cur.forwardPos()) {
1186 LYXERR(Debug::FIND, "Advancing cur: " << cur);
1187 int match_len = match(cur);
1188 LYXERR(Debug::FIND, "match_len: " << match_len);
1190 // Sometimes in finalize we understand it wasn't a match
1191 // and we need to continue the outest loop
1192 int len = findAdvFinalize(cur, match);
1200 if (cur.pit() < cur.lastpit()) {
1201 LYXERR(Debug::FIND, "Advancing par: cur=" << cur);
1204 // This should exit nested insets, if any, or otherwise undefine the currsor.
1205 cur.pos() = cur.lastpos();
1206 LYXERR(Debug::FIND, "Advancing pos: cur=" << cur);
1214 /// Find the most backward consecutive match within same paragraph while searching backwards.
1215 int findMostBackwards(DocIterator & cur, MatchStringAdv const & match)
1217 DocIterator cur_begin = doc_iterator_begin(cur.buffer());
1218 DocIterator tmp_cur = cur;
1219 int len = findAdvFinalize(tmp_cur, match);
1220 Inset & inset = cur.inset();
1221 for (; cur != cur_begin; cur.backwardPos()) {
1222 LYXERR(Debug::FIND, "findMostBackwards(): cur=" << cur);
1223 DocIterator new_cur = cur;
1224 new_cur.backwardPos();
1225 if (new_cur == cur || &new_cur.inset() != &inset || !match(new_cur))
1227 int new_len = findAdvFinalize(new_cur, match);
1232 LYXERR(Debug::FIND, "findMostBackwards(): exiting with cur=" << cur);
1238 int findBackwardsAdv(DocIterator & cur, MatchStringAdv & match)
1242 // Backup of original position
1243 DocIterator cur_begin = doc_iterator_begin(cur.buffer());
1244 if (cur == cur_begin)
1247 DocIterator cur_orig(cur);
1249 bool pit_changed = false;
1250 found_match = false;
1253 found_match = match(cur, -1, false);
1257 cur.pos() = cur.lastpos();
1259 cur.pos() = cur_orig.pos();
1260 LYXERR(Debug::FIND, "findBackAdv2: cur: " << cur);
1261 DocIterator cur_prev_iter;
1263 found_match = match(cur);
1264 LYXERR(Debug::FIND, "findBackAdv3: found_match="
1265 << found_match << ", cur: " << cur);
1267 return findMostBackwards(cur, match);
1269 // Stop if begin of document reached
1270 if (cur == cur_begin)
1272 cur_prev_iter = cur;
1276 if (cur == cur_begin)
1283 } while (!theApp()->longOperationCancelled());
1288 } // anonym namespace
1291 docstring stringifyFromForSearch(FindAndReplaceOptions const & opt,
1292 DocIterator const & cur, int len)
1294 LASSERT(cur.pos() >= 0 && cur.pos() <= cur.lastpos(),
1295 return docstring());
1296 if (!opt.ignoreformat)
1297 return latexifyFromCursor(cur, len);
1299 return stringifyFromCursor(cur, len);
1303 FindAndReplaceOptions::FindAndReplaceOptions(
1304 docstring const & find_buf_name, bool casesensitive,
1305 bool matchword, bool forward, bool expandmacros, bool ignoreformat,
1306 docstring const & repl_buf_name, bool keep_case,
1307 SearchScope scope, SearchRestriction restr)
1308 : find_buf_name(find_buf_name), casesensitive(casesensitive), matchword(matchword),
1309 forward(forward), expandmacros(expandmacros), ignoreformat(ignoreformat),
1310 repl_buf_name(repl_buf_name), keep_case(keep_case), scope(scope), restr(restr)
1318 /** Check if 'len' letters following cursor are all non-lowercase */
1319 static bool allNonLowercase(Cursor const & cur, int len)
1321 pos_type beg_pos = cur.selectionBegin().pos();
1322 pos_type end_pos = cur.selectionBegin().pos() + len;
1323 if (len > cur.lastpos() + 1 - beg_pos) {
1324 LYXERR(Debug::FIND, "This should not happen, more debug needed");
1325 len = cur.lastpos() + 1 - beg_pos;
1327 for (pos_type pos = beg_pos; pos != end_pos; ++pos)
1328 if (isLowerCase(cur.paragraph().getChar(pos)))
1334 /** Check if first letter is upper case and second one is lower case */
1335 static bool firstUppercase(Cursor const & cur)
1338 pos_type pos = cur.selectionBegin().pos();
1339 if (pos >= cur.lastpos() - 1) {
1340 LYXERR(Debug::FIND, "No upper-case at cur: " << cur);
1343 ch1 = cur.paragraph().getChar(pos);
1344 ch2 = cur.paragraph().getChar(pos + 1);
1345 bool result = isUpperCase(ch1) && isLowerCase(ch2);
1346 LYXERR(Debug::FIND, "firstUppercase(): "
1347 << "ch1=" << ch1 << "(" << char(ch1) << "), ch2="
1348 << ch2 << "(" << char(ch2) << ")"
1349 << ", result=" << result << ", cur=" << cur);
1354 /** Make first letter of supplied buffer upper-case, and the rest lower-case.
1356 ** \fixme What to do with possible further paragraphs in replace buffer ?
1358 static void changeFirstCase(Buffer & buffer, TextCase first_case, TextCase others_case)
1360 ParagraphList::iterator pit = buffer.paragraphs().begin();
1361 LASSERT(pit->size() >= 1, /**/);
1362 pos_type right = pos_type(1);
1363 pit->changeCase(buffer.params(), pos_type(0), right, first_case);
1364 right = pit->size();
1365 pit->changeCase(buffer.params(), pos_type(1), right, others_case);
1371 static void findAdvReplace(BufferView * bv, FindAndReplaceOptions const & opt, MatchStringAdv & matchAdv)
1373 Cursor & cur = bv->cursor();
1374 if (opt.repl_buf_name == docstring())
1377 DocIterator sel_beg = cur.selectionBegin();
1378 DocIterator sel_end = cur.selectionEnd();
1379 if (&sel_beg.inset() != &sel_end.inset()
1380 || sel_beg.pit() != sel_end.pit())
1382 int sel_len = sel_end.pos() - sel_beg.pos();
1383 LYXERR(Debug::FIND, "sel_beg: " << sel_beg << ", sel_end: " << sel_end
1384 << ", sel_len: " << sel_len << endl);
1387 LASSERT(sel_len > 0, return);
1389 if (!matchAdv(sel_beg, sel_len))
1392 // Build a copy of the replace buffer, adapted to the KeepCase option
1393 Buffer & repl_buffer_orig = *theBufferList().getBuffer(FileName(to_utf8(opt.repl_buf_name)), true);
1395 repl_buffer_orig.write(oss);
1396 string lyx = oss.str();
1397 Buffer repl_buffer("", false);
1398 repl_buffer.setUnnamed(true);
1399 LASSERT(repl_buffer.readString(lyx), return);
1400 if (opt.keep_case && sel_len >= 2) {
1401 LYXERR(Debug::FIND, "keep_case true: cur.pos()=" << cur.pos() << ", sel_len=" << sel_len);
1402 if (cur.inTexted()) {
1403 if (firstUppercase(cur))
1404 changeFirstCase(repl_buffer, text_uppercase, text_lowercase);
1405 else if (allNonLowercase(cur, sel_len))
1406 changeFirstCase(repl_buffer, text_uppercase, text_uppercase);
1409 cap::cutSelection(cur, false, false);
1410 if (cur.inTexted()) {
1411 repl_buffer.changeLanguage(
1412 repl_buffer.language(),
1413 cur.getFont().language());
1414 LYXERR(Debug::FIND, "Replacing by pasteParagraphList()ing repl_buffer");
1415 LYXERR(Debug::FIND, "Before pasteParagraphList() cur=" << cur << endl);
1416 cap::pasteParagraphList(cur, repl_buffer.paragraphs(),
1417 repl_buffer.params().documentClassPtr(),
1418 bv->buffer().errorList("Paste"));
1419 LYXERR(Debug::FIND, "After pasteParagraphList() cur=" << cur << endl);
1420 sel_len = repl_buffer.paragraphs().begin()->size();
1421 } else if (cur.inMathed()) {
1423 odocstringstream ods;
1424 otexstream os(ods, texrow);
1425 OutputParams runparams(&repl_buffer.params().encoding());
1426 runparams.nice = false;
1427 runparams.flavor = OutputParams::LATEX;
1428 runparams.linelen = 8000; //lyxrc.plaintext_linelen;
1429 runparams.dryrun = true;
1430 TeXOnePar(repl_buffer, repl_buffer.text(), 0, os, runparams);
1431 //repl_buffer.getSourceCode(ods, 0, repl_buffer.paragraphs().size(), false);
1432 docstring repl_latex = ods.str();
1433 LYXERR(Debug::FIND, "Latexified replace_buffer: '" << repl_latex << "'");
1435 regex_replace(to_utf8(repl_latex), s, "\\$(.*)\\$", "$1");
1436 regex_replace(s, s, "\\\\\\[(.*)\\\\\\]", "$1");
1437 repl_latex = from_utf8(s);
1438 LYXERR(Debug::FIND, "Replacing by insert()ing latex: '" << repl_latex << "' cur=" << cur << " with depth=" << cur.depth());
1439 MathData ar(cur.buffer());
1440 asArray(repl_latex, ar, Parse::NORMAL);
1442 sel_len = ar.size();
1443 LYXERR(Debug::FIND, "After insert() cur=" << cur << " with depth: " << cur.depth() << " and len: " << sel_len);
1445 if (cur.pos() >= sel_len)
1446 cur.pos() -= sel_len;
1449 LYXERR(Debug::FIND, "After pos adj cur=" << cur << " with depth: " << cur.depth() << " and len: " << sel_len);
1450 bv->putSelectionAt(DocIterator(cur), sel_len, !opt.forward);
1451 bv->processUpdateFlags(Update::Force);
1452 bv->buffer().updatePreviews();
1456 /// Perform a FindAdv operation.
1457 bool findAdv(BufferView * bv, FindAndReplaceOptions const & opt)
1463 MatchStringAdv matchAdv(bv->buffer(), opt);
1464 int length = bv->cursor().selectionEnd().pos() - bv->cursor().selectionBegin().pos();
1466 bv->putSelectionAt(bv->cursor().selectionBegin(), length, !opt.forward);
1467 findAdvReplace(bv, opt, matchAdv);
1470 match_len = findForwardAdv(cur, matchAdv);
1472 match_len = findBackwardsAdv(cur, matchAdv);
1474 // This may only be raised by lyx::regex()
1475 bv->message(_("Invalid regular expression!"));
1479 if (match_len == 0) {
1480 bv->message(_("Match not found!"));
1484 bv->message(_("Match found!"));
1486 LYXERR(Debug::FIND, "Putting selection at cur=" << cur << " with len: " << match_len);
1487 bv->putSelectionAt(cur, match_len, !opt.forward);
1493 ostringstream & operator<<(ostringstream & os, FindAndReplaceOptions const & opt)
1495 os << to_utf8(opt.find_buf_name) << "\nEOSS\n"
1496 << opt.casesensitive << ' '
1497 << opt.matchword << ' '
1498 << opt.forward << ' '
1499 << opt.expandmacros << ' '
1500 << opt.ignoreformat << ' '
1501 << to_utf8(opt.repl_buf_name) << "\nEOSS\n"
1502 << opt.keep_case << ' '
1503 << int(opt.scope) << ' '
1506 LYXERR(Debug::FIND, "built: " << os.str());
1512 istringstream & operator>>(istringstream & is, FindAndReplaceOptions & opt)
1514 LYXERR(Debug::FIND, "parsing");
1518 while (line != "EOSS") {
1522 if (is.eof()) // Tolerate malformed request
1526 LYXERR(Debug::FIND, "file_buf_name: '" << s << "'");
1527 opt.find_buf_name = from_utf8(s);
1528 is >> opt.casesensitive >> opt.matchword >> opt.forward >> opt.expandmacros >> opt.ignoreformat;
1529 is.get(); // Waste space before replace string
1532 while (line != "EOSS") {
1536 if (is.eof()) // Tolerate malformed request
1540 LYXERR(Debug::FIND, "repl_buf_name: '" << s << "'");
1541 opt.repl_buf_name = from_utf8(s);
1542 is >> opt.keep_case;
1545 opt.scope = FindAndReplaceOptions::SearchScope(i);
1547 opt.restr = FindAndReplaceOptions::SearchRestriction(i);
1549 LYXERR(Debug::FIND, "parsed: " << opt.casesensitive << ' ' << opt.matchword << ' ' << opt.forward << ' '
1550 << opt.expandmacros << ' ' << opt.ignoreformat << ' ' << opt.keep_case << ' '
1551 << opt.scope << ' ' << opt.restr);