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