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/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"
35 using support::absolutePath;
36 using support::bformat;
37 using support::compare_ascii_no_case;
38 using support::contains;
39 using support::FileName;
40 using support::libScriptSearch;
41 using support::makeDisplayPath;
42 using support::onlyPath;
43 using support::quoteName;
45 using support::Systemcall;
48 namespace Alert = frontend::Alert;
49 namespace os = support::os;
53 string const token_from_format("$$i");
54 string const token_path_format("$$p");
55 string const token_socket_format("$$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), flags_(flags)
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(FileName const & filename) const
139 if (filename.empty())
142 string const format = filename.guessFormatFromContents();
146 // try to find a format from the file extension.
147 string const ext = support::getExtension(filename.absFilename());
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;
165 static string fixCommand(string const & cmd, string const & ext,
166 os::auto_open_mode mode)
168 // configure.py says we do not want a viewer/editor
172 // Does the OS manage this format?
173 if (os::canAutoOpenFile(ext, mode))
176 // if configure.py found nothing, clear the command
177 if (token(cmd, ' ', 0) == "auto")
180 // use the command found by configure.py
185 void Formats::setAutoOpen()
187 FormatList::iterator fit = formatlist.begin();
188 FormatList::iterator const fend = formatlist.end();
189 for ( ; fit != fend ; ++fit) {
190 fit->setViewer(fixCommand(fit->viewer(), fit->extension(), os::VIEW));
191 fit->setEditor(fixCommand(fit->editor(), fit->extension(), os::EDIT));
196 int Formats::getNumber(string const & name) const
198 FormatList::const_iterator cit =
199 find_if(formatlist.begin(), formatlist.end(),
200 FormatNamesEqual(name));
201 if (cit != formatlist.end())
202 return distance(formatlist.begin(), cit);
208 void Formats::add(string const & name)
210 if (!getFormat(name))
211 add(name, name, name, string(), string(), string(),
216 void Formats::add(string const & name, string const & extension,
217 string const & prettyname, string const & shortcut,
218 string const & viewer, string const & editor,
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, flags));
228 *it = Format(name, extension, prettyname, shortcut, viewer,
233 void Formats::erase(string const & name)
235 FormatList::iterator it =
236 find_if(formatlist.begin(), formatlist.end(),
237 FormatNamesEqual(name));
238 if (it != formatlist.end())
239 formatlist.erase(it);
245 std::sort(formatlist.begin(), formatlist.end());
249 void Formats::setViewer(string const & name, string const & command)
252 FormatList::iterator it =
253 find_if(formatlist.begin(), formatlist.end(),
254 FormatNamesEqual(name));
255 if (it != formatlist.end())
256 it->setViewer(command);
260 bool Formats::view(Buffer const & buffer, FileName const & filename,
261 string const & format_name) const
263 if (filename.empty() || !filename.exists()) {
264 Alert::error(_("Cannot view file"),
265 bformat(_("File does not exist: %1$s"),
266 from_utf8(filename.absFilename())));
270 Format const * format = getFormat(format_name);
271 if (format && format->viewer().empty() &&
272 format->isChildFormat())
273 format = getFormat(format->parentFormat());
274 if (!format || format->viewer().empty()) {
275 // FIXME: I believe this is the wrong place to show alerts, it should be done
276 // by the caller (this should be "utility" code)
277 Alert::error(_("Cannot view file"),
278 bformat(_("No information for viewing %1$s"),
279 prettyName(format_name)));
283 if (format->viewer() == "auto") {
284 if (os::autoOpenFile(filename.absFilename(), os::VIEW))
287 Alert::error(_("Cannot view file"),
288 bformat(_("Auto-view file %1$s failed"),
289 from_utf8(filename.absFilename())));
294 string command = libScriptSearch(format->viewer());
296 if (format_name == "dvi" &&
297 !lyxrc.view_dvi_paper_option.empty()) {
298 command += ' ' + lyxrc.view_dvi_paper_option;
299 string paper_size = buffer.params().paperSizeName();
300 if (paper_size == "letter")
302 command += ' ' + paper_size;
303 if (buffer.params().orientation == ORIENTATION_LANDSCAPE)
307 if (!contains(command, token_from_format))
308 command += ' ' + token_from_format;
310 command = subst(command, token_from_format, quoteName(filename.toFilesystemEncoding()));
311 command = subst(command, token_path_format, quoteName(onlyPath(filename.toFilesystemEncoding())));
312 command = subst(command, token_socket_format, quoteName(theServerSocket().address()));
313 LYXERR(Debug::FILES) << "Executing command: " << command << std::endl;
314 // FIXME UNICODE utf8 can be wrong for files
315 buffer.message(_("Executing command: ") + from_utf8(command));
318 int const res = one.startscript(Systemcall::DontWait, command);
321 Alert::error(_("Cannot view file"),
322 bformat(_("An error occurred whilst running %1$s"),
323 makeDisplayPath(command, 50)));
330 bool Formats::edit(Buffer const & buffer, FileName const & filename,
331 string const & format_name) const
333 if (filename.empty() || !filename.exists()) {
334 Alert::error(_("Cannot edit file"),
335 bformat(_("File does not exist: %1$s"),
336 from_utf8(filename.absFilename())));
340 Format const * format = getFormat(format_name);
341 if (format && format->editor().empty() &&
342 format->isChildFormat())
343 format = getFormat(format->parentFormat());
344 if (!format || format->editor().empty()) {
345 // FIXME: I believe this is the wrong place to show alerts, it should
346 // be done by the caller (this should be "utility" code)
347 Alert::error(_("Cannot edit file"),
348 bformat(_("No information for editing %1$s"),
349 prettyName(format_name)));
353 if (format->editor() == "auto") {
354 if (os::autoOpenFile(filename.absFilename(), os::EDIT))
357 Alert::error(_("Cannot edit file"),
358 bformat(_("Auto-edit file %1$s failed"),
359 from_utf8(filename.absFilename())));
364 string command = format->editor();
366 if (!contains(command, token_from_format))
367 command += ' ' + token_from_format;
369 command = subst(command, token_from_format, quoteName(filename.toFilesystemEncoding()));
370 command = subst(command, token_path_format, quoteName(onlyPath(filename.toFilesystemEncoding())));
371 command = subst(command, token_socket_format, quoteName(theServerSocket().address()));
372 LYXERR(Debug::FILES) << "Executing command: " << command << std::endl;
373 // FIXME UNICODE utf8 can be wrong for files
374 buffer.message(_("Executing command: ") + from_utf8(command));
377 int const res = one.startscript(Systemcall::DontWait, command);
380 Alert::error(_("Cannot edit file"),
381 bformat(_("An error occurred whilst running %1$s"),
382 makeDisplayPath(command, 50)));
389 docstring const Formats::prettyName(string const & name) const
391 Format const * format = getFormat(name);
393 return from_utf8(format->prettyname());
395 return from_utf8(name);
399 string const Formats::extension(string const & name) const
401 Format const * format = getFormat(name);
403 return format->extension();
413 Formats system_formats;