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