]> git.lyx.org Git - lyx.git/blob - src/Format.cpp
InsetInfo: enable inset dissolve
[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, " EMF"))
341                         format = "emf";
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
383         if (format.empty() && maybelatex && !isBinaryFile(fn))
384                 format = "latex";
385
386         if (format.empty()) {
387                 if (ifs.eof())
388                         LYXERR(Debug::GRAPHICS, "filetools(getFormatFromContents)\n"
389                                "\tFile type not recognised before EOF!");
390         } else {
391                 LYXERR(Debug::GRAPHICS, "Recognised Fileformat: " << format);
392                 return format;
393         }
394
395         LYXERR(Debug::GRAPHICS, "filetools(getFormatFromContents)\n"
396                 << "\tCouldn't find a known format!");
397         return string();
398 }
399
400 } // namespace
401
402
403 string Formats::getFormatFromFile(FileName const & filename) const
404 {
405         if (filename.empty())
406                 return string();
407
408         string psformat;
409         string format;
410         if (filename.exists()) {
411                 // one instance of Magic that will be reused for next calls
412                 // This avoids to read the magic file everytime
413                 // If libmagic is not available, Magic::file returns an empty string.
414                 static Magic magic;
415                 string const result = magic.file(filename.toFilesystemEncoding());
416                 string const mime = token(result, ';', 0);
417                 // our own detection is better for binary files (can be anything)
418                 // and different plain text formats
419                 if (!mime.empty() && mime != "application/octet-stream" &&
420                         mime != "text/plain") {
421                         Formats::const_iterator cit =
422                                 find_if(formatlist_.begin(), formatlist_.end(),
423                                                 FormatMimeEqual(mime));
424                         if (cit != formatlist_.end()) {
425                                 LYXERR(Debug::GRAPHICS, "\tgot format from MIME type: "
426                                            << mime << " -> " << cit->name());
427                                 // See special eps/ps handling below
428                                 if (mime == "application/postscript")
429                                         psformat = cit->name();
430                                 else
431                                         format = cit->name();
432                         }
433                 }
434
435                 // libmagic recognizes as latex also some formats of ours
436                 // such as pstex and pdftex. Therefore we have to perform
437                 // additional checks in this case (bug 9244).
438                 if (!format.empty() && format != "latex")
439                         return format;
440         }
441
442         string const ext = getExtension(filename.absFileName());
443         if (format.empty()) {
444                 // libmagic does not distinguish eps and ps.
445                 // Therefore we need to use our own detection here, but only if it
446                 // recognizes either ps or eps. Otherwise the libmagic guess will
447                 // be better (bug 9146).
448                 format = guessFormatFromContents(filename);
449                 if (!psformat.empty()) {
450                         if (isPostScriptFileFormat(format))
451                                 return format;
452                         else
453                                 return psformat;
454                 }
455
456                 if (isZippedFileFormat(format) && !ext.empty()) {
457                         string const & fmt_name = getFormatFromExtension(ext);
458                         if (!fmt_name.empty()) {
459                                 Format const * p_format = getFormat(fmt_name);
460                                 if (p_format && p_format->zippedNative())
461                                         return p_format->name();
462                         }
463                 }
464                 // Don't simply return latex (bug 9244).
465                 if (!format.empty() && format != "latex")
466                         return format;
467         }
468
469         // Both libmagic and our guessing from contents may return as latex
470         // also lyx files and our pstex and pdftex formats. In this case we
471         // give precedence to the format determined by the extension.
472         if (format == "latex") {
473                 format = getFormatFromExtension(ext);
474                 return format.empty() ? "latex" : format;
475         }
476
477         // try to find a format from the file extension.
478         return getFormatFromExtension(ext);
479 }
480
481
482 string Formats::getFormatFromExtension(string const & ext) const
483 {
484         if (!ext.empty()) {
485                 // this is ambigous if two formats have the same extension,
486                 // but better than nothing
487                 Formats::const_iterator cit =
488                         find_if(formatlist_.begin(), formatlist_.end(),
489                                 FormatExtensionsEqual(ext));
490                 if (cit != formatlist_.end()) {
491                         LYXERR(Debug::GRAPHICS, "\twill guess format from file extension: "
492                                 << ext << " -> " << cit->name());
493                         return cit->name();
494                 }
495         }
496         return string();
497 }
498
499
500 /// Used to store last timestamp of file and whether it is (was) zipped
501 struct ZippedInfo {
502         bool zipped;
503         std::time_t timestamp;
504         ZippedInfo(bool zipped, std::time_t timestamp)
505         : zipped(zipped), timestamp(timestamp) { }
506 };
507
508
509 /// Mapping absolute pathnames of files to their ZippedInfo metadata.
510 static std::map<std::string, ZippedInfo> zipped_;
511 static Mutex zipped_mutex;
512
513
514 bool Formats::isZippedFile(support::FileName const & filename) const {
515         string const & fname = filename.absFileName();
516         time_t timestamp = filename.lastModified();
517         Mutex::Locker lock(&zipped_mutex);
518         map<string, ZippedInfo>::iterator it = zipped_.find(fname);
519         if (it != zipped_.end() && it->second.timestamp == timestamp)
520                 return it->second.zipped;
521         // FIXME perf: This very expensive function is called on startup on each
522         // file whic is going to be parsed, and also on svgz icons. Maybe there is a
523         // quicker way to check whether a file is zipped?  I.e. for icons we
524         // probably just need to check the extension (svgz vs svg).
525         string const & format = getFormatFromFile(filename);
526         bool zipped = (format == "gzip" || format == "zip");
527         zipped_.insert(make_pair(fname, ZippedInfo(zipped, timestamp)));
528         return zipped;
529 }
530
531
532 bool Formats::isZippedFileFormat(string const & format)
533 {
534         return contains("gzip zip compress", format) && !format.empty();
535 }
536
537
538 bool Formats::isPostScriptFileFormat(string const & format)
539 {
540         return format == "ps" || format == "eps";
541 }
542
543 static string fixCommand(string const & cmd, string const & ext,
544                   os::auto_open_mode mode)
545 {
546         // configure.py says we do not want a viewer/editor
547         if (cmd.empty())
548                 return cmd;
549
550         // Does the OS manage this format?
551         if (os::canAutoOpenFile(ext, mode))
552                 return "auto";
553
554         // if configure.py found nothing, clear the command
555         if (token(cmd, ' ', 0) == "auto")
556                 return string();
557
558         // use the command found by configure.py
559         return cmd;
560 }
561
562
563 void Formats::setAutoOpen()
564 {
565         FormatList::iterator fit = formatlist_.begin();
566         FormatList::iterator const fend = formatlist_.end();
567         for ( ; fit != fend ; ++fit) {
568                 fit->setViewer(fixCommand(fit->viewer(), fit->extension(), os::VIEW));
569                 fit->setEditor(fixCommand(fit->editor(), fit->extension(), os::EDIT));
570         }
571 }
572
573
574 int Formats::getNumber(string const & name) const
575 {
576         FormatList::const_iterator cit =
577                 find_if(formatlist_.begin(), formatlist_.end(),
578                         FormatNamesEqual(name));
579         if (cit != formatlist_.end())
580                 return distance(formatlist_.begin(), cit);
581         else
582                 return -1;
583 }
584
585
586 void Formats::add(string const & name)
587 {
588         if (!getFormat(name))
589                 add(name, name, from_utf8(name), string(), string(), string(),
590                     string(), Format::document);
591 }
592
593
594 void Formats::add(string const & name, string const & extensions,
595                   docstring const & prettyname, string const & shortcut,
596                   string const & viewer, string const & editor,
597                   string const & mime, int flags)
598 {
599         FormatList::iterator it =
600                 find_if(formatlist_.begin(), formatlist_.end(),
601                         FormatNamesEqual(name));
602         if (it == formatlist_.end())
603                 formatlist_.push_back(Format(name, extensions, prettyname,
604                                             shortcut, viewer, editor, mime, flags));
605         else
606                 *it = Format(name, extensions, prettyname, shortcut, viewer,
607                              editor, mime, flags);
608 }
609
610
611 void Formats::erase(string const & name)
612 {
613         FormatList::iterator it =
614                 find_if(formatlist_.begin(), formatlist_.end(),
615                         FormatNamesEqual(name));
616         if (it != formatlist_.end())
617                 formatlist_.erase(it);
618 }
619
620
621 void Formats::sort()
622 {
623         std::sort(formatlist_.begin(), formatlist_.end());
624 }
625
626
627 void Formats::setViewer(string const & name, string const & command)
628 {
629         add(name);
630         FormatList::iterator it =
631                 find_if(formatlist_.begin(), formatlist_.end(),
632                         FormatNamesEqual(name));
633         if (it != formatlist_.end())
634                 it->setViewer(command);
635 }
636
637
638 void Formats::setEditor(string const & name, string const & command)
639 {
640         add(name);
641         FormatList::iterator it =
642                 find_if(formatlist_.begin(), formatlist_.end(),
643                         FormatNamesEqual(name));
644         if (it != formatlist_.end())
645                 it->setEditor(command);
646 }
647
648
649 bool Formats::view(Buffer const & buffer, FileName const & filename,
650                    string const & format_name) const
651 {
652         if (filename.empty() || !filename.exists()) {
653                 Alert::error(_("Cannot view file"),
654                         bformat(_("File does not exist: %1$s"),
655                                 from_utf8(filename.absFileName())));
656                 return false;
657         }
658
659         Format const * format = getFormat(format_name);
660         if (format && format->viewer().empty() &&
661             format->isChildFormat())
662                 format = getFormat(format->parentFormat());
663         if (!format || format->viewer().empty()) {
664 // FIXME: I believe this is the wrong place to show alerts, it should be done
665 // by the caller (this should be "utility" code)
666                 Alert::error(_("Cannot view file"),
667                         bformat(_("No information for viewing %1$s"),
668                                 prettyName(format_name)));
669                 return false;
670         }
671         // viewer is 'auto'
672         if (format->viewer() == "auto") {
673                 if (os::autoOpenFile(filename.absFileName(), os::VIEW, buffer.filePath()))
674                         return true;
675                 else {
676                         Alert::error(_("Cannot view file"),
677                                 bformat(_("Auto-view file %1$s failed"),
678                                         from_utf8(filename.absFileName())));
679                         return false;
680                 }
681         }
682
683         string command = format->viewer();
684
685         if (format_name == "dvi" &&
686             !lyxrc.view_dvi_paper_option.empty()) {
687                 string paper_size = buffer.params().paperSizeName(BufferParams::XDVI);
688                 if (!paper_size.empty()) {
689                         command += ' ' + lyxrc.view_dvi_paper_option;
690                         command += ' ' + paper_size;
691                         if (buffer.params().orientation == ORIENTATION_LANDSCAPE &&
692                             buffer.params().papersize != PAPER_CUSTOM)
693                                 command += 'r';
694                 }
695         }
696
697         if (!contains(command, token_from_format))
698                 command += ' ' + token_from_format;
699
700         command = subst(command, token_from_format,
701                         quoteName(onlyFileName(filename.toFilesystemEncoding()), quote_shell_filename));
702         command = subst(command, token_path_format,
703                         quoteName(onlyPath(filename.toFilesystemEncoding()), quote_shell_filename));
704         command = subst(command, token_socket_format, quoteName(theServerSocket().address()));
705         LYXERR(Debug::FILES, "Executing command: " << command);
706         // FIXME UNICODE utf8 can be wrong for files
707         buffer.message(_("Executing command: ") + from_utf8(command));
708
709         PathChanger p(filename.onlyPath());
710         Systemcall one;
711         one.startscript(Systemcall::DontWait, command,
712                         buffer.filePath(), buffer.layoutPos());
713
714         // we can't report any sort of error, since we aren't waiting
715         return true;
716 }
717
718
719 bool Formats::edit(Buffer const & buffer, FileName const & filename,
720                          string const & format_name) const
721 {
722         if (filename.empty() || !filename.exists()) {
723                 Alert::error(_("Cannot edit file"),
724                         bformat(_("File does not exist: %1$s"),
725                                 from_utf8(filename.absFileName())));
726                 return false;
727         }
728
729         // LinkBack files look like PDF, but have the .linkback extension
730         string const ext = getExtension(filename.absFileName());
731         if (format_name == "pdf6" && ext == "linkback") {
732 #ifdef USE_MACOSX_PACKAGING
733                 return editLinkBackFile(filename.absFileName().c_str());
734 #else
735                 Alert::error(_("Cannot edit file"),
736                              _("LinkBack files can only be edited on Apple Mac OSX."));
737                 return false;
738 #endif // USE_MACOSX_PACKAGING
739         }
740
741         Format const * format = getFormat(format_name);
742         if (format && format->editor().empty() &&
743             format->isChildFormat())
744                 format = getFormat(format->parentFormat());
745         if (!format || format->editor().empty()) {
746 // FIXME: I believe this is the wrong place to show alerts, it should
747 // be done by the caller (this should be "utility" code)
748                 Alert::error(_("Cannot edit file"),
749                         bformat(_("No information for editing %1$s"),
750                                 prettyName(format_name)));
751                 return false;
752         }
753
754         // editor is 'auto'
755         if (format->editor() == "auto") {
756                 if (os::autoOpenFile(filename.absFileName(), os::EDIT, buffer.filePath()))
757                         return true;
758                 else {
759                         Alert::error(_("Cannot edit file"),
760                                 bformat(_("Auto-edit file %1$s failed"),
761                                         from_utf8(filename.absFileName())));
762                         return false;
763                 }
764         }
765
766         string command = format->editor();
767
768         if (!contains(command, token_from_format))
769                 command += ' ' + token_from_format;
770
771         command = subst(command, token_from_format,
772                         quoteName(filename.toFilesystemEncoding(), quote_shell_filename));
773         command = subst(command, token_path_format,
774                         quoteName(onlyPath(filename.toFilesystemEncoding()), quote_shell_filename));
775         command = subst(command, token_socket_format, quoteName(theServerSocket().address()));
776         LYXERR(Debug::FILES, "Executing command: " << command);
777         // FIXME UNICODE utf8 can be wrong for files
778         buffer.message(_("Executing command: ") + from_utf8(command));
779
780         Systemcall one;
781         one.startscript(Systemcall::DontWait, command,
782                         buffer.filePath(), buffer.layoutPos());
783
784         // we can't report any sort of error, since we aren't waiting
785         return true;
786 }
787
788
789 docstring const Formats::prettyName(string const & name) const
790 {
791         Format const * format = getFormat(name);
792         if (format)
793                 return format->prettyname();
794         else
795                 return from_utf8(name);
796 }
797
798
799 string const Formats::extension(string const & name) const
800 {
801         Format const * format = getFormat(name);
802         if (format)
803                 return format->extension();
804         else
805                 return name;
806 }
807
808
809 string const Formats::extensions(string const & name) const
810 {
811         Format const * format = getFormat(name);
812         if (format)
813                 return format->extensions();
814         else
815                 return name;
816 }
817
818
819 namespace {
820
821 typedef Translator<OutputParams::FLAVOR, string> FlavorTranslator;
822
823
824 FlavorTranslator initFlavorTranslator()
825 {
826         FlavorTranslator f(OutputParams::LATEX, "latex");
827         f.addPair(OutputParams::DVILUATEX, "dviluatex");
828         f.addPair(OutputParams::LUATEX, "luatex");
829         f.addPair(OutputParams::PDFLATEX, "pdflatex");
830         f.addPair(OutputParams::XETEX, "xetex");
831         f.addPair(OutputParams::XML, "docbook-xml");
832         f.addPair(OutputParams::HTML, "xhtml");
833         f.addPair(OutputParams::TEXT, "text");
834         f.addPair(OutputParams::LYX, "lyx");
835         return f;
836 }
837
838
839 FlavorTranslator const & flavorTranslator()
840 {
841         static FlavorTranslator const translator = initFlavorTranslator();
842         return translator;
843 }
844
845 } // namespace
846
847
848 std::string flavor2format(OutputParams::FLAVOR flavor)
849 {
850         return flavorTranslator().find(flavor);
851 }
852
853
854 /* Not currently needed, but I'll leave the code in case it is.
855 OutputParams::FLAVOR format2flavor(std::string fmt)
856 {
857         return flavorTranslator().find(fmt);
858 } */
859
860 } // namespace lyx