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 "ServerSocket.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>
38 using support::absolutePath;
39 using support::bformat;
40 using support::compare_ascii_no_case;
41 using support::contains;
42 using support::FileName;
43 using support::libScriptSearch;
44 using support::makeDisplayPath;
45 using support::onlyPath;
46 using support::quoteName;
48 using support::Systemcall;
51 namespace Alert = frontend::Alert;
52 namespace fs = boost::filesystem;
53 namespace os = support::os;
57 string const token_from_format("$$i");
58 string const token_path_format("$$p");
59 string const token_socket_format("$$a");
62 class FormatNamesEqual : public std::unary_function<Format, bool> {
64 FormatNamesEqual(string const & name)
66 bool operator()(Format const & f) const
68 return f.name() == name_;
75 class FormatExtensionsEqual : public std::unary_function<Format, bool> {
77 FormatExtensionsEqual(string const & extension)
78 : extension_(extension) {}
79 bool operator()(Format const & f) const
81 return f.extension() == extension_;
89 bool operator<(Format const & a, Format const & b)
91 // use the compare_ascii_no_case instead of compare_no_case,
92 // because in turkish, 'i' is not the lowercase version of 'I',
93 // and thus turkish locale breaks parsing of tags.
95 return compare_ascii_no_case(a.prettyname(), b.prettyname()) < 0;
99 Format::Format(string const & n, string const & e, string const & p,
100 string const & s, string const & v, string const & ed,
102 : name_(n), extension_(e), prettyname_(p), shortcut_(s), viewer_(v),
103 editor_(ed), flags_(flags)
107 bool Format::dummy() const
109 return extension().empty();
113 bool Format::isChildFormat() const
117 return isdigit(name_[name_.length() - 1]);
121 string const Format::parentFormat() const
123 return name_.substr(0, name_.length() - 1);
127 // This method should return a reference, and throw an exception
128 // if the format named name cannot be found (Lgb)
129 Format const * Formats::getFormat(string const & name) const
131 FormatList::const_iterator cit =
132 find_if(formatlist.begin(), formatlist.end(),
133 FormatNamesEqual(name));
134 if (cit != formatlist.end())
141 string Formats::getFormatFromFile(FileName const & filename) const
143 if (filename.empty())
146 string const format = support::getFormatFromContents(filename);
150 // try to find a format from the file extension.
151 string const ext(support::getExtension(filename.absFilename()));
153 // this is ambigous if two formats have the same extension,
154 // but better than nothing
155 Formats::const_iterator cit =
156 find_if(formatlist.begin(), formatlist.end(),
157 FormatExtensionsEqual(ext));
158 if (cit != formats.end()) {
159 LYXERR(Debug::GRAPHICS)
160 << "\twill guess format from file extension: "
161 << ext << " -> " << cit->name() << std::endl;
170 string fixCommand(string const & cmd, string const & ext,
171 os::auto_open_mode mode)
173 // configure.py says we do not want a viewer/editor
177 // Does the OS manage this format?
178 if (os::canAutoOpenFile(ext, mode))
181 // if configure.py found nothing, clear the command
182 if (token(cmd, ' ', 0) == "auto")
185 // use the command found by configure.py
191 void Formats::setAutoOpen()
193 FormatList::iterator fit = formatlist.begin();
194 FormatList::iterator const fend = formatlist.end();
195 for ( ; fit != fend ; ++fit) {
196 fit->setViewer(fixCommand(fit->viewer(), fit->extension(), os::VIEW));
197 fit->setEditor(fixCommand(fit->editor(), fit->extension(), os::EDIT));
202 int Formats::getNumber(string const & name) const
204 FormatList::const_iterator cit =
205 find_if(formatlist.begin(), formatlist.end(),
206 FormatNamesEqual(name));
207 if (cit != formatlist.end())
208 return distance(formatlist.begin(), cit);
214 void Formats::add(string const & name)
216 if (!getFormat(name))
217 add(name, name, name, string(), string(), string(),
222 void Formats::add(string const & name, string const & extension,
223 string const & prettyname, string const & shortcut,
224 string const & viewer, string const & editor,
227 FormatList::iterator it =
228 find_if(formatlist.begin(), formatlist.end(),
229 FormatNamesEqual(name));
230 if (it == formatlist.end())
231 formatlist.push_back(Format(name, extension, prettyname,
232 shortcut, viewer, editor, flags));
234 *it = Format(name, extension, prettyname, shortcut, viewer,
239 void Formats::erase(string const & name)
241 FormatList::iterator it =
242 find_if(formatlist.begin(), formatlist.end(),
243 FormatNamesEqual(name));
244 if (it != formatlist.end())
245 formatlist.erase(it);
251 std::sort(formatlist.begin(), formatlist.end());
255 void Formats::setViewer(string const & name, string const & command)
258 FormatList::iterator it =
259 find_if(formatlist.begin(), formatlist.end(),
260 FormatNamesEqual(name));
261 if (it != formatlist.end())
262 it->setViewer(command);
266 bool Formats::view(Buffer const & buffer, FileName const & filename,
267 string const & format_name) const
269 if (filename.empty() || !fs::exists(filename.toFilesystemEncoding())) {
270 Alert::error(_("Cannot view file"),
271 bformat(_("File does not exist: %1$s"),
272 from_utf8(filename.absFilename())));
276 Format const * format = getFormat(format_name);
277 if (format && format->viewer().empty() &&
278 format->isChildFormat())
279 format = getFormat(format->parentFormat());
280 if (!format || format->viewer().empty()) {
281 // FIXME: I believe this is the wrong place to show alerts, it should be done
282 // by the caller (this should be "utility" code)
283 Alert::error(_("Cannot view file"),
284 bformat(_("No information for viewing %1$s"),
285 prettyName(format_name)));
289 if (format->viewer() == "auto") {
290 if (os::autoOpenFile(filename.absFilename(), os::VIEW))
293 Alert::error(_("Cannot view file"),
294 bformat(_("Auto-view file %1$s failed"),
295 from_utf8(filename.absFilename())));
300 string command = libScriptSearch(format->viewer());
302 if (format_name == "dvi" &&
303 !lyxrc.view_dvi_paper_option.empty()) {
304 command += ' ' + lyxrc.view_dvi_paper_option;
305 string paper_size = buffer.params().paperSizeName();
306 if (paper_size == "letter")
308 command += ' ' + paper_size;
309 if (buffer.params().orientation == ORIENTATION_LANDSCAPE)
313 if (!contains(command, token_from_format))
314 command += ' ' + token_from_format;
316 command = subst(command, token_from_format, quoteName(filename.toFilesystemEncoding()));
317 command = subst(command, token_path_format, quoteName(onlyPath(filename.toFilesystemEncoding())));
318 command = subst(command, token_socket_format, quoteName(theServerSocket().address()));
319 LYXERR(Debug::FILES) << "Executing command: " << command << std::endl;
320 // FIXME UNICODE utf8 can be wrong for files
321 buffer.message(_("Executing command: ") + from_utf8(command));
324 int const res = one.startscript(Systemcall::DontWait, command);
327 Alert::error(_("Cannot view file"),
328 bformat(_("An error occurred whilst running %1$s"),
329 makeDisplayPath(command, 50)));
336 bool Formats::edit(Buffer const & buffer, FileName const & filename,
337 string const & format_name) const
339 if (filename.empty() || !fs::exists(filename.toFilesystemEncoding())) {
340 Alert::error(_("Cannot edit file"),
341 bformat(_("File does not exist: %1$s"),
342 from_utf8(filename.absFilename())));
346 Format const * format = getFormat(format_name);
347 if (format && format->editor().empty() &&
348 format->isChildFormat())
349 format = getFormat(format->parentFormat());
350 if (!format || format->editor().empty()) {
351 // FIXME: I believe this is the wrong place to show alerts, it should
352 // be done by the caller (this should be "utility" code)
353 Alert::error(_("Cannot edit file"),
354 bformat(_("No information for editing %1$s"),
355 prettyName(format_name)));
359 if (format->editor() == "auto") {
360 if (os::autoOpenFile(filename.absFilename(), os::EDIT))
363 Alert::error(_("Cannot edit file"),
364 bformat(_("Auto-edit file %1$s failed"),
365 from_utf8(filename.absFilename())));
370 string command = format->editor();
372 if (!contains(command, token_from_format))
373 command += ' ' + token_from_format;
375 command = subst(command, token_from_format, quoteName(filename.toFilesystemEncoding()));
376 command = subst(command, token_path_format, quoteName(onlyPath(filename.toFilesystemEncoding())));
377 command = subst(command, token_socket_format, quoteName(theServerSocket().address()));
378 LYXERR(Debug::FILES) << "Executing command: " << command << std::endl;
379 // FIXME UNICODE utf8 can be wrong for files
380 buffer.message(_("Executing command: ") + from_utf8(command));
383 int const res = one.startscript(Systemcall::DontWait, command);
386 Alert::error(_("Cannot edit file"),
387 bformat(_("An error occurred whilst running %1$s"),
388 makeDisplayPath(command, 50)));
395 docstring const Formats::prettyName(string const & name) const
397 Format const * format = getFormat(name);
399 return from_utf8(format->prettyname());
401 return from_utf8(name);
405 string const Formats::extension(string const & name) const
407 Format const * format = getFormat(name);
409 return format->extension();
419 Formats system_formats;