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 void Formats::add(string const & name, string const & extension,
123 string const & prettyname, string const & shortcut)
125 FormatList::iterator it =
126 find_if(formatlist.begin(), formatlist.end(),
127 lyx::compare_memfun(&Format::name, name));
128 if (it == formatlist.end())
129 formatlist.push_back(Format(name, extension, prettyname,
132 string viewer = it->viewer();
133 *it = Format(name, extension, prettyname, shortcut, viewer);
138 void Formats::erase(string const & name)
140 FormatList::iterator it =
141 find_if(formatlist.begin(), formatlist.end(),
142 lyx::compare_memfun(&Format::name, name));
143 if (it != formatlist.end())
144 formatlist.erase(it);
150 std::sort(formatlist.begin(), formatlist.end());
154 void Formats::setViewer(string const & name, string const & command)
157 FormatList::iterator it =
158 find_if(formatlist.begin(), formatlist.end(),
159 lyx::compare_memfun(&Format::name, name));
160 if (it != formatlist.end())
161 it->setViewer(command);
165 bool Formats::view(Buffer const * buffer, string const & filename,
166 string const & format_name) const
168 if (filename.empty())
171 Format const * format = getFormat(format_name);
172 if (format && format->viewer().empty() &&
173 format->isChildFormat())
174 format = getFormat(format->parentFormat());
175 if (!format || format->viewer().empty()) {
177 Alert::alert(_("Cannot view file"),
178 boost::io::str(boost::format(_("No information for viewing %1$s"))
179 % prettyName(format_name)));
181 Alert::alert(_("Cannot view file"),
182 _("No information for viewing ")
183 + prettyName(format_name));
188 string command = format->viewer();
190 if (format_name == "dvi" &&
191 !lyxrc.view_dvi_paper_option.empty()) {
192 command += ' ' + lyxrc.view_dvi_paper_option;
193 string paper_size = converters.papersize(buffer);
194 if (paper_size == "letter")
196 command += ' ' + paper_size;
197 if (buffer->params.orientation
198 == BufferParams::ORIENTATION_LANDSCAPE)
202 if (!contains(command, token_from))
203 command += ' ' + token_from;
205 command = subst(command, token_from,
206 QuoteName(OnlyFilename(filename)));
207 command = subst(command, token_path, QuoteName(OnlyPath(filename)));
209 lyxerr[Debug::FILES] << "Executing command: " << command << endl;
210 ShowMessage(buffer, _("Executing command:"), command);
212 Path p(OnlyPath(filename));
214 int const res = one.startscript(Systemcall::DontWait, command);
217 Alert::alert(_("Cannot view file"),
218 _("Error while executing"),
219 command.substr(0, 50));
226 string const Formats::prettyName(string const & name) const
228 Format const * format = getFormat(name);
230 return format->prettyname();
236 string const Formats::extension(string const & name) const
238 Format const * format = getFormat(name);
240 return format->extension();
245 //////////////////////////////////////////////////////////////////////////////
247 void Converter::readFlags()
249 string flag_list(flags);
250 while (!flag_list.empty()) {
251 string flag_name, flag_value;
252 flag_list = split(flag_list, flag_value, ',');
253 flag_value = split(flag_value, flag_name, '=');
254 if (flag_name == "latex")
256 else if (flag_name == "originaldir")
258 else if (flag_name == "needaux")
260 else if (flag_name == "resultdir")
261 result_dir = (flag_value.empty())
262 ? token_base : flag_value;
263 else if (flag_name == "resultfile")
264 result_file = flag_value;
265 else if (flag_name == "parselog")
266 parselog = flag_value;
268 if (!result_dir.empty() && result_file.empty())
269 result_file = "index." + formats.extension(to);
270 //if (!contains(command, token_from))
275 bool operator<(Converter const & a, Converter const & b)
277 // use the compare_ascii_no_case instead of compare_no_case,
278 // because in turkish, 'i' is not the lowercase version of 'I',
279 // and thus turkish locale breaks parsing of tags.
280 int const i = compare_ascii_no_case(a.From->prettyname(),
281 b.From->prettyname());
283 return compare_ascii_no_case(a.To->prettyname(), b.To->prettyname())
289 //////////////////////////////////////////////////////////////////////////////
291 class compare_Converter {
293 compare_Converter(string const & f, string const & t)
295 bool operator()(Converter const & c) {
296 return c.from == from && c.to == to;
304 Converter const * Converters::getConverter(string const & from,
307 ConverterList::const_iterator cit =
308 find_if(converterlist_.begin(), converterlist_.end(),
309 compare_Converter(from, to));
310 if (cit != converterlist_.end())
317 int Converters::getNumber(string const & from, string const & to)
319 ConverterList::const_iterator cit =
320 find_if(converterlist_.begin(), converterlist_.end(),
321 compare_Converter(from, to));
322 if (cit != converterlist_.end())
323 return cit - converterlist_.begin();
329 void Converters::add(string const & from, string const & to,
330 string const & command, string const & flags)
334 ConverterList::iterator it = find_if(converterlist_.begin(),
335 converterlist_.end(),
336 compare_Converter(from, to));
338 Converter converter(from, to, command, flags);
339 if (it != converterlist_.end() && !flags.empty() && flags[0] == '*') {
341 converter.command = command;
342 converter.flags = flags;
344 converter.readFlags();
346 if (converter.latex && (latex_command_.empty() || to == "dvi"))
347 latex_command_ = subst(command, token_from, "");
348 // If we have both latex & pdflatex, we set latex_command to latex.
349 // The latex_command is used to update the .aux file when running
350 // a converter that uses it.
352 if (it == converterlist_.end()) {
353 converterlist_.push_back(converter);
355 converter.From = it->From;
356 converter.To = it->To;
362 void Converters::erase(string const & from, string const & to)
364 ConverterList::iterator it = find_if(converterlist_.begin(),
365 converterlist_.end(),
366 compare_Converter(from, to));
367 if (it != converterlist_.end())
368 converterlist_.erase(it);
372 // This method updates the pointers From and To in all the converters.
373 // The code is not very efficient, but it doesn't matter as the number
374 // of formats and converters is small.
375 // Furthermore, this method is called only on startup, or after
376 // adding/deleting a format in FormPreferences (the latter calls can be
377 // eliminated if the formats in the Formats class are stored using a map or
378 // a list (instead of a vector), but this will cause other problems).
379 void Converters::update(Formats const & formats)
381 ConverterList::iterator it = converterlist_.begin();
382 ConverterList::iterator end = converterlist_.end();
383 for (; it != end; ++it) {
384 it->From = formats.getFormat(it->from);
385 it->To = formats.getFormat(it->to);
390 // This method updates the pointers From and To in the last converter.
391 // It is called when adding a new converter in FormPreferences
392 void Converters::updateLast(Formats const & formats)
394 if (converterlist_.begin() != converterlist_.end()) {
395 ConverterList::iterator it = converterlist_.end() - 1;
396 it->From = formats.getFormat(it->from);
397 it->To = formats.getFormat(it->to);
402 void Converters::sort()
404 std::sort(converterlist_.begin(), converterlist_.end());
408 int Converters::bfs_init(string const & start, bool clear_visited)
410 int const s = formats.getNumber(start);
416 fill(visited_.begin(), visited_.end(), false);
417 if (visited_[s] == false) {
425 vector<Format const *> const
426 Converters::getReachableTo(string const & target, bool clear_visited)
428 vector<Format const *> result;
429 int const s = bfs_init(target, clear_visited);
433 while (!Q_.empty()) {
434 int const i = Q_.front();
436 if (i != s || target != "lyx") {
437 result.push_back(&formats.get(i));
440 vector<int>::iterator it = vertices_[i].in_vertices.begin();
441 vector<int>::iterator end = vertices_[i].in_vertices.end();
442 for (; it != end; ++it) {
443 if (!visited_[*it]) {
444 visited_[*it] = true;
454 vector<Format const *> const
455 Converters::getReachable(string const & from, bool only_viewable,
458 vector<Format const *> result;
460 if (bfs_init(from, clear_visited) < 0)
463 while (!Q_.empty()) {
464 int const i = Q_.front();
466 Format const & format = formats.get(i);
467 if (format.name() == "lyx")
469 if (!only_viewable || !format.viewer().empty() ||
470 format.isChildFormat())
471 result.push_back(&format);
473 vector<int>::const_iterator cit =
474 vertices_[i].out_vertices.begin();
475 vector<int>::const_iterator end =
476 vertices_[i].out_vertices.end();
477 for (; cit != end; ++cit)
478 if (!visited_[*cit]) {
479 visited_[*cit] = true;
488 bool Converters::isReachable(string const & from, string const & to)
493 int const s = bfs_init(from);
494 int const t = formats.getNumber(to);
498 while (!Q_.empty()) {
499 int const i = Q_.front();
504 vector<int>::const_iterator cit =
505 vertices_[i].out_vertices.begin();
506 vector<int>::const_iterator end =
507 vertices_[i].out_vertices.end();
508 for (; cit != end; ++cit) {
509 if (!visited_[*cit]) {
510 visited_[*cit] = true;
520 Converters::EdgePath const
521 Converters::getPath(string const & from, string const & to)
527 int const s = bfs_init(from);
528 int t = formats.getNumber(to);
532 vector<int> prev_edge(formats.size());
533 vector<int> prev_vertex(formats.size());
536 while (!Q_.empty()) {
537 int const i = Q_.front();
544 vector<int>::const_iterator beg =
545 vertices_[i].out_vertices.begin();
546 vector<int>::const_iterator cit = beg;
547 vector<int>::const_iterator end =
548 vertices_[i].out_vertices.end();
549 for (; cit != end; ++cit)
550 if (!visited_[*cit]) {
554 int const k = cit - beg;
555 prev_edge[j] = vertices_[i].out_edges[k];
563 path.push_back(prev_edge[t]);
566 reverse(path.begin(), path.end());
571 bool Converters::usePdflatex(EdgePath const & path)
573 for (EdgePath::const_iterator cit = path.begin();
574 cit != path.end(); ++cit) {
575 Converter const & conv = converterlist_[*cit];
577 return contains(conv.to, "pdf");
583 bool Converters::convert(Buffer const * buffer,
584 string const & from_file, string const & to_file_base,
585 string const & from_format, string const & to_format,
588 to_file = ChangeExtension(to_file_base,
589 formats.extension(to_format));
591 if (from_format == to_format)
592 return move(from_file, to_file, false);
594 EdgePath edgepath = getPath(from_format, to_format);
595 if (edgepath.empty()) {
599 string path = OnlyPath(from_file);
602 bool run_latex = false;
603 string from_base = ChangeExtension(from_file, "");
604 string to_base = ChangeExtension(to_file, "");
606 string outfile = from_file;
607 for (EdgePath::const_iterator cit = edgepath.begin();
608 cit != edgepath.end(); ++cit) {
609 Converter const & conv = converterlist_[*cit];
610 bool dummy = conv.To->dummy() && conv.to != "program";
612 lyxerr[Debug::FILES] << "Converting from "
613 << conv.from << " to " << conv.to << endl;
615 outfile = conv.result_dir.empty()
616 ? ChangeExtension(from_file, conv.To->extension())
617 : AddName(subst(conv.result_dir,
618 token_base, from_base),
619 subst(conv.result_file,
620 token_base, OnlyFilename(from_base)));
622 // if input and output files are equal, we use a
623 // temporary file as intermediary (JMarc)
625 if (outfile == infile) {
626 real_outfile = infile;
627 outfile = AddName(buffer->tmppath, "tmpfile.out");
632 string command = subst(conv.command, token_from, "");
633 lyxerr[Debug::FILES] << "Running " << command << endl;
634 if (!runLaTeX(buffer, command))
637 if (conv.need_aux && !run_latex
638 && !latex_command_.empty()) {
640 << "Running " << latex_command_
641 << " to update aux file"<< endl;
642 runLaTeX(buffer, latex_command_);
645 string infile2 = (conv.original_dir)
646 ? infile : MakeRelPath(infile, path);
647 string outfile2 = (conv.original_dir)
648 ? outfile : MakeRelPath(outfile, path);
650 string command = conv.command;
651 command = subst(command, token_from, QuoteName(infile2));
652 command = subst(command, token_base, QuoteName(from_base));
653 command = subst(command, token_to, QuoteName(outfile2));
654 command = LibScriptSearch(command);
656 if (!conv.parselog.empty())
657 command += " 2> " + QuoteName(infile2 + ".out");
659 if (conv.from == "dvi" && conv.to == "ps")
660 command = add_options(command,
661 dvips_options(buffer));
662 else if (conv.from == "dvi" && prefixIs(conv.to, "pdf"))
663 command = add_options(command,
664 dvipdfm_options(buffer));
666 lyxerr[Debug::FILES] << "Calling " << command << endl;
668 ShowMessage(buffer, _("Executing command:"), command);
670 Systemcall::Starttype type = (dummy)
671 ? Systemcall::DontWait : Systemcall::Wait;
674 if (conv.original_dir && buffer) {
675 Path p(buffer->filePath());
676 res = one.startscript(type, command);
678 res = one.startscript(type, command);
680 if (!real_outfile.empty()) {
681 if (!lyx::rename(outfile, real_outfile))
685 << "renaming file " << outfile
686 << " to " << real_outfile
690 if (!conv.parselog.empty()) {
691 string const logfile = infile2 + ".log";
692 string const script = LibScriptSearch(conv.parselog);
693 string const command2 = script +
694 " < " + QuoteName(infile2 + ".out") +
695 " > " + QuoteName(logfile);
696 one.startscript(Systemcall::Wait, command2);
697 if (!scanLog(buffer, command, logfile))
702 if (conv.to == "program")
703 Alert::alert(_("There were errors during the Build process."),
704 _("You should try to fix them."));
706 Alert::alert(_("Cannot convert file"),
707 _("Error while executing"),
708 command.substr(0, 50));
714 Converter const & conv = converterlist_[edgepath.back()];
715 if (conv.To->dummy())
719 if (!conv.result_dir.empty()) {
720 to_file = AddName(subst(conv.result_dir, token_base, to_base),
721 subst(conv.result_file,
722 token_base, OnlyFilename(to_base)));
723 if (from_base != to_base) {
724 string from = subst(conv.result_dir,
725 token_base, from_base);
726 string to = subst(conv.result_dir,
727 token_base, to_base);
728 if (!lyx::rename(from, to)) {
730 Alert::alert(_("Error while trying to move directory:"),
731 from, boost::io::str(boost::format(_("to %1$s")) % to));
733 Alert::alert(_("Error while trying to move directory:"),
734 from, _("to ") + to);
741 return move(outfile, to_file, conv.latex);
745 // If from = /path/file.ext and to = /path2/file2.ext2 then this method
746 // moves each /path/file*.ext file to /path2/file2*.ext2'
747 bool Converters::move(string const & from, string const & to, bool copy)
752 bool no_errors = true;
753 string const path = OnlyPath(from);
754 string const base = OnlyFilename(ChangeExtension(from, ""));
755 string const to_base = ChangeExtension(to, "");
756 string const to_extension = GetExtension(to);
758 vector<string> files = DirList(OnlyPath(from), GetExtension(from));
759 for (vector<string>::const_iterator it = files.begin();
760 it != files.end(); ++it)
761 if (prefixIs(*it, base)) {
762 string const from2 = path + *it;
763 string to2 = to_base + it->substr(base.length());
764 to2 = ChangeExtension(to2, to_extension);
765 lyxerr[Debug::FILES] << "moving " << from2
766 << " to " << to2 << endl;
767 bool const moved = (copy)
768 ? lyx::copy(from2, to2)
769 : lyx::rename(from2, to2);
770 if (!moved && no_errors) {
772 Alert::alert(_("Error while trying to move file:"),
773 from2, boost::io::str(boost::format(_("to %1$s")) % to2));
775 Alert::alert(_("Error while trying to move file:"),
776 from2, _("to ") + to2);
785 bool Converters::convert(Buffer const * buffer,
786 string const & from_file, string const & to_file_base,
787 string const & from_format, string const & to_format)
790 return convert(buffer, from_file, to_file_base, from_format, to_format,
795 void Converters::buildGraph()
797 vertices_ = vector<Vertex>(formats.size());
798 visited_.resize(formats.size());
800 for (ConverterList::iterator it = converterlist_.begin();
801 it != converterlist_.end(); ++it) {
802 int const s = formats.getNumber(it->from);
803 int const t = formats.getNumber(it->to);
804 vertices_[t].in_vertices.push_back(s);
805 vertices_[s].out_vertices.push_back(t);
806 vertices_[s].out_edges.push_back(it - converterlist_.begin());
811 bool Converters::formatIsUsed(string const & format)
813 ConverterList::const_iterator cit = converterlist_.begin();
814 ConverterList::const_iterator end = converterlist_.end();
815 for (; cit != end; ++cit) {
816 if (cit->from == format || cit->to == format)
823 bool Converters::scanLog(Buffer const * buffer, string const & command,
824 string const & filename)
829 BufferView * bv = buffer->getUser();
831 bv->owner()->prohibitInput();
832 // all error insets should have been removed by now
835 LaTeX latex("", filename, "");
837 int result = latex.scanLogFile(terr);
839 if ((result & LaTeX::ERRORS)) {
840 // Insert all errors as errors boxes
841 bv->insertErrors(terr);
842 #warning repaint() or update() or nothing ?
846 bv->owner()->allowInput();
849 if ((result & LaTeX::ERRORS)) {
850 int num_errors = latex.getNumErrors();
853 if (num_errors == 1) {
854 s = _("One error detected");
855 t = _("You should try to fix it.");
857 s = tostr(num_errors);
858 s += _(" errors detected.");
859 t = _("You should try to fix them.");
862 split(command, head, ' ');
864 Alert::alert(boost::io::str(boost::format(_("There were errors during running of %1$s")) % head),
867 Alert::alert(_("There were errors during running of ") + head,
871 } else if (result & LaTeX::NO_OUTPUT) {
872 string const s = _("The operation resulted in");
873 string const t = _("an empty file.");
874 Alert::alert(_("Resulting file is empty"), s, t);
881 bool Converters::runLaTeX(Buffer const * buffer, string const & command)
886 BufferView * bv = buffer->getUser();
889 bv->owner()->prohibitInput();
890 bv->owner()->message(_("Running LaTeX..."));
891 // all the autoinsets have already been removed
894 // do the LaTeX run(s)
895 string name = buffer->getLatexName();
896 LaTeX latex(command, name, buffer->filePath());
898 int result = latex.run(terr,
899 bv ? &bv->owner()->getLyXFunc() : 0);
902 if ((result & LaTeX::ERRORS)) {
903 // Insert all errors as errors boxes
904 bv->insertErrors(terr);
905 #warning repaint() or update() or nothing ?
911 // check return value from latex.run().
912 if ((result & LaTeX::NO_LOGFILE)) {
913 Alert::alert(_("LaTeX did not work!"),
914 _("Missing log file:"), name);
915 } else if ((result & LaTeX::ERRORS)) {
916 int num_errors = latex.getNumErrors();
919 if (num_errors == 1) {
920 s = _("One error detected");
921 t = _("You should try to fix it.");
923 s = tostr(num_errors);
924 s += _(" errors detected.");
925 t = _("You should try to fix them.");
927 Alert::alert(_("There were errors during the LaTeX run."),
929 } else if (result & LaTeX::NO_OUTPUT) {
930 string const s = _("The operation resulted in");
931 string const t = _("an empty file.");
932 Alert::alert(_("Resulting file is empty"), s, t);
936 bv->owner()->allowInput();
938 int const ERROR_MASK =
943 return (result & ERROR_MASK) == 0;
948 string const Converters::papersize(Buffer const * buffer)
950 char real_papersize = buffer->params.papersize;
951 if (real_papersize == BufferParams::PAPER_DEFAULT)
952 real_papersize = lyxrc.default_papersize;
954 switch (real_papersize) {
955 case BufferParams::PAPER_A3PAPER:
957 case BufferParams::PAPER_A4PAPER:
959 case BufferParams::PAPER_A5PAPER:
961 case BufferParams::PAPER_B5PAPER:
963 case BufferParams::PAPER_EXECUTIVEPAPER:
965 case BufferParams::PAPER_LEGALPAPER:
967 case BufferParams::PAPER_USLETTER:
974 string const Converters::dvips_options(Buffer const * buffer)
980 if (buffer->params.use_geometry
981 && buffer->params.papersize2 == BufferParams::VM_PAPER_CUSTOM
982 && !lyxrc.print_paper_dimension_flag.empty()
983 && !buffer->params.paperwidth.empty()
984 && !buffer->params.paperheight.empty()) {
985 // using a custom papersize
986 result = lyxrc.print_paper_dimension_flag;
987 result += ' ' + buffer->params.paperwidth;
988 result += ',' + buffer->params.paperheight;
990 string const paper_option = papersize(buffer);
991 if (paper_option != "letter" ||
992 buffer->params.orientation != BufferParams::ORIENTATION_LANDSCAPE) {
993 // dvips won't accept -t letter -t landscape. In all other
994 // cases, include the paper size explicitly.
995 result = lyxrc.print_paper_flag;
996 result += ' ' + paper_option;
999 if (buffer->params.orientation == BufferParams::ORIENTATION_LANDSCAPE &&
1000 buffer->params.papersize2 != BufferParams::VM_PAPER_CUSTOM)
1001 result += ' ' + lyxrc.print_landscape_flag;
1006 string const Converters::dvipdfm_options(Buffer const * buffer)
1012 if (buffer->params.papersize2 != BufferParams::VM_PAPER_CUSTOM) {
1013 string const paper_size = papersize(buffer);
1014 if (paper_size != "b5" && paper_size != "foolscap")
1015 result = "-p "+ paper_size;
1017 if (buffer->params.orientation == BufferParams::ORIENTATION_LANDSCAPE)
1025 vector<Converters::Vertex> Converters::vertices_;
1028 /// The global instance
1030 Converters converters;
1032 // The global copy after reading lyxrc.defaults
1033 Formats system_formats;
1034 Converters system_converters;