]> git.lyx.org Git - lyx.git/blob - src/format.C
Whitespace, only whitespace.
[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 } //namespace anon
63
64 bool operator<(Format const & a, Format const & b)
65 {
66         // use the compare_ascii_no_case instead of compare_no_case,
67         // because in turkish, 'i' is not the lowercase version of 'I',
68         // and thus turkish locale breaks parsing of tags.
69
70         return compare_ascii_no_case(a.prettyname(), b.prettyname()) < 0;
71 }
72
73 Format::Format(string const & n, string const & e, string const & p,
74                string const & s, string const & v, string const & ed)
75         : name_(n), extension_(e), prettyname_(p), shortcut_(s), viewer_(v), editor_(ed)
76 {}
77
78
79 bool Format::dummy() const
80 {
81         return extension().empty();
82 }
83
84
85 bool Format::isChildFormat() const
86 {
87         if (name_.empty())
88                 return false;
89         return isdigit(name_[name_.length() - 1]);
90 }
91
92
93 string const Format::parentFormat() const
94 {
95         return name_.substr(0, name_.length() - 1);
96 }
97
98
99 // This method should return a reference, and throw an exception
100 // if the format named name cannot be found (Lgb)
101 Format const * Formats::getFormat(string const & name) const
102 {
103         FormatList::const_iterator cit =
104                 find_if(formatlist.begin(), formatlist.end(),
105                         FormatNamesEqual(name));
106         if (cit != formatlist.end())
107                 return &(*cit);
108         else
109                 return 0;
110 }
111
112
113 int Formats::getNumber(string const & name) const
114 {
115         FormatList::const_iterator cit =
116                 find_if(formatlist.begin(), formatlist.end(),
117                         FormatNamesEqual(name));
118         if (cit != formatlist.end())
119                 return distance(formatlist.begin(), cit);
120         else
121                 return -1;
122 }
123
124
125 void Formats::add(string const & name)
126 {
127         if (!getFormat(name))
128                 add(name, name, name, string(), string(), string());
129 }
130
131
132 void Formats::add(string const & name, string const & extension,
133                   string const & prettyname, string const & shortcut,
134                   string const & viewer, string const & editor)
135 {
136         FormatList::iterator it =
137                 find_if(formatlist.begin(), formatlist.end(),
138                         FormatNamesEqual(name));
139         if (it == formatlist.end())
140                 formatlist.push_back(Format(name, extension, prettyname,
141                                             shortcut, viewer, editor));
142         else
143                 *it = Format(name, extension, prettyname, shortcut, viewer, editor);
144 }
145
146
147 void Formats::erase(string const & name)
148 {
149         FormatList::iterator it =
150                 find_if(formatlist.begin(), formatlist.end(),
151                         FormatNamesEqual(name));
152         if (it != formatlist.end())
153                 formatlist.erase(it);
154 }
155
156
157 void Formats::sort()
158 {
159         std::sort(formatlist.begin(), formatlist.end());
160 }
161
162
163 void Formats::setViewer(string const & name, string const & command)
164 {
165         add(name);
166         FormatList::iterator it =
167                 find_if(formatlist.begin(), formatlist.end(),
168                         FormatNamesEqual(name));
169         if (it != formatlist.end())
170                 it->setViewer(command);
171 }
172
173
174 bool Formats::view(Buffer const & buffer, string const & filename,
175                    string const & format_name) const
176 {
177         if (filename.empty())
178                 return false;
179
180         Format const * format = getFormat(format_name);
181         if (format && format->viewer().empty() &&
182             format->isChildFormat())
183                 format = getFormat(format->parentFormat());
184         if (!format || format->viewer().empty()) {
185 // I believe this is the wrong place to show alerts, it should be done by
186 // the caller (this should be "utility" code)
187                 Alert::error(_("Cannot view file"),
188                         bformat(_("No information for viewing %1$s"),
189                                 prettyName(format_name)));
190                 return false;
191         }
192
193         string command = format->viewer();
194
195         if (format_name == "dvi" &&
196             !lyxrc.view_dvi_paper_option.empty()) {
197                 command += ' ' + lyxrc.view_dvi_paper_option;
198                 string paper_size = buffer.params().paperSizeName();
199                 if (paper_size == "letter")
200                         paper_size = "us";
201                 command += ' ' + paper_size;
202                 if (buffer.params().orientation == ORIENTATION_LANDSCAPE)
203                         command += 'r';
204         }
205
206         if (!contains(command, token_from))
207                 command += ' ' + token_from;
208
209         command = subst(command, token_from,
210                         QuoteName(OnlyFilename(filename)));
211         command = subst(command, token_path, QuoteName(OnlyPath(filename)));
212         command = subst(command, token_socket, QuoteName(lyxsocket->address()));
213         lyxerr[Debug::FILES] << "Executing command: " << command << std::endl;
214         buffer.message(_("Executing command: ") + command);
215
216         Path p(OnlyPath(filename));
217         Systemcall one;
218         int const res = one.startscript(Systemcall::DontWait, command);
219
220         if (res) {
221                 Alert::error(_("Cannot view file"),
222                              bformat(_("An error occurred whilst running %1$s"),
223                                MakeDisplayPath(command, 50)));
224                 return false;
225         }
226         return true;
227 }
228
229
230 bool Formats::edit(Buffer const & buffer, string const & filename,
231                          string const & format_name) const
232 {
233         if (filename.empty())
234                 return false;
235
236         Format const * format = getFormat(format_name);
237         if (format && format->editor().empty() &&
238             format->isChildFormat())
239                 format = getFormat(format->parentFormat());
240         if (!format || format->editor().empty()) {
241 // I believe this is the wrong place to show alerts, it should be done by
242 // the caller (this should be "utility" code)
243                 Alert::error(_("Cannot edit file"),
244                         bformat(_("No information for editing %1$s"),
245                                 prettyName(format_name)));
246                 return false;
247         }
248
249         string command = format->editor();
250
251         if (!contains(command, token_from))
252                 command += ' ' + token_from;
253
254         command = subst(command, token_from,
255                         QuoteName(OnlyFilename(filename)));
256         command = subst(command, token_path, QuoteName(OnlyPath(filename)));
257         command = subst(command, token_socket, QuoteName(lyxsocket->address()));
258         lyxerr[Debug::FILES] << "Executing command: " << command << std::endl;
259         buffer.message(_("Executing command: ") + command);
260
261         Path p(OnlyPath(filename));
262         Systemcall one;
263         int const res = one.startscript(Systemcall::DontWait, command);
264
265         if (res) {
266                 Alert::error(_("Cannot edit file"),
267                              bformat(_("An error occurred whilst running %1$s"),
268                                MakeDisplayPath(command, 50)));
269                 return false;
270         }
271         return true;
272 }
273
274
275 string const Formats::prettyName(string const & name) const
276 {
277         Format const * format = getFormat(name);
278         if (format)
279                 return format->prettyname();
280         else
281                 return name;
282 }
283
284
285 string const Formats::extension(string const & name) const
286 {
287         Format const * format = getFormat(name);
288         if (format)
289                 return format->extension();
290         else
291                 return name;
292 }
293
294
295
296
297 Formats formats;
298
299 Formats system_formats;