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