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);
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());
100 void InsetExternal::setFromParams(Params const & p)
102 params_.filename = p.filename;
103 params_.parameters = p.parameters;
104 params_.templ = p.templ;
108 string const InsetExternal::editMessage() const
110 return doSubstitution(0, params_.templ.guiName);
114 void InsetExternal::edit(BufferView * bv, int, int, mouse_button::state)
116 InsetExternalMailer mailer(*this);
117 mailer.showDialog(bv);
121 void InsetExternal::edit(BufferView * bv, bool)
123 edit(bv, 0, 0, mouse_button::none);
127 void InsetExternal::write(Buffer const *, ostream & os) const
129 os << "External " << params_.templ.lyxName << ",\""
130 << params_.filename << "\",\"" << params_.parameters << "\"\n";
134 void InsetExternal::read(Buffer const *, LyXLex & lex)
139 // Read inset data from lex and store in format
141 format = lex.getString();
143 lex.printError("InsetExternal: Parse error: `$$Token'");
148 token = lex.getString();
149 if (token == "\\end_inset")
152 if (token != "\\end_inset") {
153 lex.printError("Missing \\end_inset at this point. "
157 // Parse string format...
158 string::size_type const pos1 = format.find(',');
159 params_.templ = ExternalTemplateManager::get().getTemplateByName(format.substr(0, pos1));
160 string::size_type const pos2 = format.find("\",\"", pos1);
161 params_.filename = format.substr(pos1 + 2, pos2 - (pos1 + 2));
162 params_.parameters = format.substr(pos2 + 3, format.length() - (pos2 + 4));
164 lyxerr[Debug::INFO] << "InsetExternal::Read: " << params_.templ.lyxName
165 << ' ' << params_.filename
166 << ' ' << params_.parameters << endl;
170 int InsetExternal::write(string const & format,
171 Buffer const * buf, ostream & os) const
173 ExternalTemplate const & et = params_.templ;
174 ExternalTemplate::Formats::const_iterator cit =
175 et.formats.find(format);
176 if (cit == et.formats.end()) {
177 lyxerr << "External template format '" << format
178 << "' not specified in template "
179 << params_.templ.lyxName << endl;
183 updateExternal(format, buf);
184 os << doSubstitution(buf, cit->second.product);
185 return 0; // CHECK (FIXME check what ? - jbl)
189 int InsetExternal::latex(Buffer const * buf,
190 ostream & os, bool, bool) const
192 return write("LaTeX", buf, os);
196 int InsetExternal::ascii(Buffer const * buf, ostream & os, int) const
198 return write("Ascii", buf, os);
202 int InsetExternal::linuxdoc(Buffer const * buf, ostream & os) const
204 return write("LinuxDoc", buf, os);
208 int InsetExternal::docbook(Buffer const * buf, ostream & os, bool) const
210 return write("DocBook", buf, os);
214 void InsetExternal::validate(LaTeXFeatures & features) const
216 ExternalTemplate const & et = params_.templ;
217 ExternalTemplate::Formats::const_iterator cit =
218 et.formats.find("LaTeX");
220 if (cit == et.formats.end())
223 if (!cit->second.requirement.empty()) {
224 features.require(cit->second.requirement);
226 if (!cit->second.preamble.empty()) {
227 features.addExternalPreamble(cit->second.preamble + "\n");
232 Inset * InsetExternal::clone(Buffer const &, bool same_id) const
234 InsetExternal * inset = new InsetExternal;
235 inset->params_ = params_;
236 inset->view_ = view_;
243 string const InsetExternal::getScreenLabel(Buffer const *) const
245 ExternalTemplate const & et = params_.templ;
246 if (et.guiName.empty())
247 return _("External");
249 return doSubstitution(0, et.guiName);
253 void InsetExternal::executeCommand(string const & s,
254 Buffer const * buffer) const
256 Path p(buffer->filePath());
258 if (lyxerr.debugging()) {
259 lyxerr << "Executing '" << s << "' in '"
260 << buffer->filePath() << '\'' << endl;
262 one.startscript(Systemcall::Wait, s);
266 string const InsetExternal::doSubstitution(Buffer const * buffer,
267 string const & s) const
270 string const basename = ChangeExtension(params_.filename, string());
272 if (buffer && !buffer->tmppath.empty() && !buffer->niceFile) {
273 filepath = buffer->filePath();
275 result = subst(s, "$$FName", params_.filename);
276 result = subst(result, "$$Basename", basename);
277 result = subst(result, "$$Parameters", params_.parameters);
278 result = subst(result, "$$FPath", filepath);
279 result = subst(result, "$$Tempname", tempname_);
280 result = subst(result, "$$Sysdir", system_lyxdir);
282 // Handle the $$Contents(filename) syntax
283 if (contains(result, "$$Contents(\"")) {
285 string::size_type const pos = result.find("$$Contents(\"");
286 string::size_type const end = result.find("\")", pos);
287 string const file = result.substr(pos + 12, end - (pos + 12));
290 // Make sure we are in the directory of the buffer
291 Path p(buffer->filePath());
292 contents = GetFileContents(file);
294 contents = GetFileContents(file);
296 result = subst(result,
297 ("$$Contents(\"" + file + "\")").c_str(),
305 void InsetExternal::updateExternal() const
307 updateExternal("LaTeX", view_->buffer());
310 void InsetExternal::updateExternal(string const & format,
311 Buffer const * buf) const
313 ExternalTemplate const & et = params_.templ;
314 ExternalTemplate::Formats::const_iterator cit =
315 et.formats.find(format);
317 if (cit == et.formats.end() ||
318 cit->second.updateCommand.empty() ||
319 !et.automaticProduction)
322 if (!cit->second.updateResult.empty()) {
323 string const resultfile = doSubstitution(buf,
324 cit->second.updateResult);
325 FileInfo fi(params_.filename);
326 FileInfo fi2(resultfile);
327 if (fi2.exist() && fi.exist() &&
328 difftime(fi2.getModificationTime(),
329 fi.getModificationTime()) >= 0) {
330 lyxerr[Debug::FILES] << resultfile
331 << " is up to date" << endl;
336 executeCommand(doSubstitution(buf, cit->second.updateCommand), buf);
340 void InsetExternal::viewExternal() const
342 ExternalTemplate const & et = params_.templ;
343 if (et.viewCommand.empty())
347 executeCommand(doSubstitution(view_->buffer(),
353 void InsetExternal::editExternal() const
355 ExternalTemplate const & et = params_.templ;
356 if (et.editCommand.empty())
360 executeCommand(doSubstitution(view_->buffer(),
366 string const InsetExternalMailer::name_("external");
368 InsetExternalMailer::InsetExternalMailer(InsetExternal & inset)
373 string const InsetExternalMailer::inset2string() const
375 return params2string(inset_.params());
379 void InsetExternalMailer::string2params(string const & in,
380 InsetExternal::Params & params)
382 params = InsetExternal::Params();
384 istringstream data(in);
390 string const token = lex.getString();
395 // This is part of the inset proper that is usually swallowed
396 // by Buffer::readInset
399 string const token = lex.getString();
400 if (token != "External")
407 params = inset.params();
413 InsetExternalMailer::params2string(InsetExternal::Params const & params)
416 inset.setFromParams(params);
418 data << name_ << ' ';
419 inset.write(0, data);
420 data << "\\end_inset\n";