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"
41 InsetExternal::InsetExternal()
44 tempname_ = lyx::tempName(string(), "lyxext");
45 //ExternalTemplateManager::Templates::const_iterator i1;
46 params_.templ = ExternalTemplateManager::get().getTemplates().begin()->second;
50 InsetExternal::~InsetExternal()
52 lyx::unlink(tempname_);
53 InsetExternalMailer mailer(*this);
58 InsetExternal::Params const & InsetExternal::params() const
64 dispatch_result InsetExternal::localDispatch(FuncRequest const & cmd)
68 case LFUN_INSET_MODIFY: {
69 InsetExternal::Params p;
70 InsetExternalMailer::string2params(cmd.argument, p);
71 if (!p.filename.empty()) {
73 cmd.view()->updateInset(this);
78 case LFUN_INSET_DIALOG_UPDATE:
79 InsetExternalMailer(*this).updateDialog(cmd.view());
82 case LFUN_MOUSE_RELEASE:
84 InsetExternalMailer(*this).showDialog(cmd.view());
93 void InsetExternal::setFromParams(Params const & p)
95 params_.filename = p.filename;
96 params_.parameters = p.parameters;
97 params_.templ = p.templ;
101 string const InsetExternal::editMessage() const
103 return doSubstitution(0, params_.templ.guiName);
107 void InsetExternal::write(Buffer const *, ostream & os) const
109 os << "External " << params_.templ.lyxName << ",\""
110 << params_.filename << "\",\"" << params_.parameters << "\"\n";
114 void InsetExternal::read(Buffer const *, LyXLex & lex)
119 // Read inset data from lex and store in format
121 format = lex.getString();
123 lex.printError("InsetExternal: Parse error: `$$Token'");
128 token = lex.getString();
129 if (token == "\\end_inset")
132 if (token != "\\end_inset") {
133 lex.printError("Missing \\end_inset at this point. "
137 // Parse string format...
138 string::size_type const pos1 = format.find(',');
139 params_.templ = ExternalTemplateManager::get().getTemplateByName(format.substr(0, pos1));
140 string::size_type const pos2 = format.find("\",\"", pos1);
141 params_.filename = format.substr(pos1 + 2, pos2 - (pos1 + 2));
142 params_.parameters = format.substr(pos2 + 3, format.length() - (pos2 + 4));
144 lyxerr[Debug::INFO] << "InsetExternal::Read: " << params_.templ.lyxName
145 << ' ' << params_.filename
146 << ' ' << params_.parameters << endl;
150 int InsetExternal::write(string const & format,
151 Buffer const * buf, ostream & os) const
153 ExternalTemplate const & et = params_.templ;
154 ExternalTemplate::Formats::const_iterator cit =
155 et.formats.find(format);
156 if (cit == et.formats.end()) {
157 lyxerr << "External template format '" << format
158 << "' not specified in template "
159 << params_.templ.lyxName << endl;
163 updateExternal(format, buf);
164 os << doSubstitution(buf, cit->second.product);
165 return 0; // CHECK (FIXME check what ? - jbl)
169 int InsetExternal::latex(Buffer const * buf, ostream & os,
170 LatexRunParams 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 string const InsetExternalMailer::name_("external");
349 InsetExternalMailer::InsetExternalMailer(InsetExternal & inset)
354 string const InsetExternalMailer::inset2string() const
356 return params2string(inset_.params());
360 void InsetExternalMailer::string2params(string const & in,
361 InsetExternal::Params & params)
363 params = InsetExternal::Params();
368 istringstream data(STRCONV(in));
374 string const token = lex.getString();
379 // This is part of the inset proper that is usually swallowed
380 // by Buffer::readInset
383 string const token = lex.getString();
384 if (token != "External")
391 params = inset.params();
397 InsetExternalMailer::params2string(InsetExternal::Params const & params)
400 inset.setFromParams(params);
402 data << name_ << ' ';
403 inset.write(0, data);
404 data << "\\end_inset\n";
405 return STRCONV(data.str());