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 #include "insetexternal.h"
15 #include "ExternalTemplate.h"
16 #include "BufferView.h"
18 #include "frontends/LyXView.h"
20 #include "LaTeXFeatures.h"
25 #include "frontends/Dialogs.h"
27 #include "support/filetools.h"
28 #include "support/lstrings.h"
29 #include "support/path.h"
30 #include "support/systemcall.h"
31 #include "support/FileInfo.h"
41 InsetExternal::InsetExternal()
44 tempname_ = lyx::tempName(string(), "lyxext");
45 //ExternalTemplateManager::Templates::const_iterator i1;
46 params_.templ = ExternalTemplateManager::get().getTemplates().begin()->second;
50 InsetExternal::~InsetExternal()
52 lyx::unlink(tempname_);
57 InsetExternal::Params InsetExternal::params() const
63 void InsetExternal::setFromParams(Params const & p)
65 params_.filename = p.filename;
66 params_.parameters = p.parameters;
67 params_.templ = p.templ;
71 string const InsetExternal::editMessage() const
73 return doSubstitution(0, params_.templ.guiName);
77 void InsetExternal::edit(BufferView * bv,
78 int /*x*/, int /*y*/, mouse_button::state)
81 view_->owner()->getDialogs().showExternal(this);
85 void InsetExternal::edit(BufferView * bv, bool)
87 edit(bv, 0, 0, mouse_button::none);
91 void InsetExternal::write(Buffer const *, ostream & os) const
93 os << "External " << params_.templ.lyxName << ",\""
94 << params_.filename << "\",\"" << params_.parameters << "\"\n";
98 void InsetExternal::read(Buffer const *, LyXLex & lex)
103 // Read inset data from lex and store in format
105 format = lex.getString();
107 lex.printError("InsetExternal: Parse error: `$$Token'");
112 token = lex.getString();
113 if (token == "\\end_inset")
116 if (token != "\\end_inset") {
117 lex.printError("Missing \\end_inset at this point. "
121 // Parse string format...
122 string::size_type const pos1 = format.find(',');
123 params_.templ = ExternalTemplateManager::get().getTemplateByName(format.substr(0, pos1));
124 string::size_type const pos2 = format.find("\",\"", pos1);
125 params_.filename = format.substr(pos1 + 2, pos2 - (pos1 + 2));
126 params_.parameters = format.substr(pos2 + 3, format.length() - (pos2 + 4));
128 lyxerr[Debug::INFO] << "InsetExternal::Read: " << params_.templ.lyxName
129 << ' ' << params_.filename
130 << ' ' << params_.parameters << endl;
134 int InsetExternal::write(string const & format,
135 Buffer const * buf, ostream & os) const
137 ExternalTemplate const & et = params_.templ;
138 ExternalTemplate::Formats::const_iterator cit =
139 et.formats.find(format);
140 if (cit == et.formats.end()) {
141 lyxerr << "External template format '" << format
142 << "' not specified in template "
143 << params_.templ.lyxName << endl;
147 updateExternal(format, buf);
148 os << doSubstitution(buf, cit->second.product);
149 return 0; // CHECK (FIXME check what ? - jbl)
153 int InsetExternal::latex(Buffer const * buf,
154 ostream & os, bool, bool) const
156 return write("LaTeX", buf, os);
160 int InsetExternal::ascii(Buffer const * buf, ostream & os, int) const
162 return write("Ascii", buf, os);
166 int InsetExternal::linuxdoc(Buffer const * buf, ostream & os) const
168 return write("LinuxDoc", buf, os);
172 int InsetExternal::docbook(Buffer const * buf, ostream & os, bool) const
174 return write("DocBook", buf, os);
178 void InsetExternal::validate(LaTeXFeatures & features) const
180 ExternalTemplate const & et = params_.templ;
181 ExternalTemplate::Formats::const_iterator cit =
182 et.formats.find("LaTeX");
184 if (cit == et.formats.end())
187 if (!cit->second.requirement.empty()) {
188 features.require(cit->second.requirement);
190 if (!cit->second.preamble.empty()) {
191 features.addExternalPreamble(cit->second.preamble + "\n");
196 Inset * InsetExternal::clone(Buffer const &, bool same_id) const
198 InsetExternal * inset = new InsetExternal;
199 inset->params_ = params_;
200 inset->view_ = view_;
207 string const InsetExternal::getScreenLabel(Buffer const *) const
209 ExternalTemplate const & et = params_.templ;
210 if (et.guiName.empty())
211 return _("External");
213 return doSubstitution(0, et.guiName);
217 void InsetExternal::executeCommand(string const & s,
218 Buffer const * buffer) const
220 Path p(buffer->filePath());
222 if (lyxerr.debugging()) {
223 lyxerr << "Executing '" << s << "' in '"
224 << buffer->filePath() << '\'' << endl;
226 one.startscript(Systemcall::Wait, s);
230 string const InsetExternal::doSubstitution(Buffer const * buffer,
231 string const & s) const
234 string const basename = ChangeExtension(params_.filename, string());
236 if (buffer && !buffer->tmppath.empty() && !buffer->niceFile) {
237 filepath = buffer->filePath();
239 result = subst(s, "$$FName", params_.filename);
240 result = subst(result, "$$Basename", basename);
241 result = subst(result, "$$Parameters", params_.parameters);
242 result = subst(result, "$$FPath", filepath);
243 result = subst(result, "$$Tempname", tempname_);
244 result = subst(result, "$$Sysdir", system_lyxdir);
246 // Handle the $$Contents(filename) syntax
247 if (contains(result, "$$Contents(\"")) {
249 string::size_type const pos = result.find("$$Contents(\"");
250 string::size_type const end = result.find("\")", pos);
251 string const file = result.substr(pos + 12, end - (pos + 12));
254 // Make sure we are in the directory of the buffer
255 Path p(buffer->filePath());
256 contents = GetFileContents(file);
258 contents = GetFileContents(file);
260 result = subst(result,
261 ("$$Contents(\"" + file + "\")").c_str(),
269 void InsetExternal::updateExternal() const
271 updateExternal("LaTeX", view_->buffer());
274 void InsetExternal::updateExternal(string const & format,
275 Buffer const * buf) const
277 ExternalTemplate const & et = params_.templ;
278 ExternalTemplate::Formats::const_iterator cit =
279 et.formats.find(format);
281 if (cit == et.formats.end() ||
282 cit->second.updateCommand.empty() ||
283 !et.automaticProduction)
286 if (!cit->second.updateResult.empty()) {
287 string const resultfile = doSubstitution(buf,
288 cit->second.updateResult);
289 FileInfo fi(params_.filename);
290 FileInfo fi2(resultfile);
291 if (fi2.exist() && fi.exist() &&
292 difftime(fi2.getModificationTime(),
293 fi.getModificationTime()) >= 0) {
294 lyxerr[Debug::FILES] << resultfile
295 << " is up to date" << endl;
300 executeCommand(doSubstitution(buf, cit->second.updateCommand), buf);
304 void InsetExternal::viewExternal() const
306 ExternalTemplate const & et = params_.templ;
307 if (et.viewCommand.empty())
311 executeCommand(doSubstitution(view_->buffer(),
317 void InsetExternal::editExternal() const
319 ExternalTemplate const & et = params_.templ;
320 if (et.editCommand.empty())
324 executeCommand(doSubstitution(view_->buffer(),
330 bool operator==(InsetExternal::Params const & left,
331 InsetExternal::Params const & right)
333 return ((left.filename == right.filename) &&
334 (left.parameters == right.parameters) &&
335 (left.templ.lyxName == right.templ.lyxName));
339 bool operator!=(InsetExternal::Params const & left,
340 InsetExternal::Params const & right)
342 return !(left == right);