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