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/Systemcall.h"
30 // FIXME: Q_WS_MACX is not available, it's in Qt
31 #ifdef USE_MACOSX_PACKAGING
32 #include "support/linkback/LinkBackProxy.h"
36 using namespace lyx::support;
40 namespace Alert = frontend::Alert;
41 namespace os = support::os;
45 string const token_from_format("$$i");
46 string const token_path_format("$$p");
47 string const token_socket_format("$$a");
50 class FormatNamesEqual : public unary_function<Format, bool> {
52 FormatNamesEqual(string const & name)
54 bool operator()(Format const & f) const
56 return f.name() == name_;
63 class FormatExtensionsEqual : public unary_function<Format, bool> {
65 FormatExtensionsEqual(string const & extension)
66 : extension_(extension) {}
67 bool operator()(Format const & f) const
69 return f.extension() == extension_;
77 bool operator<(Format const & a, Format const & b)
79 // use the compare_ascii_no_case instead of compare_no_case,
80 // because in turkish, 'i' is not the lowercase version of 'I',
81 // and thus turkish locale breaks parsing of tags.
83 return compare_ascii_no_case(a.prettyname(), b.prettyname()) < 0;
87 Format::Format(string const & n, string const & e, string const & p,
88 string const & s, string const & v, string const & ed,
90 : name_(n), extension_(e), prettyname_(p), shortcut_(s), viewer_(v),
91 editor_(ed), flags_(flags)
95 bool Format::dummy() const
97 return extension().empty();
101 bool Format::isChildFormat() const
105 return isdigit(name_[name_.length() - 1]);
109 string const Format::parentFormat() const
111 return name_.substr(0, name_.length() - 1);
115 // This method should return a reference, and throw an exception
116 // if the format named name cannot be found (Lgb)
117 Format const * Formats::getFormat(string const & name) const
119 FormatList::const_iterator cit =
120 find_if(formatlist.begin(), formatlist.end(),
121 FormatNamesEqual(name));
122 if (cit != formatlist.end())
129 string Formats::getFormatFromFile(FileName const & filename) const
131 if (filename.empty())
134 string const format = filename.guessFormatFromContents();
138 // try to find a format from the file extension.
139 string const ext = getExtension(filename.absFileName());
141 // this is ambigous if two formats have the same extension,
142 // but better than nothing
143 Formats::const_iterator cit =
144 find_if(formatlist.begin(), formatlist.end(),
145 FormatExtensionsEqual(ext));
146 if (cit != formats.end()) {
147 LYXERR(Debug::GRAPHICS, "\twill guess format from file extension: "
148 << ext << " -> " << cit->name());
156 static string fixCommand(string const & cmd, string const & ext,
157 os::auto_open_mode mode)
159 // configure.py says we do not want a viewer/editor
163 // Does the OS manage this format?
164 if (os::canAutoOpenFile(ext, mode))
167 // if configure.py found nothing, clear the command
168 if (token(cmd, ' ', 0) == "auto")
171 // use the command found by configure.py
176 void Formats::setAutoOpen()
178 FormatList::iterator fit = formatlist.begin();
179 FormatList::iterator const fend = formatlist.end();
180 for ( ; fit != fend ; ++fit) {
181 fit->setViewer(fixCommand(fit->viewer(), fit->extension(), os::VIEW));
182 fit->setEditor(fixCommand(fit->editor(), fit->extension(), os::EDIT));
187 int Formats::getNumber(string const & name) const
189 FormatList::const_iterator cit =
190 find_if(formatlist.begin(), formatlist.end(),
191 FormatNamesEqual(name));
192 if (cit != formatlist.end())
193 return distance(formatlist.begin(), cit);
199 void Formats::add(string const & name)
201 if (!getFormat(name))
202 add(name, name, name, string(), string(), string(),
207 void Formats::add(string const & name, string const & extension,
208 string const & prettyname, string const & shortcut,
209 string const & viewer, string const & editor,
212 FormatList::iterator it =
213 find_if(formatlist.begin(), formatlist.end(),
214 FormatNamesEqual(name));
215 if (it == formatlist.end())
216 formatlist.push_back(Format(name, extension, prettyname,
217 shortcut, viewer, editor, flags));
219 *it = Format(name, extension, prettyname, shortcut, viewer,
224 void Formats::erase(string const & name)
226 FormatList::iterator it =
227 find_if(formatlist.begin(), formatlist.end(),
228 FormatNamesEqual(name));
229 if (it != formatlist.end())
230 formatlist.erase(it);
236 std::sort(formatlist.begin(), formatlist.end());
240 void Formats::setViewer(string const & name, string const & command)
243 FormatList::iterator it =
244 find_if(formatlist.begin(), formatlist.end(),
245 FormatNamesEqual(name));
246 if (it != formatlist.end())
247 it->setViewer(command);
251 void Formats::setEditor(string const & name, string const & command)
254 FormatList::iterator it =
255 find_if(formatlist.begin(), formatlist.end(),
256 FormatNamesEqual(name));
257 if (it != formatlist.end())
258 it->setEditor(command);
262 bool Formats::viewURL(docstring const & url) {
263 Format const * format = getFormat("html");
267 string command = libScriptSearch(format->viewer());
269 if (!contains(command, token_from_format))
270 command += ' ' + token_from_format;
271 command = subst(command, token_from_format, quoteName(to_utf8(url)));
273 LYXERR(Debug::FILES, "Executing command: " << command);
276 one.startscript(Systemcall::DontWait, command);
278 // we can't report any sort of error, since we aren't waiting
283 bool Formats::view(Buffer const & buffer, FileName const & filename,
284 string const & format_name) const
286 if (filename.empty() || !filename.exists()) {
287 Alert::error(_("Cannot view file"),
288 bformat(_("File does not exist: %1$s"),
289 from_utf8(filename.absFileName())));
293 Format const * format = getFormat(format_name);
294 if (format && format->viewer().empty() &&
295 format->isChildFormat())
296 format = getFormat(format->parentFormat());
297 if (!format || format->viewer().empty()) {
298 // FIXME: I believe this is the wrong place to show alerts, it should be done
299 // by the caller (this should be "utility" code)
300 Alert::error(_("Cannot view file"),
301 bformat(_("No information for viewing %1$s"),
302 prettyName(format_name)));
306 if (format->viewer() == "auto") {
307 if (os::autoOpenFile(filename.absFileName(), os::VIEW))
310 Alert::error(_("Cannot view file"),
311 bformat(_("Auto-view file %1$s failed"),
312 from_utf8(filename.absFileName())));
317 string command = libScriptSearch(format->viewer());
319 if (format_name == "dvi" &&
320 !lyxrc.view_dvi_paper_option.empty()) {
321 string paper_size = buffer.params().paperSizeName(BufferParams::XDVI);
322 if (!paper_size.empty()) {
323 command += ' ' + lyxrc.view_dvi_paper_option;
324 command += ' ' + paper_size;
325 if (buffer.params().orientation == ORIENTATION_LANDSCAPE &&
326 buffer.params().papersize != PAPER_CUSTOM)
331 if (!contains(command, token_from_format))
332 command += ' ' + token_from_format;
334 command = subst(command, token_from_format, quoteName(filename.toFilesystemEncoding()));
335 command = subst(command, token_path_format, quoteName(onlyPath(filename.toFilesystemEncoding())));
336 command = subst(command, token_socket_format, quoteName(theServerSocket().address()));
337 LYXERR(Debug::FILES, "Executing command: " << command);
338 // FIXME UNICODE utf8 can be wrong for files
339 buffer.message(_("Executing command: ") + from_utf8(command));
342 one.startscript(Systemcall::DontWait, command);
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))
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);
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();
440 Formats system_formats;