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"
36 using support::absolutePath;
37 using support::bformat;
38 using support::compare_ascii_no_case;
39 using support::contains;
40 using support::FileName;
41 using support::libScriptSearch;
42 using support::makeDisplayPath;
43 using support::onlyPath;
44 using support::quoteName;
46 using support::Systemcall;
49 namespace Alert = frontend::Alert;
50 namespace os = support::os;
54 string const token_from_format("$$i");
55 string const token_path_format("$$p");
56 string const token_socket_format("$$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), flags_(flags)
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(FileName const & filename) const
140 if (filename.empty())
143 string const format = filename.guessFormatFromContents();
147 // try to find a format from the file extension.
148 string const ext = support::getExtension(filename.absFilename());
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;
166 static 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
186 void Formats::setAutoOpen()
188 FormatList::iterator fit = formatlist.begin();
189 FormatList::iterator const fend = formatlist.end();
190 for ( ; fit != fend ; ++fit) {
191 fit->setViewer(fixCommand(fit->viewer(), fit->extension(), os::VIEW));
192 fit->setEditor(fixCommand(fit->editor(), fit->extension(), os::EDIT));
197 int Formats::getNumber(string const & name) const
199 FormatList::const_iterator cit =
200 find_if(formatlist.begin(), formatlist.end(),
201 FormatNamesEqual(name));
202 if (cit != formatlist.end())
203 return distance(formatlist.begin(), cit);
209 void Formats::add(string const & name)
211 if (!getFormat(name))
212 add(name, name, name, string(), string(), string(),
217 void Formats::add(string const & name, string const & extension,
218 string const & prettyname, string const & shortcut,
219 string const & viewer, string const & editor,
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, flags));
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, FileName const & filename,
262 string const & format_name) const
264 if (filename.empty() || !filename.exists()) {
265 Alert::error(_("Cannot view file"),
266 bformat(_("File does not exist: %1$s"),
267 from_utf8(filename.absFilename())));
271 Format const * format = getFormat(format_name);
272 if (format && format->viewer().empty() &&
273 format->isChildFormat())
274 format = getFormat(format->parentFormat());
275 if (!format || format->viewer().empty()) {
276 // FIXME: I believe this is the wrong place to show alerts, it should be done
277 // by the caller (this should be "utility" code)
278 Alert::error(_("Cannot view file"),
279 bformat(_("No information for viewing %1$s"),
280 prettyName(format_name)));
284 if (format->viewer() == "auto") {
285 if (os::autoOpenFile(filename.absFilename(), os::VIEW))
288 Alert::error(_("Cannot view file"),
289 bformat(_("Auto-view file %1$s failed"),
290 from_utf8(filename.absFilename())));
295 string command = libScriptSearch(format->viewer());
297 if (format_name == "dvi" &&
298 !lyxrc.view_dvi_paper_option.empty()) {
299 command += ' ' + lyxrc.view_dvi_paper_option;
300 string paper_size = buffer.params().paperSizeName();
301 if (paper_size == "letter")
303 command += ' ' + paper_size;
304 if (buffer.params().orientation == ORIENTATION_LANDSCAPE)
308 if (!contains(command, token_from_format))
309 command += ' ' + token_from_format;
311 command = subst(command, token_from_format, quoteName(filename.toFilesystemEncoding()));
312 command = subst(command, token_path_format, quoteName(onlyPath(filename.toFilesystemEncoding())));
313 command = subst(command, token_socket_format, quoteName(theServerSocket().address()));
314 LYXERR(Debug::FILES) << "Executing command: " << command << std::endl;
315 // FIXME UNICODE utf8 can be wrong for files
316 buffer.message(_("Executing command: ") + 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, FileName const & filename,
332 string const & format_name) const
334 if (filename.empty() || !filename.exists()) {
335 Alert::error(_("Cannot edit file"),
336 bformat(_("File does not exist: %1$s"),
337 from_utf8(filename.absFilename())));
341 Format const * format = getFormat(format_name);
342 if (format && format->editor().empty() &&
343 format->isChildFormat())
344 format = getFormat(format->parentFormat());
345 if (!format || format->editor().empty()) {
346 // FIXME: I believe this is the wrong place to show alerts, it should
347 // be done by the caller (this should be "utility" code)
348 Alert::error(_("Cannot edit file"),
349 bformat(_("No information for editing %1$s"),
350 prettyName(format_name)));
354 if (format->editor() == "auto") {
355 if (os::autoOpenFile(filename.absFilename(), os::EDIT))
358 Alert::error(_("Cannot edit file"),
359 bformat(_("Auto-edit file %1$s failed"),
360 from_utf8(filename.absFilename())));
365 string command = format->editor();
367 if (!contains(command, token_from_format))
368 command += ' ' + token_from_format;
370 command = subst(command, token_from_format, quoteName(filename.toFilesystemEncoding()));
371 command = subst(command, token_path_format, quoteName(onlyPath(filename.toFilesystemEncoding())));
372 command = subst(command, token_socket_format, quoteName(theServerSocket().address()));
373 LYXERR(Debug::FILES) << "Executing command: " << command << std::endl;
374 // FIXME UNICODE utf8 can be wrong for files
375 buffer.message(_("Executing command: ") + 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 from_utf8(format->prettyname());
396 return 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;