1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2000 The LyX Team.
9 * ====================================================== */
14 #pragma implementation
19 #include "converter.h"
21 #include "support/syscall.h"
22 #include "support/path.h"
24 #include "bufferview_funcs.h"
27 #include "minibuffer.h"
28 #include "lyx_gui_misc.h"
29 #include "lyx_cb.h" // ShowMessage()
39 //////////////////////////////////////////////////////////////////////////////
41 vector<Command> Converter::commands;
42 string Converter::latex_command;
45 string const add_options(string const & command, string const & options)
48 string tail = split(command, head, ' ');
49 return head + ' ' + options + ' ' + tail;
52 //////////////////////////////////////////////////////////////////////////////
55 bool Format::dummy() const
57 return extension.empty();
61 void Formats::Add(string const & name)
63 if (formats.find(name) == formats.end())
64 formats[name] = Format(name, name, name, "", "");
68 void Formats::Add(string const & name, string const & extension,
69 string const & prettyname, string const & shortcut)
72 if (prettyname.empty()) {
73 FormatList::iterator it = formats.find(name);
74 if (it != formats.end())
79 string old_viewer = formats[name].viewer;
80 formats[name] = Format(name, extension, prettyname, shortcut,
85 void Formats::SetViewer(string const & name, string const & command)
87 string command2 = command;
88 if (!command2.empty() && !contains(command2,"$$FName"))
89 command2 += " $$FName";
92 GetFormat(name)->viewer = command2;
96 bool Formats::View(Buffer const * buffer, string const & filename,
97 string const & format_name)
102 Format const * format = GetFormat(format_name);
103 if (!format || format->viewer.empty()) {
104 WriteAlert(_("Can not view file"),
105 _("No information for viewing ")
106 + PrettyName(format_name));
110 string command = format->viewer;
112 if (format_name == "dvi" &&
113 !lyxrc.view_dvi_paper_option.empty()) {
114 string options = lyxrc.view_dvi_paper_option;
115 options += " " + Converter::dvi_papersize(buffer);
116 if (buffer->params.orientation
117 == BufferParams::ORIENTATION_LANDSCAPE)
119 command = add_options(command, options);
122 string command2 = subst(command, "$$FName", OnlyFilename(filename));
123 lyxerr << "Executing command: " << command2 << endl;
124 ShowMessage(buffer, _("Executing command:"), command2);
126 command = subst(command, "$$FName", QuoteName(filename));
128 int res = one.startscript(Systemcalls::SystemDontWait, command);
131 WriteAlert(_("Can not view file"),
132 _("Error while executing"),
133 command.substr(0, 50));
140 Format * Formats::GetFormat(string const & name)
142 FormatList::iterator it = formats.find(name);
143 if (it != formats.end())
150 string const Formats::PrettyName(string const & name)
152 Format const * format = GetFormat(name);
154 return format->prettyname;
160 string const Formats::Extension(string const & name)
162 Format const * format = GetFormat(name);
164 return format->extension;
171 const Formats::GetAllFormats()
173 vector<Format> result;
174 for (FormatList::iterator it = formats.begin();
175 it != formats.end(); ++it)
176 result.push_back(it->second);
181 //////////////////////////////////////////////////////////////////////////////
183 class compare_Command {
185 compare_Command(Command const & c) : com(c) {}
186 bool operator()(Command const & c) {
187 return c.from == com.from && c.to == com.to;
194 void Converter::Add(string const & from, string const & to,
195 string const & command, string const & flags)
199 Command Com(formats.GetFormat(from), formats.GetFormat(to), command);
200 vector<Command>::iterator it = find_if(commands.begin(),
202 compare_Command(Com));
204 if (command.empty() || command == "none") {
205 if (it != commands.end())
212 string flag_list(flags);
213 while (!flag_list.empty()) {
214 string flag_name, flag_value;
215 flag_list = split(flag_list, flag_value, ',');
216 flag_value = split(flag_value, flag_name, '=');
217 if (flag_name == "*") {
218 if (it != commands.end()) {
220 Com.command = command;
223 else if (flag_name == "importer")
225 else if (flag_name == "latex")
227 else if (flag_name == "originaldir")
228 Com.original_dir = true;
229 else if (flag_name == "needaux")
231 else if (flag_name == "resultdir")
232 Com.result_dir = (flag_value.empty())
233 ? "$$BaseName" : flag_value;
234 else if (flag_name == "resultfile")
235 Com.result_file = flag_value;
236 else if (flag_name == "parselog")
237 Com.parselog = flag_value;
238 else if (flag_name == "disable") {
239 while (!flag_value.empty()) {
241 flag_value = split(flag_value, tmp, '&');
242 Com.disable.push_back(tmp);
246 if (!Com.result_dir.empty() && Com.result_file.empty())
247 Com.result_file = "index." + to;
248 //if (!contains(command, "$$FName"))
251 if (Com.latex && (latex_command.empty() || to == "dvi"))
252 latex_command = command;
253 // If we have both latex & pdflatex, we set latex_command to latex.
254 // The latex_command is used to update the .aux file when running
255 // a converter that uses it.
257 if (it != commands.end()) {
261 commands.push_back(Com);
266 bool enable(vector<Command>::iterator it, string const & from)
268 return find(it->disable.begin(), it->disable.end(), from)
269 == it->disable.end();
273 vector<FormatPair> const
274 Converter::GetReachableTo(string const & target)
276 vector<FormatPair> result;
278 queue< vector<Command>::iterator > Q;
279 for (vector<Command>::iterator it = commands.begin();
280 it != commands.end(); ++it)
281 if (it->to->name == target && it->importer) {
288 vector<Command>::iterator it = Q.front();
290 result.push_back(FormatPair(it->from, 0, ""));
291 for (vector<Command>::iterator it2 = commands.begin();
292 it2 != commands.end(); ++it2)
293 if (!it2->visited && it->from == it2->to &&
304 vector<FormatPair> const
305 Converter::GetReachable(string const & from, bool only_viewable)
307 vector<FormatPair> result;
308 Format const * format = formats.GetFormat(from);
312 if (!only_viewable || !format->viewer.empty())
313 result.push_back(FormatPair(format, 0, ""));
315 queue< vector<Command>::iterator > Q;
316 for (vector<Command>::iterator it = commands.begin();
317 it != commands.end(); ++it)
318 if (it->from->name == from && enable(it, from)
326 vector<Command>::iterator it = Q.front();
328 if (!only_viewable || !it->to->viewer.empty())
329 result.push_back(FormatPair(it->to, it->from,
331 for (vector<Command>::iterator it2 = commands.begin();
332 it2 != commands.end(); ++it2)
333 if (!it2->visited && it->to == it2->from &&
334 enable(it2, from) && !it2->importer) {
344 bool Converter::IsReachable(string const & from, string const & to)
349 queue< vector<Command>::iterator > Q;
350 for (vector<Command>::iterator it = commands.begin();
351 it != commands.end(); ++it)
352 if (it->from->name == from && enable(it, from)) {
359 vector<Command>::iterator it = Q.front();
361 if (it->to->name == to)
363 for (vector<Command>::iterator it2 = commands.begin();
364 it2 != commands.end(); ++it2)
365 if (!it2->visited && it->to == it2->from &&
375 bool Converter::Convert(Buffer const * buffer,
376 string const & from_file, string const & to_file_base,
377 string const & from_format, string const & to_format,
378 string const & using_format, string & to_file)
380 to_file = ChangeExtension(to_file_base,
381 formats.Extension(to_format));
383 if (from_format == to_format)
384 if (from_file != to_file)
385 return lyx::rename(from_file, to_file);
389 queue< vector<Command>::iterator > Q;
390 for (vector<Command>::iterator it = commands.begin();
391 it != commands.end(); ++it)
392 if (it->from->name == from_format && enable(it, from_format)) {
395 it->previous = commands.end();
400 WriteAlert(_("Can not convert file"),
401 ("Unknown format ") + from_format);
406 vector<Command>::iterator it;
409 if (it->to->name == to_format &&
410 (using_format.empty() || using_format == it->from->name)) {
415 for (vector<Command>::iterator it2 = commands.begin();
416 it2 != commands.end(); ++it2)
417 if (!it2->visited && it->to == it2->from &&
418 enable(it2, from_format)) {
426 WriteAlert(_("Can not convert file"),
427 _("No information for converting from ")
428 + formats.PrettyName(from_format) + _(" to ")
429 + formats.PrettyName(to_format));
433 vector< vector<Command>::iterator > S;
434 while (it != commands.end()) {
439 string path = OnlyPath(from_file);
442 bool run_latex = false;
443 string from_base = ChangeExtension(from_file, "");
444 string to_base = ChangeExtension(to_file, "");
446 string outfile = from_file;
447 for (vector< vector<Command>::iterator >::reverse_iterator rit =
448 S.rbegin(); rit != S.rend(); ++rit) {
450 bool dummy = it->to->dummy() && it->to->name != "program";
452 lyxerr << "Converting from "
453 << it->from->name << " to " << it->to->name << endl;
455 outfile = it->result_dir.empty()
456 ? ChangeExtension(from_file, it->to->extension)
457 : AddName(subst(it->result_dir,
458 "$$BaseName", from_base),
459 subst(it->result_file,
460 "$$BaseName", OnlyFilename(from_base)));
463 lyxrc.pdf_mode = it->to->name == "pdf";
464 lyxerr << "Running " << it->command << endl;
466 if (!runLaTeX(buffer, it->command))
469 if (it->need_aux && !run_latex
470 && !latex_command.empty()) {
471 lyxerr << "Running " << latex_command
472 << " to update aux file"<< endl;
473 runLaTeX(buffer, latex_command);
476 string infile2 = (it->original_dir)
477 ? infile : MakeRelPath(infile, path);
478 string outfile2 = (it->original_dir)
479 ? outfile : MakeRelPath(outfile, path);
481 string command = it->command;
482 command = subst(command, "$$FName", QuoteName(infile2));
483 command = subst(command, "$$BaseName", QuoteName(from_base));
484 command = subst(command, "$$OutName", QuoteName(outfile2));
486 if (!it->parselog.empty())
487 command += " 2> " + QuoteName(infile2 + ".out");
489 if (it->from->name == "dvi" && it->to->name == "ps")
490 command = add_options(command,
491 dvips_options(buffer));
493 lyxerr << "Calling " << command << endl;
495 ShowMessage(buffer, _("Executing command:"), command);
497 Systemcalls::Starttype type = (dummy)
498 ? Systemcalls::SystemDontWait : Systemcalls::System;
501 if (it->original_dir && buffer) {
502 Path p(buffer->filepath);
503 res = one.startscript(type, command);
505 res = one.startscript(type, command);
507 if (!it->parselog.empty()) {
508 string const logfile = infile2 + ".log";
509 string const command2 = it->parselog +
510 " < " + QuoteName(infile2 + ".out") +
511 " > " + QuoteName(logfile);
512 one.startscript(Systemcalls::System, command2);
513 if (!scanLog(buffer, command, logfile))
518 if (it->to->name == "program")
519 WriteAlert(_("There were errors during the Build process."),
520 _("You should try to fix them."));
522 WriteAlert(_("Can not convert file"),
523 "Error while executing",
524 command.substr(0, 50));
534 if (!it->result_dir.empty()) {
535 to_file = AddName(subst(it->result_dir,
536 "$$BaseName", to_base),
537 subst(it->result_file,
538 "$$BaseName", OnlyFilename(to_base)));
539 if (from_base != to_base) {
540 string from = subst(it->result_dir,
541 "$$BaseName", from_base);
542 string to = subst(it->result_dir,
543 "$$BaseName", to_base);
544 if (!lyx::rename(from, to)) {
545 WriteAlert(_("Error while trying to move directory:"),
550 } else if (outfile != to_file) {
551 bool moved = (it->latex)
552 ? lyx::copy(outfile, to_file)
553 : lyx::rename(outfile, to_file);
555 WriteAlert(_("Error while trying to move file:"),
556 outfile, _("to ") + to_file);
565 bool Converter::Convert(Buffer const * buffer,
566 string const & from_file, string const & to_file_base,
567 string const & from_format, string const & to_format,
568 string const & using_format)
571 return Convert(buffer, from_file, to_file_base, from_format, to_format,
572 using_format, to_file);
576 string const Converter::SplitFormat(string const & str, string & format)
578 string using_format = split(str, format, ':');
585 bool Converter::scanLog(Buffer const * buffer, string const & command,
586 string const & filename)
591 BufferView * bv = buffer->getUser();
592 bool need_redraw = false;
595 // Remove all error insets
596 need_redraw = bv->removeAutoInsets();
599 LaTeX latex("", filename, "");
601 int result = latex.scanLogFile(terr);
603 if ((result & LaTeX::ERRORS)) {
604 // Insert all errors as errors boxes
605 bv->insertErrors(terr);
610 bv->fitCursor(bv->text);
615 if ((result & LaTeX::ERRORS)) {
616 int num_errors = latex.getNumErrors();
619 if (num_errors == 1) {
620 s = _("One error detected");
621 t = _("You should try to fix it.");
623 s = tostr(num_errors);
624 s += _(" errors detected.");
625 t = _("You should try to fix them.");
628 split(command, head, ' ');
629 WriteAlert(_("There were errors during running of ") + head,
632 } else if (result & LaTeX::NO_OUTPUT) {
633 string const s = _("The operation resulted in");
634 string const t = _("an empty file.");
635 WriteAlert(_("Resulting file is empty"), s, t);
642 bool Converter::runLaTeX(Buffer const * buffer, string const & command)
647 BufferView * bv = buffer->getUser();
648 string name = buffer->getLatexName();
649 bool need_redraw = false;
653 bv->owner()->getMiniBuffer()->Set(_("Running LaTeX..."));
654 // Remove all error insets
655 need_redraw = bv->removeAutoInsets();
659 // do the LaTex run(s)
661 LaTeX latex(command, name, buffer->filepath);
662 int result = latex.run(terr,
663 bv ? bv->owner()->getMiniBuffer() : 0);
667 if ((result & LaTeX::ERRORS)) {
668 // Insert all errors as errors boxes
669 bv->insertErrors(terr);
673 // if we removed error insets before we ran LaTeX or if we inserted
674 // error insets after we ran LaTeX this must be run:
677 bv->fitCursor(bv->text);
681 // check return value from latex.run().
682 if ((result & LaTeX::NO_LOGFILE)) {
683 WriteAlert(_("LaTeX did not work!"),
684 _("Missing log file:"), name);
685 } else if ((result & LaTeX::ERRORS)) {
686 int num_errors = latex.getNumErrors();
689 if (num_errors == 1) {
690 s = _("One error detected");
691 t = _("You should try to fix it.");
693 s = tostr(num_errors);
694 s += _(" errors detected.");
695 t = _("You should try to fix them.");
697 WriteAlert(_("There were errors during the LaTeX run."),
699 } else if (result & LaTeX::NO_OUTPUT) {
700 string const s = _("The operation resulted in");
701 string const t = _("an empty file.");
702 WriteAlert(_("Resulting file is empty"), s, t);
708 int const ERROR_MASK =
713 return (result & ERROR_MASK) == 0;
718 string const Converter::dvi_papersize(Buffer const * buffer)
720 char real_papersize = buffer->params.papersize;
721 if (real_papersize == BufferParams::PAPER_DEFAULT)
722 real_papersize = lyxrc.default_papersize;
724 switch (real_papersize) {
725 case BufferParams::PAPER_A3PAPER:
727 case BufferParams::PAPER_A4PAPER:
729 case BufferParams::PAPER_A5PAPER:
731 case BufferParams::PAPER_B5PAPER:
733 case BufferParams::PAPER_EXECUTIVEPAPER:
735 case BufferParams::PAPER_LEGALPAPER:
737 case BufferParams::PAPER_USLETTER:
744 string const Converter::dvips_options(Buffer const * buffer)
750 if (buffer->params.use_geometry
751 && buffer->params.papersize2 == BufferParams::VM_PAPER_CUSTOM
752 && !lyxrc.print_paper_dimension_flag.empty()
753 && !buffer->params.paperwidth.empty()
754 && !buffer->params.paperheight.empty()) {
755 // using a custom papersize
756 result = lyxrc.print_paper_dimension_flag;
757 result += ' ' + buffer->params.paperwidth;
758 result += ',' + buffer->params.paperheight;
760 string paper_option = dvi_papersize(buffer);
761 if (paper_option == "us")
762 paper_option = "letter";
763 if (paper_option != "letter" ||
764 buffer->params.orientation != BufferParams::ORIENTATION_LANDSCAPE) {
765 // dvips won't accept -t letter -t landscape. In all other
766 // cases, include the paper size explicitly.
767 result = lyxrc.print_paper_flag;
768 result += ' ' + paper_option;
771 if (buffer->params.orientation == BufferParams::ORIENTATION_LANDSCAPE)
772 result += ' ' + lyxrc.print_landscape_flag;
777 void Converter::init()
781 /// The global instance
784 // The global copy of the system lyxrc entries (everything except preferences)
785 Formats system_formats;