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