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 if (cmd.action != LFUN_INSET_MODIFY)
70 InsetExternal::Params p;
71 InsetExternalMailer::string2params(cmd.argument, p);
72 if (p.filename.empty())
76 cmd.view()->updateInset(this, true);
81 void InsetExternal::setFromParams(Params const & p)
83 params_.filename = p.filename;
84 params_.parameters = p.parameters;
85 params_.templ = p.templ;
89 string const InsetExternal::editMessage() const
91 return doSubstitution(0, params_.templ.guiName);
95 void InsetExternal::edit(BufferView *, int, int, mouse_button::state)
97 InsetExternalMailer mailer(*this);
102 void InsetExternal::edit(BufferView * bv, bool)
104 edit(bv, 0, 0, mouse_button::none);
108 void InsetExternal::write(Buffer const *, ostream & os) const
110 os << "External " << params_.templ.lyxName << ",\""
111 << params_.filename << "\",\"" << params_.parameters << "\"\n";
115 void InsetExternal::read(Buffer const *, LyXLex & lex)
120 // Read inset data from lex and store in format
122 format = lex.getString();
124 lex.printError("InsetExternal: Parse error: `$$Token'");
129 token = lex.getString();
130 if (token == "\\end_inset")
133 if (token != "\\end_inset") {
134 lex.printError("Missing \\end_inset at this point. "
138 // Parse string format...
139 string::size_type const pos1 = format.find(',');
140 params_.templ = ExternalTemplateManager::get().getTemplateByName(format.substr(0, pos1));
141 string::size_type const pos2 = format.find("\",\"", pos1);
142 params_.filename = format.substr(pos1 + 2, pos2 - (pos1 + 2));
143 params_.parameters = format.substr(pos2 + 3, format.length() - (pos2 + 4));
145 lyxerr[Debug::INFO] << "InsetExternal::Read: " << params_.templ.lyxName
146 << ' ' << params_.filename
147 << ' ' << params_.parameters << endl;
151 int InsetExternal::write(string const & format,
152 Buffer const * buf, ostream & os) const
154 ExternalTemplate const & et = params_.templ;
155 ExternalTemplate::Formats::const_iterator cit =
156 et.formats.find(format);
157 if (cit == et.formats.end()) {
158 lyxerr << "External template format '" << format
159 << "' not specified in template "
160 << params_.templ.lyxName << endl;
164 updateExternal(format, buf);
165 os << doSubstitution(buf, cit->second.product);
166 return 0; // CHECK (FIXME check what ? - jbl)
170 int InsetExternal::latex(Buffer const * buf,
171 ostream & os, bool, bool) const
173 return write("LaTeX", buf, os);
177 int InsetExternal::ascii(Buffer const * buf, ostream & os, int) const
179 return write("Ascii", buf, os);
183 int InsetExternal::linuxdoc(Buffer const * buf, ostream & os) const
185 return write("LinuxDoc", buf, os);
189 int InsetExternal::docbook(Buffer const * buf, ostream & os, bool) const
191 return write("DocBook", buf, os);
195 void InsetExternal::validate(LaTeXFeatures & features) const
197 ExternalTemplate const & et = params_.templ;
198 ExternalTemplate::Formats::const_iterator cit =
199 et.formats.find("LaTeX");
201 if (cit == et.formats.end())
204 if (!cit->second.requirement.empty()) {
205 features.require(cit->second.requirement);
207 if (!cit->second.preamble.empty()) {
208 features.addExternalPreamble(cit->second.preamble + "\n");
213 Inset * InsetExternal::clone(Buffer const &, bool same_id) const
215 InsetExternal * inset = new InsetExternal;
216 inset->params_ = params_;
217 inset->view_ = view_;
224 string const InsetExternal::getScreenLabel(Buffer const *) const
226 ExternalTemplate const & et = params_.templ;
227 if (et.guiName.empty())
228 return _("External");
230 return doSubstitution(0, et.guiName);
234 void InsetExternal::executeCommand(string const & s,
235 Buffer const * buffer) const
237 Path p(buffer->filePath());
239 if (lyxerr.debugging()) {
240 lyxerr << "Executing '" << s << "' in '"
241 << buffer->filePath() << '\'' << endl;
243 one.startscript(Systemcall::Wait, s);
247 string const InsetExternal::doSubstitution(Buffer const * buffer,
248 string const & s) const
251 string const basename = ChangeExtension(params_.filename, string());
253 if (buffer && !buffer->tmppath.empty() && !buffer->niceFile) {
254 filepath = buffer->filePath();
256 result = subst(s, "$$FName", params_.filename);
257 result = subst(result, "$$Basename", basename);
258 result = subst(result, "$$Parameters", params_.parameters);
259 result = subst(result, "$$FPath", filepath);
260 result = subst(result, "$$Tempname", tempname_);
261 result = subst(result, "$$Sysdir", system_lyxdir);
263 // Handle the $$Contents(filename) syntax
264 if (contains(result, "$$Contents(\"")) {
266 string::size_type const pos = result.find("$$Contents(\"");
267 string::size_type const end = result.find("\")", pos);
268 string const file = result.substr(pos + 12, end - (pos + 12));
271 // Make sure we are in the directory of the buffer
272 Path p(buffer->filePath());
273 contents = GetFileContents(file);
275 contents = GetFileContents(file);
277 result = subst(result,
278 ("$$Contents(\"" + file + "\")").c_str(),
286 void InsetExternal::updateExternal() const
288 updateExternal("LaTeX", view_->buffer());
291 void InsetExternal::updateExternal(string const & format,
292 Buffer const * buf) const
294 ExternalTemplate const & et = params_.templ;
295 ExternalTemplate::Formats::const_iterator cit =
296 et.formats.find(format);
298 if (cit == et.formats.end() ||
299 cit->second.updateCommand.empty() ||
300 !et.automaticProduction)
303 if (!cit->second.updateResult.empty()) {
304 string const resultfile = doSubstitution(buf,
305 cit->second.updateResult);
306 FileInfo fi(params_.filename);
307 FileInfo fi2(resultfile);
308 if (fi2.exist() && fi.exist() &&
309 difftime(fi2.getModificationTime(),
310 fi.getModificationTime()) >= 0) {
311 lyxerr[Debug::FILES] << resultfile
312 << " is up to date" << endl;
317 executeCommand(doSubstitution(buf, cit->second.updateCommand), buf);
321 void InsetExternal::viewExternal() const
323 ExternalTemplate const & et = params_.templ;
324 if (et.viewCommand.empty())
328 executeCommand(doSubstitution(view_->buffer(),
334 void InsetExternal::editExternal() const
336 ExternalTemplate const & et = params_.templ;
337 if (et.editCommand.empty())
341 executeCommand(doSubstitution(view_->buffer(),
347 bool operator==(InsetExternal::Params const & left,
348 InsetExternal::Params const & right)
350 return ((left.filename == right.filename) &&
351 (left.parameters == right.parameters) &&
352 (left.templ.lyxName == right.templ.lyxName));
356 bool operator!=(InsetExternal::Params const & left,
357 InsetExternal::Params const & right)
359 return !(left == right);
363 string const InsetExternalMailer::name_("external");
365 InsetExternalMailer::InsetExternalMailer(InsetExternal & inset)
370 string const InsetExternalMailer::inset2string() const
372 return params2string(inset_.params());
376 void InsetExternalMailer::string2params(string const & in,
377 InsetExternal::Params & params)
379 params = InsetExternal::Params();
381 istringstream data(in);
387 string const token = lex.getString();
392 // This is part of the inset proper that is usually swallowed
393 // by Buffer::readInset
396 string const token = lex.getString();
397 if (token != "External")
403 params = inset.params();
408 InsetExternalMailer::params2string(InsetExternal::Params const & params)
411 inset.setFromParams(params);
413 data << name_ << ' ';
414 inset.write(0, data);
415 data << "\\end_inset\n";