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 "funcrequest.h"
20 #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, true);
82 case LFUN_INSET_DIALOG_UPDATE: {
83 InsetExternalMailer mailer(*this);
84 mailer.updateDialog();
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::edit(BufferView *, int, int, mouse_button::state)
112 InsetExternalMailer mailer(*this);
117 void InsetExternal::edit(BufferView * bv, bool)
119 edit(bv, 0, 0, mouse_button::none);
123 void InsetExternal::write(Buffer const *, ostream & os) const
125 os << "External " << params_.templ.lyxName << ",\""
126 << params_.filename << "\",\"" << params_.parameters << "\"\n";
130 void InsetExternal::read(Buffer const *, LyXLex & lex)
135 // Read inset data from lex and store in format
137 format = lex.getString();
139 lex.printError("InsetExternal: Parse error: `$$Token'");
144 token = lex.getString();
145 if (token == "\\end_inset")
148 if (token != "\\end_inset") {
149 lex.printError("Missing \\end_inset at this point. "
153 // Parse string format...
154 string::size_type const pos1 = format.find(',');
155 params_.templ = ExternalTemplateManager::get().getTemplateByName(format.substr(0, pos1));
156 string::size_type const pos2 = format.find("\",\"", pos1);
157 params_.filename = format.substr(pos1 + 2, pos2 - (pos1 + 2));
158 params_.parameters = format.substr(pos2 + 3, format.length() - (pos2 + 4));
160 lyxerr[Debug::INFO] << "InsetExternal::Read: " << params_.templ.lyxName
161 << ' ' << params_.filename
162 << ' ' << params_.parameters << endl;
166 int InsetExternal::write(string const & format,
167 Buffer const * buf, ostream & os) const
169 ExternalTemplate const & et = params_.templ;
170 ExternalTemplate::Formats::const_iterator cit =
171 et.formats.find(format);
172 if (cit == et.formats.end()) {
173 lyxerr << "External template format '" << format
174 << "' not specified in template "
175 << params_.templ.lyxName << endl;
179 updateExternal(format, buf);
180 os << doSubstitution(buf, cit->second.product);
181 return 0; // CHECK (FIXME check what ? - jbl)
185 int InsetExternal::latex(Buffer const * buf,
186 ostream & os, bool, bool) const
188 return write("LaTeX", buf, os);
192 int InsetExternal::ascii(Buffer const * buf, ostream & os, int) const
194 return write("Ascii", buf, os);
198 int InsetExternal::linuxdoc(Buffer const * buf, ostream & os) const
200 return write("LinuxDoc", buf, os);
204 int InsetExternal::docbook(Buffer const * buf, ostream & os, bool) const
206 return write("DocBook", buf, os);
210 void InsetExternal::validate(LaTeXFeatures & features) const
212 ExternalTemplate const & et = params_.templ;
213 ExternalTemplate::Formats::const_iterator cit =
214 et.formats.find("LaTeX");
216 if (cit == et.formats.end())
219 if (!cit->second.requirement.empty()) {
220 features.require(cit->second.requirement);
222 if (!cit->second.preamble.empty()) {
223 features.addExternalPreamble(cit->second.preamble + "\n");
228 Inset * InsetExternal::clone(Buffer const &, bool same_id) const
230 InsetExternal * inset = new InsetExternal;
231 inset->params_ = params_;
232 inset->view_ = view_;
239 string const InsetExternal::getScreenLabel(Buffer const *) const
241 ExternalTemplate const & et = params_.templ;
242 if (et.guiName.empty())
243 return _("External");
245 return doSubstitution(0, et.guiName);
249 void InsetExternal::executeCommand(string const & s,
250 Buffer const * buffer) const
252 Path p(buffer->filePath());
254 if (lyxerr.debugging()) {
255 lyxerr << "Executing '" << s << "' in '"
256 << buffer->filePath() << '\'' << endl;
258 one.startscript(Systemcall::Wait, s);
262 string const InsetExternal::doSubstitution(Buffer const * buffer,
263 string const & s) const
266 string const basename = ChangeExtension(params_.filename, string());
268 if (buffer && !buffer->tmppath.empty() && !buffer->niceFile) {
269 filepath = buffer->filePath();
271 result = subst(s, "$$FName", params_.filename);
272 result = subst(result, "$$Basename", basename);
273 result = subst(result, "$$Parameters", params_.parameters);
274 result = subst(result, "$$FPath", filepath);
275 result = subst(result, "$$Tempname", tempname_);
276 result = subst(result, "$$Sysdir", system_lyxdir);
278 // Handle the $$Contents(filename) syntax
279 if (contains(result, "$$Contents(\"")) {
281 string::size_type const pos = result.find("$$Contents(\"");
282 string::size_type const end = result.find("\")", pos);
283 string const file = result.substr(pos + 12, end - (pos + 12));
286 // Make sure we are in the directory of the buffer
287 Path p(buffer->filePath());
288 contents = GetFileContents(file);
290 contents = GetFileContents(file);
292 result = subst(result,
293 ("$$Contents(\"" + file + "\")").c_str(),
301 void InsetExternal::updateExternal() const
303 updateExternal("LaTeX", view_->buffer());
306 void InsetExternal::updateExternal(string const & format,
307 Buffer const * buf) const
309 ExternalTemplate const & et = params_.templ;
310 ExternalTemplate::Formats::const_iterator cit =
311 et.formats.find(format);
313 if (cit == et.formats.end() ||
314 cit->second.updateCommand.empty() ||
315 !et.automaticProduction)
318 if (!cit->second.updateResult.empty()) {
319 string const resultfile = doSubstitution(buf,
320 cit->second.updateResult);
321 FileInfo fi(params_.filename);
322 FileInfo fi2(resultfile);
323 if (fi2.exist() && fi.exist() &&
324 difftime(fi2.getModificationTime(),
325 fi.getModificationTime()) >= 0) {
326 lyxerr[Debug::FILES] << resultfile
327 << " is up to date" << endl;
332 executeCommand(doSubstitution(buf, cit->second.updateCommand), buf);
336 void InsetExternal::viewExternal() const
338 ExternalTemplate const & et = params_.templ;
339 if (et.viewCommand.empty())
343 executeCommand(doSubstitution(view_->buffer(),
349 void InsetExternal::editExternal() const
351 ExternalTemplate const & et = params_.templ;
352 if (et.editCommand.empty())
356 executeCommand(doSubstitution(view_->buffer(),
362 string const InsetExternalMailer::name_("external");
364 InsetExternalMailer::InsetExternalMailer(InsetExternal & inset)
369 string const InsetExternalMailer::inset2string() const
371 return params2string(inset_.params());
375 void InsetExternalMailer::string2params(string const & in,
376 InsetExternal::Params & params)
378 params = InsetExternal::Params();
380 istringstream data(in);
386 string const token = lex.getString();
391 // This is part of the inset proper that is usually swallowed
392 // by Buffer::readInset
395 string const token = lex.getString();
396 if (token != "External")
402 params = inset.params();
407 InsetExternalMailer::params2string(InsetExternal::Params const & params)
410 inset.setFromParams(params);
412 data << name_ << ' ';
413 inset.write(0, data);
414 data << "\\end_inset\n";