]> git.lyx.org Git - lyx.git/blobdiff - src/converter.C
small fix with footnote, use stringstream some more
[lyx.git] / src / converter.C
index 90babcaa0d66945604b5ea664c2782c8e9e956f1..604e4d70f4048428c34125581fcfeaae90758d22 100644 (file)
 #include "bufferview_funcs.h"
 #include "LaTeX.h"
 #include "LyXView.h"
-#include "minibuffer.h"
 #include "lyx_gui_misc.h"
 #include "lyx_cb.h" // ShowMessage()
 #include "support/lyxfunctional.h"
+#include "gettext.h"
 
 using std::vector;
 using std::queue;
@@ -38,13 +38,15 @@ using std::find_if;
 using std::reverse;
 using std::sort;
 
-static string const token_from("$$i");
-static string const token_base("$$b");
-static string const token_to("$$o");
+namespace {
+
+string const token_from("$$i");
+string const token_base("$$b");
+string const token_to("$$o");
 
 //////////////////////////////////////////////////////////////////////////////
 
-static inline
+inline
 string const add_options(string const & command, string const & options)
 {
        string head;
@@ -52,6 +54,8 @@ string const add_options(string const & command, string const & options)
        return head + ' ' + options + ' ' + tail;
 }
 
+} // namespace anon
+
 //////////////////////////////////////////////////////////////////////////////
 
 bool Format::dummy() const
@@ -81,7 +85,7 @@ Format const * Formats::GetFormat(string const & name) const
 {
        FormatList::const_iterator cit =
                find_if(formatlist.begin(), formatlist.end(),
-                       compare_memfun(&Format::name, name));
+                       lyx::compare_memfun(&Format::name, name));
        if (cit != formatlist.end())
                return &(*cit);
        else
@@ -93,7 +97,7 @@ int Formats::GetNumber(string const & name) const
 {
        FormatList::const_iterator cit =
                find_if(formatlist.begin(), formatlist.end(),
-                       compare_memfun(&Format::name, name));
+                       lyx::compare_memfun(&Format::name, name));
        if (cit != formatlist.end())
                return cit - formatlist.begin();
        else
@@ -113,7 +117,7 @@ void Formats::Add(string const & name, string const & extension,
 {
        FormatList::iterator it = 
                find_if(formatlist.begin(), formatlist.end(),
-                       compare_memfun(&Format::name, name));
+                       lyx::compare_memfun(&Format::name, name));
        if (it == formatlist.end())
                formatlist.push_back(Format(name, extension, prettyname,
                                            shortcut, ""));
@@ -128,7 +132,7 @@ void Formats::Delete(string const & name)
 {
        FormatList::iterator it = 
                find_if(formatlist.begin(), formatlist.end(),
-                       compare_memfun(&Format::name, name));
+                       lyx::compare_memfun(&Format::name, name));
        if (it != formatlist.end())
                formatlist.erase(it);
 }
@@ -145,7 +149,7 @@ void Formats::SetViewer(string const & name, string const & command)
        Add(name);
        FormatList::iterator it =
                find_if(formatlist.begin(), formatlist.end(),
-                       compare_memfun(&Format::name, name));
+                       lyx::compare_memfun(&Format::name, name));
        if (it != formatlist.end())
                it->setViewer(command);
 }
@@ -173,24 +177,28 @@ 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 += " " + converters.dvi_papersize(buffer);
+               string paper_size = converters.papersize(buffer);
+               if (paper_size == "letter")
+                       paper_size = "us";
+               command += " " + paper_size;
                if (buffer->params.orientation 
                    == BufferParams::ORIENTATION_LANDSCAPE)
                        command += 'r';
         }
 
-       string command2 = command + " " + OnlyFilename(filename);
-       lyxerr << "Executing command: " << command2 << endl;
-       ShowMessage(buffer, _("Executing command:"), command2);
+       command += " " + QuoteName(OnlyFilename((filename)));
 
-       command += " " + QuoteName(filename);
+       lyxerr << "Executing command: " << command << endl;
+       ShowMessage(buffer, _("Executing command:"), command);
+
+       Path p(OnlyPath(filename));
        Systemcalls one;
-       int res = one.startscript(Systemcalls::SystemDontWait, command);
+       int const res = one.startscript(Systemcalls::SystemDontWait, command);
 
        if (res) {
                WriteAlert(_("Can not view file"),
                           _("Error while executing"),
-                          command2.substr(0, 50));
+                          command.substr(0, 50));
                return false;
        }
        return true;
@@ -248,7 +256,8 @@ void Converter::ReadFlags()
 
 bool operator<(Converter const & a, Converter const & b)
 {
-       int i = compare_no_case(a.From->prettyname(), b.From->prettyname());
+       int const i = compare_no_case(a.From->prettyname(),
+                                     b.From->prettyname());
        if (i == 0)
                return compare_no_case(a.To->prettyname(), b.To->prettyname())
                        < 0;
@@ -266,8 +275,8 @@ public:
                return c.from == from && c.to == to;
        }
 private:
