1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2000 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"
35 InsetExternal::InsetExternal()
38 tempname = lyx::tempName(string(), "lyxext");
39 //ExternalTemplateManager::Templates::const_iterator i1;
40 params_.templ = ExternalTemplateManager::get().getTemplates().begin()->second;
44 InsetExternal::~InsetExternal()
46 lyx::unlink(tempname);
51 InsetExternal::Params InsetExternal::params() const
57 void InsetExternal::setFromParams(Params const & p)
59 params_.filename = p.filename;
60 params_.parameters = p.parameters;
61 params_.templ = p.templ;
65 string const InsetExternal::EditMessage() const
67 return doSubstitution(0, params_.templ.guiName);
71 void InsetExternal::Edit(BufferView * bv,
72 int /*x*/, int /*y*/, unsigned int /*button*/)
75 view->owner()->getDialogs()->showExternal(this);
79 void InsetExternal::Write(Buffer const *, std::ostream & os) const
81 os << "External " << params_.templ.lyxName << ",\"" << params_.filename
82 << "\",\"" << params_.parameters << "\"\n";
86 void InsetExternal::Read(Buffer const *, LyXLex & lex)
91 // Read inset data from lex and store in format
93 format = lex.GetString();
95 lex.printError("InsetExternal: Parse error: `$$Token'");
98 token = lex.GetString();
99 if (token == "\\end_inset")
102 if (token != "\\end_inset") {
103 lex.printError("Missing \\end_inset at this point. "
107 // Parse string format...
108 string::size_type const pos1 = format.find(",");
109 params_.templ = ExternalTemplateManager::get().getTemplateByName(format.substr(0, pos1));
110 string::size_type const pos2 = format.find("\",\"", pos1);
111 params_.filename = format.substr(pos1 + 2, pos2 - (pos1 + 2));
112 params_.parameters = format.substr(pos2 + 3, format.length() - (pos2 + 4));
114 lyxerr[Debug::INFO] << "InsetExternal::Read: " << params_.templ.lyxName
115 << " " << params_.filename
116 << " " << params_.parameters << endl;
120 int InsetExternal::write(string const & format,
121 Buffer const * buf, std::ostream & os) const
123 ExternalTemplate const & et = params_.templ;
124 ExternalTemplate::Formats::const_iterator cit =
125 et.formats.find(format);
126 if (cit == et.formats.end()) {
127 lyxerr << "External template format '" << format
128 << "' not specified in template " << params_.templ.lyxName
133 if (et.automaticProduction) {
134 executeCommand(doSubstitution(buf,
135 (*cit).second.updateCommand),
139 os << doSubstitution(buf, (*cit).second.product);
140 return 0; // CHECK (FIXME check what ? - jbl)
144 int InsetExternal::Latex(Buffer const * buf,
145 std::ostream & os, bool, bool) const
147 return write("LaTeX", buf, os);
151 int InsetExternal::Ascii(Buffer const * buf, std::ostream & os, int) const
153 return write("Ascii", buf, os);
157 int InsetExternal::Linuxdoc(Buffer const * buf, std::ostream & os) const
159 return write("LinuxDoc", buf, os);
163 int InsetExternal::DocBook(Buffer const * buf, std::ostream & os) const
165 return write("DocBook", buf, os);
169 void InsetExternal::Validate(LaTeXFeatures & features) const
171 ExternalTemplate const & et = params_.templ;
172 ExternalTemplate::Formats::const_iterator cit =
173 et.formats.find("LaTeX");
175 if (cit == et.formats.end())
178 if (!(*cit).second.requirement.empty()) {
179 features.require((*cit).second.requirement);
181 if (!(*cit).second.preamble.empty()) {
182 features.externalPreambles += (*cit).second.preamble + "\n";
187 Inset * InsetExternal::Clone(Buffer const &) const
189 InsetExternal * inset = new InsetExternal();
190 inset->params_ = params_;
195 string const InsetExternal::getScreenLabel() const
197 ExternalTemplate const & et = params_.templ;
198 if (et.guiName.empty())
199 return _("External");
201 return doSubstitution(0, et.guiName);
205 void InsetExternal::executeCommand(string const & s,
206 Buffer const * buffer) const
208 string buf = MakeAbsPath(buffer->fileName());
209 string path = OnlyPath(buf);
212 if (lyxerr.debugging()) {
213 lyxerr << "Executing '" << s << "' in '"
214 << path << "'" << endl;
216 one.startscript(Systemcalls::Wait, s);
220 string const InsetExternal::doSubstitution(Buffer const * buffer,
221 string const & s) const
224 string const basename = ChangeExtension(params_.filename, string());
225 result = subst(s, "$$FName", params_.filename);
226 result = subst(result, "$$Basename", basename);
227 result = subst(result, "$$Parameters", params_.parameters);
228 result = ReplaceEnvironmentPath(result);
229 result = subst(result, "$$Tempname", tempname);
230 result = subst(result, "$$Sysdir", system_lyxdir);
232 // Handle the $$Contents(filename) syntax
233 if (contains(result, "$$Contents(\"")) {
235 string::size_type const pos = result.find("$$Contents(\"");
236 string::size_type const end = result.find("\")", pos);
237 string const file = result.substr(pos + 12, end - (pos + 12));
240 // Make sure we are in the directory of the buffer
241 string const buf = MakeAbsPath(buffer->fileName());
242 string const path = OnlyPath(buf);
244 contents = GetFileContents(file);
246 contents = GetFileContents(file);
248 result = subst(result,
249 ("$$Contents(\"" + file + "\")").c_str(),
257 void InsetExternal::updateExternal() const
259 ExternalTemplate const & et = params_.templ;
260 ExternalTemplate::Formats::const_iterator cit =
261 et.formats.find("LaTeX");
262 if (cit == et.formats.end())
265 executeCommand(doSubstitution(view->buffer(),
266 (*cit).second.updateCommand),
271 void InsetExternal::viewExternal() const
273 ExternalTemplate const & et = params_.templ;
274 if (et.automaticProduction)
277 executeCommand(doSubstitution(view->buffer(),
283 void InsetExternal::editExternal() const
285 ExternalTemplate const & et = params_.templ;
286 if (et.automaticProduction)
289 executeCommand(doSubstitution(view->buffer(),
295 bool operator==(InsetExternal::Params const & left,
296 InsetExternal::Params const & right)
298 return ((left.filename == right.filename) &&
299 (left.parameters == right.parameters) &&
300 (left.templ.lyxName == right.templ.lyxName));
304 bool operator!=(InsetExternal::Params const & left,
305 InsetExternal::Params const & right)
307 return !(left == right);