7 #include "insetinclude.h"
9 #include "bufferlist.h"
10 #include "BufferView.h"
14 #include "LaTeXFeatures.h"
16 #include "lyxtextclasslist.h"
18 #include "frontends/Dialogs.h"
20 #include "support/filetools.h"
21 #include "support/FileInfo.h"
22 #include "support/lstrings.h"
32 extern BufferList bufferlist;
36 string const unique_id()
38 static unsigned int seed = 1000;
41 ost << "file" << ++seed;
43 // Needed if we use lyxstring.
44 return ost.str().c_str();
50 InsetInclude::InsetInclude(Params const & p)
51 : params_(p), include_label(unique_id())
55 InsetInclude::InsetInclude(InsetCommandParams const & p, Buffer const & b)
58 params_.masterFilename_ = b.fileName();
59 include_label = unique_id();
63 InsetInclude::~InsetInclude()
69 InsetInclude::Params const & InsetInclude::params() const
75 bool InsetInclude::Params::operator==(Params const & o) const
77 if (cparams == o.cparams && flag == o.flag &&
78 noload == o.noload && masterFilename_ == o.masterFilename_)
85 bool InsetInclude::Params::operator!=(Params const & o) const
91 void InsetInclude::set(Params const & p)
98 switch (params_.flag) {
103 command="verbatiminput";
109 command="verbatiminput*";
113 params_.cparams.setCmdName(command);
117 Inset * InsetInclude::clone(Buffer const & buffer, bool) const
120 p.masterFilename_ = buffer.fileName();
122 return new InsetInclude(p);
126 void InsetInclude::edit(BufferView * bv, int, int, unsigned int)
128 bv->owner()->getDialogs()->showInclude(this);
132 void InsetInclude::edit(BufferView * bv, bool)
138 void InsetInclude::write(Buffer const *, ostream & os) const
140 os << "Include " << params_.cparams.getCommand() << "\n";
144 void InsetInclude::read(Buffer const *, LyXLex & lex)
146 params_.cparams.read(lex);
148 if (params_.cparams.getCmdName() == "include")
149 params_.flag = INCLUDE;
150 else if (params_.cparams.getCmdName() == "input")
151 params_.flag = INPUT;
152 /* FIXME: is this logic necessary now ? */
153 else if (contains(params_.cparams.getCmdName(), "verbatim")) {
155 if (params_.cparams.getCmdName() == "verbatiminput*")
156 params_.flag = VERBAST;
161 bool InsetInclude::display() const
163 return !(params_.flag == INPUT);
167 string const InsetInclude::getScreenLabel(Buffer const *) const
171 switch (params_.flag) {
172 case INPUT: temp += _("Input"); break;
173 case VERB: temp += _("Verbatim Input"); break;
174 case VERBAST: temp += _("Verbatim Input*"); break;
175 case INCLUDE: temp += _("Include"); break;
180 if (params_.cparams.getContents().empty())
183 temp += params_.cparams.getContents();
189 string const InsetInclude::getRelFileBaseName() const
191 return OnlyFilename(ChangeExtension(params_.cparams.getContents(), string()));
195 string const InsetInclude::getFileName() const
197 return MakeAbsPath(params_.cparams.getContents(),
198 OnlyPath(getMasterFilename()));
202 string const InsetInclude::getMasterFilename() const
204 return params_.masterFilename_;
208 bool InsetInclude::loadIfNeeded() const
210 if (params_.noload || isVerbatim())
213 if (!IsLyXFilename(getFileName()))
216 if (bufferlist.exists(getFileName()))
219 // the readonly flag can/will be wrong, not anymore I think.
220 FileInfo finfo(getFileName());
221 bool const ro = !(!finfo.isOK() || finfo.writable());
222 return bufferlist.readFile(getFileName(), ro) != 0;
226 int InsetInclude::latex(Buffer const * buffer, ostream & os,
227 bool /*fragile*/, bool /*fs*/) const
229 string incfile(params_.cparams.getContents());
231 // Do nothing if no file name has been specified
235 if (loadIfNeeded()) {
236 Buffer * tmp = bufferlist.getBuffer(getFileName());
238 // FIXME: this should be a GUI warning
239 if (tmp->params.textclass != buffer->params.textclass) {
240 lyxerr << "WARNING: Included file `"
241 << MakeDisplayPath(getFileName())
242 << "' has textclass `"
243 << textclasslist.NameOfClass(tmp->params.textclass)
244 << "' while parent file has textclass `"
245 << textclasslist.NameOfClass(buffer->params.textclass)
250 // write it to a file (so far the complete file)
251 string writefile = ChangeExtension(getFileName(), ".tex");
253 if (!buffer->tmppath.empty()
254 && !buffer->niceFile) {
255 incfile = subst(incfile, '/','@');
257 incfile = subst(incfile, ':', '$');
259 writefile = AddName(buffer->tmppath, incfile);
261 writefile = getFileName();
262 writefile = ChangeExtension(writefile, ".tex");
263 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
264 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
266 tmp->markDepClean(buffer->tmppath);
268 tmp->makeLaTeXFile(writefile,
269 OnlyPath(getMasterFilename()),
270 buffer->niceFile, true);
274 os << '\\' << params_.cparams.getCmdName() << '{' << incfile << '}';
275 } else if (params_.flag == INPUT) {
276 // \input wants file with extension (default is .tex)
277 if (!IsLyXFilename(getFileName())) {
278 os << '\\' << params_.cparams.getCmdName() << '{' << incfile << '}';
280 os << '\\' << params_.cparams.getCmdName() << '{'
281 << ChangeExtension(incfile, ".tex")
285 // \include don't want extension and demands that the
286 // file really have .tex
287 os << '\\' << params_.cparams.getCmdName() << '{'
288 << ChangeExtension(incfile, string())
296 int InsetInclude::ascii(Buffer const *, std::ostream & os, int) const
299 os << GetFileContents(getFileName());
304 int InsetInclude::linuxdoc(Buffer const * buffer, ostream & os) const
306 string incfile(params_.cparams.getContents());
308 // Do nothing if no file name has been specified
312 if (loadIfNeeded()) {
313 Buffer * tmp = bufferlist.getBuffer(getFileName());
315 // write it to a file (so far the complete file)
316 string writefile = ChangeExtension(getFileName(), ".sgml");
317 if (!buffer->tmppath.empty() && !buffer->niceFile) {
318 incfile = subst(incfile, '/','@');
319 writefile = AddName(buffer->tmppath, incfile);
321 writefile = getFileName();
323 if (IsLyXFilename(getFileName()))
324 writefile = ChangeExtension(writefile, ".sgml");
326 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
327 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
329 tmp->makeLinuxDocFile(writefile, buffer->niceFile, true);
333 os << "<inlinegraphic fileref=\"" << '&' << include_label << ';'
334 << "\" format=\"linespecific\">"
335 << "</inlinegraphic>";
337 os << '&' << include_label << ';';
343 int InsetInclude::docbook(Buffer const * buffer, ostream & os) const
345 string incfile(params_.cparams.getContents());
347 // Do nothing if no file name has been specified
351 if (loadIfNeeded()) {
352 Buffer * tmp = bufferlist.getBuffer(getFileName());
354 // write it to a file (so far the complete file)
355 string writefile = ChangeExtension(getFileName(), ".sgml");
356 if (!buffer->tmppath.empty() && !buffer->niceFile) {
357 incfile = subst(incfile, '/','@');
358 writefile = AddName(buffer->tmppath, incfile);
360 writefile = getFileName();
361 if (IsLyXFilename(getFileName()))
362 writefile = ChangeExtension(writefile, ".sgml");
364 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
365 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
367 tmp->makeDocBookFile(writefile, buffer->niceFile, true);
371 os << "<inlinegraphic fileref=\"" << '&' << include_label << ';'
372 << "\" format=\"linespecific\">"
373 << "</inlinegraphic>";
375 os << '&' << include_label << ';';
381 void InsetInclude::validate(LaTeXFeatures & features) const
384 string incfile(params_.cparams.getContents());
387 Buffer const * const b = bufferlist.getBuffer(getMasterFilename());
389 if (b && !b->tmppath.empty() && b->niceFile) {
390 incfile = subst(incfile, '/','@');
391 writefile = AddName(b->tmppath, incfile);
393 writefile = getFileName();
395 if (IsLyXFilename(getFileName()))
396 writefile = ChangeExtension(writefile, ".sgml");
398 features.includeFile(include_label, writefile);
401 features.require("verbatim");
403 // Here we must do the fun stuff...
404 // Load the file in the include if it needs
406 if (loadIfNeeded()) {
408 Buffer const * const tmp = bufferlist.getBuffer(getFileName());
410 tmp->validate(features);
415 vector<string> const InsetInclude::getLabelList() const
419 if (loadIfNeeded()) {
420 Buffer * tmp = bufferlist.getBuffer(getFileName());
421 tmp->setParentName("");
422 l = tmp->getLabelList();
423 tmp->setParentName(getMasterFilename());
430 vector<pair<string,string> > const InsetInclude::getKeys() const
432 vector<pair<string,string> > keys;
434 if (loadIfNeeded()) {
435 Buffer * tmp = bufferlist.getBuffer(getFileName());
436 tmp->setParentName("");
437 keys = tmp->getBibkeyList();
438 tmp->setParentName(getMasterFilename());