]> git.lyx.org Git - lyx.git/blobdiff - src/Format.cpp
Harmonize naming
[lyx.git] / src / Format.cpp
index f031b6806d42562838a242e422e0fecb905ba1e8..ff03ab52ff4cc17b6a56a4136344f27ab46f3b91 100644 (file)
@@ -23,7 +23,7 @@
 #include "support/gettext.h"
 #include "support/lstrings.h"
 #include "support/os.h"
-#include "support/Path.h"
+#include "support/PathChanger.h"
 #include "support/Systemcall.h"
 #include "support/textutils.h"
 #include "support/Translator.h"
@@ -59,7 +59,8 @@ string const token_socket_format("$$a");
 class FormatNamesEqual : public unary_function<Format, bool> {
 public:
        FormatNamesEqual(string const & name)
-               : name_(name) {}
+               : name_(name)
+       {}
        bool operator()(Format const & f) const
        {
                return f.name() == name_;
@@ -72,7 +73,8 @@ private:
 class FormatExtensionsEqual : public unary_function<Format, bool> {
 public:
        FormatExtensionsEqual(string const & extension)
-               : extension_(extension) {}
+               : extension_(extension)
+       {}
        bool operator()(Format const & f) const
        {
                return f.hasExtension(extension_);
@@ -85,7 +87,8 @@ private:
 class FormatMimeEqual : public unary_function<Format, bool> {
 public:
        FormatMimeEqual(string const & mime)
-               : mime_(mime) {}
+               : mime_(mime)
+       {}
        bool operator()(Format const & f) const
        {
                // The test for empty mime strings is needed since we allow
@@ -97,20 +100,12 @@ private:
 };
 
 
-class FormatPrettyNameEqual : public unary_function<Format, bool> {
-public:
-       FormatPrettyNameEqual(string const & prettyname)
-               : prettyname_(prettyname) {}
-       bool operator()(Format const & f) const
-       {
-               return f.prettyname() == prettyname_;
-       }
-private:
-       string prettyname_;
-};
-
 } //namespace anon
 
+bool Format::formatSorter(Format const * lhs, Format const * rhs)
+{
+       return _(lhs->prettyname()) < _(rhs->prettyname());
+}
 
 bool operator<(Format const & a, Format const & b)
 {
@@ -198,7 +193,7 @@ string guessFormatFromContents(FileName const & fn)
        // FIG  #FIG...
        // FITS ...BITPIX...
        // GIF  GIF...
-       // JPG  JFIF
+       // JPG  \377\330...     (0xFFD8)
        // PDF  %PDF-...
        // PNG  .PNG...
        // PBM  P1... or P4     (B/W)
@@ -245,12 +240,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);
@@ -273,6 +267,9 @@ string guessFormatFromContents(FileName const & fn)
                        } else if (stamp == "BM") {
                                format =  "bmp";
 
+                       } else if (stamp == "\377\330") {
+                               format =  "jpg";
+
                        } else if (stamp == "\001\332") {
                                format =  "sgi";
 
@@ -328,11 +325,9 @@ string guessFormatFromContents(FileName const & fn)
                else if (contains(str, "Grace"))
                        format = "agr";
 
-               else if (contains(str, "JFIF"))
-                       format = "jpg";
-
                else if (contains(str, "%PDF"))
-                       format = "pdf";
+                       // autodetect pdf format for graphics inclusion
+                       format = "pdf6";
 
                else if (contains(str, "PNG"))
                        format = "png";
@@ -354,9 +349,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;
        }
@@ -442,19 +460,6 @@ string Formats::getFormatFromExtension(string const & ext) const
 }
 
 
-string Formats::getFormatFromPrettyName(string const & prettyname) const
-{
-       if (!prettyname.empty()) {
-               Formats::const_iterator cit =
-                       find_if(formatlist.begin(), formatlist.end(),
-                               FormatPrettyNameEqual(prettyname));
-               if (cit != formats.end())
-                       return cit->name();
-       }
-       return string();
-}
-
-
 /// Used to store last timestamp of file and whether it is (was) zipped
 struct ZippedInfo {
        bool zipped;
@@ -476,7 +481,7 @@ bool Formats::isZippedFile(support::FileName const & filename) const {
                return it->second.zipped;
        string const & format = getFormatFromFile(filename);
        bool zipped = (format == "gzip" || format == "zip");
-       zipped_.insert(pair<string, ZippedInfo>(fname, ZippedInfo(zipped, timestamp)));
+       zipped_.insert(make_pair(fname, ZippedInfo(zipped, timestamp)));
        return zipped;
 }
 
@@ -677,7 +682,7 @@ bool Formats::edit(Buffer const & buffer, FileName const & filename,
 
        // LinkBack files look like PDF, but have the .linkback extension
        string const ext = getExtension(filename.absFileName());
-       if (format_name == "pdf" && ext == "linkback") {
+       if (format_name == "pdf6" && ext == "linkback") {
 #ifdef USE_MACOSX_PACKAGING
                return editLinkBackFile(filename.absFileName().c_str());
 #else
@@ -763,8 +768,10 @@ string const Formats::extensions(string const & name) const
 
 
 namespace {
+
 typedef Translator<OutputParams::FLAVOR, string> FlavorTranslator;
 
+
 FlavorTranslator initFlavorTranslator()
 {
        FlavorTranslator f(OutputParams::LATEX, "latex");
@@ -784,6 +791,7 @@ FlavorTranslator const & flavorTranslator()
        static FlavorTranslator translator = initFlavorTranslator();
        return translator;
 }
+
 }