1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2001 The LyX Team.
9 * ====================================================== */
14 #pragma implementation
17 #include "insetexternal.h"
18 #include "ExternalTemplate.h"
19 #include "BufferView.h"
23 #include "LaTeXFeatures.h"
27 #include "frontends/Dialogs.h"
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"
43 InsetExternal::InsetExternal()
46 tempname_ = lyx::tempName(string(), "lyxext");
47 //ExternalTemplateManager::Templates::const_iterator i1;
48 params_.templ = ExternalTemplateManager::get().getTemplates().begin()->second;
52 InsetExternal::~InsetExternal()
54 lyx::unlink(tempname_);
59 InsetExternal::Params InsetExternal::params() const
65 void InsetExternal::setFromParams(Params const & p)
67 params_.filename = p.filename;
68 params_.parameters = p.parameters;
69 params_.templ = p.templ;
73 string const InsetExternal::editMessage() const
75 return doSubstitution(0, params_.templ.guiName);
79 void InsetExternal::edit(BufferView * bv,
80 int /*x*/, int /*y*/, unsigned int /*button*/)
83 view_->owner()->getDialogs()->showExternal(this);
87 void InsetExternal::edit(BufferView * bv, bool)
93 void InsetExternal::write(Buffer const *, ostream & os) const
95 os << "External " << params_.templ.lyxName << ",\""
96 << params_.filename << "\",\"" << params_.parameters << "\"\n";
100 void InsetExternal::read(Buffer const *, LyXLex & lex)
105 // Read inset data from lex and store in format
107 format = lex.getString();
109 lex.printError("InsetExternal: Parse error: `$$Token'");
114 token = lex.getString();
115 if (token == "\\end_inset")
118 if (token != "\\end_inset") {
119 lex.printError("Missing \\end_inset at this point. "
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));
130 lyxerr[Debug::INFO] << "InsetExternal::Read: " << params_.templ.lyxName
131 << " " << params_.filename
132 << " " << params_.parameters << endl;
136 int InsetExternal::write(string const & format,
137 Buffer const * buf, ostream & os) const
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;
149 updateExternal(format, buf);
150 os << doSubstitution(buf, cit->second.product);
151 return 0; // CHECK (FIXME check what ? - jbl)
155 int InsetExternal::latex(Buffer const * buf,
156 ostream & os, bool, bool) const
158 return write("LaTeX", buf, os);
162 int InsetExternal::ascii(Buffer const * buf, ostream & os, int) const
164 return write("Ascii", buf, os);
168 int InsetExternal::linuxdoc(Buffer const * buf, ostream & os) const
170 return write("LinuxDoc", buf, os);
174 int InsetExternal::docbook(Buffer const * buf, ostream & os) const
176 return write("DocBook", buf, os);
180 void InsetExternal::validate(LaTeXFeatures & features) const
182 ExternalTemplate const & et = params_.templ;
183 ExternalTemplate::Formats::const_iterator cit =
184 et.formats.find("LaTeX");
186 if (cit == et.formats.end())
189 if (!cit->second.requirement.empty()) {
190 features.require(cit->second.requirement);
192 if (!cit->second.preamble.empty()) {
193 features.addExternalPreamble(cit->second.preamble + "\n");
198 Inset * InsetExternal::clone(Buffer const &, bool same_id) const
200 InsetExternal * inset = new InsetExternal();
201 inset->params_ = params_;
202 inset->view_ = view_;
209 string const InsetExternal::getScreenLabel(Buffer const *) const
211 ExternalTemplate const & et = params_.templ;
212 if (et.guiName.empty())
213 return _("External");
215 return doSubstitution(0, et.guiName);
219 void InsetExternal::executeCommand(string const & s,
220 Buffer const * buffer) const
222 Path p(buffer->filePath());
224 if (lyxerr.debugging()) {
225 lyxerr << "Executing '" << s << "' in '"
226 << buffer->filePath() << "'" << endl;
228 one.startscript(Systemcall::Wait, s);
232 string const InsetExternal::doSubstitution(Buffer const * buffer,
233 string const & s) const
236 string const basename = ChangeExtension(params_.filename, string());
238 if (buffer && (!buffer->niceFile)) {
239 filepath = buffer->filePath();
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);
249 // Handle the $$Contents(filename) syntax
250 if (contains(result, "$$Contents(\"")) {
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));
257 // Make sure we are in the directory of the buffer
258 Path p(buffer->filePath());
259 contents = GetFileContents(file);
261 contents = GetFileContents(file);
263 result = subst(result,
264 ("$$Contents(\"" + file + "\")").c_str(),
272 void InsetExternal::updateExternal() const
274 updateExternal("LaTeX", view_->buffer());
277 void InsetExternal::updateExternal(string const & format,
278 Buffer const * buf) const
280 ExternalTemplate const & et = params_.templ;
281 ExternalTemplate::Formats::const_iterator cit =
282 et.formats.find(format);
284 if (cit == et.formats.end() ||
285 cit->second.updateCommand.empty() ||
286 !et.automaticProduction)
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;
303 executeCommand(doSubstitution(buf, cit->second.updateCommand), buf);
307 void InsetExternal::viewExternal() const
309 ExternalTemplate const & et = params_.templ;
310 if (et.viewCommand.empty())
314 executeCommand(doSubstitution(view_->buffer(),
320 void InsetExternal::editExternal() const
322 ExternalTemplate const & et = params_.templ;
323 if (et.editCommand.empty())
327 executeCommand(doSubstitution(view_->buffer(),
333 bool operator==(InsetExternal::Params const & left,
334 InsetExternal::Params const & right)
336 return ((left.filename == right.filename) &&
337 (left.parameters == right.parameters) &&
338 (left.templ.lyxName == right.templ.lyxName));
342 bool operator!=(InsetExternal::Params const & left,
343 InsetExternal::Params const & right)
345 return !(left == right);