]> git.lyx.org Git - lyx.git/blobdiff - src/Format.cpp
Allow \nocite again with the basic citation engine.
[lyx.git] / src / Format.cpp
index 1b2ffea0d7ddde5ef00679fd9629f3d919126717..2ef745e50d12a9d38f03ee0c52f35c50414ff85f 100644 (file)
 #include "support/gettext.h"
 #include "support/lstrings.h"
 #include "support/os.h"
+#include "support/Path.h"
 #include "support/Systemcall.h"
+#include "support/textutils.h"
+#include "support/Translator.h"
+
+#include <algorithm>
+#include <map>
+#include <ctime>
 
 // FIXME: Q_WS_MACX is not available, it's in Qt
 #ifdef USE_MACOSX_PACKAGING
@@ -64,7 +71,7 @@ public:
                : extension_(extension) {}
        bool operator()(Format const & f) const
        {
-               return f.extension() == extension_;
+               return f.hasExtension(extension_);
        }
 private:
        string extension_;
@@ -72,22 +79,22 @@ private:
 
 } //namespace anon
 
+
 bool operator<(Format const & a, Format const & b)
 {
-       // use the compare_ascii_no_case instead of compare_no_case,
-       // because in turkish, 'i' is not the lowercase version of 'I',
-       // and thus turkish locale breaks parsing of tags.
-
-       return compare_ascii_no_case(a.prettyname(), b.prettyname()) < 0;
+       return _(a.prettyname()) < _(b.prettyname());
 }
 
 
 Format::Format(string const & n, string const & e, string const & p,
               string const & s, string const & v, string const & ed,
               int flags)
-       : name_(n), extension_(e), prettyname_(p), shortcut_(s), viewer_(v),
+       : name_(n), prettyname_(p), shortcut_(s), viewer_(v),
          editor_(ed), flags_(flags)
-{}
+{
+       extension_list_ = getVectorFromString(e, ",");
+       LYXERR(Debug::GRAPHICS, "New Format: n=" << n << ", flags=" << flags);
+}
 
 
 bool Format::dummy() const
@@ -96,11 +103,24 @@ bool Format::dummy() const
 }
 
 
+string const Format::extensions() const
+{
+       return getStringFromVector(extension_list_, ", ");
+}
+
+
+bool Format::hasExtension(string const & e) const
+{
+       return (find(extension_list_.begin(), extension_list_.end(), e)
+               != extension_list_.end());
+}
+
+
 bool Format::isChildFormat() const
 {
        if (name_.empty())
                return false;
-       return isdigit(name_[name_.length() - 1]);
+       return isDigitASCII(name_[name_.length() - 1]);
 }
 
 
@@ -110,6 +130,12 @@ string const Format::parentFormat() const
 }
 
 
+void Format::setExtensions(string const & e)
+{
+       extension_list_ = getVectorFromString(e, ",");
+}
+
+
 // This method should return a reference, and throw an exception
 // if the format named name cannot be found (Lgb)
 Format const * Formats::getFormat(string const & name) const
@@ -130,11 +156,26 @@ string Formats::getFormatFromFile(FileName const & filename) const
                return string();
 
        string const format = filename.guessFormatFromContents();
+       string const ext = getExtension(filename.absFileName());
+       if ((format == "gzip" || format == "zip" || format == "compress")
+           && !ext.empty()) {
+               string const & fmt_name = formats.getFormatFromExtension(ext);
+               if (!fmt_name.empty()) {
+                       Format const * p_format = formats.getFormat(fmt_name);
+                       if (p_format && p_format->zippedNative())
+                               return p_format->name();
+               }
+       }
        if (!format.empty())
                return format;
 
        // try to find a format from the file extension.
