]> git.lyx.org Git - lyx.git/blob - src/format.C
minimal effort implementation of:
[lyx.git] / src / format.C
1 /**
2  * \file format.C
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 "lyxsocket.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 #include <boost/filesystem/operations.hpp>
29
30 using lyx::support::absolutePath;
31 using lyx::support::bformat;
32 using lyx::support::compare_ascii_no_case;
33 using lyx::support::contains;
34 using lyx::support::libScriptSearch;
35 using lyx::support::makeDisplayPath;
36 using lyx::support::onlyPath;
37 using lyx::support::quoteName;
38 using lyx::support::subst;
39 using lyx::support::Systemcall;
40 using lyx::support::token;
41
42 using std::string;
43 using std::distance;
44
45 namespace fs = boost::filesystem;
46 namespace os = lyx::support::os;
47
48 extern LyXServerSocket * lyxsocket;
49
50 namespace {
51
52 string const token_from("$$i");
53 string const token_path("$$p");
54 string const token_socket("$$a");
55
56
57 class FormatNamesEqual : public std::unary_function<Format, bool> {
58 public:
59         FormatNamesEqual(string const & name)
60                 : name_(name) {}
61         bool operator()(Format const & f) const
62         {
63                 return f.name() == name_;
64         }
65 private:
66         string name_;
67 };
68
69
70 class FormatExtensionsEqual : public std::unary_function<Format, bool> {
71 public:
72         FormatExtensionsEqual(string const & extension)
73                 : extension_(extension) {}
74         bool operator()(Format const & f) const
75         {
76                 return f.extension() == extension_;
77         }
78 private:
79         string extension_;
80 };
81
82 } //namespace anon
83
84 bool operator<(Format const & a, Format const & b)
85 {
86         // use the compare_ascii_no_case instead of compare_no_case,
87         // because in turkish, 'i' is not the lowercase version of 'I',
88         // and thus turkish locale breaks parsing of tags.
89
90         return compare_ascii_no_case(a.prettyname(), b.prettyname()) < 0;
91 }
92
93
94 Format::Format(string const & n, string const & e, string const & p,
95                string const & s, string const & v, string const & ed,
96                bool d)
97         : name_(n), extension_(e), prettyname_(p), shortcut_(s), viewer_(v),
98           editor_(ed), document_(d)
99 {}
100
101
102 bool Format::dummy() const
103 {
104         return extension().empty();
105 }
106
107
108 bool Format::isChildFormat() const
109 {
110         if (name_.empty())
111                 return false;
112         return isdigit(name_[name_.length() - 1]);
113 }
114
115
116 string const Format::parentFormat() const
117 {
118         return name_.substr(0, name_.length() - 1);
119 }
120
121
122 // This method should return a reference, and throw an exception
123 // if the format named name cannot be found (Lgb)
124 Format const * Formats::getFormat(string const & name) const
125 {
126         FormatList::const_iterator cit =
127                 find_if(formatlist.begin(), formatlist.end(),
128                         FormatNamesEqual(name));
129         if (cit != formatlist.end())
130                 return &(*cit);
131         else
132                 return 0;
133 }
134
135
136 string Formats::getFormatFromFile(string const & filename) const
137 {
138         if (filename.empty())
139                 return string();
140
141         string const format = lyx::support::getFormatFromContents(filename);
142         if (!format.empty())
143                 return format;
144
145         // try to find a format from the file extension.
146         string const ext(lyx::support::getExtension(filename));
147         if (!ext.empty()) {
148                 // this is ambigous if two formats have the same extension,
149                 // but better than nothing
150                 Formats::const_iterator cit =
151                         find_if(formatlist.begin(), formatlist.end(),
152                                 FormatExtensionsEqual(ext));
153                 if (cit != formats.end()) {
154                         lyxerr[Debug::GRAPHICS]
155                                 << "\twill guess format from file extension: "
156                                 << ext << " -> " << cit->name() << std::endl;
157                         return cit->name();
158                 }
159         }
160         return string();
161 }
162
163 namespace {
164
165 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
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(), true);
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, bool document)
219 {
220         FormatList::iterator it =
221                 find_if(formatlist.begin(), formatlist.end(),
222                         FormatNamesEqual(name));
223         if (it == formatlist.end())
224                 formatlist.push_back(Format(name, extension, prettyname,
225                                             shortcut, viewer, editor,
226                                             document));
227         else
228                 *it = Format(name, extension, prettyname, shortcut, viewer,
229                              editor, document);
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, string const & filename,
261                    string const & format_name) const
262 {
263         BOOST_ASSERT(absolutePath(filename));
264         if (filename.empty() || !fs::exists(filename)) {
265                 Alert::error(_("Cannot view file"),
266                         bformat(_("File does not exist: %1$s"),
267                                 filename));
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, os::VIEW))
286                         return true;
287                 else {
288                         Alert::error(_("Cannot view file"),
289                                 bformat(_("Auto-view file %1$s failed"),
290                                         filename));
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))
309                 command += ' ' + token_from;
310
311         command = subst(command, token_from, quoteName(filename));
312         command = subst(command, token_path, quoteName(onlyPath(filename)));
313         command = subst(command, token_socket, quoteName(lyxsocket->address()));
314         lyxerr[Debug::FILES] << "Executing command: " << command << std::endl;
315         buffer.message(_("Executing command: ") + 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, string const & filename,
331                          string const & format_name) const
332 {
333         BOOST_ASSERT(absolutePath(filename));
334         if (filename.empty() || !fs::exists(filename)) {
335                 Alert::error(_("Cannot edit file"),
336                         bformat(_("File does not exist: %1$s"),
337                                 filename));
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, os::EDIT))
356                         return true;
357                 else {
358                         Alert::error(_("Cannot edit file"),
359                                 bformat(_("Auto-edit file %1$s failed"),
360                                         filename));
361                         return false;
362                 }
363         }
364
365         string command = format->editor();
366
367         if (!contains(command, token_from))
368                 command += ' ' + token_from;
369
370         command = subst(command, token_from, quoteName(filename));
371         command = subst(command, token_path, quoteName(onlyPath(filename)));
372         command = subst(command, token_socket, quoteName(lyxsocket->address()));
373         lyxerr[Debug::FILES] << "Executing command: " << command << std::endl;
374         buffer.message(_("Executing command: ") + 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 string const Formats::prettyName(string const & name) const
390 {
391         Format const * format = getFormat(name);
392         if (format)
393                 return format->prettyname();
394         else
395                 return 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;