]> git.lyx.org Git - features.git/commitdiff
Improve LaTeX format detection
authorGeorg Baum <baum@lyx.org>
Fri, 12 Apr 2013 20:31:48 +0000 (22:31 +0200)
committerGeorg Baum <baum@lyx.org>
Fri, 12 Apr 2013 20:31:48 +0000 (22:31 +0200)
libmime is a bit lacking here.

src/Format.cpp
src/support/lstrings.cpp
src/support/lstrings.h

index 8e31a1e96bded7db0958f57b71a275a3b37a3f97..485715b6e3b23911d622c31897856467f748bd0e 100644 (file)
@@ -253,12 +253,11 @@ string guessFormatFromContents(FileName const & fn)
        string str;
        string format;
        bool firstLine = true;
+       bool backslash = false;
+       int dollars = 0;
        while ((count++ < max_count) && format.empty()) {
-               if (ifs.eof()) {
-                       LYXERR(Debug::GRAPHICS, "filetools(getFormatFromContents)\n"
-                               << "\tFile type not recognised before EOF!");
+               if (ifs.eof())
                        break;
-               }
 
                getline(ifs, str);
                string const stamp = str.substr(0, 2);
@@ -363,9 +362,32 @@ string guessFormatFromContents(FileName const & fn)
 
                else if (contains(str, "BITPIX"))
                        format = "fits";
+
+               else if (contains(str, "\\documentclass") ||
+                        contains(str, "\\chapter") ||
+                        contains(str, "\\section") ||
+                        contains(str, "\\begin") ||
+                        contains(str, "\\end") ||
+                        contains(str, "$$") ||
+                        contains(str, "\\[") ||
+                        contains(str, "\\]"))
+                       format = "latex";
+               else {
+                       if (contains(str, '\\'))
+                               backslash = true;
+                       dollars += count_char(str, '$');
+               }
        }
 
-       if (!format.empty()) {
+       if (format.empty() && backslash && dollars > 1)
+               // inline equation
+               format = "latex";
+
+       if (format.empty()) {
+               if (ifs.eof())
+                       LYXERR(Debug::GRAPHICS, "filetools(getFormatFromContents)\n"
+                              "\tFile type not recognised before EOF!");
+       } else {
                LYXERR(Debug::GRAPHICS, "Recognised Fileformat: " << format);
                return format;
        }
index 651dbc1e1dd2a42097f4a677b0e817e7efe8e71c..339318989f188d646b9e2866a3bbe400ff6f85c9 100644 (file)
@@ -882,6 +882,18 @@ docstring const subst(docstring const & a,
 }
 
 
+int count_char(string const & str, char chr)
+{
+       int count = 0;
+       string::const_iterator lit = str.begin();
+       string::const_iterator end = str.end();
+       for (; lit != end; ++lit)
+               if ((*lit) == chr)
+                       count++;
+       return count;
+}
+
+
 /// Count all occurences of char \a chr inside \a str
 int count_char(docstring const & str, docstring::value_type chr)
 {
index 4845bc6120db9fa922301e7a56c5e508357c4008..2606546fa05ce3c29cdb5c0c47341e83e1fd814c 100644 (file)
@@ -188,6 +188,9 @@ std::string const subst(std::string const & a,
 docstring const subst(docstring const & a,
                docstring const & oldstr, docstring const & newstr);
 
+/// Count all occurences of char \a chr inside \a str
+int count_char(std::string const & str, char chr);
+
 /// Count all occurences of char \a chr inside \a str
 int count_char(docstring const & str, docstring::value_type chr);