]> git.lyx.org Git - lyx.git/blob - src/converter.C
multicol; small stuff
[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 // FIXME: horrednously mis-named, especially given the other ::add
123 // function
124 void Formats::add(string const & name, string const & extension,
125                   string const & prettyname, string const & shortcut)
126 {
127         FormatList::iterator it =
128                 find_if(formatlist.begin(), formatlist.end(),
129                         lyx::compare_memfun(&Format::name, name));
130         if (it == formatlist.end())
131                 formatlist.push_back(Format(name, extension, prettyname,
132                                             shortcut, ""));
133         else {
134                 string viewer = it->viewer();
135                 *it = Format(name, extension, prettyname, shortcut, viewer);
136         }
137 }
138
139
140 void Formats::erase(string const & name)
141 {
142         FormatList::iterator it =
143                 find_if(formatlist.begin(), formatlist.end(),
144                         lyx::compare_memfun(&Format::name, name));
145         if (it != formatlist.end())
146                 formatlist.erase(it);
147 }
148
149
150 void Formats::sort()
151 {
152         std::sort(formatlist.begin(), formatlist.end());
153 }
154
155
156 void Formats::setViewer(string const & name, string const & command)
157 {
158         add(name);
159         FormatList::iterator it =
160                 find_if(formatlist.begin(), formatlist.end(),
161                         lyx::compare_memfun(&Format::name, name));
162         if (it != formatlist.end())
163                 it->setViewer(command);
164 }
165
166
167 bool Formats::view(Buffer const * buffer, string const & filename,
168                    string const & format_name) const
169 {
170         if (filename.empty())
171                 return false;
172
173         Format const * format = getFormat(format_name);
174         if (format && format->viewer().empty() &&
175             format->isChildFormat())
176                 format = getFormat(format->parentFormat());
177         if (!format || format->viewer().empty()) {
178 #if USE_BOOST_FORMAT
179                 Alert::alert(_("Cannot view file"),
180                              boost::io::str(boost::format(_("No information for viewing %1$s"))
181                            % prettyName(format_name)));
182 #else
183                 Alert::alert(_("Cannot view file"),
184                              _("No information for viewing ")
185                              + prettyName(format_name));
186 #endif
187                            return false;
188         }
189
190         string command = format->viewer();
191
192         if (format_name == "dvi" &&
193             !lyxrc.view_dvi_paper_option.empty()) {
194                 command += ' ' + lyxrc.view_dvi_paper_option;
195                 string paper_size = converters.papersize(buffer);
196                 if (paper_size == "letter")
197                         paper_size = "us";
198                 command += ' ' + paper_size;
199                 if (buffer->params.orientation
200                     == BufferParams::ORIENTATION_LANDSCAPE)
201                         command += 'r';
202         }
203
204         if (!contains(command, token_from))
205                 command += ' ' + token_from;
206
207         command = subst(command, token_from,
208                         QuoteName(OnlyFilename(filename)));
209         command = subst(command, token_path, QuoteName(OnlyPath(filename)));
210
211         lyxerr[Debug::FILES] << "Executing command: " << command << endl;
212         ShowMessage(buffer, _("Executing command:"), command);
213
214         Path p(OnlyPath(filename));
215         Systemcall one;
216         int const res = one.startscript(Systemcall::DontWait, command);
217
218         if (res) {
219                 Alert::alert(_("Cannot view file"),
220                            _("Error while executing"),
221                            command.substr(0, 50));
222                 return false;
223         }
224         return true;
225 }
226
227
228 string const Formats::prettyName(string const & name) const
229 {
230         Format const * format = getFormat(name);
231         if (format)
232                 return format->prettyname();
233         else
234                 return name;
235 }
236
237
238 string const Formats::extension(string const & name) const
239 {
240         Format const * format = getFormat(name);
241         if (format)
242                 return format->extension();
243         else
244                 return name;
245 }
246
247 //////////////////////////////////////////////////////////////////////////////
248
249 void Converter::readFlags()
250 {
251         string flag_list(flags);
252         while (!flag_list.empty()) {
253                 string flag_name, flag_value;
254                 flag_list = split(flag_list, flag_value, ',');
255                 flag_value = split(flag_value, flag_name, '=');
256                 if (flag_name == "latex")
257                         latex = true;
258                 else if (flag_name == "originaldir")
259                         original_dir = true;
260                 else if (flag_name == "needaux")
261                         need_aux = true;
262                 else if (flag_name == "resultdir")
263                         result_dir = (flag_value.empty())
264                                 ? token_base : flag_value;
265                 else if (flag_name == "resultfile")
266                         result_file = flag_value;
267                 else if (flag_name == "parselog")
268                         parselog = flag_value;
269         }
270         if (!result_dir.empty() && result_file.empty())
271                 result_file = "index." + formats.extension(to);
272         //if (!contains(command, token_from))
273         //      latex = true;
274 }
275
276
277 bool operator<(Converter const & a, Converter const & b)
278 {
279         // use the compare_ascii_no_case instead of compare_no_case,
280         // because in turkish, 'i' is not the lowercase version of 'I',
281         // and thus turkish locale breaks parsing of tags.
282         int const i = compare_ascii_no_case(a.From->prettyname(),
283                                             b.From->prettyname());
284         if (i == 0)
285                 return compare_ascii_no_case(a.To->prettyname(), b.To->prettyname())
286                         < 0;
287         else
288                 return i < 0;
289 }
290
291 //////////////////////////////////////////////////////////////////////////////
292
293 class compare_Converter {
294 public:
295         compare_Converter(string const & f, string const & t)
296                 : from(f), to(t) {}
297         bool operator()(Converter const & c) {
298                 return c.from == from && c.to == to;
299         }
300 private:
301         string const & from;
302         string const & to;
303 };
304
305
306 Converter const * Converters::getConverter(string const & from,
307                                             string const & to)
308 {
309         ConverterList::const_iterator cit =
310                 find_if(converterlist_.begin(), converterlist_.end(),
311                         compare_Converter(from, to));
312         if (cit != converterlist_.end())
313                 return &(*cit);
314         else
315                 return 0;
316 }
317
318
319 int Converters::getNumber(string const & from, string const & to)
320 {
321         ConverterList::const_iterator cit =
322                 find_if(converterlist_.begin(), converterlist_.end(),
323                         compare_Converter(from, to));
324         if (cit != converterlist_.end())
325                 return cit - converterlist_.begin();
326         else
327                 return -1;
328 }
329
330
331 void Converters::add(string const & from, string const & to,
332                      string const & command, string const & flags)
333 {
334         formats.add(from);
335         formats.add(to);
336         ConverterList::iterator it = find_if(converterlist_.begin(),
337                                              converterlist_.end(),
338                                              compare_Converter(from, to));
339
340         Converter converter(from, to, command, flags);
341         if (it != converterlist_.end() && !flags.empty() && flags[0] == '*') {
342                 converter = *it;
343                 converter.command = command;
344                 converter.flags = flags;
345         }
346         converter.readFlags();
347
348         if (converter.latex && (latex_command_.empty() || to == "dvi"))
349                 latex_command_ = subst(command, token_from, "");
350         // If we have both latex & pdflatex, we set latex_command to latex.
351         // The latex_command is used to update the .aux file when running
352         // a converter that uses it.
353
354         if (it == converterlist_.end()) {
355                 converterlist_.push_back(converter);
356         } else {
357                 converter.From = it->From;
358                 converter.To = it->To;
359                 *it = converter;
360         }
361 }
362
363
364 void Converters::erase(string const & from, string const & to)
365 {
366         ConverterList::iterator it = find_if(converterlist_.begin(),
367                                              converterlist_.end(),
368                                              compare_Converter(from, to));
369         if (it != converterlist_.end())
370                 converterlist_.erase(it);
371 }
372
373
374 // This method updates the pointers From and To in all the converters.
375 // The code is not very efficient, but it doesn't matter as the number
376 // of formats and converters is small.
377 // Furthermore, this method is called only on startup, or after
378 // adding/deleting a format in FormPreferences (the latter calls can be
379 // eliminated if the formats in the Formats class are stored using a map or
380 // a list (instead of a vector), but this will cause other problems).
381 void Converters::update(Formats const & formats)
382 {
383         ConverterList::iterator it = converterlist_.begin();
384         ConverterList::iterator end = converterlist_.end();
385         for (; it != end; ++it) {
386                 it->From = formats.getFormat(it->from);
387                 it->To = formats.getFormat(it->to);
388         }
389 }
390
391
392 // This method updates the pointers From and To in the last converter.
393 // It is called when adding a new converter in FormPreferences
394 void Converters::updateLast(Formats const & formats)
395 {
396         if (converterlist_.begin() != converterlist_.end()) {
397                 ConverterList::iterator it = converterlist_.end() - 1;
398                 it->From = formats.getFormat(it->from);
399                 it->To = formats.getFormat(it->to);
400         }
401 }
402
403
404 void Converters::sort()
405 {
406         std::sort(converterlist_.begin(), converterlist_.end());
407 }
408
409
410 int Converters::bfs_init(string const & start, bool clear_visited)
411 {
412         int const s = formats.getNumber(start);
413         if (s < 0)
414                 return s;
415
416         Q_ = queue<int>();
417         if (clear_visited)
418                 fill(visited_.begin(), visited_.end(), false);
419         if (visited_[s] == false) {
420                 Q_.push(s);
421                 visited_[s] = true;
422         }
423         return s;
424 }
425
426
427 vector<Format const *> const
428 Converters::getReachableTo(string const & target, bool clear_visited)
429 {
430         vector<Format const *> result;
431         int const s = bfs_init(target, clear_visited);
432         if (s < 0)
433                 return result;
434
435         while (!Q_.empty()) {
436                 int const i = Q_.front();
437                 Q_.pop();
438                 if (i != s || target != "lyx") {
439                         result.push_back(&formats.get(i));
440                 }
441
442                 vector<int>::iterator it = vertices_[i].in_vertices.begin();
443                 vector<int>::iterator end = vertices_[i].in_vertices.end();
444                 for (; it != end; ++it) {
445                         if (!visited_[*it]) {
446                                 visited_[*it] = true;
447                                 Q_.push(*it);
448                         }
449                 }
450         }
451
452         return result;
453 }
454
455
456 vector<Format const *> const
457 Converters::getReachable(string const & from, bool only_viewable,
458                          bool clear_visited)
459 {
460         vector<Format const *> result;
461
462         if (bfs_init(from, clear_visited) < 0)
463                 return result;
464
465         while (!Q_.empty()) {
466                 int const i = Q_.front();
467                 Q_.pop();
468                 Format const & format = formats.get(i);
469                 if (format.name() == "lyx")
470                         continue;
471                 if (!only_viewable || !format.viewer().empty() ||
472                     format.isChildFormat())
473                         result.push_back(&format);
474
475                 vector<int>::const_iterator cit =
476                         vertices_[i].out_vertices.begin();
477                 vector<int>::const_iterator end =
478                         vertices_[i].out_vertices.end();
479                 for (; cit != end; ++cit)
480                         if (!visited_[*cit]) {
481                                 visited_[*cit] = true;
482                                 Q_.push(*cit);
483                         }
484         }
485
486         return result;
487 }
488
489
490 bool Converters::isReachable(string const & from, string const & to)
491 {
492         if (from == to)
493                 return true;
494
495         int const s = bfs_init(from);
496         int const t = formats.getNumber(to);
497         if (s < 0 || t < 0)
498                 return false;
499
500         while (!Q_.empty()) {
501                 int const i = Q_.front();
502                 Q_.pop();
503                 if (i == t)
504                         return true;
505
506                 vector<int>::const_iterator cit =
507                         vertices_[i].out_vertices.begin();
508                 vector<int>::const_iterator end =
509                         vertices_[i].out_vertices.end();
510                 for (; cit != end; ++cit) {
511                         if (!visited_[*cit]) {
512                                 visited_[*cit] = true;
513                                 Q_.push(*cit);
514                         }
515                 }
516         }
517
518         return false;
519 }
520
521
522 Converters::EdgePath const
523 Converters::getPath(string const & from, string const & to)
524 {
525         EdgePath path;
526         if (from == to)
527                 return path;
528
529         int const s = bfs_init(from);
530         int t = formats.getNumber(to);
531         if (s < 0 || t < 0)
532                 return path;
533
534         vector<int> prev_edge(formats.size());
535         vector<int> prev_vertex(formats.size());
536
537         bool found = false;
538         while (!Q_.empty()) {
539                 int const i = Q_.front();
540                 Q_.pop();
541                 if (i == t) {
542                         found = true;
543                         break;
544                 }
545
546                 vector<int>::const_iterator beg =
547                         vertices_[i].out_vertices.begin();
548                 vector<int>::const_iterator cit = beg;
549                 vector<int>::const_iterator end =
550                         vertices_[i].out_vertices.end();
551                 for (; cit != end; ++cit)
552                         if (!visited_[*cit]) {
553                                 int const j = *cit;
554                                 visited_[j] = true;
555                                 Q_.push(j);
556                                 int const k = cit - beg;
557                                 prev_edge[j] = vertices_[i].out_edges[k];
558                                 prev_vertex[j] = i;
559                         }
560         }
561         if (!found)
562                 return path;
563
564         while (t != s) {
565                 path.push_back(prev_edge[t]);
566                 t = prev_vertex[t];
567         }
568         reverse(path.begin(), path.end());
569         return path;
570 }
571
572
573 bool Converters::usePdflatex(EdgePath const & path)
574 {
575         for (EdgePath::const_iterator cit = path.begin();
576              cit != path.end(); ++cit) {
577                 Converter const & conv = converterlist_[*cit];
578                 if (conv.latex)
579                         return contains(conv.to, "pdf");
580         }
581         return false;
582 }
583
584
585 bool Converters::convert(Buffer const * buffer,
586                          string const & from_file, string const & to_file_base,
587                          string const & from_format, string const & to_format,
588                          string & to_file)
589 {
590         to_file = ChangeExtension(to_file_base,
591                                   formats.extension(to_format));
592
593         if (from_format == to_format)
594                 return move(from_file, to_file, false);
595
596         EdgePath edgepath = getPath(from_format, to_format);
597         if (edgepath.empty()) {
598                 return false;
599         }
600
601         string path = OnlyPath(from_file);
602         Path p(path);
603
604         bool run_latex = false;
605         string from_base = ChangeExtension(from_file, "");
606         string to_base = ChangeExtension(to_file, "");
607         string infile;
608         string outfile = from_file;
609         for (EdgePath::const_iterator cit = edgepath.begin();
610              cit != edgepath.end(); ++cit) {
611                 Converter const & conv = converterlist_[*cit];
612                 bool dummy = conv.To->dummy() && conv.to != "program";
613                 if (!dummy)
614                         lyxerr[Debug::FILES] << "Converting from  "
615                                << conv.from << " to " << conv.to << endl;
616                 infile = outfile;
617                 outfile = conv.result_dir.empty()
618                         ? ChangeExtension(from_file, conv.To->extension())
619                         : AddName(subst(conv.result_dir,
620                                         token_base, from_base),
621                                   subst(conv.result_file,
622                                         token_base, OnlyFilename(from_base)));
623
624                 // if input and output files are equal, we use a
625                 // temporary file as intermediary (JMarc)
626                 string real_outfile;
627                 if (outfile == infile) {
628                         real_outfile = infile;
629                         outfile = AddName(buffer->tmppath, "tmpfile.out");
630                 }
631
632                 if (conv.latex) {
633                         run_latex = true;
634                         string command = subst(conv.command, token_from, "");
635                         lyxerr[Debug::FILES] << "Running " << command << endl;
636                         if (!runLaTeX(buffer, command))
637                                 return false;
638                 } else {
639                         if (conv.need_aux && !run_latex
640                             && !latex_command_.empty()) {
641                                 lyxerr[Debug::FILES]
642                                         << "Running " << latex_command_
643                                         << " to update aux file"<<  endl;
644                                 runLaTeX(buffer, latex_command_);
645                         }
646
647                         string infile2 = (conv.original_dir)
648                                 ? infile : MakeRelPath(infile, path);
649                         string outfile2 = (conv.original_dir)
650                                 ? outfile : MakeRelPath(outfile, path);
651
652                         string command = conv.command;
653                         command = subst(command, token_from, QuoteName(infile2));
654                         command = subst(command, token_base, QuoteName(from_base));
655                         command = subst(command, token_to, QuoteName(outfile2));
656                         command = LibScriptSearch(command);
657
658                         if (!conv.parselog.empty())
659                                 command += " 2> " + QuoteName(infile2 + ".out");
660
661                         if (conv.from == "dvi" && conv.to == "ps")
662                                 command = add_options(command,
663                                                       dvips_options(buffer));
664                         else if (conv.from == "dvi" && prefixIs(conv.to, "pdf"))
665                                 command = add_options(command,
666                                                       dvipdfm_options(buffer));
667
668                         lyxerr[Debug::FILES] << "Calling " << command << endl;
669                         if (buffer)
670                                 ShowMessage(buffer, _("Executing command:"), command);
671
672                         Systemcall::Starttype type = (dummy)
673                                 ? Systemcall::DontWait : Systemcall::Wait;
674                         Systemcall one;
675                         int res;
676                         if (conv.original_dir && buffer) {
677                                 Path p(buffer->filePath());
678                                 res = one.startscript(type, command);
679                         } else
680                                 res = one.startscript(type, command);
681
682                         if (!real_outfile.empty()) {
683                                 if (!lyx::rename(outfile, real_outfile))
684                                         res = -1;
685                                 else
686                                         lyxerr[Debug::FILES]
687                                                 << "renaming file " << outfile
688                                                 << " to " << real_outfile
689                                                 << endl;
690                         }
691
692                         if (!conv.parselog.empty()) {
693                                 string const logfile =  infile2 + ".log";
694                                 string const script = LibScriptSearch(conv.parselog);
695                                 string const command2 = script +
696                                         " < " + QuoteName(infile2 + ".out") +
697                                         " > " + QuoteName(logfile);
698                                 one.startscript(Systemcall::Wait, command2);
699                                 if (!scanLog(buffer, command, logfile))
700                                         return false;
701                         }
702
703                         if (res) {
704                                 if (conv.to == "program")
705                                         Alert::alert(_("There were errors during the Build process."),
706                                                    _("You should try to fix them."));
707                                 else
708                                         Alert::alert(_("Cannot convert file"),
709                                                    _("Error while executing"),
710                                                    command.substr(0, 50));
711                                 return false;
712                         }
713                 }
714         }
715
716         Converter const & conv = converterlist_[edgepath.back()];
717         if (conv.To->dummy())
718                 return true;
719
720
721         if (!conv.result_dir.empty()) {
722                 to_file = AddName(subst(conv.result_dir, token_base, to_base),
723                                   subst(conv.result_file,
724                                         token_base, OnlyFilename(to_base)));
725                 if (from_base != to_base) {
726                         string from = subst(conv.result_dir,
727                                             token_base, from_base);
728                         string to = subst(conv.result_dir,
729                                           token_base, to_base);
730                         if (!lyx::rename(from, to)) {
731 #if USE_BOOST_FORMAT
732                                 Alert::alert(_("Error while trying to move directory:"),
733                                            from, boost::io::str(boost::format(_("to %1$s")) % to));
734 #else
735                                 Alert::alert(_("Error while trying to move directory:"),
736                                            from, _("to ") + to);
737 #endif
738                                 return false;
739                         }
740                 }
741                 return true;
742         } else
743                 return move(outfile, to_file, conv.latex);
744 }
745
746
747 // If from = /path/file.ext and to = /path2/file2.ext2 then this method
748 // moves each /path/file*.ext file to /path2/file2*.ext2'
749 bool Converters::move(string const & from, string const & to, bool copy)
750 {
751         if (from == to)
752                 return true;
753
754         bool no_errors = true;
755         string const path = OnlyPath(from);
756         string const base = OnlyFilename(ChangeExtension(from, ""));
757         string const to_base = ChangeExtension(to, "");
758         string const to_extension = GetExtension(to);
759
760         vector<string> files = DirList(OnlyPath(from), GetExtension(from));
761         for (vector<string>::const_iterator it = files.begin();
762              it != files.end(); ++it)
763                 if (prefixIs(*it, base)) {
764                         string const from2 = path + *it;
765                         string to2 = to_base + it->substr(base.length());
766                         to2 = ChangeExtension(to2, to_extension);
767                         lyxerr[Debug::FILES] << "moving " << from2
768                                              << " to " << to2 << endl;
769                         bool const moved = (copy)
770                                 ? lyx::copy(from2, to2)
771                                 : lyx::rename(from2, to2);
772                         if (!moved && no_errors) {
773 #if USE_BOOST_FORMAT
774                                 Alert::alert(_("Error while trying to move file:"),
775                                            from2, boost::io::str(boost::format(_("to %1$s")) % to2));
776 #else
777                                 Alert::alert(_("Error while trying to move file:"),
778                                            from2, _("to ") + to2);
779 #endif
780                                 no_errors = false;
781                         }
782                 }
783         return no_errors;
784 }
785
786
787 bool Converters::convert(Buffer const * buffer,
788                         string const & from_file, string const & to_file_base,
789                         string const & from_format, string const & to_format)
790 {
791         string to_file;
792         return convert(buffer, from_file, to_file_base, from_format, to_format,
793                        to_file);
794 }
795
796
797 void Converters::buildGraph()
798 {
799         vertices_ = vector<Vertex>(formats.size());
800         visited_.resize(formats.size());
801
802         for (ConverterList::iterator it = converterlist_.begin();
803              it != converterlist_.end(); ++it) {
804                 int const s = formats.getNumber(it->from);
805                 int const t = formats.getNumber(it->to);
806                 vertices_[t].in_vertices.push_back(s);
807                 vertices_[s].out_vertices.push_back(t);
808                 vertices_[s].out_edges.push_back(it - converterlist_.begin());
809         }
810 }
811
812
813 bool Converters::formatIsUsed(string const & format)
814 {
815         ConverterList::const_iterator cit = converterlist_.begin();
816         ConverterList::const_iterator end = converterlist_.end();
817         for (; cit != end; ++cit) {
818                 if (cit->from == format || cit->to == format)
819                         return true;
820         }
821         return false;
822 }
823
824
825 bool Converters::scanLog(Buffer const * buffer, string const & command,
826                         string const & filename)
827 {
828         if (!buffer)
829                 return false;
830
831         BufferView * bv = buffer->getUser();
832         if (bv) {
833                 bv->owner()->busy(true);
834                 // all error insets should have been removed by now
835         }
836
837         LaTeX latex("", filename, "");
838         TeXErrors terr;
839         int result = latex.scanLogFile(terr);
840         if (bv) {
841                 if ((result & LaTeX::ERRORS)) {
842                         // Insert all errors as errors boxes
843                         bv->insertErrors(terr);
844 #warning repaint() or update() or nothing ?
845                         bv->repaint();
846                         bv->fitCursor();
847                 }
848                 bv->owner()->busy(false);
849         }
850
851         if ((result & LaTeX::ERRORS)) {
852                 int num_errors = latex.getNumErrors();
853                 string s;
854                 string t;
855                 if (num_errors == 1) {
856                         s = _("One error detected");
857                         t = _("You should try to fix it.");
858                 } else {
859                         s = tostr(num_errors);
860                         s += _(" errors detected.");
861                         t = _("You should try to fix them.");
862                 }
863                 string head;
864                 split(command, head, ' ');
865 #if USE_BOOST_FORMAT
866                 Alert::alert(boost::io::str(boost::format(_("There were errors during running of %1$s")) % head),
867                            s, t);
868 #else
869                 Alert::alert(_("There were errors during running of ") + head,
870                            s, t);
871 #endif
872                 return false;
873         } else if (result & LaTeX::NO_OUTPUT) {
874                 string const s = _("The operation resulted in");
875                 string const t = _("an empty file.");
876                 Alert::alert(_("Resulting file is empty"), s, t);
877                 return false;
878         }
879         return true;
880 }
881
882
883 bool Converters::runLaTeX(Buffer const * buffer, string const & command)
884 {
885         if (!buffer)
886                 return false;
887
888         BufferView * bv = buffer->getUser();
889
890         if (bv) {
891                 bv->owner()->busy(true);
892                 bv->owner()->message(_("Running LaTeX..."));
893                 // all the autoinsets have already been removed
894         }
895
896         // do the LaTeX run(s)
897         string name = buffer->getLatexName();
898         LaTeX latex(command, name, buffer->filePath());
899         TeXErrors terr;
900         int result = latex.run(terr,
901                                bv ? &bv->owner()->getLyXFunc() : 0);
902
903         if (bv) {
904                 if ((result & LaTeX::ERRORS)) {
905                         // Insert all errors as errors boxes
906                         bv->insertErrors(terr);
907 #warning repaint() or update() or nothing ?
908                         bv->repaint();
909                         bv->fitCursor();
910                 }
911         }
912
913         // check return value from latex.run().
914         if ((result & LaTeX::NO_LOGFILE)) {
915                 Alert::alert(_("LaTeX did not work!"),
916                            _("Missing log file:"), name);
917         } else if ((result & LaTeX::ERRORS)) {
918                 int num_errors = latex.getNumErrors();
919                 string s;
920                 string t;
921                 if (num_errors == 1) {
922                         s = _("One error detected");
923                         t = _("You should try to fix it.");
924                 } else {
925                         s = tostr(num_errors);
926                         s += _(" errors detected.");
927                         t = _("You should try to fix them.");
928                 }
929                 Alert::alert(_("There were errors during the LaTeX run."),
930                            s, t);
931         }  else if (result & LaTeX::NO_OUTPUT) {
932                 string const s = _("The operation resulted in");
933                 string const t = _("an empty file.");
934                 Alert::alert(_("Resulting file is empty"), s, t);
935         }
936
937         if (bv)
938                 bv->owner()->busy(false);
939
940         int const ERROR_MASK =
941                         LaTeX::NO_LOGFILE |
942                         LaTeX::ERRORS |
943                         LaTeX::NO_OUTPUT;
944
945         return (result & ERROR_MASK) == 0;
946
947 }
948
949
950 string const Converters::papersize(Buffer const * buffer)
951 {
952         char real_papersize = buffer->params.papersize;
953         if (real_papersize == BufferParams::PAPER_DEFAULT)
954                 real_papersize = lyxrc.default_papersize;
955
956         switch (real_papersize) {
957         case BufferParams::PAPER_A3PAPER:
958                 return "a3";
959         case BufferParams::PAPER_A4PAPER:
960                 return "a4";
961         case BufferParams::PAPER_A5PAPER:
962                 return "a5";
963         case BufferParams::PAPER_B5PAPER:
964                 return "b5";
965         case BufferParams::PAPER_EXECUTIVEPAPER:
966                 return "foolscap";
967         case BufferParams::PAPER_LEGALPAPER:
968                 return "legal";
969         case BufferParams::PAPER_USLETTER:
970         default:
971                 return "letter";
972         }
973 }
974
975
976 string const Converters::dvips_options(Buffer const * buffer)
977 {
978         string result;
979         if (!buffer)
980                 return result;
981
982         if (buffer->params.use_geometry
983             && buffer->params.papersize2 == BufferParams::VM_PAPER_CUSTOM
984             && !lyxrc.print_paper_dimension_flag.empty()
985             && !buffer->params.paperwidth.empty()
986             && !buffer->params.paperheight.empty()) {
987                 // using a custom papersize
988                 result = lyxrc.print_paper_dimension_flag;
989                 result += ' ' + buffer->params.paperwidth;
990                 result += ',' + buffer->params.paperheight;
991         } else {
992                 string const paper_option = papersize(buffer);
993                 if (paper_option != "letter" ||
994                     buffer->params.orientation != BufferParams::ORIENTATION_LANDSCAPE) {
995                         // dvips won't accept -t letter -t landscape.  In all other
996                         // cases, include the paper size explicitly.
997                         result = lyxrc.print_paper_flag;
998                         result += ' ' + paper_option;
999                 }
1000         }
1001         if (buffer->params.orientation == BufferParams::ORIENTATION_LANDSCAPE &&
1002             buffer->params.papersize2 != BufferParams::VM_PAPER_CUSTOM)
1003                 result += ' ' + lyxrc.print_landscape_flag;
1004         return result;
1005 }
1006
1007
1008 string const Converters::dvipdfm_options(Buffer const * buffer)
1009 {
1010         string result;
1011         if (!buffer)
1012                 return result;
1013
1014         if (buffer->params.papersize2 != BufferParams::VM_PAPER_CUSTOM) {
1015                 string const paper_size = papersize(buffer);
1016                 if (paper_size != "b5" && paper_size != "foolscap")
1017                         result = "-p "+ paper_size;
1018
1019                 if (buffer->params.orientation == BufferParams::ORIENTATION_LANDSCAPE)
1020                         result += " -l";
1021         }
1022
1023         return result;
1024 }
1025
1026
1027 vector<Converters::Vertex> Converters::vertices_;
1028
1029
1030 /// The global instance
1031 Formats formats;
1032 Converters converters;
1033
1034 // The global copy after reading lyxrc.defaults
1035 Formats system_formats;
1036 Converters system_converters;