]> git.lyx.org Git - lyx.git/blob - src/Format.cpp
LaTeXFeatures.cpp: "wrapfig" has to be loaded after "float"
[lyx.git] / src / Format.cpp
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.
5  *
6  * \author Dekel Tsur
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "Format.h"
14 #include "Buffer.h"
15 #include "BufferParams.h"
16 #include "LyXRC.h"
17 #include "debug.h"
18 #include "gettext.h"
19 #include "ServerSocket.h"
20
21 #include "frontends/Application.h"
22 #include "frontends/alert.h" //to be removed?
23
24 #include "support/filetools.h"
25 #include "support/lstrings.h"
26 #include "support/os.h"
27 #include "support/Systemcall.h"
28
29 #include <boost/filesystem/operations.hpp>
30
31 using std::find_if;
32 using std::string;
33 using std::distance;
34
35
36 namespace lyx {
37
38 using support::absolutePath;
39 using support::bformat;
40 using support::compare_ascii_no_case;
41 using support::contains;
42 using support::FileName;
43 using support::libScriptSearch;
44 using support::makeDisplayPath;
45 using support::onlyPath;
46 using support::quoteName;
47 using support::subst;
48 using support::Systemcall;
49 using support::token;
50
51 namespace Alert = frontend::Alert;
52 namespace fs = boost::filesystem;
53 namespace os = support::os;
54
55 namespace {
56
57 string const token_from_format("$$i");
58 string const token_path_format("$$p");
59 string const token_socket_format("$$a");
60
61
62 class FormatNamesEqual : public std::unary_function<Format, bool> {
63 public:
64         FormatNamesEqual(string const & name)
65                 : name_(name) {}
66         bool operator()(Format const & f) const
67         {
68                 return f.name() == name_;
69         }
70 private:
71         string name_;
72 };
73
74
75 class FormatExtensionsEqual : public std::unary_function<Format, bool> {
76 public:
77         FormatExtensionsEqual(string const & extension)
78                 : extension_(extension) {}
79         bool operator()(Format const & f) const
80         {
81                 return f.extension() == extension_;
82         }
83 private:
84         string extension_;
85 };
86
87 } //namespace anon
88
89 bool operator<(Format const & a, Format const & b)
90 {
91         // use the compare_ascii_no_case instead of compare_no_case,
92         // because in turkish, 'i' is not the lowercase version of 'I',
93         // and thus turkish locale breaks parsing of tags.
94
95         return compare_ascii_no_case(a.prettyname(), b.prettyname()) < 0;
96 }
97
98
99 Format::Format(string const & n, string const & e, string const & p,
100                string const & s, string const & v, string const & ed,
101                int flags)
102         : name_(n), extension_(e), prettyname_(p), shortcut_(s), viewer_(v),
103           editor_(ed), flags_(flags)
104 {}
105
106
107 bool Format::dummy() const
108 {
109         return extension().empty();
110 }
111
112
113 bool Format::isChildFormat() const
114 {
115         if (name_.empty())
116                 return false;
117         return isdigit(name_[name_.length() - 1]);
118 }
119
120
121 string const Format::parentFormat() const
122 {
123         return name_.substr(0, name_.length() - 1);
124 }
125
126
127 // This method should return a reference, and throw an exception
128 // if the format named name cannot be found (Lgb)
129 Format const * Formats::getFormat(string const & name) const
130 {
131         FormatList::const_iterator cit =
132                 find_if(formatlist.begin(), formatlist.end(),
133                         FormatNamesEqual(name));
134         if (cit != formatlist.end())
135                 return &(*cit);
136         else
137                 return 0;
138 }
139
140
141 string Formats::getFormatFromFile(FileName const & filename) const
142 {
143         if (filename.empty())
144                 return string();
145
146         string const format = support::getFormatFromContents(filename);
147         if (!format.empty())
148                 return format;
149
150         // try to find a format from the file extension.
151         string const ext(support::getExtension(filename.absFilename()));
152         if (!ext.empty()) {
153                 // this is ambigous if two formats have the same extension,
154                 // but better than nothing
155                 Formats::const_iterator cit =
156                         find_if(formatlist.begin(), formatlist.end(),
157                                 FormatExtensionsEqual(ext));
158                 if (cit != formats.end()) {
159                         LYXERR(Debug::GRAPHICS)
160                                 << "\twill guess format from file extension: "
161                                 << ext << " -> " << cit->name() << std::endl;
162                         return cit->name();
163                 }
164         }
165         return string();
166 }
167
168 namespace {
169
170 string fixCommand(string const & cmd, string const & ext,
171                   os::auto_open_mode mode)
172 {
173         // configure.py says we do not want a viewer/editor
174         if (cmd.empty())
175                 return cmd;
176
177         // Does the OS manage this format?
178         if (os::canAutoOpenFile(ext, mode))
179                 return "auto";
180
181         // if configure.py found nothing, clear the command
182         if (token(cmd, ' ', 0) == "auto")
183                 return string();
184
185         // use the command found by configure.py
186         return cmd;
187 }
188
189 }
190
191 void Formats::setAutoOpen()
192 {
193         FormatList::iterator fit = formatlist.begin();
194         FormatList::iterator const fend = formatlist.end();
195         for ( ; fit != fend ; ++fit) {
196                 fit->setViewer(fixCommand(fit->viewer(), fit->extension(), os::VIEW));
197                 fit->setEditor(fixCommand(fit->editor(), fit->extension(), os::EDIT));
198         }
199 }
200
201
202 int Formats::getNumber(string const & name) const
203 {
204         FormatList::const_iterator cit =
205                 find_if(formatlist.begin(), formatlist.end(),
206                         FormatNamesEqual(name));
207         if (cit != formatlist.end())
208                 return distance(formatlist.begin(), cit);
209         else
210                 return -1;
211 }
212
213
214 void Formats::add(string const & name)
215 {
216         if (!getFormat(name))
217                 add(name, name, name, string(), string(), string(),
218                     Format::document);
219 }
220
221
222 void Formats::add(string const & name, string const & extension,
223                   string const & prettyname, string const & shortcut,
224                   string const & viewer, string const & editor,
225                   int flags)
226 {
227         FormatList::iterator it =
228                 find_if(formatlist.begin(), formatlist.end(),
229                         FormatNamesEqual(name));
230         if (it == formatlist.end())
231                 formatlist.push_back(Format(name, extension, prettyname,
232                                             shortcut, viewer, editor, flags));
233         else
234                 *it = Format(name, extension, prettyname, shortcut, viewer,
235                              editor, flags);
236 }
237
238
239 void Formats::erase(string const & name)
240 {
241         FormatList::iterator it =
242                 find_if(formatlist.begin(), formatlist.end(),
243                         FormatNamesEqual(name));
244         if (it != formatlist.end())
245                 formatlist.erase(it);
246 }
247
248
249 void Formats::sort()
250 {
251         std::sort(formatlist.begin(), formatlist.end());
252 }
253
254
255 void Formats::setViewer(string const & name, string const & command)
256 {
257         add(name);
258         FormatList::iterator it =
259                 find_if(formatlist.begin(), formatlist.end(),
260                         FormatNamesEqual(name));
261         if (it != formatlist.end())
262                 it->setViewer(command);
263 }
264
265
266 bool Formats::view(Buffer const & buffer, FileName const & filename,
267                    string const & format_name) const
268 {
269         if (filename.empty() || !fs::exists(filename.toFilesystemEncoding())) {
270                 Alert::error(_("Cannot view file"),
271                         bformat(_("File does not exist: %1$s"),
272                                 from_utf8(filename.absFilename())));
273                 return false;
274         }
275
276         Format const * format = getFormat(format_name);
277         if (format && format->viewer().empty() &&
278             format->isChildFormat())
279                 format = getFormat(format->parentFormat());
280         if (!format || format->viewer().empty()) {
281 // FIXME: I believe this is the wrong place to show alerts, it should be done
282 // by the caller (this should be "utility" code)
283                 Alert::error(_("Cannot view file"),
284                         bformat(_("No information for viewing %1$s"),
285                                 prettyName(format_name)));
286                 return false;
287         }
288         // viewer is 'auto'
289         if (format->viewer() == "auto") {
290                 if (os::autoOpenFile(filename.absFilename(), os::VIEW))
291                         return true;
292                 else {
293                         Alert::error(_("Cannot view file"),
294                                 bformat(_("Auto-view file %1$s failed"),
295                                         from_utf8(filename.absFilename())));
296                         return false;
297                 }
298         }
299
300         string command = libScriptSearch(format->viewer());
301
302         if (format_name == "dvi" &&
303             !lyxrc.view_dvi_paper_option.empty()) {
304                 command += ' ' + lyxrc.view_dvi_paper_option;
305                 string paper_size = buffer.params().paperSizeName();
306                 if (paper_size == "letter")
307                         paper_size = "us";
308                 command += ' ' + paper_size;
309                 if (buffer.params().orientation == ORIENTATION_LANDSCAPE)
310                         command += 'r';
311         }
312
313         if (!contains(command, token_from_format))
314                 command += ' ' + token_from_format;
315
316         command = subst(command, token_from_format, quoteName(filename.toFilesystemEncoding()));
317         command = subst(command, token_path_format, quoteName(onlyPath(filename.toFilesystemEncoding())));
318         command = subst(command, token_socket_format, quoteName(theServerSocket().address()));
319         LYXERR(Debug::FILES) << "Executing command: " << command << std::endl;
320         // FIXME UNICODE utf8 can be wrong for files
321         buffer.message(_("Executing command: ") + from_utf8(command));
322
323         Systemcall one;
324         int const res = one.startscript(Systemcall::DontWait, command);
325
326         if (res) {
327                 Alert::error(_("Cannot view file"),
328                              bformat(_("An error occurred whilst running %1$s"),
329                                makeDisplayPath(command, 50)));
330                 return false;
331         }
332         return true;
333 }
334
335
336 bool Formats::edit(Buffer const & buffer, FileName const & filename,
337                          string const & format_name) const
338 {
339         if (filename.empty() || !fs::exists(filename.toFilesystemEncoding())) {
340                 Alert::error(_("Cannot edit file"),
341                         bformat(_("File does not exist: %1$s"),
342                                 from_utf8(filename.absFilename())));
343                 return false;
344         }
345
346         Format const * format = getFormat(format_name);
347         if (format && format->editor().empty() &&
348             format->isChildFormat())
349                 format = getFormat(format->parentFormat());
350         if (!format || format->editor().empty()) {
351 // FIXME: I believe this is the wrong place to show alerts, it should
352 // be done by the caller (this should be "utility" code)
353                 Alert::error(_("Cannot edit file"),
354                         bformat(_("No information for editing %1$s"),
355                                 prettyName(format_name)));
356                 return false;
357         }
358         // editor is 'auto'
359         if (format->editor() == "auto") {
360                 if (os::autoOpenFile(filename.absFilename(), os::EDIT))
361                         return true;
362                 else {
363                         Alert::error(_("Cannot edit file"),
364                                 bformat(_("Auto-edit file %1$s failed"),
365                                         from_utf8(filename.absFilename())));
366                         return false;
367                 }
368         }
369
370         string command = format->editor();
371
372         if (!contains(command, token_from_format))
373                 command += ' ' + token_from_format;
374
375         command = subst(command, token_from_format, quoteName(filename.toFilesystemEncoding()));
376         command = subst(command, token_path_format, quoteName(onlyPath(filename.toFilesystemEncoding())));
377         command = subst(command, token_socket_format, quoteName(theServerSocket().address()));
378         LYXERR(Debug::FILES) << "Executing command: " << command << std::endl;
379         // FIXME UNICODE utf8 can be wrong for files
380         buffer.message(_("Executing command: ") + from_utf8(command));
381
382         Systemcall one;
383         int const res = one.startscript(Systemcall::DontWait, command);
384
385         if (res) {
386                 Alert::error(_("Cannot edit file"),
387                              bformat(_("An error occurred whilst running %1$s"),
388                                makeDisplayPath(command, 50)));
389                 return false;
390         }
391         return true;
392 }
393
394
395 docstring const Formats::prettyName(string const & name) const
396 {
397         Format const * format = getFormat(name);
398         if (format)
399                 return from_utf8(format->prettyname());
400         else
401                 return from_utf8(name);
402 }
403
404
405 string const Formats::extension(string const & name) const
406 {
407         Format const * format = getFormat(name);
408         if (format)
409                 return format->extension();
410         else
411                 return name;
412 }
413
414
415
416
417 Formats formats;
418
419 Formats system_formats;
420
421
422 } // namespace lyx