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, ",");
101 bool Format::dummy() const
103 return extension().empty();
107 bool Format::hasExtension(string const & e) const
109 return (find(extension_list_.begin(), extension_list_.end(), e)
110 != extension_list_.end());
114 bool Format::isChildFormat() const
118 return isDigitASCII(name_[name_.length() - 1]);
122 string const Format::parentFormat() const
124 return name_.substr(0, name_.length() - 1);
128 void Format::setExtensions(string const & e)
131 extension_list_ = getVectorFromString(e, ",");
135 // This method should return a reference, and throw an exception
136 // if the format named name cannot be found (Lgb)
137 Format const * Formats::getFormat(string const & name) const
139 FormatList::const_iterator cit =
140 find_if(formatlist.begin(), formatlist.end(),
141 FormatNamesEqual(name));
142 if (cit != formatlist.end())
149 string Formats::getFormatFromFile(FileName const & filename) const
151 if (filename.empty())
154 string const format = filename.guessFormatFromContents();
158 // try to find a format from the file extension.
159 string const ext = getExtension(filename.absFileName());
161 // this is ambigous if two formats have the same extension,
162 // but better than nothing
163 Formats::const_iterator cit =
164 find_if(formatlist.begin(), formatlist.end(),
165 FormatExtensionsEqual(ext));
166 if (cit != formats.end()) {
167 LYXERR(Debug::GRAPHICS, "\twill guess format from file extension: "
168 << ext << " -> " << cit->name());
176 static string fixCommand(string const & cmd, string const & ext,
177 os::auto_open_mode mode)
179 // configure.py says we do not want a viewer/editor
183 // Does the OS manage this format?
184 if (os::canAutoOpenFile(ext, mode))
187 // if configure.py found nothing, clear the command
188 if (token(cmd, ' ', 0) == "auto")
191 // use the command found by configure.py
196 void Formats::setAutoOpen()
198 FormatList::iterator fit = formatlist.begin();
199 FormatList::iterator const fend = formatlist.end();
200 for ( ; fit != fend ; ++fit) {
201 fit->setViewer(fixCommand(fit->viewer(), fit->extension(), os::VIEW));
202 fit->setEditor(fixCommand(fit->editor(), fit->extension(), os::EDIT));
207 int Formats::getNumber(string const & name) const
209 FormatList::const_iterator cit =
210 find_if(formatlist.begin(), formatlist.end(),
211 FormatNamesEqual(name));
212 if (cit != formatlist.end())
213 return distance(formatlist.begin(), cit);
219 void Formats::add(string const & name)
221 if (!getFormat(name))
222 add(name, name, name, string(), string(), string(),
227 void Formats::add(string const & name, string const & extensions,
228 string const & prettyname, string const & shortcut,
229 string const & viewer, string const & editor,
232 FormatList::iterator it =
233 find_if(formatlist.begin(), formatlist.end(),
234 FormatNamesEqual(name));
235 if (it == formatlist.end())
236 formatlist.push_back(Format(name, extensions, prettyname,
237 shortcut, viewer, editor, flags));
239 *it = Format(name, extensions, prettyname, shortcut, viewer,
244 void Formats::erase(string const & name)
246 FormatList::iterator it =
247 find_if(formatlist.begin(), formatlist.end(),
248 FormatNamesEqual(name));
249 if (it != formatlist.end())
250 formatlist.erase(it);
256 std::sort(formatlist.begin(), formatlist.end());
260 void Formats::setViewer(string const & name, string const & command)
263 FormatList::iterator it =
264 find_if(formatlist.begin(), formatlist.end(),
265 FormatNamesEqual(name));
266 if (it != formatlist.end())
267 it->setViewer(command);
271 void Formats::setEditor(string const & name, string const & command)
274 FormatList::iterator it =
275 find_if(formatlist.begin(), formatlist.end(),
276 FormatNamesEqual(name));
277 if (it != formatlist.end())
278 it->setEditor(command);
282 bool Formats::view(Buffer const & buffer, FileName const & filename,
283 string const & format_name) const
285 if (filename.empty() || !filename.exists()) {
286 Alert::error(_("Cannot view file"),
287 bformat(_("File does not exist: %1$s"),
288 from_utf8(filename.absFileName())));
292 Format const * format = getFormat(format_name);
293 if (format && format->viewer().empty() &&
294 format->isChildFormat())
295 format = getFormat(format->parentFormat());
296 if (!format || format->viewer().empty()) {
297 // FIXME: I believe this is the wrong place to show alerts, it should be done
298 // by the caller (this should be "utility" code)
299 Alert::error(_("Cannot view file"),
300 bformat(_("No information for viewing %1$s"),
301 prettyName(format_name)));
305 if (format->viewer() == "auto") {
306 if (os::autoOpenFile(filename.absFileName(), os::VIEW, buffer.filePath()))
309 Alert::error(_("Cannot view file"),
310 bformat(_("Auto-view file %1$s failed"),
311 from_utf8(filename.absFileName())));
316 string command = libScriptSearch(format->viewer());
318 if (format_name == "dvi" &&
319 !lyxrc.view_dvi_paper_option.empty()) {
320 string paper_size = buffer.params().paperSizeName(BufferParams::XDVI);
321 if (!paper_size.empty()) {
322 command += ' ' + lyxrc.view_dvi_paper_option;
323 command += ' ' + paper_size;
324 if (buffer.params().orientation == ORIENTATION_LANDSCAPE &&
325 buffer.params().papersize != PAPER_CUSTOM)
330 if (!contains(command, token_from_format))
331 command += ' ' + token_from_format;
333 command = subst(command, token_from_format, quoteName(onlyFileName(filename.toFilesystemEncoding())));
334 command = subst(command, token_path_format, quoteName(onlyPath(filename.toFilesystemEncoding())));
335 command = subst(command, token_socket_format, quoteName(theServerSocket().address()));
336 LYXERR(Debug::FILES, "Executing command: " << command);
337 // FIXME UNICODE utf8 can be wrong for files
338 buffer.message(_("Executing command: ") + from_utf8(command));
340 PathChanger p(filename.onlyPath());
342 one.startscript(Systemcall::DontWait, command, buffer.filePath());
344 // we can't report any sort of error, since we aren't waiting
349 bool Formats::edit(Buffer const & buffer, FileName const & filename,
350 string const & format_name) const
352 if (filename.empty() || !filename.exists()) {
353 Alert::error(_("Cannot edit file"),
354 bformat(_("File does not exist: %1$s"),
355 from_utf8(filename.absFileName())));
359 // LinkBack files look like PDF, but have the .linkback extension
360 string const ext = getExtension(filename.absFileName());
361 if (format_name == "pdf" && ext == "linkback") {
362 #ifdef USE_MACOSX_PACKAGING
363 return editLinkBackFile(filename.absFileName().c_str());
365 Alert::error(_("Cannot edit file"),
366 _("LinkBack files can only be edited on Apple Mac OSX."));
368 #endif // USE_MACOSX_PACKAGING
371 Format const * format = getFormat(format_name);
372 if (format && format->editor().empty() &&
373 format->isChildFormat())
374 format = getFormat(format->parentFormat());
375 if (!format || format->editor().empty()) {
376 // FIXME: I believe this is the wrong place to show alerts, it should
377 // be done by the caller (this should be "utility" code)
378 Alert::error(_("Cannot edit file"),
379 bformat(_("No information for editing %1$s"),
380 prettyName(format_name)));
385 if (format->editor() == "auto") {
386 if (os::autoOpenFile(filename.absFileName(), os::EDIT, buffer.filePath()))
389 Alert::error(_("Cannot edit file"),
390 bformat(_("Auto-edit file %1$s failed"),
391 from_utf8(filename.absFileName())));
396 string command = format->editor();
398 if (!contains(command, token_from_format))
399 command += ' ' + token_from_format;
401 command = subst(command, token_from_format, quoteName(filename.toFilesystemEncoding()));
402 command = subst(command, token_path_format, quoteName(onlyPath(filename.toFilesystemEncoding())));
403 command = subst(command, token_socket_format, quoteName(theServerSocket().address()));
404 LYXERR(Debug::FILES, "Executing command: " << command);
405 // FIXME UNICODE utf8 can be wrong for files
406 buffer.message(_("Executing command: ") + from_utf8(command));
409 one.startscript(Systemcall::DontWait, command, buffer.filePath());
411 // we can't report any sort of error, since we aren't waiting
416 docstring const Formats::prettyName(string const & name) const
418 Format const * format = getFormat(name);
420 return from_utf8(format->prettyname());
422 return from_utf8(name);
426 string const Formats::extension(string const & name) const
428 Format const * format = getFormat(name);
430 return format->extension();
436 string const Formats::extensions(string const & name) const
438 Format const * format = getFormat(name);
440 return format->extensions();
447 typedef Translator<OutputParams::FLAVOR, string> FlavorTranslator;
449 FlavorTranslator initFlavorTranslator()
451 FlavorTranslator f(OutputParams::LATEX, "latex");
452 f.addPair(OutputParams::DVILUATEX, "dviluatex");
453 f.addPair(OutputParams::LUATEX, "luatex");
454 f.addPair(OutputParams::PDFLATEX, "pdflatex");
455 f.addPair(OutputParams::XETEX, "xetex");
456 f.addPair(OutputParams::XML, "docbook-xml");
457 f.addPair(OutputParams::HTML, "xhtml");
458 f.addPair(OutputParams::TEXT, "text");
463 FlavorTranslator const & flavorTranslator()
465 static FlavorTranslator translator = initFlavorTranslator();
471 std::string flavor2format(OutputParams::FLAVOR flavor)
473 return flavorTranslator().find(flavor);
477 /* Not currently needed, but I'll leave the code in case it is.
478 OutputParams::FLAVOR format2flavor(std::string fmt)
480 return flavorTranslator().find(fmt);
485 Formats system_formats;