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'");
108 token = lex.getString();
109 if (token == "\\end_inset")
112 if (token != "\\end_inset") {
113 lex.printError("Missing \\end_inset at this point. "
117 // Parse string format...
118 string::size_type const pos1 = format.find(",");
119 params_.templ = ExternalTemplateManager::get().getTemplateByName(format.substr(0, pos1));
120 string::size_type const pos2 = format.find("\",\"", pos1);
121 params_.filename = format.substr(pos1 + 2, pos2 - (pos1 + 2));
122 params_.parameters = format.substr(pos2 + 3, format.length() - (pos2 + 4));
124 lyxerr[Debug::INFO] << "InsetExternal::Read: " << params_.templ.lyxName
125 << " " << params_.filename
126 << " " << params_.parameters << endl;
130 int InsetExternal::write(string const & format,
131 Buffer const * buf, std::ostream & os) const
133 ExternalTemplate const & et = params_.templ;
134 ExternalTemplate::Formats::const_iterator cit =
135 et.formats.find(format);
136 if (cit == et.formats.end()) {
137 lyxerr << "External template format '" << format
138 << "' not specified in template " << params_.templ.lyxName
143 if (et.automaticProduction) {
144 executeCommand(doSubstitution(buf,
145 cit->second.updateCommand),
149 os << doSubstitution(buf, cit->second.product);
150 return 0; // CHECK (FIXME check what ? - jbl)
154 int InsetExternal::latex(Buffer const * buf,
155 std::ostream & os, bool, bool) const
157 return write("LaTeX", buf, os);
161 int InsetExternal::ascii(Buffer const * buf, std::ostream & os, int) const
163 return write("Ascii", buf, os);
167 int InsetExternal::linuxdoc(Buffer const * buf, std::ostream & os) const
169 return write("LinuxDoc", buf, os);
173 int InsetExternal::docbook(Buffer const * buf, std::ostream & os) const
175 return write("DocBook", buf, os);
179 void InsetExternal::validate(LaTeXFeatures & features) const
181 ExternalTemplate const & et = params_.templ;
182 ExternalTemplate::Formats::const_iterator cit =
183 et.formats.find("LaTeX");
185 if (cit == et.formats.end())
188 if (!cit->second.requirement.empty()) {
189 features.require(cit->second.requirement);
191 if (!cit->second.preamble.empty()) {
192 features.externalPreambles += cit->second.preamble + "\n";
197 Inset * InsetExternal::clone(Buffer const &, bool same_id) const
199 InsetExternal * inset = new InsetExternal();
200 inset->params_ = params_;
201 inset->view_ = view_;
208 string const InsetExternal::getScreenLabel(Buffer const *) const
210 ExternalTemplate const & et = params_.templ;
211 if (et.guiName.empty())
212 return _("External");
214 return doSubstitution(0, et.guiName);
218 void InsetExternal::executeCommand(string const & s,
219 Buffer const * buffer) const
221 string buf = MakeAbsPath(buffer->fileName());
222 string path = OnlyPath(buf);
225 if (lyxerr.debugging()) {
226 lyxerr << "Executing '" << s << "' in '"
227 << path << "'" << endl;
229 one.startscript(Systemcalls::Wait, s);
233 string const InsetExternal::doSubstitution(Buffer const * buffer,
234 string const & s) const
237 string const basename = ChangeExtension(params_.filename, string());
238 result = subst(s, "$$FName", params_.filename);
239 result = subst(result, "$$Basename", basename);
240 result = subst(result, "$$Parameters", params_.parameters);
241 result = ReplaceEnvironmentPath(result);
242 result = subst(result, "$$Tempname", tempname_);
243 result = subst(result, "$$Sysdir", system_lyxdir);
245 // Handle the $$Contents(filename) syntax
246 if (contains(result, "$$Contents(\"")) {
248 string::size_type const pos = result.find("$$Contents(\"");
249 string::size_type const end = result.find("\")", pos);
250 string const file = result.substr(pos + 12, end - (pos + 12));
253 // Make sure we are in the directory of the buffer
254 string const buf = MakeAbsPath(buffer->fileName());
255 string const path = OnlyPath(buf);
257 contents = GetFileContents(file);
259 contents = GetFileContents(file);
261 result = subst(result,
262 ("$$Contents(\"" + file + "\")").c_str(),
270 void InsetExternal::updateExternal() const
272 ExternalTemplate const & et = params_.templ;
273 ExternalTemplate::Formats::const_iterator cit =
274 et.formats.find("LaTeX");
275 if (cit == et.formats.end())
278 executeCommand(doSubstitution(view_->buffer(),
279 cit->second.updateCommand),
284 void InsetExternal::viewExternal() const
286 ExternalTemplate const & et = params_.templ;
287 if (et.automaticProduction)
290 executeCommand(doSubstitution(view_->buffer(),
296 void InsetExternal::editExternal() const
298 ExternalTemplate const & et = params_.templ;
299 if (et.automaticProduction)
302 executeCommand(doSubstitution(view_->buffer(),
308 bool operator==(InsetExternal::Params const & left,
309 InsetExternal::Params const & right)
311 return ((left.filename == right.filename) &&
312 (left.parameters == right.parameters) &&
313 (left.templ.lyxName == right.templ.lyxName));
317 bool operator!=(InsetExternal::Params const & left,
318 InsetExternal::Params const & right)
320 return !(left == right);