]> git.lyx.org Git - lyx.git/blob - src/insets/insetexternal.C
Make it compile when USE_BOOST_FORMAT is unset
[lyx.git] / src / insets / insetexternal.C
1 /**
2  * \file insetexternal.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Asger Alstrup Nielsen
7  *
8  * Full author contact details are available in file CREDITS
9  */
10
11 #include <config.h>
12
13 #ifdef __GNUG__
14 #pragma implementation
15 #endif
16
17 #include "insetexternal.h"
18 #include "ExternalTemplate.h"
19 #include "BufferView.h"
20 #include "buffer.h"
21 #include "frontends/LyXView.h"
22 #include "lyx_main.h"
23 #include "LaTeXFeatures.h"
24 #include "gettext.h"
25 #include "debug.h"
26 #include "lyxlex.h"
27
28 #include "frontends/Dialogs.h"
29
30 #include "support/filetools.h"
31 #include "support/lstrings.h"
32 #include "support/path.h"
33 #include "support/systemcall.h"
34 #include "support/FileInfo.h"
35
36 #include <cstdio>
37 #include <utility>
38
39
40 using std::ostream;
41 using std::endl;
42
43
44 InsetExternal::InsetExternal()
45         : view_(0)
46 {
47         tempname_ = lyx::tempName(string(), "lyxext");
48         //ExternalTemplateManager::Templates::const_iterator i1;
49         params_.templ = ExternalTemplateManager::get().getTemplates().begin()->second;
50 }
51
52
53 InsetExternal::~InsetExternal()
54 {
55         lyx::unlink(tempname_);
56         hideDialog();
57 }
58
59
60 InsetExternal::Params InsetExternal::params() const
61 {
62         return params_;
63 }
64
65
66 void InsetExternal::setFromParams(Params const & p)
67 {
68         params_.filename = p.filename;
69         params_.parameters = p.parameters;
70         params_.templ = p.templ;
71 }
72
73
74 string const InsetExternal::editMessage() const
75 {
76         return doSubstitution(0, params_.templ.guiName);
77 }
78
79
80 void InsetExternal::edit(BufferView * bv,
81                          int /*x*/, int /*y*/, mouse_button::state)
82 {
83         view_ = bv;
84         view_->owner()->getDialogs().showExternal(this);
85 }
86
87
88 void InsetExternal::edit(BufferView * bv, bool)
89 {
90         edit(bv, 0, 0, mouse_button::none);
91 }
92
93
94 void InsetExternal::write(Buffer const *, ostream & os) const
95 {
96         os << "External " << params_.templ.lyxName << ",\""
97            << params_.filename << "\",\"" << params_.parameters << "\"\n";
98 }
99
100
101 void InsetExternal::read(Buffer const *, LyXLex & lex)
102 {
103         string format;
104         string token;
105
106         // Read inset data from lex and store in format
107         if (lex.eatLine()) {
108                 format = lex.getString();
109         } else {
110                 lex.printError("InsetExternal: Parse error: `$$Token'");
111         }
112
113         while (lex.isOK()) {
114                 lex.nextToken();
115                 token = lex.getString();
116                 if (token == "\\end_inset")
117                         break;
118         }
119         if (token != "\\end_inset") {
120                 lex.printError("Missing \\end_inset at this point. "
121                                "Read: `$$Token'");
122         }
123
124         // Parse string format...
125         string::size_type const pos1 = format.find(",");
126         params_.templ = ExternalTemplateManager::get().getTemplateByName(format.substr(0, pos1));
127         string::size_type const pos2 = format.find("\",\"", pos1);
128         params_.filename = format.substr(pos1 + 2, pos2 - (pos1 + 2));
129         params_.parameters = format.substr(pos2 + 3, format.length() - (pos2 + 4));
130
131         lyxerr[Debug::INFO] << "InsetExternal::Read: " << params_.templ.lyxName
132                             << " " << params_.filename
133                             << " " << params_.parameters << endl;
134 }
135
136
137 int InsetExternal::write(string const & format,
138                          Buffer const * buf, ostream & os) const
139 {
140         ExternalTemplate const & et = params_.templ;
141         ExternalTemplate::Formats::const_iterator cit =
142                 et.formats.find(format);
143         if (cit == et.formats.end()) {
144                 lyxerr << "External template format '" << format
145                        << "' not specified in template "
146                        << params_.templ.lyxName << endl;
147                 return 0;
148         }
149
150         updateExternal(format, buf);
151         os << doSubstitution(buf, cit->second.product);
152         return 0; // CHECK  (FIXME check what ? - jbl)
153 }
154
155
156 int InsetExternal::latex(Buffer const * buf,
157                          ostream & os, bool, bool) const
158 {
159         return write("LaTeX", buf, os);
160 }
161
162
163 int InsetExternal::ascii(Buffer const * buf, ostream & os, int) const
164 {
165         return write("Ascii", buf, os);
166 }
167
168
169 int InsetExternal::linuxdoc(Buffer const * buf, ostream & os) const
170 {
171         return write("LinuxDoc", buf, os);
172 }
173
174
175 int InsetExternal::docbook(Buffer const * buf, ostream & os, bool) const
176 {
177         return write("DocBook", buf, os);
178 }
179
180
181 void InsetExternal::validate(LaTeXFeatures & features) const
182 {
183         ExternalTemplate const & et = params_.templ;
184         ExternalTemplate::Formats::const_iterator cit =
185                 et.formats.find("LaTeX");
186
187         if (cit == et.formats.end())
188                 return;
189
190         if (!cit->second.requirement.empty()) {
191                 features.require(cit->second.requirement);
192         }
193         if (!cit->second.preamble.empty()) {
194                 features.addExternalPreamble(cit->second.preamble + "\n");
195         }
196 }
197
198
199 Inset * InsetExternal::clone(Buffer const &, bool same_id) const
200 {
201         InsetExternal * inset = new InsetExternal;
202         inset->params_ = params_;
203         inset->view_ = view_;
204         if (same_id)
205                 inset->id_ = id_;
206         return inset;
207 }
208
209
210 string const InsetExternal::getScreenLabel(Buffer const *) const
211 {
212         ExternalTemplate const & et = params_.templ;
213         if (et.guiName.empty())
214                 return _("External");
215         else
216                 return doSubstitution(0, et.guiName);
217 }
218
219
220 void InsetExternal::executeCommand(string const & s,
221                                    Buffer const * buffer) const
222 {
223         Path p(buffer->filePath());
224         Systemcall one;
225         if (lyxerr.debugging()) {
226                 lyxerr << "Executing '" << s << "' in '"
227                        << buffer->filePath() << "'" << endl;
228         }
229         one.startscript(Systemcall::Wait, s);
230 }
231
232
233 string const InsetExternal::doSubstitution(Buffer const * buffer,
234                                            string const & s) const
235 {
236         string result;
237         string const basename = ChangeExtension(params_.filename, string());
238         string filepath;
239         if (buffer && !buffer->tmppath.empty() && !buffer->niceFile) {
240                 filepath = buffer->filePath();
241         }
242         result = subst(s, "$$FName", params_.filename);
243         result = subst(result, "$$Basename", basename);
244         result = subst(result, "$$Parameters", params_.parameters);
245         result = subst(result, "$$FPath", filepath);
246         result = subst(result, "$$Tempname", tempname_);
247         result = subst(result, "$$Sysdir", system_lyxdir);
248
249         // Handle the $$Contents(filename) syntax
250         if (contains(result, "$$Contents(\"")) {
251
252                 string::size_type const pos = result.find("$$Contents(\"");
253                 string::size_type const end = result.find("\")", pos);
254                 string const file = result.substr(pos + 12, end - (pos + 12));
255                 string contents;
256                 if (buffer) {
257                         // Make sure we are in the directory of the buffer
258                         Path p(buffer->filePath());
259                         contents = GetFileContents(file);
260                 } else {
261                         contents = GetFileContents(file);
262                 }
263                 result = subst(result,
264                                ("$$Contents(\"" + file + "\")").c_str(),
265                                contents);
266         }
267
268         return result;
269 }
270
271
272 void InsetExternal::updateExternal() const
273 {
274         updateExternal("LaTeX", view_->buffer());
275 }
276
277 void InsetExternal::updateExternal(string const & format,
278                                    Buffer const * buf) const
279 {
280         ExternalTemplate const & et = params_.templ;
281         ExternalTemplate::Formats::const_iterator cit =
282                 et.formats.find(format);
283
284         if (cit == et.formats.end() ||
285             cit->second.updateCommand.empty() ||
286             !et.automaticProduction)
287                 return;
288
289         if (!cit->second.updateResult.empty()) {
290                 string const resultfile = doSubstitution(buf,
291                                                          cit->second.updateResult);
292                 FileInfo fi(params_.filename);
293                 FileInfo fi2(resultfile);
294                 if (fi2.exist() && fi.exist() &&
295                     difftime(fi2.getModificationTime(),
296                              fi.getModificationTime()) >= 0) {
297                         lyxerr[Debug::FILES] << resultfile
298                                              << " is up to date" << endl;
299                         return;
300                 }
301         }
302
303         executeCommand(doSubstitution(buf, cit->second.updateCommand), buf);
304 }
305
306
307 void InsetExternal::viewExternal() const
308 {
309         ExternalTemplate const & et = params_.templ;
310         if (et.viewCommand.empty())
311                 return;
312
313         updateExternal();
314         executeCommand(doSubstitution(view_->buffer(),
315                                       et.viewCommand),
316                        view_->buffer());
317 }
318
319
320 void InsetExternal::editExternal() const
321 {
322         ExternalTemplate const & et = params_.templ;
323         if (et.editCommand.empty())
324                 return;
325
326         updateExternal();
327         executeCommand(doSubstitution(view_->buffer(),
328                                       et.editCommand),
329                        view_->buffer());
330 }
331
332
333 bool operator==(InsetExternal::Params const & left,
334                 InsetExternal::Params const & right)
335 {
336         return ((left.filename   == right.filename) &&
337                 (left.parameters == right.parameters) &&
338                 (left.templ.lyxName == right.templ.lyxName));
339 }
340
341
342 bool operator!=(InsetExternal::Params const & left,
343                 InsetExternal::Params const & right)
344 {
345         return  !(left == right);
346 }