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"
28 // FIXME: Q_WS_MACX is not available, it's in Qt
29 #ifdef USE_MACOSX_PACKAGING
30 #include "support/linkback/LinkBackProxy.h"
34 using namespace lyx::support;
38 namespace Alert = frontend::Alert;
39 namespace os = support::os;
43 string const token_from_format("$$i");
44 string const token_path_format("$$p");
45 string const token_socket_format("$$a");
48 class FormatNamesEqual : public unary_function<Format, bool> {
50 FormatNamesEqual(string const & name)
52 bool operator()(Format const & f) const
54 return f.name() == name_;
61 class FormatExtensionsEqual : public unary_function<Format, bool> {
63 FormatExtensionsEqual(string const & extension)
64 : extension_(extension) {}
65 bool operator()(Format const & f) const
67 return f.extension() == extension_;
75 bool operator<(Format const & a, Format const & b)
77 // use the compare_ascii_no_case instead of compare_no_case,
78 // because in turkish, 'i' is not the lowercase version of 'I',
79 // and thus turkish locale breaks parsing of tags.
81 return compare_ascii_no_case(a.prettyname(), b.prettyname()) < 0;
85 Format::Format(string const & n, string const & e, string const & p,
86 string const & s, string const & v, string const & ed,
88 : name_(n), extension_(e), prettyname_(p), shortcut_(s), viewer_(v),
89 editor_(ed), flags_(flags)
93 bool Format::dummy() const
95 return extension().empty();
99 bool Format::isChildFormat() const
103 return isdigit(name_[name_.length() - 1]);
107 string const Format::parentFormat() const
109 return name_.substr(0, name_.length() - 1);
113 // This method should return a reference, and throw an exception
114 // if the format named name cannot be found (Lgb)
115 Format const * Formats::getFormat(string const & name) const
117 FormatList::const_iterator cit =
118 find_if(formatlist.begin(), formatlist.end(),
119 FormatNamesEqual(name));
120 if (cit != formatlist.end())
127 string Formats::getFormatFromFile(FileName const & filename) const
129 if (filename.empty())
132 string const format = filename.guessFormatFromContents();
136 // try to find a format from the file extension.
137 string const ext = getExtension(filename.absFilename());
139 // this is ambigous if two formats have the same extension,
140 // but better than nothing
141 Formats::const_iterator cit =
142 find_if(formatlist.begin(), formatlist.end(),
143 FormatExtensionsEqual(ext));
144 if (cit != formats.end()) {
145 LYXERR(Debug::GRAPHICS, "\twill guess format from file extension: "
146 << ext << " -> " << cit->name());
154 static string fixCommand(string const & cmd, string const & ext,
155 os::auto_open_mode mode)
157 // configure.py says we do not want a viewer/editor
161 // Does the OS manage this format?
162 if (os::canAutoOpenFile(ext, mode))
165 // if configure.py found nothing, clear the command
166 if (token(cmd, ' ', 0) == "auto")
169 // use the command found by configure.py
174 void Formats::setAutoOpen()
176 FormatList::iterator fit = formatlist.begin();
177 FormatList::iterator const fend = formatlist.end();
178 for ( ; fit != fend ; ++fit) {
179 fit->setViewer(fixCommand(fit->viewer(), fit->extension(), os::VIEW));
180 fit->setEditor(fixCommand(fit->editor(), fit->extension(), os::EDIT));
185 int Formats::getNumber(string const & name) const
187 FormatList::const_iterator cit =
188 find_if(formatlist.begin(), formatlist.end(),
189 FormatNamesEqual(name));
190 if (cit != formatlist.end())
191 return distance(formatlist.begin(), cit);
197 void Formats::add(string const & name)
199 if (!getFormat(name))
200 add(name, name, name, string(), string(), string(),
205 void Formats::add(string const & name, string const & extension,
206 string const & prettyname, string const & shortcut,
207 string const & viewer, string const & editor,
210 FormatList::iterator it =
211 find_if(formatlist.begin(), formatlist.end(),
212 FormatNamesEqual(name));
213 if (it == formatlist.end())
214 formatlist.push_back(Format(name, extension, prettyname,
215 shortcut, viewer, editor, flags));
217 *it = Format(name, extension, prettyname, shortcut, viewer,
222 void Formats::erase(string const & name)
224 FormatList::iterator it =
225 find_if(formatlist.begin(), formatlist.end(),
226 FormatNamesEqual(name));
227 if (it != formatlist.end())
228 formatlist.erase(it);
234 std::sort(formatlist.begin(), formatlist.end());
238 void Formats::setViewer(string const & name, string const & command)
241 FormatList::iterator it =
242 find_if(formatlist.begin(), formatlist.end(),
243 FormatNamesEqual(name));
244 if (it != formatlist.end())
245 it->setViewer(command);
249 void Formats::setEditor(string const & name, string const & command)
252 FormatList::iterator it =
253 find_if(formatlist.begin(), formatlist.end(),
254 FormatNamesEqual(name));
255 if (it != formatlist.end())
256 it->setEditor(command);
260 bool Formats::view(Buffer const & buffer, FileName const & filename,
261 string const & format_name) const
263 if (filename.empty() || !filename.exists()) {
264 Alert::error(_("Cannot view file"),
265 bformat(_("File does not exist: %1$s"),
266 from_utf8(filename.absFilename())));
270 Format const * format = getFormat(format_name);
271 if (format && format->viewer().empty() &&
272 format->isChildFormat())
273 format = getFormat(format->parentFormat());
274 if (!format || format->viewer().empty()) {
275 // FIXME: I believe this is the wrong place to show alerts, it should be done
276 // by the caller (this should be "utility" code)
277 Alert::error(_("Cannot view file"),
278 bformat(_("No information for viewing %1$s"),
279 prettyName(format_name)));
283 if (format->viewer() == "auto") {
284 if (os::autoOpenFile(filename.absFilename(), os::VIEW))
287 Alert::error(_("Cannot view file"),
288 bformat(_("Auto-view file %1$s failed"),
289 from_utf8(filename.absFilename())));
294 string command = libScriptSearch(format->viewer());
296 if (format_name == "dvi" &&
297 !lyxrc.view_dvi_paper_option.empty()) {
298 string paper_size = buffer.params().paperSizeName(BufferParams::XDVI);
299 if (!paper_size.empty()) {
300 command += ' ' + lyxrc.view_dvi_paper_option;
301 command += ' ' + paper_size;
302 if (buffer.params().orientation == ORIENTATION_LANDSCAPE &&
303 buffer.params().papersize != PAPER_CUSTOM)
308 if (!contains(command, token_from_format))
309 command += ' ' + token_from_format;
311 command = subst(command, token_from_format, quoteName(filename.toFilesystemEncoding()));
312 command = subst(command, token_path_format, quoteName(onlyPath(filename.toFilesystemEncoding())));
313 command = subst(command, token_socket_format, quoteName(theServerSocket().address()));
314 LYXERR(Debug::FILES, "Executing command: " << command);
315 // FIXME UNICODE utf8 can be wrong for files
316 buffer.message(_("Executing command: ") + from_utf8(command));
319 int const res = one.startscript(Systemcall::DontWait, command);
322 Alert::error(_("Cannot view file"),
323 bformat(_("An error occurred whilst running %1$s"),
324 makeDisplayPath(command, 50)));
331 bool Formats::edit(Buffer const & buffer, FileName const & filename,
332 string const & format_name) const
334 if (filename.empty() || !filename.exists()) {
335 Alert::error(_("Cannot edit file"),
336 bformat(_("File does not exist: %1$s"),
337 from_utf8(filename.absFilename())));
341 // LinkBack files look like PDF, but have the .linkback extension
342 string const ext = getExtension(filename.absFilename());
343 if (format_name == "pdf" && ext == "linkback") {
344 #ifdef USE_MACOSX_PACKAGING
345 return editLinkBackFile(filename.absFilename().c_str());
347 Alert::error(_("Cannot edit file"),
348 _("LinkBack files can only be edited on Apple Mac OSX."));
350 #endif // USE_MACOSX_PACKAGING
353 Format const * format = getFormat(format_name);
354 if (format && format->editor().empty() &&
355 format->isChildFormat())
356 format = getFormat(format->parentFormat());
357 if (!format || format->editor().empty()) {
358 // FIXME: I believe this is the wrong place to show alerts, it should
359 // be done by the caller (this should be "utility" code)
360 Alert::error(_("Cannot edit file"),
361 bformat(_("No information for editing %1$s"),
362 prettyName(format_name)));
367 if (format->editor() == "auto") {
368 if (os::autoOpenFile(filename.absFilename(), os::EDIT))
371 Alert::error(_("Cannot edit file"),
372 bformat(_("Auto-edit file %1$s failed"),
373 from_utf8(filename.absFilename())));
378 string command = format->editor();
380 if (!contains(command, token_from_format))
381 command += ' ' + token_from_format;
383 command = subst(command, token_from_format, quoteName(filename.toFilesystemEncoding()));
384 command = subst(command, token_path_format, quoteName(onlyPath(filename.toFilesystemEncoding())));
385 command = subst(command, token_socket_format, quoteName(theServerSocket().address()));
386 LYXERR(Debug::FILES, "Executing command: " << command);
387 // FIXME UNICODE utf8 can be wrong for files
388 buffer.message(_("Executing command: ") + from_utf8(command));
391 int const res = one.startscript(Systemcall::DontWait, command);
394 Alert::error(_("Cannot edit file"),
395 bformat(_("An error occurred whilst running %1$s"),
396 makeDisplayPath(command, 50)));
403 docstring const Formats::prettyName(string const & name) const
405 Format const * format = getFormat(name);
407 return from_utf8(format->prettyname());
409 return from_utf8(name);
413 string const Formats::extension(string const & name) const
415 Format const * format = getFormat(name);
417 return format->extension();
427 Formats system_formats;