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.extension() == extension_;
80 bool operator<(Format const & a, Format const & b)
82 // use the compare_ascii_no_case instead of compare_no_case,
83 // because in turkish, 'i' is not the lowercase version of 'I',
84 // and thus turkish locale breaks parsing of tags.
86 return compare_ascii_no_case(a.prettyname(), b.prettyname()) < 0;
90 Format::Format(string const & n, string const & e, string const & p,
91 string const & s, string const & v, string const & ed,
93 : name_(n), extension_(e), prettyname_(p), shortcut_(s), viewer_(v),
94 editor_(ed), flags_(flags)
98 bool Format::dummy() const
100 return extension().empty();
104 bool Format::isChildFormat() const
108 return isDigitASCII(name_[name_.length() - 1]);
112 string const Format::parentFormat() const
114 return name_.substr(0, name_.length() - 1);
118 // This method should return a reference, and throw an exception
119 // if the format named name cannot be found (Lgb)
120 Format const * Formats::getFormat(string const & name) const
122 FormatList::const_iterator cit =
123 find_if(formatlist.begin(), formatlist.end(),
124 FormatNamesEqual(name));
125 if (cit != formatlist.end())
132 string Formats::getFormatFromFile(FileName const & filename) const
134 if (filename.empty())
137 string const format = filename.guessFormatFromContents();
141 // try to find a format from the file extension.
142 string const ext = getExtension(filename.absFileName());
144 // this is ambigous if two formats have the same extension,
145 // but better than nothing
146 Formats::const_iterator cit =
147 find_if(formatlist.begin(), formatlist.end(),
148 FormatExtensionsEqual(ext));
149 if (cit != formats.end()) {
150 LYXERR(Debug::GRAPHICS, "\twill guess format from file extension: "
151 << ext << " -> " << cit->name());
159 static string fixCommand(string const & cmd, string const & ext,
160 os::auto_open_mode mode)
162 // configure.py says we do not want a viewer/editor
166 // Does the OS manage this format?
167 if (os::canAutoOpenFile(ext, mode))
170 // if configure.py found nothing, clear the command
171 if (token(cmd, ' ', 0) == "auto")
174 // use the command found by configure.py
179 void Formats::setAutoOpen()
181 FormatList::iterator fit = formatlist.begin();
182 FormatList::iterator const fend = formatlist.end();
183 for ( ; fit != fend ; ++fit) {
184 fit->setViewer(fixCommand(fit->viewer(), fit->extension(), os::VIEW));
185 fit->setEditor(fixCommand(fit->editor(), fit->extension(), os::EDIT));
190 int Formats::getNumber(string const & name) const
192 FormatList::const_iterator cit =
193 find_if(formatlist.begin(), formatlist.end(),
194 FormatNamesEqual(name));
195 if (cit != formatlist.end())
196 return distance(formatlist.begin(), cit);
202 void Formats::add(string const & name)
204 if (!getFormat(name))
205 add(name, name, name, string(), string(), string(),
210 void Formats::add(string const & name, string const & extension,
211 string const & prettyname, string const & shortcut,
212 string const & viewer, string const & editor,
215 FormatList::iterator it =
216 find_if(formatlist.begin(), formatlist.end(),
217 FormatNamesEqual(name));
218 if (it == formatlist.end())
219 formatlist.push_back(Format(name, extension, prettyname,
220 shortcut, viewer, editor, flags));
222 *it = Format(name, extension, prettyname, shortcut, viewer,
227 void Formats::erase(string const & name)
229 FormatList::iterator it =
230 find_if(formatlist.begin(), formatlist.end(),
231 FormatNamesEqual(name));
232 if (it != formatlist.end())
233 formatlist.erase(it);
239 std::sort(formatlist.begin(), formatlist.end());
243 void Formats::setViewer(string const & name, string const & command)
246 FormatList::iterator it =
247 find_if(formatlist.begin(), formatlist.end(),
248 FormatNamesEqual(name));
249 if (it != formatlist.end())
250 it->setViewer(command);
254 void Formats::setEditor(string const & name, string const & command)
257 FormatList::iterator it =
258 find_if(formatlist.begin(), formatlist.end(),
259 FormatNamesEqual(name));
260 if (it != formatlist.end())
261 it->setEditor(command);
265 bool Formats::view(Buffer const & buffer, FileName const & filename,
266 string const & format_name) const
268 if (filename.empty() || !filename.exists()) {
269 Alert::error(_("Cannot view file"),
270 bformat(_("File does not exist: %1$s"),
271 from_utf8(filename.absFileName())));
275 Format const * format = getFormat(format_name);
276 if (format && format->viewer().empty() &&
277 format->isChildFormat())
278 format = getFormat(format->parentFormat());
279 if (!format || format->viewer().empty()) {
280 // FIXME: I believe this is the wrong place to show alerts, it should be done
281 // by the caller (this should be "utility" code)
282 Alert::error(_("Cannot view file"),
283 bformat(_("No information for viewing %1$s"),
284 prettyName(format_name)));
288 if (format->viewer() == "auto") {
289 if (os::autoOpenFile(filename.absFileName(), os::VIEW, buffer.filePath()))
292 Alert::error(_("Cannot view file"),
293 bformat(_("Auto-view file %1$s failed"),
294 from_utf8(filename.absFileName())));
299 string command = libScriptSearch(format->viewer());
301 if (format_name == "dvi" &&
302 !lyxrc.view_dvi_paper_option.empty()) {
303 string paper_size = buffer.params().paperSizeName(BufferParams::XDVI);
304 if (!paper_size.empty()) {
305 command += ' ' + lyxrc.view_dvi_paper_option;
306 command += ' ' + paper_size;
307 if (buffer.params().orientation == ORIENTATION_LANDSCAPE &&
308 buffer.params().papersize != PAPER_CUSTOM)
313 if (!contains(command, token_from_format))
314 command += ' ' + token_from_format;
316 command = subst(command, token_from_format, quoteName(onlyFileName(filename.toFilesystemEncoding())));
317 command = subst(command, token_path_format, quoteName(onlyPath(filename.toFilesystemEncoding())));
318 command = subst(command, token_socket_format, quoteName(theServerSocket().address()));
319 LYXERR(Debug::FILES, "Executing command: " << command);
320 // FIXME UNICODE utf8 can be wrong for files
321 buffer.message(_("Executing command: ") + from_utf8(command));
323 PathChanger p(filename.onlyPath());
325 one.startscript(Systemcall::DontWait, command, buffer.filePath());
327 // we can't report any sort of error, since we aren't waiting
332 bool Formats::edit(Buffer const & buffer, FileName const & filename,
333 string const & format_name) const
335 if (filename.empty() || !filename.exists()) {
336 Alert::error(_("Cannot edit file"),
337 bformat(_("File does not exist: %1$s"),
338 from_utf8(filename.absFileName())));
342 // LinkBack files look like PDF, but have the .linkback extension
343 string const ext = getExtension(filename.absFileName());
344 if (format_name == "pdf" && ext == "linkback") {
345 #ifdef USE_MACOSX_PACKAGING
346 return editLinkBackFile(filename.absFileName().c_str());
348 Alert::error(_("Cannot edit file"),
349 _("LinkBack files can only be edited on Apple Mac OSX."));
351 #endif // USE_MACOSX_PACKAGING
354 Format const * format = getFormat(format_name);
355 if (format && format->editor().empty() &&
356 format->isChildFormat())
357 format = getFormat(format->parentFormat());
358 if (!format || format->editor().empty()) {
359 // FIXME: I believe this is the wrong place to show alerts, it should
360 // be done by the caller (this should be "utility" code)
361 Alert::error(_("Cannot edit file"),
362 bformat(_("No information for editing %1$s"),
363 prettyName(format_name)));
368 if (format->editor() == "auto") {
369 if (os::autoOpenFile(filename.absFileName(), os::EDIT, buffer.filePath()))
372 Alert::error(_("Cannot edit file"),
373 bformat(_("Auto-edit file %1$s failed"),
374 from_utf8(filename.absFileName())));
379 string command = format->editor();
381 if (!contains(command, token_from_format))
382 command += ' ' + token_from_format;
384 command = subst(command, token_from_format, quoteName(filename.toFilesystemEncoding()));
385 command = subst(command, token_path_format, quoteName(onlyPath(filename.toFilesystemEncoding())));
386 command = subst(command, token_socket_format, quoteName(theServerSocket().address()));
387 LYXERR(Debug::FILES, "Executing command: " << command);
388 // FIXME UNICODE utf8 can be wrong for files
389 buffer.message(_("Executing command: ") + from_utf8(command));
392 one.startscript(Systemcall::DontWait, command, buffer.filePath());
394 // we can't report any sort of error, since we aren't waiting
399 docstring const Formats::prettyName(string const & name) const
401 Format const * format = getFormat(name);
403 return from_utf8(format->prettyname());
405 return from_utf8(name);
409 string const Formats::extension(string const & name) const
411 Format const * format = getFormat(name);
413 return format->extension();
420 typedef Translator<OutputParams::FLAVOR, string> FlavorTranslator;
422 FlavorTranslator initFlavorTranslator()
424 FlavorTranslator f(OutputParams::LATEX, "latex");
425 f.addPair(OutputParams::LUATEX, "luatex");
426 f.addPair(OutputParams::PDFLATEX, "pdflatex");
427 f.addPair(OutputParams::XETEX, "xetex");
428 f.addPair(OutputParams::XML, "docbook-xml");
429 f.addPair(OutputParams::HTML, "xhtml");
430 f.addPair(OutputParams::TEXT, "text");
435 FlavorTranslator const & flavorTranslator()
437 static FlavorTranslator translator = initFlavorTranslator();
443 std::string flavor2format(OutputParams::FLAVOR flavor)
445 return flavorTranslator().find(flavor);
449 /* Not currently needed, but I'll leave the code in case it is.
450 OutputParams::FLAVOR format2flavor(std::string fmt)
452 return flavorTranslator().find(fmt);
457 Formats system_formats;