3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
15 #include "BufferParams.h"
17 #include "ServerSocket.h"
19 #include "frontends/alert.h" //to be removed?
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/Path.h"
27 #include "support/Systemcall.h"
28 #include "support/textutils.h"
29 #include "support/Translator.h"
35 // FIXME: Q_WS_MACX is not available, it's in Qt
36 #ifdef USE_MACOSX_PACKAGING
37 #include "support/linkback/LinkBackProxy.h"
41 using namespace lyx::support;
45 namespace Alert = frontend::Alert;
46 namespace os = support::os;
50 string const token_from_format("$$i");
51 string const token_path_format("$$p");
52 string const token_socket_format("$$a");
55 class FormatNamesEqual : public unary_function<Format, bool> {
57 FormatNamesEqual(string const & name)
59 bool operator()(Format const & f) const
61 return f.name() == name_;
68 class FormatExtensionsEqual : public unary_function<Format, bool> {
70 FormatExtensionsEqual(string const & extension)
71 : extension_(extension) {}
72 bool operator()(Format const & f) const
74 return f.hasExtension(extension_);
83 bool operator<(Format const & a, Format const & b)
85 // use the compare_ascii_no_case instead of compare_no_case,
86 // because in turkish, 'i' is not the lowercase version of 'I',
87 // and thus turkish locale breaks parsing of tags.
89 return compare_ascii_no_case(a.prettyname(), b.prettyname()) < 0;
93 Format::Format(string const & n, string const & e, string const & p,
94 string const & s, string const & v, string const & ed,
96 : name_(n), prettyname_(p), shortcut_(s), viewer_(v),
97 editor_(ed), flags_(flags)
99 extension_list_ = getVectorFromString(e, ",");
100 LYXERR(Debug::GRAPHICS, "New Format: n=" << n << ", flags=" << flags);
104 bool Format::dummy() const
106 return extension().empty();
110 string const Format::extensions() const
112 return getStringFromVector(extension_list_, ", ");
116 bool Format::hasExtension(string const & e) const
118 return (find(extension_list_.begin(), extension_list_.end(), e)
119 != extension_list_.end());
123 bool Format::isChildFormat() const
127 return isDigitASCII(name_[name_.length() - 1]);
131 string const Format::parentFormat() const
133 return name_.substr(0, name_.length() - 1);
137 void Format::setExtensions(string const & e)
139 extension_list_ = getVectorFromString(e, ",");
143 // This method should return a reference, and throw an exception
144 // if the format named name cannot be found (Lgb)
145 Format const * Formats::getFormat(string const & name) const
147 FormatList::const_iterator cit =
148 find_if(formatlist.begin(), formatlist.end(),
149 FormatNamesEqual(name));
150 if (cit != formatlist.end())
157 string Formats::getFormatFromFile(FileName const & filename) const
159 if (filename.empty())
162 string const format = filename.guessFormatFromContents();
163 string const ext = getExtension(filename.absFileName());
164 if ((format == "gzip" || format == "zip" || format == "compress")
166 string const & fmt_name = formats.getFormatFromExtension(ext);
167 if (!fmt_name.empty()) {
168 Format const * p_format = formats.getFormat(fmt_name);
169 if (p_format && p_format->zippedNative())
170 return p_format->name();
176 // try to find a format from the file extension.
177 return getFormatFromExtension(ext);
181 string Formats::getFormatFromExtension(string const & ext) const
184 // this is ambigous if two formats have the same extension,
185 // but better than nothing
186 Formats::const_iterator cit =
187 find_if(formatlist.begin(), formatlist.end(),
188 FormatExtensionsEqual(ext));
189 if (cit != formats.end()) {
190 LYXERR(Debug::GRAPHICS, "\twill guess format from file extension: "
191 << ext << " -> " << cit->name());
199 /// Used to store last timestamp of file and whether it is (was) zipped
202 std::time_t timestamp;
203 ZippedInfo(bool zipped, std::time_t timestamp)
204 : zipped(zipped), timestamp(timestamp) { }
208 /// Mapping absolute pathnames of files to their ZippedInfo metadata.
209 static std::map<std::string, ZippedInfo> zipped_;
212 bool Formats::isZippedFile(support::FileName const & filename) const {
213 string const & fname = filename.absFileName();
214 time_t timestamp = filename.lastModified();
215 map<string, ZippedInfo>::iterator it = zipped_.find(fname);
216 if (it != zipped_.end() && it->second.timestamp == timestamp)
217 return it->second.zipped;
218 string const & format = getFormatFromFile(filename);
219 bool zipped = (format == "gzip" || format == "zip");
220 zipped_.insert(pair<string, ZippedInfo>(fname, ZippedInfo(zipped, timestamp)));
225 static string fixCommand(string const & cmd, string const & ext,
226 os::auto_open_mode mode)
228 // configure.py says we do not want a viewer/editor
232 // Does the OS manage this format?
233 if (os::canAutoOpenFile(ext, mode))
236 // if configure.py found nothing, clear the command
237 if (token(cmd, ' ', 0) == "auto")
240 // use the command found by configure.py
245 void Formats::setAutoOpen()
247 FormatList::iterator fit = formatlist.begin();
248 FormatList::iterator const fend = formatlist.end();
249 for ( ; fit != fend ; ++fit) {
250 fit->setViewer(fixCommand(fit->viewer(), fit->extension(), os::VIEW));
251 fit->setEditor(fixCommand(fit->editor(), fit->extension(), os::EDIT));
256 int Formats::getNumber(string const & name) const
258 FormatList::const_iterator cit =
259 find_if(formatlist.begin(), formatlist.end(),
260 FormatNamesEqual(name));
261 if (cit != formatlist.end())
262 return distance(formatlist.begin(), cit);
268 void Formats::add(string const & name)
270 if (!getFormat(name))
271 add(name, name, name, string(), string(), string(),
276 void Formats::add(string const & name, string const & extensions,
277 string const & prettyname, string const & shortcut,
278 string const & viewer, string const & editor,
281 FormatList::iterator it =
282 find_if(formatlist.begin(), formatlist.end(),
283 FormatNamesEqual(name));
284 if (it == formatlist.end())
285 formatlist.push_back(Format(name, extensions, prettyname,
286 shortcut, viewer, editor, flags));
288 *it = Format(name, extensions, prettyname, shortcut, viewer,
293 void Formats::erase(string const & name)
295 FormatList::iterator it =
296 find_if(formatlist.begin(), formatlist.end(),
297 FormatNamesEqual(name));
298 if (it != formatlist.end())
299 formatlist.erase(it);
305 std::sort(formatlist.begin(), formatlist.end());
309 void Formats::setViewer(string const & name, string const & command)
312 FormatList::iterator it =
313 find_if(formatlist.begin(), formatlist.end(),
314 FormatNamesEqual(name));
315 if (it != formatlist.end())
316 it->setViewer(command);
320 void Formats::setEditor(string const & name, string const & command)
323 FormatList::iterator it =
324 find_if(formatlist.begin(), formatlist.end(),
325 FormatNamesEqual(name));
326 if (it != formatlist.end())
327 it->setEditor(command);
331 bool Formats::view(Buffer const & buffer, FileName const & filename,
332 string const & format_name) const
334 if (filename.empty() || !filename.exists()) {
335 Alert::error(_("Cannot view file"),
336 bformat(_("File does not exist: %1$s"),
337 from_utf8(filename.absFileName())));
341 Format const * format = getFormat(format_name);
342 if (format && format->viewer().empty() &&
343 format->isChildFormat())
344 format = getFormat(format->parentFormat());
345 if (!format || format->viewer().empty()) {
346 // FIXME: I believe this is the wrong place to show alerts, it should be done
347 // by the caller (this should be "utility" code)
348 Alert::error(_("Cannot view file"),
349 bformat(_("No information for viewing %1$s"),
350 prettyName(format_name)));
354 if (format->viewer() == "auto") {
355 if (os::autoOpenFile(filename.absFileName(), os::VIEW, buffer.filePath()))
358 Alert::error(_("Cannot view file"),
359 bformat(_("Auto-view file %1$s failed"),
360 from_utf8(filename.absFileName())));
365 string command = libScriptSearch(format->viewer());
367 if (format_name == "dvi" &&
368 !lyxrc.view_dvi_paper_option.empty()) {
369 string paper_size = buffer.params().paperSizeName(BufferParams::XDVI);
370 if (!paper_size.empty()) {
371 command += ' ' + lyxrc.view_dvi_paper_option;
372 command += ' ' + paper_size;
373 if (buffer.params().orientation == ORIENTATION_LANDSCAPE &&
374 buffer.params().papersize != PAPER_CUSTOM)
379 if (!contains(command, token_from_format))
380 command += ' ' + token_from_format;
382 command = subst(command, token_from_format, quoteName(onlyFileName(filename.toFilesystemEncoding())));
383 command = subst(command, token_path_format, quoteName(onlyPath(filename.toFilesystemEncoding())));
384 command = subst(command, token_socket_format, quoteName(theServerSocket().address()));
385 LYXERR(Debug::FILES, "Executing command: " << command);
386 // FIXME UNICODE utf8 can be wrong for files
387 buffer.message(_("Executing command: ") + from_utf8(command));
389 PathChanger p(filename.onlyPath());
391 one.startscript(Systemcall::DontWait, command, buffer.filePath());
393 // we can't report any sort of error, since we aren't waiting
398 bool Formats::edit(Buffer const & buffer, FileName const & filename,
399 string const & format_name) const
401 if (filename.empty() || !filename.exists()) {
402 Alert::error(_("Cannot edit file"),
403 bformat(_("File does not exist: %1$s"),
404 from_utf8(filename.absFileName())));
408 // LinkBack files look like PDF, but have the .linkback extension
409 string const ext = getExtension(filename.absFileName());
410 if (format_name == "pdf" && ext == "linkback") {
411 #ifdef USE_MACOSX_PACKAGING
412 return editLinkBackFile(filename.absFileName().c_str());
414 Alert::error(_("Cannot edit file"),
415 _("LinkBack files can only be edited on Apple Mac OSX."));
417 #endif // USE_MACOSX_PACKAGING
420 Format const * format = getFormat(format_name);
421 if (format && format->editor().empty() &&
422 format->isChildFormat())
423 format = getFormat(format->parentFormat());
424 if (!format || format->editor().empty()) {
425 // FIXME: I believe this is the wrong place to show alerts, it should
426 // be done by the caller (this should be "utility" code)
427 Alert::error(_("Cannot edit file"),
428 bformat(_("No information for editing %1$s"),
429 prettyName(format_name)));
434 if (format->editor() == "auto") {
435 if (os::autoOpenFile(filename.absFileName(), os::EDIT, buffer.filePath()))
438 Alert::error(_("Cannot edit file"),
439 bformat(_("Auto-edit file %1$s failed"),
440 from_utf8(filename.absFileName())));
445 string command = format->editor();
447 if (!contains(command, token_from_format))
448 command += ' ' + token_from_format;
450 command = subst(command, token_from_format, quoteName(filename.toFilesystemEncoding()));
451 command = subst(command, token_path_format, quoteName(onlyPath(filename.toFilesystemEncoding())));
452 command = subst(command, token_socket_format, quoteName(theServerSocket().address()));
453 LYXERR(Debug::FILES, "Executing command: " << command);
454 // FIXME UNICODE utf8 can be wrong for files
455 buffer.message(_("Executing command: ") + from_utf8(command));
458 one.startscript(Systemcall::DontWait, command, buffer.filePath());
460 // we can't report any sort of error, since we aren't waiting
465 docstring const Formats::prettyName(string const & name) const
467 Format const * format = getFormat(name);
469 return from_utf8(format->prettyname());
471 return from_utf8(name);
475 string const Formats::extension(string const & name) const
477 Format const * format = getFormat(name);
479 return format->extension();
485 string const Formats::extensions(string const & name) const
487 Format const * format = getFormat(name);
489 return format->extensions();
496 typedef Translator<OutputParams::FLAVOR, string> FlavorTranslator;
498 FlavorTranslator initFlavorTranslator()
500 FlavorTranslator f(OutputParams::LATEX, "latex");
501 f.addPair(OutputParams::DVILUATEX, "dviluatex");
502 f.addPair(OutputParams::LUATEX, "luatex");
503 f.addPair(OutputParams::PDFLATEX, "pdflatex");
504 f.addPair(OutputParams::XETEX, "xetex");
505 f.addPair(OutputParams::XML, "docbook-xml");
506 f.addPair(OutputParams::HTML, "xhtml");
507 f.addPair(OutputParams::TEXT, "text");
512 FlavorTranslator const & flavorTranslator()
514 static FlavorTranslator translator = initFlavorTranslator();
520 std::string flavor2format(OutputParams::FLAVOR flavor)
522 return flavorTranslator().find(flavor);
526 /* Not currently needed, but I'll leave the code in case it is.
527 OutputParams::FLAVOR format2flavor(std::string fmt)
529 return flavorTranslator().find(fmt);
534 Formats system_formats;