]> git.lyx.org Git - lyx.git/blob - src/converter.C
8a1fa744dfaeac523c6564167b3a9ff37258715e
[lyx.git] / src / converter.C
1 /* This file is part of
2  * ======================================================
3  *
4  *           LyX, The Document Processor
5  *
6  *           Copyright 1995 Matthias Ettrich
7  *           Copyright 1995-2001 The LyX Team.
8  *
9  * ====================================================== */
10
11 #include <config.h>
12
13 #include "converter.h"
14 #include "lyxrc.h"
15 #include "buffer.h"
16 #include "bufferview_funcs.h"
17 #include "LaTeX.h"
18 #include "lyx_cb.h" // ShowMessage()
19 #include "gettext.h"
20 #include "BufferView.h"
21 #include "debug.h"
22
23 #include "frontends/Alert.h"
24 #include "frontends/LyXView.h"
25
26 #include "support/filetools.h"
27 #include "support/lyxfunctional.h"
28 #include "support/path.h"
29 #include "support/systemcall.h"
30
31 #include "BoostFormat.h"
32
33 #include <cctype>
34
35 #ifndef CXX_GLOBAL_CSTD
36 using std::isdigit;
37 #endif
38
39 using std::vector;
40 using std::queue;
41 using std::endl;
42 using std::fill;
43 using std::find_if;
44 using std::reverse;
45 using std::sort;
46
47 namespace {
48
49 string const token_from("$$i");
50 string const token_base("$$b");
51 string const token_to("$$o");
52 string const token_path("$$p");
53
54 //////////////////////////////////////////////////////////////////////////////
55
56 inline
57 string const add_options(string const & command, string const & options)
58 {
59         string head;
60         string const tail = split(command, head, ' ');
61         return head + ' ' + options + ' ' + tail;
62 }
63
64 } // namespace anon
65
66 //////////////////////////////////////////////////////////////////////////////
67
68 bool Format::dummy() const
69 {
70         return extension().empty();
71 }
72
73
74 bool Format::isChildFormat() const
75 {
76         if (name_.empty())
77                 return false;
78         return isdigit(name_[name_.length() - 1]);
79 }
80
81
82 string const Format::parentFormat() const
83 {
84         return name_.substr(0, name_.length() - 1);
85 }
86
87 //////////////////////////////////////////////////////////////////////////////
88
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
92 {
93         FormatList::const_iterator cit =
94                 find_if(formatlist.begin(), formatlist.end(),
95                         lyx::compare_memfun(&Format::name, name));
96         if (cit != formatlist.end())
97                 return &(*cit);
98         else
99                 return 0;
100 }
101
102
103 int Formats::getNumber(string const & name) const
104 {
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();
110         else
111                 return -1;
112 }
113
114
115 void Formats::add(string const & name)
116 {
117         if (!getFormat(name))
118                 add(name, name, name, string());
119 }
120
121
122 void Formats::add(string const & name, string const & extension,
123                   string const & prettyname, string const & shortcut)
124 {
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,
130                                             shortcut, ""));
131         else {
132                 string viewer = it->viewer();
133                 *it = Format(name, extension, prettyname, shortcut, viewer);
134         }
135 }
136
137
138 void Formats::erase(string const & name)
139 {
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);
145 }
146
147
148 void Formats::sort()
149 {
150         std::sort(formatlist.begin(), formatlist.end());
151 }
152
153
154 void Formats::setViewer(string const & name, string const & command)
155 {
156         add(name);
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);
162 }
163
164
165 bool Formats::view(Buffer const * buffer, string const & filename,
166                    string const & format_name) const
167 {
168         if (filename.empty())
169                 return false;
170
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()) {
176 #if USE_BOOST_FORMAT
177                 Alert::alert(_("Cannot view file"),
178                              boost::io::str(boost::format(_("No information for viewing %1$s"))
179                            % prettyName(format_name)));
180 #else
181                 Alert::alert(_("Cannot view file"),
182                              _("No information for viewing ")
183                              + prettyName(format_name));
184 #endif
185                            return false;
186         }
187
188         string command = format->viewer();
189
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")
195                         paper_size = "us";
196                 command += ' ' + paper_size;
197                 if (buffer->params.orientation
198                     == BufferParams::ORIENTATION_LANDSCAPE)
199                         command += 'r';
200         }
201
202         if (!contains(command, token_from))
203                 command += ' ' + token_from;
204
205         command = subst(command, token_from,
206                         QuoteName(OnlyFilename(filename)));
207         command = subst(command, token_path, QuoteName(OnlyPath(filename)));
208
209         lyxerr[Debug::FILES] << "Executing command: " << command << endl;
210         ShowMessage(buffer, _("Executing command:"), command);
211
212         Path p(OnlyPath(filename));
213         Systemcall one;
214         int const res = one.startscript(Systemcall::DontWait, command);
215
216         if (res) {
217                 Alert::alert(_("Cannot view file"),
218                            _("Error while executing"),
219                            command.substr(0, 50));
220                 return false;
221         }
222         return true;
223 }
224
225
226 string const Formats::prettyName(string const & name) const
227 {
228         Format const * format = getFormat(name);
229         if (format)
230                 return format->prettyname();
231         else
232                 return name;
233 }
234
235
236 string const Formats::extension(string const & name) const
237 {
238         Format const * format = getFormat(name);
239         if (format)
240                 return format->extension();
241         else
242                 return name;
243 }
244
245 //////////////////////////////////////////////////////////////////////////////
246
247 void Converter::readFlags()
248 {
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")
255                         latex = true;
256                 else if (flag_name == "originaldir")
257                         original_dir = true;
258                 else if (flag_name == "needaux")
259                         need_aux = true;
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;
267         }
268         if (!result_dir.empty() && result_file.empty())
269                 result_file = "index." + formats.extension(to);
270         //if (!contains(command, token_from))
271         //      latex = true;
272 }
273
274
275 bool operator<(Converter const & a, Converter const & b)
276 {
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());
282         if (i == 0)
283                 return compare_ascii_no_case(a.To->prettyname(), b.To->prettyname())
284                         < 0;
285         else
286                 return i < 0;
287 }
288
289 //////////////////////////////////////////////////////////////////////////////
290
291 class compare_Converter {
292 public:
293         compare_Converter(string const & f, string const & t)
294                 : from(f), to(t) {}
295         bool operator()(Converter const & c) {
296                 return c.from == from && c.to == to;
297         }
298 private:
299         string const & from;
300         string const & to;
301 };
302
303
304 Converter const * Converters::getConverter(string const & from,
305                                             string const & to)
306 {
307         ConverterList::const_iterator cit =
308                 find_if(converterlist_.begin(), converterlist_.end(),
309                         compare_Converter(from, to));
310         if (cit != converterlist_.end())
311                 return &(*cit);
312         else
313                 return 0;
314 }
315
316
317 int Converters::getNumber(string const & from, string const & to)
318 {
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();
324         else
325                 return -1;
326 }
327
328
329 void Converters::add(string const & from, string const & to,
330                      string const & command, string const & flags)
331 {
332         formats.add(from);
333         formats.add(to);
334         ConverterList::iterator it = find_if(converterlist_.begin(),
335                                              converterlist_.end(),
336                                              compare_Converter(from, to));
337
338         Converter converter(from, to, command, flags);
339         if (it != converterlist_.end() && !flags.empty() && flags[0] == '*') {
340                 converter = *it;
341                 converter.command = command;
342                 converter.flags = flags;
343         }
344         converter.readFlags();
345
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.
351
352         if (it == converterlist_.end()) {
353                 converterlist_.push_back(converter);
354         } else {
355                 converter.From = it->From;
356                 converter.To = it->To;
357                 *it = converter;
358         }
359 }
360
361
362 void Converters::erase(string const & from, string const & to)
363 {
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);
369 }
370
371
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)
380 {
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);
386         }
387 }
388
389
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)
393 {
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);
398         }
399 }
400
401
402 void Converters::sort()
403 {
404         std::sort(converterlist_.begin(), converterlist_.end());
405 }
406
407
408 int Converters::bfs_init(string const & start, bool clear_visited)
409 {
410         int const s = formats.getNumber(start);
411         if (s < 0)
412                 return s;
413
414         Q_ = queue<int>();
415         if (clear_visited)
416                 fill(visited_.begin(), visited_.end(), false);
417         if (visited_[s] == false) {
418                 Q_.push(s);
419                 visited_[s] = true;
420         }
421         return s;
422 }
423
424
425 vector<Format const *> const
426 Converters::getReachableTo(string const & target, bool clear_visited)
427 {
428         vector<Format const *> result;
429         int const s = bfs_init(target, clear_visited);
430         if (s < 0)
431                 return result;
432
433         while (!Q_.empty()) {
434                 int const i = Q_.front();
435                 Q_.pop();
436                 if (i != s || target != "lyx") {
437                         result.push_back(&formats.get(i));
438                 }
439
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;
445                                 Q_.push(*it);
446                         }
447                 }
448         }
449
450         return result;
451 }
452
453
454 vector<Format const *> const
455 Converters::getReachable(string const & from, bool only_viewable,
456                          bool clear_visited)
457 {
458         vector<Format const *> result;
459
460         if (bfs_init(from, clear_visited) < 0)
461                 return result;
462
463         while (!Q_.empty()) {
464                 int const i = Q_.front();
465                 Q_.pop();
466                 Format const & format = formats.get(i);
467                 if (format.name() == "lyx")
468                         continue;
469                 if (!only_viewable || !format.viewer().empty() ||
470                     format.isChildFormat())
471                         result.push_back(&format);
472
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;
480                                 Q_.push(*cit);
481                         }
482         }
483
484         return result;
485 }
486
487
488 bool Converters::isReachable(string const & from, string const & to)
489 {
490         if (from == to)
491                 return true;
492
493         int const s = bfs_init(from);
494         int const t = formats.getNumber(to);
495         if (s < 0 || t < 0)
496                 return false;
497
498         while (!Q_.empty()) {
499                 int const i = Q_.front();
500                 Q_.pop();
501                 if (i == t)
502                         return true;
503
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;
511                                 Q_.push(*cit);
512                         }
513                 }
514         }
515
516         return false;
517 }
518
519
520 Converters::EdgePath const
521 Converters::getPath(string const & from, string const & to)
522 {
523         EdgePath path;
524         if (from == to)
525                 return path;
526
527         int const s = bfs_init(from);
528         int t = formats.getNumber(to);
529         if (s < 0 || t < 0)
530                 return path;
531
532         vector<int> prev_edge(formats.size());
533         vector<int> prev_vertex(formats.size());
534
535         bool found = false;
536         while (!Q_.empty()) {
537                 int const i = Q_.front();
538                 Q_.pop();
539                 if (i == t) {
540                         found = true;
541                         break;
542                 }
543
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]) {
551                                 int const j = *cit;
552                                 visited_[j] = true;
553                                 Q_.push(j);
554                                 int const k = cit - beg;
555                                 prev_edge[j] = vertices_[i].out_edges[k];
556                                 prev_vertex[j] = i;
557                         }
558         }
559         if (!found)
560                 return path;
561
562         while (t != s) {
563                 path.push_back(prev_edge[t]);
564                 t = prev_vertex[t];
565         }
566         reverse(path.begin(), path.end());
567         return path;
568 }
569
570
571 bool Converters::usePdflatex(EdgePath const & path)
572 {
573         for (EdgePath::const_iterator cit = path.begin();
574              cit != path.end(); ++cit) {
575                 Converter const & conv = converterlist_[*cit];
576                 if (conv.latex)
577                         return contains(conv.to, "pdf");
578         }
579         return false;
580 }
581
582
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,
586                          string & to_file)
587 {
588         to_file = ChangeExtension(to_file_base,
589                                   formats.extension(to_format));
590
591         if (from_format == to_format)
592                 return move(from_file, to_file, false);
593
594         EdgePath edgepath = getPath(from_format, to_format);
595         if (edgepath.empty()) {
596                 return false;
597         }
598
599         string path = OnlyPath(from_file);
600         Path p(path);
601
602         bool run_latex = false;
603         string from_base = ChangeExtension(from_file, "");
604         string to_base = ChangeExtension(to_file, "");
605         string infile;
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";
611                 if (!dummy)
612                         lyxerr[Debug::FILES] << "Converting from  "
613                                << conv.from << " to " << conv.to << endl;
614                 infile = outfile;
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)));
621
622                 // if input and output files are equal, we use a
623                 // temporary file as intermediary (JMarc)
624                 string real_outfile;
625                 if (outfile == infile) {
626                         real_outfile = infile;
627                         outfile = AddName(buffer->tmppath, "tmpfile.out");
628                 }
629
630                 if (conv.latex) {
631                         run_latex = true;
632                         string command = subst(conv.command, token_from, "");
633                         lyxerr[Debug::FILES] << "Running " << command << endl;
634                         if (!runLaTeX(buffer, command))
635                                 return false;
636                 } else {
637                         if (conv.need_aux && !run_latex
638                             && !latex_command_.empty()) {
639                                 lyxerr[Debug::FILES]
640                                         << "Running " << latex_command_
641                                         << " to update aux file"<<  endl;
642                                 runLaTeX(buffer, latex_command_);
643                         }
644
645                         string infile2 = (conv.original_dir)
646                                 ? infile : MakeRelPath(infile, path);
647                         string outfile2 = (conv.original_dir)
648                                 ? outfile : MakeRelPath(outfile, path);
649
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);
655
656                         if (!conv.parselog.empty())
657                                 command += " 2> " + QuoteName(infile2 + ".out");
658
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));
665
666                         lyxerr[Debug::FILES] << "Calling " << command << endl;
667                         if (buffer)
668                                 ShowMessage(buffer, _("Executing command:"), command);
669
670                         Systemcall::Starttype type = (dummy)
671                                 ? Systemcall::DontWait : Systemcall::Wait;
672                         Systemcall one;
673                         int res;
674                         if (conv.original_dir && buffer) {
675                                 Path p(buffer->filePath());
676                                 res = one.startscript(type, command);
677                         } else
678                                 res = one.startscript(type, command);
679
680                         if (!real_outfile.empty()) {
681                                 if (!lyx::rename(outfile, real_outfile))
682                                         res = -1;
683                                 else
684                                         lyxerr[Debug::FILES]
685                                                 << "renaming file " << outfile
686                                                 << " to " << real_outfile
687                                                 << endl;
688                         }
689
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))
698                                         return false;
699                         }
700
701                         if (res) {
702                                 if (conv.to == "program")
703                                         Alert::alert(_("There were errors during the Build process."),
704                                                    _("You should try to fix them."));
705                                 else
706                                         Alert::alert(_("Cannot convert file"),
707                                                    _("Error while executing"),
708                                                    command.substr(0, 50));
709                                 return false;
710                         }
711                 }
712         }
713
714         Converter const & conv = converterlist_[edgepath.back()];
715         if (conv.To->dummy())
716                 return true;
717
718
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)) {
729 #if USE_BOOST_FORMAT
730                                 Alert::alert(_("Error while trying to move directory:"),
731                                            from, boost::io::str(boost::format(_("to %1$s")) % to));
732 #else
733                                 Alert::alert(_("Error while trying to move directory:"),
734                                            from, _("to ") + to);
735 #endif
736                                 return false;
737                         }
738                 }
739                 return true;
740         } else
741                 return move(outfile, to_file, conv.latex);
742 }
743
744
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)
748 {
749         if (from == to)
750                 return true;
751
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);
757
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) {
771 #if USE_BOOST_FORMAT
772                                 Alert::alert(_("Error while trying to move file:"),
773                                            from2, boost::io::str(boost::format(_("to %1$s")) % to2));
774 #else
775                                 Alert::alert(_("Error while trying to move file:"),
776                                            from2, _("to ") + to2);
777 #endif
778                                 no_errors = false;
779                         }
780                 }
781         return no_errors;
782 }
783
784
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)
788 {
789         string to_file;
790         return convert(buffer, from_file, to_file_base, from_format, to_format,
791                        to_file);
792 }
793
794
795 void Converters::buildGraph()
796 {
797         vertices_ = vector<Vertex>(formats.size());
798         visited_.resize(formats.size());
799
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());
807         }
808 }
809
810
811 bool Converters::formatIsUsed(string const & format)
812 {
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)
817                         return true;
818         }
819         return false;
820 }
821
822
823 bool Converters::scanLog(Buffer const * buffer, string const & command,
824                         string const & filename)
825 {
826         if (!buffer)
827                 return false;
828
829         BufferView * bv = buffer->getUser();
830         if (bv) {
831                 bv->owner()->prohibitInput();
832                 // all error insets should have been removed by now
833         }
834
835         LaTeX latex("", filename, "");
836         TeXErrors terr;
837         int result = latex.scanLogFile(terr);
838         if (bv) {
839                 if ((result & LaTeX::ERRORS)) {
840                         // Insert all errors as errors boxes
841                         bv->insertErrors(terr);
842 #warning repaint() or update() or nothing ?
843                         bv->repaint();
844                         bv->fitCursor();
845                 }
846                 bv->owner()->allowInput();
847         }
848
849         if ((result & LaTeX::ERRORS)) {
850                 int num_errors = latex.getNumErrors();
851                 string s;
852                 string t;
853                 if (num_errors == 1) {
854                         s = _("One error detected");
855                         t = _("You should try to fix it.");
856                 } else {
857                         s = tostr(num_errors);
858                         s += _(" errors detected.");
859                         t = _("You should try to fix them.");
860                 }
861                 string head;
862                 split(command, head, ' ');
863 #if USE_BOOST_FORMAT
864                 Alert::alert(boost::io::str(boost::format(_("There were errors during running of %1$s")) % head),
865                            s, t);
866 #else
867                 Alert::alert(_("There were errors during running of ") + head,
868                            s, t);
869 #endif
870                 return false;
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);
875                 return false;
876         }
877         return true;
878 }
879
880
881 bool Converters::runLaTeX(Buffer const * buffer, string const & command)
882 {
883         if (!buffer)
884                 return false;
885
886         BufferView * bv = buffer->getUser();
887
888         if (bv) {
889                 bv->owner()->prohibitInput();
890                 bv->owner()->message(_("Running LaTeX..."));
891                 // all the autoinsets have already been removed
892         }
893
894         // do the LaTeX run(s)
895         string name = buffer->getLatexName();
896         LaTeX latex(command, name, buffer->filePath());
897         TeXErrors terr;
898         int result = latex.run(terr,
899                                bv ? &bv->owner()->getLyXFunc() : 0);
900
901         if (bv) {
902                 if ((result & LaTeX::ERRORS)) {
903                         // Insert all errors as errors boxes
904                         bv->insertErrors(terr);
905 #warning repaint() or update() or nothing ?
906                         bv->repaint();
907                         bv->fitCursor();
908                 }
909         }
910
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();
917                 string s;
918                 string t;
919                 if (num_errors == 1) {
920                         s = _("One error detected");
921                         t = _("You should try to fix it.");
922                 } else {
923                         s = tostr(num_errors);
924                         s += _(" errors detected.");
925                         t = _("You should try to fix them.");
926                 }
927                 Alert::alert(_("There were errors during the LaTeX run."),
928                            s, t);
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);
933         }
934
935         if (bv)
936                 bv->owner()->allowInput();
937
938         int const ERROR_MASK =
939                         LaTeX::NO_LOGFILE |
940                         LaTeX::ERRORS |
941                         LaTeX::NO_OUTPUT;
942
943         return (result & ERROR_MASK) == 0;
944
945 }
946
947
948 string const Converters::papersize(Buffer const * buffer)
949 {
950         char real_papersize = buffer->params.papersize;
951         if (real_papersize == BufferParams::PAPER_DEFAULT)
952                 real_papersize = lyxrc.default_papersize;
953
954         switch (real_papersize) {
955         case BufferParams::PAPER_A3PAPER:
956                 return "a3";
957         case BufferParams::PAPER_A4PAPER:
958                 return "a4";
959         case BufferParams::PAPER_A5PAPER:
960                 return "a5";
961         case BufferParams::PAPER_B5PAPER:
962                 return "b5";
963         case BufferParams::PAPER_EXECUTIVEPAPER:
964                 return "foolscap";
965         case BufferParams::PAPER_LEGALPAPER:
966                 return "legal";
967         case BufferParams::PAPER_USLETTER:
968         default:
969                 return "letter";
970         }
971 }
972
973
974 string const Converters::dvips_options(Buffer const * buffer)
975 {
976         string result;
977         if (!buffer)
978                 return result;
979
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;
989         } else {
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;
997                 }
998         }
999         if (buffer->params.orientation == BufferParams::ORIENTATION_LANDSCAPE &&
1000             buffer->params.papersize2 != BufferParams::VM_PAPER_CUSTOM)
1001                 result += ' ' + lyxrc.print_landscape_flag;
1002         return result;
1003 }
1004
1005
1006 string const Converters::dvipdfm_options(Buffer const * buffer)
1007 {
1008         string result;
1009         if (!buffer)
1010                 return result;
1011
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;
1016
1017                 if (buffer->params.orientation == BufferParams::ORIENTATION_LANDSCAPE)
1018                         result += " -l";
1019         }
1020
1021         return result;
1022 }
1023
1024
1025 vector<Converters::Vertex> Converters::vertices_;
1026
1027
1028 /// The global instance
1029 Formats formats;
1030 Converters converters;
1031
1032 // The global copy after reading lyxrc.defaults
1033 Formats system_formats;
1034 Converters system_converters;