]> git.lyx.org Git - lyx.git/blob - src/format.C
don't pass temporary string objects as parameters
[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/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::libScriptSearch;
39 using support::makeDisplayPath;
40 using support::onlyPath;
41 using support::quoteName;
42 using support::subst;
43 using support::Systemcall;
44 using support::token;
45
46 using std::string;
47 using std::distance;
48
49 namespace Alert = frontend::Alert;
50 namespace fs = boost::filesystem;
51 namespace os = support::os;
52
53 namespace {
54
55 string const token_from("$$i");
56 string const token_path("$$p");
57 string const token_socket("$$a");
58
59
60 class FormatNamesEqual : public std::unary_function<Format, bool> {
61 public:
62         FormatNamesEqual(string const & name)
63                 : name_(name) {}
64         bool operator()(Format const & f) const
65         {
66                 return f.name() == name_;
67         }
68 private:
69         string name_;
70 };
71
72
73 class FormatExtensionsEqual : public std::unary_function<Format, bool> {
74 public:
75         FormatExtensionsEqual(string const & extension)
76                 : extension_(extension) {}
77         bool operator()(Format const & f) const
78         {
79                 return f.extension() == extension_;
80         }
81 private:
82         string extension_;
83 };
84
85 } //namespace anon
86
87 bool operator<(Format const & a, Format const & b)
88 {
89         // use the compare_ascii_no_case instead of compare_no_case,
90         // because in turkish, 'i' is not the lowercase version of 'I',
91         // and thus turkish locale breaks parsing of tags.
92
93         return compare_ascii_no_case(a.prettyname(), b.prettyname()) < 0;
94 }
95
96
97 Format::Format(string const & n, string const & e, string const & p,
98                string const & s, string const & v, string const & ed,
99                int flags)
100         : name_(n), extension_(e), prettyname_(p), shortcut_(s), viewer_(v),
101           editor_(ed), flags_(flags)
102 {}
103
104
105 bool Format::dummy() const
106 {
107         return extension().empty();
108 }
109
110
111 bool Format::isChildFormat() const
112 {
113         if (name_.empty())
114                 return false;
115         return isdigit(name_[name_.length() - 1]);
116 }
117
118
119 string const Format::parentFormat() const
120 {
121         return name_.substr(0, name_.length() - 1);
122 }
123
124
125 // This method should return a reference, and throw an exception
126 // if the format named name cannot be found (Lgb)
127 Format const * Formats::getFormat(string const & name) const
128 {
129         FormatList::const_iterator cit =
130                 find_if(formatlist.begin(), formatlist.end(),
131                         FormatNamesEqual(name));
132         if (cit != formatlist.end())
133                 return &(*cit);
134         else
135                 return 0;
136 }
137
138
139 string Formats::getFormatFromFile(string const & filename) const
140 {
141         if (filename.empty())
142                 return string();
143
144         string const format = support::getFormatFromContents(filename);
145         if (!format.empty())
146                 return format;
147
148         // try to find a format from the file extension.
149         string const ext(support::getExtension(filename));
150         if (!ext.empty()) {
151                 // this is ambigous if two formats have the same extension,
152                 // but better than nothing
153                 Formats::const_iterator cit =
154                         find_if(formatlist.begin(), formatlist.end(),
155                                 FormatExtensionsEqual(ext));
156                 if (cit != formats.end()) {
157                         lyxerr[Debug::GRAPHICS]
158                                 << "\twill guess format from file extension: "
159                                 << ext << " -> " << cit->name() << std::endl;
160                         return cit->name();
161                 }
162         }
163         return string();
164 }
165
166 namespace {
167
168 string fixCommand(string const & cmd, string const & ext,
169                   os::auto_open_mode mode)
170 {
171         // configure.py says we do not want a viewer/editor
172         if (cmd.empty())
173                 return cmd;
174
175         // Does the OS manage this format?
176         if (os::canAutoOpenFile(ext, mode))
177                 return "auto";
178
179         // if configure.py found nothing, clear the command
180         if (token(cmd, ' ', 0) == "auto")
181                 return string();
182
183         // use the command found by configure.py
184         return cmd;
185 }
186
187 }
188
189 void Formats::setAutoOpen()
190 {
191         FormatList::iterator fit = formatlist.begin();
192         FormatList::iterator const fend = formatlist.end();
193         for ( ; fit != fend ; ++fit) {
194                 fit->setViewer(fixCommand(fit->viewer(), fit->extension(), os::VIEW));
195                 fit->setEditor(fixCommand(fit->editor(), fit->extension(), os::EDIT));
196         }
197 }
198
199
200 int Formats::getNumber(string const & name) const
201 {
202         FormatList::const_iterator cit =
203                 find_if(formatlist.begin(), formatlist.end(),
204                         FormatNamesEqual(name));
205         if (cit != formatlist.end())
206                 return distance(formatlist.begin(), cit);
207         else
208                 return -1;
209 }
210
211
212 void Formats::add(string const & name)
213 {
214         if (!getFormat(name))
215                 add(name, name, name, string(), string(), string(),
216                     Format::document);
217 }
218
219
220 void Formats::add(string const & name, string const & extension,
221                   string const & prettyname, string const & shortcut,
222                   string const & viewer, string const & editor,
223                   int flags)
224 {
225         FormatList::iterator it =
226                 find_if(formatlist.begin(), formatlist.end(),
227                         FormatNamesEqual(name));
228         if (it == formatlist.end())
229                 formatlist.push_back(Format(name, extension, prettyname,
230                                             shortcut, viewer, editor, flags));
231         else
232                 *it = Format(name, extension, prettyname, shortcut, viewer,
233                              editor, flags);
234 }
235
236
237 void Formats::erase(string const & name)
238 {
239         FormatList::iterator it =
240                 find_if(formatlist.begin(), formatlist.end(),
241                         FormatNamesEqual(name));
242         if (it != formatlist.end())
243                 formatlist.erase(it);
244 }
245
246
247 void Formats::sort()
248 {
249         std::sort(formatlist.begin(), formatlist.end());
250 }
251
252
253 void Formats::setViewer(string const & name, string const & command)
254 {
255         add(name);
256         FormatList::iterator it =
257                 find_if(formatlist.begin(), formatlist.end(),
258                         FormatNamesEqual(name));
259         if (it != formatlist.end())
260                 it->setViewer(command);
261 }
262
263
264 bool Formats::view(Buffer const & buffer, string const & filename,
265                    string const & format_name) const
266 {
267         BOOST_ASSERT(absolutePath(filename));
268         if (filename.empty() || !fs::exists(filename)) {
269                 Alert::error(_("Cannot view file"),
270                         bformat(_("File does not exist: %1$s"),
271                                 from_utf8(filename)));
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, 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)));
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))
313                 command += ' ' + token_from;
314
315         command = subst(command, token_from, quoteName(filename));
316         command = subst(command, token_path, quoteName(onlyPath(filename)));
317         command = subst(command, token_socket, quoteName(theLyXServerSocket().address()));
318         lyxerr[Debug::FILES] << "Executing command: " << command << std::endl;
319         buffer.message(_("Executing command: ") + from_utf8(command));
320
321         Systemcall one;
322         int const res = one.startscript(Systemcall::DontWait, command);
323
324         if (res) {
325                 Alert::error(_("Cannot view file"),
326                              bformat(_("An error occurred whilst running %1$s"),
327                                makeDisplayPath(command, 50)));
328                 return false;
329         }
330         return true;
331 }
332
333
334 bool Formats::edit(Buffer const & buffer, string const & filename,
335                          string const & format_name) const
336 {
337         BOOST_ASSERT(absolutePath(filename));
338         if (filename.empty() || !fs::exists(filename)) {
339                 Alert::error(_("Cannot edit file"),
340                         bformat(_("File does not exist: %1$s"),
341                                 from_utf8(filename)));
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, 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)));
365                         return false;
366                 }
367         }
368
369         string command = format->editor();
370
371         if (!contains(command, token_from))
372                 command += ' ' + token_from;
373
374         command = subst(command, token_from, quoteName(filename));
375         command = subst(command, token_path, quoteName(onlyPath(filename)));
376         command = subst(command, token_socket, quoteName(theLyXServerSocket().address()));
377         lyxerr[Debug::FILES] << "Executing command: " << command << std::endl;
378         buffer.message(_("Executing command: ") + from_utf8(command));
379
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 docstring const Formats::prettyName(string const & name) const
394 {
395         Format const * format = getFormat(name);
396         if (format)
397                 return from_utf8(format->prettyname());
398         else
399                 return from_utf8(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;
418
419
420 } // namespace lyx