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 = support::getFormatFromContents(filename);
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;
167 string fixCommand(string const & cmd, string const & ext,
168 os::auto_open_mode mode)
170 // configure.py says we do not want a viewer/editor
174 // Does the OS manage this format?
175 if (os::canAutoOpenFile(ext, mode))
178 // if configure.py found nothing, clear the command
179 if (token(cmd, ' ', 0) == "auto")
182 // use the command found by configure.py
188 void Formats::setAutoOpen()
190 FormatList::iterator fit = formatlist.begin();
191 FormatList::iterator const fend = formatlist.end();
192 for ( ; fit != fend ; ++fit) {
193 fit->setViewer(fixCommand(fit->viewer(), fit->extension(), os::VIEW));
194 fit->setEditor(fixCommand(fit->editor(), fit->extension(), os::EDIT));
199 int Formats::getNumber(string const & name) const
201 FormatList::const_iterator cit =
202 find_if(formatlist.begin(), formatlist.end(),
203 FormatNamesEqual(name));
204 if (cit != formatlist.end())
205 return distance(formatlist.begin(), cit);
211 void Formats::add(string const & name)
213 if (!getFormat(name))
214 add(name, name, name, string(), string(), string(),
219 void Formats::add(string const & name, string const & extension,
220 string const & prettyname, string const & shortcut,
221 string const & viewer, string const & editor,
224 FormatList::iterator it =
225 find_if(formatlist.begin(), formatlist.end(),
226 FormatNamesEqual(name));
227 if (it == formatlist.end())
228 formatlist.push_back(Format(name, extension, prettyname,
229 shortcut, viewer, editor, flags));
231 *it = Format(name, extension, prettyname, shortcut, viewer,
236 void Formats::erase(string const & name)
238 FormatList::iterator it =
239 find_if(formatlist.begin(), formatlist.end(),
240 FormatNamesEqual(name));
241 if (it != formatlist.end())
242 formatlist.erase(it);
248 std::sort(formatlist.begin(), formatlist.end());
252 void Formats::setViewer(string const & name, string const & command)
255 FormatList::iterator it =
256 find_if(formatlist.begin(), formatlist.end(),
257 FormatNamesEqual(name));
258 if (it != formatlist.end())
259 it->setViewer(command);
263 bool Formats::view(Buffer const & buffer, FileName const & filename,
264 string const & format_name) const
266 if (filename.empty() || !filename.exists()) {
267 Alert::error(_("Cannot view file"),
268 bformat(_("File does not exist: %1$s"),
269 from_utf8(filename.absFilename())));
273 Format const * format = getFormat(format_name);
274 if (format && format->viewer().empty() &&
275 format->isChildFormat())
276 format = getFormat(format->parentFormat());
277 if (!format || format->viewer().empty()) {
278 // FIXME: I believe this is the wrong place to show alerts, it should be done
279 // by the caller (this should be "utility" code)
280 Alert::error(_("Cannot view file"),
281 bformat(_("No information for viewing %1$s"),
282 prettyName(format_name)));
286 if (format->viewer() == "auto") {
287 if (os::autoOpenFile(filename.absFilename(), os::VIEW))
290 Alert::error(_("Cannot view file"),
291 bformat(_("Auto-view file %1$s failed"),
292 from_utf8(filename.absFilename())));
297 string command = libScriptSearch(format->viewer());
299 if (format_name == "dvi" &&
300 !lyxrc.view_dvi_paper_option.empty()) {
301 command += ' ' + lyxrc.view_dvi_paper_option;
302 string paper_size = buffer.params().paperSizeName();
303 if (paper_size == "letter")
305 command += ' ' + paper_size;
306 if (buffer.params().orientation == ORIENTATION_LANDSCAPE)
310 if (!contains(command, token_from_format))
311 command += ' ' + token_from_format;
313 command = subst(command, token_from_format, quoteName(filename.toFilesystemEncoding()));
314 command = subst(command, token_path_format, quoteName(onlyPath(filename.toFilesystemEncoding())));
315 command = subst(command, token_socket_format, quoteName(theServerSocket().address()));
316 LYXERR(Debug::FILES) << "Executing command: " << command << std::endl;
317 // FIXME UNICODE utf8 can be wrong for files
318 buffer.message(_("Executing command: ") + from_utf8(command));
321 int const res = one.startscript(Systemcall::DontWait, command);
324 Alert::error(_("Cannot view file"),
325 bformat(_("An error occurred whilst running %1$s"),
326 makeDisplayPath(command, 50)));
333 bool Formats::edit(Buffer const & buffer, FileName const & filename,
334 string const & format_name) const
336 if (filename.empty() || !filename.exists()) {
337 Alert::error(_("Cannot edit file"),
338 bformat(_("File does not exist: %1$s"),
339 from_utf8(filename.absFilename())));
343 Format const * format = getFormat(format_name);
344 if (format && format->editor().empty() &&
345 format->isChildFormat())
346 format = getFormat(format->parentFormat());
347 if (!format || format->editor().empty()) {
348 // FIXME: I believe this is the wrong place to show alerts, it should
349 // be done by the caller (this should be "utility" code)
350 Alert::error(_("Cannot edit file"),
351 bformat(_("No information for editing %1$s"),
352 prettyName(format_name)));
356 if (format->editor() == "auto") {
357 if (os::autoOpenFile(filename.absFilename(), os::EDIT))
360 Alert::error(_("Cannot edit file"),
361 bformat(_("Auto-edit file %1$s failed"),
362 from_utf8(filename.absFilename())));
367 string command = format->editor();
369 if (!contains(command, token_from_format))
370 command += ' ' + token_from_format;
372 command = subst(command, token_from_format, quoteName(filename.toFilesystemEncoding()));
373 command = subst(command, token_path_format, quoteName(onlyPath(filename.toFilesystemEncoding())));
374 command = subst(command, token_socket_format, quoteName(theServerSocket().address()));
375 LYXERR(Debug::FILES) << "Executing command: " << command << std::endl;
376 // FIXME UNICODE utf8 can be wrong for files
377 buffer.message(_("Executing command: ") + from_utf8(command));
380 int const res = one.startscript(Systemcall::DontWait, command);
383 Alert::error(_("Cannot edit file"),
384 bformat(_("An error occurred whilst running %1$s"),
385 makeDisplayPath(command, 50)));
392 docstring const Formats::prettyName(string const & name) const
394 Format const * format = getFormat(name);
396 return from_utf8(format->prettyname());
398 return from_utf8(name);
402 string const Formats::extension(string const & name) const
404 Format const * format = getFormat(name);
406 return format->extension();
416 Formats system_formats;