-       string const ext = getExtension(filename.absFileName());
+       return getFormatFromExtension(ext);
+}
+
+
+string Formats::getFormatFromExtension(string const & ext) const
+{
        if (!ext.empty()) {
                // this is ambigous if two formats have the same extension,
                // but better than nothing
@@ -151,6 +192,32 @@ string Formats::getFormatFromFile(FileName const & filename) const
 }
 
 
+/// Used to store last timestamp of file and whether it is (was) zipped
+struct ZippedInfo {
+       bool zipped;
+       std::time_t timestamp;
+       ZippedInfo(bool zipped, std::time_t timestamp)
+       : zipped(zipped), timestamp(timestamp) { }
+};
+
+
+/// Mapping absolute pathnames of files to their ZippedInfo metadata.
+static std::map<std::string, ZippedInfo> zipped_;
+
+
+bool Formats::isZippedFile(support::FileName const & filename) const {
+       string const & fname = filename.absFileName();
+       time_t timestamp = filename.lastModified();
+       map<string, ZippedInfo>::iterator it = zipped_.find(fname);
+       if (it != zipped_.end() && it->second.timestamp == timestamp)
+               return it->second.zipped;
+       string const & format = getFormatFromFile(filename);
+       bool zipped = (format == "gzip" || format == "zip");
+       zipped_.insert(pair<string, ZippedInfo>(fname, ZippedInfo(zipped, timestamp)));
+       return zipped;
+}
+
+
 static string fixCommand(string const & cmd, string const & ext,
                  os::auto_open_mode mode)
 {
@@ -202,7 +269,7 @@ void Formats::add(string const & name)
 }
 
 
-void Formats::add(string const & name, string const & extension,
+void Formats::add(string const & name, string const & extensions,
                  string const & prettyname, string const & shortcut,
                  string const & viewer, string const & editor,
                  int flags)
@@ -211,10 +278,10 @@ void Formats::add(string const & name, string const & extension,
                find_if(formatlist.begin(), formatlist.end(),
                        FormatNamesEqual(name));
        if (it == formatlist.end())
-               formatlist.push_back(Format(name, extension, prettyname,
+               formatlist.push_back(Format(name, extensions, prettyname,
                                            shortcut, viewer, editor, flags));
        else
-               *it = Format(name, extension, prettyname, shortcut, viewer,
+               *it = Format(name, extensions, prettyname, shortcut, viewer,
                             editor, flags);
 }
 
@@ -281,7 +348,7 @@ bool Formats::view(Buffer const & buffer, FileName const & filename,
        }
        // viewer is 'auto'
        if (format->viewer() == "auto") {
-               if (os::autoOpenFile(filename.absFileName(), os::VIEW))
+               if (os::autoOpenFile(filename.absFileName(), os::VIEW, buffer.filePath()))
                        return true;
                else {
                        Alert::error(_("Cannot view file"),
@@ -308,22 +375,18 @@ bool Formats::view(Buffer const & buffer, FileName const & filename,
        if (!contains(command, token_from_format))
                command += ' ' + token_from_format;
 
-       command = subst(command, token_from_format, quoteName(filename.toFilesystemEncoding()));
+       command = subst(command, token_from_format, quoteName(onlyFileName(filename.toFilesystemEncoding())));
        command = subst(command, token_path_format, quoteName(onlyPath(filename.toFilesystemEncoding())));
        command = subst(command, token_socket_format, quoteName(theServerSocket().address()));
        LYXERR(Debug::FILES, "Executing command: " << command);
        // FIXME UNICODE utf8 can be wrong for files
        buffer.message(_("Executing command: ") + from_utf8(command));
 
+       PathChanger p(filename.onlyPath());
        Systemcall one;
-       int const res = one.startscript(Systemcall::DontWait, command);
+       one.startscript(Systemcall::DontWait, command, buffer.filePath());
 
-       if (res) {
-               Alert::error(_("Cannot view file"),
-                            bformat(_("An error occurred whilst running %1$s"),
-                              makeDisplayPath(command, 50)));
-               return false;
-       }
+       // we can't report any sort of error, since we aren't waiting
        return true;
 }
 
@@ -362,10 +425,10 @@ bool Formats::edit(Buffer const & buffer, FileName const & filename,
                                prettyName(format_name)));
                return false;
        }
-       
+
        // editor is 'auto'
        if (format->editor() == "auto") {
-               if (os::autoOpenFile(filename.absFileName(), os::EDIT))
+               if (os::autoOpenFile(filename.absFileName(), os::EDIT, buffer.filePath()))
                        return true;
                else {
                        Alert::error(_("Cannot edit file"),
@@ -388,14 +451,9 @@ bool Formats::edit(Buffer const & buffer, FileName const & filename,
        buffer.message(_("Executing command: ") + from_utf8(command));
 
        Systemcall one;
-       int const res = one.startscript(Systemcall::DontWait, command);
+       one.startscript(Systemcall::DontWait, command, buffer.filePath());
 
-       if (res) {
-               Alert::error(_("Cannot edit file"),
-                            bformat(_("An error occurred whilst running %1$s"),
-                              makeDisplayPath(command, 50)));
-               return false;
-       }
+       // we can't report any sort of error, since we aren't waiting
        return true;
 }
 
@@ -420,8 +478,53 @@ string const Formats::extension(string const & name) const
 }
 
 
+string const Formats::extensions(string const & name) const
+{
+       Format const * format = getFormat(name);
+       if (format)
+               return format->extensions();
+       else
+               return name;
+}
+
+
+namespace {
+typedef Translator<OutputParams::FLAVOR, string> FlavorTranslator;
+
+FlavorTranslator initFlavorTranslator()
+{
+       FlavorTranslator f(OutputParams::LATEX, "latex");
+       f.addPair(OutputParams::DVILUATEX, "dviluatex");
+       f.addPair(OutputParams::LUATEX, "luatex");
+       f.addPair(OutputParams::PDFLATEX, "pdflatex");
+       f.addPair(OutputParams::XETEX, "xetex");
+       f.addPair(OutputParams::XML, "docbook-xml");
+       f.addPair(OutputParams::HTML, "xhtml");
+       f.addPair(OutputParams::TEXT, "text");
+       return f;
+}
+
+
+FlavorTranslator const & flavorTranslator()
+{
+       static FlavorTranslator translator = initFlavorTranslator();
+       return translator;
+}
+}
+
+
+std::string flavor2format(OutputParams::FLAVOR flavor)
+{
+       return flavorTranslator().find(flavor);
+}
 
 
+/* Not currently needed, but I'll leave the code in case it is.
+OutputParams::FLAVOR format2flavor(std::string fmt)
+{
+       return flavorTranslator().find(fmt);
+} */
+
 Formats formats;
 
 Formats system_formats;