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, "\twill guess format from file extension: "
156 << ext << " -> " << cit->name());
164 static string fixCommand(string const & cmd, string const & ext,
165 os::auto_open_mode mode)
167 // configure.py says we do not want a viewer/editor
171 // Does the OS manage this format?
172 if (os::canAutoOpenFile(ext, mode))
175 // if configure.py found nothing, clear the command
176 if (token(cmd, ' ', 0) == "auto")
179 // use the command found by configure.py
184 void Formats::setAutoOpen()
186 FormatList::iterator fit = formatlist.begin();
187 FormatList::iterator const fend = formatlist.end();
188 for ( ; fit != fend ; ++fit) {
189 fit->setViewer(fixCommand(fit->viewer(), fit->extension(), os::VIEW));
190 fit->setEditor(fixCommand(fit->editor(), fit->extension(), os::EDIT));
195 int Formats::getNumber(string const & name) const
197 FormatList::const_iterator cit =
198 find_if(formatlist.begin(), formatlist.end(),
199 FormatNamesEqual(name));
200 if (cit != formatlist.end())
201 return distance(formatlist.begin(), cit);
207 void Formats::add(string const & name)
209 if (!getFormat(name))
210 add(name, name, name, string(), string(), string(),
215 void Formats::add(string const & name, string const & extension,
216 string const & prettyname, string const & shortcut,
217 string const & viewer, string const & editor,
220 FormatList::iterator it =
221 find_if(formatlist.begin(), formatlist.end(),
222 FormatNamesEqual(name));
223 if (it == formatlist.end())
224 formatlist.push_back(Format(name, extension, prettyname,
225 shortcut, viewer, editor, flags));
227 *it = Format(name, extension, prettyname, shortcut, viewer,
232 void Formats::erase(string const & name)
234 FormatList::iterator it =
235 find_if(formatlist.begin(), formatlist.end(),
236 FormatNamesEqual(name));
237 if (it != formatlist.end())
238 formatlist.erase(it);
244 std::sort(formatlist.begin(), formatlist.end());
248 void Formats::setViewer(string const & name, string const & command)
251 FormatList::iterator it =
252 find_if(formatlist.begin(), formatlist.end(),
253 FormatNamesEqual(name));
254 if (it != formatlist.end())
255 it->setViewer(command);
259 bool Formats::view(Buffer const & buffer, FileName const & filename,
260 string const & format_name) const
262 if (filename.empty() || !filename.exists()) {
263 Alert::error(_("Cannot view file"),
264 bformat(_("File does not exist: %1$s"),
265 from_utf8(filename.absFilename())));
269 Format const * format = getFormat(format_name);
270 if (format && format->viewer().empty() &&
271 format->isChildFormat())
272 format = getFormat(format->parentFormat());
273 if (!format || format->viewer().empty()) {
274 // FIXME: I believe this is the wrong place to show alerts, it should be done
275 // by the caller (this should be "utility" code)
276 Alert::error(_("Cannot view file"),
277 bformat(_("No information for viewing %1$s"),
278 prettyName(format_name)));
282 if (format->viewer() == "auto") {
283 if (os::autoOpenFile(filename.absFilename(), os::VIEW))
286 Alert::error(_("Cannot view file"),
287 bformat(_("Auto-view file %1$s failed"),
288 from_utf8(filename.absFilename())));
293 string command = libScriptSearch(format->viewer());
295 if (format_name == "dvi" &&
296 !lyxrc.view_dvi_paper_option.empty()) {
297 command += ' ' + lyxrc.view_dvi_paper_option;
298 string paper_size = buffer.params().paperSizeName();
299 if (paper_size == "letter")
301 command += ' ' + paper_size;
302 if (buffer.params().orientation == ORIENTATION_LANDSCAPE)
306 if (!contains(command, token_from_format))
307 command += ' ' + token_from_format;
309 command = subst(command, token_from_format, quoteName(filename.toFilesystemEncoding()));
310 command = subst(command, token_path_format, quoteName(onlyPath(filename.toFilesystemEncoding())));
311 command = subst(command, token_socket_format, quoteName(theServerSocket().address()));
312 LYXERR(Debug::FILES, "Executing command: " << command);
313 // FIXME UNICODE utf8 can be wrong for files
314 buffer.message(_("Executing command: ") + from_utf8(command));
317 int const res = one.startscript(Systemcall::DontWait, command);
320 Alert::error(_("Cannot view file"),
321 bformat(_("An error occurred whilst running %1$s"),
322 makeDisplayPath(command, 50)));
329 bool Formats::edit(Buffer const & buffer, FileName const & filename,
330 string const & format_name) const
332 if (filename.empty() || !filename.exists()) {
333 Alert::error(_("Cannot edit file"),
334 bformat(_("File does not exist: %1$s"),
335 from_utf8(filename.absFilename())));
339 Format const * format = getFormat(format_name);
340 if (format && format->editor().empty() &&
341 format->isChildFormat())
342 format = getFormat(format->parentFormat());
343 if (!format || format->editor().empty()) {
344 // FIXME: I believe this is the wrong place to show alerts, it should
345 // be done by the caller (this should be "utility" code)
346 Alert::error(_("Cannot edit file"),
347 bformat(_("No information for editing %1$s"),
348 prettyName(format_name)));
352 if (format->editor() == "auto") {
353 if (os::autoOpenFile(filename.absFilename(), os::EDIT))
356 Alert::error(_("Cannot edit file"),
357 bformat(_("Auto-edit file %1$s failed"),
358 from_utf8(filename.absFilename())));
363 string command = format->editor();
365 if (!contains(command, token_from_format))
366 command += ' ' + token_from_format;
368 command = subst(command, token_from_format, quoteName(filename.toFilesystemEncoding()));
369 command = subst(command, token_path_format, quoteName(onlyPath(filename.toFilesystemEncoding())));
370 command = subst(command, token_socket_format, quoteName(theServerSocket().address()));
371 LYXERR(Debug::FILES, "Executing command: " << command);
372 // FIXME UNICODE utf8 can be wrong for files
373 buffer.message(_("Executing command: ") + from_utf8(command));
376 int const res = one.startscript(Systemcall::DontWait, command);
379 Alert::error(_("Cannot edit file"),
380 bformat(_("An error occurred whilst running %1$s"),
381 makeDisplayPath(command, 50)));
388 docstring const Formats::prettyName(string const & name) const
390 Format const * format = getFormat(name);
392 return from_utf8(format->prettyname());
394 return from_utf8(name);
398 string const Formats::extension(string const & name) const
400 Format const * format = getFormat(name);
402 return format->extension();
412 Formats system_formats;