7 #include "insetinclude.h"
9 #include "bufferlist.h"
10 #include "BufferView.h"
13 #include "frontends/LyXView.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)
56 : include_label(unique_id())
59 params_.masterFilename_ = b.fileName();
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());
224 return bufferlist.readFile(getFileName(), !finfo.writable()) != 0;
228 int InsetInclude::latex(Buffer const * buffer, ostream & os,
229 bool /*fragile*/, bool /*fs*/) const
231 string incfile(params_.cparams.getContents());
233 // Do nothing if no file name has been specified
237 if (loadIfNeeded()) {
238 Buffer * tmp = bufferlist.getBuffer(getFileName());
240 // FIXME: this should be a GUI warning
241 if (tmp->params.textclass != buffer->params.textclass) {
242 lyxerr << "WARNING: Included file `"
243 << MakeDisplayPath(getFileName())
244 << "' has textclass `"
245 << textclasslist[tmp->params.textclass].name()
246 << "' while parent file has textclass `"
247 << textclasslist[buffer->params.textclass].name()
252 // write it to a file (so far the complete file)
253 string writefile = ChangeExtension(getFileName(), ".tex");
255 if (!buffer->tmppath.empty()
256 && !buffer->niceFile) {
257 incfile = subst(incfile, '/','@');
259 incfile = subst(incfile, ':', '$');
261 writefile = AddName(buffer->tmppath, incfile);
263 writefile = getFileName();
264 writefile = ChangeExtension(writefile, ".tex");
265 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
266 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
268 tmp->markDepClean(buffer->tmppath);
270 tmp->makeLaTeXFile(writefile,
271 OnlyPath(getMasterFilename()),
272 buffer->niceFile, true);
276 os << '\\' << params_.cparams.getCmdName() << '{' << incfile << '}';
277 } else if (params_.flag == INPUT) {
278 // \input wants file with extension (default is .tex)
279 if (!IsLyXFilename(getFileName())) {
280 os << '\\' << params_.cparams.getCmdName() << '{' << incfile << '}';
282 os << '\\' << params_.cparams.getCmdName() << '{'
283 << ChangeExtension(incfile, ".tex")
287 // \include don't want extension and demands that the
288 // file really have .tex
289 os << '\\' << params_.cparams.getCmdName() << '{'
290 << ChangeExtension(incfile, string())
298 int InsetInclude::ascii(Buffer const *, ostream & os, int) const
301 os << GetFileContents(getFileName());
306 int InsetInclude::linuxdoc(Buffer const * buffer, ostream & os) const
308 string incfile(params_.cparams.getContents());
310 // Do nothing if no file name has been specified
314 if (loadIfNeeded()) {
315 Buffer * tmp = bufferlist.getBuffer(getFileName());
317 // write it to a file (so far the complete file)
318 string writefile = ChangeExtension(getFileName(), ".sgml");
319 if (!buffer->tmppath.empty() && !buffer->niceFile) {
320 incfile = subst(incfile, '/','@');
321 writefile = AddName(buffer->tmppath, incfile);
323 writefile = getFileName();
325 if (IsLyXFilename(getFileName()))
326 writefile = ChangeExtension(writefile, ".sgml");
328 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
329 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
331 tmp->makeLinuxDocFile(writefile, buffer->niceFile, true);
336 << GetFileContents(getFileName())
339 os << '&' << include_label << ';';
345 int InsetInclude::docbook(Buffer const * buffer, ostream & os) 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());