]> git.lyx.org Git - lyx.git/blob - src/converter.C
redraw fix 1.
[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                 return false;
584         }
585
586         string path = OnlyPath(from_file);
587         Path p(path);
588
589         bool run_latex = false;
590         string from_base = ChangeExtension(from_file, "");
591         string to_base = ChangeExtension(to_file, "");
592         string infile;
593         string outfile = from_file;
594         for (EdgePath::const_iterator cit = edgepath.begin();
595              cit != edgepath.end(); ++cit) {
596                 Converter const & conv = converterlist_[*cit];
597                 bool dummy = conv.To->dummy() && conv.to != "program";
598                 if (!dummy)
599                         lyxerr[Debug::FILES] << "Converting from  "
600                                << conv.from << " to " << conv.to << endl;
601                 infile = outfile;
602                 outfile = conv.result_dir.empty()
603                         ? ChangeExtension(from_file, conv.To->extension())
604                         : AddName(subst(conv.result_dir,
605                                         token_base, from_base),
606                                   subst(conv.result_file,
607                                         token_base, OnlyFilename(from_base)));
608
609                 if (conv.latex) {
610                         run_latex = true;
611                         string command = subst(conv.command, token_from, "");
612                         lyxerr[Debug::FILES] << "Running " << command << endl;
613                         if (!runLaTeX(buffer, command))
614                                 return false;
615                 } else {
616                         if (conv.need_aux && !run_latex
617                             && !latex_command_.empty()) {
618                                 lyxerr[Debug::FILES]
619                                         << "Running " << latex_command_
620                                         << " to update aux file"<<  endl;
621                                 runLaTeX(buffer, latex_command_);
622                         }
623
624                         string infile2 = (conv.original_dir)
625                                 ? infile : MakeRelPath(infile, path);
626                         string outfile2 = (conv.original_dir)
627                                 ? outfile : MakeRelPath(outfile, path);
628
629                         string command = conv.command;
630                         command = subst(command, token_from, QuoteName(infile2));
631                         command = subst(command, token_base, QuoteName(from_base));
632                         command = subst(command, token_to, QuoteName(outfile2));
633
634                         if (!conv.parselog.empty())
635                                 command += " 2> " + QuoteName(infile2 + ".out");
636
637                         if (conv.from == "dvi" && conv.to == "ps")
638                                 command = add_options(command,
639                                                       dvips_options(buffer));
640                         else if (conv.from == "dvi" && prefixIs(conv.to, "pdf"))
641                                 command = add_options(command,
642                                                       dvipdfm_options(buffer));
643
644                         lyxerr[Debug::FILES] << "Calling " << command << endl;
645                         if (buffer)
646                                 ShowMessage(buffer, _("Executing command:"), command);
647
648                         Systemcall::Starttype type = (dummy)
649                                 ? Systemcall::DontWait : Systemcall::Wait;
650                         Systemcall one;
651                         int res;
652                         if (conv.original_dir && buffer) {
653                                 Path p(buffer->filePath());
654                                 res = one.startscript(type, command);
655                         } else
656                                 res = one.startscript(type, command);
657
658                         if (!conv.parselog.empty()) {
659                                 string const logfile =  infile2 + ".log";
660                                 string const script = LibScriptSearch(conv.parselog);
661                                 string const command2 = script +
662                                         " < " + QuoteName(infile2 + ".out") +
663                                         " > " + QuoteName(logfile);
664                                 one.startscript(Systemcall::Wait, command2);
665                                 if (!scanLog(buffer, command, logfile))
666                                         return false;
667                         }
668
669                         if (res) {
670                                 if (conv.to == "program")
671                                         Alert::alert(_("There were errors during the Build process."),
672                                                    _("You should try to fix them."));
673                                 else
674                                         Alert::alert(_("Cannot convert file"),
675                                                    "Error while executing",
676                                                    command.substr(0, 50));
677                                 return false;
678                         }
679                 }
680         }
681
682         Converter const & conv = converterlist_[edgepath.back()];
683         if (conv.To->dummy())
684                 return true;
685
686
687         if (!conv.result_dir.empty()) {
688                 to_file = AddName(subst(conv.result_dir, token_base, to_base),
689                                   subst(conv.result_file,
690                                         token_base, OnlyFilename(to_base)));
691                 if (from_base != to_base) {
692                         string from = subst(conv.result_dir,
693                                             token_base, from_base);
694                         string to = subst(conv.result_dir,
695                                           token_base, to_base);
696                         if (!lyx::rename(from, to)) {
697                                 Alert::alert(_("Error while trying to move directory:"),
698                                            from, ("to ") + to);
699                                 return false;
700                         }
701                 }
702                 return true;
703         } else
704                 return move(outfile, to_file, conv.latex);
705 }
706
707 // If from = /path/file.ext and to = /path2/file2.ext2 then this method
708 // moves each /path/file*.ext file to /path2/file2*.ext2'
709 bool Converters::move(string const & from, string const & to, bool copy)
710 {
711         if (from == to)
712                 return true;
713
714         bool no_errors = true;
715         string const path = OnlyPath(from);
716         string const base = OnlyFilename(ChangeExtension(from, ""));
717         string const to_base = ChangeExtension(to, "");
718         string const to_extension = GetExtension(to);
719
720         vector<string> files = DirList(OnlyPath(from), GetExtension(from));
721         for (vector<string>::const_iterator it = files.begin();
722              it != files.end(); ++it)
723                 if (prefixIs(*it, base)) {
724                         string const from2 = path + *it;
725                         string to2 = to_base + it->substr(base.length());
726                         to2 = ChangeExtension(to2, to_extension);
727                         lyxerr[Debug::FILES] << "moving " << from2
728                                              << " to " << to2 << endl;
729                         bool const moved = (copy)
730                                 ? lyx::copy(from2, to2)
731                                 : lyx::rename(from2, to2);
732                         if (!moved && no_errors) {
733                                 Alert::alert(_("Error while trying to move file:"),
734                                            from2, _("to ") + to2);
735                                 no_errors = false;
736                         }
737                 }
738         return no_errors;
739 }
740
741
742 bool Converters::convert(Buffer const * buffer,
743                         string const & from_file, string const & to_file_base,
744                         string const & from_format, string const & to_format)
745 {
746         string to_file;
747         return convert(buffer, from_file, to_file_base, from_format, to_format,
748                        to_file);
749 }
750
751
752 void Converters::buildGraph()
753 {
754         vertices_ = vector<Vertex>(formats.size());
755         visited_.resize(formats.size());
756
757         for (ConverterList::iterator it = converterlist_.begin();
758              it != converterlist_.end(); ++it) {
759                 int const s = formats.getNumber(it->from);
760                 int const t = formats.getNumber(it->to);
761                 vertices_[t].in_vertices.push_back(s);
762                 vertices_[s].out_vertices.push_back(t);
763                 vertices_[s].out_edges.push_back(it - converterlist_.begin());
764         }
765 }
766
767
768 bool Converters::formatIsUsed(string const & format)
769 {
770         ConverterList::const_iterator cit = converterlist_.begin();
771         ConverterList::const_iterator end = converterlist_.end();
772         for (; cit != end; ++cit) {
773                 if (cit->from == format || cit->to == format)
774                         return true;
775         }
776         return false;
777 }
778
779
780 bool Converters::scanLog(Buffer const * buffer, string const & command,
781                         string const & filename)
782 {
783         if (!buffer)
784                 return false;
785
786         BufferView * bv = buffer->getUser();
787         if (bv) {
788                 bv->owner()->prohibitInput();
789                 // all error insets should have been removed by now
790         }
791
792         LaTeX latex("", filename, "");
793         TeXErrors terr;
794         int result = latex.scanLogFile(terr);
795         if (bv) {
796                 if ((result & LaTeX::ERRORS)) {
797                         // Insert all errors as errors boxes
798                         bv->insertErrors(terr);
799 #warning repaint() or update() or nothing ?
800                         bv->repaint();
801                         bv->fitCursor();
802                 }
803                 bv->owner()->allowInput();
804         }
805
806         if ((result & LaTeX::ERRORS)) {
807                 int num_errors = latex.getNumErrors();
808                 string s;
809                 string t;
810                 if (num_errors == 1) {
811                         s = _("One error detected");
812                         t = _("You should try to fix it.");
813                 } else {
814                         s = tostr(num_errors);
815                         s += _(" errors detected.");
816                         t = _("You should try to fix them.");
817                 }
818                 string head;
819                 split(command, head, ' ');
820                 Alert::alert(_("There were errors during running of ") + head,
821                            s, t);
822                 return false;
823         } else if (result & LaTeX::NO_OUTPUT) {
824                 string const s = _("The operation resulted in");
825                 string const t = _("an empty file.");
826                 Alert::alert(_("Resulting file is empty"), s, t);
827                 return false;
828         }
829         return true;
830 }
831
832
833 bool Converters::runLaTeX(Buffer const * buffer, string const & command)
834 {
835         if (!buffer)
836                 return false;
837
838         BufferView * bv = buffer->getUser();
839
840         if (bv) {
841                 bv->owner()->prohibitInput();
842                 bv->owner()->message(_("Running LaTeX..."));
843                 // all the autoinsets have already been removed
844         }
845
846         // do the LaTeX run(s)
847         string name = buffer->getLatexName();
848         LaTeX latex(command, name, buffer->filePath());
849         TeXErrors terr;
850         int result = latex.run(terr,
851                                bv ? bv->owner()->getLyXFunc() : 0);
852
853         if (bv) {
854                 if ((result & LaTeX::ERRORS)) {
855                         // Insert all errors as errors boxes
856                         bv->insertErrors(terr);
857 #warning repaint() or update() or nothing ?
858                         bv->repaint();
859                         bv->fitCursor();
860                 }
861         }
862
863         // check return value from latex.run().
864         if ((result & LaTeX::NO_LOGFILE)) {
865                 Alert::alert(_("LaTeX did not work!"),
866                            _("Missing log file:"), name);
867         } else if ((result & LaTeX::ERRORS)) {
868                 int num_errors = latex.getNumErrors();
869                 string s;
870                 string t;
871                 if (num_errors == 1) {
872                         s = _("One error detected");
873                         t = _("You should try to fix it.");
874                 } else {
875                         s = tostr(num_errors);
876                         s += _(" errors detected.");
877                         t = _("You should try to fix them.");
878                 }
879                 Alert::alert(_("There were errors during the LaTeX run."),
880                            s, t);
881         }  else if (result & LaTeX::NO_OUTPUT) {
882                 string const s = _("The operation resulted in");
883                 string const t = _("an empty file.");
884                 Alert::alert(_("Resulting file is empty"), s, t);
885         }
886
887         if (bv)
888                 bv->owner()->allowInput();
889
890         int const ERROR_MASK =
891                         LaTeX::NO_LOGFILE |
892                         LaTeX::ERRORS |
893                         LaTeX::NO_OUTPUT;
894
895         return (result & ERROR_MASK) == 0;
896
897 }
898
899
900 string const Converters::papersize(Buffer const * buffer)
901 {
902         char real_papersize = buffer->params.papersize;
903         if (real_papersize == BufferParams::PAPER_DEFAULT)
904                 real_papersize = lyxrc.default_papersize;
905
906         switch (real_papersize) {
907         case BufferParams::PAPER_A3PAPER:
908                 return "a3";
909         case BufferParams::PAPER_A4PAPER:
910                 return "a4";
911         case BufferParams::PAPER_A5PAPER:
912                 return "a5";
913         case BufferParams::PAPER_B5PAPER:
914                 return "b5";
915         case BufferParams::PAPER_EXECUTIVEPAPER:
916                 return "foolscap";
917         case BufferParams::PAPER_LEGALPAPER:
918                 return "legal";
919         case BufferParams::PAPER_USLETTER:
920         default:
921                 return "letter";
922         }
923 }
924
925
926 string const Converters::dvips_options(Buffer const * buffer)
927 {
928         string result;
929         if (!buffer)
930                 return result;
931
932         if (buffer->params.use_geometry
933             && buffer->params.papersize2 == BufferParams::VM_PAPER_CUSTOM
934             && !lyxrc.print_paper_dimension_flag.empty()
935             && !buffer->params.paperwidth.empty()
936             && !buffer->params.paperheight.empty()) {
937                 // using a custom papersize
938                 result = lyxrc.print_paper_dimension_flag;
939                 result += ' ' + buffer->params.paperwidth;
940                 result += ',' + buffer->params.paperheight;
941         } else {
942                 string const paper_option = papersize(buffer);
943                 if (paper_option != "letter" ||
944                     buffer->params.orientation != BufferParams::ORIENTATION_LANDSCAPE) {
945                         // dvips won't accept -t letter -t landscape.  In all other
946                         // cases, include the paper size explicitly.
947                         result = lyxrc.print_paper_flag;
948                         result += ' ' + paper_option;
949                 }
950         }
951         if (buffer->params.orientation == BufferParams::ORIENTATION_LANDSCAPE &&
952             buffer->params.papersize2 != BufferParams::VM_PAPER_CUSTOM)
953                 result += ' ' + lyxrc.print_landscape_flag;
954         return result;
955 }
956
957
958 string const Converters::dvipdfm_options(Buffer const * buffer)
959 {
960         string result;
961         if (!buffer)
962                 return result;
963
964         if (buffer->params.papersize2 != BufferParams::VM_PAPER_CUSTOM) {
965                 string const paper_size = papersize(buffer);
966                 if (paper_size != "b5" && paper_size != "foolscap")
967                         result = "-p "+ paper_size;
968
969                 if (buffer->params.orientation == BufferParams::ORIENTATION_LANDSCAPE)
970                         result += " -l";
971         }
972
973         return result;
974 }
975
976
977 vector<Converters::Vertex> Converters::vertices_;
978
979
980 /// The global instance
981 Formats formats;
982 Converters converters;
983
984 // The global copy after reading lyxrc.defaults
985 Formats system_formats;
986 Converters system_converters;