7 #include "insetinclude.h"
9 #include "bufferlist.h"
10 #include "BufferView.h"
13 #include "frontends/LyXView.h"
14 #include "LaTeXFeatures.h"
17 #include "frontends/Dialogs.h"
19 #include "support/filetools.h"
20 #include "support/FileInfo.h"
21 #include "support/lstrings.h"
31 extern BufferList bufferlist;
35 string const uniqueID()
37 static unsigned int seed = 1000;
40 ost << "file" << ++seed;
42 // Needed if we use lyxstring.
43 return ost.str().c_str();
49 InsetInclude::InsetInclude(Params const & p)
50 : params_(p), include_label(uniqueID())
54 InsetInclude::InsetInclude(InsetCommandParams const & p, Buffer const & b)
55 : include_label(uniqueID())
58 params_.masterFilename_ = b.fileName();
62 InsetInclude::~InsetInclude()
68 InsetInclude::Params const & InsetInclude::params() const
74 bool InsetInclude::Params::operator==(Params const & o) const
76 if (cparams == o.cparams && flag == o.flag &&
77 noload == o.noload && masterFilename_ == o.masterFilename_)
84 bool InsetInclude::Params::operator!=(Params const & o) const
90 void InsetInclude::set(Params const & p)
97 switch (params_.flag) {
102 command="verbatiminput";
108 command="verbatiminput*";
112 params_.cparams.setCmdName(command);
116 Inset * InsetInclude::clone(Buffer const & buffer, bool) const
119 p.masterFilename_ = buffer.fileName();
121 return new InsetInclude(p);
125 void InsetInclude::edit(BufferView * bv, int, int, mouse_button::state)
127 bv->owner()->getDialogs()->showInclude(this);
131 void InsetInclude::edit(BufferView * bv, bool)
133 edit(bv, 0, 0, mouse_button::none);
137 void InsetInclude::write(Buffer const *, ostream & os) const
139 os << "Include " << params_.cparams.getCommand() << "\n";
143 void InsetInclude::read(Buffer const *, LyXLex & lex)
145 params_.cparams.read(lex);
147 if (params_.cparams.getCmdName() == "include")
148 params_.flag = INCLUDE;
149 else if (params_.cparams.getCmdName() == "input")
150 params_.flag = INPUT;
151 /* FIXME: is this logic necessary now ? */
152 else if (contains(params_.cparams.getCmdName(), "verbatim")) {
154 if (params_.cparams.getCmdName() == "verbatiminput*")
155 params_.flag = VERBAST;
160 bool InsetInclude::display() const
162 return !(params_.flag == INPUT);
166 string const InsetInclude::getScreenLabel(Buffer const *) const
170 switch (params_.flag) {
171 case INPUT: temp += _("Input"); break;
172 case VERB: temp += _("Verbatim Input"); break;
173 case VERBAST: temp += _("Verbatim Input*"); break;
174 case INCLUDE: temp += _("Include"); break;
179 if (params_.cparams.getContents().empty())
182 temp += params_.cparams.getContents();
188 string const InsetInclude::getRelFileBaseName() const
190 return OnlyFilename(ChangeExtension(params_.cparams.getContents(), string()));
194 string const InsetInclude::getFileName() const
196 return MakeAbsPath(params_.cparams.getContents(),
197 OnlyPath(getMasterFilename()));
201 string const InsetInclude::getMasterFilename() const
203 return params_.masterFilename_;
207 bool InsetInclude::loadIfNeeded() const
209 if (params_.noload || isVerbatim())
212 if (!IsLyXFilename(getFileName()))
215 if (bufferlist.exists(getFileName()))
218 // the readonly flag can/will be wrong, not anymore I think.
219 FileInfo finfo(getFileName());
223 return bufferlist.readFile(getFileName(), !finfo.writable()) != 0;
227 int InsetInclude::latex(Buffer const * buffer, ostream & os,
228 bool /*fragile*/, bool /*fs*/) const
230 string incfile(params_.cparams.getContents());
232 // Do nothing if no file name has been specified
236 if (loadIfNeeded()) {
237 Buffer * tmp = bufferlist.getBuffer(getFileName());
239 // FIXME: this should be a GUI warning
240 if (tmp->params.textclass != buffer->params.textclass) {
241 lyxerr << "WARNING: Included file `"
242 << MakeDisplayPath(getFileName())
243 << "' has textclass `"
244 << tmp->params.getLyXTextClass().name()
245 << "' while parent file has textclass `"
246 << buffer->params.getLyXTextClass().name()
251 // write it to a file (so far the complete file)
252 string writefile = ChangeExtension(getFileName(), ".tex");
254 if (!buffer->tmppath.empty()
255 && !buffer->niceFile) {
256 incfile = subst(incfile, '/','@');
258 incfile = subst(incfile, ':', '$');
260 writefile = AddName(buffer->tmppath, incfile);
262 writefile = getFileName();
263 writefile = ChangeExtension(writefile, ".tex");
264 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
265 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
267 tmp->markDepClean(buffer->tmppath);
269 tmp->makeLaTeXFile(writefile,
270 OnlyPath(getMasterFilename()),
271 buffer->niceFile, true);
275 os << '\\' << params_.cparams.getCmdName() << '{' << incfile << '}';
276 } else if (params_.flag == INPUT) {
277 // \input wants file with extension (default is .tex)
278 if (!IsLyXFilename(getFileName())) {
279 os << '\\' << params_.cparams.getCmdName() << '{' << incfile << '}';
281 os << '\\' << params_.cparams.getCmdName() << '{'
282 << ChangeExtension(incfile, ".tex")
286 // \include don't want extension and demands that the
287 // file really have .tex
288 os << '\\' << params_.cparams.getCmdName() << '{'
289 << ChangeExtension(incfile, string())
297 int InsetInclude::ascii(Buffer const *, ostream & os, int) const
300 os << GetFileContents(getFileName());
305 int InsetInclude::linuxdoc(Buffer const * buffer, ostream & os) const
307 string incfile(params_.cparams.getContents());
309 // Do nothing if no file name has been specified
313 if (loadIfNeeded()) {
314 Buffer * tmp = bufferlist.getBuffer(getFileName());
316 // write it to a file (so far the complete file)
317 string writefile = ChangeExtension(getFileName(), ".sgml");
318 if (!buffer->tmppath.empty() && !buffer->niceFile) {
319 incfile = subst(incfile, '/','@');
320 writefile = AddName(buffer->tmppath, incfile);
322 writefile = getFileName();
324 if (IsLyXFilename(getFileName()))
325 writefile = ChangeExtension(writefile, ".sgml");
327 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
328 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
330 tmp->makeLinuxDocFile(writefile, buffer->niceFile, true);
335 << GetFileContents(getFileName())
338 os << '&' << include_label << ';';
344 int InsetInclude::docbook(Buffer const * buffer, ostream & os,
345 bool /*mixcont*/) const
347 string incfile(params_.cparams.getContents());
349 // Do nothing if no file name has been specified
353 if (loadIfNeeded()) {
354 Buffer * tmp = bufferlist.getBuffer(getFileName());
356 // write it to a file (so far the complete file)
357 string writefile = ChangeExtension(getFileName(), ".sgml");
358 if (!buffer->tmppath.empty() && !buffer->niceFile) {
359 incfile = subst(incfile, '/','@');
360 writefile = AddName(buffer->tmppath, incfile);
362 writefile = getFileName();
363 if (IsLyXFilename(getFileName()))
364 writefile = ChangeExtension(writefile, ".sgml");
366 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
367 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
369 tmp->makeDocBookFile(writefile, buffer->niceFile, true);
373 os << "<inlinegraphic fileref=\""
374 << '&' << include_label << ';'
375 << "\" format=\"linespecific\">";
377 os << '&' << include_label << ';';
383 void InsetInclude::validate(LaTeXFeatures & features) const
386 string incfile(params_.cparams.getContents());
389 Buffer const * const b = bufferlist.getBuffer(getMasterFilename());
391 if (b && !b->tmppath.empty() && !b->niceFile && !isVerbatim()) {
392 incfile = subst(incfile, '/','@');
393 writefile = AddName(b->tmppath, incfile);
395 writefile = getFileName();
397 if (IsLyXFilename(getFileName()))
398 writefile = ChangeExtension(writefile, ".sgml");
400 features.includeFile(include_label, writefile);
403 features.require("verbatim");
405 // Here we must do the fun stuff...
406 // Load the file in the include if it needs
408 if (loadIfNeeded()) {
410 Buffer * const tmp = bufferlist.getBuffer(getFileName());
413 tmp->niceFile = b->niceFile;
414 tmp->validate(features);
420 vector<string> const InsetInclude::getLabelList() const
424 if (loadIfNeeded()) {
425 Buffer * tmp = bufferlist.getBuffer(getFileName());
426 tmp->setParentName("");
427 l = tmp->getLabelList();
428 tmp->setParentName(getMasterFilename());
435 vector<pair<string,string> > const InsetInclude::getKeys() const
437 vector<pair<string,string> > keys;
439 if (loadIfNeeded()) {
440 Buffer * tmp = bufferlist.getBuffer(getFileName());
441 tmp->setParentName("");
442 keys = tmp->getBibkeyList();
443 tmp->setParentName(getMasterFilename());