]> git.lyx.org Git - lyx.git/blobdiff - src/converter.C
Lars says _(_(b)) is a bug ... fix
[lyx.git] / src / converter.C
index ced4400b1104b6eed346ca29b573cc8cd5861ba2..c37a0028855308fd9db5a3156a92a895167c8caf 100644 (file)
@@ -1,8 +1,8 @@
 /* This file is part of
- * ====================================================== 
- * 
+ * ======================================================
+ *
  *           LyX, The Document Processor
- *        
+ *
  *           Copyright 1995 Matthias Ettrich
  *           Copyright 1995-2001 The LyX Team.
  *
 #pragma implementation
 #endif
 
-#include <cctype>
-
 #include "converter.h"
 #include "lyxrc.h"
-#include "support/syscall.h"
-#include "support/path.h"
-#include "support/filetools.h"
 #include "buffer.h"
 #include "bufferview_funcs.h"
 #include "LaTeX.h"
-#include "LyXView.h"
-#include "lyx_gui_misc.h"
 #include "lyx_cb.h" // ShowMessage()
-#include "support/lyxfunctional.h"
 #include "gettext.h"
 #include "BufferView.h"
 #include "debug.h"
 
+#include "frontends/Alert.h"
+#include "frontends/LyXView.h"
+
+#include "support/filetools.h"
+#include "support/lyxfunctional.h"
+#include "support/path.h"
+#include "support/systemcall.h"
+
+#include "BoostFormat.h"
+
+#include <cctype>
+
+#ifndef CXX_GLOBAL_CSTD
+using std::isdigit;
+#endif
+
 using std::vector;
 using std::queue;
 using std::endl;
@@ -45,6 +53,7 @@ namespace {
 string const token_from("$$i");
 string const token_base("$$b");
 string const token_to("$$o");
+string const token_path("$$p");
 
 //////////////////////////////////////////////////////////////////////////////
 
@@ -70,7 +79,7 @@ bool Format::isChildFormat() const
 {
        if (name_.empty())
                return false;
-        return isdigit(name_[name_.length() - 1]);
+       return isdigit(name_[name_.length() - 1]);
 }
 
 
@@ -114,10 +123,10 @@ void Formats::add(string const & name)
 }
 
 
-void Formats::add(string const & name, string const & extension, 
+void Formats::add(string const & name, string const & extension,
                  string const & prettyname, string const & shortcut)
 {
-       FormatList::iterator it = 
+       FormatList::iterator it =
                find_if(formatlist.begin(), formatlist.end(),
                        lyx::compare_memfun(&Format::name, name));
        if (it == formatlist.end())
@@ -132,7 +141,7 @@ void Formats::add(string const & name, string const & extension,
 
 void Formats::erase(string const & name)
 {
-       FormatList::iterator it = 
+       FormatList::iterator it =
                find_if(formatlist.begin(), formatlist.end(),
                        lyx::compare_memfun(&Format::name, name));
        if (it != formatlist.end())
@@ -168,9 +177,15 @@ bool Formats::view(Buffer const * buffer, string const & filename,
            format->isChildFormat())
                format = getFormat(format->parentFormat());
        if (!format || format->viewer().empty()) {
-               WriteAlert(_("Can not view file"),
-                          _("No information for viewing ")
-                          + prettyName(format_name));
+#if USE_BOOST_FORMAT
+               Alert::alert(_("Cannot view file"),
+                            boost::io::str(boost::format(_("No information for viewing %1$s"))
+                          % prettyName(format_name)));
+#else
+               Alert::alert(_("Cannot view file"),
+                            _("No information for viewing ")
+                            + prettyName(format_name));
+#endif
                           return false;
        }
 
@@ -178,27 +193,32 @@ bool Formats::view(Buffer const * buffer, string const & filename,
 
        if (format_name == "dvi" &&
            !lyxrc.view_dvi_paper_option.empty()) {
-               command += " " + lyxrc.view_dvi_paper_option;
+               command += ' ' + lyxrc.view_dvi_paper_option;
                string paper_size = converters.papersize(buffer);
                if (paper_size == "letter")
                        paper_size = "us";
-               command += " " + paper_size;
-               if (buffer->params.orientation 
+               command += ' ' + paper_size;
+               if (buffer->params.orientation
                    == BufferParams::ORIENTATION_LANDSCAPE)
                        command += 'r';
-        }
+       }
 
-       command += " " + QuoteName(OnlyFilename((filename)));
+       if (!contains(command, token_from))
+               command += ' ' + token_from;
+
+       command = subst(command, token_from,
+                       QuoteName(OnlyFilename(filename)));
+       command = subst(command, token_path, QuoteName(OnlyPath(filename)));
 
        lyxerr[Debug::FILES] << "Executing command: " << command << endl;
        ShowMessage(buffer, _("Executing command:"), command);
 
        Path p(OnlyPath(filename));
-       Systemcalls one;
-       int const res = one.startscript(Systemcalls::SystemDontWait, command);
+       Systemcall one;
+       int const res = one.startscript(Systemcall::DontWait, command);
 
        if (res) {
-               WriteAlert(_("Can not view file"),
+               Alert::alert(_("Cannot view file"),
                           _("Error while executing"),
                           command.substr(0, 50));
                return false;
@@ -258,10 +278,13 @@ void Converter::readFlags()
 
 bool operator<(Converter const & a, Converter const & b)
 {
-       int const i = compare_no_case(a.From->prettyname(),
-                                     b.From->prettyname());
+       // 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.
+       int const i = compare_ascii_no_case(a.From->prettyname(),
+                                           b.From->prettyname());
        if (i == 0)
-               return compare_no_case(a.To->prettyname(), b.To->prettyname())
+               return compare_ascii_no_case(a.To->prettyname(), b.To->prettyname())
                        < 0;
        else
                return i < 0;
@@ -282,7 +305,6 @@ private:
 };
 
 
-#warning why is a pointer returned and not a const reference? (Lgb)
 Converter const * Converters::getConverter(string const & from,
                                            string const & to)
 {
@@ -324,7 +346,7 @@ void Converters::add(string const & from, string const & to,
                converter.flags = flags;
        }
        converter.readFlags();
-       
+
        if (converter.latex && (latex_command_.empty() || to == "dvi"))
                latex_command_ = subst(command, token_from, "");
        // If we have both latex & pdflatex, we set latex_command to latex.
@@ -354,10 +376,10 @@ void Converters::erase(string const & from, string const & to)
 // This method updates the pointers From and To in all the converters.
 // The code is not very efficient, but it doesn't matter as the number
 // of formats and converters is small.
-// Furthermore, this method is called only on startup, or after 
+// Furthermore, this method is called only on startup, or after
 // adding/deleting a format in FormPreferences (the latter calls can be
 // eliminated if the formats in the Formats class are stored using a map or
-// a list (instead of a vector), but this will cause other problems). 
+// a list (instead of a vector), but this will cause other problems).
 void Converters::update(Formats const & formats)
 {
        ConverterList::iterator it = converterlist_.begin();
@@ -418,7 +440,7 @@ Converters::getReachableTo(string const & target, bool clear_visited)
                if (i != s || target != "lyx") {
                        result.push_back(&formats.get(i));
                }
-               
+
                vector<int>::iterator it = vertices_[i].in_vertices.begin();
                vector<int>::iterator end = vertices_[i].in_vertices.end();
                for (; it != end; ++it) {
@@ -522,7 +544,7 @@ Converters::getPath(string const & from, string const & to)
                        found = true;
                        break;
                }
-               
+
                vector<int>::const_iterator beg =
                        vertices_[i].out_vertices.begin();
                vector<int>::const_iterator cit = beg;
@@ -575,10 +597,6 @@ bool Converters::convert(Buffer const * buffer,
 
        EdgePath edgepath = getPath(from_format, to_format);
        if (edgepath.empty()) {
-               WriteAlert(_("Can not convert file"),
-                          _("No information for converting from ")
-                          + formats.prettyName(from_format) + _(" to ")
-                          + formats.prettyName(to_format));
                return false;
        }
 
@@ -605,6 +623,14 @@ bool Converters::convert(Buffer const * buffer,
                                  subst(conv.result_file,
                                        token_base, OnlyFilename(from_base)));
 
+               // if input and output files are equal, we use a
+               // temporary file as intermediary (JMarc)
+               string real_outfile;
+               if (outfile == infile) {
+                       real_outfile = infile;
+                       outfile = AddName(buffer->tmppath, "tmpfile.out");
+               }
+
                if (conv.latex) {
                        run_latex = true;
                        string command = subst(conv.command, token_from, "");
@@ -614,8 +640,8 @@ bool Converters::convert(Buffer const * buffer,
                } else {
                        if (conv.need_aux && !run_latex
                            && !latex_command_.empty()) {
-                               lyxerr[Debug::FILES] 
-                                       << "Running " << latex_command_ 
+                               lyxerr[Debug::FILES]
+                                       << "Running " << latex_command_
                                        << " to update aux file"<<  endl;
                                runLaTeX(buffer, latex_command_);
                        }
@@ -629,6 +655,7 @@ bool Converters::convert(Buffer const * buffer,
                        command = subst(command, token_from, QuoteName(infile2));
                        command = subst(command, token_base, QuoteName(from_base));
                        command = subst(command, token_to, QuoteName(outfile2));
+                       command = LibScriptSearch(command);
 
                        if (!conv.parselog.empty())
                                command += " 2> " + QuoteName(infile2 + ".out");
@@ -644,33 +671,44 @@ bool Converters::convert(Buffer const * buffer,
                        if (buffer)
                                ShowMessage(buffer, _("Executing command:"), command);
 
-                       Systemcalls::Starttype type = (dummy)
-                               ? Systemcalls::SystemDontWait : Systemcalls::System;
-                       Systemcalls one;
+                       Systemcall::Starttype type = (dummy)
+                               ? Systemcall::DontWait : Systemcall::Wait;
+                       Systemcall one;
                        int res;
                        if (conv.original_dir && buffer) {
-                               Path p(buffer->filepath);
+                               Path p(buffer->filePath());
                                res = one.startscript(type, command);
                        } else
                                res = one.startscript(type, command);
 
+                       if (!real_outfile.empty()) {
+                               if (!lyx::rename(outfile, real_outfile))
+                                       res = -1;
+                               else
+                                       lyxerr[Debug::FILES]
+                                               << "renaming file " << outfile
+                                               << " to " << real_outfile
+                                               << endl;
+                       }
+                       
                        if (!conv.parselog.empty()) {
                                string const logfile =  infile2 + ".log";
-                               string const command2 = conv.parselog +
+                               string const script = LibScriptSearch(conv.parselog);
+                               string const command2 = script +
                                        " < " + QuoteName(infile2 + ".out") +
                                        " > " + QuoteName(logfile);
-                               one.startscript(Systemcalls::System, command2);
+                               one.startscript(Systemcall::Wait, command2);
                                if (!scanLog(buffer, command, logfile))
                                        return false;
                        }
 
                        if (res) {
                                if (conv.to == "program")
-                                       WriteAlert(_("There were errors during the Build process."),
+                                       Alert::alert(_("There were errors during the Build process."),
                                                   _("You should try to fix them."));
                                else
-                                       WriteAlert(_("Can not convert file"),
-                                                  "Error while executing",
+                                       Alert::alert(_("Cannot convert file"),
+                                                  _("Error while executing"),
                                                   command.substr(0, 50));
                                return false;
                        }
@@ -692,17 +730,23 @@ bool Converters::convert(Buffer const * buffer,
                        string to = subst(conv.result_dir,
                                          token_base, to_base);
                        if (!lyx::rename(from, to)) {
-                               WriteAlert(_("Error while trying to move directory:"),
-                                          from, ("to ") + to);
+#if USE_BOOST_FORMAT
+                               Alert::alert(_("Error while trying to move directory:"),
+                                          from, boost::io::str(boost::format(_("to %1$s")) % to));
+#else
+                               Alert::alert(_("Error while trying to move directory:"),
+                                          from, _("to ") + to);
+#endif
                                return false;
                        }
                }
                return true;
-       } else 
+       } else
                return move(outfile, to_file, conv.latex);
 }
 
-// If from = /path/file.ext and to = /path2/file2.ext2 then this method 
+
+// If from = /path/file.ext and to = /path2/file2.ext2 then this method
 // moves each /path/file*.ext file to /path2/file2*.ext2'
 bool Converters::move(string const & from, string const & to, bool copy)
 {
@@ -719,17 +763,22 @@ bool Converters::move(string const & from, string const & to, bool copy)
        for (vector<string>::const_iterator it = files.begin();
             it != files.end(); ++it)
                if (prefixIs(*it, base)) {
-                       string from2 = path + *it;
+                       string const from2 = path + *it;
                        string to2 = to_base + it->substr(base.length());
                        to2 = ChangeExtension(to2, to_extension);
-                       lyxerr[Debug::FILES] << "moving " << from2 
+                       lyxerr[Debug::FILES] << "moving " << from2
                                             << " to " << to2 << endl;
-                       bool moved = (copy)
+                       bool const moved = (copy)
                                ? lyx::copy(from2, to2)
                                : lyx::rename(from2, to2);
                        if (!moved && no_errors) {
-                               WriteAlert(_("Error while trying to move file:"),
+#if USE_BOOST_FORMAT
+                               Alert::alert(_("Error while trying to move file:"),
+                                          from2, boost::io::str(boost::format(_("to %1$s")) % to2));
+#else
+                               Alert::alert(_("Error while trying to move file:"),
                                           from2, _("to ") + to2);
+#endif
                                no_errors = false;
                        }
                }
@@ -782,11 +831,9 @@ bool Converters::scanLog(Buffer const * buffer, string const & command,
                return false;
 
        BufferView * bv = buffer->getUser();
-       bool need_redraw = false;
        if (bv) {
                bv->owner()->prohibitInput();
-               // Remove all error insets
-               need_redraw = bv->removeAutoInsets();
+               // all error insets should have been removed by now
        }
 
        LaTeX latex("", filename, "");
@@ -796,10 +843,8 @@ bool Converters::scanLog(Buffer const * buffer, string const & command,
                if ((result & LaTeX::ERRORS)) {
                        // Insert all errors as errors boxes
                        bv->insertErrors(terr);
-                       need_redraw = true;
-               }
-               if (need_redraw) {
-                       bv->redraw();
+#warning repaint() or update() or nothing ?
+                       bv->repaint();
                        bv->fitCursor();
                }
                bv->owner()->allowInput();
@@ -819,13 +864,18 @@ bool Converters::scanLog(Buffer const * buffer, string const & command,
                }
                string head;
                split(command, head, ' ');
-               WriteAlert(_("There were errors during running of ") + head,
+#if USE_BOOST_FORMAT
+               Alert::alert(boost::io::str(boost::format(_("There were errors during running of %1$s")) % head),
+                          s, t);
+#else
+               Alert::alert(_("There were errors during running of ") + head,
                           s, t);
+#endif
                return false;
        } else if (result & LaTeX::NO_OUTPUT) {
                string const s = _("The operation resulted in");
                string const t = _("an empty file.");
-               WriteAlert(_("Resulting file is empty"), s, t);
+               Alert::alert(_("Resulting file is empty"), s, t);
                return false;
        }
        return true;
@@ -838,42 +888,33 @@ bool Converters::runLaTeX(Buffer const * buffer, string const & command)
                return false;
 
        BufferView * bv = buffer->getUser();
-       string name = buffer->getLatexName();
-       bool need_redraw = false;
 
        if (bv) {
                bv->owner()->prohibitInput();
                bv->owner()->message(_("Running LaTeX..."));
-               // Remove all error insets
-               need_redraw = bv->removeAutoInsets();
+               // all the autoinsets have already been removed
        }
 
-
-       // do the LaTex run(s)
+       // do the LaTeX run(s)
+       string name = buffer->getLatexName();
+       LaTeX latex(command, name, buffer->filePath());
        TeXErrors terr;
-       LaTeX latex(command, name, buffer->filepath);
        int result = latex.run(terr,
-                              bv ? bv->owner()->getLyXFunc() : 0);
-       
+                              bv ? &bv->owner()->getLyXFunc() : 0);
 
        if (bv) {
                if ((result & LaTeX::ERRORS)) {
                        // Insert all errors as errors boxes
                        bv->insertErrors(terr);
-                       need_redraw = true;
-               }
-
-               // if we removed error insets before we ran LaTeX or if we inserted
-               // error insets after we ran LaTeX this must be run:
-               if (need_redraw) {
-                       bv->redraw();
+#warning repaint() or update() or nothing ?
+                       bv->repaint();
                        bv->fitCursor();
                }
        }
 
        // check return value from latex.run().
        if ((result & LaTeX::NO_LOGFILE)) {
-               WriteAlert(_("LaTeX did not work!"),
+               Alert::alert(_("LaTeX did not work!"),
                           _("Missing log file:"), name);
        } else if ((result & LaTeX::ERRORS)) {
                int num_errors = latex.getNumErrors();
@@ -887,22 +928,22 @@ bool Converters::runLaTeX(Buffer const * buffer, string const & command)
                        s += _(" errors detected.");
                        t = _("You should try to fix them.");
                }
-               WriteAlert(_("There were errors during the LaTeX run."),
+               Alert::alert(_("There were errors during the LaTeX run."),
                           s, t);
        }  else if (result & LaTeX::NO_OUTPUT) {
                string const s = _("The operation resulted in");
                string const t = _("an empty file.");
-               WriteAlert(_("Resulting file is empty"), s, t);
+               Alert::alert(_("Resulting file is empty"), s, t);
        }
 
        if (bv)
                bv->owner()->allowInput();
-       int const ERROR_MASK = 
+
+       int const ERROR_MASK =
                        LaTeX::NO_LOGFILE |
                        LaTeX::ERRORS |
                        LaTeX::NO_OUTPUT;
-       
+
        return (result & ERROR_MASK) == 0;
 
 }