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"
29 #include <boost/filesystem/operations.hpp>
34 using support::absolutePath;
35 using support::bformat;
36 using support::compare_ascii_no_case;
37 using support::contains;
38 using support::FileName;
39 using support::libScriptSearch;
40 using support::makeDisplayPath;
41 using support::onlyPath;
42 using support::quoteName;
44 using support::Systemcall;
50 namespace Alert = frontend::Alert;
51 namespace fs = boost::filesystem;
52 namespace os = support::os;
56 string const token_from_format("$$i");
57 string const token_path_format("$$p");
58 string const token_socket_format("$$a");
61 class FormatNamesEqual : public std::unary_function<Format, bool> {
63 FormatNamesEqual(string const & name)
65 bool operator()(Format const & f) const
67 return f.name() == name_;
74 class FormatExtensionsEqual : public std::unary_function<Format, bool> {
76 FormatExtensionsEqual(string const & extension)
77 : extension_(extension) {}
78 bool operator()(Format const & f) const
80 return f.extension() == extension_;
88 bool operator<(Format const & a, Format const & b)
90 // use the compare_ascii_no_case instead of compare_no_case,
91 // because in turkish, 'i' is not the lowercase version of 'I',
92 // and thus turkish locale breaks parsing of tags.
94 return compare_ascii_no_case(a.prettyname(), b.prettyname()) < 0;
98 Format::Format(string const & n, string const & e, string const & p,
99 string const & s, string const & v, string const & ed,
101 : name_(n), extension_(e), prettyname_(p), shortcut_(s), viewer_(v),
102 editor_(ed), flags_(flags)
106 bool Format::dummy() const
108 return extension().empty();
112 bool Format::isChildFormat() const
116 return isdigit(name_[name_.length() - 1]);
120 string const Format::parentFormat() const
122 return name_.substr(0, name_.length() - 1);
126 // This method should return a reference, and throw an exception
127 // if the format named name cannot be found (Lgb)
128 Format const * Formats::getFormat(string const & name) const
130 FormatList::const_iterator cit =
131 find_if(formatlist.begin(), formatlist.end(),
132 FormatNamesEqual(name));
133 if (cit != formatlist.end())
140 string Formats::getFormatFromFile(FileName const & filename) const
142 if (filename.empty())
145 string const format = support::getFormatFromContents(filename);
149 // try to find a format from the file extension.
150 string const ext(support::getExtension(filename.absFilename()));
152 // this is ambigous if two formats have the same extension,
153 // but better than nothing
154 Formats::const_iterator cit =
155 find_if(formatlist.begin(), formatlist.end(),
156 FormatExtensionsEqual(ext));
157 if (cit != formats.end()) {
158 LYXERR(Debug::GRAPHICS)
159 << "\twill guess format from file extension: "
160 << ext << " -> " << cit->name() << std::endl;
169 string fixCommand(string const & cmd, string const & ext,
170 os::auto_open_mode mode)
172 // configure.py says we do not want a viewer/editor
176 // Does the OS manage this format?
177 if (os::canAutoOpenFile(ext, mode))
180 // if configure.py found nothing, clear the command
181 if (token(cmd, ' ', 0) == "auto")
184 // use the command found by configure.py
190 void Formats::setAutoOpen()
192 FormatList::iterator fit = formatlist.begin();
193 FormatList::iterator const fend = formatlist.end();
194 for ( ; fit != fend ; ++fit) {
195 fit->setViewer(fixCommand(fit->viewer(), fit->extension(), os::VIEW));
196 fit->setEditor(fixCommand(fit->editor(), fit->extension(), os::EDIT));
201 int Formats::getNumber(string const & name) const
203 FormatList::const_iterator cit =
204 find_if(formatlist.begin(), formatlist.end(),
205 FormatNamesEqual(name));
206 if (cit != formatlist.end())
207 return distance(formatlist.begin(), cit);
213 void Formats::add(string const & name)
215 if (!getFormat(name))
216 add(name, name, name, string(), string(), string(),
221 void Formats::add(string const & name, string const & extension,
222 string const & prettyname, string const & shortcut,
223 string const & viewer, string const & editor,
226 FormatList::iterator it =
227 find_if(formatlist.begin(), formatlist.end(),
228 FormatNamesEqual(name));
229 if (it == formatlist.end())
230 formatlist.push_back(Format(name, extension, prettyname,
231 shortcut, viewer, editor, flags));
233 *it = Format(name, extension, prettyname, shortcut, viewer,
238 void Formats::erase(string const & name)
240 FormatList::iterator it =
241 find_if(formatlist.begin(), formatlist.end(),
242 FormatNamesEqual(name));
243 if (it != formatlist.end())
244 formatlist.erase(it);
250 std::sort(formatlist.begin(), formatlist.end());
254 void Formats::setViewer(string const & name, string const & command)
257 FormatList::iterator it =
258 find_if(formatlist.begin(), formatlist.end(),
259 FormatNamesEqual(name));
260 if (it != formatlist.end())
261 it->setViewer(command);
265 bool Formats::view(Buffer const & buffer, FileName const & filename,
266 string const & format_name) const
268 if (filename.empty() || !fs::exists(filename.toFilesystemEncoding())) {
269 Alert::error(_("Cannot view file"),
270 bformat(_("File does not exist: %1$s"),
271 from_utf8(filename.absFilename())));
275 Format const * format = getFormat(format_name);
276 if (format && format->viewer().empty() &&
277 format->isChildFormat())
278 format = getFormat(format->parentFormat());
279 if (!format || format->viewer().empty()) {
280 // FIXME: I believe this is the wrong place to show alerts, it should be done
281 // by the caller (this should be "utility" code)
282 Alert::error(_("Cannot view file"),
283 bformat(_("No information for viewing %1$s"),
284 prettyName(format_name)));
288 if (format->viewer() == "auto") {
289 if (os::autoOpenFile(filename.absFilename(), os::VIEW))
292 Alert::error(_("Cannot view file"),
293 bformat(_("Auto-view file %1$s failed"),
294 from_utf8(filename.absFilename())));
299 string command = libScriptSearch(format->viewer());
301 if (format_name == "dvi" &&
302 !lyxrc.view_dvi_paper_option.empty()) {
303 command += ' ' + lyxrc.view_dvi_paper_option;
304 string paper_size = buffer.params().paperSizeName();
305 if (paper_size == "letter")
307 command += ' ' + paper_size;
308 if (buffer.params().orientation == ORIENTATION_LANDSCAPE)
312 if (!contains(command, token_from_format))
313 command += ' ' + token_from_format;
315 command = subst(command, token_from_format, quoteName(filename.toFilesystemEncoding()));
316 command = subst(command, token_path_format, quoteName(onlyPath(filename.toFilesystemEncoding())));
317 command = subst(command, token_socket_format, quoteName(theServerSocket().address()));
318 LYXERR(Debug::FILES) << "Executing command: " << command << std::endl;
319 // FIXME UNICODE utf8 can be wrong for files
320 buffer.message(_("Executing command: ") + from_utf8(command));
323 int const res = one.startscript(Systemcall::DontWait, command);
326 Alert::error(_("Cannot view file"),
327 bformat(_("An error occurred whilst running %1$s"),
328 makeDisplayPath(command, 50)));
335 bool Formats::edit(Buffer const & buffer, FileName const & filename,
336 string const & format_name) const
338 if (filename.empty() || !fs::exists(filename.toFilesystemEncoding())) {
339 Alert::error(_("Cannot edit file"),
340 bformat(_("File does not exist: %1$s"),
341 from_utf8(filename.absFilename())));
345 Format const * format = getFormat(format_name);
346 if (format && format->editor().empty() &&
347 format->isChildFormat())
348 format = getFormat(format->parentFormat());
349 if (!format || format->editor().empty()) {
350 // FIXME: I believe this is the wrong place to show alerts, it should
351 // be done by the caller (this should be "utility" code)
352 Alert::error(_("Cannot edit file"),
353 bformat(_("No information for editing %1$s"),
354 prettyName(format_name)));
358 if (format->editor() == "auto") {
359 if (os::autoOpenFile(filename.absFilename(), os::EDIT))
362 Alert::error(_("Cannot edit file"),
363 bformat(_("Auto-edit file %1$s failed"),
364 from_utf8(filename.absFilename())));
369 string command = format->editor();
371 if (!contains(command, token_from_format))
372 command += ' ' + token_from_format;
374 command = subst(command, token_from_format, quoteName(filename.toFilesystemEncoding()));
375 command = subst(command, token_path_format, quoteName(onlyPath(filename.toFilesystemEncoding())));
376 command = subst(command, token_socket_format, quoteName(theServerSocket().address()));
377 LYXERR(Debug::FILES) << "Executing command: " << command << std::endl;
378 // FIXME UNICODE utf8 can be wrong for files
379 buffer.message(_("Executing command: ") + from_utf8(command));
382 int const res = one.startscript(Systemcall::DontWait, command);
385 Alert::error(_("Cannot edit file"),
386 bformat(_("An error occurred whilst running %1$s"),
387 makeDisplayPath(command, 50)));
394 docstring const Formats::prettyName(string const & name) const
396 Format const * format = getFormat(name);
398 return from_utf8(format->prettyname());
400 return from_utf8(name);
404 string const Formats::extension(string const & name) const
406 Format const * format = getFormat(name);
408 return format->extension();
418 Formats system_formats;