]> git.lyx.org Git - lyx.git/blob - src/format.C
Change the semantics of 'none' and 'auto' viewers/editors: 'none' means now
[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/path.h"
27 #include "support/systemcall.h"
28
29 #include <boost/filesystem/operations.hpp>
30
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::onlyFilename;
37 using lyx::support::onlyPath;
38 using lyx::support::Path;
39 using lyx::support::quoteName;
40 using lyx::support::subst;
41 using lyx::support::Systemcall;
42 using lyx::support::token;
43
44 using std::string;
45 using std::distance;
46
47 namespace fs = boost::filesystem;
48 namespace os = lyx::support::os;
49
50 extern LyXServerSocket * lyxsocket;
51
52 namespace {
53
54 string const token_from("$$i");
55 string const token_path("$$p");
56 string const token_socket("$$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                bool d)
99         : name_(n), extension_(e), prettyname_(p), shortcut_(s), viewer_(v),
100           editor_(ed), document_(d)
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(string const & filename) const
139 {
140         if (filename.empty())
141                 return string();
142
143         string const format = lyx::support::getFormatFromContents(filename);
144         if (!format.empty())
145                 return format;
146
147         // try to find a format from the file extension.
148         string const ext(lyx::support::getExtension(filename));
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 namespace {
166
167 string fixCommand(string const & cmd, string const & ext, 
168                   os::auto_open_mode mode)
169 {
170         // configure.py says we do not want a viewer/editor
171         if (cmd.empty())
172                 return cmd;
173
174         // Does the OS manage this format?
175         if (os::canAutoOpenFile(ext, mode))
176                 return "auto";
177
178         // if configure.py found nothing, clear the command
179         if (token(cmd, ' ', 0) == "auto")
180                 return string();
181
182         // use the command found by configure.py
183         return cmd;
184 }
185
186 }
187
188 void Formats::setAutoOpen()
189 {
190         FormatList::iterator fit = formatlist.begin();
191         FormatList::iterator const fend = formatlist.end();
192         for ( ; fit != fend ; ++fit) {
193                 fit->setViewer(fixCommand(fit->viewer(), fit->extension(), os::VIEW));
194                 fit->setEditor(fixCommand(fit->editor(), fit->extension(), os::EDIT));
195         }
196 }
197
198
199 int Formats::getNumber(string const & name) const
200 {
201         FormatList::const_iterator cit =
202                 find_if(formatlist.begin(), formatlist.end(),
203                         FormatNamesEqual(name));
204         if (cit != formatlist.end())
205                 return distance(formatlist.begin(), cit);
206         else
207                 return -1;
208 }
209
210
211 void Formats::add(string const & name)
212 {
213         if (!getFormat(name))
214                 add(name, name, name, string(), string(), string(), true);
215 }
216
217
218 void Formats::add(string const & name, string const & extension,
219                   string const & prettyname, string const & shortcut,
220                   string const & viewer, string const & editor, bool document)
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,
228                                             document));
229         else
230                 *it = Format(name, extension, prettyname, shortcut, viewer,
231                              editor, document);
232 }
233
234
235 void Formats::erase(string const & name)
236 {
237         FormatList::iterator it =
238                 find_if(formatlist.begin(), formatlist.end(),
239                         FormatNamesEqual(name));
240         if (it != formatlist.end())
241                 formatlist.erase(it);
242 }
243
244
245 void Formats::sort()
246 {
247         std::sort(formatlist.begin(), formatlist.end());
248 }
249
250
251 void Formats::setViewer(string const & name, string const & command)
252 {
253         add(name);
254         FormatList::iterator it =
255                 find_if(formatlist.begin(), formatlist.end(),
256                         FormatNamesEqual(name));
257         if (it != formatlist.end())
258                 it->setViewer(command);
259 }
260
261
262 bool Formats::view(Buffer const & buffer, string const & filename,
263                    string const & format_name) const
264 {
265         if (filename.empty() || !fs::exists(filename)) {
266                 Alert::error(_("Cannot view file"),
267                         bformat(_("File does not exist: %1$s"),
268                                 filename));
269                 return false;
270         }
271
272         Format const * format = getFormat(format_name);
273         if (format && format->viewer().empty() &&
274             format->isChildFormat())
275                 format = getFormat(format->parentFormat());
276         if (!format || format->viewer().empty()) {
277 // FIXME: I believe this is the wrong place to show alerts, it should be done
278 // by the caller (this should be "utility" code)
279                 Alert::error(_("Cannot view file"),
280                         bformat(_("No information for viewing %1$s"),
281                                 prettyName(format_name)));
282                 return false;
283         }
284         // viewer is 'auto'
285         if (format->viewer() == "auto") {
286                 if (os::autoOpenFile(filename, os::VIEW))
287                         return true;
288                 else {
289                         Alert::error(_("Cannot view file"),
290                                 bformat(_("Auto-view file %1$s failed"),
291                                         filename));
292                         return false;
293                 }
294         }
295
296         string command = libScriptSearch(format->viewer());
297
298         if (format_name == "dvi" &&
299             !lyxrc.view_dvi_paper_option.empty()) {
300                 command += ' ' + lyxrc.view_dvi_paper_option;
301                 string paper_size = buffer.params().paperSizeName();
302                 if (paper_size == "letter")
303                         paper_size = "us";
304                 command += ' ' + paper_size;
305                 if (buffer.params().orientation == ORIENTATION_LANDSCAPE)
306                         command += 'r';
307         }
308
309         if (!contains(command, token_from))
310                 command += ' ' + token_from;
311
312         command = subst(command, token_from,
313                         quoteName(onlyFilename(filename)));
314         command = subst(command, token_path, quoteName(onlyPath(filename)));
315         command = subst(command, token_socket, quoteName(lyxsocket->address()));
316         lyxerr[Debug::FILES] << "Executing command: " << command << std::endl;
317         buffer.message(_("Executing command: ") + command);
318
319         Path p(onlyPath(filename));
320         Systemcall one;
321         int const res = one.startscript(Systemcall::DontWait, command);
322
323         if (res) {
324                 Alert::error(_("Cannot view file"),
325                              bformat(_("An error occurred whilst running %1$s"),
326                                makeDisplayPath(command, 50)));
327                 return false;
328         }
329         return true;
330 }
331
332
333 bool Formats::edit(Buffer const & buffer, string const & filename,
334                          string const & format_name) const
335 {
336         if (filename.empty() || !fs::exists(filename)) {
337                 Alert::error(_("Cannot edit file"),
338                         bformat(_("File does not exist: %1$s"),
339                                 filename));
340                 return false;
341         }
342
343         Format const * format = getFormat(format_name);
344         if (format && format->editor().empty() &&
345             format->isChildFormat())
346                 format = getFormat(format->parentFormat());
347         if (!format || format->editor().empty()) {
348 // FIXME: I believe this is the wrong place to show alerts, it should
349 // be done by the caller (this should be "utility" code)
350                 Alert::error(_("Cannot edit file"),
351                         bformat(_("No information for editing %1$s"),
352                                 prettyName(format_name)));
353                 return false;
354         }
355         // editor is 'auto'
356         if (format->editor() == "auto") {
357                 if (os::autoOpenFile(filename, os::EDIT))
358                         return true;
359                 else {
360                         Alert::error(_("Cannot edit file"),
361                                 bformat(_("Auto-edit file %1$s failed"),
362                                         filename));
363                         return false;
364                 }
365         }
366
367         string command = format->editor();
368
369         if (!contains(command, token_from))
370                 command += ' ' + token_from;
371
372         command = subst(command, token_from,
373                         quoteName(onlyFilename(filename)));
374         command = subst(command, token_path, quoteName(onlyPath(filename)));
375         command = subst(command, token_socket, quoteName(lyxsocket->address()));
376         lyxerr[Debug::FILES] << "Executing command: " << command << std::endl;
377         buffer.message(_("Executing command: ") + command);
378
379         Path p(onlyPath(filename));
380         Systemcall one;
381         int const res = one.startscript(Systemcall::DontWait, command);
382
383         if (res) {
384                 Alert::error(_("Cannot edit file"),
385                              bformat(_("An error occurred whilst running %1$s"),
386                                makeDisplayPath(command, 50)));
387                 return false;
388         }
389         return true;
390 }
391
392
393 string const Formats::prettyName(string const & name) const
394 {
395         Format const * format = getFormat(name);
396         if (format)
397                 return format->prettyname();
398         else
399                 return name;
400 }
401
402
403 string const Formats::extension(string const & name) const
404 {
405         Format const * format = getFormat(name);
406         if (format)
407                 return format->extension();
408         else
409                 return name;
410 }
411
412
413
414
415 Formats formats;
416
417 Formats system_formats;