]> git.lyx.org Git - lyx.git/blobdiff - src/lyxfind.cpp
Amend 3093789e for cmake build
[lyx.git] / src / lyxfind.cpp
index b9633bc7f9f66dc2532e3406f1fc79641ac62444..fc7b0b737a71522a0e006d0e7074afe62213e18f 100644 (file)
@@ -17,7 +17,6 @@
 #include "lyxfind.h"
 
 #include "Buffer.h"
-#include "buffer_funcs.h"
 #include "BufferList.h"
 #include "BufferParams.h"
 #include "BufferView.h"
@@ -29,8 +28,6 @@
 #include "output_latex.h"
 #include "OutputParams.h"
 #include "Paragraph.h"
-#include "ParIterator.h"
-#include "TexRow.h"
 #include "Text.h"
 #include "Encoding.h"
 
 #include "frontends/alert.h"
 
 #include "mathed/InsetMath.h"
-#include "mathed/InsetMathGrid.h"
 #include "mathed/InsetMathHull.h"
 #include "mathed/MathData.h"
 #include "mathed/MathStream.h"
 #include "mathed/MathSupport.h"
 
-#include "support/convert.h"
 #include "support/debug.h"
 #include "support/docstream.h"
 #include "support/FileName.h"
@@ -66,56 +61,51 @@ namespace lyx {
 class IgnoreFormats {
  public:
        ///
-       IgnoreFormats()
-               : ignoreFamily_(false), ignoreSeries_(false),
-                 ignoreShape_(false), ignoreUnderline_(false),
-                 ignoreMarkUp_(false), ignoreStrikeOut_(false),
-                 ignoreSectioning_(false), ignoreFrontMatter_(false),
-                 ignoreColor_(false), ignoreLanguage_(false) {}
+       IgnoreFormats() = default;
        ///
-       bool getFamily() { return ignoreFamily_; };
+       bool getFamily() const { return ignoreFamily_; }
        ///
-       bool getSeries() { return ignoreSeries_; };
+       bool getSeries() const { return ignoreSeries_; }
        ///
-       bool getShape() { return ignoreShape_; };
+       bool getShape() const { return ignoreShape_; }
        ///
-       bool getUnderline() { return ignoreUnderline_; };
+       bool getUnderline() const { return ignoreUnderline_; }
        ///
-       bool getMarkUp() { return ignoreMarkUp_; };
+       bool getMarkUp() const { return ignoreMarkUp_; }
        ///
-       bool getStrikeOut() { return ignoreStrikeOut_; };
+       bool getStrikeOut() const { return ignoreStrikeOut_; }
        ///
-       bool getSectioning() { return ignoreSectioning_; };
+       bool getSectioning() const { return ignoreSectioning_; }
        ///
-       bool getFrontMatter() { return ignoreFrontMatter_; };
+       bool getFrontMatter() const { return ignoreFrontMatter_; }
        ///
-       bool getColor() { return ignoreColor_; };
+       bool getColor() const { return ignoreColor_; }
        ///
-       bool getLanguage() { return ignoreLanguage_; };
+       bool getLanguage() const { return ignoreLanguage_; }
        ///
        void setIgnoreFormat(string const & type, bool value);
 
 private:
        ///
-       bool ignoreFamily_;
+       bool ignoreFamily_ = false;
        ///
-       bool ignoreSeries_;
+       bool ignoreSeries_ = false;
        ///
-       bool ignoreShape_;
+       bool ignoreShape_ = false;
        ///
-       bool ignoreUnderline_;
+       bool ignoreUnderline_ = false;
        ///
-       bool ignoreMarkUp_;
+       bool ignoreMarkUp_ = false;
        ///
-       bool ignoreStrikeOut_;
+       bool ignoreStrikeOut_ = false;
        ///
-       bool ignoreSectioning_;
+       bool ignoreSectioning_ = false;
        ///
-       bool ignoreFrontMatter_;
+       bool ignoreFrontMatter_ = false;
        ///
-       bool ignoreColor_;
+       bool ignoreColor_ = false;
        ///
-       bool ignoreLanguage_;
+       bool ignoreLanguage_ = false;
 };
 
 
@@ -357,11 +347,10 @@ pair<bool, int> replaceOne(BufferView * bv, docstring searchstr,
                           bool whole, bool forward, bool findnext)
 {
        Cursor & cur = bv->cursor();
-       bool found = false;
        if (!cur.selection()) {
                // no selection, non-empty search string: find it
                if (!searchstr.empty()) {
-                       found = findOne(bv, searchstr, case_sens, whole, forward, true, findnext);
+                       bool const found = findOne(bv, searchstr, case_sens, whole, forward, true, findnext);
                        return make_pair(found, 0);
                }
                // empty search string
@@ -390,7 +379,7 @@ pair<bool, int> replaceOne(BufferView * bv, docstring searchstr,
        // no selection or current selection is not search word:
        // just find the search word
        if (!have_selection || !match) {
-               found = findOne(bv, searchstr, case_sens, whole, forward, true, findnext);
+               bool const found = findOne(bv, searchstr, case_sens, whole, forward, true, findnext);
                return make_pair(found, 0);
        }
 
@@ -968,9 +957,9 @@ static size_t identifyLeading(string const & s)
        // + allow to search for colored text too
        while (regex_replace(t, t, REGEX_BOS "\\\\(((footnotesize|tiny|scriptsize|small|large|Large|LARGE|huge|Huge|emph|noun|minisec|text(bf|md|sl|sf|it|tt))|((textcolor|foreignlanguage|latexenvironment)\\{[a-z]+\\*?\\})|(u|uu)line|(s|x)out|uwave)|((sub)?(((sub)?section)|paragraph)|part|chapter)\\*?)\\{", "")
               || regex_replace(t, t, REGEX_BOS "\\$", "")
-              || regex_replace(t, t, REGEX_BOS "\\\\\\[ ", "")
+              || regex_replace(t, t, REGEX_BOS "\\\\\\[", "")
               || regex_replace(t, t, REGEX_BOS " ?\\\\item\\{[a-z]+\\}", "")
-              || regex_replace(t, t, REGEX_BOS "\\\\begin\\{[a-zA-Z_]*\\*?\\} ", ""))
+              || regex_replace(t, t, REGEX_BOS "\\\\begin\\{[a-zA-Z_]*\\*?\\}", ""))
               ;
        LYXERR(Debug::FIND, "  after removing leading $, \\[ , \\emph{, \\textbf{, etc.: '" << t << "'");
        return s.find(t);
@@ -1071,35 +1060,20 @@ class KeyInfo {
      * so that they can be ignored */
     endArguments
   };
- KeyInfo()
-   : keytype(invalid),
-    head(""),
-    _tokensize(-1),
-    _tokenstart(-1),
-    _dataStart(-1),
-    _dataEnd(-1),
-    parenthesiscount(1),
-    disabled(false),
-    used(false)
-  {};
+ KeyInfo() = default;
  KeyInfo(KeyType type, int parcount, bool disable)
    : keytype(type),
-    _tokensize(-1),
-    _tokenstart(-1),
-    _dataStart(-1),
-    _dataEnd(-1),
     parenthesiscount(parcount),
-    disabled(disable),
-    used(false) {};
-  KeyType keytype;
+    disabled(disable) {}
+  KeyType keytype = invalid;
   string head;
-  int _tokensize;
-  int _tokenstart;
-  int _dataStart;
-  int _dataEnd;
-  int parenthesiscount;
-  bool disabled;
-  bool used;                            /* by pattern */
+  int _tokensize = -1;
+  int _tokenstart = -1;
+  int _dataStart = -1;
+  int _dataEnd = -1;
+  int parenthesiscount = 1;
+  bool disabled = false;
+  bool used = false;                    /* by pattern */
 };
 
 class Border {
@@ -1127,20 +1101,20 @@ public:
   int depts[MAXOPENED];
   int closes[MAXOPENED];
   int actualdeptindex;
-  int previousNotIgnored(int);
-  int nextNotIgnored(int);
+  int previousNotIgnored(int) const;
+  int nextNotIgnored(int) const;
   void handleOpenP(int i);
   void handleCloseP(int i, bool closingAllowed);
   void resetOpenedP(int openPos);
   void addIntervall(int upper);
   void addIntervall(int low, int upper); /* if explicit */
   void removeAccents();
-  void setForDefaultLang(KeyInfo &defLang);
+  void setForDefaultLang(KeyInfo const & defLang) const;
   int findclosing(int start, int end, char up, char down, int repeat);
   void handleParentheses(int lastpos, bool closingAllowed);
   bool hasTitle;
   int langcount;       // Number of disabled language specs up to current position in actual interval
-  int isOpeningPar(int pos);
+  int isOpeningPar(int pos) const;
   string titleValue;
   void output(ostringstream &os, int lastpos);
   // string show(int lastpos);
@@ -1148,7 +1122,7 @@ public:
 
 vector<Border> Intervall::borders = vector<Border>(30);
 
-int Intervall::isOpeningPar(int pos)
+int Intervall::isOpeningPar(int pos) const
 {
   if ((pos < 0) || (size_t(pos) >= par.size()))
     return 0;
@@ -1163,7 +1137,7 @@ int Intervall::isOpeningPar(int pos)
   return 1;
 }
 
-void Intervall::setForDefaultLang(KeyInfo &defLang)
+void Intervall::setForDefaultLang(KeyInfo const & defLang) const
 {
   // Enable the use of first token again
   if (ignoreidx >= 0) {
@@ -1284,8 +1258,8 @@ static void buildaccent(string n, string param, string values)
   const char delim = '|';
   while (getline(s, name, delim)) {
     size_t start = 0;
-    for (size_t i = 0; i < param.size(); i++) {
-      string key = name + "{" + param[i] + "}";
+    for (char c : param) {
+      string key = name + "{" + c + "}";
       // get the corresponding utf8-value
       if ((values[start] & 0xc0) != 0xc0) {
         // should not happen, utf8 encoding starts at least with 11xxxxxx
@@ -1321,6 +1295,7 @@ static void buildAccentsMap()
   accents["imath"] = "ı";
   accents["i"] = "ı";
   accents["jmath"] = "ȷ";
+  accents["cdot"] = "·";
   accents["lyxmathsym{ß}"] = "ß";
   accents["text{ß}"] = "ß";
   accents["ddot{\\imath}"] = "ï";
@@ -1386,7 +1361,7 @@ void Intervall::removeAccents()
 {
   if (accents.empty())
     buildAccentsMap();
-  static regex const accre("\\\\(([\\S]|grave|breve|lyxmathsym|text|ddot|dot|acute|dacute|mathring|check|hat|bar|tilde|subdot|ogonek|cedilla|subring|textsubring|subhat|textsubcircum|subtilde|textsubtilde|dgrave|textdoublegrave|rcap|textroundcap|slashed)\\{[^\\{\\}]+\\}|(i|imath|jmath)(?![a-zA-Z]))");
+  static regex const accre("\\\\(([\\S]|grave|breve|lyxmathsym|text|ddot|dot|acute|dacute|mathring|check|hat|bar|tilde|subdot|ogonek|cedilla|subring|textsubring|subhat|textsubcircum|subtilde|textsubtilde|dgrave|textdoublegrave|rcap|textroundcap|slashed)\\{[^\\{\\}]+\\}|(i|imath|jmath|cdot)(?![a-zA-Z]))");
   smatch sub;
   for (sregex_iterator itacc(par.begin(), par.end(), accre), end; itacc != end; ++itacc) {
     sub = *itacc;
@@ -1397,7 +1372,11 @@ void Intervall::removeAccents()
       for (size_t i = 0; i < val.size(); i++) {
         par[pos+i] = val[i];
       }
-      addIntervall(pos+val.size(), pos + sub.str(0).size());
+      // Remove possibly following space too
+      if (par[pos+sub.str(0).size()] == ' ')
+       addIntervall(pos+val.size(), pos + sub.str(0).size()+1);
+      else
+       addIntervall(pos+val.size(), pos + sub.str(0).size());
       for (size_t i = pos+val.size(); i < pos + sub.str(0).size(); i++) {
         // remove traces of any remaining chars
         par[i] = ' ';
@@ -1439,7 +1418,7 @@ void Intervall::resetOpenedP(int openPos)
   closes[1] = -1;
 }
 
-int Intervall::previousNotIgnored(int start)
+int Intervall::previousNotIgnored(int start) const
 {
     int idx = 0;                          /* int intervalls */
     for (idx = ignoreidx; idx >= 0; --idx) {
@@ -1451,7 +1430,7 @@ int Intervall::previousNotIgnored(int start)
     return start;
 }
 
-int Intervall::nextNotIgnored(int start)
+int Intervall::nextNotIgnored(int start) const
 {
     int idx = 0;                          /* int intervalls */
     for (idx = 0; idx <= ignoreidx; idx++) {
@@ -1476,7 +1455,7 @@ class LatexInfo {
   void buildEntries(bool);
   void makeKey(const string &, KeyInfo, bool isPatternString);
   void processRegion(int start, int region_end); /*  remove {} parts */
-  void removeHead(KeyInfo&, int count=0);
+  void removeHead(KeyInfo const &, int count=0);
 
  public:
  LatexInfo(string const & par, bool isPatternString)
@@ -1525,7 +1504,7 @@ class LatexInfo {
     else
       return false;
   };
-  int find(int start, KeyInfo::KeyType keytype) {
+  int find(int start, KeyInfo::KeyType keytype) const {
     if (start < 0)
       return -1;
     int tmpIdx = start;
@@ -1536,8 +1515,8 @@ class LatexInfo {
     }
     return -1;
   };
-  int process(ostringstream &os, KeyInfo &actual);
-  int dispatch(ostringstream &os, int previousStart, KeyInfo &actual);
+  int process(ostringstream & os, KeyInfo const & actual);
+  int dispatch(ostringstream & os, int previousStart, KeyInfo & actual);
   // string show(int lastpos) { return interval.show(lastpos);};
   int nextNotIgnored(int start) { return interval_.nextNotIgnored(start);};
   KeyInfo &getKeyInfo(int keyinfo) {
@@ -1547,7 +1526,7 @@ class LatexInfo {
     else
       return entries_[keyinfo];
   };
-  void setForDefaultLang(KeyInfo &defLang) {interval_.setForDefaultLang(defLang);};
+  void setForDefaultLang(KeyInfo const & defLang) {interval_.setForDefaultLang(defLang);};
   void addIntervall(int low, int up) { interval_.addIntervall(low, up); };
 };
 
@@ -1598,14 +1577,14 @@ class MathInfo {
     m.mathSize = end - start;
     entries_.push_back(m);
   }
-  bool empty() { return entries_.empty(); };
-  size_t getEndPos() {
+  bool empty() const { return entries_.empty(); };
+  size_t getEndPos() const {
     if (entries_.empty() || (actualIdx_ >= entries_.size())) {
       return 0;
     }
     return entries_[actualIdx_].mathEnd;
   }
-  size_t getStartPos() {
+  size_t getStartPos() const {
     if (entries_.empty() || (actualIdx_ >= entries_.size())) {
       return 100000;                    /*  definitely enough? */
     }
@@ -1615,7 +1594,7 @@ class MathInfo {
     actualIdx_ = 0;
     return getStartPos();
   }
-  size_t getSize() {
+  size_t getSize() const {
     if (entries_.empty() || (actualIdx_ >= entries_.size())) {
       return size_t(0);
     }
@@ -2221,7 +2200,7 @@ void LatexInfo::processRegion(int start, int region_end)
   }
 }
 
-void LatexInfo::removeHead(KeyInfo &actual, int count)
+void LatexInfo::removeHead(KeyInfo const & actual, int count)
 {
   if (actual.parenthesiscount == 0) {
     // "{\tiny{} ...}" ==> "{{} ...}"
@@ -2505,7 +2484,7 @@ int LatexInfo::dispatch(ostringstream &os, int previousStart, KeyInfo &actual)
   return nextKeyIdx;
 }
 
-int LatexInfo::process(ostringstream &os, KeyInfo &actual )
+int LatexInfo::process(ostringstream & os, KeyInfo const & actual )
 {
   int end = interval_.nextNotIgnored(actual._dataEnd);
   int oldStart = actual._dataStart;
@@ -2711,9 +2690,9 @@ static int identifyClosing(string & t)
                LYXERR(Debug::FIND, "identifyClosing(): t now is '" << t << "'");
                if (regex_replace(t, t, "(.*[^\\\\])\\$" REGEX_EOS, "$1"))
                        continue;
-               if (regex_replace(t, t, "(.*[^\\\\]) \\\\\\]" REGEX_EOS, "$1"))
+               if (regex_replace(t, t, "(.*[^\\\\])\\\\\\]" REGEX_EOS, "$1"))
                        continue;
-               if (regex_replace(t, t, "(.*[^\\\\]) \\\\end\\{[a-zA-Z_]*\\*?\\}" REGEX_EOS, "$1"))
+               if (regex_replace(t, t, "(.*[^\\\\])\\\\end\\{[a-zA-Z_]*\\*?\\}" REGEX_EOS, "$1"))
                        continue;
                if (regex_replace(t, t, "(.*[^\\\\])\\}" REGEX_EOS, "$1")) {
                        ++open_braces;
@@ -3040,9 +3019,14 @@ MatchResult MatchStringAdv::operator()(DocIterator const & cur, int len, bool at
        bool ws_left = (cur.pos() > 0)
                ? par.isWordSeparator(cur.pos() - 1)
                : true;
-       bool ws_right = (cur.pos() + len < par.size())
+       bool ws_right;
+       if (len < 0)
+               ws_right = true;
+       else {
+               ws_right = (cur.pos() + len < par.size())
                ? par.isWordSeparator(cur.pos() + len)
                : true;
+       }
        LYXERR(Debug::FIND,
               "cur.pos()=" << cur.pos() << ", res=" << res
               << ", separ: " << ws_left << ", " << ws_right
@@ -3345,7 +3329,7 @@ int findAdvFinalize(DocIterator & cur, MatchStringAdv const & match)
 
 
 /// Finds forward
-int findForwardAdv(DocIterator & cur, MatchStringAdv & match)
+int findForwardAdv(DocIterator & cur, MatchStringAdv const & match)
 {
        if (!cur)
                return 0;
@@ -3588,7 +3572,7 @@ static bool firstUppercase(Cursor const & cur)
 static void changeFirstCase(Buffer & buffer, TextCase first_case, TextCase others_case)
 {
        ParagraphList::iterator pit = buffer.paragraphs().begin();
-       LASSERT(pit->size() >= 1, /**/);
+       LASSERT(!pit->empty(), /**/);
        pos_type right = pos_type(1);
        pit->changeCase(buffer.params(), pos_type(0), right, first_case);
        right = pit->size();
@@ -3601,7 +3585,7 @@ static void changeFirstCase(Buffer & buffer, TextCase first_case, TextCase other
 static int findAdvReplace(BufferView * bv, FindAndReplaceOptions const & opt, MatchStringAdv & matchAdv)
 {
        Cursor & cur = bv->cursor();
-       if (opt.repl_buf_name == docstring()
+       if (opt.repl_buf_name.empty()
            || theBufferList().getBuffer(FileName(to_utf8(opt.repl_buf_name)), true) == 0
            || theBufferList().getBuffer(FileName(to_utf8(opt.find_buf_name)), true) == 0)
                return 0;
@@ -3623,7 +3607,7 @@ static int findAdvReplace(BufferView * bv, FindAndReplaceOptions const & opt, Ma
                return 0;
 
        // Build a copy of the replace buffer, adapted to the KeepCase option
-       Buffer & repl_buffer_orig = *theBufferList().getBuffer(FileName(to_utf8(opt.repl_buf_name)), true);
+       Buffer const & repl_buffer_orig = *theBufferList().getBuffer(FileName(to_utf8(opt.repl_buf_name)), true);
        ostringstream oss;
        repl_buffer_orig.write(oss);
        string lyx = oss.str();