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
17 #include "insetexternal.h"
18 #include "ExternalTemplate.h"
19 #include "BufferView.h"
21 #include "frontends/LyXView.h"
23 #include "LaTeXFeatures.h"
27 #include "frontends/Dialogs.h"
29 #include "support/filetools.h"
30 #include "support/lstrings.h"
31 #include "support/path.h"
32 #include "support/systemcall.h"
33 #include "support/FileInfo.h"
39 #ifndef CXX_GLOBAL_CSTD
47 InsetExternal::InsetExternal()
50 tempname_ = lyx::tempName(string(), "lyxext");
51 //ExternalTemplateManager::Templates::const_iterator i1;
52 params_.templ = ExternalTemplateManager::get().getTemplates().begin()->second;
56 InsetExternal::~InsetExternal()
58 lyx::unlink(tempname_);
63 InsetExternal::Params InsetExternal::params() const
69 void InsetExternal::setFromParams(Params const & p)
71 params_.filename = p.filename;
72 params_.parameters = p.parameters;
73 params_.templ = p.templ;
77 string const InsetExternal::editMessage() const
79 return doSubstitution(0, params_.templ.guiName);
83 void InsetExternal::edit(BufferView * bv,
84 int /*x*/, int /*y*/, mouse_button::state)
87 view_->owner()->getDialogs()->showExternal(this);
91 void InsetExternal::edit(BufferView * bv, bool)
93 edit(bv, 0, 0, mouse_button::none);
97 void InsetExternal::write(Buffer const *, ostream & os) const
99 os << "External " << params_.templ.lyxName << ",\""
100 << params_.filename << "\",\"" << params_.parameters << "\"\n";
104 void InsetExternal::read(Buffer const *, LyXLex & lex)
109 // Read inset data from lex and store in format
111 format = lex.getString();
113 lex.printError("InsetExternal: Parse error: `$$Token'");
118 token = lex.getString();
119 if (token == "\\end_inset")
122 if (token != "\\end_inset") {
123 lex.printError("Missing \\end_inset at this point. "
127 // Parse string format...
128 string::size_type const pos1 = format.find(",");
129 params_.templ = ExternalTemplateManager::get().getTemplateByName(format.substr(0, pos1));
130 string::size_type const pos2 = format.find("\",\"", pos1);
131 params_.filename = format.substr(pos1 + 2, pos2 - (pos1 + 2));
132 params_.parameters = format.substr(pos2 + 3, format.length() - (pos2 + 4));
134 lyxerr[Debug::INFO] << "InsetExternal::Read: " << params_.templ.lyxName
135 << " " << params_.filename
136 << " " << params_.parameters << endl;
140 int InsetExternal::write(string const & format,
141 Buffer const * buf, ostream & os) const
143 ExternalTemplate const & et = params_.templ;
144 ExternalTemplate::Formats::const_iterator cit =
145 et.formats.find(format);
146 if (cit == et.formats.end()) {
147 lyxerr << "External template format '" << format
148 << "' not specified in template "
149 << params_.templ.lyxName << endl;
153 updateExternal(format, buf);
154 os << doSubstitution(buf, cit->second.product);
155 return 0; // CHECK (FIXME check what ? - jbl)
159 int InsetExternal::latex(Buffer const * buf,
160 ostream & os, bool, bool) const
162 return write("LaTeX", buf, os);
166 int InsetExternal::ascii(Buffer const * buf, ostream & os, int) const
168 return write("Ascii", buf, os);
172 int InsetExternal::linuxdoc(Buffer const * buf, ostream & os) const
174 return write("LinuxDoc", buf, os);
178 int InsetExternal::docbook(Buffer const * buf, ostream & os) const
180 return write("DocBook", buf, os);
184 void InsetExternal::validate(LaTeXFeatures & features) const
186 ExternalTemplate const & et = params_.templ;
187 ExternalTemplate::Formats::const_iterator cit =
188 et.formats.find("LaTeX");
190 if (cit == et.formats.end())
193 if (!cit->second.requirement.empty()) {
194 features.require(cit->second.requirement);
196 if (!cit->second.preamble.empty()) {
197 features.addExternalPreamble(cit->second.preamble + "\n");
202 Inset * InsetExternal::clone(Buffer const &, bool same_id) const
204 InsetExternal * inset = new InsetExternal;
205 inset->params_ = params_;
206 inset->view_ = view_;
213 string const InsetExternal::getScreenLabel(Buffer const *) const
215 ExternalTemplate const & et = params_.templ;
216 if (et.guiName.empty())
217 return _("External");
219 return doSubstitution(0, et.guiName);
223 void InsetExternal::executeCommand(string const & s,
224 Buffer const * buffer) const
226 Path p(buffer->filePath());
228 if (lyxerr.debugging()) {
229 lyxerr << "Executing '" << s << "' in '"
230 << buffer->filePath() << "'" << endl;
232 one.startscript(Systemcall::Wait, s);
236 string const InsetExternal::doSubstitution(Buffer const * buffer,
237 string const & s) const
240 string const basename = ChangeExtension(params_.filename, string());
242 if (buffer && !buffer->tmppath.empty() && !buffer->niceFile) {
243 filepath = buffer->filePath();
245 result = subst(s, "$$FName", params_.filename);
246 result = subst(result, "$$Basename", basename);
247 result = subst(result, "$$Parameters", params_.parameters);
248 result = subst(result, "$$FPath", filepath);
249 result = ReplaceEnvironmentPath(result);
250 result = subst(result, "$$Tempname", tempname_);
251 result = subst(result, "$$Sysdir", system_lyxdir);
253 // Handle the $$Contents(filename) syntax
254 if (contains(result, "$$Contents(\"")) {
256 string::size_type const pos = result.find("$$Contents(\"");
257 string::size_type const end = result.find("\")", pos);
258 string const file = result.substr(pos + 12, end - (pos + 12));
261 // Make sure we are in the directory of the buffer
262 Path p(buffer->filePath());
263 contents = GetFileContents(file);
265 contents = GetFileContents(file);
267 result = subst(result,
268 ("$$Contents(\"" + file + "\")").c_str(),
276 void InsetExternal::updateExternal() const
278 updateExternal("LaTeX", view_->buffer());
281 void InsetExternal::updateExternal(string const & format,
282 Buffer const * buf) const
284 ExternalTemplate const & et = params_.templ;
285 ExternalTemplate::Formats::const_iterator cit =
286 et.formats.find(format);
288 if (cit == et.formats.end() ||
289 cit->second.updateCommand.empty() ||
290 !et.automaticProduction)
293 if (!cit->second.updateResult.empty()) {
294 string const resultfile = doSubstitution(buf,
295 cit->second.updateResult);
296 FileInfo fi(params_.filename);
297 FileInfo fi2(resultfile);
298 if (fi2.exist() && fi.exist() &&
299 difftime(fi2.getModificationTime(),
300 fi.getModificationTime()) >= 0) {
301 lyxerr[Debug::FILES] << resultfile
302 << " is up to date" << endl;
307 executeCommand(doSubstitution(buf, cit->second.updateCommand), buf);
311 void InsetExternal::viewExternal() const
313 ExternalTemplate const & et = params_.templ;
314 if (et.viewCommand.empty())
318 executeCommand(doSubstitution(view_->buffer(),
324 void InsetExternal::editExternal() const
326 ExternalTemplate const & et = params_.templ;
327 if (et.editCommand.empty())
331 executeCommand(doSubstitution(view_->buffer(),
337 bool operator==(InsetExternal::Params const & left,
338 InsetExternal::Params const & right)
340 return ((left.filename == right.filename) &&
341 (left.parameters == right.parameters) &&
342 (left.templ.lyxName == right.templ.lyxName));
346 bool operator!=(InsetExternal::Params const & left,
347 InsetExternal::Params const & right)
349 return !(left == right);