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