1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2001 The LyX Team.
9 * ====================================================== */
13 #include "converter.h"
16 #include "bufferview_funcs.h"
18 #include "lyx_cb.h" // ShowMessage()
20 #include "BufferView.h"
23 #include "frontends/Alert.h"
24 #include "frontends/LyXView.h"
26 #include "support/filetools.h"
27 #include "support/lyxfunctional.h"
28 #include "support/path.h"
29 #include "support/systemcall.h"
31 #include "BoostFormat.h"
35 #ifndef CXX_GLOBAL_CSTD
49 string const token_from("$$i");
50 string const token_base("$$b");
51 string const token_to("$$o");
52 string const token_path("$$p");
54 //////////////////////////////////////////////////////////////////////////////
57 string const add_options(string const & command, string const & options)
60 string const tail = split(command, head, ' ');
61 return head + ' ' + options + ' ' + tail;
66 //////////////////////////////////////////////////////////////////////////////
68 bool Format::dummy() const
70 return extension().empty();
74 bool Format::isChildFormat() const
78 return isdigit(name_[name_.length() - 1]);
82 string const Format::parentFormat() const
84 return name_.substr(0, name_.length() - 1);
87 //////////////////////////////////////////////////////////////////////////////
89 // This method should return a reference, and throw an exception
90 // if the format named name cannot be found (Lgb)
91 Format const * Formats::getFormat(string const & name) const
93 FormatList::const_iterator cit =
94 find_if(formatlist.begin(), formatlist.end(),
95 lyx::compare_memfun(&Format::name, name));
96 if (cit != formatlist.end())
103 int Formats::getNumber(string const & name) const
105 FormatList::const_iterator cit =
106 find_if(formatlist.begin(), formatlist.end(),
107 lyx::compare_memfun(&Format::name, name));
108 if (cit != formatlist.end())
109 return cit - formatlist.begin();
115 void Formats::add(string const & name)
117 if (!getFormat(name))
118 add(name, name, name, string());
122 // FIXME: horrednously mis-named, especially given the other ::add
124 void Formats::add(string const & name, string const & extension,
125 string const & prettyname, string const & shortcut)
127 FormatList::iterator it =
128 find_if(formatlist.begin(), formatlist.end(),
129 lyx::compare_memfun(&Format::name, name));
130 if (it == formatlist.end())
131 formatlist.push_back(Format(name, extension, prettyname,
134 string viewer = it->viewer();
135 *it = Format(name, extension, prettyname, shortcut, viewer);
140 void Formats::erase(string const & name)
142 FormatList::iterator it =
143 find_if(formatlist.begin(), formatlist.end(),
144 lyx::compare_memfun(&Format::name, name));
145 if (it != formatlist.end())
146 formatlist.erase(it);
152 std::sort(formatlist.begin(), formatlist.end());
156 void Formats::setViewer(string const & name, string const & command)
159 FormatList::iterator it =
160 find_if(formatlist.begin(), formatlist.end(),
161 lyx::compare_memfun(&Format::name, name));
162 if (it != formatlist.end())
163 it->setViewer(command);
167 bool Formats::view(Buffer const * buffer, string const & filename,
168 string const & format_name) const
170 if (filename.empty())
173 Format const * format = getFormat(format_name);
174 if (format && format->viewer().empty() &&
175 format->isChildFormat())
176 format = getFormat(format->parentFormat());
177 if (!format || format->viewer().empty()) {
179 Alert::alert(_("Cannot view file"),
180 boost::io::str(boost::format(_("No information for viewing %1$s"))
181 % prettyName(format_name)));
183 Alert::alert(_("Cannot view file"),
184 _("No information for viewing ")
185 + prettyName(format_name));
190 string command = format->viewer();
192 if (format_name == "dvi" &&
193 !lyxrc.view_dvi_paper_option.empty()) {
194 command += ' ' + lyxrc.view_dvi_paper_option;
195 string paper_size = converters.papersize(buffer);
196 if (paper_size == "letter")
198 command += ' ' + paper_size;
199 if (buffer->params.orientation
200 == BufferParams::ORIENTATION_LANDSCAPE)
204 if (!contains(command, token_from))
205 command += ' ' + token_from;
207 command = subst(command, token_from,
208 QuoteName(OnlyFilename(filename)));
209 command = subst(command, token_path, QuoteName(OnlyPath(filename)));
211 lyxerr[Debug::FILES] << "Executing command: " << command << endl;
212 ShowMessage(buffer, _("Executing command:"), command);
214 Path p(OnlyPath(filename));
216 int const res = one.startscript(Systemcall::DontWait, command);
219 Alert::alert(_("Cannot view file"),
220 _("Error while executing"),
221 command.substr(0, 50));
228 string const Formats::prettyName(string const & name) const
230 Format const * format = getFormat(name);
232 return format->prettyname();
238 string const Formats::extension(string const & name) const
240 Format const * format = getFormat(name);
242 return format->extension();
247 //////////////////////////////////////////////////////////////////////////////
249 void Converter::readFlags()
251 string flag_list(flags);
252 while (!flag_list.empty()) {
253 string flag_name, flag_value;
254 flag_list = split(flag_list, flag_value, ',');
255 flag_value = split(flag_value, flag_name, '=');
256 if (flag_name == "latex")
258 else if (flag_name == "originaldir")
260 else if (flag_name == "needaux")
262 else if (flag_name == "resultdir")
263 result_dir = (flag_value.empty())
264 ? token_base : flag_value;
265 else if (flag_name == "resultfile")
266 result_file = flag_value;
267 else if (flag_name == "parselog")
268 parselog = flag_value;
270 if (!result_dir.empty() && result_file.empty())
271 result_file = "index." + formats.extension(to);
272 //if (!contains(command, token_from))
277 bool operator<(Converter const & a, Converter const & b)
279 // use the compare_ascii_no_case instead of compare_no_case,
280 // because in turkish, 'i' is not the lowercase version of 'I',
281 // and thus turkish locale breaks parsing of tags.
282 int const i = compare_ascii_no_case(a.From->prettyname(),
283 b.From->prettyname());
285 return compare_ascii_no_case(a.To->prettyname(), b.To->prettyname())
291 //////////////////////////////////////////////////////////////////////////////
293 class compare_Converter {
295 compare_Converter(string const & f, string const & t)
297 bool operator()(Converter const & c) {
298 return c.from == from && c.to == to;
306 Converter const * Converters::getConverter(string const & from,
309 ConverterList::const_iterator cit =
310 find_if(converterlist_.begin(), converterlist_.end(),
311 compare_Converter(from, to));
312 if (cit != converterlist_.end())
319 int Converters::getNumber(string const & from, string const & to)
321 ConverterList::const_iterator cit =
322 find_if(converterlist_.begin(), converterlist_.end(),
323 compare_Converter(from, to));
324 if (cit != converterlist_.end())
325 return cit - converterlist_.begin();
331 void Converters::add(string const & from, string const & to,
332 string const & command, string const & flags)
336 ConverterList::iterator it = find_if(converterlist_.begin(),
337 converterlist_.end(),
338 compare_Converter(from, to));
340 Converter converter(from, to, command, flags);
341 if (it != converterlist_.end() && !flags.empty() && flags[0] == '*') {
343 converter.command = command;
344 converter.flags = flags;
346 converter.readFlags();
348 if (converter.latex && (latex_command_.empty() || to == "dvi"))
349 latex_command_ = subst(command, token_from, "");
350 // If we have both latex & pdflatex, we set latex_command to latex.
351 // The latex_command is used to update the .aux file when running
352 // a converter that uses it.
354 if (it == converterlist_.end()) {
355 converterlist_.push_back(converter);
357 converter.From = it->From;
358 converter.To = it->To;
364 void Converters::erase(string const & from, string const & to)
366 ConverterList::iterator it = find_if(converterlist_.begin(),
367 converterlist_.end(),
368 compare_Converter(from, to));
369 if (it != converterlist_.end())
370 converterlist_.erase(it);
374 // This method updates the pointers From and To in all the converters.
375 // The code is not very efficient, but it doesn't matter as the number
376 // of formats and converters is small.
377 // Furthermore, this method is called only on startup, or after
378 // adding/deleting a format in FormPreferences (the latter calls can be
379 // eliminated if the formats in the Formats class are stored using a map or
380 // a list (instead of a vector), but this will cause other problems).
381 void Converters::update(Formats const & formats)
383 ConverterList::iterator it = converterlist_.begin();
384 ConverterList::iterator end = converterlist_.end();
385 for (; it != end; ++it) {
386 it->From = formats.getFormat(it->from);
387 it->To = formats.getFormat(it->to);
392 // This method updates the pointers From and To in the last converter.
393 // It is called when adding a new converter in FormPreferences
394 void Converters::updateLast(Formats const & formats)
396 if (converterlist_.begin() != converterlist_.end()) {
397 ConverterList::iterator it = converterlist_.end() - 1;
398 it->From = formats.getFormat(it->from);
399 it->To = formats.getFormat(it->to);
404 void Converters::sort()
406 std::sort(converterlist_.begin(), converterlist_.end());
410 int Converters::bfs_init(string const & start, bool clear_visited)
412 int const s = formats.getNumber(start);
418 fill(visited_.begin(), visited_.end(), false);
419 if (visited_[s] == false) {
427 vector<Format const *> const
428 Converters::getReachableTo(string const & target, bool clear_visited)
430 vector<Format const *> result;
431 int const s = bfs_init(target, clear_visited);
435 while (!Q_.empty()) {
436 int const i = Q_.front();
438 if (i != s || target != "lyx") {
439 result.push_back(&formats.get(i));
442 vector<int>::iterator it = vertices_[i].in_vertices.begin();
443 vector<int>::iterator end = vertices_[i].in_vertices.end();
444 for (; it != end; ++it) {
445 if (!visited_[*it]) {
446 visited_[*it] = true;
456 vector<Format const *> const
457 Converters::getReachable(string const & from, bool only_viewable,
460 vector<Format const *> result;
462 if (bfs_init(from, clear_visited) < 0)
465 while (!Q_.empty()) {
466 int const i = Q_.front();
468 Format const & format = formats.get(i);
469 if (format.name() == "lyx")
471 if (!only_viewable || !format.viewer().empty() ||
472 format.isChildFormat())
473 result.push_back(&format);
475 vector<int>::const_iterator cit =
476 vertices_[i].out_vertices.begin();
477 vector<int>::const_iterator end =
478 vertices_[i].out_vertices.end();
479 for (; cit != end; ++cit)
480 if (!visited_[*cit]) {
481 visited_[*cit] = true;
490 bool Converters::isReachable(string const & from, string const & to)
495 int const s = bfs_init(from);
496 int const t = formats.getNumber(to);
500 while (!Q_.empty()) {
501 int const i = Q_.front();
506 vector<int>::const_iterator cit =
507 vertices_[i].out_vertices.begin();
508 vector<int>::const_iterator end =
509 vertices_[i].out_vertices.end();
510 for (; cit != end; ++cit) {
511 if (!visited_[*cit]) {
512 visited_[*cit] = true;
522 Converters::EdgePath const
523 Converters::getPath(string const & from, string const & to)
529 int const s = bfs_init(from);
530 int t = formats.getNumber(to);
534 vector<int> prev_edge(formats.size());
535 vector<int> prev_vertex(formats.size());
538 while (!Q_.empty()) {
539 int const i = Q_.front();
546 vector<int>::const_iterator beg =
547 vertices_[i].out_vertices.begin();
548 vector<int>::const_iterator cit = beg;
549 vector<int>::const_iterator end =
550 vertices_[i].out_vertices.end();
551 for (; cit != end; ++cit)
552 if (!visited_[*cit]) {
556 int const k = cit - beg;
557 prev_edge[j] = vertices_[i].out_edges[k];
565 path.push_back(prev_edge[t]);
568 reverse(path.begin(), path.end());
573 bool Converters::usePdflatex(EdgePath const & path)
575 for (EdgePath::const_iterator cit = path.begin();
576 cit != path.end(); ++cit) {
577 Converter const & conv = converterlist_[*cit];
579 return contains(conv.to, "pdf");
585 bool Converters::convert(Buffer const * buffer,
586 string const & from_file, string const & to_file_base,
587 string const & from_format, string const & to_format,
590 to_file = ChangeExtension(to_file_base,
591 formats.extension(to_format));
593 if (from_format == to_format)
594 return move(from_file, to_file, false);
596 EdgePath edgepath = getPath(from_format, to_format);
597 if (edgepath.empty()) {
601 string path = OnlyPath(from_file);
604 bool run_latex = false;
605 string from_base = ChangeExtension(from_file, "");
606 string to_base = ChangeExtension(to_file, "");
608 string outfile = from_file;
609 for (EdgePath::const_iterator cit = edgepath.begin();
610 cit != edgepath.end(); ++cit) {
611 Converter const & conv = converterlist_[*cit];
612 bool dummy = conv.To->dummy() && conv.to != "program";
614 lyxerr[Debug::FILES] << "Converting from "
615 << conv.from << " to " << conv.to << endl;
617 outfile = conv.result_dir.empty()
618 ? ChangeExtension(from_file, conv.To->extension())
619 : AddName(subst(conv.result_dir,
620 token_base, from_base),
621 subst(conv.result_file,
622 token_base, OnlyFilename(from_base)));
624 // if input and output files are equal, we use a
625 // temporary file as intermediary (JMarc)
627 if (outfile == infile) {
628 real_outfile = infile;
629 outfile = AddName(buffer->tmppath, "tmpfile.out");
634 string command = subst(conv.command, token_from, "");
635 lyxerr[Debug::FILES] << "Running " << command << endl;
636 if (!runLaTeX(buffer, command))
639 if (conv.need_aux && !run_latex
640 && !latex_command_.empty()) {
642 << "Running " << latex_command_
643 << " to update aux file"<< endl;
644 runLaTeX(buffer, latex_command_);
647 string infile2 = (conv.original_dir)
648 ? infile : MakeRelPath(infile, path);
649 string outfile2 = (conv.original_dir)
650 ? outfile : MakeRelPath(outfile, path);
652 string command = conv.command;
653 command = subst(command, token_from, QuoteName(infile2));
654 command = subst(command, token_base, QuoteName(from_base));
655 command = subst(command, token_to, QuoteName(outfile2));
656 command = LibScriptSearch(command);
658 if (!conv.parselog.empty())
659 command += " 2> " + QuoteName(infile2 + ".out");
661 if (conv.from == "dvi" && conv.to == "ps")
662 command = add_options(command,
663 dvips_options(buffer));
664 else if (conv.from == "dvi" && prefixIs(conv.to, "pdf"))
665 command = add_options(command,
666 dvipdfm_options(buffer));
668 lyxerr[Debug::FILES] << "Calling " << command << endl;
670 ShowMessage(buffer, _("Executing command:"), command);
672 Systemcall::Starttype type = (dummy)
673 ? Systemcall::DontWait : Systemcall::Wait;
676 if (conv.original_dir && buffer) {
677 Path p(buffer->filePath());
678 res = one.startscript(type, command);
680 res = one.startscript(type, command);
682 if (!real_outfile.empty()) {
683 if (!lyx::rename(outfile, real_outfile))
687 << "renaming file " << outfile
688 << " to " << real_outfile
692 if (!conv.parselog.empty()) {
693 string const logfile = infile2 + ".log";
694 string const script = LibScriptSearch(conv.parselog);
695 string const command2 = script +
696 " < " + QuoteName(infile2 + ".out") +
697 " > " + QuoteName(logfile);
698 one.startscript(Systemcall::Wait, command2);
699 if (!scanLog(buffer, command, logfile))
704 if (conv.to == "program")
705 Alert::alert(_("There were errors during the Build process."),
706 _("You should try to fix them."));
708 Alert::alert(_("Cannot convert file"),
709 _("Error while executing"),
710 command.substr(0, 50));
716 Converter const & conv = converterlist_[edgepath.back()];
717 if (conv.To->dummy())
721 if (!conv.result_dir.empty()) {
722 to_file = AddName(subst(conv.result_dir, token_base, to_base),
723 subst(conv.result_file,
724 token_base, OnlyFilename(to_base)));
725 if (from_base != to_base) {
726 string from = subst(conv.result_dir,
727 token_base, from_base);
728 string to = subst(conv.result_dir,
729 token_base, to_base);
730 if (!lyx::rename(from, to)) {
732 Alert::alert(_("Error while trying to move directory:"),
733 from, boost::io::str(boost::format(_("to %1$s")) % to));
735 Alert::alert(_("Error while trying to move directory:"),
736 from, _("to ") + to);
743 return move(outfile, to_file, conv.latex);
747 // If from = /path/file.ext and to = /path2/file2.ext2 then this method
748 // moves each /path/file*.ext file to /path2/file2*.ext2'
749 bool Converters::move(string const & from, string const & to, bool copy)
754 bool no_errors = true;
755 string const path = OnlyPath(from);
756 string const base = OnlyFilename(ChangeExtension(from, ""));
757 string const to_base = ChangeExtension(to, "");
758 string const to_extension = GetExtension(to);
760 vector<string> files = DirList(OnlyPath(from), GetExtension(from));
761 for (vector<string>::const_iterator it = files.begin();
762 it != files.end(); ++it)
763 if (prefixIs(*it, base)) {
764 string const from2 = path + *it;
765 string to2 = to_base + it->substr(base.length());
766 to2 = ChangeExtension(to2, to_extension);
767 lyxerr[Debug::FILES] << "moving " << from2
768 << " to " << to2 << endl;
769 bool const moved = (copy)
770 ? lyx::copy(from2, to2)
771 : lyx::rename(from2, to2);
772 if (!moved && no_errors) {
774 Alert::alert(_("Error while trying to move file:"),
775 from2, boost::io::str(boost::format(_("to %1$s")) % to2));
777 Alert::alert(_("Error while trying to move file:"),
778 from2, _("to ") + to2);
787 bool Converters::convert(Buffer const * buffer,
788 string const & from_file, string const & to_file_base,
789 string const & from_format, string const & to_format)
792 return convert(buffer, from_file, to_file_base, from_format, to_format,
797 void Converters::buildGraph()
799 vertices_ = vector<Vertex>(formats.size());
800 visited_.resize(formats.size());
802 for (ConverterList::iterator it = converterlist_.begin();
803 it != converterlist_.end(); ++it) {
804 int const s = formats.getNumber(it->from);
805 int const t = formats.getNumber(it->to);
806 vertices_[t].in_vertices.push_back(s);
807 vertices_[s].out_vertices.push_back(t);
808 vertices_[s].out_edges.push_back(it - converterlist_.begin());
813 bool Converters::formatIsUsed(string const & format)
815 ConverterList::const_iterator cit = converterlist_.begin();
816 ConverterList::const_iterator end = converterlist_.end();
817 for (; cit != end; ++cit) {
818 if (cit->from == format || cit->to == format)
825 bool Converters::scanLog(Buffer const * buffer, string const & command,
826 string const & filename)
831 BufferView * bv = buffer->getUser();
833 bv->owner()->busy(true);
834 // all error insets should have been removed by now
837 LaTeX latex("", filename, "");
839 int result = latex.scanLogFile(terr);
841 if ((result & LaTeX::ERRORS)) {
842 // Insert all errors as errors boxes
843 bv->insertErrors(terr);
844 #warning repaint() or update() or nothing ?
848 bv->owner()->busy(false);
851 if ((result & LaTeX::ERRORS)) {
852 int num_errors = latex.getNumErrors();
855 if (num_errors == 1) {
856 s = _("One error detected");
857 t = _("You should try to fix it.");
859 s = tostr(num_errors);
860 s += _(" errors detected.");
861 t = _("You should try to fix them.");
864 split(command, head, ' ');
866 Alert::alert(boost::io::str(boost::format(_("There were errors during running of %1$s")) % head),
869 Alert::alert(_("There were errors during running of ") + head,
873 } else if (result & LaTeX::NO_OUTPUT) {
874 string const s = _("The operation resulted in");
875 string const t = _("an empty file.");
876 Alert::alert(_("Resulting file is empty"), s, t);
883 bool Converters::runLaTeX(Buffer const * buffer, string const & command)
888 BufferView * bv = buffer->getUser();
891 bv->owner()->busy(true);
892 bv->owner()->message(_("Running LaTeX..."));
893 // all the autoinsets have already been removed
896 // do the LaTeX run(s)
897 string name = buffer->getLatexName();
898 LaTeX latex(command, name, buffer->filePath());
900 int result = latex.run(terr,
901 bv ? &bv->owner()->getLyXFunc() : 0);
904 if ((result & LaTeX::ERRORS)) {
905 // Insert all errors as errors boxes
906 bv->insertErrors(terr);
907 #warning repaint() or update() or nothing ?
913 // check return value from latex.run().
914 if ((result & LaTeX::NO_LOGFILE)) {
915 Alert::alert(_("LaTeX did not work!"),
916 _("Missing log file:"), name);
917 } else if ((result & LaTeX::ERRORS)) {
918 int num_errors = latex.getNumErrors();
921 if (num_errors == 1) {
922 s = _("One error detected");
923 t = _("You should try to fix it.");
925 s = tostr(num_errors);
926 s += _(" errors detected.");
927 t = _("You should try to fix them.");
929 Alert::alert(_("There were errors during the LaTeX run."),
931 } else if (result & LaTeX::NO_OUTPUT) {
932 string const s = _("The operation resulted in");
933 string const t = _("an empty file.");
934 Alert::alert(_("Resulting file is empty"), s, t);
938 bv->owner()->busy(false);
940 int const ERROR_MASK =
945 return (result & ERROR_MASK) == 0;
950 string const Converters::papersize(Buffer const * buffer)
952 char real_papersize = buffer->params.papersize;
953 if (real_papersize == BufferParams::PAPER_DEFAULT)
954 real_papersize = lyxrc.default_papersize;
956 switch (real_papersize) {
957 case BufferParams::PAPER_A3PAPER:
959 case BufferParams::PAPER_A4PAPER:
961 case BufferParams::PAPER_A5PAPER:
963 case BufferParams::PAPER_B5PAPER:
965 case BufferParams::PAPER_EXECUTIVEPAPER:
967 case BufferParams::PAPER_LEGALPAPER:
969 case BufferParams::PAPER_USLETTER:
976 string const Converters::dvips_options(Buffer const * buffer)
982 if (buffer->params.use_geometry
983 && buffer->params.papersize2 == BufferParams::VM_PAPER_CUSTOM
984 && !lyxrc.print_paper_dimension_flag.empty()
985 && !buffer->params.paperwidth.empty()
986 && !buffer->params.paperheight.empty()) {
987 // using a custom papersize
988 result = lyxrc.print_paper_dimension_flag;
989 result += ' ' + buffer->params.paperwidth;
990 result += ',' + buffer->params.paperheight;
992 string const paper_option = papersize(buffer);
993 if (paper_option != "letter" ||
994 buffer->params.orientation != BufferParams::ORIENTATION_LANDSCAPE) {
995 // dvips won't accept -t letter -t landscape. In all other
996 // cases, include the paper size explicitly.
997 result = lyxrc.print_paper_flag;
998 result += ' ' + paper_option;
1001 if (buffer->params.orientation == BufferParams::ORIENTATION_LANDSCAPE &&
1002 buffer->params.papersize2 != BufferParams::VM_PAPER_CUSTOM)
1003 result += ' ' + lyxrc.print_landscape_flag;
1008 string const Converters::dvipdfm_options(Buffer const * buffer)
1014 if (buffer->params.papersize2 != BufferParams::VM_PAPER_CUSTOM) {
1015 string const paper_size = papersize(buffer);
1016 if (paper_size != "b5" && paper_size != "foolscap")
1017 result = "-p "+ paper_size;
1019 if (buffer->params.orientation == BufferParams::ORIENTATION_LANDSCAPE)
1027 vector<Converters::Vertex> Converters::vertices_;
1030 /// The global instance
1032 Converters converters;
1034 // The global copy after reading lyxrc.defaults
1035 Formats system_formats;
1036 Converters system_converters;