]> git.lyx.org Git - features.git/blob - src/Format.cpp
a28629fad1c149c369ac691b60ea0a3c80df2676
[features.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/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 using std::find_if;
29 using std::string;
30 using std::distance;
31
32
33 namespace lyx {
34
35 using support::absolutePath;
36 using support::bformat;
37 using support::compare_ascii_no_case;
38 using support::contains;
39 using support::FileName;
40 using support::libScriptSearch;
41 using support::makeDisplayPath;
42 using support::onlyPath;
43 using support::quoteName;
44 using support::subst;
45 using support::Systemcall;
46 using support::token;
47
48 namespace Alert = frontend::Alert;
49 namespace os = support::os;
50
51 namespace {
52
53 string const token_from_format("$$i");
54 string const token_path_format("$$p");
55 string const token_socket_format("$$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(FileName const & filename) const
138 {
139         if (filename.empty())
140                 return string();
141
142         string const format = filename.guessFormatFromContents();
143         if (!format.empty())
144                 return format;
145
146         // try to find a format from the file extension.
147         string const ext = support::getExtension(filename.absFilename());
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
165 static string fixCommand(string const & cmd, string const & ext,
166                   os::auto_open_mode mode)
167 {
168         // configure.py says we do not want a viewer/editor
169         if (cmd.empty())
170                 return cmd;
171
172         // Does the OS manage this format?
173         if (os::canAutoOpenFile(ext, mode))
174                 return "auto";
175
176         // if configure.py found nothing, clear the command
177         if (token(cmd, ' ', 0) == "auto")
178                 return string();
179
180         // use the command found by configure.py
181         return cmd;
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(),
212                     Format::document);
213 }
214
215
216 void Formats::add(string const & name, string const & extension,
217                   string const & prettyname, string const & shortcut,
218                   string const & viewer, string const & editor,
219                   int flags)
220 {
221         FormatList::iterator it =
222                 find_if(formatlist.begin(), formatlist.end(),
223                         FormatNamesEqual(name));
224         if (it == formatlist.end())
225                 formatlist.push_back(Format(name, extension, prettyname,
226                                             shortcut, viewer, editor, flags));
227         else
228                 *it = Format(name, extension, prettyname, shortcut, viewer,
229                              editor, flags);
230 }
231
232
233 void Formats::erase(string const & name)
234 {
235         FormatList::iterator it =
236                 find_if(formatlist.begin(), formatlist.end(),
237                         FormatNamesEqual(name));
238         if (it != formatlist.end())
239                 formatlist.erase(it);
240 }
241
242
243 void Formats::sort()
244 {
245         std::sort(formatlist.begin(), formatlist.end());
246 }
247
248
249 void Formats::setViewer(string const & name, string const & command)
250 {
251         add(name);
252         FormatList::iterator it =
253                 find_if(formatlist.begin(), formatlist.end(),
254                         FormatNamesEqual(name));
255         if (it != formatlist.end())
256                 it->setViewer(command);
257 }
258
259
260 bool Formats::view(Buffer const & buffer, FileName const & filename,
261                    string const & format_name) const
262 {
263         if (filename.empty() || !filename.exists()) {
264                 Alert::error(_("Cannot view file"),
265                         bformat(_("File does not exist: %1$s"),
266                                 from_utf8(filename.absFilename())));
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.absFilename(), os::VIEW))
285                         return true;
286                 else {
287                         Alert::error(_("Cannot view file"),
288                                 bformat(_("Auto-view file %1$s failed"),
289                                         from_utf8(filename.absFilename())));
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_format))
308                 command += ' ' + token_from_format;
309
310         command = subst(command, token_from_format, quoteName(filename.toFilesystemEncoding()));
311         command = subst(command, token_path_format, quoteName(onlyPath(filename.toFilesystemEncoding())));
312         command = subst(command, token_socket_format, quoteName(theServerSocket().address()));
313         LYXERR(Debug::FILES) << "Executing command: " << command << std::endl;
314         // FIXME UNICODE utf8 can be wrong for files
315         buffer.message(_("Executing command: ") + from_utf8(command));
316
317         Systemcall one;
318         int const res = one.startscript(Systemcall::DontWait, command);
319
320         if (res) {
321                 Alert::error(_("Cannot view file"),
322                              bformat(_("An error occurred whilst running %1$s"),
323                                makeDisplayPath(command, 50)));
324                 return false;
325         }
326         return true;
327 }
328
329
330 bool Formats::edit(Buffer const & buffer, FileName const & filename,
331                          string const & format_name) const
332 {
333         if (filename.empty() || !filename.exists()) {
334                 Alert::error(_("Cannot edit file"),
335                         bformat(_("File does not exist: %1$s"),
336                                 from_utf8(filename.absFilename())));
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.absFilename(), os::EDIT))
355                         return true;
356                 else {
357                         Alert::error(_("Cannot edit file"),
358                                 bformat(_("Auto-edit file %1$s failed"),
359                                         from_utf8(filename.absFilename())));
360                         return false;
361                 }
362         }
363
364         string command = format->editor();
365
366         if (!contains(command, token_from_format))
367                 command += ' ' + token_from_format;
368
369         command = subst(command, token_from_format, quoteName(filename.toFilesystemEncoding()));
370         command = subst(command, token_path_format, quoteName(onlyPath(filename.toFilesystemEncoding())));
371         command = subst(command, token_socket_format, quoteName(theServerSocket().address()));
372         LYXERR(Debug::FILES) << "Executing command: " << command << std::endl;
373         // FIXME UNICODE utf8 can be wrong for files
374         buffer.message(_("Executing command: ") + from_utf8(command));
375
376         Systemcall one;
377         int const res = one.startscript(Systemcall::DontWait, command);
378
379         if (res) {
380                 Alert::error(_("Cannot edit file"),
381                              bformat(_("An error occurred whilst running %1$s"),
382                                makeDisplayPath(command, 50)));
383                 return false;
384         }
385         return true;
386 }
387
388
389 docstring const Formats::prettyName(string const & name) const
390 {
391         Format const * format = getFormat(name);
392         if (format)
393                 return from_utf8(format->prettyname());
394         else
395                 return from_utf8(name);
396 }
397
398
399 string const Formats::extension(string const & name) const
400 {
401         Format const * format = getFormat(name);
402         if (format)
403                 return format->extension();
404         else
405                 return name;
406 }
407
408
409
410
411 Formats formats;
412
413 Formats system_formats;
414
415
416 } // namespace lyx