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/Application.h"
22 #include "frontends/Alert.h" //to be removed?
24 #include "support/filetools.h"
25 #include "support/lstrings.h"
26 #include "support/os.h"
27 #include "support/systemcall.h"
29 #include <boost/filesystem/operations.hpp>
34 using support::absolutePath;
35 using support::bformat;
36 using support::compare_ascii_no_case;
37 using support::contains;
38 using support::libScriptSearch;
39 using support::makeDisplayPath;
40 using support::onlyPath;
41 using support::quoteName;
43 using support::Systemcall;
49 namespace Alert = frontend::Alert;
50 namespace fs = boost::filesystem;
51 namespace os = support::os;
55 string const token_from("$$i");
56 string const token_path("$$p");
57 string const token_socket("$$a");
60 class FormatNamesEqual : public std::unary_function<Format, bool> {
62 FormatNamesEqual(string const & name)
64 bool operator()(Format const & f) const
66 return f.name() == name_;
73 class FormatExtensionsEqual : public std::unary_function<Format, bool> {
75 FormatExtensionsEqual(string const & extension)
76 : extension_(extension) {}
77 bool operator()(Format const & f) const
79 return f.extension() == extension_;
87 bool operator<(Format const & a, Format const & b)
89 // use the compare_ascii_no_case instead of compare_no_case,
90 // because in turkish, 'i' is not the lowercase version of 'I',
91 // and thus turkish locale breaks parsing of tags.
93 return compare_ascii_no_case(a.prettyname(), b.prettyname()) < 0;
97 Format::Format(string const & n, string const & e, string const & p,
98 string const & s, string const & v, string const & ed,
100 : name_(n), extension_(e), prettyname_(p), shortcut_(s), viewer_(v),
101 editor_(ed), flags_(flags)
105 bool Format::dummy() const
107 return extension().empty();
111 bool Format::isChildFormat() const
115 return isdigit(name_[name_.length() - 1]);
119 string const Format::parentFormat() const
121 return name_.substr(0, name_.length() - 1);
125 // This method should return a reference, and throw an exception
126 // if the format named name cannot be found (Lgb)
127 Format const * Formats::getFormat(string const & name) const
129 FormatList::const_iterator cit =
130 find_if(formatlist.begin(), formatlist.end(),
131 FormatNamesEqual(name));
132 if (cit != formatlist.end())
139 string Formats::getFormatFromFile(string const & filename) const
141 if (filename.empty())
144 string const format = support::getFormatFromContents(filename);
148 // try to find a format from the file extension.
149 string const ext(support::getExtension(filename));
151 // this is ambigous if two formats have the same extension,
152 // but better than nothing
153 Formats::const_iterator cit =
154 find_if(formatlist.begin(), formatlist.end(),
155 FormatExtensionsEqual(ext));
156 if (cit != formats.end()) {
157 lyxerr[Debug::GRAPHICS]
158 << "\twill guess format from file extension: "
159 << ext << " -> " << cit->name() << std::endl;
168 string fixCommand(string const & cmd, string const & ext,
169 os::auto_open_mode mode)
171 // configure.py says we do not want a viewer/editor
175 // Does the OS manage this format?
176 if (os::canAutoOpenFile(ext, mode))
179 // if configure.py found nothing, clear the command
180 if (token(cmd, ' ', 0) == "auto")
183 // use the command found by configure.py
189 void Formats::setAutoOpen()
191 FormatList::iterator fit = formatlist.begin();
192 FormatList::iterator const fend = formatlist.end();
193 for ( ; fit != fend ; ++fit) {
194 fit->setViewer(fixCommand(fit->viewer(), fit->extension(), os::VIEW));
195 fit->setEditor(fixCommand(fit->editor(), fit->extension(), os::EDIT));
200 int Formats::getNumber(string const & name) const
202 FormatList::const_iterator cit =
203 find_if(formatlist.begin(), formatlist.end(),
204 FormatNamesEqual(name));
205 if (cit != formatlist.end())
206 return distance(formatlist.begin(), cit);
212 void Formats::add(string const & name)
214 if (!getFormat(name))
215 add(name, name, name, string(), string(), string(),
220 void Formats::add(string const & name, string const & extension,
221 string const & prettyname, string const & shortcut,
222 string const & viewer, string const & editor,
225 FormatList::iterator it =
226 find_if(formatlist.begin(), formatlist.end(),
227 FormatNamesEqual(name));
228 if (it == formatlist.end())
229 formatlist.push_back(Format(name, extension, prettyname,
230 shortcut, viewer, editor, flags));
232 *it = Format(name, extension, prettyname, shortcut, viewer,
237 void Formats::erase(string const & name)
239 FormatList::iterator it =
240 find_if(formatlist.begin(), formatlist.end(),
241 FormatNamesEqual(name));
242 if (it != formatlist.end())
243 formatlist.erase(it);
249 std::sort(formatlist.begin(), formatlist.end());
253 void Formats::setViewer(string const & name, string const & command)
256 FormatList::iterator it =
257 find_if(formatlist.begin(), formatlist.end(),
258 FormatNamesEqual(name));
259 if (it != formatlist.end())
260 it->setViewer(command);
264 bool Formats::view(Buffer const & buffer, string const & filename,
265 string const & format_name) const
267 BOOST_ASSERT(absolutePath(filename));
268 if (filename.empty() || !fs::exists(filename)) {
269 Alert::error(_("Cannot view file"),
270 bformat(_("File does not exist: %1$s"),
271 from_utf8(filename)));
275 Format const * format = getFormat(format_name);
276 if (format && format->viewer().empty() &&
277 format->isChildFormat())
278 format = getFormat(format->parentFormat());
279 if (!format || format->viewer().empty()) {
280 // FIXME: I believe this is the wrong place to show alerts, it should be done
281 // by the caller (this should be "utility" code)
282 Alert::error(_("Cannot view file"),
283 bformat(_("No information for viewing %1$s"),
284 prettyName(format_name)));
288 if (format->viewer() == "auto") {
289 if (os::autoOpenFile(filename, os::VIEW))
292 Alert::error(_("Cannot view file"),
293 bformat(_("Auto-view file %1$s failed"),
294 from_utf8(filename)));
299 string command = libScriptSearch(format->viewer());
301 if (format_name == "dvi" &&
302 !lyxrc.view_dvi_paper_option.empty()) {
303 command += ' ' + lyxrc.view_dvi_paper_option;
304 string paper_size = buffer.params().paperSizeName();
305 if (paper_size == "letter")
307 command += ' ' + paper_size;
308 if (buffer.params().orientation == ORIENTATION_LANDSCAPE)
312 if (!contains(command, token_from))
313 command += ' ' + token_from;
315 command = subst(command, token_from, quoteName(filename));
316 command = subst(command, token_path, quoteName(onlyPath(filename)));
317 command = subst(command, token_socket, quoteName(theLyXServerSocket().address()));
318 lyxerr[Debug::FILES] << "Executing command: " << command << std::endl;
319 buffer.message(_("Executing command: ") + from_utf8(command));
322 int const res = one.startscript(Systemcall::DontWait, command);
325 Alert::error(_("Cannot view file"),
326 bformat(_("An error occurred whilst running %1$s"),
327 makeDisplayPath(command, 50)));
334 bool Formats::edit(Buffer const & buffer, string const & filename,
335 string const & format_name) const
337 BOOST_ASSERT(absolutePath(filename));
338 if (filename.empty() || !fs::exists(filename)) {
339 Alert::error(_("Cannot edit file"),
340 bformat(_("File does not exist: %1$s"),
341 from_utf8(filename)));
345 Format const * format = getFormat(format_name);
346 if (format && format->editor().empty() &&
347 format->isChildFormat())
348 format = getFormat(format->parentFormat());
349 if (!format || format->editor().empty()) {
350 // FIXME: I believe this is the wrong place to show alerts, it should
351 // be done by the caller (this should be "utility" code)
352 Alert::error(_("Cannot edit file"),
353 bformat(_("No information for editing %1$s"),
354 prettyName(format_name)));
358 if (format->editor() == "auto") {
359 if (os::autoOpenFile(filename, os::EDIT))
362 Alert::error(_("Cannot edit file"),
363 bformat(_("Auto-edit file %1$s failed"),
364 from_utf8(filename)));
369 string command = format->editor();
371 if (!contains(command, token_from))
372 command += ' ' + token_from;
374 command = subst(command, token_from, quoteName(filename));
375 command = subst(command, token_path, quoteName(onlyPath(filename)));
376 command = subst(command, token_socket, quoteName(theLyXServerSocket().address()));
377 lyxerr[Debug::FILES] << "Executing command: " << command << std::endl;
378 buffer.message(_("Executing command: ") + from_utf8(command));
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 docstring const Formats::prettyName(string const & name) const
395 Format const * format = getFormat(name);
397 return from_utf8(format->prettyname());
399 return from_utf8(name);
403 string const Formats::extension(string const & name) const
405 Format const * format = getFormat(name);
407 return format->extension();
417 Formats system_formats;