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