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