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