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