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
13 #include "insetexternal.h"
14 #include "ExternalTemplate.h"
15 #include "BufferView.h"
17 #include "funcrequest.h"
19 #include "LaTeXFeatures.h"
25 #include "frontends/LyXView.h"
26 #include "frontends/Dialogs.h"
28 #include "support/filetools.h"
29 #include "support/lstrings.h"
30 #include "support/path.h"
31 #include "support/systemcall.h"
32 #include "support/FileInfo.h"
42 InsetExternal::InsetExternal()
45 tempname_ = lyx::tempName(string(), "lyxext");
46 //ExternalTemplateManager::Templates::const_iterator i1;
47 params_.templ = ExternalTemplateManager::get().getTemplates().begin()->second;
51 InsetExternal::~InsetExternal()
53 lyx::unlink(tempname_);
54 InsetExternalMailer mailer(*this);
59 InsetExternal::Params const & InsetExternal::params() const
65 dispatch_result InsetExternal::localDispatch(FuncRequest const & cmd)
67 dispatch_result result = UNDISPATCHED;
70 case LFUN_INSET_MODIFY: {
71 InsetExternal::Params p;
72 InsetExternalMailer::string2params(cmd.argument, p);
73 if (p.filename.empty())
77 cmd.view()->updateInset(this);
82 case LFUN_INSET_DIALOG_UPDATE: {
83 InsetExternalMailer mailer(*this);
84 mailer.updateDialog(cmd.view());
88 case LFUN_MOUSE_RELEASE:
89 edit(cmd.view(), cmd.x, cmd.y, cmd.button());
101 void InsetExternal::setFromParams(Params const & p)
103 params_.filename = p.filename;
104 params_.parameters = p.parameters;
105 params_.templ = p.templ;
109 string const InsetExternal::editMessage() const
111 return doSubstitution(0, params_.templ.guiName);
115 void InsetExternal::edit(BufferView * bv, int, int, mouse_button::state)
117 InsetExternalMailer mailer(*this);
118 mailer.showDialog(bv);
122 void InsetExternal::edit(BufferView * bv, bool)
124 edit(bv, 0, 0, mouse_button::none);
128 void InsetExternal::write(Buffer const *, ostream & os) const
130 os << "External " << params_.templ.lyxName << ",\""
131 << params_.filename << "\",\"" << params_.parameters << "\"\n";
135 void InsetExternal::read(Buffer const *, LyXLex & lex)
140 // Read inset data from lex and store in format
142 format = lex.getString();
144 lex.printError("InsetExternal: Parse error: `$$Token'");
149 token = lex.getString();
150 if (token == "\\end_inset")
153 if (token != "\\end_inset") {
154 lex.printError("Missing \\end_inset at this point. "
158 // Parse string format...
159 string::size_type const pos1 = format.find(',');
160 params_.templ = ExternalTemplateManager::get().getTemplateByName(format.substr(0, pos1));
161 string::size_type const pos2 = format.find("\",\"", pos1);
162 params_.filename = format.substr(pos1 + 2, pos2 - (pos1 + 2));
163 params_.parameters = format.substr(pos2 + 3, format.length() - (pos2 + 4));
165 lyxerr[Debug::INFO] << "InsetExternal::Read: " << params_.templ.lyxName
166 << ' ' << params_.filename
167 << ' ' << params_.parameters << endl;
171 int InsetExternal::write(string const & format,
172 Buffer const * buf, ostream & os) const
174 ExternalTemplate const & et = params_.templ;
175 ExternalTemplate::Formats::const_iterator cit =
176 et.formats.find(format);
177 if (cit == et.formats.end()) {
178 lyxerr << "External template format '" << format
179 << "' not specified in template "
180 << params_.templ.lyxName << endl;
184 updateExternal(format, buf);
185 os << doSubstitution(buf, cit->second.product);
186 return 0; // CHECK (FIXME check what ? - jbl)
190 int InsetExternal::latex(Buffer const * buf,
191 ostream & os, bool, bool) const
193 return write("LaTeX", buf, os);
197 int InsetExternal::ascii(Buffer const * buf, ostream & os, int) const
199 return write("Ascii", buf, os);
203 int InsetExternal::linuxdoc(Buffer const * buf, ostream & os) const
205 return write("LinuxDoc", buf, os);
209 int InsetExternal::docbook(Buffer const * buf, ostream & os, bool) const
211 return write("DocBook", buf, os);
215 void InsetExternal::validate(LaTeXFeatures & features) const
217 ExternalTemplate const & et = params_.templ;
218 ExternalTemplate::Formats::const_iterator cit =
219 et.formats.find("LaTeX");
221 if (cit == et.formats.end())
224 if (!cit->second.requirement.empty()) {
225 features.require(cit->second.requirement);
227 if (!cit->second.preamble.empty()) {
228 features.addExternalPreamble(cit->second.preamble + "\n");
233 Inset * InsetExternal::clone(Buffer const &, bool same_id) const
235 InsetExternal * inset = new InsetExternal;
236 inset->params_ = params_;
237 inset->view_ = view_;
244 string const InsetExternal::getScreenLabel(Buffer const *) const
246 ExternalTemplate const & et = params_.templ;
247 if (et.guiName.empty())
248 return _("External");
250 return doSubstitution(0, et.guiName);
254 void InsetExternal::executeCommand(string const & s,
255 Buffer const * buffer) const
257 Path p(buffer->filePath());
259 if (lyxerr.debugging()) {
260 lyxerr << "Executing '" << s << "' in '"
261 << buffer->filePath() << '\'' << endl;
263 one.startscript(Systemcall::Wait, s);
267 string const InsetExternal::doSubstitution(Buffer const * buffer,
268 string const & s) const
271 string const basename = ChangeExtension(params_.filename, string());
273 if (buffer && !buffer->tmppath.empty() && !buffer->niceFile) {
274 filepath = buffer->filePath();
276 result = subst(s, "$$FName", params_.filename);
277 result = subst(result, "$$Basename", basename);
278 result = subst(result, "$$Parameters", params_.parameters);
279 result = subst(result, "$$FPath", filepath);
280 result = subst(result, "$$Tempname", tempname_);
281 result = subst(result, "$$Sysdir", system_lyxdir);
283 // Handle the $$Contents(filename) syntax
284 if (contains(result, "$$Contents(\"")) {
286 string::size_type const pos = result.find("$$Contents(\"");
287 string::size_type const end = result.find("\")", pos);
288 string const file = result.substr(pos + 12, end - (pos + 12));
291 // Make sure we are in the directory of the buffer
292 Path p(buffer->filePath());
293 contents = GetFileContents(file);
295 contents = GetFileContents(file);
297 result = subst(result,
298 ("$$Contents(\"" + file + "\")").c_str(),
306 void InsetExternal::updateExternal() const
308 updateExternal("LaTeX", view_->buffer());
311 void InsetExternal::updateExternal(string const & format,
312 Buffer const * buf) const
314 ExternalTemplate const & et = params_.templ;
315 ExternalTemplate::Formats::const_iterator cit =
316 et.formats.find(format);
318 if (cit == et.formats.end() ||
319 cit->second.updateCommand.empty() ||
320 !et.automaticProduction)
323 if (!cit->second.updateResult.empty()) {
324 string const resultfile = doSubstitution(buf,
325 cit->second.updateResult);
326 FileInfo fi(params_.filename);
327 FileInfo fi2(resultfile);
328 if (fi2.exist() && fi.exist() &&
329 difftime(fi2.getModificationTime(),
330 fi.getModificationTime()) >= 0) {
331 lyxerr[Debug::FILES] << resultfile
332 << " is up to date" << endl;
337 executeCommand(doSubstitution(buf, cit->second.updateCommand), buf);
341 void InsetExternal::viewExternal() const
343 ExternalTemplate const & et = params_.templ;
344 if (et.viewCommand.empty())
348 executeCommand(doSubstitution(view_->buffer(),
354 void InsetExternal::editExternal() const
356 ExternalTemplate const & et = params_.templ;
357 if (et.editCommand.empty())
361 executeCommand(doSubstitution(view_->buffer(),
367 string const InsetExternalMailer::name_("external");
369 InsetExternalMailer::InsetExternalMailer(InsetExternal & inset)
374 string const InsetExternalMailer::inset2string() const
376 return params2string(inset_.params());
380 void InsetExternalMailer::string2params(string const & in,
381 InsetExternal::Params & params)
383 params = InsetExternal::Params();
388 istringstream data(STRCONV(in));
394 string const token = lex.getString();
399 // This is part of the inset proper that is usually swallowed
400 // by Buffer::readInset
403 string const token = lex.getString();
404 if (token != "External")
411 params = inset.params();
417 InsetExternalMailer::params2string(InsetExternal::Params const & params)
420 inset.setFromParams(params);
422 data << name_ << ' ';
423 inset.write(0, data);
424 data << "\\end_inset\n";
425 return STRCONV(data.str());