2 * \file insetexternal.C
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Asger Alstrup Nielsen
8 * Full author contact details are available in file CREDITS
14 #pragma implementation
17 #include "insetexternal.h"
18 #include "ExternalTemplate.h"
19 #include "BufferView.h"
21 #include "frontends/LyXView.h"
23 #include "LaTeXFeatures.h"
28 #include "frontends/Dialogs.h"
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"
44 InsetExternal::InsetExternal()
47 tempname_ = lyx::tempName(string(), "lyxext");
48 //ExternalTemplateManager::Templates::const_iterator i1;
49 params_.templ = ExternalTemplateManager::get().getTemplates().begin()->second;
53 InsetExternal::~InsetExternal()
55 lyx::unlink(tempname_);
60 InsetExternal::Params InsetExternal::params() const
66 void InsetExternal::setFromParams(Params const & p)
68 params_.filename = p.filename;
69 params_.parameters = p.parameters;
70 params_.templ = p.templ;
74 string const InsetExternal::editMessage() const
76 return doSubstitution(0, params_.templ.guiName);
80 void InsetExternal::edit(BufferView * bv,
81 int /*x*/, int /*y*/, mouse_button::state)
84 view_->owner()->getDialogs().showExternal(this);
88 void InsetExternal::edit(BufferView * bv, bool)
90 edit(bv, 0, 0, mouse_button::none);
94 void InsetExternal::write(Buffer const *, ostream & os) const
96 os << "External " << params_.templ.lyxName << ",\""
97 << params_.filename << "\",\"" << params_.parameters << "\"\n";
101 void InsetExternal::read(Buffer const *, LyXLex & lex)
106 // Read inset data from lex and store in format
108 format = lex.getString();
110 lex.printError("InsetExternal: Parse error: `$$Token'");
115 token = lex.getString();
116 if (token == "\\end_inset")
119 if (token != "\\end_inset") {
120 lex.printError("Missing \\end_inset at this point. "
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));
131 lyxerr[Debug::INFO] << "InsetExternal::Read: " << params_.templ.lyxName
132 << ' ' << params_.filename
133 << ' ' << params_.parameters << endl;
137 int InsetExternal::write(string const & format,
138 Buffer const * buf, ostream & os) const
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;
150 updateExternal(format, buf);
151 os << doSubstitution(buf, cit->second.product);
152 return 0; // CHECK (FIXME check what ? - jbl)
156 int InsetExternal::latex(Buffer const * buf,
157 ostream & os, bool, bool) const
159 return write("LaTeX", buf, os);
163 int InsetExternal::ascii(Buffer const * buf, ostream & os, int) const
165 return write("Ascii", buf, os);
169 int InsetExternal::linuxdoc(Buffer const * buf, ostream & os) const
171 return write("LinuxDoc", buf, os);
175 int InsetExternal::docbook(Buffer const * buf, ostream & os, bool) const
177 return write("DocBook", buf, os);
181 void InsetExternal::validate(LaTeXFeatures & features) const
183 ExternalTemplate const & et = params_.templ;
184 ExternalTemplate::Formats::const_iterator cit =
185 et.formats.find("LaTeX");
187 if (cit == et.formats.end())
190 if (!cit->second.requirement.empty()) {
191 features.require(cit->second.requirement);
193 if (!cit->second.preamble.empty()) {
194 features.addExternalPreamble(cit->second.preamble + "\n");
199 Inset * InsetExternal::clone(Buffer const &, bool same_id) const
201 InsetExternal * inset = new InsetExternal;
202 inset->params_ = params_;
203 inset->view_ = view_;
210 string const InsetExternal::getScreenLabel(Buffer const *) const
212 ExternalTemplate const & et = params_.templ;
213 if (et.guiName.empty())
214 return _("External");
216 return doSubstitution(0, et.guiName);
220 void InsetExternal::executeCommand(string const & s,
221 Buffer const * buffer) const
223 Path p(buffer->filePath());
225 if (lyxerr.debugging()) {
226 lyxerr << "Executing '" << s << "' in '"
227 << buffer->filePath() << '\'' << endl;
229 one.startscript(Systemcall::Wait, s);
233 string const InsetExternal::doSubstitution(Buffer const * buffer,
234 string const & s) const
237 string const basename = ChangeExtension(params_.filename, string());
239 if (buffer && !buffer->tmppath.empty() && !buffer->niceFile) {
240 filepath = buffer->filePath();
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 = 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);