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"
17 #include "ServerSocket.h"
19 #include "frontends/alert.h" //to be removed?
21 #include "support/debug.h"
22 #include "support/filetools.h"
23 #include "support/gettext.h"
24 #include "support/lstrings.h"
25 #include "support/os.h"
26 #include "support/Systemcall.h"
29 using namespace lyx::support;
33 namespace Alert = frontend::Alert;
34 namespace os = support::os;
38 string const token_from_format("$$i");
39 string const token_path_format("$$p");
40 string const token_socket_format("$$a");
43 class FormatNamesEqual : public unary_function<Format, bool> {
45 FormatNamesEqual(string const & name)
47 bool operator()(Format const & f) const
49 return f.name() == name_;
56 class FormatExtensionsEqual : public unary_function<Format, bool> {
58 FormatExtensionsEqual(string const & extension)
59 : extension_(extension) {}
60 bool operator()(Format const & f) const
62 return f.extension() == extension_;
70 bool operator<(Format const & a, Format const & b)
72 // use the compare_ascii_no_case instead of compare_no_case,
73 // because in turkish, 'i' is not the lowercase version of 'I',
74 // and thus turkish locale breaks parsing of tags.
76 return compare_ascii_no_case(a.prettyname(), b.prettyname()) < 0;
80 Format::Format(string const & n, string const & e, string const & p,
81 string const & s, string const & v, string const & ed,
83 : name_(n), extension_(e), prettyname_(p), shortcut_(s), viewer_(v),
84 editor_(ed), flags_(flags)
88 bool Format::dummy() const
90 return extension().empty();
94 bool Format::isChildFormat() const
98 return isdigit(name_[name_.length() - 1]);
102 string const Format::parentFormat() const
104 return name_.substr(0, name_.length() - 1);
108 // This method should return a reference, and throw an exception
109 // if the format named name cannot be found (Lgb)
110 Format const * Formats::getFormat(string const & name) const
112 FormatList::const_iterator cit =
113 find_if(formatlist.begin(), formatlist.end(),
114 FormatNamesEqual(name));
115 if (cit != formatlist.end())
122 string Formats::getFormatFromFile(FileName const & filename) const
124 if (filename.empty())
127 string const format = filename.guessFormatFromContents();
131 // try to find a format from the file extension.
132 string const ext = getExtension(filename.absFilename());
134 // this is ambigous if two formats have the same extension,
135 // but better than nothing
136 Formats::const_iterator cit =
137 find_if(formatlist.begin(), formatlist.end(),
138 FormatExtensionsEqual(ext));
139 if (cit != formats.end()) {
140 LYXERR(Debug::GRAPHICS, "\twill guess format from file extension: "
141 << ext << " -> " << cit->name());
149 static string fixCommand(string const & cmd, string const & ext,
150 os::auto_open_mode mode)
152 // configure.py says we do not want a viewer/editor
156 // Does the OS manage this format?
157 if (os::canAutoOpenFile(ext, mode))
160 // if configure.py found nothing, clear the command
161 if (token(cmd, ' ', 0) == "auto")
164 // use the command found by configure.py
169 void Formats::setAutoOpen()
171 FormatList::iterator fit = formatlist.begin();
172 FormatList::iterator const fend = formatlist.end();
173 for ( ; fit != fend ; ++fit) {
174 fit->setViewer(fixCommand(fit->viewer(), fit->extension(), os::VIEW));
175 fit->setEditor(fixCommand(fit->editor(), fit->extension(), os::EDIT));
180 int Formats::getNumber(string const & name) const
182 FormatList::const_iterator cit =
183 find_if(formatlist.begin(), formatlist.end(),
184 FormatNamesEqual(name));
185 if (cit != formatlist.end())
186 return distance(formatlist.begin(), cit);
192 void Formats::add(string const & name)
194 if (!getFormat(name))
195 add(name, name, name, string(), string(), string(),
200 void Formats::add(string const & name, string const & extension,
201 string const & prettyname, string const & shortcut,
202 string const & viewer, string const & editor,
205 FormatList::iterator it =
206 find_if(formatlist.begin(), formatlist.end(),
207 FormatNamesEqual(name));
208 if (it == formatlist.end())
209 formatlist.push_back(Format(name, extension, prettyname,
210 shortcut, viewer, editor, flags));
212 *it = Format(name, extension, prettyname, shortcut, viewer,
217 void Formats::erase(string const & name)
219 FormatList::iterator it =
220 find_if(formatlist.begin(), formatlist.end(),
221 FormatNamesEqual(name));
222 if (it != formatlist.end())
223 formatlist.erase(it);
229 std::sort(formatlist.begin(), formatlist.end());
233 void Formats::setViewer(string const & name, string const & command)
236 FormatList::iterator it =
237 find_if(formatlist.begin(), formatlist.end(),
238 FormatNamesEqual(name));
239 if (it != formatlist.end())
240 it->setViewer(command);
244 bool Formats::view(Buffer const & buffer, FileName const & filename,
245 string const & format_name) const
247 if (filename.empty() || !filename.exists()) {
248 Alert::error(_("Cannot view file"),
249 bformat(_("File does not exist: %1$s"),
250 from_utf8(filename.absFilename())));
254 Format const * format = getFormat(format_name);
255 if (format && format->viewer().empty() &&
256 format->isChildFormat())
257 format = getFormat(format->parentFormat());
258 if (!format || format->viewer().empty()) {
259 // FIXME: I believe this is the wrong place to show alerts, it should be done
260 // by the caller (this should be "utility" code)
261 Alert::error(_("Cannot view file"),
262 bformat(_("No information for viewing %1$s"),
263 prettyName(format_name)));
267 if (format->viewer() == "auto") {
268 if (os::autoOpenFile(filename.absFilename(), os::VIEW))
271 Alert::error(_("Cannot view file"),
272 bformat(_("Auto-view file %1$s failed"),
273 from_utf8(filename.absFilename())));
278 string command = libScriptSearch(format->viewer());
280 if (format_name == "dvi" &&
281 !lyxrc.view_dvi_paper_option.empty()) {
282 command += ' ' + lyxrc.view_dvi_paper_option;
283 string paper_size = buffer.params().paperSizeName();
284 if (paper_size == "letter")
286 command += ' ' + paper_size;
287 if (buffer.params().orientation == ORIENTATION_LANDSCAPE)
291 if (!contains(command, token_from_format))
292 command += ' ' + token_from_format;
294 command = subst(command, token_from_format, quoteName(filename.toFilesystemEncoding()));
295 command = subst(command, token_path_format, quoteName(onlyPath(filename.toFilesystemEncoding())));
296 command = subst(command, token_socket_format, quoteName(theServerSocket().address()));
297 LYXERR(Debug::FILES, "Executing command: " << command);
298 // FIXME UNICODE utf8 can be wrong for files
299 buffer.message(_("Executing command: ") + from_utf8(command));
302 int const res = one.startscript(Systemcall::DontWait, command);
305 Alert::error(_("Cannot view file"),
306 bformat(_("An error occurred whilst running %1$s"),
307 makeDisplayPath(command, 50)));
314 bool Formats::edit(Buffer const & buffer, FileName const & filename,
315 string const & format_name) const
317 if (filename.empty() || !filename.exists()) {
318 Alert::error(_("Cannot edit file"),
319 bformat(_("File does not exist: %1$s"),
320 from_utf8(filename.absFilename())));
324 Format const * format = getFormat(format_name);
325 if (format && format->editor().empty() &&
326 format->isChildFormat())
327 format = getFormat(format->parentFormat());
328 if (!format || format->editor().empty()) {
329 // FIXME: I believe this is the wrong place to show alerts, it should
330 // be done by the caller (this should be "utility" code)
331 Alert::error(_("Cannot edit file"),
332 bformat(_("No information for editing %1$s"),
333 prettyName(format_name)));
337 if (format->editor() == "auto") {
338 if (os::autoOpenFile(filename.absFilename(), os::EDIT))
341 Alert::error(_("Cannot edit file"),
342 bformat(_("Auto-edit file %1$s failed"),
343 from_utf8(filename.absFilename())));
348 string command = format->editor();
350 if (!contains(command, token_from_format))
351 command += ' ' + token_from_format;
353 command = subst(command, token_from_format, quoteName(filename.toFilesystemEncoding()));
354 command = subst(command, token_path_format, quoteName(onlyPath(filename.toFilesystemEncoding())));
355 command = subst(command, token_socket_format, quoteName(theServerSocket().address()));
356 LYXERR(Debug::FILES, "Executing command: " << command);
357 // FIXME UNICODE utf8 can be wrong for files
358 buffer.message(_("Executing command: ") + from_utf8(command));
361 int const res = one.startscript(Systemcall::DontWait, command);
364 Alert::error(_("Cannot edit file"),
365 bformat(_("An error occurred whilst running %1$s"),
366 makeDisplayPath(command, 50)));
373 docstring const Formats::prettyName(string const & name) const
375 Format const * format = getFormat(name);
377 return from_utf8(format->prettyname());
379 return from_utf8(name);
383 string const Formats::extension(string const & name) const
385 Format const * format = getFormat(name);
387 return format->extension();
397 Formats system_formats;