-       string from;
-       string to;
+       string const & from;
+       string const & to;
 };
 
 
@@ -314,7 +323,7 @@ void Converters::Add(string const & from, string const & to,
        converter.ReadFlags();
        
        if (converter.latex && (latex_command.empty() || to == "dvi"))
-               latex_command = command;
+               latex_command = subst(command, token_from, "");
        // If we have both latex & pdflatex, we set latex_command to latex.
        // The latex_command is used to update the .aux file when running
        // a converter that uses it.
@@ -376,7 +385,7 @@ void Converters::Sort()
 
 int Converters::BFS_init(string const & start, bool clear_visited)
 {
-       int s = formats.GetNumber(start);
+       int const s = formats.GetNumber(start);
        if (s < 0)
                return s;
 
@@ -395,12 +404,12 @@ vector<Format const *> const
 Converters::GetReachableTo(string const & target, bool clear_visited)
 {
        vector<Format const *> result;
-       int s = BFS_init(target, clear_visited);
+       int const s = BFS_init(target, clear_visited);
        if (s < 0)
                return result;
 
        while (!Q.empty()) {
-               int i = Q.front();
+               int const i = Q.front();
                Q.pop();
                if (i != s || target != "lyx")
                        result.push_back(&formats.Get(i));
@@ -426,7 +435,7 @@ Converters::GetReachable(string const & from, bool only_viewable,
                return result;
 
        while (!Q.empty()) {
-               int i = Q.front();
+               int const i = Q.front();
                Q.pop();
                Format const & format = formats.Get(i);
                if (format.name() == "lyx")
@@ -451,13 +460,13 @@ bool Converters::IsReachable(string const & from, string const & to)
        if (from == to)
                return true;
 
-       int s = BFS_init(from);
-       int t = formats.GetNumber(to);
+       int const s = BFS_init(from);
+       int const t = formats.GetNumber(to);
        if (s < 0 || t < 0)
                return false;
 
        while (!Q.empty()) {
-               int i = Q.front();
+               int const i = Q.front();
                Q.pop();
                if (i == t)
                        return true;
@@ -480,7 +489,7 @@ Converters::GetPath(string const & from, string const & to)
        if (from == to)
                return path;
 
-       int s = BFS_init(from);
+       int const s = BFS_init(from);
        int t = formats.GetNumber(to);
        if (s < 0 || t < 0)
                return path;
@@ -490,7 +499,7 @@ Converters::GetPath(string const & from, string const & to)
 
        bool found = false;
        while (!Q.empty()) {
-               int i = Q.front();
+               int const i = Q.front();
                Q.pop();
                if (i == t) {
                        found = true;
@@ -499,10 +508,10 @@ Converters::GetPath(string const & from, string const & to)
                for (vector<int>::iterator it = vertices[i].out_vertices.begin();
                     it != vertices[i].out_vertices.end(); ++it)
                        if (!visited[*it]) {
-                               int j = *it;
+                               int const j = *it;
                                visited[j] = true;
                                Q.push(j);
-                               int k = it - vertices[i].out_vertices.begin();
+                               int const k = it - vertices[i].out_vertices.begin();
                                prev_edge[j] = vertices[i].out_edges[k];
                                prev_vertex[j] = i;
                        }
@@ -540,10 +549,7 @@ bool Converters::Convert(Buffer const * buffer,
                                  formats.Extension(to_format));
 
        if (from_format == to_format)
-               if (from_file != to_file)
-                       return lyx::rename(from_file, to_file);
-               else
-                       return true;
+               return Move(from_file, to_file, false);
 
        EdgePath edgepath = GetPath(from_format, to_format);
        if (edgepath.empty()) {
@@ -607,6 +613,9 @@ bool Converters::Convert(Buffer const * buffer,
                        if (conv.from == "dvi" && conv.to == "ps")
                                command = add_options(command,
                                                      dvips_options(buffer));
+                       else if (conv.from == "dvi" && prefixIs(conv.to, "pdf"))
+                               command = add_options(command,
+                                                     dvipdfm_options(buffer));
 
                        lyxerr << "Calling " << command << endl;
                        if (buffer)
@@ -665,20 +674,43 @@ bool Converters::Convert(Buffer const * buffer,
                                return false;
                        }
                }
-       } else if (outfile != to_file) {
-               bool moved = (conv.latex)
-                       ? lyx::copy(outfile, to_file)
-                       : lyx::rename(outfile, to_file);
-               if (!moved) {
-                       WriteAlert(_("Error while trying to move file:"),
-                                  outfile, _("to ") + to_file);
-                       return false;
-               }
-       }
-
-        return true;
+               return true;
+       } else 
+               return Move(outfile, to_file, conv.latex);
 }
 
+// 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)
+{
+       if (from == to)
+               return true;
+
+       bool no_errors = true;
+       string const path = OnlyPath(from);
+       string const base = OnlyFilename(ChangeExtension(from, ""));
+       string const to_base = ChangeExtension(to, "");
+       string const to_extension = GetExtension(to);
+
+       vector<string> files = DirList(OnlyPath(from), GetExtension(from));
+       for (vector<string>::const_iterator it = files.begin();
+            it != files.end(); ++it)
+               if (prefixIs(*it, base)) {
+                       string from2 = path + *it;
+                       string to2 = to_base + (*it).substr(base.length());
+                       to2 = ChangeExtension(to2, to_extension);
+                       lyxerr << "moving " << from2 << " to " << to2 << endl;
+                       bool moved = (copy)
+                               ? lyx::copy(from2, to2)
+                               : lyx::rename(from2, to2);
+                       if (!moved && no_errors) {
+                               WriteAlert(_("Error while trying to move file:"),
+                                          from2, _("to ") + to2);
+                               no_errors = false;
+                       }
+               }
+       return no_errors;
+}
 
 bool Converters::Convert(Buffer const * buffer,
                        string const & from_file, string const & to_file_base,
@@ -690,6 +722,7 @@ bool Converters::Convert(Buffer const * buffer,
 }
 
 
+
 void Converters::BuildGraph()
 {
        vertices = vector<Vertex>(formats.size());
@@ -697,8 +730,8 @@ void Converters::BuildGraph()
 
        for (ConverterList::iterator it = converterlist.begin();
             it != converterlist.end(); ++it) {
-               int s = formats.GetNumber(it->from);
-               int t = formats.GetNumber(it->to);
+               int const s = formats.GetNumber(it->from);
+               int const t = formats.GetNumber(it->to);
                vertices[t].in_vertices.push_back(s);
                vertices[s].out_vertices.push_back(t);
                vertices[s].out_edges.push_back(it - converterlist.begin());
@@ -784,7 +817,7 @@ bool Converters::runLaTeX(Buffer const * buffer, string const & command)
 
        if (bv) {
                ProhibitInput(bv);
-               bv->owner()->getMiniBuffer()->Set(_("Running LaTeX..."));
+               bv->owner()->message(_("Running LaTeX..."));
                // Remove all error insets
                need_redraw = bv->removeAutoInsets();
        }
@@ -794,7 +827,7 @@ bool Converters::runLaTeX(Buffer const * buffer, string const & command)
        TeXErrors terr;
        LaTeX latex(command, name, buffer->filepath);
        int result = latex.run(terr,
-                              bv ? bv->owner()->getMiniBuffer() : 0);
+                              bv ? bv->owner()->getLyXFunc() : 0);
        
 
        if (bv) {
@@ -849,7 +882,7 @@ bool Converters::runLaTeX(Buffer const * buffer, string const & command)
 }
 
 
-string const Converters::dvi_papersize(Buffer const * buffer)
+string const Converters::papersize(Buffer const * buffer)
 {
        char real_papersize = buffer->params.papersize;
        if (real_papersize == BufferParams::PAPER_DEFAULT)
@@ -870,7 +903,7 @@ string const Converters::dvi_papersize(Buffer const * buffer)
                return "legal";
        case BufferParams::PAPER_USLETTER:
        default:
-               return "us";
+               return "letter";
        }
 }
 
@@ -891,9 +924,7 @@ string const Converters::dvips_options(Buffer const * buffer)
                result += ' ' + buffer->params.paperwidth;
                result += ',' + buffer->params.paperheight;
        } else {
-               string paper_option = dvi_papersize(buffer);
-               if (paper_option == "us")
-                       paper_option = "letter";
+               string paper_option = papersize(buffer);
                if (paper_option != "letter" ||
                    buffer->params.orientation != BufferParams::ORIENTATION_LANDSCAPE) {
                        // dvips won't accept -t letter -t landscape.  In all other
@@ -902,11 +933,32 @@ string const Converters::dvips_options(Buffer const * buffer)
                        result += ' ' + paper_option;
                }
        }
-       if (buffer->params.orientation == BufferParams::ORIENTATION_LANDSCAPE)
+       if (buffer->params.orientation == BufferParams::ORIENTATION_LANDSCAPE &&
+           buffer->params.papersize2 != BufferParams::VM_PAPER_CUSTOM)
                result += ' ' + lyxrc.print_landscape_flag;
        return result;
 }
 
+
+string const Converters::dvipdfm_options(Buffer const * buffer)
+{
+       string result;
+       if (!buffer)
+               return result;
+
+       if (buffer->params.papersize2 != BufferParams::VM_PAPER_CUSTOM) {
+               string paper_size = papersize(buffer);
+               if (paper_size != "b5" && paper_size != "foolscap")
+                       result = "-p "+ paper_size;
+
+               if (buffer->params.orientation == BufferParams::ORIENTATION_LANDSCAPE)
+                       result += " -l";
+       }
+
+       return result;
+}
+
+
 vector<Converters::Vertex> Converters::vertices;