]> git.lyx.org Git - lyx.git/blob - src/Format.cpp
Embedding: saving inzip name to .lyx file so that embedded files can always be found...
[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                 command += ' ' + lyxrc.view_dvi_paper_option;
283                 string paper_size = buffer.params().paperSizeName();
284                 if (paper_size == "letter")
285                         paper_size = "us";
286                 command += ' ' + paper_size;
287                 if (buffer.params().orientation == ORIENTATION_LANDSCAPE)
288                         command += 'r';
289         }
290
291         if (!contains(command, token_from_format))
292                 command += ' ' + token_from_format;
293
294         command = subst(command, token_from_format, quoteName(filename.toFilesystemEncoding()));
295         command = subst(command, token_path_format, quoteName(onlyPath(filename.toFilesystemEncoding())));
296         command = subst(command, token_socket_format, quoteName(theServerSocket().address()));
297         LYXERR(Debug::FILES, "Executing command: " << command);
298         // FIXME UNICODE utf8 can be wrong for files
299         buffer.message(_("Executing command: ") + from_utf8(command));
300
301         Systemcall one;
302         int const res = one.startscript(Systemcall::DontWait, command);
303
304         if (res) {
305                 Alert::error(_("Cannot view file"),
306                              bformat(_("An error occurred whilst running %1$s"),
307                                makeDisplayPath(command, 50)));
308                 return false;
309         }
310         return true;
311 }
312
313
314 bool Formats::edit(Buffer const & buffer, FileName const & filename,
315                          string const & format_name) const
316 {
317         if (filename.empty() || !filename.exists()) {
318                 Alert::error(_("Cannot edit file"),
319                         bformat(_("File does not exist: %1$s"),
320                                 from_utf8(filename.absFilename())));
321                 return false;
322         }
323
324         Format const * format = getFormat(format_name);
325         if (format && format->editor().empty() &&
326             format->isChildFormat())
327                 format = getFormat(format->parentFormat());
328         if (!format || format->editor().empty()) {
329 // FIXME: I believe this is the wrong place to show alerts, it should
330 // be done by the caller (this should be "utility" code)
331                 Alert::error(_("Cannot edit file"),
332                         bformat(_("No information for editing %1$s"),
333                                 prettyName(format_name)));
334                 return false;
335         }
336         // editor is 'auto'
337         if (format->editor() == "auto") {
338                 if (os::autoOpenFile(filename.absFilename(), os::EDIT))
339                         return true;
340                 else {
341                         Alert::error(_("Cannot edit file"),
342                                 bformat(_("Auto-edit file %1$s failed"),
343                                         from_utf8(filename.absFilename())));
344                         return false;
345                 }
346         }
347
348         string command = format->editor();
349
350         if (!contains(command, token_from_format))
351                 command += ' ' + token_from_format;
352
353         command = subst(command, token_from_format, quoteName(filename.toFilesystemEncoding()));
354         command = subst(command, token_path_format, quoteName(onlyPath(filename.toFilesystemEncoding())));
355         command = subst(command, token_socket_format, quoteName(theServerSocket().address()));
356         LYXERR(Debug::FILES, "Executing command: " << command);
357         // FIXME UNICODE utf8 can be wrong for files
358         buffer.message(_("Executing command: ") + from_utf8(command));
359
360         Systemcall one;
361         int const res = one.startscript(Systemcall::DontWait, command);
362
363         if (res) {
364                 Alert::error(_("Cannot edit file"),
365                              bformat(_("An error occurred whilst running %1$s"),
366                                makeDisplayPath(command, 50)));
367                 return false;
368         }
369         return true;
370 }
371
372
373 docstring const Formats::prettyName(string const & name) const
374 {
375         Format const * format = getFormat(name);
376         if (format)
377                 return from_utf8(format->prettyname());
378         else
379                 return from_utf8(name);
380 }
381
382
383 string const Formats::extension(string const & name) const
384 {
385         Format const * format = getFormat(name);
386         if (format)
387                 return format->extension();
388         else
389                 return name;
390 }
391
392
393
394
395 Formats formats;
396
397 Formats system_formats;
398
399
400 } // namespace lyx