]> git.lyx.org Git - lyx.git/blob - src/Format.cpp
Convert cursorX() and computeRowMetrics() to the new scheme
[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         string str;
245         string format;
246         bool firstLine = true;
247         bool backslash = false;
248         bool maybelatex = false;
249         int dollars = 0;
250         while ((count++ < max_count) && format.empty() && !maybelatex) {
251                 if (ifs.eof())
252                         break;
253
254                 getline(ifs, str);
255                 string const stamp = str.substr(0, 2);
256                 if (firstLine && str.size() >= 2) {
257                         // at first we check for a zipped file, because this
258                         // information is saved in the first bytes of the file!
259                         // also some graphic formats which save the information
260                         // in the first line, too.
261                         if (prefixIs(str, gzipStamp)) {
262                                 format =  "gzip";
263
264                         } else if (stamp == zipStamp &&
265                                    !contains(str, nonzipStamp)) {
266                                 format =  "zip";
267
268                         } else if (stamp == compressStamp) {
269                                 format =  "compress";
270
271                         // the graphics part
272                         } else if (stamp == "BM") {
273                                 format =  "bmp";
274
275                         } else if (stamp == "\377\330") {
276                                 format =  "jpg";
277
278                         } else if (stamp == "\001\332") {
279                                 format =  "sgi";
280                         } else if (prefixIs(str, binEPSStamp)) {
281                                 format =  "eps";
282
283                         // PBM family
284                         // Don't need to use str.at(0), str.at(1) because
285                         // we already know that str.size() >= 2
286                         } else if (str[0] == 'P') {
287                                 switch (str[1]) {
288                                 case '1':
289                                 case '4':
290                                         format =  "pbm";
291                                     break;
292                                 case '2':
293                                 case '5':
294                                         format =  "pgm";
295                                     break;
296                                 case '3':
297                                 case '6':
298                                         format =  "ppm";
299                                 }
300                                 break;
301
302                         } else if ((stamp == "II") || (stamp == "MM")) {
303                                 format =  "tiff";
304
305                         } else if (prefixIs(str,"%TGIF")) {
306                                 format =  "tgif";
307
308                         } else if (prefixIs(str,"#FIG")) {
309                                 format =  "fig";
310
311                         } else if (prefixIs(str,"GIF")) {
312                                 format =  "gif";
313
314                         } else if (str.size() > 3) {
315                                 int const c = ((str[0] << 24) & (str[1] << 16) &
316                                                (str[2] << 8)  & str[3]);
317                                 if (c == 105) {
318                                         format =  "xwd";
319                                 }
320                         }
321
322                         firstLine = false;
323                 }
324
325                 if (!format.empty())
326                     break;
327                 else if (contains(str,"EPSF"))
328                         // dummy, if we have wrong file description like
329                         // %!PS-Adobe-2.0EPSF"
330                         format = "eps";
331
332                 else if (contains(str, "Grace"))
333                         format = "agr";
334
335                 else if (contains(str, "%PDF"))
336                         // autodetect pdf format for graphics inclusion
337                         format = "pdf6";
338
339                 else if (contains(str, "PNG"))
340                         format = "png";
341
342                 else if (contains(str, "%!PS-Adobe")) {
343                         // eps or ps
344                         ifs >> str;
345                         if (contains(str,"EPSF"))
346                                 format = "eps";
347                         else
348                             format = "ps";
349                 }
350
351                 else if (contains(str, "_bits[]"))
352                         format = "xbm";
353
354                 else if (contains(str, "XPM") || contains(str, "static char *"))
355                         format = "xpm";
356
357                 else if (contains(str, "BITPIX"))
358                         format = "fits";
359
360                 else if (contains(str, "\\documentclass") ||
361                          contains(str, "\\chapter") ||
362                          contains(str, "\\section") ||
363                          contains(str, "\\begin") ||
364                          contains(str, "\\end") ||
365                          contains(str, "$$") ||
366                          contains(str, "\\[") ||
367                          contains(str, "\\]"))
368                         maybelatex = true;
369                 else {
370                         if (contains(str, '\\'))
371                                 backslash = true;
372                         dollars += count_char(str, '$');
373                         if (backslash && dollars > 1)
374                                 // inline equation
375                                 maybelatex = true;
376                 }
377         }
378
379         if (format.empty() && maybelatex && !isBinaryFile(fn))
380                 format = "latex";
381
382         if (format.empty()) {
383                 if (ifs.eof())
384                         LYXERR(Debug::GRAPHICS, "filetools(getFormatFromContents)\n"
385                                "\tFile type not recognised before EOF!");
386         } else {
387                 LYXERR(Debug::GRAPHICS, "Recognised Fileformat: " << format);
388                 return format;
389         }
390
391         LYXERR(Debug::GRAPHICS, "filetools(getFormatFromContents)\n"
392                 << "\tCouldn't find a known format!");
393         return string();
394 }
395
396 }
397
398
399 string Formats::getFormatFromFile(FileName const & filename) const
400 {
401         if (filename.empty())
402                 return string();
403
404         string psformat;
405 #ifdef HAVE_MAGIC_H
406         if (filename.exists()) {
407                 magic_t magic_cookie = magic_open(MAGIC_MIME);
408                 if (magic_cookie) {
409                         string format;
410                         if (magic_load(magic_cookie, NULL) != 0) {
411                                 LYXERR(Debug::GRAPHICS, "Formats::getFormatFromFile\n"
412                                         << "\tCouldn't load magic database - "
413                                         << magic_error(magic_cookie));
414                         } else {
415                                 char const * result = magic_file(magic_cookie,
416                                         filename.toFilesystemEncoding().c_str());
417                                 string mime;
418                                 if (result)
419                                         mime = token(result, ';', 0);
420                                 else {
421                                         LYXERR(Debug::GRAPHICS, "Formats::getFormatFromFile\n"
422                                                 << "\tCouldn't query magic database - "
423                                                 << magic_error(magic_cookie));
424                                 }
425                                 // our own detection is better for binary files (can be anything)
426                                 // and different plain text formats
427                                 if (!mime.empty() && mime != "application/octet-stream" &&
428                                     mime != "text/plain") {
429                                         Formats::const_iterator cit =
430                                                 find_if(formatlist.begin(), formatlist.end(),
431                                                         FormatMimeEqual(mime));
432                                         if (cit != formats.end()) {
433                                                 LYXERR(Debug::GRAPHICS, "\tgot format from MIME type: "
434                                                         << mime << " -> " << cit->name());
435                                                 // See special eps/ps handling below
436                                                 if (mime == "application/postscript")
437                                                         psformat = cit->name();
438                                                 else
439                                                         format = cit->name();
440                                         }
441                                 }
442                         }
443                         magic_close(magic_cookie);
444                         if (!format.empty())
445                                 return format;
446                 }
447         }
448 #endif
449
450         // libmagic does not distinguish eps and ps.
451         // Therefore we need to use our own detection here, but only if it
452         // recognizes either ps or eps. Otherwise the libmagic guess will
453         // be better (bug 9146).
454         string const format = guessFormatFromContents(filename);
455         if (!psformat.empty()) {
456                 if (isPostScriptFileFormat(format))
457                         return format;
458                 else
459                         return psformat;
460         }
461
462         string const ext = getExtension(filename.absFileName());
463         if (isZippedFileFormat(format) && !ext.empty()) {
464                 string const & fmt_name = formats.getFormatFromExtension(ext);
465                 if (!fmt_name.empty()) {
466                         Format const * p_format = formats.getFormat(fmt_name);
467                         if (p_format && p_format->zippedNative())
468                                 return p_format->name();
469                 }
470         }
471         if (!format.empty())
472                 return format;
473
474         // try to find a format from the file extension.
475         return getFormatFromExtension(ext);
476 }
477
478
479 string Formats::getFormatFromExtension(string const & ext) const
480 {
481         if (!ext.empty()) {
482                 // this is ambigous if two formats have the same extension,
483                 // but better than nothing
484                 Formats::const_iterator cit =
485                         find_if(formatlist.begin(), formatlist.end(),
486                                 FormatExtensionsEqual(ext));
487                 if (cit != formats.end()) {
488                         LYXERR(Debug::GRAPHICS, "\twill guess format from file extension: "
489                                 << ext << " -> " << cit->name());
490                         return cit->name();
491                 }
492         }
493         return string();
494 }
495
496
497 /// Used to store last timestamp of file and whether it is (was) zipped
498 struct ZippedInfo {
499         bool zipped;
500         std::time_t timestamp;
501         ZippedInfo(bool zipped, std::time_t timestamp)
502         : zipped(zipped), timestamp(timestamp) { }
503 };
504
505
506 // FIXME THREAD
507 /// Mapping absolute pathnames of files to their ZippedInfo metadata.
508 static std::map<std::string, ZippedInfo> zipped_;
509
510
511 bool Formats::isZippedFile(support::FileName const & filename) const {
512         string const & fname = filename.absFileName();
513         time_t timestamp = filename.lastModified();
514         map<string, ZippedInfo>::iterator it = zipped_.find(fname);
515         if (it != zipped_.end() && it->second.timestamp == timestamp)
516                 return it->second.zipped;
517         string const & format = getFormatFromFile(filename);
518         bool zipped = (format == "gzip" || format == "zip");
519         zipped_.insert(make_pair(fname, ZippedInfo(zipped, timestamp)));
520         return zipped;
521 }
522
523
524 bool Formats::isZippedFileFormat(string const & format)
525 {
526         return contains("gzip zip compress", format) && !format.empty();
527 }
528
529
530 bool Formats::isPostScriptFileFormat(string const & format)
531 {
532         return format == "ps" || format == "eps";
533 }
534
535 static string fixCommand(string const & cmd, string const & ext,
536                   os::auto_open_mode mode)
537 {
538         // configure.py says we do not want a viewer/editor
539         if (cmd.empty())
540                 return cmd;
541
542         // Does the OS manage this format?
543         if (os::canAutoOpenFile(ext, mode))
544                 return "auto";
545
546         // if configure.py found nothing, clear the command
547         if (token(cmd, ' ', 0) == "auto")
548                 return string();
549
550         // use the command found by configure.py
551         return cmd;
552 }
553
554
555 void Formats::setAutoOpen()
556 {
557         FormatList::iterator fit = formatlist.begin();
558         FormatList::iterator const fend = formatlist.end();
559         for ( ; fit != fend ; ++fit) {
560                 fit->setViewer(fixCommand(fit->viewer(), fit->extension(), os::VIEW));
561                 fit->setEditor(fixCommand(fit->editor(), fit->extension(), os::EDIT));
562         }
563 }
564
565
566 int Formats::getNumber(string const & name) const
567 {
568         FormatList::const_iterator cit =
569                 find_if(formatlist.begin(), formatlist.end(),
570                         FormatNamesEqual(name));
571         if (cit != formatlist.end())
572                 return distance(formatlist.begin(), cit);
573         else
574                 return -1;
575 }
576
577
578 void Formats::add(string const & name)
579 {
580         if (!getFormat(name))
581                 add(name, name, name, string(), string(), string(),
582                     string(), Format::document);
583 }
584
585
586 void Formats::add(string const & name, string const & extensions,
587                   string const & prettyname, string const & shortcut,
588                   string const & viewer, string const & editor,
589                   string const & mime, int flags)
590 {
591         FormatList::iterator it =
592                 find_if(formatlist.begin(), formatlist.end(),
593                         FormatNamesEqual(name));
594         if (it == formatlist.end())
595                 formatlist.push_back(Format(name, extensions, prettyname,
596                                             shortcut, viewer, editor, mime, flags));
597         else
598                 *it = Format(name, extensions, prettyname, shortcut, viewer,
599                              editor, mime, flags);
600 }
601
602
603 void Formats::erase(string const & name)
604 {
605         FormatList::iterator it =
606                 find_if(formatlist.begin(), formatlist.end(),
607                         FormatNamesEqual(name));
608         if (it != formatlist.end())
609                 formatlist.erase(it);
610 }
611
612
613 void Formats::sort()
614 {
615         std::sort(formatlist.begin(), formatlist.end());
616 }
617
618
619 void Formats::setViewer(string const & name, string const & command)
620 {
621         add(name);
622         FormatList::iterator it =
623                 find_if(formatlist.begin(), formatlist.end(),
624                         FormatNamesEqual(name));
625         if (it != formatlist.end())
626                 it->setViewer(command);
627 }
628
629
630 void Formats::setEditor(string const & name, string const & command)
631 {
632         add(name);
633         FormatList::iterator it =
634                 find_if(formatlist.begin(), formatlist.end(),
635                         FormatNamesEqual(name));
636         if (it != formatlist.end())
637                 it->setEditor(command);
638 }
639
640
641 bool Formats::view(Buffer const & buffer, FileName const & filename,
642                    string const & format_name) const
643 {
644         if (filename.empty() || !filename.exists()) {
645                 Alert::error(_("Cannot view file"),
646                         bformat(_("File does not exist: %1$s"),
647                                 from_utf8(filename.absFileName())));
648                 return false;
649         }
650
651         Format const * format = getFormat(format_name);
652         if (format && format->viewer().empty() &&
653             format->isChildFormat())
654                 format = getFormat(format->parentFormat());
655         if (!format || format->viewer().empty()) {
656 // FIXME: I believe this is the wrong place to show alerts, it should be done
657 // by the caller (this should be "utility" code)
658                 Alert::error(_("Cannot view file"),
659                         bformat(_("No information for viewing %1$s"),
660                                 prettyName(format_name)));
661                 return false;
662         }
663         // viewer is 'auto'
664         if (format->viewer() == "auto") {
665                 if (os::autoOpenFile(filename.absFileName(), os::VIEW, buffer.filePath()))
666                         return true;
667                 else {
668                         Alert::error(_("Cannot view file"),
669                                 bformat(_("Auto-view file %1$s failed"),
670                                         from_utf8(filename.absFileName())));
671                         return false;
672                 }
673         }
674
675         string command = format->viewer();
676
677         if (format_name == "dvi" &&
678             !lyxrc.view_dvi_paper_option.empty()) {
679                 string paper_size = buffer.params().paperSizeName(BufferParams::XDVI);
680                 if (!paper_size.empty()) {
681                         command += ' ' + lyxrc.view_dvi_paper_option;
682                         command += ' ' + paper_size;
683                         if (buffer.params().orientation == ORIENTATION_LANDSCAPE &&
684                             buffer.params().papersize != PAPER_CUSTOM)
685                                 command += 'r';
686                 }
687         }
688
689         if (!contains(command, token_from_format))
690                 command += ' ' + token_from_format;
691
692         command = subst(command, token_from_format, quoteName(onlyFileName(filename.toFilesystemEncoding())));
693         command = subst(command, token_path_format, quoteName(onlyPath(filename.toFilesystemEncoding())));
694         command = subst(command, token_socket_format, quoteName(theServerSocket().address()));
695         LYXERR(Debug::FILES, "Executing command: " << command);
696         // FIXME UNICODE utf8 can be wrong for files
697         buffer.message(_("Executing command: ") + from_utf8(command));
698
699         PathChanger p(filename.onlyPath());
700         Systemcall one;
701         one.startscript(Systemcall::DontWait, command, buffer.filePath());
702
703         // we can't report any sort of error, since we aren't waiting
704         return true;
705 }
706
707
708 bool Formats::edit(Buffer const & buffer, FileName const & filename,
709                          string const & format_name) const
710 {
711         if (filename.empty() || !filename.exists()) {
712                 Alert::error(_("Cannot edit file"),
713                         bformat(_("File does not exist: %1$s"),
714                                 from_utf8(filename.absFileName())));
715                 return false;
716         }
717
718         // LinkBack files look like PDF, but have the .linkback extension
719         string const ext = getExtension(filename.absFileName());
720         if (format_name == "pdf6" && ext == "linkback") {
721 #ifdef USE_MACOSX_PACKAGING
722                 return editLinkBackFile(filename.absFileName().c_str());
723 #else
724                 Alert::error(_("Cannot edit file"),
725                              _("LinkBack files can only be edited on Apple Mac OSX."));
726                 return false;
727 #endif // USE_MACOSX_PACKAGING
728         }
729
730         Format const * format = getFormat(format_name);
731         if (format && format->editor().empty() &&
732             format->isChildFormat())
733                 format = getFormat(format->parentFormat());
734         if (!format || format->editor().empty()) {
735 // FIXME: I believe this is the wrong place to show alerts, it should
736 // be done by the caller (this should be "utility" code)
737                 Alert::error(_("Cannot edit file"),
738                         bformat(_("No information for editing %1$s"),
739                                 prettyName(format_name)));
740                 return false;
741         }
742
743         // editor is 'auto'
744         if (format->editor() == "auto") {
745                 if (os::autoOpenFile(filename.absFileName(), os::EDIT, buffer.filePath()))
746                         return true;
747                 else {
748                         Alert::error(_("Cannot edit file"),
749                                 bformat(_("Auto-edit file %1$s failed"),
750                                         from_utf8(filename.absFileName())));
751                         return false;
752                 }
753         }
754
755         string command = format->editor();
756
757         if (!contains(command, token_from_format))
758                 command += ' ' + token_from_format;
759
760         command = subst(command, token_from_format, quoteName(filename.toFilesystemEncoding()));
761         command = subst(command, token_path_format, quoteName(onlyPath(filename.toFilesystemEncoding())));
762         command = subst(command, token_socket_format, quoteName(theServerSocket().address()));
763         LYXERR(Debug::FILES, "Executing command: " << command);
764         // FIXME UNICODE utf8 can be wrong for files
765         buffer.message(_("Executing command: ") + from_utf8(command));
766
767         Systemcall one;
768         one.startscript(Systemcall::DontWait, command, buffer.filePath());
769
770         // we can't report any sort of error, since we aren't waiting
771         return true;
772 }
773
774
775 docstring const Formats::prettyName(string const & name) const
776 {
777         Format const * format = getFormat(name);
778         if (format)
779                 return from_utf8(format->prettyname());
780         else
781                 return from_utf8(name);
782 }
783
784
785 string const Formats::extension(string const & name) const
786 {
787         Format const * format = getFormat(name);
788         if (format)
789                 return format->extension();
790         else
791                 return name;
792 }
793
794
795 string const Formats::extensions(string const & name) const
796 {
797         Format const * format = getFormat(name);
798         if (format)
799                 return format->extensions();
800         else
801                 return name;
802 }
803
804
805 namespace {
806
807 typedef Translator<OutputParams::FLAVOR, string> FlavorTranslator;
808
809
810 FlavorTranslator initFlavorTranslator()
811 {
812         FlavorTranslator f(OutputParams::LATEX, "latex");
813         f.addPair(OutputParams::DVILUATEX, "dviluatex");
814         f.addPair(OutputParams::LUATEX, "luatex");
815         f.addPair(OutputParams::PDFLATEX, "pdflatex");
816         f.addPair(OutputParams::XETEX, "xetex");
817         f.addPair(OutputParams::XML, "docbook-xml");
818         f.addPair(OutputParams::HTML, "xhtml");
819         f.addPair(OutputParams::TEXT, "text");
820         return f;
821 }
822
823
824 FlavorTranslator const & flavorTranslator()
825 {
826         static FlavorTranslator const translator = initFlavorTranslator();
827         return translator;
828 }
829
830 }
831
832
833 std::string flavor2format(OutputParams::FLAVOR flavor)
834 {
835         return flavorTranslator().find(flavor);
836 }
837
838
839 /* Not currently needed, but I'll leave the code in case it is.
840 OutputParams::FLAVOR format2flavor(std::string fmt)
841 {
842         return flavorTranslator().find(fmt);
843 } */
844
845 Formats formats;
846
847 Formats system_formats;
848
849
850 } // namespace lyx