]> git.lyx.org Git - lyx.git/blob - src/Format.cpp
Update Russian localization
[lyx.git] / src / Format.cpp
1 /**
2  * \file Format.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Dekel Tsur
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "Format.h"
14 #include "Buffer.h"
15 #include "BufferParams.h"
16 #include "LyXRC.h"
17 #include "ServerSocket.h"
18
19 #include "frontends/alert.h" //to be removed?
20
21 #include "support/debug.h"
22 #include "support/docstream.h"
23 #include "support/filetools.h"
24 #include "support/gettext.h"
25 #include "support/lstrings.h"
26 #include "support/lyxmagic.h"
27 #include "support/mutex.h"
28 #include "support/os.h"
29 #include "support/PathChanger.h"
30 #include "support/Systemcall.h"
31 #include "support/textutils.h"
32 #include "support/Translator.h"
33
34 #include <algorithm>
35 #include <functional>
36 #include <map>
37 #include <ctime>
38
39 // FIXME: Q_OS_MAC is not available, it's in Qt
40 #ifdef USE_MACOSX_PACKAGING
41 #include "support/linkback/LinkBackProxy.h"
42 #endif
43
44 using namespace std;
45 using namespace lyx::support;
46
47 namespace lyx {
48
49 namespace Alert = frontend::Alert;
50 namespace os = support::os;
51
52 namespace {
53
54 string const token_from_format("$$i");
55 string const token_path_format("$$p");
56 string const token_socket_format("$$a");
57
58 } // namespace
59
60
61 bool Format::formatSorter(Format const * lhs, Format const * rhs)
62 {
63         return compare_locale(translateIfPossible(lhs->prettyname()),
64                               translateIfPossible(rhs->prettyname())) < 0;
65 }
66
67 bool operator<(Format const & a, Format const & b)
68 {
69         return compare_locale(translateIfPossible(a.prettyname()),
70                               translateIfPossible(b.prettyname())) < 0;
71 }
72
73
74 Format::Format(string const & n, string const & e, docstring const & p,
75                string const & s, string const & v, string const & ed,
76                string const & m, int flags)
77         : name_(n), prettyname_(p), shortcut_(s), viewer_(v),
78           editor_(ed), mime_(m), flags_(flags)
79 {
80         extension_list_ = getVectorFromString(e, ",");
81         LYXERR(Debug::GRAPHICS, "New Format: n=" << n << ", flags=" << flags);
82 }
83
84
85 bool Format::dummy() const
86 {
87         return extension().empty();
88 }
89
90
91 string const Format::extensions() const
92 {
93         return getStringFromVector(extension_list_, ", ");
94 }
95
96
97 bool Format::hasExtension(string const & ext) const
98 {
99         return (find(extension_list_.begin(), extension_list_.end(), ext)
100                 != extension_list_.end());
101 }
102
103
104 bool Format::isChildFormat() const
105 {
106         if (name_.empty())
107                 return false;
108         return isDigitASCII(name_[name_.length() - 1]);
109 }
110
111
112 string const Format::parentFormat() const
113 {
114         return name_.substr(0, name_.length() - 1);
115 }
116
117
118 void Format::setExtensions(string const & e)
119 {
120         extension_list_ = getVectorFromString(e, ",");
121 }
122
123
124 namespace {
125
126 std::function<bool (Format const &)> FormatNameIs(string const & name)
127 {
128         return [name](Format const & f){ return f.name() == name; };
129 }
130
131 }
132
133 // This method should return a reference, and throw an exception
134 // if the format named name cannot be found (Lgb)
135 Format const * Formats::getFormat(string const & name) const
136 {
137         FormatList::const_iterator cit =
138                 find_if(formatlist_.begin(), formatlist_.end(),
139                         FormatNameIs(name));
140         if (cit != formatlist_.end())
141                 return &(*cit);
142         else
143                 return nullptr;
144 }
145
146
147 Format * Formats::getFormat(string const & name)
148 {
149         FormatList::iterator it =
150                 find_if(formatlist_.begin(), formatlist_.end(),
151                                 FormatNameIs(name));
152
153         if (it != formatlist_.end())
154                 return &(*it);
155
156         return nullptr;
157 }
158
159
160 namespace {
161
162 /** Guess the file format name (as in Format::name()) from contents.
163  *  Normally you don't want to use this directly, but rather
164  *  Formats::getFormatFromFile().
165  */
166 string guessFormatFromContents(FileName const & fn)
167 {
168         // the different filetypes and what they contain in one of the first lines
169         // (dots are any characters).           (Herbert 20020131)
170         // AGR  Grace...
171         // BMP  BM...
172         // EPS  %!PS-Adobe-3.0 EPSF...
173         // FIG  #FIG...
174         // FITS ...BITPIX...
175         // GIF  GIF...
176         // JPG  \377\330...     (0xFFD8)
177         // PDF  %PDF-...
178         // PNG  .PNG...
179         // PBM  P1... or P4     (B/W)
180         // PGM  P2... or P5     (Grayscale)
181         // PPM  P3... or P6     (color)
182         // PS   %!PS-Adobe-2.0 or 1.0,  no "EPSF"!
183         // SGI  \001\332...     (decimal 474)
184         // TGIF %TGIF...
185         // TIFF II... or MM...
186         // XBM  ..._bits[]...
187         // XPM  /* XPM */    sometimes missing (f.ex. tgif-export)
188         //      ...static char *...
189         // XWD  \000\000\000\151        (0x00006900) decimal 105
190         //
191         // GZIP \037\213        http://www.ietf.org/rfc/rfc1952.txt
192         // ZIP  PK...                   http://www.halyava.ru/document/ind_arch.htm
193         // Z    \037\235                UNIX compress
194
195         // paranoia check
196         if (fn.empty() || !fn.isReadableFile())
197                 return string();
198
199         ifstream ifs(fn.toFilesystemEncoding().c_str());
200         if (!ifs)
201                 // Couldn't open file...
202                 return string();
203
204         // gnuzip
205         static string const gzipStamp = "\037\213";
206
207         // PKZIP
208         static string const zipStamp = "PK";
209
210         // ZIP containers (koffice, openoffice.org etc).
211         static string const nonzipStamp = "\010\0\0\0mimetypeapplication/";
212
213         // compress
214         static string const compressStamp = "\037\235";
215
216         // DOS binary EPS according to Adobe TN-5002
217         static string const binEPSStamp = "\xC5\xD0\xD3\xC6";
218
219
220         // Maximum strings to read
221         int const max_count = 50;
222         int count = 0;
223
224         string str;
225         string format;
226         bool firstLine = true;
227         bool backslash = false;
228         bool maybelatex = false;
229         int dollars = 0;
230         while ((count++ < max_count) && format.empty() && !maybelatex) {
231                 if (ifs.eof())
232                         break;
233
234                 getline(ifs, str);
235                 string const stamp = str.substr(0, 2);
236                 if (firstLine && str.size() >= 2) {
237                         // at first we check for a zipped file, because this
238                         // information is saved in the first bytes of the file!
239                         // also some graphic formats which save the information
240                         // in the first line, too.
241                         if (prefixIs(str, gzipStamp)) {
242                                 format =  "gzip";
243
244                         } else if (stamp == zipStamp &&
245                                    !contains(str, nonzipStamp)) {
246                                 format =  "zip";
247
248                         } else if (stamp == compressStamp) {
249                                 format =  "compress";
250
251                         // the graphics part
252                         } else if (stamp == "BM") {
253                                 format =  "bmp";
254
255                         } else if (stamp == "\377\330") {
256                                 format =  "jpg";
257
258                         } else if (stamp == "\001\332") {
259                                 format =  "sgi";
260                         } else if (prefixIs(str, binEPSStamp)) {
261                                 format =  "eps";
262
263                         // PBM family
264                         // Don't need to use str.at(0), str.at(1) because
265                         // we already know that str.size() >= 2
266                         } else if (str[0] == 'P') {
267                                 switch (str[1]) {
268                                 case '1':
269                                 case '4':
270                                         format =  "pbm";
271                                     break;
272                                 case '2':
273                                 case '5':
274                                         format =  "pgm";
275                                     break;
276                                 case '3':
277                                 case '6':
278                                         format =  "ppm";
279                                 }
280                                 break;
281
282                         } else if ((stamp == "II") || (stamp == "MM")) {
283                                 format =  "tiff";
284
285                         } else if (prefixIs(str,"%TGIF")) {
286                                 format =  "tgif";
287
288                         } else if (prefixIs(str,"#FIG")) {
289                                 format =  "fig";
290
291                         } else if (prefixIs(str,"GIF")) {
292                                 format =  "gif";
293
294                         } else if (str.size() > 3) {
295                                 int const c = ((str[0] << 24) & (str[1] << 16) &
296                                                (str[2] << 8)  & str[3]);
297                                 if (c == 105) {
298                                         format =  "xwd";
299                                 }
300                         }
301
302                         firstLine = false;
303                 }
304
305                 if (!format.empty())
306                     break;
307                 else if (contains(str,"EPSF"))
308                         // dummy, if we have wrong file description like
309                         // %!PS-Adobe-2.0EPSF"
310                         format = "eps";
311
312                 else if (contains(str, "Grace"))
313                         format = "agr";
314
315                 else if (contains(str, "%PDF"))
316                         // autodetect pdf format for graphics inclusion
317                         format = "pdf6";
318
319                 else if (contains(str, " EMF"))
320                         format = "emf";
321
322                 else if (contains(str, "PNG"))
323                         format = "png";
324
325                 else if (contains(str, "%!PS-Adobe")) {
326                         // eps or ps
327                         ifs >> str;
328                         if (contains(str,"EPSF"))
329                                 format = "eps";
330                         else
331                             format = "ps";
332                 }
333
334                 else if (contains(str, "_bits[]"))
335                         format = "xbm";
336
337                 else if (contains(str, "XPM") || contains(str, "static char *"))
338                         format = "xpm";
339
340                 else if (contains(str, "BITPIX"))
341                         format = "fits";
342
343                 else if (contains(str, "\\documentclass") ||
344                          contains(str, "\\chapter") ||
345                          contains(str, "\\section") ||
346                          contains(str, "\\begin") ||
347                          contains(str, "\\end") ||
348                          contains(str, "$$") ||
349                          contains(str, "\\[") ||
350                          contains(str, "\\]"))
351                         maybelatex = true;
352                 else {
353                         if (contains(str, '\\'))
354                                 backslash = true;
355                         dollars += count_char(str, '$');
356                         if (backslash && dollars > 1)
357                                 // inline equation
358                                 maybelatex = true;
359                 }
360         }
361
362         if (format.empty() && maybelatex && !isBinaryFile(fn))
363                 format = "latex";
364
365         if (format.empty()) {
366                 if (ifs.eof())
367                         LYXERR(Debug::GRAPHICS, "filetools(getFormatFromContents)\n"
368                                "\tFile type not recognised before EOF!");
369         } else {
370                 LYXERR(Debug::GRAPHICS, "Recognised Fileformat: " << format);
371                 return format;
372         }
373
374         LYXERR(Debug::GRAPHICS, "filetools(getFormatFromContents)\n"
375                 << "\tCouldn't find a known format!");
376         return string();
377 }
378
379 } // namespace
380
381
382 string Formats::getFormatFromFile(FileName const & filename) const
383 {
384         if (filename.empty())
385                 return string();
386
387         string psformat;
388         string format;
389         if (filename.exists()) {
390                 // one instance of Magic that will be reused for next calls
391                 // This avoids to read the magic file everytime
392                 // If libmagic is not available, Magic::file returns an empty string.
393                 static Magic magic;
394                 string const result = magic.file(filename.toFilesystemEncoding());
395                 string const mime = token(result, ';', 0);
396                 // our own detection is better for binary files (can be anything)
397                 // and different plain text formats
398                 if (!mime.empty() && mime != "application/octet-stream" &&
399                         mime != "text/plain") {
400                         Formats::const_iterator cit =
401                                 find_if(formatlist_.begin(), formatlist_.end(),
402                                                 [mime](Format const & f){ return f.mime() == mime; });
403                         if (cit != formatlist_.end()) {
404                                 LYXERR(Debug::GRAPHICS, "\tgot format from MIME type: "
405                                            << mime << " -> " << cit->name());
406                                 // See special eps/ps handling below
407                                 if (mime == "application/postscript")
408                                         psformat = cit->name();
409                                 else
410                                         format = cit->name();
411                         }
412                 }
413
414                 // libmagic recognizes as latex also some formats of ours
415                 // such as pstex and pdftex. Therefore we have to perform
416                 // additional checks in this case (bug 9244).
417                 if (!format.empty() && format != "latex")
418                         return format;
419         }
420
421         string const ext = getExtension(filename.absFileName());
422         if (format.empty()) {
423                 // libmagic does not distinguish eps and ps.
424                 // Therefore we need to use our own detection here, but only if it
425                 // recognizes either ps or eps. Otherwise the libmagic guess will
426                 // be better (bug 9146).
427                 format = guessFormatFromContents(filename);
428                 if (!psformat.empty()) {
429                         if (isPostScriptFileFormat(format))
430                                 return format;
431                         else
432                                 return psformat;
433                 }
434
435                 if (isZippedFileFormat(format) && !ext.empty()) {
436                         string const & fmt_name = getFormatFromExtension(ext);
437                         if (!fmt_name.empty()) {
438                                 Format const * p_format = getFormat(fmt_name);
439                                 if (p_format && p_format->zippedNative())
440                                         return p_format->name();
441                         }
442                 }
443                 // Don't simply return latex (bug 9244).
444                 if (!format.empty() && format != "latex")
445                         return format;
446         }
447
448         // Both libmagic and our guessing from contents may return as latex
449         // also lyx files and our pstex and pdftex formats. In this case we
450         // give precedence to the format determined by the extension.
451         if (format == "latex") {
452                 format = getFormatFromExtension(ext);
453                 return format.empty() ? "latex" : format;
454         }
455
456         // try to find a format from the file extension.
457         return getFormatFromExtension(ext);
458 }
459
460
461 string Formats::getFormatFromExtension(string const & ext) const
462 {
463         if (!ext.empty()) {
464                 // this is ambigous if two formats have the same extension,
465                 // but better than nothing
466                 Formats::const_iterator cit =
467                         find_if(formatlist_.begin(), formatlist_.end(),
468                                 [ext](Format const & f){ return f.hasExtension(ext); });
469                 if (cit != formatlist_.end()) {
470                         LYXERR(Debug::GRAPHICS, "\twill guess format from file extension: "
471                                 << ext << " -> " << cit->name());
472                         return cit->name();
473                 }
474         }
475         return string();
476 }
477
478
479 /// Used to store last timestamp of file and whether it is (was) zipped
480 struct ZippedInfo {
481         bool zipped;
482         std::time_t timestamp;
483         ZippedInfo(bool zipped, std::time_t timestamp)
484         : zipped(zipped), timestamp(timestamp) { }
485 };
486
487
488 /// Mapping absolute pathnames of files to their ZippedInfo metadata.
489 static std::map<std::string, ZippedInfo> zipped_;
490 static Mutex zipped_mutex;
491
492
493 bool Formats::isZippedFile(support::FileName const & filename) const {
494         string const & fname = filename.absFileName();
495         time_t timestamp = filename.lastModified();
496         Mutex::Locker lock(&zipped_mutex);
497         map<string, ZippedInfo>::iterator it = zipped_.find(fname);
498         if (it != zipped_.end() && it->second.timestamp == timestamp)
499                 return it->second.zipped;
500         // FIXME perf: This very expensive function is called on startup on each
501         // file whic is going to be parsed, and also on svgz icons. Maybe there is a
502         // quicker way to check whether a file is zipped?  I.e. for icons we
503         // probably just need to check the extension (svgz vs svg).
504         string const & format = getFormatFromFile(filename);
505         bool zipped = (format == "gzip" || format == "zip");
506         zipped_.insert(make_pair(fname, ZippedInfo(zipped, timestamp)));
507         return zipped;
508 }
509
510
511 bool Formats::isZippedFileFormat(string const & format)
512 {
513         return contains("gzip zip compress", format) && !format.empty();
514 }
515
516
517 bool Formats::isPostScriptFileFormat(string const & format)
518 {
519         return format == "ps" || format == "eps";
520 }
521
522 static string fixCommand(string const & cmd, string const & ext,
523                   os::auto_open_mode mode)
524 {
525         // configure.py says we do not want a viewer/editor
526         if (cmd.empty())
527                 return cmd;
528
529         // Does the OS manage this format?
530         if (os::canAutoOpenFile(ext, mode))
531                 return "auto";
532
533         // if configure.py found nothing, clear the command
534         if (token(cmd, ' ', 0) == "auto")
535                 return string();
536
537         // use the command found by configure.py
538         return cmd;
539 }
540
541
542 void Formats::setAutoOpen()
543 {
544         FormatList::iterator fit = formatlist_.begin();
545         FormatList::iterator const fend = formatlist_.end();
546         for ( ; fit != fend ; ++fit) {
547                 fit->setViewer(fixCommand(fit->viewer(), fit->extension(), os::VIEW));
548                 fit->setEditor(fixCommand(fit->editor(), fit->extension(), os::EDIT));
549         }
550 }
551
552
553 int Formats::getNumber(string const & name) const
554 {
555         FormatList::const_iterator cit =
556                 find_if(formatlist_.begin(), formatlist_.end(),
557                         FormatNameIs(name));
558         if (cit == formatlist_.end())
559                 return -1;
560
561         return distance(formatlist_.begin(), cit);
562 }
563
564
565 void Formats::add(string const & name)
566 {
567         if (!getFormat(name))
568                 add(name, name, from_utf8(name), string(), string(), string(),
569                     string(), Format::document);
570 }
571
572
573 void Formats::add(string const & name, string const & extensions,
574                   docstring const & prettyname, string const & shortcut,
575                   string const & viewer, string const & editor,
576                   string const & mime, int flags)
577 {
578         Format * format = getFormat(name);
579         if (format)
580                 *format = Format(name, extensions, prettyname, shortcut, viewer,
581                                  editor, mime, flags);
582         else
583                 formatlist_.push_back(Format(name, extensions, prettyname,
584                                                 shortcut, viewer, editor, mime, flags));
585 }
586
587
588 void Formats::erase(string const & name)
589 {
590         FormatList::iterator it =
591                 find_if(formatlist_.begin(), formatlist_.end(),
592                         FormatNameIs(name));
593         if (it != formatlist_.end())
594                 formatlist_.erase(it);
595 }
596
597
598 void Formats::sort()
599 {
600         std::sort(formatlist_.begin(), formatlist_.end());
601 }
602
603
604 void Formats::setViewer(string const & name, string const & command)
605 {
606         add(name);
607         Format * format = getFormat(name);
608         if (format)
609                 format->setViewer(command);
610         else
611                 LYXERR0("Unable to set viewer for non-existent format: " << name);
612 }
613
614
615 void Formats::setEditor(string const & name, string const & command)
616 {
617         add(name);
618         Format * format = getFormat(name);
619         if (format)
620                 format->setEditor(command);
621         else
622                 LYXERR0("Unable to set editor for non-existent format: " << name);
623 }
624
625
626 bool Formats::view(Buffer const & buffer, FileName const & filename,
627                    string const & format_name) const
628 {
629         if (filename.empty() || !filename.exists()) {
630                 Alert::error(_("Cannot view file"),
631                         bformat(_("File does not exist: %1$s"),
632                                 from_utf8(filename.absFileName())));
633                 return false;
634         }
635
636         Format const * format = getFormat(format_name);
637         if (format && format->viewer().empty() &&
638             format->isChildFormat())
639                 format = getFormat(format->parentFormat());
640         if (!format || format->viewer().empty()) {
641 // FIXME: I believe this is the wrong place to show alerts, it should be done
642 // by the caller (this should be "utility" code)
643                 Alert::error(_("Cannot view file"),
644                         bformat(_("No information for viewing %1$s"),
645                                 translateIfPossible(prettyName(format_name))));
646                 return false;
647         }
648         // viewer is 'auto'
649         if (format->viewer() == "auto") {
650                 if (os::autoOpenFile(filename.absFileName(), os::VIEW, buffer.filePath()))
651                         return true;
652                 else {
653                         Alert::error(_("Cannot view file"),
654                                 bformat(_("Auto-view file %1$s failed"),
655                                         from_utf8(filename.absFileName())));
656                         return false;
657                 }
658         }
659
660         string command = format->viewer();
661
662         // Escape backslashes if not already in double or single quotes.
663         // We cannot simply quote the whole command as there may be arguments.
664         if (contains(command, '\\')) {
665                 bool inquote1 = false;
666                 bool inquote2 = false;
667                 string::iterator cit = command.begin();
668                 for (; cit != command.end(); ++cit) {
669                         switch (*cit) {
670                         case '"':
671                                 inquote1 = !inquote1;
672                                 break;
673                         case '\'':
674                                 inquote2 = !inquote2;
675                                 break;
676                         case '\\':
677                                 if (!inquote1 && !inquote2)
678                                         cit = ++command.insert(cit, '\\');
679                                 break;
680                         }
681                 }
682         }
683
684         if (format_name == "dvi" &&
685             !lyxrc.view_dvi_paper_option.empty()) {
686                 string paper_size = buffer.params().paperSizeName(BufferParams::XDVI);
687                 if (!paper_size.empty()) {
688                         command += ' ' + lyxrc.view_dvi_paper_option;
689                         command += ' ' + paper_size;
690                         if (buffer.params().orientation == ORIENTATION_LANDSCAPE &&
691                             buffer.params().papersize != PAPER_CUSTOM)
692                                 command += 'r';
693                 }
694         }
695
696         if (!contains(command, token_from_format))
697                 command += ' ' + token_from_format;
698
699         command = subst(command, token_from_format,
700                         quoteName(onlyFileName(filename.toFilesystemEncoding()), quote_shell_filename));
701         command = subst(command, token_path_format,
702                         quoteName(onlyPath(filename.toFilesystemEncoding()), quote_shell_filename));
703         command = subst(command, token_socket_format, quoteName(theServerSocket().address()));
704         LYXERR(Debug::FILES, "Executing command: " << command);
705         // FIXME UNICODE utf8 can be wrong for files
706         buffer.message(_("Executing command: ") + from_utf8(command));
707
708         PathChanger p(filename.onlyPath());
709         Systemcall one;
710         one.startscript(Systemcall::DontWait, command,
711                         buffer.filePath(), buffer.layoutPos());
712
713         // we can't report any sort of error, since we aren't waiting
714         return true;
715 }
716
717
718 bool Formats::edit(Buffer const & buffer, FileName const & filename,
719                          string const & format_name) const
720 {
721         if (filename.empty() || !filename.exists()) {
722                 Alert::error(_("Cannot edit file"),
723                         bformat(_("File does not exist: %1$s"),
724                                 from_utf8(filename.absFileName())));
725                 return false;
726         }
727
728         // LinkBack files look like PDF, but have the .linkback extension
729         string const ext = getExtension(filename.absFileName());
730         if (format_name == "pdf6" && ext == "linkback") {
731 #ifdef USE_MACOSX_PACKAGING
732                 return editLinkBackFile(filename.absFileName().c_str());
733 #else
734                 Alert::error(_("Cannot edit file"),
735                              _("LinkBack files can only be edited on Apple Mac OSX."));
736                 return false;
737 #endif // USE_MACOSX_PACKAGING
738         }
739
740         Format const * format = getFormat(format_name);
741         if (format && format->editor().empty() &&
742             format->isChildFormat())
743                 format = getFormat(format->parentFormat());
744         if (!format || format->editor().empty()) {
745 // FIXME: I believe this is the wrong place to show alerts, it should
746 // be done by the caller (this should be "utility" code)
747                 Alert::error(_("Cannot edit file"),
748                         bformat(_("No information for editing %1$s"),
749                                 translateIfPossible(prettyName(format_name))));
750                 return false;
751         }
752
753         // editor is 'auto'
754         if (format->editor() == "auto") {
755                 if (os::autoOpenFile(filename.absFileName(), os::EDIT, buffer.filePath()))
756                         return true;
757                 else {
758                         Alert::error(_("Cannot edit file"),
759                                 bformat(_("Auto-edit file %1$s failed"),
760                                         from_utf8(filename.absFileName())));
761                         return false;
762                 }
763         }
764
765         string command = format->editor();
766
767         if (!contains(command, token_from_format))
768                 command += ' ' + token_from_format;
769
770         command = subst(command, token_from_format,
771                         quoteName(filename.toFilesystemEncoding(), quote_shell_filename));
772         command = subst(command, token_path_format,
773                         quoteName(onlyPath(filename.toFilesystemEncoding()), quote_shell_filename));
774         command = subst(command, token_socket_format, quoteName(theServerSocket().address()));
775         LYXERR(Debug::FILES, "Executing command: " << command);
776         // FIXME UNICODE utf8 can be wrong for files
777         buffer.message(_("Executing command: ") + from_utf8(command));
778
779         Systemcall one;
780         one.startscript(Systemcall::DontWait, command,
781                         buffer.filePath(), buffer.layoutPos());
782
783         // we can't report any sort of error, since we aren't waiting
784         return true;
785 }
786
787
788 docstring const Formats::prettyName(string const & name) const
789 {
790         Format const * format = getFormat(name);
791         if (format)
792                 return format->prettyname();
793         else
794                 return from_utf8(name);
795 }
796
797
798 string const Formats::extension(string const & name) const
799 {
800         Format const * format = getFormat(name);
801         if (format)
802                 return format->extension();
803         else
804                 return name;
805 }
806
807
808 string const Formats::extensions(string const & name) const
809 {
810         Format const * format = getFormat(name);
811         if (format)
812                 return format->extensions();
813         else
814                 return name;
815 }
816
817
818 namespace {
819
820 typedef Translator<OutputParams::FLAVOR, string> FlavorTranslator;
821
822
823 FlavorTranslator initFlavorTranslator()
824 {
825         FlavorTranslator f(OutputParams::LATEX, "latex");
826         f.addPair(OutputParams::DVILUATEX, "dviluatex");
827         f.addPair(OutputParams::LUATEX, "luatex");
828         f.addPair(OutputParams::PDFLATEX, "pdflatex");
829         f.addPair(OutputParams::XETEX, "xetex");
830         f.addPair(OutputParams::DOCBOOK5, "docbook-xml");
831         f.addPair(OutputParams::HTML, "xhtml");
832         f.addPair(OutputParams::TEXT, "text");
833         f.addPair(OutputParams::LYX, "lyx");
834         return f;
835 }
836
837
838 FlavorTranslator const & flavorTranslator()
839 {
840         static FlavorTranslator const translator = initFlavorTranslator();
841         return translator;
842 }
843
844 } // namespace
845
846
847 std::string flavor2format(OutputParams::FLAVOR flavor)
848 {
849         return flavorTranslator().find(flavor);
850 }
851
852
853 /* Not currently needed, but I'll leave the code in case it is.
854 OutputParams::FLAVOR format2flavor(std::string fmt)
855 {
856         return flavorTranslator().find(fmt);
857 } */
858
859 } // namespace lyx