+string Formats::getFormatFromFile(FileName const & filename) const
+{
+ if (filename.empty())
+ return string();
+
+ string const format = support::getFormatFromContents(filename);
+ if (!format.empty())
+ return format;
+
+ // try to find a format from the file extension.
+ string const ext(support::getExtension(filename.absFilename()));
+ 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();
+}
+
+namespace {
+
+string fixCommand(string const & cmd, string const & ext,
+ os::auto_open_mode mode)
+{
+ // configure.py says we do not want a viewer/editor
+ if (cmd.empty())
+ return cmd;
+
+ // Does the OS manage this format?
+ if (os::canAutoOpenFile(ext, mode))
+ return "auto";
+
+ // if configure.py found nothing, clear the command
+ if (token(cmd, ' ', 0) == "auto")
+ return string();
+
+ // use the command found by configure.py
+ return cmd;
+}
+
+}
+
+void Formats::setAutoOpen()
+{
+ FormatList::iterator fit = formatlist.begin();
+ FormatList::iterator const fend = formatlist.end();
+ for ( ; fit != fend ; ++fit) {
+ fit->setViewer(fixCommand(fit->viewer(), fit->extension(), os::VIEW));
+ fit->setEditor(fixCommand(fit->editor(), fit->extension(), os::EDIT));
+ }
+}
+
+