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"
36 InsetExternal::InsetExternal()
39 tempname_ = lyx::tempName(string(), "lyxext");
40 //ExternalTemplateManager::Templates::const_iterator i1;
41 params_.templ = ExternalTemplateManager::get().getTemplates().begin()->second;
45 InsetExternal::~InsetExternal()
47 lyx::unlink(tempname_);
52 InsetExternal::Params InsetExternal::params() const
58 void InsetExternal::setFromParams(Params const & p)
60 params_.filename = p.filename;
61 params_.parameters = p.parameters;
62 params_.templ = p.templ;
66 string const InsetExternal::editMessage() const
68 return doSubstitution(0, params_.templ.guiName);
72 void InsetExternal::edit(BufferView * bv,
73 int /*x*/, int /*y*/, unsigned int /*button*/)
76 view_->owner()->getDialogs()->showExternal(this);
80 void InsetExternal::edit(BufferView * bv, bool)
86 void InsetExternal::write(Buffer const *, std::ostream & os) const
88 os << "External " << params_.templ.lyxName << ",\"" << params_.filename
89 << "\",\"" << params_.parameters << "\"\n";
93 void InsetExternal::read(Buffer const *, LyXLex & lex)
98 // Read inset data from lex and store in format
100 format = lex.GetString();
102 lex.printError("InsetExternal: Parse error: `$$Token'");
105 token = lex.GetString();
106 if (token == "\\end_inset")
109 if (token != "\\end_inset") {
110 lex.printError("Missing \\end_inset at this point. "
114 // Parse string format...
115 string::size_type const pos1 = format.find(",");
116 params_.templ = ExternalTemplateManager::get().getTemplateByName(format.substr(0, pos1));
117 string::size_type const pos2 = format.find("\",\"", pos1);
118 params_.filename = format.substr(pos1 + 2, pos2 - (pos1 + 2));
119 params_.parameters = format.substr(pos2 + 3, format.length() - (pos2 + 4));
121 lyxerr[Debug::INFO] << "InsetExternal::Read: " << params_.templ.lyxName
122 << " " << params_.filename
123 << " " << params_.parameters << endl;
127 int InsetExternal::write(string const & format,
128 Buffer const * buf, std::ostream & os) const
130 ExternalTemplate const & et = params_.templ;
131 ExternalTemplate::Formats::const_iterator cit =
132 et.formats.find(format);
133 if (cit == et.formats.end()) {
134 lyxerr << "External template format '" << format
135 << "' not specified in template " << params_.templ.lyxName
140 if (et.automaticProduction) {
141 executeCommand(doSubstitution(buf,
142 cit->second.updateCommand),
146 os << doSubstitution(buf, cit->second.product);
147 return 0; // CHECK (FIXME check what ? - jbl)
151 int InsetExternal::latex(Buffer const * buf,
152 std::ostream & os, bool, bool) const
154 return write("LaTeX", buf, os);
158 int InsetExternal::ascii(Buffer const * buf, std::ostream & os, int) const
160 return write("Ascii", buf, os);
164 int InsetExternal::linuxdoc(Buffer const * buf, std::ostream & os) const
166 return write("LinuxDoc", buf, os);
170 int InsetExternal::docBook(Buffer const * buf, std::ostream & os) const
172 return write("DocBook", buf, os);
176 void InsetExternal::validate(LaTeXFeatures & features) const
178 ExternalTemplate const & et = params_.templ;
179 ExternalTemplate::Formats::const_iterator cit =
180 et.formats.find("LaTeX");
182 if (cit == et.formats.end())
185 if (!cit->second.requirement.empty()) {
186 features.require(cit->second.requirement);
188 if (!cit->second.preamble.empty()) {
189 features.externalPreambles += cit->second.preamble + "\n";
194 Inset * InsetExternal::clone(Buffer const &, bool same_id) const
196 InsetExternal * inset = new InsetExternal();
197 inset->params_ = params_;
198 inset->view_ = view_;
205 string const InsetExternal::getScreenLabel(Buffer const *) const
207 ExternalTemplate const & et = params_.templ;
208 if (et.guiName.empty())
209 return _("External");
211 return doSubstitution(0, et.guiName);
215 void InsetExternal::executeCommand(string const & s,
216 Buffer const * buffer) const
218 string buf = MakeAbsPath(buffer->fileName());
219 string path = OnlyPath(buf);
222 if (lyxerr.debugging()) {
223 lyxerr << "Executing '" << s << "' in '"
224 << path << "'" << endl;
226 one.startscript(Systemcalls::Wait, s);
230 string const InsetExternal::doSubstitution(Buffer const * buffer,
231 string const & s) const
234 string const basename = ChangeExtension(params_.filename, string());
235 result = subst(s, "$$FName", params_.filename);
236 result = subst(result, "$$Basename", basename);
237 result = subst(result, "$$Parameters", params_.parameters);
238 result = ReplaceEnvironmentPath(result);
239 result = subst(result, "$$Tempname", tempname_);
240 result = subst(result, "$$Sysdir", system_lyxdir);
242 // Handle the $$Contents(filename) syntax
243 if (contains(result, "$$Contents(\"")) {
245 string::size_type const pos = result.find("$$Contents(\"");
246 string::size_type const end = result.find("\")", pos);
247 string const file = result.substr(pos + 12, end - (pos + 12));
250 // Make sure we are in the directory of the buffer
251 string const buf = MakeAbsPath(buffer->fileName());
252 string const path = OnlyPath(buf);
254 contents = GetFileContents(file);
256 contents = GetFileContents(file);
258 result = subst(result,
259 ("$$Contents(\"" + file + "\")").c_str(),
267 void InsetExternal::updateExternal() const
269 ExternalTemplate const & et = params_.templ;
270 ExternalTemplate::Formats::const_iterator cit =
271 et.formats.find("LaTeX");
272 if (cit == et.formats.end())
275 executeCommand(doSubstitution(view_->buffer(),
276 cit->second.updateCommand),
281 void InsetExternal::viewExternal() const
283 ExternalTemplate const & et = params_.templ;
284 if (et.automaticProduction)
287 executeCommand(doSubstitution(view_->buffer(),
293 void InsetExternal::editExternal() const
295 ExternalTemplate const & et = params_.templ;
296 if (et.automaticProduction)
299 executeCommand(doSubstitution(view_->buffer(),
305 bool operator==(InsetExternal::Params const & left,
306 InsetExternal::Params const & right)
308 return ((left.filename == right.filename) &&
309 (left.parameters == right.parameters) &&
310 (left.templ.lyxName == right.templ.lyxName));
314 bool operator!=(InsetExternal::Params const & left,
315 InsetExternal::Params const & right)
317 return !(left == right);