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