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