]> git.lyx.org Git - lyx.git/blobdiff - src/format.C
Really fix start_of_appendix output
[lyx.git] / src / format.C
index 0e83306e2506a0ed74b6b8f270f985a5d1040da3..5215e34bb31dc4b9a2ac4341b3c52b349349a12d 100644 (file)
@@ -59,6 +59,19 @@ private:
        string name_;
 };
 
+
+class FormatExtensionsEqual : public std::unary_function<Format, bool> {
+public:
+       FormatExtensionsEqual(string const & extension)
+               : extension_(extension) {}
+       bool operator()(Format const & f) const
+       {
+               return f.extension() == extension_;
+       }
+private:
+       string extension_;
+};
+
 } //namespace anon
 
 bool operator<(Format const & a, Format const & b)
@@ -110,6 +123,34 @@ Format const * Formats::getFormat(string const & name) const
 }
 
 
+string Formats::getFormatFromFile(string const & filename) const
+{
+       if (filename.empty())
+               return string();
+
+       string const format = lyx::support::getFormatFromContents(filename);
+       if (!format.empty())
+               return format;
+
+       // try to find a format from the file extension.
+       string const ext(lyx::support::GetExtension(filename));
+        if (!ext.empty()) {
+               // this is ambigous if two formats have the same extension,
+               // but better than nothing
+               Formats::const_iterator cit =
+                       find_if(formatlist.begin(), formatlist.end(),
+                               FormatExtensionsEqual(ext));
+               if (cit != formats.end()) {
+                       lyxerr[Debug::GRAPHICS]
+                               << "\twill guess format from file extension: "
+                               << ext << " -> " << cit->name() << std::endl;
+                       return cit->name();
+               }
+       }
+       return string();
+}
+
+
 int Formats::getNumber(string const & name) const
 {
        FormatList::const_iterator cit =