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"
33 // FIXME: Q_WS_MACX is not available, it's in Qt
34 #ifdef USE_MACOSX_PACKAGING
35 #include "support/linkback/LinkBackProxy.h"
39 using namespace lyx::support;
43 namespace Alert = frontend::Alert;
44 namespace os = support::os;
48 string const token_from_format("$$i");
49 string const token_path_format("$$p");
50 string const token_socket_format("$$a");
53 class FormatNamesEqual : public unary_function<Format, bool> {
55 FormatNamesEqual(string const & name)
57 bool operator()(Format const & f) const
59 return f.name() == name_;
66 class FormatExtensionsEqual : public unary_function<Format, bool> {
68 FormatExtensionsEqual(string const & extension)
69 : extension_(extension) {}
70 bool operator()(Format const & f) const
72 return f.hasExtension(extension_);
81 bool operator<(Format const & a, Format const & b)
83 // use the compare_ascii_no_case instead of compare_no_case,
84 // because in turkish, 'i' is not the lowercase version of 'I',
85 // and thus turkish locale breaks parsing of tags.
87 return compare_ascii_no_case(a.prettyname(), b.prettyname()) < 0;
91 Format::Format(string const & n, string const & e, string const & p,
92 string const & s, string const & v, string const & ed,
94 : name_(n), extensions_(e), prettyname_(p), shortcut_(s), viewer_(v),
95 editor_(ed), flags_(flags)
97 extension_list_ = getVectorFromString(e, ",");
98 LYXERR(Debug::GRAPHICS, "New Format: n=" << n << ", flags=" << flags);
102 bool Format::dummy() const
104 return extension().empty();
108 bool Format::hasExtension(string const & e) const
110 return (find(extension_list_.begin(), extension_list_.end(), e)
111 != extension_list_.end());
115 bool Format::isChildFormat() const
119 return isDigitASCII(name_[name_.length() - 1]);
123 string const Format::parentFormat() const
125 return name_.substr(0, name_.length() - 1);
129 void Format::setExtensions(string const & e)
132 extension_list_ = getVectorFromString(e, ",");
136 // This method should return a reference, and throw an exception
137 // if the format named name cannot be found (Lgb)
138 Format const * Formats::getFormat(string const & name) const
140 FormatList::const_iterator cit =
141 find_if(formatlist.begin(), formatlist.end(),
142 FormatNamesEqual(name));
143 if (cit != formatlist.end())
150 string Formats::getFormatFromFile(FileName const & filename) const
152 if (filename.empty())
155 string const format = filename.guessFormatFromContents();
156 string const ext = getExtension(filename.absFileName());
157 if ((format == "gzip" || format == "zip" || format == "compress")
159 string const & fmt_name = formats.getFormatFromExtension(ext);
160 if (!fmt_name.empty()) {
161 Format const * p_format = formats.getFormat(fmt_name);
162 if (p_format && p_format->zippedNative())
163 return p_format->name();
169 // try to find a format from the file extension.
170 return getFormatFromExtension(ext);
174 string Formats::getFormatFromExtension(string const & ext) const
177 // this is ambigous if two formats have the same extension,
178 // but better than nothing
179 Formats::const_iterator cit =
180 find_if(formatlist.begin(), formatlist.end(),
181 FormatExtensionsEqual(ext));
182 if (cit != formats.end()) {
183 LYXERR(Debug::GRAPHICS, "\twill guess format from file extension: "
184 << ext << " -> " << cit->name());
192 bool Formats::isZippedFile(support::FileName const & filename) const {
193 string const & fname = filename.absFileName();
194 time_t timestamp = filename.lastModified();
195 map<string, ZippedInfo>::iterator it = zipped_.find(fname);
196 if (it != zipped_.end() && it->second.timestamp == timestamp)
197 return it->second.zipped;
198 string const & format = getFormatFromFile(filename);
199 bool zipped = (format == "gzip" || format == "zip");
200 zipped_.insert(pair<string, ZippedInfo>(fname, ZippedInfo(zipped, timestamp)));
205 static string fixCommand(string const & cmd, string const & ext,
206 os::auto_open_mode mode)
208 // configure.py says we do not want a viewer/editor
212 // Does the OS manage this format?
213 if (os::canAutoOpenFile(ext, mode))
216 // if configure.py found nothing, clear the command
217 if (token(cmd, ' ', 0) == "auto")
220 // use the command found by configure.py
225 void Formats::setAutoOpen()
227 FormatList::iterator fit = formatlist.begin();
228 FormatList::iterator const fend = formatlist.end();
229 for ( ; fit != fend ; ++fit) {
230 fit->setViewer(fixCommand(fit->viewer(), fit->extension(), os::VIEW));
231 fit->setEditor(fixCommand(fit->editor(), fit->extension(), os::EDIT));
236 int Formats::getNumber(string const & name) const
238 FormatList::const_iterator cit =
239 find_if(formatlist.begin(), formatlist.end(),
240 FormatNamesEqual(name));
241 if (cit != formatlist.end())
242 return distance(formatlist.begin(), cit);
248 void Formats::add(string const & name)
250 if (!getFormat(name))
251 add(name, name, name, string(), string(), string(),
256 void Formats::add(string const & name, string const & extensions,
257 string const & prettyname, string const & shortcut,
258 string const & viewer, string const & editor,
261 FormatList::iterator it =
262 find_if(formatlist.begin(), formatlist.end(),
263 FormatNamesEqual(name));
264 if (it == formatlist.end())
265 formatlist.push_back(Format(name, extensions, prettyname,
266 shortcut, viewer, editor, flags));
268 *it = Format(name, extensions, prettyname, shortcut, viewer,
273 void Formats::erase(string const & name)
275 FormatList::iterator it =
276 find_if(formatlist.begin(), formatlist.end(),
277 FormatNamesEqual(name));
278 if (it != formatlist.end())
279 formatlist.erase(it);
285 std::sort(formatlist.begin(), formatlist.end());
289 void Formats::setViewer(string const & name, string const & command)
292 FormatList::iterator it =
293 find_if(formatlist.begin(), formatlist.end(),
294 FormatNamesEqual(name));
295 if (it != formatlist.end())
296 it->setViewer(command);
300 void Formats::setEditor(string const & name, string const & command)
303 FormatList::iterator it =
304 find_if(formatlist.begin(), formatlist.end(),
305 FormatNamesEqual(name));
306 if (it != formatlist.end())
307 it->setEditor(command);
311 bool Formats::view(Buffer const & buffer, FileName const & filename,
312 string const & format_name) const
314 if (filename.empty() || !filename.exists()) {
315 Alert::error(_("Cannot view file"),
316 bformat(_("File does not exist: %1$s"),
317 from_utf8(filename.absFileName())));
321 Format const * format = getFormat(format_name);
322 if (format && format->viewer().empty() &&
323 format->isChildFormat())
324 format = getFormat(format->parentFormat());
325 if (!format || format->viewer().empty()) {
326 // FIXME: I believe this is the wrong place to show alerts, it should be done
327 // by the caller (this should be "utility" code)
328 Alert::error(_("Cannot view file"),
329 bformat(_("No information for viewing %1$s"),
330 prettyName(format_name)));
334 if (format->viewer() == "auto") {
335 if (os::autoOpenFile(filename.absFileName(), os::VIEW, buffer.filePath()))
338 Alert::error(_("Cannot view file"),
339 bformat(_("Auto-view file %1$s failed"),
340 from_utf8(filename.absFileName())));
345 string command = libScriptSearch(format->viewer());
347 if (format_name == "dvi" &&
348 !lyxrc.view_dvi_paper_option.empty()) {
349 string paper_size = buffer.params().paperSizeName(BufferParams::XDVI);
350 if (!paper_size.empty()) {
351 command += ' ' + lyxrc.view_dvi_paper_option;
352 command += ' ' + paper_size;
353 if (buffer.params().orientation == ORIENTATION_LANDSCAPE &&
354 buffer.params().papersize != PAPER_CUSTOM)
359 if (!contains(command, token_from_format))
360 command += ' ' + token_from_format;
362 command = subst(command, token_from_format, quoteName(onlyFileName(filename.toFilesystemEncoding())));
363 command = subst(command, token_path_format, quoteName(onlyPath(filename.toFilesystemEncoding())));
364 command = subst(command, token_socket_format, quoteName(theServerSocket().address()));
365 LYXERR(Debug::FILES, "Executing command: " << command);
366 // FIXME UNICODE utf8 can be wrong for files
367 buffer.message(_("Executing command: ") + from_utf8(command));
369 PathChanger p(filename.onlyPath());
371 one.startscript(Systemcall::DontWait, command, buffer.filePath());
373 // we can't report any sort of error, since we aren't waiting
378 bool Formats::edit(Buffer const & buffer, FileName const & filename,
379 string const & format_name) const
381 if (filename.empty() || !filename.exists()) {
382 Alert::error(_("Cannot edit file"),
383 bformat(_("File does not exist: %1$s"),
384 from_utf8(filename.absFileName())));
388 // LinkBack files look like PDF, but have the .linkback extension
389 string const ext = getExtension(filename.absFileName());
390 if (format_name == "pdf" && ext == "linkback") {
391 #ifdef USE_MACOSX_PACKAGING
392 return editLinkBackFile(filename.absFileName().c_str());
394 Alert::error(_("Cannot edit file"),
395 _("LinkBack files can only be edited on Apple Mac OSX."));
397 #endif // USE_MACOSX_PACKAGING
400 Format const * format = getFormat(format_name);
401 if (format && format->editor().empty() &&
402 format->isChildFormat())
403 format = getFormat(format->parentFormat());
404 if (!format || format->editor().empty()) {
405 // FIXME: I believe this is the wrong place to show alerts, it should
406 // be done by the caller (this should be "utility" code)
407 Alert::error(_("Cannot edit file"),
408 bformat(_("No information for editing %1$s"),
409 prettyName(format_name)));
414 if (format->editor() == "auto") {
415 if (os::autoOpenFile(filename.absFileName(), os::EDIT, buffer.filePath()))
418 Alert::error(_("Cannot edit file"),
419 bformat(_("Auto-edit file %1$s failed"),
420 from_utf8(filename.absFileName())));
425 string command = format->editor();
427 if (!contains(command, token_from_format))
428 command += ' ' + token_from_format;
430 command = subst(command, token_from_format, quoteName(filename.toFilesystemEncoding()));
431 command = subst(command, token_path_format, quoteName(onlyPath(filename.toFilesystemEncoding())));
432 command = subst(command, token_socket_format, quoteName(theServerSocket().address()));
433 LYXERR(Debug::FILES, "Executing command: " << command);
434 // FIXME UNICODE utf8 can be wrong for files
435 buffer.message(_("Executing command: ") + from_utf8(command));
438 one.startscript(Systemcall::DontWait, command, buffer.filePath());
440 // we can't report any sort of error, since we aren't waiting
445 docstring const Formats::prettyName(string const & name) const
447 Format const * format = getFormat(name);
449 return from_utf8(format->prettyname());
451 return from_utf8(name);
455 string const Formats::extension(string const & name) const
457 Format const * format = getFormat(name);
459 return format->extension();
465 string const Formats::extensions(string const & name) const
467 Format const * format = getFormat(name);
469 return format->extensions();
476 typedef Translator<OutputParams::FLAVOR, string> FlavorTranslator;
478 FlavorTranslator initFlavorTranslator()
480 FlavorTranslator f(OutputParams::LATEX, "latex");
481 f.addPair(OutputParams::DVILUATEX, "dviluatex");
482 f.addPair(OutputParams::LUATEX, "luatex");
483 f.addPair(OutputParams::PDFLATEX, "pdflatex");
484 f.addPair(OutputParams::XETEX, "xetex");
485 f.addPair(OutputParams::XML, "docbook-xml");
486 f.addPair(OutputParams::HTML, "xhtml");
487 f.addPair(OutputParams::TEXT, "text");
492 FlavorTranslator const & flavorTranslator()
494 static FlavorTranslator translator = initFlavorTranslator();
500 std::string flavor2format(OutputParams::FLAVOR flavor)
502 return flavorTranslator().find(flavor);
506 /* Not currently needed, but I'll leave the code in case it is.
507 OutputParams::FLAVOR format2flavor(std::string fmt)
509 return flavorTranslator().find(fmt);
514 Formats system_formats;