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