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