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