1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2001 The LyX Team.
9 * ====================================================== */
14 #pragma implementation
20 #include "insetexternal.h"
21 #include "ExternalTemplate.h"
22 #include "BufferView.h"
25 #include "frontends/Dialogs.h"
27 #include "LaTeXFeatures.h"
28 #include "support/filetools.h"
29 #include "support/lstrings.h"
30 #include "support/path.h"
31 #include "support/syscall.h"
34 #include "support/FileInfo.h"
38 InsetExternal::InsetExternal()
41 tempname_ = lyx::tempName(string(), "lyxext");
42 //ExternalTemplateManager::Templates::const_iterator i1;
43 params_.templ = ExternalTemplateManager::get().getTemplates().begin()->second;
47 InsetExternal::~InsetExternal()
49 lyx::unlink(tempname_);
54 InsetExternal::Params InsetExternal::params() const
60 void InsetExternal::setFromParams(Params const & p)
62 params_.filename = p.filename;
63 params_.parameters = p.parameters;
64 params_.templ = p.templ;
68 string const InsetExternal::editMessage() const
70 return doSubstitution(0, params_.templ.guiName);
74 void InsetExternal::edit(BufferView * bv,
75 int /*x*/, int /*y*/, unsigned int /*button*/)
78 view_->owner()->getDialogs()->showExternal(this);
82 void InsetExternal::edit(BufferView * bv, bool)
88 void InsetExternal::write(Buffer const *, std::ostream & os) const
90 os << "External " << params_.templ.lyxName << ",\"" << params_.filename
91 << "\",\"" << params_.parameters << "\"\n";
95 void InsetExternal::read(Buffer const *, LyXLex & lex)
100 // Read inset data from lex and store in format
102 format = lex.getString();
104 lex.printError("InsetExternal: Parse error: `$$Token'");
109 token = lex.getString();
110 if (token == "\\end_inset")
113 if (token != "\\end_inset") {
114 lex.printError("Missing \\end_inset at this point. "
118 // Parse string format...
119 string::size_type const pos1 = format.find(",");
120 params_.templ = ExternalTemplateManager::get().getTemplateByName(format.substr(0, pos1));
121 string::size_type const pos2 = format.find("\",\"", pos1);
122 params_.filename = format.substr(pos1 + 2, pos2 - (pos1 + 2));
123 params_.parameters = format.substr(pos2 + 3, format.length() - (pos2 + 4));
125 lyxerr[Debug::INFO] << "InsetExternal::Read: " << params_.templ.lyxName
126 << " " << params_.filename
127 << " " << params_.parameters << endl;
131 int InsetExternal::write(string const & format,
132 Buffer const * buf, std::ostream & os) const
134 ExternalTemplate const & et = params_.templ;
135 ExternalTemplate::Formats::const_iterator cit =
136 et.formats.find(format);
137 if (cit == et.formats.end()) {
138 lyxerr << "External template format '" << format
139 << "' not specified in template " << params_.templ.lyxName
144 updateExternal(format, buf);
145 os << doSubstitution(buf, cit->second.product);
146 return 0; // CHECK (FIXME check what ? - jbl)
150 int InsetExternal::latex(Buffer const * buf,
151 std::ostream & os, bool, bool) const
153 return write("LaTeX", buf, os);
157 int InsetExternal::ascii(Buffer const * buf, std::ostream & os, int) const
159 return write("Ascii", buf, os);
163 int InsetExternal::linuxdoc(Buffer const * buf, std::ostream & os) const
165 return write("LinuxDoc", buf, os);
169 int InsetExternal::docbook(Buffer const * buf, std::ostream & os) const
171 return write("DocBook", buf, os);
175 void InsetExternal::validate(LaTeXFeatures & features) const
177 ExternalTemplate const & et = params_.templ;
178 ExternalTemplate::Formats::const_iterator cit =
179 et.formats.find("LaTeX");
181 if (cit == et.formats.end())
184 if (!cit->second.requirement.empty()) {
185 features.require(cit->second.requirement);
187 if (!cit->second.preamble.empty()) {
188 features.addExternalPreamble(cit->second.preamble + "\n");
193 Inset * InsetExternal::clone(Buffer const &, bool same_id) const
195 InsetExternal * inset = new InsetExternal();
196 inset->params_ = params_;
197 inset->view_ = view_;
204 string const InsetExternal::getScreenLabel(Buffer const *) const
206 ExternalTemplate const & et = params_.templ;
207 if (et.guiName.empty())
208 return _("External");
210 return doSubstitution(0, et.guiName);
214 void InsetExternal::executeCommand(string const & s,
215 Buffer const * buffer) const
217 string buf = MakeAbsPath(buffer->fileName());
218 string path = OnlyPath(buf);
221 if (lyxerr.debugging()) {
222 lyxerr << "Executing '" << s << "' in '"
223 << path << "'" << endl;
225 one.startscript(Systemcalls::Wait, s);
229 string const InsetExternal::doSubstitution(Buffer const * buffer,
230 string const & s) const
233 string const basename = ChangeExtension(params_.filename, string());
234 string filepath = "";
235 if (buffer && (!buffer->niceFile)) {
236 filepath = OnlyPath(MakeAbsPath(buffer->fileName()));
238 result = subst(s, "$$FName", params_.filename);
239 result = subst(result, "$$Basename", basename);
240 result = subst(result, "$$Parameters", params_.parameters);
241 result = subst(result, "$$FPath", filepath);
242 result = ReplaceEnvironmentPath(result);
243 result = subst(result, "$$Tempname", tempname_);
244 result = subst(result, "$$Sysdir", system_lyxdir);
246 // Handle the $$Contents(filename) syntax
247 if (contains(result, "$$Contents(\"")) {
249 string::size_type const pos = result.find("$$Contents(\"");
250 string::size_type const end = result.find("\")", pos);
251 string const file = result.substr(pos + 12, end - (pos + 12));
254 // Make sure we are in the directory of the buffer
255 string const buf = MakeAbsPath(buffer->fileName());
256 string const path = OnlyPath(buf);
258 contents = GetFileContents(file);
260 contents = GetFileContents(file);
262 result = subst(result,
263 ("$$Contents(\"" + file + "\")").c_str(),
271 void InsetExternal::updateExternal() const
273 updateExternal("LaTeX", view_->buffer());
276 void InsetExternal::updateExternal(string const & format,
277 Buffer const * buf) const
279 ExternalTemplate const & et = params_.templ;
280 ExternalTemplate::Formats::const_iterator cit =
281 et.formats.find(format);
283 if (cit == et.formats.end() ||
284 cit->second.updateCommand.empty() ||
285 !et.automaticProduction)
288 if (!cit->second.updateResult.empty()) {
289 string const resultfile = doSubstitution(buf,
290 cit->second.updateResult);
291 FileInfo fi(params_.filename);
292 FileInfo fi2(resultfile);
293 if (fi2.exist() && fi.exist() &&
294 ::difftime(fi2.getModificationTime(),
295 fi.getModificationTime()) >= 0) {
296 lyxerr[Debug::FILES] << resultfile
297 << " is up to date" << endl;
302 executeCommand(doSubstitution(buf, cit->second.updateCommand), buf);
306 void InsetExternal::viewExternal() const
308 ExternalTemplate const & et = params_.templ;
309 if (et.viewCommand.empty())
313 executeCommand(doSubstitution(view_->buffer(),
319 void InsetExternal::editExternal() const
321 ExternalTemplate const & et = params_.templ;
322 if (et.editCommand.empty())
326 executeCommand(doSubstitution(view_->buffer(),
332 bool operator==(InsetExternal::Params const & left,
333 InsetExternal::Params const & right)
335 return ((left.filename == right.filename) &&
336 (left.parameters == right.parameters) &&
337 (left.templ.lyxName == right.templ.lyxName));
341 bool operator!=(InsetExternal::Params const & left,
342 InsetExternal::Params const & right)
344 return !(left == right);