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