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/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;
49 extern LyXServerSocket * lyxsocket;
53 string const token_from("$$i");
54 string const token_path("$$p");
55 string const token_socket("$$a");
58 class FormatNamesEqual : public std::unary_function<Format, bool> {
60 FormatNamesEqual(string const & name)
62 bool operator()(Format const & f) const
64 return f.name() == name_;
71 class FormatExtensionsEqual : public std::unary_function<Format, bool> {
73 FormatExtensionsEqual(string const & extension)
74 : extension_(extension) {}
75 bool operator()(Format const & f) const
77 return f.extension() == extension_;
85 bool operator<(Format const & a, Format const & b)
87 // use the compare_ascii_no_case instead of compare_no_case,
88 // because in turkish, 'i' is not the lowercase version of 'I',
89 // and thus turkish locale breaks parsing of tags.
91 return compare_ascii_no_case(a.prettyname(), b.prettyname()) < 0;
95 Format::Format(string const & n, string const & e, string const & p,
96 string const & s, string const & v, string const & ed,
98 : name_(n), extension_(e), prettyname_(p), shortcut_(s), viewer_(v),
99 editor_(ed), document_(d)
103 bool Format::dummy() const
105 return extension().empty();
109 bool Format::isChildFormat() const
113 return isdigit(name_[name_.length() - 1]);
117 string const Format::parentFormat() const
119 return name_.substr(0, name_.length() - 1);
123 // This method should return a reference, and throw an exception
124 // if the format named name cannot be found (Lgb)
125 Format const * Formats::getFormat(string const & name) const
127 FormatList::const_iterator cit =
128 find_if(formatlist.begin(), formatlist.end(),
129 FormatNamesEqual(name));
130 if (cit != formatlist.end())
137 string Formats::getFormatFromFile(string const & filename) const
139 if (filename.empty())
142 string const format = lyx::support::getFormatFromContents(filename);
146 // try to find a format from the file extension.
147 string const ext(lyx::support::getExtension(filename));
149 // this is ambigous if two formats have the same extension,
150 // but better than nothing
151 Formats::const_iterator cit =
152 find_if(formatlist.begin(), formatlist.end(),
153 FormatExtensionsEqual(ext));
154 if (cit != formats.end()) {
155 lyxerr[Debug::GRAPHICS]
156 << "\twill guess format from file extension: "
157 << ext << " -> " << cit->name() << std::endl;
166 string fixCommand(string const & cmd, string const & ext,
167 os::auto_open_mode mode)
169 // configure.py says we do not want a viewer/editor
173 // Does the OS manage this format?
174 if (os::canAutoOpenFile(ext, mode))
177 // if configure.py found nothing, clear the command
178 if (token(cmd, ' ', 0) == "auto")
181 // use the command found by configure.py
187 void Formats::setAutoOpen()
189 FormatList::iterator fit = formatlist.begin();
190 FormatList::iterator const fend = formatlist.end();
191 for ( ; fit != fend ; ++fit) {
192 fit->setViewer(fixCommand(fit->viewer(), fit->extension(), os::VIEW));
193 fit->setEditor(fixCommand(fit->editor(), fit->extension(), os::EDIT));
198 int Formats::getNumber(string const & name) const
200 FormatList::const_iterator cit =
201 find_if(formatlist.begin(), formatlist.end(),
202 FormatNamesEqual(name));
203 if (cit != formatlist.end())
204 return distance(formatlist.begin(), cit);
210 void Formats::add(string const & name)
212 if (!getFormat(name))
213 add(name, name, name, string(), string(), string(), true);
217 void Formats::add(string const & name, string const & extension,
218 string const & prettyname, string const & shortcut,
219 string const & viewer, string const & editor, bool document)
221 FormatList::iterator it =
222 find_if(formatlist.begin(), formatlist.end(),
223 FormatNamesEqual(name));
224 if (it == formatlist.end())
225 formatlist.push_back(Format(name, extension, prettyname,
226 shortcut, viewer, editor,
229 *it = Format(name, extension, prettyname, shortcut, viewer,
234 void Formats::erase(string const & name)
236 FormatList::iterator it =
237 find_if(formatlist.begin(), formatlist.end(),
238 FormatNamesEqual(name));
239 if (it != formatlist.end())
240 formatlist.erase(it);
246 std::sort(formatlist.begin(), formatlist.end());
250 void Formats::setViewer(string const & name, string const & command)
253 FormatList::iterator it =
254 find_if(formatlist.begin(), formatlist.end(),
255 FormatNamesEqual(name));
256 if (it != formatlist.end())
257 it->setViewer(command);
261 bool Formats::view(Buffer const & buffer, string const & filename,
262 string const & format_name) const
264 BOOST_ASSERT(absolutePath(filename));
265 if (filename.empty() || !fs::exists(filename)) {
266 Alert::error(_("Cannot view file"),
267 bformat(_("File does not exist: %1$s"),
268 lyx::from_utf8(filename)));
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"),
291 lyx::from_utf8(filename)));
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, quoteName(filename));
313 command = subst(command, token_path, quoteName(onlyPath(filename)));
314 command = subst(command, token_socket, quoteName(lyxsocket->address()));
315 lyxerr[Debug::FILES] << "Executing command: " << command << std::endl;
316 buffer.message(_("Executing command: ") + lyx::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, string const & filename,
332 string const & format_name) const
334 BOOST_ASSERT(absolutePath(filename));
335 if (filename.empty() || !fs::exists(filename)) {
336 Alert::error(_("Cannot edit file"),
337 bformat(_("File does not exist: %1$s"),
338 lyx::from_utf8(filename)));
342 Format const * format = getFormat(format_name);
343 if (format && format->editor().empty() &&
344 format->isChildFormat())
345 format = getFormat(format->parentFormat());
346 if (!format || format->editor().empty()) {
347 // FIXME: I believe this is the wrong place to show alerts, it should
348 // be done by the caller (this should be "utility" code)
349 Alert::error(_("Cannot edit file"),
350 bformat(_("No information for editing %1$s"),
351 prettyName(format_name)));
355 if (format->editor() == "auto") {
356 if (os::autoOpenFile(filename, os::EDIT))
359 Alert::error(_("Cannot edit file"),
360 bformat(_("Auto-edit file %1$s failed"),
361 lyx::from_utf8(filename)));
366 string command = format->editor();
368 if (!contains(command, token_from))
369 command += ' ' + token_from;
371 command = subst(command, token_from, quoteName(filename));
372 command = subst(command, token_path, quoteName(onlyPath(filename)));
373 command = subst(command, token_socket, quoteName(lyxsocket->address()));
374 lyxerr[Debug::FILES] << "Executing command: " << command << std::endl;
375 buffer.message(_("Executing command: ") + lyx::from_utf8(command));
378 int const res = one.startscript(Systemcall::DontWait, command);
381 Alert::error(_("Cannot edit file"),
382 bformat(_("An error occurred whilst running %1$s"),
383 makeDisplayPath(command, 50)));
390 docstring const Formats::prettyName(string const & name) const
392 Format const * format = getFormat(name);
394 return lyx::from_utf8(format->prettyname());
396 return lyx::from_utf8(name);
400 string const Formats::extension(string const & name) const
402 Format const * format = getFormat(name);
404 return format->extension();
414 Formats system_formats;