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 * ====================================================== */
14 #pragma implementation
19 #include "converter.h"
21 #include "support/syscall.h"
22 #include "support/path.h"
23 #include "support/filetools.h"
25 #include "bufferview_funcs.h"
28 #include "lyx_gui_misc.h"
29 #include "lyx_cb.h" // ShowMessage()
30 #include "support/lyxfunctional.h"
31 #include "frontends/Alert.h"
33 #include "BufferView.h"
46 string const token_from("$$i");
47 string const token_base("$$b");
48 string const token_to("$$o");
50 //////////////////////////////////////////////////////////////////////////////
53 string const add_options(string const & command, string const & options)
56 string const tail = split(command, head, ' ');
57 return head + ' ' + options + ' ' + tail;
62 //////////////////////////////////////////////////////////////////////////////
64 bool Format::dummy() const
66 return extension().empty();
70 bool Format::isChildFormat() const
74 return isdigit(name_[name_.length() - 1]);
78 string const Format::parentFormat() const
80 return name_.substr(0, name_.length() - 1);
83 //////////////////////////////////////////////////////////////////////////////
85 // This method should return a reference, and throw an exception
86 // if the format named name cannot be found (Lgb)
87 Format const * Formats::getFormat(string const & name) const
89 FormatList::const_iterator cit =
90 find_if(formatlist.begin(), formatlist.end(),
91 lyx::compare_memfun(&Format::name, name));
92 if (cit != formatlist.end())
99 int Formats::getNumber(string const & name) const
101 FormatList::const_iterator cit =
102 find_if(formatlist.begin(), formatlist.end(),
103 lyx::compare_memfun(&Format::name, name));
104 if (cit != formatlist.end())
105 return cit - formatlist.begin();
111 void Formats::add(string const & name)
113 if (!getFormat(name))
114 add(name, name, name, string());
118 void Formats::add(string const & name, string const & extension,
119 string const & prettyname, string const & shortcut)
121 FormatList::iterator it =
122 find_if(formatlist.begin(), formatlist.end(),
123 lyx::compare_memfun(&Format::name, name));
124 if (it == formatlist.end())
125 formatlist.push_back(Format(name, extension, prettyname,
128 string viewer = it->viewer();
129 *it = Format(name, extension, prettyname, shortcut, viewer);
134 void Formats::erase(string const & name)
136 FormatList::iterator it =
137 find_if(formatlist.begin(), formatlist.end(),
138 lyx::compare_memfun(&Format::name, name));
139 if (it != formatlist.end())
140 formatlist.erase(it);
146 std::sort(formatlist.begin(), formatlist.end());
150 void Formats::setViewer(string const & name, string const & command)
153 FormatList::iterator it =
154 find_if(formatlist.begin(), formatlist.end(),
155 lyx::compare_memfun(&Format::name, name));
156 if (it != formatlist.end())
157 it->setViewer(command);
161 bool Formats::view(Buffer const * buffer, string const & filename,
162 string const & format_name) const
164 if (filename.empty())
167 Format const * format = getFormat(format_name);
168 if (format && format->viewer().empty() &&
169 format->isChildFormat())
170 format = getFormat(format->parentFormat());
171 if (!format || format->viewer().empty()) {
172 Alert::alert(_("Cannot view file"),
173 _("No information for viewing ")
174 + prettyName(format_name));
178 string command = format->viewer();
180 if (format_name == "dvi" &&
181 !lyxrc.view_dvi_paper_option.empty()) {
182 command += " " + lyxrc.view_dvi_paper_option;
183 string paper_size = converters.papersize(buffer);
184 if (paper_size == "letter")
186 command += " " + paper_size;
187 if (buffer->params.orientation
188 == BufferParams::ORIENTATION_LANDSCAPE)
192 command += " " + QuoteName(OnlyFilename((filename)));
194 lyxerr[Debug::FILES] << "Executing command: " << command << endl;
195 ShowMessage(buffer, _("Executing command:"), command);
197 Path p(OnlyPath(filename));
199 int const res = one.startscript(Systemcalls::SystemDontWait, command);
202 Alert::alert(_("Cannot view file"),
203 _("Error while executing"),
204 command.substr(0, 50));
211 string const Formats::prettyName(string const & name) const
213 Format const * format = getFormat(name);
215 return format->prettyname();
221 string const Formats::extension(string const & name) const
223 Format const * format = getFormat(name);
225 return format->extension();
230 //////////////////////////////////////////////////////////////////////////////
232 void Converter::readFlags()
234 string flag_list(flags);
235 while (!flag_list.empty()) {
236 string flag_name, flag_value;
237 flag_list = split(flag_list, flag_value, ',');
238 flag_value = split(flag_value, flag_name, '=');
239 if (flag_name == "latex")
241 else if (flag_name == "originaldir")
243 else if (flag_name == "needaux")
245 else if (flag_name == "resultdir")
246 result_dir = (flag_value.empty())
247 ? token_base : flag_value;
248 else if (flag_name == "resultfile")
249 result_file = flag_value;
250 else if (flag_name == "parselog")
251 parselog = flag_value;
253 if (!result_dir.empty() && result_file.empty())
254 result_file = "index." + formats.extension(to);
255 //if (!contains(command, token_from))
260 bool operator<(Converter const & a, Converter const & b)
262 int const i = compare_no_case(a.From->prettyname(),
263 b.From->prettyname());
265 return compare_no_case(a.To->prettyname(), b.To->prettyname())
271 //////////////////////////////////////////////////////////////////////////////
273 class compare_Converter {
275 compare_Converter(string const & f, string const & t)
277 bool operator()(Converter const & c) {
278 return c.from == from && c.to == to;
286 Converter const * Converters::getConverter(string const & from,
289 ConverterList::const_iterator cit =
290 find_if(converterlist_.begin(), converterlist_.end(),
291 compare_Converter(from, to));
292 if (cit != converterlist_.end())
299 int Converters::getNumber(string const & from, string const & to)
301 ConverterList::const_iterator cit =
302 find_if(converterlist_.begin(), converterlist_.end(),
303 compare_Converter(from, to));
304 if (cit != converterlist_.end())
305 return cit - converterlist_.begin();
311 void Converters::add(string const & from, string const & to,
312 string const & command, string const & flags)
316 ConverterList::iterator it = find_if(converterlist_.begin(),
317 converterlist_.end(),
318 compare_Converter(from, to));
320 Converter converter(from, to, command, flags);
321 if (it != converterlist_.end() && !flags.empty() && flags[0] == '*') {
323 converter.command = command;
324 converter.flags = flags;
326 converter.readFlags();
328 if (converter.latex && (latex_command_.empty() || to == "dvi"))
329 latex_command_ = subst(command, token_from, "");
330 // If we have both latex & pdflatex, we set latex_command to latex.
331 // The latex_command is used to update the .aux file when running
332 // a converter that uses it.
334 if (it == converterlist_.end()) {
335 converterlist_.push_back(converter);
337 converter.From = it->From;
338 converter.To = it->To;
344 void Converters::erase(string const & from, string const & to)
346 ConverterList::iterator it = find_if(converterlist_.begin(),
347 converterlist_.end(),
348 compare_Converter(from, to));
349 if (it != converterlist_.end())
350 converterlist_.erase(it);
354 // This method updates the pointers From and To in all the converters.
355 // The code is not very efficient, but it doesn't matter as the number
356 // of formats and converters is small.
357 // Furthermore, this method is called only on startup, or after
358 // adding/deleting a format in FormPreferences (the latter calls can be
359 // eliminated if the formats in the Formats class are stored using a map or
360 // a list (instead of a vector), but this will cause other problems).
361 void Converters::update(Formats const & formats)
363 ConverterList::iterator it = converterlist_.begin();
364 ConverterList::iterator end = converterlist_.end();
365 for (; it != end; ++it) {
366 it->From = formats.getFormat(it->from);
367 it->To = formats.getFormat(it->to);
372 // This method updates the pointers From and To in the last converter.
373 // It is called when adding a new converter in FormPreferences
374 void Converters::updateLast(Formats const & formats)
376 if (converterlist_.begin() != converterlist_.end()) {
377 ConverterList::iterator it = converterlist_.end() - 1;
378 it->From = formats.getFormat(it->from);
379 it->To = formats.getFormat(it->to);
384 void Converters::sort()
386 std::sort(converterlist_.begin(), converterlist_.end());
390 int Converters::bfs_init(string const & start, bool clear_visited)
392 int const s = formats.getNumber(start);
398 fill(visited_.begin(), visited_.end(), false);
399 if (visited_[s] == false) {
407 vector<Format const *> const
408 Converters::getReachableTo(string const & target, bool clear_visited)
410 vector<Format const *> result;
411 int const s = bfs_init(target, clear_visited);
415 while (!Q_.empty()) {
416 int const i = Q_.front();
418 if (i != s || target != "lyx") {
419 result.push_back(&formats.get(i));
422 vector<int>::iterator it = vertices_[i].in_vertices.begin();
423 vector<int>::iterator end = vertices_[i].in_vertices.end();
424 for (; it != end; ++it) {
425 if (!visited_[*it]) {
426 visited_[*it] = true;
436 vector<Format const *> const
437 Converters::getReachable(string const & from, bool only_viewable,
440 vector<Format const *> result;
442 if (bfs_init(from, clear_visited) < 0)
445 while (!Q_.empty()) {
446 int const i = Q_.front();
448 Format const & format = formats.get(i);
449 if (format.name() == "lyx")
451 if (!only_viewable || !format.viewer().empty() ||
452 format.isChildFormat())
453 result.push_back(&format);
455 vector<int>::const_iterator cit =
456 vertices_[i].out_vertices.begin();
457 vector<int>::const_iterator end =
458 vertices_[i].out_vertices.end();
459 for (; cit != end; ++cit)
460 if (!visited_[*cit]) {
461 visited_[*cit] = true;
470 bool Converters::isReachable(string const & from, string const & to)
475 int const s = bfs_init(from);
476 int const t = formats.getNumber(to);
480 while (!Q_.empty()) {
481 int const i = Q_.front();
486 vector<int>::const_iterator cit =
487 vertices_[i].out_vertices.begin();
488 vector<int>::const_iterator end =
489 vertices_[i].out_vertices.end();
490 for (; cit != end; ++cit) {
491 if (!visited_[*cit]) {
492 visited_[*cit] = true;
502 Converters::EdgePath const
503 Converters::getPath(string const & from, string const & to)
509 int const s = bfs_init(from);
510 int t = formats.getNumber(to);
514 vector<int> prev_edge(formats.size());
515 vector<int> prev_vertex(formats.size());
518 while (!Q_.empty()) {
519 int const i = Q_.front();
526 vector<int>::const_iterator beg =
527 vertices_[i].out_vertices.begin();
528 vector<int>::const_iterator cit = beg;
529 vector<int>::const_iterator end =
530 vertices_[i].out_vertices.end();
531 for (; cit != end; ++cit)
532 if (!visited_[*cit]) {
536 int const k = cit - beg;
537 prev_edge[j] = vertices_[i].out_edges[k];
545 path.push_back(prev_edge[t]);
548 reverse(path.begin(), path.end());
553 bool Converters::usePdflatex(EdgePath const & path)
555 for (EdgePath::const_iterator cit = path.begin();
556 cit != path.end(); ++cit) {
557 Converter const & conv = converterlist_[*cit];
559 return contains(conv.to, "pdf");
565 bool Converters::convert(Buffer const * buffer,
566 string const & from_file, string const & to_file_base,
567 string const & from_format, string const & to_format,
570 to_file = ChangeExtension(to_file_base,
571 formats.extension(to_format));
573 if (from_format == to_format)
574 return move(from_file, to_file, false);
576 EdgePath edgepath = getPath(from_format, to_format);
577 if (edgepath.empty()) {
578 Alert::alert(_("Cannot convert file"),
579 _("No information for converting from ")
580 + formats.prettyName(from_format) + _(" to ")
581 + formats.prettyName(to_format));
585 string path = OnlyPath(from_file);
588 bool run_latex = false;
589 string from_base = ChangeExtension(from_file, "");
590 string to_base = ChangeExtension(to_file, "");
592 string outfile = from_file;
593 for (EdgePath::const_iterator cit = edgepath.begin();
594 cit != edgepath.end(); ++cit) {
595 Converter const & conv = converterlist_[*cit];
596 bool dummy = conv.To->dummy() && conv.to != "program";
598 lyxerr[Debug::FILES] << "Converting from "
599 << conv.from << " to " << conv.to << endl;
601 outfile = conv.result_dir.empty()
602 ? ChangeExtension(from_file, conv.To->extension())
603 : AddName(subst(conv.result_dir,
604 token_base, from_base),
605 subst(conv.result_file,
606 token_base, OnlyFilename(from_base)));
610 string command = subst(conv.command, token_from, "");
611 lyxerr[Debug::FILES] << "Running " << command << endl;
612 if (!runLaTeX(buffer, command))
615 if (conv.need_aux && !run_latex
616 && !latex_command_.empty()) {
618 << "Running " << latex_command_
619 << " to update aux file"<< endl;
620 runLaTeX(buffer, latex_command_);
623 string infile2 = (conv.original_dir)
624 ? infile : MakeRelPath(infile, path);
625 string outfile2 = (conv.original_dir)
626 ? outfile : MakeRelPath(outfile, path);
628 string command = conv.command;
629 command = subst(command, token_from, QuoteName(infile2));
630 command = subst(command, token_base, QuoteName(from_base));
631 command = subst(command, token_to, QuoteName(outfile2));
633 if (!conv.parselog.empty())
634 command += " 2> " + QuoteName(infile2 + ".out");
636 if (conv.from == "dvi" && conv.to == "ps")
637 command = add_options(command,
638 dvips_options(buffer));
639 else if (conv.from == "dvi" && prefixIs(conv.to, "pdf"))
640 command = add_options(command,
641 dvipdfm_options(buffer));
643 lyxerr[Debug::FILES] << "Calling " << command << endl;
645 ShowMessage(buffer, _("Executing command:"), command);
647 Systemcalls::Starttype type = (dummy)
648 ? Systemcalls::SystemDontWait : Systemcalls::System;
651 if (conv.original_dir && buffer) {
652 Path p(buffer->filepath);
653 res = one.startscript(type, command);
655 res = one.startscript(type, command);
657 if (!conv.parselog.empty()) {
658 string const logfile = infile2 + ".log";
659 string const command2 = conv.parselog +
660 " < " + QuoteName(infile2 + ".out") +
661 " > " + QuoteName(logfile);
662 one.startscript(Systemcalls::System, command2);
663 if (!scanLog(buffer, command, logfile))
668 if (conv.to == "program")
669 Alert::alert(_("There were errors during the Build process."),
670 _("You should try to fix them."));
672 Alert::alert(_("Cannot convert file"),
673 "Error while executing",
674 command.substr(0, 50));
680 Converter const & conv = converterlist_[edgepath.back()];
681 if (conv.To->dummy())
685 if (!conv.result_dir.empty()) {
686 to_file = AddName(subst(conv.result_dir, token_base, to_base),
687 subst(conv.result_file,
688 token_base, OnlyFilename(to_base)));
689 if (from_base != to_base) {
690 string from = subst(conv.result_dir,
691 token_base, from_base);
692 string to = subst(conv.result_dir,
693 token_base, to_base);
694 if (!lyx::rename(from, to)) {
695 Alert::alert(_("Error while trying to move directory:"),
702 return move(outfile, to_file, conv.latex);
705 // If from = /path/file.ext and to = /path2/file2.ext2 then this method
706 // moves each /path/file*.ext file to /path2/file2*.ext2'
707 bool Converters::move(string const & from, string const & to, bool copy)
712 bool no_errors = true;
713 string const path = OnlyPath(from);
714 string const base = OnlyFilename(ChangeExtension(from, ""));
715 string const to_base = ChangeExtension(to, "");
716 string const to_extension = GetExtension(to);
718 vector<string> files = DirList(OnlyPath(from), GetExtension(from));
719 for (vector<string>::const_iterator it = files.begin();
720 it != files.end(); ++it)
721 if (prefixIs(*it, base)) {
722 string from2 = path + *it;
723 string to2 = to_base + it->substr(base.length());
724 to2 = ChangeExtension(to2, to_extension);
725 lyxerr[Debug::FILES] << "moving " << from2
726 << " to " << to2 << endl;
728 ? lyx::copy(from2, to2)
729 : lyx::rename(from2, to2);
730 if (!moved && no_errors) {
731 Alert::alert(_("Error while trying to move file:"),
732 from2, _("to ") + to2);
740 bool Converters::convert(Buffer const * buffer,
741 string const & from_file, string const & to_file_base,
742 string const & from_format, string const & to_format)
745 return convert(buffer, from_file, to_file_base, from_format, to_format,
750 void Converters::buildGraph()
752 vertices_ = vector<Vertex>(formats.size());
753 visited_.resize(formats.size());
755 for (ConverterList::iterator it = converterlist_.begin();
756 it != converterlist_.end(); ++it) {
757 int const s = formats.getNumber(it->from);
758 int const t = formats.getNumber(it->to);
759 vertices_[t].in_vertices.push_back(s);
760 vertices_[s].out_vertices.push_back(t);
761 vertices_[s].out_edges.push_back(it - converterlist_.begin());
766 bool Converters::formatIsUsed(string const & format)
768 ConverterList::const_iterator cit = converterlist_.begin();
769 ConverterList::const_iterator end = converterlist_.end();
770 for (; cit != end; ++cit) {
771 if (cit->from == format || cit->to == format)
778 bool Converters::scanLog(Buffer const * buffer, string const & command,
779 string const & filename)
784 BufferView * bv = buffer->getUser();
785 bool need_redraw = false;
787 bv->owner()->prohibitInput();
788 // Remove all error insets
789 need_redraw = bv->removeAutoInsets();
792 LaTeX latex("", filename, "");
794 int result = latex.scanLogFile(terr);
796 if ((result & LaTeX::ERRORS)) {
797 // Insert all errors as errors boxes
798 bv->insertErrors(terr);
805 bv->owner()->allowInput();
808 if ((result & LaTeX::ERRORS)) {
809 int num_errors = latex.getNumErrors();
812 if (num_errors == 1) {
813 s = _("One error detected");
814 t = _("You should try to fix it.");
816 s = tostr(num_errors);
817 s += _(" errors detected.");
818 t = _("You should try to fix them.");
821 split(command, head, ' ');
822 Alert::alert(_("There were errors during running of ") + head,
825 } else if (result & LaTeX::NO_OUTPUT) {
826 string const s = _("The operation resulted in");
827 string const t = _("an empty file.");
828 Alert::alert(_("Resulting file is empty"), s, t);
835 bool Converters::runLaTeX(Buffer const * buffer, string const & command)
840 BufferView * bv = buffer->getUser();
841 string name = buffer->getLatexName();
842 bool need_redraw = false;
845 bv->owner()->prohibitInput();
846 bv->owner()->message(_("Running LaTeX..."));
847 // Remove all error insets
848 need_redraw = bv->removeAutoInsets();
852 // do the LaTex run(s)
854 LaTeX latex(command, name, buffer->filepath);
855 int result = latex.run(terr,
856 bv ? bv->owner()->getLyXFunc() : 0);
860 if ((result & LaTeX::ERRORS)) {
861 // Insert all errors as errors boxes
862 bv->insertErrors(terr);
866 // if we removed error insets before we ran LaTeX or if we inserted
867 // error insets after we ran LaTeX this must be run:
874 // check return value from latex.run().
875 if ((result & LaTeX::NO_LOGFILE)) {
876 Alert::alert(_("LaTeX did not work!"),
877 _("Missing log file:"), name);
878 } else if ((result & LaTeX::ERRORS)) {
879 int num_errors = latex.getNumErrors();
882 if (num_errors == 1) {
883 s = _("One error detected");
884 t = _("You should try to fix it.");
886 s = tostr(num_errors);
887 s += _(" errors detected.");
888 t = _("You should try to fix them.");
890 Alert::alert(_("There were errors during the LaTeX run."),
892 } else if (result & LaTeX::NO_OUTPUT) {
893 string const s = _("The operation resulted in");
894 string const t = _("an empty file.");
895 Alert::alert(_("Resulting file is empty"), s, t);
899 bv->owner()->allowInput();
901 int const ERROR_MASK =
906 return (result & ERROR_MASK) == 0;
911 string const Converters::papersize(Buffer const * buffer)
913 char real_papersize = buffer->params.papersize;
914 if (real_papersize == BufferParams::PAPER_DEFAULT)
915 real_papersize = lyxrc.default_papersize;
917 switch (real_papersize) {
918 case BufferParams::PAPER_A3PAPER:
920 case BufferParams::PAPER_A4PAPER:
922 case BufferParams::PAPER_A5PAPER:
924 case BufferParams::PAPER_B5PAPER:
926 case BufferParams::PAPER_EXECUTIVEPAPER:
928 case BufferParams::PAPER_LEGALPAPER:
930 case BufferParams::PAPER_USLETTER:
937 string const Converters::dvips_options(Buffer const * buffer)
943 if (buffer->params.use_geometry
944 && buffer->params.papersize2 == BufferParams::VM_PAPER_CUSTOM
945 && !lyxrc.print_paper_dimension_flag.empty()
946 && !buffer->params.paperwidth.empty()
947 && !buffer->params.paperheight.empty()) {
948 // using a custom papersize
949 result = lyxrc.print_paper_dimension_flag;
950 result += ' ' + buffer->params.paperwidth;
951 result += ',' + buffer->params.paperheight;
953 string const paper_option = papersize(buffer);
954 if (paper_option != "letter" ||
955 buffer->params.orientation != BufferParams::ORIENTATION_LANDSCAPE) {
956 // dvips won't accept -t letter -t landscape. In all other
957 // cases, include the paper size explicitly.
958 result = lyxrc.print_paper_flag;
959 result += ' ' + paper_option;
962 if (buffer->params.orientation == BufferParams::ORIENTATION_LANDSCAPE &&
963 buffer->params.papersize2 != BufferParams::VM_PAPER_CUSTOM)
964 result += ' ' + lyxrc.print_landscape_flag;
969 string const Converters::dvipdfm_options(Buffer const * buffer)
975 if (buffer->params.papersize2 != BufferParams::VM_PAPER_CUSTOM) {
976 string const paper_size = papersize(buffer);
977 if (paper_size != "b5" && paper_size != "foolscap")
978 result = "-p "+ paper_size;
980 if (buffer->params.orientation == BufferParams::ORIENTATION_LANDSCAPE)
988 vector<Converters::Vertex> Converters::vertices_;
991 /// The global instance
993 Converters converters;
995 // The global copy after reading lyxrc.defaults
996 Formats system_formats;
997 Converters system_converters;