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"
16 #include "BufferView.h"
18 #include "ExternalTemplate.h"
19 #include "funcrequest.h"
21 #include "LaTeXFeatures.h"
22 #include "latexrunparams.h"
27 #include "frontends/LyXView.h"
28 #include "frontends/Dialogs.h"
30 #include "support/filetools.h"
31 #include "support/lstrings.h"
32 #include "support/lyxalgo.h"
33 #include "support/path.h"
34 #include "support/systemcall.h"
35 #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_);
56 InsetExternalMailer mailer(*this);
61 InsetExternal::Params const & InsetExternal::params() const
67 dispatch_result InsetExternal::localDispatch(FuncRequest const & cmd)
71 case LFUN_INSET_MODIFY: {
72 InsetExternal::Params p;
73 InsetExternalMailer::string2params(cmd.argument, p);
74 if (!p.filename.empty()) {
76 cmd.view()->updateInset(this);
81 case LFUN_INSET_DIALOG_UPDATE:
82 InsetExternalMailer(*this).updateDialog(cmd.view());
85 case LFUN_MOUSE_RELEASE:
87 InsetExternalMailer(*this).showDialog(cmd.view());
96 void InsetExternal::setFromParams(Params const & p)
98 params_.filename = p.filename;
99 params_.parameters = p.parameters;
100 params_.templ = p.templ;
104 string const InsetExternal::editMessage() const
106 return doSubstitution(0, params_.templ.guiName);
110 void InsetExternal::write(Buffer const *, ostream & os) const
112 os << "External " << params_.templ.lyxName << ",\""
113 << params_.filename << "\",\"" << params_.parameters << "\"\n";
117 void InsetExternal::read(Buffer const *, LyXLex & lex)
122 // Read inset data from lex and store in format
124 format = lex.getString();
126 lex.printError("InsetExternal: Parse error: `$$Token'");
131 token = lex.getString();
132 if (token == "\\end_inset")
135 if (token != "\\end_inset") {
136 lex.printError("Missing \\end_inset at this point. "
140 // Parse string format...
141 string::size_type const pos1 = format.find(',');
142 params_.templ = ExternalTemplateManager::get().getTemplateByName(format.substr(0, pos1));
143 string::size_type const pos2 = format.find("\",\"", pos1);
144 params_.filename = format.substr(pos1 + 2, pos2 - (pos1 + 2));
145 params_.parameters = format.substr(pos2 + 3, format.length() - (pos2 + 4));
147 lyxerr[Debug::INFO] << "InsetExternal::Read: " << params_.templ.lyxName
148 << ' ' << params_.filename
149 << ' ' << params_.parameters << endl;
153 int InsetExternal::write(string const & format,
154 Buffer const * buf, ostream & os) const
156 ExternalTemplate const & et = params_.templ;
157 ExternalTemplate::Formats::const_iterator cit =
158 et.formats.find(format);
159 if (cit == et.formats.end()) {
160 lyxerr << "External template format '" << format
161 << "' not specified in template "
162 << params_.templ.lyxName << endl;
166 updateExternal(format, buf);
167 string const str = doSubstitution(buf, cit->second.product);
169 return int(lyx::count(str.begin(), str.end(),'\n') + 1);
173 int InsetExternal::latex(Buffer const * buf, ostream & os,
174 LatexRunParams const & runparams) const
176 // If the template has specified a PDFLaTeX output, then we try and
178 if (runparams.flavor == LatexRunParams::PDFLATEX) {
179 ExternalTemplate const & et = params_.templ;
180 ExternalTemplate::Formats::const_iterator cit =
181 et.formats.find("PDFLaTeX");
182 if (cit != et.formats.end())
183 return write("PDFLaTeX", buf, os);
186 return write("LaTeX", buf, os);
190 int InsetExternal::ascii(Buffer const * buf, ostream & os, int) const
192 return write("Ascii", buf, os);
196 int InsetExternal::linuxdoc(Buffer const * buf, ostream & os) const
198 return write("LinuxDoc", buf, os);
202 int InsetExternal::docbook(Buffer const * buf, ostream & os, bool) const
204 return write("DocBook", buf, os);
208 void InsetExternal::validate(LaTeXFeatures & features) const
210 ExternalTemplate const & et = params_.templ;
211 ExternalTemplate::Formats::const_iterator cit =
212 et.formats.find("LaTeX");
214 if (cit == et.formats.end())
217 if (!cit->second.requirement.empty()) {
218 features.require(cit->second.requirement);
220 if (!cit->second.preamble.empty()) {
221 features.addExternalPreamble(cit->second.preamble + "\n");
226 Inset * InsetExternal::clone(Buffer const &) const
228 InsetExternal * inset = new InsetExternal;
229 inset->params_ = params_;
230 inset->view_ = view_;
235 // Inset * InsetExternal::clone(Buffer const &, bool same_id) const
237 // InsetExternal * inset = new InsetExternal;
238 // inset->params_ = params_;
239 // inset->view_ = view_;
246 string const InsetExternal::getScreenLabel(Buffer const *) const
248 ExternalTemplate const & et = params_.templ;
249 if (et.guiName.empty())
250 return _("External");
252 return doSubstitution(0, et.guiName);
256 void InsetExternal::executeCommand(string const & s,
257 Buffer const * buffer) const
259 Path p(buffer->filePath());
261 if (lyxerr.debugging()) {
262 lyxerr << "Executing '" << s << "' in '"
263 << buffer->filePath() << '\'' << endl;
265 one.startscript(Systemcall::Wait, s);
269 string const InsetExternal::doSubstitution(Buffer const * buffer,
270 string const & s) const
273 string const basename = ChangeExtension(params_.filename, string());
275 if (buffer && !buffer->tmppath.empty() && !buffer->niceFile) {
276 filepath = buffer->filePath();
278 result = subst(s, "$$FName", params_.filename);
279 result = subst(result, "$$Basename", basename);
280 result = subst(result, "$$Parameters", params_.parameters);
281 result = subst(result, "$$FPath", filepath);
282 result = subst(result, "$$Tempname", tempname_);
283 result = subst(result, "$$Sysdir", system_lyxdir);
285 // Handle the $$Contents(filename) syntax
286 if (contains(result, "$$Contents(\"")) {
288 string::size_type const pos = result.find("$$Contents(\"");
289 string::size_type const end = result.find("\")", pos);
290 string const file = result.substr(pos + 12, end - (pos + 12));
293 // Make sure we are in the directory of the buffer
294 Path p(buffer->filePath());
295 contents = GetFileContents(file);
297 contents = GetFileContents(file);
299 result = subst(result,
300 ("$$Contents(\"" + file + "\")").c_str(),
308 void InsetExternal::updateExternal() const
310 updateExternal("LaTeX", view_->buffer());
313 void InsetExternal::updateExternal(string const & format,
314 Buffer const * buf) const
316 ExternalTemplate const & et = params_.templ;
317 ExternalTemplate::Formats::const_iterator cit =
318 et.formats.find(format);
320 if (cit == et.formats.end() ||
321 cit->second.updateCommand.empty() ||
322 !et.automaticProduction)
325 if (!cit->second.updateResult.empty()) {
326 string const resultfile = doSubstitution(buf,
327 cit->second.updateResult);
328 FileInfo fi(params_.filename);
329 FileInfo fi2(resultfile);
330 if (fi2.exist() && fi.exist() &&
331 difftime(fi2.getModificationTime(),
332 fi.getModificationTime()) >= 0) {
333 lyxerr[Debug::FILES] << resultfile
334 << " is up to date" << endl;
339 executeCommand(doSubstitution(buf, cit->second.updateCommand), buf);
343 void InsetExternal::viewExternal() const
345 ExternalTemplate const & et = params_.templ;
346 if (et.viewCommand.empty())
350 executeCommand(doSubstitution(view_->buffer(),
356 void InsetExternal::editExternal() const
358 ExternalTemplate const & et = params_.templ;
359 if (et.editCommand.empty())
363 executeCommand(doSubstitution(view_->buffer(),
369 string const InsetExternalMailer::name_("external");
371 InsetExternalMailer::InsetExternalMailer(InsetExternal & inset)
376 string const InsetExternalMailer::inset2string() const
378 return params2string(inset_.params());
382 void InsetExternalMailer::string2params(string const & in,
383 InsetExternal::Params & params)
385 params = InsetExternal::Params();
390 istringstream data(STRCONV(in));
396 string const token = lex.getString();
401 // This is part of the inset proper that is usually swallowed
402 // by Buffer::readInset
405 string const token = lex.getString();
406 if (token != "External")
413 params = inset.params();
419 InsetExternalMailer::params2string(InsetExternal::Params const & params)
422 inset.setFromParams(params);
424 data << name_ << ' ';
425 inset.write(0, data);
426 data << "\\end_inset\n";
427 return STRCONV(data.str());