]> git.lyx.org Git - lyx.git/blobdiff - src/Converter.cpp
Only create a new view for lyxfiles-open if no view is open (#12894)
[lyx.git] / src / Converter.cpp
index 5ea8866ce29c8c22d6db1d0d89ef8fa600b23e34..b63f77146a6f57e4dee563ef07d0e3066a553071 100644 (file)
@@ -13,9 +13,9 @@
 #include "Converter.h"
 
 #include "Buffer.h"
-#include "buffer_funcs.h"
 #include "BufferParams.h"
 #include "ConverterCache.h"
+#include "TextClass.h"
 #include "Encoding.h"
 #include "ErrorList.h"
 #include "Format.h"
@@ -24,6 +24,7 @@
 #include "LaTeX.h"
 #include "LyXRC.h"
 #include "Mover.h"
+#include "OutputParams.h"
 #include "ParagraphList.h"
 #include "Session.h"
 
@@ -58,9 +59,11 @@ string const token_to("$$o");
 string const token_path("$$p");
 string const token_orig_path("$$r");
 string const token_orig_from("$$f");
+string const token_textclass("$$c");
+string const token_modules("$$m");
 string const token_encoding("$$e");
 string const token_latex_encoding("$$E");
-
+string const token_python("$${python}");
 
 string const add_options(string const & command, string const & options)
 {
@@ -105,7 +108,7 @@ private:
 Converter::Converter(string const & f, string const & t,
                     string const & c, string const & l)
        : from_(f), to_(t), command_(c), flags_(l),
-         From_(nullptr), To_(nullptr), latex_(false), xml_(false),
+         From_(nullptr), To_(nullptr), latex_(false), docbook_(false),
          need_aux_(false), nice_(false), need_auth_(false)
 {}
 
@@ -122,7 +125,7 @@ void Converter::readFlags()
                        latex_flavor_ = flag_value.empty() ?
                                "latex" : flag_value;
                } else if (flag_name == "xml")
-                       xml_ = true;
+                       docbook_ = true;
                else if (flag_name == "needaux") {
                        need_aux_ = true;
                        latex_flavor_ = flag_value.empty() ?
@@ -148,6 +151,12 @@ void Converter::readFlags()
 }
 
 
+void Converter::setCommand(std::string const & command)
+{
+       command_ = subst(command, token_python, os::python());
+}
+
+
 Converter const * Converters::getConverter(string const & from,
                                            string const & to) const
 {
@@ -259,32 +268,32 @@ void Converters::updateLast(Formats const & formats)
 }
 
 
-OutputParams::FLAVOR Converters::getFlavor(Graph::EdgePath const & path,
-                                          Buffer const * buffer)
+Flavor Converters::getFlavor(Graph::EdgePath const & path,
+                                          Buffer const * buffer) const
 {
        for (auto const & edge : path) {
                Converter const & conv = converterlist_[edge];
                if (conv.latex() || conv.need_aux()) {
                        if (conv.latex_flavor() == "latex")
-                               return OutputParams::LATEX;
+                               return Flavor::LaTeX;
                        if (conv.latex_flavor() == "xelatex")
-                               return OutputParams::XETEX;
+                               return Flavor::XeTeX;
                        if (conv.latex_flavor() == "lualatex")
-                               return OutputParams::LUATEX;
+                               return Flavor::LuaTeX;
                        if (conv.latex_flavor() == "dvilualatex")
-                               return OutputParams::DVILUATEX;
+                               return Flavor::DviLuaTeX;
                        if (conv.latex_flavor() == "pdflatex")
-                               return OutputParams::PDFLATEX;
+                               return Flavor::PdfLaTeX;
                }
-               if (conv.xml())
-                       return OutputParams::XML;
+               if (conv.docbook())
+                       return Flavor::DocBook5;
        }
        return buffer ? buffer->params().getOutputFlavor()
-                     : OutputParams::LATEX;
+                     : Flavor::LaTeX;
 }
 
 
-string Converters::getHyperrefDriver(Graph::EdgePath const & path)
+string Converters::getHyperrefDriver(Graph::EdgePath const & path) const
 {
        for (auto const & edge : path) {
                Converter const & conv = converterlist_[edge];
@@ -466,7 +475,7 @@ Converters::RetVal Converters::convert(Buffer const * buffer,
                return FAILURE;
        }
 
-       // buffer is only invalid for importing, and then runparams is not
+       // buffer can only be null for importing, and then runparams is not
        // used anyway.
        OutputParams runparams(buffer ? &buffer->params().encoding() : nullptr);
        runparams.flavor = getFlavor(edgepath, buffer);
@@ -478,11 +487,10 @@ Converters::RetVal Converters::convert(Buffer const * buffer,
                         || suffixIs(bp.bufferFormat(), "-ja"))
                        && bp.encoding().package() == Encoding::japanese;
                runparams.use_indices = bp.use_indices;
-               runparams.bibtex_command = bp.bibtexCommand();
+               runparams.bibtex_command = bp.bibtexCommand(true);
                runparams.index_command = (bp.index_command == "default") ?
                        string() : bp.index_command;
-               runparams.document_language = bp.language->babel();
-               runparams.xindy_language = bp.language->xindy();
+               runparams.document_language = bp.language->lang();
                // Some macros rely on font encoding
                runparams.main_fontenc = bp.main_font_encoding();
                runparams.only_childbibs = !bp.useBiblatex()
@@ -595,24 +603,24 @@ Converters::RetVal Converters::convert(Buffer const * buffer,
                        // FIXME KILLED
                        // Check changed return value here.
                        RetVal const retval = runLaTeX(*buffer, command, runparams, errorList);
-                               if (retval != SUCCESS)
-                                       return retval;
+                       if (retval != SUCCESS)
+                               return retval;
                } else {
                        if (conv.need_aux() && !run_latex) {
                                // We are not importing, we have a buffer
                                LASSERT(buffer, return FAILURE);
                                string command;
                                switch (runparams.flavor) {
-                               case OutputParams::DVILUATEX:
+                               case Flavor::DviLuaTeX:
                                        command = dvilualatex_command_;
                                        break;
-                               case OutputParams::LUATEX:
+                               case Flavor::LuaTeX:
                                        command = lualatex_command_;
                                        break;
-                               case OutputParams::PDFLATEX:
+                               case Flavor::PdfLaTeX:
                                        command = pdflatex_command_;
                                        break;
-                               case OutputParams::XETEX:
+                               case Flavor::XeTeX:
                                        command = xelatex_command_;
                                        break;
                                default:
@@ -638,13 +646,25 @@ Converters::RetVal Converters::convert(Buffer const * buffer,
                                to_utf8(makeRelPath(from_utf8(outfile.absFileName()), from_utf8(path)));
 
                        string command = conv.command();
+                       BufferParams const & bparams = buffer ? buffer->params() : defaultBufferParams();
                        command = subst(command, token_from, quoteName(infile2));
                        command = subst(command, token_base, quoteName(from_base));
                        command = subst(command, token_to, quoteName(outfile2));
                        command = subst(command, token_path, quoteName(onlyPath(infile.absFileName())));
                        command = subst(command, token_orig_path, quoteName(onlyPath(orig_from.absFileName())));
                        command = subst(command, token_orig_from, quoteName(onlyFileName(orig_from.absFileName())));
-                       command = subst(command, token_encoding, buffer ? buffer->params().encoding().iconvName() : string());
+                       command = subst(command, token_textclass, quoteName(bparams.documentClass().name()));
+                       string modules = bparams.getModules().asString();
+                       // FIXME: remove when SystemCall uses QProcess with the list API.
+                       // Currently the QProcess parser is not able to encode an
+                       // empty argument as ""; work around this by passing a
+                       // single comma, that will be interpreted as a list of two
+                       // empty module names.
+                       if (modules.empty())
+                               modules = ",";
+                       command = subst(command, token_modules, quoteName(modules));
+                       command = subst(command, token_encoding, quoteName(bparams.encoding().iconvName()));
+                       command = subst(command, token_python, os::python());
 
                        if (!conv.parselog().empty())
                                command += " 2> " + quoteName(infile2 + ".out");
@@ -685,11 +705,11 @@ Converters::RetVal Converters::convert(Buffer const * buffer,
                                if (res == Systemcall::KILLED) {
                                        frontend::Alert::warning(
                                                _("Converter killed"),
-                                               bformat(_("The following converter was killed by the user.\n %1$s\n"), 
+                                               bformat(_("The following converter was killed by the user.\n %1$s\n"),
                                                        from_utf8(command)));
                                        return KILLED;
                                }
-                               
+
                                if (!real_outfile.empty()) {
                                        Mover const & mover = getMover(conv.to());
                                        if (!mover.rename(outfile, real_outfile))
@@ -714,7 +734,7 @@ Converters::RetVal Converters::convert(Buffer const * buffer,
                                        if (res == Systemcall::KILLED) {
                                                frontend::Alert::warning(
                                                        _("Converter killed"),
-                                                       bformat(_("The following converter was killed by the user.\n %1$s\n"), 
+                                                       bformat(_("The following converter was killed by the user.\n %1$s\n"),
                                                                from_utf8(command)));
                                                return KILLED;
                                        }
@@ -729,13 +749,13 @@ Converters::RetVal Converters::convert(Buffer const * buffer,
                                                bformat(_("The conversion process was killed while running:\n%1$s"),
                                                        wrapParas(from_utf8(command))));
                                        return KILLED;
-                               } 
+                               }
                                if (res == Systemcall::TIMEOUT) {
                                        Alert::information(_("Process Timed Out"),
                                                bformat(_("The conversion process:\n%1$s\ntimed out before completing."),
                                                        wrapParas(from_utf8(command))));
                                        return KILLED;
-                               } 
+                               }
                                if (conv.to() == "program") {
                                        Alert::error(_("Build errors"),
                                                _("There were errors during the build process."));
@@ -820,7 +840,7 @@ bool Converters::move(string const & fmt,
 }
 
 
-bool Converters::formatIsUsed(string const & format)
+bool Converters::formatIsUsed(string const & format) const
 {
        for (auto const & cvt : converterlist_) {
                if (cvt.from() == format || cvt.to() == format)
@@ -834,7 +854,7 @@ bool Converters::scanLog(Buffer const & buffer, string const & /*command*/,
                         FileName const & filename, ErrorList & errorList)
 {
        OutputParams runparams(nullptr);
-       runparams.flavor = OutputParams::LATEX;
+       runparams.flavor = Flavor::LaTeX;
        LaTeX latex("", runparams, filename);
        TeXErrors terr;
        int const result = latex.scanLogFile(terr);
@@ -854,7 +874,7 @@ Converters::RetVal Converters::runLaTeX(Buffer const & buffer, string const & co
 
        // do the LaTeX run(s)
        string const name = buffer.latexName();
-       LaTeX latex(command, runparams, FileName(makeAbsPath(name)),
+       LaTeX latex(command, runparams, makeAbsPath(name),
                    buffer.filePath(), buffer.layoutPos(),
                    buffer.isClone(), buffer.freshStartRequired());
        TeXErrors terr;
@@ -866,7 +886,7 @@ Converters::RetVal Converters::runLaTeX(Buffer const & buffer, string const & co
                });
        int const result = latex.run(terr);
 
-       if (result == Systemcall::KILLED) {
+       if (result == Systemcall::KILLED || result == Systemcall::TIMEOUT) {
                Alert::error(_("Export canceled"),
                        _("The export process was terminated by the user."));
                return KILLED;
@@ -875,11 +895,12 @@ Converters::RetVal Converters::runLaTeX(Buffer const & buffer, string const & co
        if (result & LaTeX::ERRORS)
                buffer.bufferErrors(terr, errorList);
 
-       if ((result & LaTeX::UNDEF_CIT) || (result & LaTeX::UNDEF_REF)) {
+       if ((result & LaTeX::UNDEF_CIT) || (result & LaTeX::UNDEF_UNKNOWN_REF)) {
                buffer.bufferRefs(terr, errorList);
                if (errorList.empty())
                        errorList.push_back(ErrorItem(_("Undefined reference"),
-                               _("Undefined reference or citation was found during the build, please check the Log."),
+                               _("Undefined references or citations were found during the build.\n"
+                                 "Please check the warnings in the LaTeX log (Document > LaTeX Log)."),
                                &buffer));
        }
 
@@ -912,7 +933,7 @@ Converters::RetVal Converters::runLaTeX(Buffer const & buffer, string const & co
                        LaTeX::NO_LOGFILE |
                        LaTeX::ERRORS |
                        LaTeX::UNDEF_CIT |
-                       LaTeX::UNDEF_REF |
+                       LaTeX::UNDEF_UNKNOWN_REF |
                        LaTeX::NO_OUTPUT;
 
        return (result & ERROR_MASK) == 0 ? SUCCESS : FAILURE;