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::write(Buffer const *, std::ostream & os) const
82 os << "External " << params_.templ.lyxName << ",\"" << params_.filename
83 << "\",\"" << params_.parameters << "\"\n";
87 void InsetExternal::read(Buffer const *, LyXLex & lex)
92 // Read inset data from lex and store in format
94 format = lex.GetString();
96 lex.printError("InsetExternal: Parse error: `$$Token'");
99 token = lex.GetString();
100 if (token == "\\end_inset")
103 if (token != "\\end_inset") {
104 lex.printError("Missing \\end_inset at this point. "
108 // Parse string format...
109 string::size_type const pos1 = format.find(",");
110 params_.templ = ExternalTemplateManager::get().getTemplateByName(format.substr(0, pos1));
111 string::size_type const pos2 = format.find("\",\"", pos1);
112 params_.filename = format.substr(pos1 + 2, pos2 - (pos1 + 2));
113 params_.parameters = format.substr(pos2 + 3, format.length() - (pos2 + 4));
115 lyxerr[Debug::INFO] << "InsetExternal::Read: " << params_.templ.lyxName
116 << " " << params_.filename
117 << " " << params_.parameters << endl;
121 int InsetExternal::write(string const & format,
122 Buffer const * buf, std::ostream & os) const
124 ExternalTemplate const & et = params_.templ;
125 ExternalTemplate::Formats::const_iterator cit =
126 et.formats.find(format);
127 if (cit == et.formats.end()) {
128 lyxerr << "External template format '" << format
129 << "' not specified in template " << params_.templ.lyxName
134 if (et.automaticProduction) {
135 executeCommand(doSubstitution(buf,
136 (*cit).second.updateCommand),
140 os << doSubstitution(buf, (*cit).second.product);
141 return 0; // CHECK (FIXME check what ? - jbl)
145 int InsetExternal::latex(Buffer const * buf,
146 std::ostream & os, bool, bool) const
148 return write("LaTeX", buf, os);
152 int InsetExternal::ascii(Buffer const * buf, std::ostream & os, int) const
154 return write("Ascii", buf, os);
158 int InsetExternal::linuxdoc(Buffer const * buf, std::ostream & os) const
160 return write("LinuxDoc", buf, os);
164 int InsetExternal::docBook(Buffer const * buf, std::ostream & os) const
166 return write("DocBook", buf, os);
170 void InsetExternal::validate(LaTeXFeatures & features) const
172 ExternalTemplate const & et = params_.templ;
173 ExternalTemplate::Formats::const_iterator cit =
174 et.formats.find("LaTeX");
176 if (cit == et.formats.end())
179 if (!(*cit).second.requirement.empty()) {
180 features.require((*cit).second.requirement);
182 if (!(*cit).second.preamble.empty()) {
183 features.externalPreambles += (*cit).second.preamble + "\n";
188 Inset * InsetExternal::clone(Buffer const &) const
190 InsetExternal * inset = new InsetExternal();
191 inset->params_ = params_;
192 inset->view_ = view_;
197 string const InsetExternal::getScreenLabel() const
199 ExternalTemplate const & et = params_.templ;
200 if (et.guiName.empty())
201 return _("External");
203 return doSubstitution(0, et.guiName);
207 void InsetExternal::executeCommand(string const & s,
208 Buffer const * buffer) const
210 string buf = MakeAbsPath(buffer->fileName());
211 string path = OnlyPath(buf);
214 if (lyxerr.debugging()) {
215 lyxerr << "Executing '" << s << "' in '"
216 << path << "'" << endl;
218 one.startscript(Systemcalls::Wait, s);
222 string const InsetExternal::doSubstitution(Buffer const * buffer,
223 string const & s) const
226 string const basename = ChangeExtension(params_.filename, string());
227 result = subst(s, "$$FName", params_.filename);
228 result = subst(result, "$$Basename", basename);
229 result = subst(result, "$$Parameters", params_.parameters);
230 result = ReplaceEnvironmentPath(result);
231 result = subst(result, "$$Tempname", tempname_);
232 result = subst(result, "$$Sysdir", system_lyxdir);
234 // Handle the $$Contents(filename) syntax
235 if (contains(result, "$$Contents(\"")) {
237 string::size_type const pos = result.find("$$Contents(\"");
238 string::size_type const end = result.find("\")", pos);
239 string const file = result.substr(pos + 12, end - (pos + 12));
242 // Make sure we are in the directory of the buffer
243 string const buf = MakeAbsPath(buffer->fileName());
244 string const path = OnlyPath(buf);
246 contents = GetFileContents(file);
248 contents = GetFileContents(file);
250 result = subst(result,
251 ("$$Contents(\"" + file + "\")").c_str(),
259 void InsetExternal::updateExternal() const
261 ExternalTemplate const & et = params_.templ;
262 ExternalTemplate::Formats::const_iterator cit =
263 et.formats.find("LaTeX");
264 if (cit == et.formats.end())
267 executeCommand(doSubstitution(view_->buffer(),
268 (*cit).second.updateCommand),
273 void InsetExternal::viewExternal() const
275 ExternalTemplate const & et = params_.templ;
276 if (et.automaticProduction)
279 executeCommand(doSubstitution(view_->buffer(),
285 void InsetExternal::editExternal() const
287 ExternalTemplate const & et = params_.templ;
288 if (et.automaticProduction)
291 executeCommand(doSubstitution(view_->buffer(),
297 bool operator==(InsetExternal::Params const & left,
298 InsetExternal::Params const & right)
300 return ((left.filename == right.filename) &&
301 (left.parameters == right.parameters) &&
302 (left.templ.lyxName == right.templ.lyxName));
306 bool operator!=(InsetExternal::Params const & left,
307 InsetExternal::Params const & right)
309 return !(left == right);