10 #include "frontends/Dialogs.h"
12 #include "insetinclude.h"
14 #include "bufferlist.h"
16 #include "support/filetools.h"
19 #include "LaTeXFeatures.h"
21 #include "support/FileInfo.h"
30 extern BufferList bufferlist;
35 static unsigned int seed = 1000;
37 std::ostringstream ost;
38 ost << "file" << ++seed;
40 // Needed if we use lyxstring.
41 return ost.str().c_str();
45 InsetInclude::InsetInclude(InsetIncludeParams const & p)
47 include_label = unique_id();
49 params_.buffer = p.buffer;
53 InsetInclude::InsetInclude(InsetCommandParams const & p, Buffer const & b)
55 include_label = unique_id();
61 InsetInclude::~InsetInclude()
67 InsetInclude::InsetIncludeParams const & InsetInclude::params() const
73 void InsetInclude::setFromParams(InsetIncludeParams const & p)
75 params_.cparams.setContents(p.cparams.getContents());
76 params_.noload = p.noload;
77 if (params_.flag == p.flag)
80 params_.flag = p.flag;
84 switch (params_.flag) {
89 command="verbatiminput";
95 command="verbatiminput*";
99 params_.cparams.setCmdName(command);
103 Inset * InsetInclude::Clone(Buffer const & buffer) const
105 InsetIncludeParams p(params_);
108 return new InsetInclude (p);
112 void InsetInclude::Edit(BufferView * bv, int, int, unsigned int)
114 bv->owner()->getDialogs()->showInclude(this);
118 void InsetInclude::Write(Buffer const *, ostream & os) const
120 os << "Include " << params_.cparams.getCommand() << "\n";
124 void InsetInclude::Read(Buffer const *, LyXLex & lex)
126 params_.cparams.Read(lex);
128 if (params_.cparams.getCmdName() == "include")
129 params_.flag = INCLUDE;
130 else if (params_.cparams.getCmdName() == "input")
131 params_.flag = INPUT;
132 /* FIXME: is this logic necessary now ? */
133 else if (contains(params_.cparams.getCmdName(), "verbatim")) {
135 if (params_.cparams.getCmdName() == "verbatiminput*")
136 params_.flag = VERBAST;
141 bool InsetInclude::display() const
143 return !(params_.flag == INPUT);
147 string const InsetInclude::getScreenLabel() const
151 switch (params_.flag) {
152 case INPUT: temp += _("Input"); break;
153 case VERB: temp += _("Verbatim Input"); break;
154 case VERBAST: temp += _("Verbatim Input*"); break;
155 case INCLUDE: temp += _("Include"); break;
160 if (params_.cparams.getContents().empty())
163 temp += params_.cparams.getContents();
169 string const InsetInclude::getRelFileBaseName() const
171 return OnlyFilename(ChangeExtension(params_.cparams.getContents(), string()));
175 string const InsetInclude::getFileName() const
177 return MakeAbsPath(params_.cparams.getContents(),
178 OnlyPath(getMasterFilename()));
182 string const InsetInclude::getMasterFilename() const
184 return params_.buffer->fileName();
188 bool InsetInclude::loadIfNeeded() const
190 if (params_.noload || isVerbatim())
193 if (!IsLyXFilename(getFileName()))
196 if (bufferlist.exists(getFileName()))
199 // the readonly flag can/will be wrong, not anymore I think.
200 FileInfo finfo(getFileName());
201 bool const ro = !finfo.writable();
202 return bufferlist.readFile(getFileName(), ro) != 0;
206 int InsetInclude::Latex(Buffer const * buffer, ostream & os,
207 bool /*fragile*/, bool /*fs*/) const
209 string incfile(params_.cparams.getContents());
211 // Do nothing if no file name has been specified
215 if (loadIfNeeded()) {
216 Buffer * tmp = bufferlist.getBuffer(getFileName());
218 // FIXME: this should be a GUI warning
219 if (tmp->params.textclass != buffer->params.textclass) {
220 lyxerr << "WARNING: Included file `"
221 << MakeDisplayPath(getFileName())
222 << "' has textclass `"
223 << textclasslist.NameOfClass(tmp->params.textclass)
224 << "' while parent file has textclass `"
225 << textclasslist.NameOfClass(buffer->params.textclass)
230 // write it to a file (so far the complete file)
231 string writefile = ChangeExtension(getFileName(), ".tex");
233 if (!buffer->tmppath.empty()
234 && !buffer->niceFile) {
235 incfile = subst(incfile, '/','@');
237 incfile = subst(incfile, ':', '$');
239 writefile = AddName(buffer->tmppath, incfile);
241 writefile = getFileName();
242 writefile = ChangeExtension(writefile, ".tex");
243 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
244 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
246 tmp->markDepClean(buffer->tmppath);
248 tmp->makeLaTeXFile(writefile,
249 OnlyPath(getMasterFilename()),
250 buffer->niceFile, true);
254 os << '\\' << params_.cparams.getCmdName() << '{' << incfile << '}';
255 } else if (params_.flag == INPUT) {
256 // \input wants file with extension (default is .tex)
257 if (!IsLyXFilename(getFileName())) {
258 os << '\\' << params_.cparams.getCmdName() << '{' << incfile << '}';
260 os << '\\' << params_.cparams.getCmdName() << '{'
261 << ChangeExtension(incfile, ".tex")
265 // \include don't want extension and demands that the
266 // file really have .tex
267 os << '\\' << params_.cparams.getCmdName() << '{'
268 << ChangeExtension(incfile, string())
276 int InsetInclude::Ascii(Buffer const *, std::ostream & os, int) const
279 os << GetFileContents(getFileName());
284 int InsetInclude::Linuxdoc(Buffer const * buffer, ostream & os) const
286 string incfile(params_.cparams.getContents());
288 // Do nothing if no file name has been specified
292 if (loadIfNeeded()) {
293 Buffer * tmp = bufferlist.getBuffer(getFileName());
295 // write it to a file (so far the complete file)
296 string writefile = ChangeExtension(getFileName(), ".sgml");
297 if (!buffer->tmppath.empty() && !buffer->niceFile) {
298 incfile = subst(incfile, '/','@');
299 writefile = AddName(buffer->tmppath, incfile);
301 writefile = getFileName();
303 if (IsLyXFilename(getFileName()))
304 writefile = ChangeExtension(writefile, ".sgml");
306 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
307 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
309 tmp->makeLinuxDocFile(writefile, buffer->niceFile, true);
313 os << "<inlinegraphic fileref=\"" << '&' << include_label << ';'
314 << "\" format=\"linespecific\">"
315 << "</inlinegraphic>";
317 os << '&' << include_label << ';';
323 int InsetInclude::DocBook(Buffer const * buffer, ostream & os) const
325 string incfile(params_.cparams.getContents());
327 // Do nothing if no file name has been specified
331 if (loadIfNeeded()) {
332 Buffer * tmp = bufferlist.getBuffer(getFileName());
334 // write it to a file (so far the complete file)
335 string writefile = ChangeExtension(getFileName(), ".sgml");
336 if (!buffer->tmppath.empty() && !buffer->niceFile) {
337 incfile = subst(incfile, '/','@');
338 writefile = AddName(buffer->tmppath, incfile);
340 writefile = getFileName();
341 if (IsLyXFilename(getFileName()))
342 writefile = ChangeExtension(writefile, ".sgml");
344 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
345 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
347 tmp->makeDocBookFile(writefile, buffer->niceFile, true);
351 os << "<inlinegraphic fileref=\"" << '&' << include_label << ';'
352 << "\" format=\"linespecific\">"
353 << "</inlinegraphic>";
355 os << '&' << include_label << ';';
361 void InsetInclude::Validate(LaTeXFeatures & features) const
364 string incfile(params_.cparams.getContents());
367 Buffer const & b = *params_.buffer;
369 if (!b.tmppath.empty() && b.niceFile) {
370 incfile = subst(incfile, '/','@');
371 writefile = AddName(b.tmppath, incfile);
373 writefile = getFileName();
375 if (IsLyXFilename(getFileName()))
376 writefile = ChangeExtension(writefile, ".sgml");
378 features.IncludedFiles[include_label] = writefile;
381 features.verbatim = true;
383 // Here we must do the fun stuff...
384 // Load the file in the include if it needs
386 if (loadIfNeeded()) {
388 Buffer * tmp = bufferlist.getBuffer(getFileName());
389 tmp->validate(features);
394 vector<string> const InsetInclude::getLabelList() const
398 if (loadIfNeeded()) {
399 Buffer * tmp = bufferlist.getBuffer(getFileName());
400 tmp->setParentName("");
401 l = tmp->getLabelList();
402 tmp->setParentName(getMasterFilename());
409 vector<pair<string,string> > const InsetInclude::getKeys() const
411 vector<pair<string,string> > keys;
413 if (loadIfNeeded()) {
414 Buffer * tmp = bufferlist.getBuffer(getFileName());
415 tmp->setParentName("");
416 keys = tmp->getBibkeyList();
417 tmp->setParentName(getMasterFilename());