]> git.lyx.org Git - lyx.git/blob - src/format.C
- Link against qt-mt333.lib which is what the current qt3 cvs produces
[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/path.h"
25 #include "support/systemcall.h"
26
27 using lyx::support::bformat;
28 using lyx::support::compare_ascii_no_case;
29 using lyx::support::contains;
30 using lyx::support::MakeDisplayPath;
31 using lyx::support::OnlyFilename;
32 using lyx::support::OnlyPath;
33 using lyx::support::Path;
34 using lyx::support::QuoteName;
35 using lyx::support::subst;
36 using lyx::support::Systemcall;
37
38 using std::string;
39 using std::distance;
40
41 extern LyXServerSocket * lyxsocket;
42
43 namespace {
44
45 string const token_from("$$i");
46 string const token_path("$$p");
47 string const token_socket("$$a");
48
49
50 class FormatNamesEqual : public std::unary_function<Format, bool> {
51 public:
52         FormatNamesEqual(string const & name)
53                 : name_(name) {}
54         bool operator()(Format const & f) const
55         {
56                 return f.name() == name_;
57         }
58 private:
59         string name_;
60 };
61
62
63 class FormatExtensionsEqual : public std::unary_function<Format, bool> {
64 public:
65         FormatExtensionsEqual(string const & extension)
66                 : extension_(extension) {}
67         bool operator()(Format const & f) const
68         {
69                 return f.extension() == extension_;
70         }
71 private:
72         string extension_;
73 };
74
75 } //namespace anon
76
77 bool operator<(Format const & a, Format const & b)
78 {
79         // use the compare_ascii_no_case instead of compare_no_case,
80         // because in turkish, 'i' is not the lowercase version of 'I',
81         // and thus turkish locale breaks parsing of tags.
82
83         return compare_ascii_no_case(a.prettyname(), b.prettyname()) < 0;
84 }
85
86 Format::Format(string const & n, string const & e, string const & p,
87                string const & s, string const & v, string const & ed)
88         : name_(n), extension_(e), prettyname_(p), shortcut_(s), viewer_(v), editor_(ed)
89 {}
90
91
92 bool Format::dummy() const
93 {
94         return extension().empty();
95 }
96
97
98 bool Format::isChildFormat() const
99 {
100         if (name_.empty())
101                 return false;
102         return isdigit(name_[name_.length() - 1]);
103 }
104
105
106 string const Format::parentFormat() const
107 {
108         return name_.substr(0, name_.length() - 1);
109 }
110
111
112 // This method should return a reference, and throw an exception
113 // if the format named name cannot be found (Lgb)
114 Format const * Formats::getFormat(string const & name) const
115 {
116         FormatList::const_iterator cit =
117                 find_if(formatlist.begin(), formatlist.end(),
118                         FormatNamesEqual(name));
119         if (cit != formatlist.end())
120                 return &(*cit);
121         else
122                 return 0;
123 }
124
125
126 string Formats::getFormatFromFile(string const & filename) const
127 {
128         if (filename.empty())
129                 return string();
130
131         string const format = lyx::support::getFormatFromContents(filename);
132         if (!format.empty())
133                 return format;
134
135         // try to find a format from the file extension.
136         string const ext(lyx::support::GetExtension(filename));
137         if (!ext.empty()) {
138                 // this is ambigous if two formats have the same extension,
139                 // but better than nothing
140                 Formats::const_iterator cit =
141                         find_if(formatlist.begin(), formatlist.end(),
142                                 FormatExtensionsEqual(ext));
143                 if (cit != formats.end()) {
144                         lyxerr[Debug::GRAPHICS]
145                                 << "\twill guess format from file extension: "
146                                 << ext << " -> " << cit->name() << std::endl;
147                         return cit->name();
148                 }
149         }
150         return string();
151 }
152
153
154 int Formats::getNumber(string const & name) const
155 {
156         FormatList::const_iterator cit =
157                 find_if(formatlist.begin(), formatlist.end(),
158                         FormatNamesEqual(name));
159         if (cit != formatlist.end())
160                 return distance(formatlist.begin(), cit);
161         else
162                 return -1;
163 }
164
165
166 void Formats::add(string const & name)
167 {
168         if (!getFormat(name))
169                 add(name, name, name, string(), string(), string());
170 }
171
172
173 void Formats::add(string const & name, string const & extension,
174                   string const & prettyname, string const & shortcut,
175                   string const & viewer, string const & editor)
176 {
177         FormatList::iterator it =
178                 find_if(formatlist.begin(), formatlist.end(),
179                         FormatNamesEqual(name));
180         if (it == formatlist.end())
181                 formatlist.push_back(Format(name, extension, prettyname,
182                                             shortcut, viewer, editor));
183         else
184                 *it = Format(name, extension, prettyname, shortcut, viewer, editor);
185 }
186
187
188 void Formats::erase(string const & name)
189 {
190         FormatList::iterator it =
191                 find_if(formatlist.begin(), formatlist.end(),
192                         FormatNamesEqual(name));
193         if (it != formatlist.end())
194                 formatlist.erase(it);
195 }
196
197
198 void Formats::sort()
199 {
200         std::sort(formatlist.begin(), formatlist.end());
201 }
202
203
204 void Formats::setViewer(string const & name, string const & command)
205 {
206         add(name);
207         FormatList::iterator it =
208                 find_if(formatlist.begin(), formatlist.end(),
209                         FormatNamesEqual(name));
210         if (it != formatlist.end())
211                 it->setViewer(command);
212 }
213
214
215 bool Formats::view(Buffer const & buffer, string const & filename,
216                    string const & format_name) const
217 {
218         if (filename.empty())
219                 return false;
220
221         Format const * format = getFormat(format_name);
222         if (format && format->viewer().empty() &&
223             format->isChildFormat())
224                 format = getFormat(format->parentFormat());
225         if (!format || format->viewer().empty()) {
226 // I believe this is the wrong place to show alerts, it should be done by
227 // the caller (this should be "utility" code)
228                 Alert::error(_("Cannot view file"),
229                         bformat(_("No information for viewing %1$s"),
230                                 prettyName(format_name)));
231                 return false;
232         }
233
234         string command = format->viewer();
235
236         if (format_name == "dvi" &&
237             !lyxrc.view_dvi_paper_option.empty()) {
238                 command += ' ' + lyxrc.view_dvi_paper_option;
239                 string paper_size = buffer.params().paperSizeName();
240                 if (paper_size == "letter")
241                         paper_size = "us";
242                 command += ' ' + paper_size;
243                 if (buffer.params().orientation == ORIENTATION_LANDSCAPE)
244                         command += 'r';
245         }
246
247         if (!contains(command, token_from))
248                 command += ' ' + token_from;
249
250         command = subst(command, token_from,
251                         QuoteName(OnlyFilename(filename)));
252         command = subst(command, token_path, QuoteName(OnlyPath(filename)));
253         command = subst(command, token_socket, QuoteName(lyxsocket->address()));
254         lyxerr[Debug::FILES] << "Executing command: " << command << std::endl;
255         buffer.message(_("Executing command: ") + command);
256
257         Path p(OnlyPath(filename));
258         Systemcall one;
259         int const res = one.startscript(Systemcall::DontWait, command);
260
261         if (res) {
262                 Alert::error(_("Cannot view file"),
263                              bformat(_("An error occurred whilst running %1$s"),
264                                MakeDisplayPath(command, 50)));
265                 return false;
266         }
267         return true;
268 }
269
270
271 bool Formats::edit(Buffer const & buffer, string const & filename,
272                          string const & format_name) const
273 {
274         if (filename.empty())
275                 return false;
276
277         Format const * format = getFormat(format_name);
278         if (format && format->editor().empty() &&
279             format->isChildFormat())
280                 format = getFormat(format->parentFormat());
281         if (!format || format->editor().empty()) {
282 // I believe this is the wrong place to show alerts, it should be done by
283 // the caller (this should be "utility" code)
284                 Alert::error(_("Cannot edit file"),
285                         bformat(_("No information for editing %1$s"),
286                                 prettyName(format_name)));
287                 return false;
288         }
289
290         string command = format->editor();
291
292         if (!contains(command, token_from))
293                 command += ' ' + token_from;
294
295         command = subst(command, token_from,
296                         QuoteName(OnlyFilename(filename)));
297         command = subst(command, token_path, QuoteName(OnlyPath(filename)));
298         command = subst(command, token_socket, QuoteName(lyxsocket->address()));
299         lyxerr[Debug::FILES] << "Executing command: " << command << std::endl;
300         buffer.message(_("Executing command: ") + command);
301
302         Path p(OnlyPath(filename));
303         Systemcall one;
304         int const res = one.startscript(Systemcall::DontWait, command);
305
306         if (res) {
307                 Alert::error(_("Cannot edit file"),
308                              bformat(_("An error occurred whilst running %1$s"),
309                                MakeDisplayPath(command, 50)));
310                 return false;
311         }
312         return true;
313 }
314
315
316 string const Formats::prettyName(string const & name) const
317 {
318         Format const * format = getFormat(name);
319         if (format)
320                 return format->prettyname();
321         else
322                 return name;
323 }
324
325
326 string const Formats::extension(string const & name) const
327 {
328         Format const * format = getFormat(name);
329         if (format)
330                 return format->extension();
331         else
332                 return name;
333 }
334
335
336
337
338 Formats formats;
339
340 Formats system_formats;