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