Another minor change, but this should almost get us to the point that we
[lyx.git] / src / Format.cpp
blob6bd5ecb8c8e13951f8ce83a94d2c9370914f37df
1 /**
2 * \file Format.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Dekel Tsur
8 * Full author contact details are available in file CREDITS.
9 */
11 #include <config.h>
13 #include "Format.h"
14 #include "Buffer.h"
15 #include "BufferParams.h"
16 #include "LyXRC.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"
28 // FIXME: Q_WS_MACX is not available, it's in Qt
29 #ifdef USE_MACOSX_PACKAGING
30 #include "support/linkback/LinkBackProxy.h"
31 #endif
33 using namespace std;
34 using namespace lyx::support;
36 namespace lyx {
38 namespace Alert = frontend::Alert;
39 namespace os = support::os;
41 namespace {
43 string const token_from_format("$$i");
44 string const token_path_format("$$p");
45 string const token_socket_format("$$a");
48 class FormatNamesEqual : public unary_function<Format, bool> {
49 public:
50 FormatNamesEqual(string const & name)
51 : name_(name) {}
52 bool operator()(Format const & f) const
54 return f.name() == name_;
56 private:
57 string name_;
61 class FormatExtensionsEqual : public unary_function<Format, bool> {
62 public:
63 FormatExtensionsEqual(string const & extension)
64 : extension_(extension) {}
65 bool operator()(Format const & f) const
67 return f.extension() == extension_;
69 private:
70 string extension_;
73 } //namespace anon
75 bool operator<(Format const & a, Format const & b)
77 // use the compare_ascii_no_case instead of compare_no_case,
78 // because in turkish, 'i' is not the lowercase version of 'I',
79 // and thus turkish locale breaks parsing of tags.
81 return compare_ascii_no_case(a.prettyname(), b.prettyname()) < 0;
85 Format::Format(string const & n, string const & e, string const & p,
86 string const & s, string const & v, string const & ed,
87 int flags)
88 : name_(n), extension_(e), prettyname_(p), shortcut_(s), viewer_(v),
89 editor_(ed), flags_(flags)
93 bool Format::dummy() const
95 return extension().empty();
99 bool Format::isChildFormat() const
101 if (name_.empty())
102 return false;
103 return isdigit(name_[name_.length() - 1]);
107 string const Format::parentFormat() const
109 return name_.substr(0, name_.length() - 1);
113 // This method should return a reference, and throw an exception
114 // if the format named name cannot be found (Lgb)
115 Format const * Formats::getFormat(string const & name) const
117 FormatList::const_iterator cit =
118 find_if(formatlist.begin(), formatlist.end(),
119 FormatNamesEqual(name));
120 if (cit != formatlist.end())
121 return &(*cit);
122 else
123 return 0;
127 string Formats::getFormatFromFile(FileName const & filename) const
129 if (filename.empty())
130 return string();
132 string const format = filename.guessFormatFromContents();
133 if (!format.empty())
134 return format;
136 // try to find a format from the file extension.
137 string const ext = getExtension(filename.absFilename());
138 if (!ext.empty()) {
139 // this is ambigous if two formats have the same extension,
140 // but better than nothing
141 Formats::const_iterator cit =
142 find_if(formatlist.begin(), formatlist.end(),
143 FormatExtensionsEqual(ext));
144 if (cit != formats.end()) {
145 LYXERR(Debug::GRAPHICS, "\twill guess format from file extension: "
146 << ext << " -> " << cit->name());
147 return cit->name();
150 return string();
154 static string fixCommand(string const & cmd, string const & ext,
155 os::auto_open_mode mode)
157 // configure.py says we do not want a viewer/editor
158 if (cmd.empty())
159 return cmd;
161 // Does the OS manage this format?
162 if (os::canAutoOpenFile(ext, mode))
163 return "auto";
165 // if configure.py found nothing, clear the command
166 if (token(cmd, ' ', 0) == "auto")
167 return string();
169 // use the command found by configure.py
170 return cmd;
174 void Formats::setAutoOpen()
176 FormatList::iterator fit = formatlist.begin();
177 FormatList::iterator const fend = formatlist.end();
178 for ( ; fit != fend ; ++fit) {
179 fit->setViewer(fixCommand(fit->viewer(), fit->extension(), os::VIEW));
180 fit->setEditor(fixCommand(fit->editor(), fit->extension(), os::EDIT));
185 int Formats::getNumber(string const & name) const
187 FormatList::const_iterator cit =
188 find_if(formatlist.begin(), formatlist.end(),
189 FormatNamesEqual(name));
190 if (cit != formatlist.end())
191 return distance(formatlist.begin(), cit);
192 else
193 return -1;
197 void Formats::add(string const & name)
199 if (!getFormat(name))
200 add(name, name, name, string(), string(), string(),
201 Format::document);
205 void Formats::add(string const & name, string const & extension,
206 string const & prettyname, string const & shortcut,
207 string const & viewer, string const & editor,
208 int flags)
210 FormatList::iterator it =
211 find_if(formatlist.begin(), formatlist.end(),
212 FormatNamesEqual(name));
213 if (it == formatlist.end())
214 formatlist.push_back(Format(name, extension, prettyname,
215 shortcut, viewer, editor, flags));
216 else
217 *it = Format(name, extension, prettyname, shortcut, viewer,
218 editor, flags);
222 void Formats::erase(string const & name)
224 FormatList::iterator it =
225 find_if(formatlist.begin(), formatlist.end(),
226 FormatNamesEqual(name));
227 if (it != formatlist.end())
228 formatlist.erase(it);
232 void Formats::sort()
234 std::sort(formatlist.begin(), formatlist.end());
238 void Formats::setViewer(string const & name, string const & command)
240 add(name);
241 FormatList::iterator it =
242 find_if(formatlist.begin(), formatlist.end(),
243 FormatNamesEqual(name));
244 if (it != formatlist.end())
245 it->setViewer(command);
249 bool Formats::view(Buffer const & buffer, FileName const & filename,
250 string const & format_name) const
252 if (filename.empty() || !filename.exists()) {
253 Alert::error(_("Cannot view file"),
254 bformat(_("File does not exist: %1$s"),
255 from_utf8(filename.absFilename())));
256 return false;
259 Format const * format = getFormat(format_name);
260 if (format && format->viewer().empty() &&
261 format->isChildFormat())
262 format = getFormat(format->parentFormat());
263 if (!format || format->viewer().empty()) {
264 // FIXME: I believe this is the wrong place to show alerts, it should be done
265 // by the caller (this should be "utility" code)
266 Alert::error(_("Cannot view file"),
267 bformat(_("No information for viewing %1$s"),
268 prettyName(format_name)));
269 return false;
271 // viewer is 'auto'
272 if (format->viewer() == "auto") {
273 if (os::autoOpenFile(filename.absFilename(), os::VIEW))
274 return true;
275 else {
276 Alert::error(_("Cannot view file"),
277 bformat(_("Auto-view file %1$s failed"),
278 from_utf8(filename.absFilename())));
279 return false;
283 string command = libScriptSearch(format->viewer());
285 if (format_name == "dvi" &&
286 !lyxrc.view_dvi_paper_option.empty()) {
287 string paper_size = buffer.params().paperSizeName(BufferParams::XDVI);
288 if (!paper_size.empty()) {
289 command += ' ' + lyxrc.view_dvi_paper_option;
290 command += ' ' + paper_size;
291 if (buffer.params().orientation == ORIENTATION_LANDSCAPE &&
292 buffer.params().papersize != PAPER_CUSTOM)
293 command += 'r';
297 if (!contains(command, token_from_format))
298 command += ' ' + token_from_format;
300 command = subst(command, token_from_format, quoteName(filename.toFilesystemEncoding()));
301 command = subst(command, token_path_format, quoteName(onlyPath(filename.toFilesystemEncoding())));
302 command = subst(command, token_socket_format, quoteName(theServerSocket().address()));
303 LYXERR(Debug::FILES, "Executing command: " << command);
304 // FIXME UNICODE utf8 can be wrong for files
305 buffer.message(_("Executing command: ") + from_utf8(command));
307 Systemcall one;
308 int const res = one.startscript(Systemcall::DontWait, command);
310 if (res) {
311 Alert::error(_("Cannot view file"),
312 bformat(_("An error occurred whilst running %1$s"),
313 makeDisplayPath(command, 50)));
314 return false;
316 return true;
320 bool Formats::edit(Buffer const & buffer, FileName const & filename,
321 string const & format_name) const
323 if (filename.empty() || !filename.exists()) {
324 Alert::error(_("Cannot edit file"),
325 bformat(_("File does not exist: %1$s"),
326 from_utf8(filename.absFilename())));
327 return false;
330 // LinkBack files look like PDF, but have the .linkback extension
331 string const ext = getExtension(filename.absFilename());
332 if (format_name == "pdf" && ext == "linkback") {
333 #ifdef USE_MACOSX_PACKAGING
334 return editLinkBackFile(filename.absFilename().c_str());
335 #else
336 Alert::error(_("Cannot edit file"),
337 _("LinkBack files can only be edited on Apple Mac OSX."));
338 return false;
339 #endif // USE_MACOSX_PACKAGING
342 Format const * format = getFormat(format_name);
343 if (format && format->editor().empty() &&
344 format->isChildFormat())
345 format = getFormat(format->parentFormat());
346 if (!format || format->editor().empty()) {
347 // FIXME: I believe this is the wrong place to show alerts, it should
348 // be done by the caller (this should be "utility" code)
349 Alert::error(_("Cannot edit file"),
350 bformat(_("No information for editing %1$s"),
351 prettyName(format_name)));
352 return false;
355 // editor is 'auto'
356 if (format->editor() == "auto") {
357 if (os::autoOpenFile(filename.absFilename(), os::EDIT))
358 return true;
359 else {
360 Alert::error(_("Cannot edit file"),
361 bformat(_("Auto-edit file %1$s failed"),
362 from_utf8(filename.absFilename())));
363 return false;
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);
376 // FIXME UNICODE utf8 can be wrong for files
377 buffer.message(_("Executing command: ") + from_utf8(command));
379 Systemcall one;
380 int const res = one.startscript(Systemcall::DontWait, command);
382 if (res) {
383 Alert::error(_("Cannot edit file"),
384 bformat(_("An error occurred whilst running %1$s"),
385 makeDisplayPath(command, 50)));
386 return false;
388 return true;
392 docstring const Formats::prettyName(string const & name) const
394 Format const * format = getFormat(name);
395 if (format)
396 return from_utf8(format->prettyname());
397 else
398 return from_utf8(name);
402 string const Formats::extension(string const & name) const
404 Format const * format = getFormat(name);
405 if (format)
406 return format->extension();
407 else
408 return name;
414 Formats formats;
416 Formats system_formats;
419 } // namespace lyx