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