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"
37 InsetExternal::InsetExternal()
40 tempname_ = lyx::tempName(string(), "lyxext");
41 //ExternalTemplateManager::Templates::const_iterator i1;
42 params_.templ = ExternalTemplateManager::get().getTemplates().begin()->second;
46 InsetExternal::~InsetExternal()
48 lyx::unlink(tempname_);
53 InsetExternal::Params InsetExternal::params() const
59 void InsetExternal::setFromParams(Params const & p)
61 params_.filename = p.filename;
62 params_.parameters = p.parameters;
63 params_.templ = p.templ;
67 string const InsetExternal::editMessage() const
69 return doSubstitution(0, params_.templ.guiName);
73 void InsetExternal::edit(BufferView * bv,
74 int /*x*/, int /*y*/, unsigned int /*button*/)
77 view_->owner()->getDialogs()->showExternal(this);
81 void InsetExternal::edit(BufferView * bv, bool)
87 void InsetExternal::write(Buffer const *, std::ostream & os) const
89 os << "External " << params_.templ.lyxName << ",\"" << params_.filename
90 << "\",\"" << params_.parameters << "\"\n";
94 void InsetExternal::read(Buffer const *, LyXLex & lex)
99 // Read inset data from lex and store in format
101 format = lex.GetString();
103 lex.printError("InsetExternal: Parse error: `$$Token'");
106 token = lex.GetString();
107 if (token == "\\end_inset")
110 if (token != "\\end_inset") {
111 lex.printError("Missing \\end_inset at this point. "
115 // Parse string format...
116 string::size_type const pos1 = format.find(",");
117 params_.templ = ExternalTemplateManager::get().getTemplateByName(format.substr(0, pos1));
118 string::size_type const pos2 = format.find("\",\"", pos1);
119 params_.filename = format.substr(pos1 + 2, pos2 - (pos1 + 2));
120 params_.parameters = format.substr(pos2 + 3, format.length() - (pos2 + 4));
122 lyxerr[Debug::INFO] << "InsetExternal::Read: " << params_.templ.lyxName
123 << " " << params_.filename
124 << " " << params_.parameters << endl;
128 int InsetExternal::write(string const & format,
129 Buffer const * buf, std::ostream & os) const
131 ExternalTemplate const & et = params_.templ;
132 ExternalTemplate::Formats::const_iterator cit =
133 et.formats.find(format);
134 if (cit == et.formats.end()) {
135 lyxerr << "External template format '" << format
136 << "' not specified in template " << params_.templ.lyxName
141 if (et.automaticProduction) {
142 executeCommand(doSubstitution(buf,
143 cit->second.updateCommand),
147 os << doSubstitution(buf, cit->second.product);
148 return 0; // CHECK (FIXME check what ? - jbl)
152 int InsetExternal::latex(Buffer const * buf,
153 std::ostream & os, bool, bool) const
155 return write("LaTeX", buf, os);
159 int InsetExternal::ascii(Buffer const * buf, std::ostream & os, int) const
161 return write("Ascii", buf, os);
165 int InsetExternal::linuxdoc(Buffer const * buf, std::ostream & os) const
167 return write("LinuxDoc", buf, os);
171 int InsetExternal::docBook(Buffer const * buf, std::ostream & os) const
173 return write("DocBook", buf, os);
177 void InsetExternal::validate(LaTeXFeatures & features) const
179 ExternalTemplate const & et = params_.templ;
180 ExternalTemplate::Formats::const_iterator cit =
181 et.formats.find("LaTeX");
183 if (cit == et.formats.end())
186 if (!cit->second.requirement.empty()) {
187 features.require(cit->second.requirement);
189 if (!cit->second.preamble.empty()) {
190 features.externalPreambles += cit->second.preamble + "\n";
195 Inset * InsetExternal::clone(Buffer const &, bool same_id) const
197 InsetExternal * inset = new InsetExternal();
198 inset->params_ = params_;
199 inset->view_ = view_;
206 string const InsetExternal::getScreenLabel(Buffer const *) const
208 ExternalTemplate const & et = params_.templ;
209 if (et.guiName.empty())
210 return _("External");
212 return doSubstitution(0, et.guiName);
216 void InsetExternal::executeCommand(string const & s,
217 Buffer const * buffer) const
219 string buf = MakeAbsPath(buffer->fileName());
220 string path = OnlyPath(buf);
223 if (lyxerr.debugging()) {
224 lyxerr << "Executing '" << s << "' in '"
225 << path << "'" << endl;
227 one.startscript(Systemcalls::Wait, s);
231 string const InsetExternal::doSubstitution(Buffer const * buffer,
232 string const & s) const
235 string const basename = ChangeExtension(params_.filename, string());
236 result = subst(s, "$$FName", params_.filename);
237 result = subst(result, "$$Basename", basename);
238 result = subst(result, "$$Parameters", params_.parameters);
239 result = ReplaceEnvironmentPath(result);
240 result = subst(result, "$$Tempname", tempname_);
241 result = subst(result, "$$Sysdir", system_lyxdir);
243 // Handle the $$Contents(filename) syntax
244 if (contains(result, "$$Contents(\"")) {
246 string::size_type const pos = result.find("$$Contents(\"");
247 string::size_type const end = result.find("\")", pos);
248 string const file = result.substr(pos + 12, end - (pos + 12));
251 // Make sure we are in the directory of the buffer
252 string const buf = MakeAbsPath(buffer->fileName());
253 string const path = OnlyPath(buf);
255 contents = GetFileContents(file);
257 contents = GetFileContents(file);
259 result = subst(result,
260 ("$$Contents(\"" + file + "\")").c_str(),
268 void InsetExternal::updateExternal() const
270 ExternalTemplate const & et = params_.templ;
271 ExternalTemplate::Formats::const_iterator cit =
272 et.formats.find("LaTeX");
273 if (cit == et.formats.end())
276 executeCommand(doSubstitution(view_->buffer(),
277 cit->second.updateCommand),
282 void InsetExternal::viewExternal() const
284 ExternalTemplate const & et = params_.templ;
285 if (et.automaticProduction)
288 executeCommand(doSubstitution(view_->buffer(),
294 void InsetExternal::editExternal() const
296 ExternalTemplate const & et = params_.templ;
297 if (et.automaticProduction)
300 executeCommand(doSubstitution(view_->buffer(),
306 bool operator==(InsetExternal::Params const & left,
307 InsetExternal::Params const & right)
309 return ((left.filename == right.filename) &&
310 (left.parameters == right.parameters) &&
311 (left.templ.lyxName == right.templ.lyxName));
315 bool operator!=(InsetExternal::Params const & left,
316 InsetExternal::Params const & right)
318 return !(left == right);