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"
19 #include "lyxsocket.h"
21 #include "frontends/Alert.h" //to be removed?
23 #include "support/filetools.h"
24 #include "support/lstrings.h"
25 #include "support/os.h"
26 #include "support/path.h"
27 #include "support/systemcall.h"
29 #include <boost/filesystem/operations.hpp>
31 using lyx::support::bformat;
32 using lyx::support::compare_ascii_no_case;
33 using lyx::support::contains;
34 using lyx::support::libScriptSearch;
35 using lyx::support::makeDisplayPath;
36 using lyx::support::onlyFilename;
37 using lyx::support::onlyPath;
38 using lyx::support::Path;
39 using lyx::support::quoteName;
40 using lyx::support::subst;
41 using lyx::support::Systemcall;
42 using lyx::support::token;
47 namespace fs = boost::filesystem;
48 namespace os = lyx::support::os;
50 extern LyXServerSocket * lyxsocket;
54 string const token_from("$$i");
55 string const token_path("$$p");
56 string const token_socket("$$a");
59 class FormatNamesEqual : public std::unary_function<Format, bool> {
61 FormatNamesEqual(string const & name)
63 bool operator()(Format const & f) const
65 return f.name() == name_;
72 class FormatExtensionsEqual : public std::unary_function<Format, bool> {
74 FormatExtensionsEqual(string const & extension)
75 : extension_(extension) {}
76 bool operator()(Format const & f) const
78 return f.extension() == extension_;
86 bool operator<(Format const & a, Format const & b)
88 // use the compare_ascii_no_case instead of compare_no_case,
89 // because in turkish, 'i' is not the lowercase version of 'I',
90 // and thus turkish locale breaks parsing of tags.
92 return compare_ascii_no_case(a.prettyname(), b.prettyname()) < 0;
96 Format::Format(string const & n, string const & e, string const & p,
97 string const & s, string const & v, string const & ed,
99 : name_(n), extension_(e), prettyname_(p), shortcut_(s), viewer_(v),
100 editor_(ed), document_(d)
104 bool Format::dummy() const
106 return extension().empty();
110 bool Format::isChildFormat() const
114 return isdigit(name_[name_.length() - 1]);
118 string const Format::parentFormat() const
120 return name_.substr(0, name_.length() - 1);
124 // This method should return a reference, and throw an exception
125 // if the format named name cannot be found (Lgb)
126 Format const * Formats::getFormat(string const & name) const
128 FormatList::const_iterator cit =
129 find_if(formatlist.begin(), formatlist.end(),
130 FormatNamesEqual(name));
131 if (cit != formatlist.end())
138 string Formats::getFormatFromFile(string const & filename) const
140 if (filename.empty())
143 string const format = lyx::support::getFormatFromContents(filename);
147 // try to find a format from the file extension.
148 string const ext(lyx::support::getExtension(filename));
150 // this is ambigous if two formats have the same extension,
151 // but better than nothing
152 Formats::const_iterator cit =
153 find_if(formatlist.begin(), formatlist.end(),
154 FormatExtensionsEqual(ext));
155 if (cit != formats.end()) {
156 lyxerr[Debug::GRAPHICS]
157 << "\twill guess format from file extension: "
158 << ext << " -> " << cit->name() << std::endl;
167 string fixCommand(string const & cmd, string const & ext,
168 os::auto_open_mode mode)
170 // configure.py says we do not want a viewer/editor
174 // Does the OS manage this format?
175 if (os::canAutoOpenFile(ext, mode))
178 // if configure.py found nothing, clear the command
179 if (token(cmd, ' ', 0) == "auto")
182 // use the command found by configure.py
188 void Formats::setAutoOpen()
190 FormatList::iterator fit = formatlist.begin();
191 FormatList::iterator const fend = formatlist.end();
192 for ( ; fit != fend ; ++fit) {
193 fit->setViewer(fixCommand(fit->viewer(), fit->extension(), os::VIEW));
194 fit->setEditor(fixCommand(fit->editor(), fit->extension(), os::EDIT));
199 int Formats::getNumber(string const & name) const
201 FormatList::const_iterator cit =
202 find_if(formatlist.begin(), formatlist.end(),
203 FormatNamesEqual(name));
204 if (cit != formatlist.end())
205 return distance(formatlist.begin(), cit);
211 void Formats::add(string const & name)
213 if (!getFormat(name))
214 add(name, name, name, string(), string(), string(), true);
218 void Formats::add(string const & name, string const & extension,
219 string const & prettyname, string const & shortcut,
220 string const & viewer, string const & editor, bool document)
222 FormatList::iterator it =
223 find_if(formatlist.begin(), formatlist.end(),
224 FormatNamesEqual(name));
225 if (it == formatlist.end())
226 formatlist.push_back(Format(name, extension, prettyname,
227 shortcut, viewer, editor,
230 *it = Format(name, extension, prettyname, shortcut, viewer,
235 void Formats::erase(string const & name)
237 FormatList::iterator it =
238 find_if(formatlist.begin(), formatlist.end(),
239 FormatNamesEqual(name));
240 if (it != formatlist.end())
241 formatlist.erase(it);
247 std::sort(formatlist.begin(), formatlist.end());
251 void Formats::setViewer(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->setViewer(command);
262 bool Formats::view(Buffer const & buffer, string const & filename,
263 string const & format_name) const
265 if (filename.empty() || !fs::exists(filename)) {
266 Alert::error(_("Cannot view file"),
267 bformat(_("File does not exist: %1$s"),
272 Format const * format = getFormat(format_name);
273 if (format && format->viewer().empty() &&
274 format->isChildFormat())
275 format = getFormat(format->parentFormat());
276 if (!format || format->viewer().empty()) {
277 // FIXME: I believe this is the wrong place to show alerts, it should be done
278 // by the caller (this should be "utility" code)
279 Alert::error(_("Cannot view file"),
280 bformat(_("No information for viewing %1$s"),
281 prettyName(format_name)));
285 if (format->viewer() == "auto") {
286 if (os::autoOpenFile(filename, os::VIEW))
289 Alert::error(_("Cannot view file"),
290 bformat(_("Auto-view file %1$s failed"),
296 string command = libScriptSearch(format->viewer());
298 if (format_name == "dvi" &&
299 !lyxrc.view_dvi_paper_option.empty()) {
300 command += ' ' + lyxrc.view_dvi_paper_option;
301 string paper_size = buffer.params().paperSizeName();
302 if (paper_size == "letter")
304 command += ' ' + paper_size;
305 if (buffer.params().orientation == ORIENTATION_LANDSCAPE)
309 if (!contains(command, token_from))
310 command += ' ' + token_from;
312 command = subst(command, token_from,
313 quoteName(onlyFilename(filename)));
314 command = subst(command, token_path, quoteName(onlyPath(filename)));
315 command = subst(command, token_socket, quoteName(lyxsocket->address()));
316 lyxerr[Debug::FILES] << "Executing command: " << command << std::endl;
317 buffer.message(_("Executing command: ") + command);
319 Path p(onlyPath(filename));
321 int const res = one.startscript(Systemcall::DontWait, command);
324 Alert::error(_("Cannot view file"),
325 bformat(_("An error occurred whilst running %1$s"),
326 makeDisplayPath(command, 50)));
333 bool Formats::edit(Buffer const & buffer, string const & filename,
334 string const & format_name) const
336 if (filename.empty() || !fs::exists(filename)) {
337 Alert::error(_("Cannot edit file"),
338 bformat(_("File does not exist: %1$s"),
343 Format const * format = getFormat(format_name);
344 if (format && format->editor().empty() &&
345 format->isChildFormat())
346 format = getFormat(format->parentFormat());
347 if (!format || format->editor().empty()) {
348 // FIXME: I believe this is the wrong place to show alerts, it should
349 // be done by the caller (this should be "utility" code)
350 Alert::error(_("Cannot edit file"),
351 bformat(_("No information for editing %1$s"),
352 prettyName(format_name)));
356 if (format->editor() == "auto") {
357 if (os::autoOpenFile(filename, os::EDIT))
360 Alert::error(_("Cannot edit file"),
361 bformat(_("Auto-edit file %1$s failed"),
367 string command = format->editor();
369 if (!contains(command, token_from))
370 command += ' ' + token_from;
372 command = subst(command, token_from,
373 quoteName(onlyFilename(filename)));
374 command = subst(command, token_path, quoteName(onlyPath(filename)));
375 command = subst(command, token_socket, quoteName(lyxsocket->address()));
376 lyxerr[Debug::FILES] << "Executing command: " << command << std::endl;
377 buffer.message(_("Executing command: ") + command);
379 Path p(onlyPath(filename));
381 int const res = one.startscript(Systemcall::DontWait, command);
384 Alert::error(_("Cannot edit file"),
385 bformat(_("An error occurred whilst running %1$s"),
386 makeDisplayPath(command, 50)));
393 string const Formats::prettyName(string const & name) const
395 Format const * format = getFormat(name);
397 return format->prettyname();
403 string const Formats::extension(string const & name) const
405 Format const * format = getFormat(name);
407 return format->extension();
417 Formats system_formats;