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/gettext.h"
49 #include "support/lassert.h"
50 #include "support/lstrings.h"
52 #include "support/regex.h"
53 #include <boost/next_prior.hpp>
56 using namespace lyx::support;
62 bool parse_bool(docstring & howto)
67 howto = split(howto, var, ' ');
72 class MatchString : public binary_function<Paragraph, pos_type, int>
75 MatchString(docstring const & str, bool cs, bool mw)
76 : str(str), case_sens(cs), whole_words(mw)
79 // returns true if the specified string is at the specified position
80 // del specifies whether deleted strings in ct mode will be considered
81 int operator()(Paragraph const & par, pos_type pos, bool del = true) const
83 return par.find(str, case_sens, whole_words, pos, del);
91 // match whole words only
96 int findForward(DocIterator & cur, MatchString const & match,
99 for (; cur; cur.forwardChar())
100 if (cur.inTexted()) {
101 int len = match(cur.paragraph(), cur.pos(), find_del);
109 int findBackwards(DocIterator & cur, MatchString const & match,
110 bool find_del = true)
114 if (cur.inTexted()) {
115 int len = match(cur.paragraph(), cur.pos(), find_del);
124 bool findChange(DocIterator & cur, bool next)
128 for (; cur; next ? cur.forwardPos() : cur.backwardPos())
129 if (cur.inTexted() && cur.paragraph().isChanged(cur.pos())) {
131 // if we search backwards, take a step forward
132 // to correctly set the anchor
141 bool searchAllowed(docstring const & str)
144 frontend::Alert::error(_("Search error"), _("Search string is empty"));
151 bool findOne(BufferView * bv, docstring const & searchstr,
152 bool case_sens, bool whole, bool forward, bool find_del = true)
154 if (!searchAllowed(searchstr))
157 DocIterator cur = bv->cursor();
159 MatchString const match(searchstr, case_sens, whole);
161 int match_len = forward ? findForward(cur, match, find_del) :
162 findBackwards(cur, match, find_del);
165 bv->putSelectionAt(cur, match_len, !forward);
167 return match_len > 0;
171 int replaceAll(BufferView * bv,
172 docstring const & searchstr, docstring const & replacestr,
173 bool case_sens, bool whole)
175 Buffer & buf = bv->buffer();
177 if (!searchAllowed(searchstr) || buf.isReadonly())
180 DocIterator cur_orig(bv->cursor());
182 MatchString const match(searchstr, case_sens, whole);
185 int const rsize = replacestr.size();
186 int const ssize = searchstr.size();
189 cur.setCursor(doc_iterator_begin(&buf));
190 int match_len = findForward(cur, match, false);
191 while (match_len > 0) {
192 // Backup current cursor position and font.
193 pos_type const pos = cur.pos();
194 Font const font = cur.paragraph().getFontSettings(buf.params(), pos);
196 int striked = ssize - cur.paragraph().eraseChars(pos, pos + match_len,
197 buf.params().trackChanges);
198 cur.paragraph().insert(pos, replacestr, font,
199 Change(buf.params().trackChanges ?
200 Change::INSERTED : Change::UNCHANGED));
201 for (int i = 0; i < rsize + striked; ++i)
204 match_len = findForward(cur, match, false);
207 bv->putSelectionAt(doc_iterator_begin(&buf), 0, false);
209 cur_orig.fixIfBroken();
210 bv->setCursor(cur_orig);
216 // the idea here is that we are going to replace the string that
217 // is selected IF it is the search string.
218 // if there is a selection, but it is not the search string, then
219 // we basically ignore it. (FIXME We ought to replace only within
221 // if there is no selection, then:
222 // (i) if some search string has been provided, then we find it.
223 // (think of how the dialog works when you hit "replace" the
225 // (ii) if no search string has been provided, then we treat the
226 // word the cursor is in as the search string. (why? i have no
227 // idea.) but this only works in text?
229 // returns the number of replacements made (one, if any) and
230 // whether anything at all was done.
231 pair<bool, int> replaceOne(BufferView * bv, docstring searchstr,
232 docstring const & replacestr, bool case_sens,
233 bool whole, bool forward, bool findnext)
235 Cursor & cur = bv->cursor();
236 if (!cur.selection()) {
237 // no selection, non-empty search string: find it
238 if (!searchstr.empty()) {
239 findOne(bv, searchstr, case_sens, whole, forward);
240 return pair<bool, int>(true, 0);
242 // empty search string
245 return pair<bool, int>(false, 0);
246 // select current word and treat it as the search string
247 cur.innerText()->selectWord(cur, WHOLE_WORD);
248 searchstr = cur.selectionAsString(false);
251 // if we still don't have a search string, report the error
253 if (!searchAllowed(searchstr))
254 return pair<bool, int>(false, 0);
256 bool have_selection = cur.selection();
257 docstring const selected = cur.selectionAsString(false);
259 case_sens ? searchstr == selected
260 : compare_no_case(searchstr, selected) == 0;
262 // no selection or current selection is not search word:
263 // just find the search word
264 if (!have_selection || !match) {
265 findOne(bv, searchstr, case_sens, whole, forward);
266 return pair<bool, int>(true, 0);
269 // we're now actually ready to replace. if the buffer is
270 // read-only, we can't, though.
271 if (bv->buffer().isReadonly())
272 return pair<bool, int>(false, 0);
274 cap::replaceSelectionWithString(cur, replacestr);
276 cur.pos() += replacestr.length();
277 LASSERT(cur.pos() <= cur.lastpos(), /* */);
280 findOne(bv, searchstr, case_sens, whole, forward, false);
282 return pair<bool, int>(true, 1);
288 docstring const find2string(docstring const & search,
289 bool casesensitive, bool matchword, bool forward)
293 << int(casesensitive) << ' '
294 << int(matchword) << ' '
300 docstring const replace2string(docstring const & replace,
301 docstring const & search, bool casesensitive, bool matchword,
302 bool all, bool forward, bool findnext)
305 ss << replace << '\n'
307 << int(casesensitive) << ' '
308 << int(matchword) << ' '
310 << int(forward) << ' '
316 bool lyxfind(BufferView * bv, FuncRequest const & ev)
318 if (!bv || ev.action() != LFUN_WORD_FIND)
321 //lyxerr << "find called, cmd: " << ev << endl;
323 // data is of the form
325 // <casesensitive> <matchword> <forward>"
327 docstring howto = split(ev.argument(), search, '\n');
329 bool casesensitive = parse_bool(howto);
330 bool matchword = parse_bool(howto);
331 bool forward = parse_bool(howto);
333 return findOne(bv, search, casesensitive, matchword, forward);
337 bool lyxreplace(BufferView * bv,
338 FuncRequest const & ev, bool has_deleted)
340 if (!bv || ev.action() != LFUN_WORD_REPLACE)
343 // data is of the form
346 // <casesensitive> <matchword> <all> <forward> <findnext>"
349 docstring howto = split(ev.argument(), rplc, '\n');
350 howto = split(howto, search, '\n');
352 bool casesensitive = parse_bool(howto);
353 bool matchword = parse_bool(howto);
354 bool all = parse_bool(howto);
355 bool forward = parse_bool(howto);
356 bool findnext = howto.empty() ? true : parse_bool(howto);
358 int replace_count = 0;
363 replace_count = replaceAll(bv, search, rplc, casesensitive, matchword);
364 update = replace_count > 0;
367 replaceOne(bv, search, rplc, casesensitive, matchword, forward, findnext);
369 replace_count = rv.second;
372 Buffer const & buf = bv->buffer();
374 // emit message signal.
375 buf.message(_("String not found!"));
377 if (replace_count == 0) {
378 buf.message(_("String found."));
379 } else if (replace_count == 1) {
380 buf.message(_("String has been replaced."));
382 docstring const str =
383 bformat(_("%1$d strings have been replaced."), replace_count);
387 } else if (findnext) {
388 // if we have deleted characters, we do not replace at all, but
389 // rather search for the next occurence
390 if (findOne(bv, search, casesensitive, matchword, forward))
393 bv->message(_("String not found!"));
399 bool findNextChange(BufferView * bv)
401 return findChange(bv, true);
405 bool findPreviousChange(BufferView * bv)
407 return findChange(bv, false);
411 bool findChange(BufferView * bv, bool next)
413 if (bv->cursor().selection()) {
414 // set the cursor at the beginning or at the end of the selection
415 // before searching. Otherwise, the current change will be found.
416 if (next != (bv->cursor().top() > bv->cursor().normalAnchor()))
417 bv->cursor().setCursorToAnchor();
420 DocIterator cur = bv->cursor();
422 // Are we within a change ? Then first search forward (backward),
423 // clear the selection and search the other way around (see the end
424 // of this function). This will avoid changes to be selected half.
425 bool search_both_sides = false;
426 DocIterator tmpcur = cur;
428 while (tmpcur.inMathed())
430 Change change_next_pos
431 = tmpcur.paragraph().lookupChange(tmpcur.pos());
432 if (change_next_pos.changed() && cur.inMathed()) {
434 search_both_sides = true;
435 } else if (tmpcur.pos() > 0 && tmpcur.inTexted()) {
436 Change change_prev_pos
437 = tmpcur.paragraph().lookupChange(tmpcur.pos() - 1);
438 if (change_next_pos.isSimilarTo(change_prev_pos))
439 search_both_sides = true;
442 if (!findChange(cur, next))
445 bv->cursor().setCursor(cur);
446 bv->cursor().resetAnchor();
449 // take a step into the change
452 Change orig_change = cur.paragraph().lookupChange(cur.pos());
454 CursorSlice & tip = cur.top();
456 for (; !tip.at_end(); tip.forwardPos()) {
457 Change change = tip.paragraph().lookupChange(tip.pos());
458 if (!change.isSimilarTo(orig_change))
462 for (; !tip.at_begin();) {
464 Change change = tip.paragraph().lookupChange(tip.pos());
465 if (!change.isSimilarTo(orig_change)) {
466 // take a step forward to correctly set the selection
473 // Now put cursor to end of selection:
474 bv->cursor().setCursor(cur);
475 bv->cursor().setSelection();
477 if (search_both_sides) {
478 bv->cursor().setSelection(false);
479 findChange(bv, !next);
487 typedef vector<pair<string, string> > Escapes;
489 /// A map of symbols and their escaped equivalent needed within a regex.
490 /// @note Beware of order
491 Escapes const & get_regexp_escapes()
493 static Escapes escape_map;
494 if (escape_map.empty()) {
495 escape_map.push_back(pair<string, string>("$", "\\$"));
496 escape_map.push_back(pair<string, string>("{", "\\{"));
497 escape_map.push_back(pair<string, string>("}", "\\}"));
498 escape_map.push_back(pair<string, string>("[", "\\["));
499 escape_map.push_back(pair<string, string>("]", "\\]"));
500 escape_map.push_back(pair<string, string>("(", "\\("));
501 escape_map.push_back(pair<string, string>(")", "\\)"));
502 escape_map.push_back(pair<string, string>("+", "\\+"));
503 escape_map.push_back(pair<string, string>("*", "\\*"));
504 escape_map.push_back(pair<string, string>(".", "\\."));
505 escape_map.push_back(pair<string, string>("\\", "(?:\\\\|\\\\backslash)"));
506 escape_map.push_back(pair<string, string>("~", "(?:\\\\textasciitilde|\\\\sim)"));
507 escape_map.push_back(pair<string, string>("^", "(?:\\^|\\\\textasciicircum\\{\\}|\\\\mathcircumflex)"));
512 /// A map of lyx escaped strings and their unescaped equivalent.
513 Escapes const & get_lyx_unescapes() {
514 static Escapes escape_map;
515 if (escape_map.empty()) {
516 escape_map.push_back(pair<string, string>("\\%", "%"));
517 escape_map.push_back(pair<string, string>("\\mathcircumflex ", "^"));
518 escape_map.push_back(pair<string, string>("\\mathcircumflex", "^"));
519 escape_map.push_back(pair<string, string>("\\backslash ", "\\"));
520 escape_map.push_back(pair<string, string>("\\backslash", "\\"));
521 escape_map.push_back(pair<string, string>("\\\\{", "_x_<"));
522 escape_map.push_back(pair<string, string>("\\\\}", "_x_>"));
523 escape_map.push_back(pair<string, string>("\\sim ", "~"));
524 escape_map.push_back(pair<string, string>("\\sim", "~"));
529 /// A map of escapes turning a regexp matching text to one matching latex.
530 Escapes const & get_regexp_latex_escapes() {
531 static Escapes escape_map;
532 if (escape_map.empty()) {
533 escape_map.push_back(pair<string, string>("\\\\", "(?:\\\\\\\\|\\\\backslash|\\\\textbackslash\\{\\})"));
534 escape_map.push_back(pair<string, string>("(<?!\\\\\\\\textbackslash)\\{", "\\\\\\{"));
535 escape_map.push_back(pair<string, string>("(<?!\\\\\\\\textbackslash\\\\\\{)\\}", "\\\\\\}"));
536 escape_map.push_back(pair<string, string>("\\[", "\\{\\[\\}"));
537 escape_map.push_back(pair<string, string>("\\]", "\\{\\]\\}"));
538 escape_map.push_back(pair<string, string>("\\^", "(?:\\^|\\\\textasciicircum\\{\\}|\\\\mathcircumflex)"));
539 escape_map.push_back(pair<string, string>("%", "\\\\\\%"));
544 /** @todo Probably the maps need to be migrated to regexps, in order to distinguish if
545 ** the found occurrence were escaped.
547 string apply_escapes(string s, Escapes const & escape_map)
549 LYXERR(Debug::FIND, "Escaping: '" << s << "'");
550 Escapes::const_iterator it;
551 for (it = escape_map.begin(); it != escape_map.end(); ++it) {
552 // LYXERR(Debug::FIND, "Escaping " << it->first << " as " << it->second);
553 unsigned int pos = 0;
554 while (pos < s.length() && (pos = s.find(it->first, pos)) < s.length()) {
555 s.replace(pos, it->first.length(), it->second);
556 LYXERR(Debug::FIND, "After escape: " << s);
557 pos += it->second.length();
558 // LYXERR(Debug::FIND, "pos: " << pos);
561 LYXERR(Debug::FIND, "Escaped : '" << s << "'");
566 /// Within \regexp{} apply get_lyx_unescapes() only (i.e., preserve regexp semantics of the string),
567 /// while outside apply get_lyx_unescapes()+get_regexp_escapes().
568 /// If match_latex is true, then apply regexp_latex_escapes() to \regexp{} contents as well.
569 string escape_for_regex(string s, bool match_latex)
572 while (pos < s.size()) {
573 size_t new_pos = s.find("\\regexp{", pos);
574 if (new_pos == string::npos)
576 LYXERR(Debug::FIND, "new_pos: " << new_pos);
577 string t = apply_escapes(s.substr(pos, new_pos - pos), get_lyx_unescapes());
578 LYXERR(Debug::FIND, "t [lyx]: " << t);
579 t = apply_escapes(t, get_regexp_escapes());
580 LYXERR(Debug::FIND, "t [rxp]: " << t);
581 s.replace(pos, new_pos - pos, t);
582 new_pos = pos + t.size();
583 LYXERR(Debug::FIND, "Regexp after escaping: " << s);
584 LYXERR(Debug::FIND, "new_pos: " << new_pos);
585 if (new_pos == s.size())
587 // Might fail if \\endregexp{} is preceeded by unexpected stuff (weird escapes)
588 size_t end_pos = s.find("\\endregexp{}}", new_pos + 8);
589 LYXERR(Debug::FIND, "end_pos: " << end_pos);
590 t = s.substr(new_pos + 8, end_pos - (new_pos + 8));
591 LYXERR(Debug::FIND, "t in regexp : " << t);
592 t = apply_escapes(t, get_lyx_unescapes());
593 LYXERR(Debug::FIND, "t in regexp [lyx]: " << t);
595 t = apply_escapes(t, get_regexp_latex_escapes());
596 LYXERR(Debug::FIND, "t in regexp [ltx]: " << t);
598 if (end_pos == s.size()) {
599 s.replace(new_pos, end_pos - new_pos, t);
601 LYXERR(Debug::FIND, "Regexp after \\regexp{} removal: " << s);
604 s.replace(new_pos, end_pos + 13 - new_pos, t);
605 LYXERR(Debug::FIND, "Regexp after \\regexp{...\\endregexp{}} removal: " << s);
606 pos = new_pos + t.size();
607 LYXERR(Debug::FIND, "pos: " << pos);
612 /// Wrapper for lyx::regex_replace with simpler interface
613 bool regex_replace(string const & s, string & t, string const & searchstr,
614 string const & replacestr)
616 lyx::regex e(searchstr);
618 ostream_iterator<char, char> it(oss);
619 lyx::regex_replace(it, s.begin(), s.end(), e, replacestr);
620 // tolerate t and s be references to the same variable
621 bool rv = (s != oss.str());
626 /** Checks if supplied string segment is well-formed from the standpoint of matching open-closed braces.
628 ** Verify that closed braces exactly match open braces. This avoids that, for example,
629 ** \frac{.*}{x} matches \frac{x+\frac{y}{x}}{z} with .* being 'x+\frac{y'.
632 ** Number of open braces that must remain open at the end for the verification to succeed.
634 bool braces_match(string::const_iterator const & beg,
635 string::const_iterator const & end,
639 string::const_iterator it = beg;
640 LYXERR(Debug::FIND, "Checking " << unmatched << " unmatched braces in '" << string(beg, end) << "'");
641 for (; it != end; ++it) {
642 // Skip escaped braces in the count
647 } else if (*it == '{') {
649 } else if (*it == '}') {
650 if (open_pars == 0) {
651 LYXERR(Debug::FIND, "Found unmatched closed brace");
657 if (open_pars != unmatched) {
658 LYXERR(Debug::FIND, "Found " << open_pars
659 << " instead of " << unmatched
660 << " unmatched open braces at the end of count");
663 LYXERR(Debug::FIND, "Braces match as expected");
667 /** The class performing a match between a position in the document and the FindAdvOptions.
669 class MatchStringAdv {
671 MatchStringAdv(lyx::Buffer & buf, FindAndReplaceOptions const & opt);
673 /** Tests if text starting at the supplied position matches with the one provided to the MatchStringAdv
674 ** constructor as opt.search, under the opt.* options settings.
677 ** If set, then match is searched only against beginning of text starting at cur.
678 ** If unset, then match is searched anywhere in text starting at cur.
681 ** The length of the matching text, or zero if no match was found.
683 int operator()(DocIterator const & cur, int len = -1, bool at_begin = true) const;
688 /// first buffer on which search was started
689 lyx::Buffer * const p_first_buf;
691 FindAndReplaceOptions const & opt;
694 /// Auxiliary find method (does not account for opt.matchword)
695 int findAux(DocIterator const & cur, int len = -1, bool at_begin = true) const;
697 /** Normalize a stringified or latexified LyX paragraph.
701 ** <li>if search is not casesensitive, then lowercase the string;
702 ** <li>remove any newline at begin or end of the string;
703 ** <li>replace any newline in the middle of the string with a simple space;
704 ** <li>remove stale empty styles and environments, like \emph{} and \textbf{}.
707 ** @todo Normalization should also expand macros, if the corresponding
708 ** search option was checked.
710 string normalize(docstring const & s, bool hack_braces) const;
711 // normalized string to search
712 string par_as_string;
713 // regular expression to use for searching
715 // same as regexp, but prefixed with a ".*"
717 // leading format material as string
718 string lead_as_string;
719 // par_as_string after removal of lead_as_string
720 string par_as_string_nolead;
721 // unmatched open braces in the search string/regexp
723 // number of (.*?) subexpressions added at end of search regexp for closing
724 // environments, math mode, styles, etc...
726 // Are we searching with regular expressions ?
731 static docstring buffer_to_latex(Buffer & buffer)
733 OutputParams runparams(&buffer.params().encoding());
735 odocstringstream ods;
736 otexstream os(ods, texrow);
737 runparams.nice = true;
738 runparams.flavor = OutputParams::LATEX;
739 runparams.linelen = 80; //lyxrc.plaintext_linelen;
740 // No side effect of file copying and image conversion
741 runparams.dryrun = true;
742 pit_type const endpit = buffer.paragraphs().size();
743 for (pit_type pit = 0; pit != endpit; ++pit) {
744 TeXOnePar(buffer, buffer.text(), pit, os, runparams);
745 LYXERR(Debug::FIND, "searchString up to here: " << ods.str());
751 static docstring stringifySearchBuffer(Buffer & buffer, FindAndReplaceOptions const & opt) {
753 if (!opt.ignoreformat) {
754 str = buffer_to_latex(buffer);
756 OutputParams runparams(&buffer.params().encoding());
757 runparams.nice = true;
758 runparams.flavor = OutputParams::LATEX;
759 runparams.linelen = 100000; //lyxrc.plaintext_linelen;
760 runparams.dryrun = true;
761 for (pos_type pit = pos_type(0); pit < (pos_type)buffer.paragraphs().size(); ++pit) {
762 Paragraph const & par = buffer.paragraphs().at(pit);
763 LYXERR(Debug::FIND, "Adding to search string: '"
764 << par.stringify(pos_type(0), par.size(),
765 AS_STR_INSETS, runparams)
767 str += par.stringify(pos_type(0), par.size(),
768 AS_STR_INSETS, runparams);
775 /// Return separation pos between the leading material and the rest
776 static size_t identifyLeading(string const & s) {
778 // @TODO Support \item[text]
779 while (regex_replace(t, t, "^\\\\(emph|textbf|subsubsection|subsection|section|subparagraph|paragraph|part)\\*?\\{", "")
780 || regex_replace(t, t, "^\\$", "")
781 || regex_replace(t, t, "^\\\\\\[ ", "")
782 || regex_replace(t, t, "^\\\\item ", "")
783 || regex_replace(t, t, "^\\\\begin\\{[a-zA-Z_]*\\*?\\} ", ""))
784 LYXERR(Debug::FIND, " after removing leading $, \\[ , \\emph{, \\textbf{, etc.: '" << t << "'");
789 // Remove trailing closure of math, macros and environments, so to catch parts of them.
790 static int identifyClosing(string & t) {
793 LYXERR(Debug::FIND, "identifyClosing(): t now is '" << t << "'");
794 if (regex_replace(t, t, "(.*[^\\\\])\\$\\'", "$1"))
796 if (regex_replace(t, t, "(.*[^\\\\]) \\\\\\]\\'", "$1"))
798 if (regex_replace(t, t, "(.*[^\\\\]) \\\\end\\{[a-zA-Z_]*\\*?\\}\\'", "$1"))
800 if (regex_replace(t, t, "(.*[^\\\\])\\}\\'", "$1")) {
810 MatchStringAdv::MatchStringAdv(lyx::Buffer & buf, FindAndReplaceOptions const & opt)
811 : p_buf(&buf), p_first_buf(&buf), opt(opt)
813 Buffer & find_buf = *theBufferList().getBuffer(FileName(to_utf8(opt.find_buf_name)), true);
814 docstring const & ds = stringifySearchBuffer(find_buf, opt);
815 use_regexp = lyx::to_utf8(ds).find("\\regexp{") != std::string::npos;
816 // When using regexp, braces are hacked already by escape_for_regex()
817 par_as_string = normalize(ds, !use_regexp);
821 size_t lead_size = 0;
822 if (!opt.ignoreformat) {
823 lead_size = identifyLeading(par_as_string);
824 lead_as_string = par_as_string.substr(0, lead_size);
825 par_as_string_nolead = par_as_string.substr(lead_size, par_as_string.size() - lead_size);
829 open_braces = identifyClosing(par_as_string);
830 identifyClosing(par_as_string_nolead);
831 LYXERR(Debug::FIND, "Open braces: " << open_braces);
832 LYXERR(Debug::FIND, "Built MatchStringAdv object: par_as_string = '" << par_as_string << "'");
834 string lead_as_regexp;
836 // @todo No need to search for \regexp{} insets in leading material
837 lead_as_regexp = escape_for_regex(par_as_string.substr(0, lead_size), !opt.ignoreformat);
838 par_as_string = par_as_string_nolead;
839 LYXERR(Debug::FIND, "lead_as_regexp is '" << lead_as_regexp << "'");
840 LYXERR(Debug::FIND, "par_as_string now is '" << par_as_string << "'");
842 par_as_string = escape_for_regex(par_as_string, !opt.ignoreformat);
843 // Insert (.*?) before trailing closure of math, macros and environments, so to catch parts of them.
844 LYXERR(Debug::FIND, "par_as_string now is '" << par_as_string << "'");
846 // Insert .* before trailing '\$' ('$' has been escaped by escape_for_regex)
847 regex_replace(par_as_string, par_as_string, "(.*[^\\\\])(\\\\\\$)\\'", "$1(.*?)$2")
848 // Insert .* before trailing '\\\]' ('\]' has been escaped by escape_for_regex)
849 || regex_replace(par_as_string, par_as_string, "(.*[^\\\\])( \\\\\\\\\\\\\\])\\'", "$1(.*?)$2")
850 // Insert .* before trailing '\\end\{...}' ('\end{...}' has been escaped by escape_for_regex)
851 || regex_replace(par_as_string, par_as_string,
852 "(.*[^\\\\])( \\\\\\\\end\\\\\\{[a-zA-Z_]*)(\\\\\\*)?(\\\\\\})\\'", "$1(.*?)$2$3$4")
853 // Insert .* before trailing '\}' ('}' has been escaped by escape_for_regex)
854 || regex_replace(par_as_string, par_as_string, "(.*[^\\\\])(\\\\\\})\\'", "$1(.*?)$2")
858 LYXERR(Debug::FIND, "par_as_string now is '" << par_as_string << "'");
859 LYXERR(Debug::FIND, "Open braces: " << open_braces);
860 LYXERR(Debug::FIND, "Close .*? : " << close_wildcards);
861 LYXERR(Debug::FIND, "Replaced text (to be used as regex): " << par_as_string);
862 // If entered regexp must match at begin of searched string buffer
863 string regexp_str = string("\\`") + lead_as_regexp + par_as_string;
864 LYXERR(Debug::FIND, "Setting regexp to : '" << regexp_str << "'");
865 regexp = lyx::regex(regexp_str);
867 // If entered regexp may match wherever in searched string buffer
868 string regexp2_str = string("\\`.*") + lead_as_regexp + ".*" + par_as_string;
869 LYXERR(Debug::FIND, "Setting regexp2 to: '" << regexp2_str << "'");
870 regexp2 = lyx::regex(regexp2_str);
875 int MatchStringAdv::findAux(DocIterator const & cur, int len, bool at_begin) const
877 docstring docstr = stringifyFromForSearch(opt, cur, len);
878 LYXERR(Debug::FIND, "Matching against '" << lyx::to_utf8(docstr) << "'");
879 string str = normalize(docstr, true);
880 LYXERR(Debug::FIND, "After normalization: '" << str << "'");
882 LYXERR(Debug::FIND, "Searching in normal mode: par_as_string='" << par_as_string << "', str='" << str << "'");
883 LYXERR(Debug::FIND, "Searching in normal mode: lead_as_string='" << lead_as_string << "', par_as_string_nolead='" << par_as_string_nolead << "'");
885 LYXERR(Debug::FIND, "size=" << par_as_string.size() << ", substr='" << str.substr(0, par_as_string.size()) << "'");
886 if (str.substr(0, par_as_string.size()) == par_as_string)
887 return par_as_string.size();
889 size_t pos = str.find(par_as_string_nolead);
890 if (pos != string::npos)
891 return par_as_string.size();
894 LYXERR(Debug::FIND, "Searching in regexp mode: at_begin=" << at_begin);
895 // Try all possible regexp matches,
896 //until one that verifies the braces match test is found
897 regex const *p_regexp = at_begin ? ®exp : ®exp2;
898 sregex_iterator re_it(str.begin(), str.end(), *p_regexp);
899 sregex_iterator re_it_end;
900 for (; re_it != re_it_end; ++re_it) {
901 match_results<string::const_iterator> const & m = *re_it;
902 // Check braces on the segment that matched the entire regexp expression,
903 // plus the last subexpression, if a (.*?) was inserted in the constructor.
904 if (!braces_match(m[0].first, m[0].second, open_braces))
906 // Check braces on segments that matched all (.*?) subexpressions,
907 // except the last "padding" one inserted by lyx.
908 for (size_t i = 1; i < m.size() - 1; ++i)
909 if (!braces_match(m[i].first, m[i].second))
911 // Exclude from the returned match length any length
912 // due to close wildcards added at end of regexp
913 if (close_wildcards == 0)
914 return m[0].second - m[0].first;
916 return m[m.size() - close_wildcards].first - m[0].first;
923 int MatchStringAdv::operator()(DocIterator const & cur, int len, bool at_begin) const
925 int res = findAux(cur, len, at_begin);
927 "res=" << res << ", at_begin=" << at_begin << ", matchword=" << opt.matchword << ", inTexted=" << cur.inTexted());
928 if (res == 0 || !at_begin || !opt.matchword || !cur.inTexted())
930 Paragraph const & par = cur.paragraph();
931 bool ws_left = cur.pos() > 0 ?
932 par.isWordSeparator(cur.pos() - 1) : true;
933 bool ws_right = cur.pos() + res < par.size() ?
934 par.isWordSeparator(cur.pos() + res) : true;
936 "cur.pos()=" << cur.pos() << ", res=" << res
937 << ", separ: " << ws_left << ", " << ws_right
939 if (ws_left && ws_right)
945 string MatchStringAdv::normalize(docstring const & s, bool hack_braces) const
948 if (! opt.casesensitive)
949 t = lyx::to_utf8(lowercase(s));
952 // Remove \n at begin
953 while (t.size() > 0 && t[0] == '\n')
956 while (t.size() > 0 && t[t.size() - 1] == '\n')
957 t = t.substr(0, t.size() - 1);
959 // Replace all other \n with spaces
960 while ((pos = t.find("\n")) != string::npos)
961 t.replace(pos, 1, " ");
962 // Remove stale empty \emph{}, \textbf{} and similar blocks from latexify
963 LYXERR(Debug::FIND, "Removing stale empty \\emph{}, \\textbf{}, \\*section{} macros from: " << t);
964 while (regex_replace(t, t, "\\\\(emph|textbf|subsubsection|subsection|section|subparagraph|paragraph|part)(\\{\\})+", ""))
965 LYXERR(Debug::FIND, " further removing stale empty \\emph{}, \\textbf{} macros from: " << t);
967 // FIXME - check what preceeds the brace
969 if (opt.ignoreformat)
970 while (regex_replace(t, t, "\\{", "_x_<")
971 || regex_replace(t, t, "\\}", "_x_>"))
972 LYXERR(Debug::FIND, "After {} replacement: '" << t << "'");
974 while (regex_replace(t, t, "\\\\\\{", "_x_<")
975 || regex_replace(t, t, "\\\\\\}", "_x_>"))
976 LYXERR(Debug::FIND, "After {} replacement: '" << t << "'");
983 docstring stringifyFromCursor(DocIterator const & cur, int len)
985 LYXERR(Debug::FIND, "Stringifying with len=" << len << " from cursor at pos: " << cur);
986 if (cur.inTexted()) {
987 Paragraph const & par = cur.paragraph();
988 // TODO what about searching beyond/across paragraph breaks ?
989 // TODO Try adding a AS_STR_INSERTS as last arg
990 pos_type end = ( len == -1 || cur.pos() + len > int(par.size()) ) ?
991 int(par.size()) : cur.pos() + len;
992 OutputParams runparams(&cur.buffer()->params().encoding());
994 runparams.nice = true;
995 runparams.flavor = OutputParams::LATEX;
996 runparams.linelen = 100000; //lyxrc.plaintext_linelen;
997 // No side effect of file copying and image conversion
998 runparams.dryrun = true;
999 LYXERR(Debug::FIND, "Stringifying with cur: "
1000 << cur << ", from pos: " << cur.pos() << ", end: " << end);
1001 return par.stringify(cur.pos(), end, AS_STR_INSETS, runparams);
1002 } else if (cur.inMathed()) {
1004 CursorSlice cs = cur.top();
1005 MathData md = cs.cell();
1006 MathData::const_iterator it_end =
1007 ( ( len == -1 || cs.pos() + len > int(md.size()) )
1008 ? md.end() : md.begin() + cs.pos() + len );
1009 for (MathData::const_iterator it = md.begin() + cs.pos(); it != it_end; ++it)
1010 s = s + asString(*it);
1011 LYXERR(Debug::FIND, "Stringified math: '" << s << "'");
1014 LYXERR(Debug::FIND, "Don't know how to stringify from here: " << cur);
1019 /** Computes the LaTeX export of buf starting from cur and ending len positions
1020 * after cur, if len is positive, or at the paragraph or innermost inset end
1023 docstring latexifyFromCursor(DocIterator const & cur, int len)
1025 LYXERR(Debug::FIND, "Latexifying with len=" << len << " from cursor at pos: " << cur);
1026 LYXERR(Debug::FIND, " with cur.lastpost=" << cur.lastpos() << ", cur.lastrow="
1027 << cur.lastrow() << ", cur.lastcol=" << cur.lastcol());
1028 Buffer const & buf = *cur.buffer();
1029 LASSERT(buf.params().isLatex(), /* */);
1032 odocstringstream ods;
1033 otexstream os(ods, texrow);
1034 OutputParams runparams(&buf.params().encoding());
1035 runparams.nice = false;
1036 runparams.flavor = OutputParams::LATEX;
1037 runparams.linelen = 8000; //lyxrc.plaintext_linelen;
1038 // No side effect of file copying and image conversion
1039 runparams.dryrun = true;
1041 if (cur.inTexted()) {
1042 // @TODO what about searching beyond/across paragraph breaks ?
1043 pos_type endpos = cur.paragraph().size();
1044 if (len != -1 && endpos > cur.pos() + len)
1045 endpos = cur.pos() + len;
1046 TeXOnePar(buf, *cur.innerText(), cur.pit(), os, runparams,
1047 string(), cur.pos(), endpos);
1048 LYXERR(Debug::FIND, "Latexified text: '" << lyx::to_utf8(ods.str()) << "'");
1049 } else if (cur.inMathed()) {
1050 // Retrieve the math environment type, and add '$' or '$[' or others (\begin{equation}) accordingly
1051 for (int s = cur.depth() - 1; s >= 0; --s) {
1052 CursorSlice const & cs = cur[s];
1053 if (cs.asInsetMath() && cs.asInsetMath() && cs.asInsetMath()->asHullInset()) {
1054 WriteStream ws(ods);
1055 cs.asInsetMath()->asHullInset()->header_write(ws);
1060 CursorSlice const & cs = cur.top();
1061 MathData md = cs.cell();
1062 MathData::const_iterator it_end = ( ( len == -1 || cs.pos() + len > int(md.size()) )
1063 ? md.end() : md.begin() + cs.pos() + len );
1064 for (MathData::const_iterator it = md.begin() + cs.pos(); it != it_end; ++it)
1065 ods << asString(*it);
1067 // Retrieve the math environment type, and add '$' or '$]'
1068 // or others (\end{equation}) accordingly
1069 for (int s = cur.depth() - 1; s >= 0; --s) {
1070 CursorSlice const & cs = cur[s];
1071 InsetMath * inset = cs.asInsetMath();
1072 if (inset && inset->asHullInset()) {
1073 WriteStream ws(ods);
1074 inset->asHullInset()->footer_write(ws);
1078 LYXERR(Debug::FIND, "Latexified math: '" << lyx::to_utf8(ods.str()) << "'");
1080 LYXERR(Debug::FIND, "Don't know how to stringify from here: " << cur);
1086 /** Finalize an advanced find operation, advancing the cursor to the innermost
1087 ** position that matches, plus computing the length of the matching text to
1090 int findAdvFinalize(DocIterator & cur, MatchStringAdv const & match)
1092 // Search the foremost position that matches (avoids find of entire math
1093 // inset when match at start of it)
1095 DocIterator old_cur(cur.buffer());
1097 LYXERR(Debug::FIND, "Forwarding one step (searching for innermost match)");
1101 } while (cur && cur.depth() > d && match(cur) > 0);
1103 LASSERT(match(cur) > 0, /* */);
1104 LYXERR(Debug::FIND, "Ok");
1106 // Compute the match length
1108 if (cur.pos() + len > cur.lastpos())
1110 LYXERR(Debug::FIND, "verifying unmatch with len = " << len);
1111 while (cur.pos() + len <= cur.lastpos() && match(cur, len) == 0) {
1113 LYXERR(Debug::FIND, "verifying unmatch with len = " << len);
1115 // Length of matched text (different from len param)
1116 int old_len = match(cur, len);
1118 // Greedy behaviour while matching regexps
1119 while ((new_len = match(cur, len + 1)) > old_len) {
1122 LYXERR(Debug::FIND, "verifying match with len = " << len);
1129 int findForwardAdv(DocIterator & cur, MatchStringAdv & match)
1133 while (!theApp()->longOperationCancelled() && cur) {
1134 LYXERR(Debug::FIND, "findForwardAdv() cur: " << cur);
1135 int match_len = match(cur, -1, false);
1136 LYXERR(Debug::FIND, "match_len: " << match_len);
1138 for (; !theApp()->longOperationCancelled() && cur; cur.forwardPos()) {
1139 LYXERR(Debug::FIND, "Advancing cur: " << cur);
1140 int match_len = match(cur);
1141 LYXERR(Debug::FIND, "match_len: " << match_len);
1143 // Sometimes in finalize we understand it wasn't a match
1144 // and we need to continue the outest loop
1145 int len = findAdvFinalize(cur, match);
1153 if (cur.pit() < cur.lastpit()) {
1154 LYXERR(Debug::FIND, "Advancing par: cur=" << cur);
1157 // This should exit nested insets, if any, or otherwise undefine the currsor.
1158 cur.pos() = cur.lastpos();
1159 LYXERR(Debug::FIND, "Advancing pos: cur=" << cur);
1167 /// Find the most backward consecutive match within same paragraph while searching backwards.
1168 int findMostBackwards(DocIterator & cur, MatchStringAdv const & match)
1170 DocIterator cur_begin = doc_iterator_begin(cur.buffer());
1171 DocIterator tmp_cur = cur;
1172 int len = findAdvFinalize(tmp_cur, match);
1173 Inset & inset = cur.inset();
1174 for (; cur != cur_begin; cur.backwardPos()) {
1175 LYXERR(Debug::FIND, "findMostBackwards(): cur=" << cur);
1176 DocIterator new_cur = cur;
1177 new_cur.backwardPos();
1178 if (new_cur == cur || &new_cur.inset() != &inset || !match(new_cur))
1180 int new_len = findAdvFinalize(new_cur, match);
1185 LYXERR(Debug::FIND, "findMostBackwards(): exiting with cur=" << cur);
1191 int findBackwardsAdv(DocIterator & cur, MatchStringAdv & match) {
1194 // Backup of original position
1195 DocIterator cur_begin = doc_iterator_begin(cur.buffer());
1196 if (cur == cur_begin)
1199 DocIterator cur_orig(cur);
1201 bool pit_changed = false;
1202 found_match = false;
1205 found_match = match(cur, -1, false);
1209 cur.pos() = cur.lastpos();
1211 cur.pos() = cur_orig.pos();
1212 LYXERR(Debug::FIND, "findBackAdv2: cur: " << cur);
1213 DocIterator cur_prev_iter;
1215 found_match = match(cur);
1216 LYXERR(Debug::FIND, "findBackAdv3: found_match="
1217 << found_match << ", cur: " << cur);
1219 return findMostBackwards(cur, match);
1221 // Stop if begin of document reached
1222 if (cur == cur_begin)
1224 cur_prev_iter = cur;
1228 if (cur == cur_begin)
1235 } while (!theApp()->longOperationCancelled());
1240 } // anonym namespace
1243 docstring stringifyFromForSearch(FindAndReplaceOptions const & opt,
1244 DocIterator const & cur, int len)
1246 LASSERT(cur.pos() >= 0 && cur.pos() <= cur.lastpos(), /* */);
1247 if (!opt.ignoreformat)
1248 return latexifyFromCursor(cur, len);
1250 return stringifyFromCursor(cur, len);
1254 FindAndReplaceOptions::FindAndReplaceOptions(docstring const & find_buf_name, bool casesensitive,
1255 bool matchword, bool forward, bool expandmacros, bool ignoreformat,
1256 docstring const & repl_buf_name, bool keep_case,
1258 : find_buf_name(find_buf_name), casesensitive(casesensitive), matchword(matchword),
1259 forward(forward), expandmacros(expandmacros), ignoreformat(ignoreformat),
1260 repl_buf_name(repl_buf_name), keep_case(keep_case), scope(scope)
1268 /** Check if 'len' letters following cursor are all non-lowercase */
1269 static bool allNonLowercase(DocIterator const & cur, int len) {
1270 pos_type end_pos = cur.pos() + len;
1271 for (pos_type pos = cur.pos(); pos != end_pos; ++pos)
1272 if (isLowerCase(cur.paragraph().getChar(pos)))
1278 /** Check if first letter is upper case and second one is lower case */
1279 static bool firstUppercase(DocIterator const & cur) {
1281 if (cur.pos() >= cur.lastpos() - 1) {
1282 LYXERR(Debug::FIND, "No upper-case at cur: " << cur);
1285 ch1 = cur.paragraph().getChar(cur.pos());
1286 ch2 = cur.paragraph().getChar(cur.pos()+1);
1287 bool result = isUpperCase(ch1) && isLowerCase(ch2);
1288 LYXERR(Debug::FIND, "firstUppercase(): "
1289 << "ch1=" << ch1 << "(" << char(ch1) << "), ch2="
1290 << ch2 << "(" << char(ch2) << ")"
1291 << ", result=" << result << ", cur=" << cur);
1296 /** Make first letter of supplied buffer upper-case, and the rest lower-case.
1298 ** \fixme What to do with possible further paragraphs in replace buffer ?
1300 static void changeFirstCase(Buffer & buffer, TextCase first_case, TextCase others_case) {
1301 ParagraphList::iterator pit = buffer.paragraphs().begin();
1302 pos_type right = pos_type(1);
1303 pit->changeCase(buffer.params(), pos_type(0), right, first_case);
1304 right = pit->size() + 1;
1305 pit->changeCase(buffer.params(), right, right, others_case);
1310 static void findAdvReplace(BufferView * bv, FindAndReplaceOptions const & opt, MatchStringAdv & matchAdv)
1312 Cursor & cur = bv->cursor();
1313 if (opt.repl_buf_name == docstring())
1316 DocIterator sel_beg = cur.selectionBegin();
1317 DocIterator sel_end = cur.selectionEnd();
1318 if (&sel_beg.inset() != &sel_end.inset()
1319 || sel_beg.pit() != sel_end.pit())
1321 int sel_len = sel_end.pos() - sel_beg.pos();
1322 LYXERR(Debug::FIND, "sel_beg: " << sel_beg << ", sel_end: " << sel_end
1323 << ", sel_len: " << sel_len << endl);
1326 LASSERT(sel_len > 0, /**/);
1328 if (!matchAdv(sel_beg, sel_len))
1331 // Build a copy of the replace buffer, adapted to the KeepCase option
1332 Buffer & repl_buffer_orig = *theBufferList().getBuffer(FileName(to_utf8(opt.repl_buf_name)), true);
1334 repl_buffer_orig.write(oss);
1335 string lyx = oss.str();
1336 Buffer repl_buffer("", false);
1337 repl_buffer.setUnnamed(true);
1338 LASSERT(repl_buffer.readString(lyx), /**/);
1339 if (opt.keep_case && sel_len >= 2) {
1340 if (cur.inTexted()) {
1341 if (firstUppercase(cur))
1342 changeFirstCase(repl_buffer, text_uppercase, text_lowercase);
1343 else if (allNonLowercase(cur, sel_len))
1344 changeFirstCase(repl_buffer, text_uppercase, text_uppercase);
1347 cap::cutSelection(cur, false, false);
1348 if (cur.inTexted()) {
1349 repl_buffer.changeLanguage(
1350 repl_buffer.language(),
1351 cur.getFont().language());
1352 LYXERR(Debug::FIND, "Replacing by pasteParagraphList()ing repl_buffer");
1353 LYXERR(Debug::FIND, "Before pasteParagraphList() cur=" << cur << endl);
1354 cap::pasteParagraphList(cur, repl_buffer.paragraphs(),
1355 repl_buffer.params().documentClassPtr(),
1356 bv->buffer().errorList("Paste"));
1357 LYXERR(Debug::FIND, "After pasteParagraphList() cur=" << cur << endl);
1358 sel_len = repl_buffer.paragraphs().begin()->size();
1359 } else if (cur.inMathed()) {
1361 odocstringstream ods;
1362 otexstream os(ods, texrow);
1363 OutputParams runparams(&repl_buffer.params().encoding());
1364 runparams.nice = false;
1365 runparams.flavor = OutputParams::LATEX;
1366 runparams.linelen = 8000; //lyxrc.plaintext_linelen;
1367 runparams.dryrun = true;
1368 TeXOnePar(repl_buffer, repl_buffer.text(), 0, os, runparams);
1369 //repl_buffer.getSourceCode(ods, 0, repl_buffer.paragraphs().size(), false);
1370 docstring repl_latex = ods.str();
1371 LYXERR(Debug::FIND, "Latexified replace_buffer: '" << repl_latex << "'");
1373 regex_replace(to_utf8(repl_latex), s, "\\$(.*)\\$", "$1");
1374 regex_replace(s, s, "\\\\\\[(.*)\\\\\\]", "$1");
1375 repl_latex = from_utf8(s);
1376 LYXERR(Debug::FIND, "Replacing by insert()ing latex: '" << repl_latex << "' cur=" << cur << " with depth=" << cur.depth());
1377 MathData ar(cur.buffer());
1378 asArray(repl_latex, ar, Parse::NORMAL);
1380 sel_len = ar.size();
1381 LYXERR(Debug::FIND, "After insert() cur=" << cur << " with depth: " << cur.depth() << " and len: " << sel_len);
1383 if (cur.pos() >= sel_len)
1384 cur.pos() -= sel_len;
1387 LYXERR(Debug::FIND, "After pos adj cur=" << cur << " with depth: " << cur.depth() << " and len: " << sel_len);
1388 bv->putSelectionAt(DocIterator(cur), sel_len, !opt.forward);
1389 bv->processUpdateFlags(Update::Force);
1390 bv->buffer().updatePreviews();
1394 /// Perform a FindAdv operation.
1395 bool findAdv(BufferView * bv, FindAndReplaceOptions const & opt)
1401 MatchStringAdv matchAdv(bv->buffer(), opt);
1402 findAdvReplace(bv, opt, matchAdv);
1405 match_len = findForwardAdv(cur, matchAdv);
1407 match_len = findBackwardsAdv(cur, matchAdv);
1409 // This may only be raised by lyx::regex()
1410 bv->message(_("Invalid regular expression!"));
1414 if (match_len == 0) {
1415 bv->message(_("Match not found!"));
1419 bv->message(_("Match found!"));
1421 LYXERR(Debug::FIND, "Putting selection at cur=" << cur << " with len: " << match_len);
1422 bv->putSelectionAt(cur, match_len, !opt.forward);
1428 ostringstream & operator<<(ostringstream & os, FindAndReplaceOptions const & opt)
1430 os << to_utf8(opt.find_buf_name) << "\nEOSS\n"
1431 << opt.casesensitive << ' '
1432 << opt.matchword << ' '
1433 << opt.forward << ' '
1434 << opt.expandmacros << ' '
1435 << opt.ignoreformat << ' '
1436 << to_utf8(opt.repl_buf_name) << "\nEOSS\n"
1437 << opt.keep_case << ' '
1440 LYXERR(Debug::FIND, "built: " << os.str());
1446 istringstream & operator>>(istringstream & is, FindAndReplaceOptions & opt)
1448 LYXERR(Debug::FIND, "parsing");
1452 while (line != "EOSS") {
1456 if (is.eof()) // Tolerate malformed request
1460 LYXERR(Debug::FIND, "file_buf_name: '" << s << "'");
1461 opt.find_buf_name = from_utf8(s);
1462 is >> opt.casesensitive >> opt.matchword >> opt.forward >> opt.expandmacros >> opt.ignoreformat;
1463 is.get(); // Waste space before replace string
1466 while (line != "EOSS") {
1470 if (is.eof()) // Tolerate malformed request
1474 LYXERR(Debug::FIND, "repl_buf_name: '" << s << "'");
1475 opt.repl_buf_name = from_utf8(s);
1476 is >> opt.keep_case;
1479 opt.scope = FindAndReplaceOptions::SearchScope(i);
1480 LYXERR(Debug::FIND, "parsed: " << opt.casesensitive << ' ' << opt.matchword << ' ' << opt.forward << ' '
1481 << opt.expandmacros << ' ' << opt.ignoreformat << ' ' << opt.keep_case);