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"
20 #include "frontends/Application.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/systemcall.h"
28 #include <boost/filesystem/operations.hpp>
31 using lyx::support::absolutePath;
32 using lyx::support::bformat;
33 using lyx::support::compare_ascii_no_case;
34 using lyx::support::contains;
35 using lyx::support::libScriptSearch;
36 using lyx::support::makeDisplayPath;
37 using lyx::support::onlyPath;
38 using lyx::support::quoteName;
39 using lyx::support::subst;
40 using lyx::support::Systemcall;
41 using lyx::support::token;
46 namespace fs = boost::filesystem;
47 namespace os = lyx::support::os;
51 string const token_from("$$i");
52 string const token_path("$$p");
53 string const token_socket("$$a");
56 class FormatNamesEqual : public std::unary_function<Format, bool> {
58 FormatNamesEqual(string const & name)
60 bool operator()(Format const & f) const
62 return f.name() == name_;
69 class FormatExtensionsEqual : public std::unary_function<Format, bool> {
71 FormatExtensionsEqual(string const & extension)
72 : extension_(extension) {}
73 bool operator()(Format const & f) const
75 return f.extension() == extension_;
83 bool operator<(Format const & a, Format const & b)
85 // use the compare_ascii_no_case instead of compare_no_case,
86 // because in turkish, 'i' is not the lowercase version of 'I',
87 // and thus turkish locale breaks parsing of tags.
89 return compare_ascii_no_case(a.prettyname(), b.prettyname()) < 0;
93 Format::Format(string const & n, string const & e, string const & p,
94 string const & s, string const & v, string const & ed,
96 : name_(n), extension_(e), prettyname_(p), shortcut_(s), viewer_(v),
97 editor_(ed), document_(d)
101 bool Format::dummy() const
103 return extension().empty();
107 bool Format::isChildFormat() const
111 return isdigit(name_[name_.length() - 1]);
115 string const Format::parentFormat() const
117 return name_.substr(0, name_.length() - 1);
121 // This method should return a reference, and throw an exception
122 // if the format named name cannot be found (Lgb)
123 Format const * Formats::getFormat(string const & name) const
125 FormatList::const_iterator cit =
126 find_if(formatlist.begin(), formatlist.end(),
127 FormatNamesEqual(name));
128 if (cit != formatlist.end())
135 string Formats::getFormatFromFile(string const & filename) const
137 if (filename.empty())
140 string const format = lyx::support::getFormatFromContents(filename);
144 // try to find a format from the file extension.
145 string const ext(lyx::support::getExtension(filename));
147 // this is ambigous if two formats have the same extension,
148 // but better than nothing
149 Formats::const_iterator cit =
150 find_if(formatlist.begin(), formatlist.end(),
151 FormatExtensionsEqual(ext));
152 if (cit != formats.end()) {
153 lyxerr[Debug::GRAPHICS]
154 << "\twill guess format from file extension: "
155 << ext << " -> " << cit->name() << std::endl;
164 string fixCommand(string const & cmd, string const & ext,
165 os::auto_open_mode mode)
167 // configure.py says we do not want a viewer/editor
171 // Does the OS manage this format?
172 if (os::canAutoOpenFile(ext, mode))
175 // if configure.py found nothing, clear the command
176 if (token(cmd, ' ', 0) == "auto")
179 // use the command found by configure.py
185 void Formats::setAutoOpen()
187 FormatList::iterator fit = formatlist.begin();
188 FormatList::iterator const fend = formatlist.end();
189 for ( ; fit != fend ; ++fit) {
190 fit->setViewer(fixCommand(fit->viewer(), fit->extension(), os::VIEW));
191 fit->setEditor(fixCommand(fit->editor(), fit->extension(), os::EDIT));
196 int Formats::getNumber(string const & name) const
198 FormatList::const_iterator cit =
199 find_if(formatlist.begin(), formatlist.end(),
200 FormatNamesEqual(name));
201 if (cit != formatlist.end())
202 return distance(formatlist.begin(), cit);
208 void Formats::add(string const & name)
210 if (!getFormat(name))
211 add(name, name, name, string(), string(), string(), true);
215 void Formats::add(string const & name, string const & extension,
216 string const & prettyname, string const & shortcut,
217 string const & viewer, string const & editor, bool document)
219 FormatList::iterator it =
220 find_if(formatlist.begin(), formatlist.end(),
221 FormatNamesEqual(name));
222 if (it == formatlist.end())
223 formatlist.push_back(Format(name, extension, prettyname,
224 shortcut, viewer, editor,
227 *it = Format(name, extension, prettyname, shortcut, viewer,
232 void Formats::erase(string const & name)
234 FormatList::iterator it =
235 find_if(formatlist.begin(), formatlist.end(),
236 FormatNamesEqual(name));
237 if (it != formatlist.end())
238 formatlist.erase(it);
244 std::sort(formatlist.begin(), formatlist.end());
248 void Formats::setViewer(string const & name, string const & command)
251 FormatList::iterator it =
252 find_if(formatlist.begin(), formatlist.end(),
253 FormatNamesEqual(name));
254 if (it != formatlist.end())
255 it->setViewer(command);
259 bool Formats::view(Buffer const & buffer, string const & filename,
260 string const & format_name) const
262 BOOST_ASSERT(absolutePath(filename));
263 if (filename.empty() || !fs::exists(filename)) {
264 Alert::error(_("Cannot view file"),
265 bformat(_("File does not exist: %1$s"),
266 lyx::from_utf8(filename)));
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, os::VIEW))
287 Alert::error(_("Cannot view file"),
288 bformat(_("Auto-view file %1$s failed"),
289 lyx::from_utf8(filename)));
294 string command = libScriptSearch(format->viewer());
296 if (format_name == "dvi" &&
297 !lyxrc.view_dvi_paper_option.empty()) {
298 command += ' ' + lyxrc.view_dvi_paper_option;
299 string paper_size = buffer.params().paperSizeName();
300 if (paper_size == "letter")
302 command += ' ' + paper_size;
303 if (buffer.params().orientation == ORIENTATION_LANDSCAPE)
307 if (!contains(command, token_from))
308 command += ' ' + token_from;
310 command = subst(command, token_from, quoteName(filename));
311 command = subst(command, token_path, quoteName(onlyPath(filename)));
312 command = subst(command, token_socket, quoteName(theApp->socket().address()));
313 lyxerr[Debug::FILES] << "Executing command: " << command << std::endl;
314 buffer.message(_("Executing command: ") + lyx::from_utf8(command));
317 int const res = one.startscript(Systemcall::DontWait, command);
320 Alert::error(_("Cannot view file"),
321 bformat(_("An error occurred whilst running %1$s"),
322 makeDisplayPath(command, 50)));
329 bool Formats::edit(Buffer const & buffer, string const & filename,
330 string const & format_name) const
332 BOOST_ASSERT(absolutePath(filename));
333 if (filename.empty() || !fs::exists(filename)) {
334 Alert::error(_("Cannot edit file"),
335 bformat(_("File does not exist: %1$s"),
336 lyx::from_utf8(filename)));
340 Format const * format = getFormat(format_name);
341 if (format && format->editor().empty() &&
342 format->isChildFormat())
343 format = getFormat(format->parentFormat());
344 if (!format || format->editor().empty()) {
345 // FIXME: I believe this is the wrong place to show alerts, it should
346 // be done by the caller (this should be "utility" code)
347 Alert::error(_("Cannot edit file"),
348 bformat(_("No information for editing %1$s"),
349 prettyName(format_name)));
353 if (format->editor() == "auto") {
354 if (os::autoOpenFile(filename, os::EDIT))
357 Alert::error(_("Cannot edit file"),
358 bformat(_("Auto-edit file %1$s failed"),
359 lyx::from_utf8(filename)));
364 string command = format->editor();
366 if (!contains(command, token_from))
367 command += ' ' + token_from;
369 command = subst(command, token_from, quoteName(filename));
370 command = subst(command, token_path, quoteName(onlyPath(filename)));
371 command = subst(command, token_socket, quoteName(theApp->socket().address()));
372 lyxerr[Debug::FILES] << "Executing command: " << command << std::endl;
373 buffer.message(_("Executing command: ") + lyx::from_utf8(command));
376 int const res = one.startscript(Systemcall::DontWait, command);
379 Alert::error(_("Cannot edit file"),
380 bformat(_("An error occurred whilst running %1$s"),
381 makeDisplayPath(command, 50)));
388 docstring const Formats::prettyName(string const & name) const
390 Format const * format = getFormat(name);
392 return lyx::from_utf8(format->prettyname());
394 return lyx::from_utf8(name);
398 string const Formats::extension(string const & name) const
400 Format const * format = getFormat(name);
402 return format->extension();
412 Formats system_formats;