]> git.lyx.org Git - lyx.git/blob - src/insets/insetexternal.C
02e94a059df18041c1372505ac77f5ef816ca837
[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
14 #include "insetexternal.h"
15 #include "ExternalTemplate.h"
16 #include "BufferView.h"
17 #include "buffer.h"
18 #include "funcrequest.h"
19 #include "lyx_main.h"
20 #include "LaTeXFeatures.h"
21 #include "gettext.h"
22 #include "debug.h"
23 #include "lyxlex.h"
24
25 #include "frontends/LyXView.h"
26 #include "frontends/Dialogs.h"
27
28 #include "support/filetools.h"
29 #include "support/lstrings.h"
30 #include "support/path.h"
31 #include "support/systemcall.h"
32 #include "support/FileInfo.h"
33
34 #include <cstdio>
35 #include <utility>
36
37
38 using std::ostream;
39 using std::endl;
40
41
42 InsetExternal::InsetExternal()
43         : view_(0)
44 {
45         tempname_ = lyx::tempName(string(), "lyxext");
46         //ExternalTemplateManager::Templates::const_iterator i1;
47         params_.templ = ExternalTemplateManager::get().getTemplates().begin()->second;
48 }
49
50
51 InsetExternal::~InsetExternal()
52 {
53         lyx::unlink(tempname_);
54         InsetExternalMailer mailer(*this);
55         mailer.hideDialog();
56 }
57
58
59 InsetExternal::Params const & InsetExternal::params() const
60 {
61         return params_;
62 }
63
64
65 dispatch_result InsetExternal::localDispatch(FuncRequest const & cmd)
66 {
67         if (cmd.action != LFUN_INSET_MODIFY)
68                 return UNDISPATCHED;
69
70         InsetExternal::Params p;
71         InsetExternalMailer::string2params(cmd.argument, p);
72         if (p.filename.empty())
73                 return UNDISPATCHED;
74
75         setFromParams(p);
76         cmd.view()->updateInset(this, true);
77         return DISPATCHED;
78 }
79
80
81 void InsetExternal::setFromParams(Params const & p)
82 {
83         params_.filename = p.filename;
84         params_.parameters = p.parameters;
85         params_.templ = p.templ;
86 }
87
88
89 string const InsetExternal::editMessage() const
90 {
91         return doSubstitution(0, params_.templ.guiName);
92 }
93
94
95 void InsetExternal::edit(BufferView *, int, int, mouse_button::state)
96 {
97         InsetExternalMailer mailer(*this);
98         mailer.showDialog();
99 }
100
101
102 void InsetExternal::edit(BufferView * bv, bool)
103 {
104         edit(bv, 0, 0, mouse_button::none);
105 }
106
107
108 void InsetExternal::write(Buffer const *, ostream & os) const
109 {
110         os << "External " << params_.templ.lyxName << ",\""
111            << params_.filename << "\",\"" << params_.parameters << "\"\n";
112 }
113
114
115 void InsetExternal::read(Buffer const *, LyXLex & lex)
116 {
117         string format;
118         string token;
119
120         // Read inset data from lex and store in format
121         if (lex.eatLine()) {
122                 format = lex.getString();
123         } else {
124                 lex.printError("InsetExternal: Parse error: `$$Token'");
125         }
126
127         while (lex.isOK()) {
128                 lex.nextToken();
129                 token = lex.getString();
130                 if (token == "\\end_inset")
131                         break;
132         }
133         if (token != "\\end_inset") {
134                 lex.printError("Missing \\end_inset at this point. "
135                                "Read: `$$Token'");
136         }
137
138         // Parse string format...
139         string::size_type const pos1 = format.find(',');
140         params_.templ = ExternalTemplateManager::get().getTemplateByName(format.substr(0, pos1));
141         string::size_type const pos2 = format.find("\",\"", pos1);
142         params_.filename = format.substr(pos1 + 2, pos2 - (pos1 + 2));
143         params_.parameters = format.substr(pos2 + 3, format.length() - (pos2 + 4));
144
145         lyxerr[Debug::INFO] << "InsetExternal::Read: " << params_.templ.lyxName
146                             << ' ' << params_.filename
147                             << ' ' << params_.parameters << endl;
148 }
149
150
151 int InsetExternal::write(string const & format,
152                          Buffer const * buf, ostream & os) const
153 {
154         ExternalTemplate const & et = params_.templ;
155         ExternalTemplate::Formats::const_iterator cit =
156                 et.formats.find(format);
157         if (cit == et.formats.end()) {
158                 lyxerr << "External template format '" << format
159                        << "' not specified in template "
160                        << params_.templ.lyxName << endl;
161                 return 0;
162         }
163
164         updateExternal(format, buf);
165         os << doSubstitution(buf, cit->second.product);
166         return 0; // CHECK  (FIXME check what ? - jbl)
167 }
168
169
170 int InsetExternal::latex(Buffer const * buf,
171                          ostream & os, bool, bool) const
172 {
173         return write("LaTeX", buf, os);
174 }
175
176
177 int InsetExternal::ascii(Buffer const * buf, ostream & os, int) const
178 {
179         return write("Ascii", buf, os);
180 }
181
182
183 int InsetExternal::linuxdoc(Buffer const * buf, ostream & os) const
184 {
185         return write("LinuxDoc", buf, os);
186 }
187
188
189 int InsetExternal::docbook(Buffer const * buf, ostream & os, bool) const
190 {
191         return write("DocBook", buf, os);
192 }
193
194
195 void InsetExternal::validate(LaTeXFeatures & features) const
196 {
197         ExternalTemplate const & et = params_.templ;
198         ExternalTemplate::Formats::const_iterator cit =
199                 et.formats.find("LaTeX");
200
201         if (cit == et.formats.end())
202                 return;
203
204         if (!cit->second.requirement.empty()) {
205                 features.require(cit->second.requirement);
206         }
207         if (!cit->second.preamble.empty()) {
208                 features.addExternalPreamble(cit->second.preamble + "\n");
209         }
210 }
211
212
213 Inset * InsetExternal::clone(Buffer const &, bool same_id) const
214 {
215         InsetExternal * inset = new InsetExternal;
216         inset->params_ = params_;
217         inset->view_ = view_;
218         if (same_id)
219                 inset->id_ = id_;
220         return inset;
221 }
222
223
224 string const InsetExternal::getScreenLabel(Buffer const *) const
225 {
226         ExternalTemplate const & et = params_.templ;
227         if (et.guiName.empty())
228                 return _("External");
229         else
230                 return doSubstitution(0, et.guiName);
231 }
232
233
234 void InsetExternal::executeCommand(string const & s,
235                                    Buffer const * buffer) const
236 {
237         Path p(buffer->filePath());
238         Systemcall one;
239         if (lyxerr.debugging()) {
240                 lyxerr << "Executing '" << s << "' in '"
241                        << buffer->filePath() << '\'' << endl;
242         }
243         one.startscript(Systemcall::Wait, s);
244 }
245
246
247 string const InsetExternal::doSubstitution(Buffer const * buffer,
248                                            string const & s) const
249 {
250         string result;
251         string const basename = ChangeExtension(params_.filename, string());
252         string filepath;
253         if (buffer && !buffer->tmppath.empty() && !buffer->niceFile) {
254                 filepath = buffer->filePath();
255         }
256         result = subst(s, "$$FName", params_.filename);
257         result = subst(result, "$$Basename", basename);
258         result = subst(result, "$$Parameters", params_.parameters);
259         result = subst(result, "$$FPath", filepath);
260         result = subst(result, "$$Tempname", tempname_);
261         result = subst(result, "$$Sysdir", system_lyxdir);
262
263         // Handle the $$Contents(filename) syntax
264         if (contains(result, "$$Contents(\"")) {
265
266                 string::size_type const pos = result.find("$$Contents(\"");
267                 string::size_type const end = result.find("\")", pos);
268                 string const file = result.substr(pos + 12, end - (pos + 12));
269                 string contents;
270                 if (buffer) {
271                         // Make sure we are in the directory of the buffer
272                         Path p(buffer->filePath());
273                         contents = GetFileContents(file);
274                 } else {
275                         contents = GetFileContents(file);
276                 }
277                 result = subst(result,
278                                ("$$Contents(\"" + file + "\")").c_str(),
279                                contents);
280         }
281
282         return result;
283 }
284
285
286 void InsetExternal::updateExternal() const
287 {
288         updateExternal("LaTeX", view_->buffer());
289 }
290
291 void InsetExternal::updateExternal(string const & format,
292                                    Buffer const * buf) const
293 {
294         ExternalTemplate const & et = params_.templ;
295         ExternalTemplate::Formats::const_iterator cit =
296                 et.formats.find(format);
297
298         if (cit == et.formats.end() ||
299             cit->second.updateCommand.empty() ||
300             !et.automaticProduction)
301                 return;
302
303         if (!cit->second.updateResult.empty()) {
304                 string const resultfile = doSubstitution(buf,
305                                                          cit->second.updateResult);
306                 FileInfo fi(params_.filename);
307                 FileInfo fi2(resultfile);
308                 if (fi2.exist() && fi.exist() &&
309                     difftime(fi2.getModificationTime(),
310                              fi.getModificationTime()) >= 0) {
311                         lyxerr[Debug::FILES] << resultfile
312                                              << " is up to date" << endl;
313                         return;
314                 }
315         }
316
317         executeCommand(doSubstitution(buf, cit->second.updateCommand), buf);
318 }
319
320
321 void InsetExternal::viewExternal() const
322 {
323         ExternalTemplate const & et = params_.templ;
324         if (et.viewCommand.empty())
325                 return;
326
327         updateExternal();
328         executeCommand(doSubstitution(view_->buffer(),
329                                       et.viewCommand),
330                        view_->buffer());
331 }
332
333
334 void InsetExternal::editExternal() const
335 {
336         ExternalTemplate const & et = params_.templ;
337         if (et.editCommand.empty())
338                 return;
339
340         updateExternal();
341         executeCommand(doSubstitution(view_->buffer(),
342                                       et.editCommand),
343                        view_->buffer());
344 }
345
346
347 bool operator==(InsetExternal::Params const & left,
348                 InsetExternal::Params const & right)
349 {
350         return ((left.filename   == right.filename) &&
351                 (left.parameters == right.parameters) &&
352                 (left.templ.lyxName == right.templ.lyxName));
353 }
354
355
356 bool operator!=(InsetExternal::Params const & left,
357                 InsetExternal::Params const & right)
358 {
359         return  !(left == right);
360 }
361
362
363 string const InsetExternalMailer::name_("external");
364
365 InsetExternalMailer::InsetExternalMailer(InsetExternal & inset)
366         : inset_(inset)
367 {}
368
369
370 string const InsetExternalMailer::inset2string() const
371 {
372         return params2string(inset_.params());
373 }
374
375
376 void InsetExternalMailer::string2params(string const & in,
377                                         InsetExternal::Params & params)
378 {
379         params = InsetExternal::Params();
380
381         istringstream data(in);
382         LyXLex lex(0,0);
383         lex.setStream(data);
384
385         if (lex.isOK()) {
386                 lex.next();
387                 string const token = lex.getString();
388                 if (token != name_)
389                         return;
390         }
391
392         // This is part of the inset proper that is usually swallowed
393         // by Buffer::readInset
394         if (lex.isOK()) {
395                 lex.next();
396                 string const token = lex.getString();
397                 if (token != "External")
398                         return;
399         }
400
401         InsetExternal inset;    
402         inset.read(0, lex);
403         params = inset.params();
404 }
405
406
407 string const
408 InsetExternalMailer::params2string(InsetExternal::Params const & params)
409 {
410         InsetExternal inset;
411         inset.setFromParams(params);
412         ostringstream data;
413         data << name_ << ' ';
414         inset.write(0, data);
415         data << "\\end_inset\n";
416
417         return data.str();
418 }