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