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"
29 extern BufferList bufferlist;
33 string const unique_id()
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();
47 InsetInclude::InsetInclude(Params const & p)
48 : params_(p), include_label(unique_id())
52 InsetInclude::InsetInclude(InsetCommandParams const & p, Buffer const & b)
55 params_.masterFilename_ = b.fileName();
56 include_label = unique_id();
60 InsetInclude::~InsetInclude()
66 InsetInclude::Params const & InsetInclude::params() const
72 bool InsetInclude::Params::operator==(Params const & o) const
74 if (cparams == o.cparams && flag == o.flag &&
75 noload == o.noload && masterFilename_ == o.masterFilename_)
82 bool InsetInclude::Params::operator!=(Params const & o) const
88 void InsetInclude::set(Params const & p)
95 switch (params_.flag) {
100 command="verbatiminput";
106 command="verbatiminput*";
110 params_.cparams.setCmdName(command);
114 Inset * InsetInclude::Clone(Buffer const & buffer) const
117 p.masterFilename_ = buffer.fileName();
119 return new InsetInclude(p);
123 void InsetInclude::Edit(BufferView * bv, int, int, unsigned int)
125 bv->owner()->getDialogs()->showInclude(this);
129 void InsetInclude::Write(Buffer const *, ostream & os) const
131 os << "Include " << params_.cparams.getCommand() << "\n";
135 void InsetInclude::Read(Buffer const *, LyXLex & lex)
137 params_.cparams.Read(lex);
139 if (params_.cparams.getCmdName() == "include")
140 params_.flag = INCLUDE;
141 else if (params_.cparams.getCmdName() == "input")
142 params_.flag = INPUT;
143 /* FIXME: is this logic necessary now ? */
144 else if (contains(params_.cparams.getCmdName(), "verbatim")) {
146 if (params_.cparams.getCmdName() == "verbatiminput*")
147 params_.flag = VERBAST;
152 bool InsetInclude::display() const
154 return !(params_.flag == INPUT);
158 string const InsetInclude::getScreenLabel() const
162 switch (params_.flag) {
163 case INPUT: temp += _("Input"); break;
164 case VERB: temp += _("Verbatim Input"); break;
165 case VERBAST: temp += _("Verbatim Input*"); break;
166 case INCLUDE: temp += _("Include"); break;
171 if (params_.cparams.getContents().empty())
174 temp += params_.cparams.getContents();
180 string const InsetInclude::getRelFileBaseName() const
182 return OnlyFilename(ChangeExtension(params_.cparams.getContents(), string()));
186 string const InsetInclude::getFileName() const
188 return MakeAbsPath(params_.cparams.getContents(),
189 OnlyPath(getMasterFilename()));
193 string const InsetInclude::getMasterFilename() const
195 return params_.masterFilename_;
199 bool InsetInclude::loadIfNeeded() const
201 if (params_.noload || isVerbatim())
204 if (!IsLyXFilename(getFileName()))
207 if (bufferlist.exists(getFileName()))
210 // the readonly flag can/will be wrong, not anymore I think.
211 FileInfo finfo(getFileName());
212 bool const ro = !finfo.writable();
213 return bufferlist.readFile(getFileName(), ro) != 0;
217 int InsetInclude::Latex(Buffer const * buffer, ostream & os,
218 bool /*fragile*/, bool /*fs*/) const
220 string incfile(params_.cparams.getContents());
222 // Do nothing if no file name has been specified
226 if (loadIfNeeded()) {
227 Buffer * tmp = bufferlist.getBuffer(getFileName());
229 // FIXME: this should be a GUI warning
230 if (tmp->params.textclass != buffer->params.textclass) {
231 lyxerr << "WARNING: Included file `"
232 << MakeDisplayPath(getFileName())
233 << "' has textclass `"
234 << textclasslist.NameOfClass(tmp->params.textclass)
235 << "' while parent file has textclass `"
236 << textclasslist.NameOfClass(buffer->params.textclass)
241 // write it to a file (so far the complete file)
242 string writefile = ChangeExtension(getFileName(), ".tex");
244 if (!buffer->tmppath.empty()
245 && !buffer->niceFile) {
246 incfile = subst(incfile, '/','@');
248 incfile = subst(incfile, ':', '$');
250 writefile = AddName(buffer->tmppath, incfile);
252 writefile = getFileName();
253 writefile = ChangeExtension(writefile, ".tex");
254 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
255 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
257 tmp->markDepClean(buffer->tmppath);
259 tmp->makeLaTeXFile(writefile,
260 OnlyPath(getMasterFilename()),
261 buffer->niceFile, true);
265 os << '\\' << params_.cparams.getCmdName() << '{' << incfile << '}';
266 } else if (params_.flag == INPUT) {
267 // \input wants file with extension (default is .tex)
268 if (!IsLyXFilename(getFileName())) {
269 os << '\\' << params_.cparams.getCmdName() << '{' << incfile << '}';
271 os << '\\' << params_.cparams.getCmdName() << '{'
272 << ChangeExtension(incfile, ".tex")
276 // \include don't want extension and demands that the
277 // file really have .tex
278 os << '\\' << params_.cparams.getCmdName() << '{'
279 << ChangeExtension(incfile, string())
287 int InsetInclude::Ascii(Buffer const *, std::ostream & os, int) const
290 os << GetFileContents(getFileName());
295 int InsetInclude::Linuxdoc(Buffer const * buffer, ostream & os) const
297 string incfile(params_.cparams.getContents());
299 // Do nothing if no file name has been specified
303 if (loadIfNeeded()) {
304 Buffer * tmp = bufferlist.getBuffer(getFileName());
306 // write it to a file (so far the complete file)
307 string writefile = ChangeExtension(getFileName(), ".sgml");
308 if (!buffer->tmppath.empty() && !buffer->niceFile) {
309 incfile = subst(incfile, '/','@');
310 writefile = AddName(buffer->tmppath, incfile);
312 writefile = getFileName();
314 if (IsLyXFilename(getFileName()))
315 writefile = ChangeExtension(writefile, ".sgml");
317 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
318 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
320 tmp->makeLinuxDocFile(writefile, buffer->niceFile, true);
324 os << "<inlinegraphic fileref=\"" << '&' << include_label << ';'
325 << "\" format=\"linespecific\">"
326 << "</inlinegraphic>";
328 os << '&' << include_label << ';';
334 int InsetInclude::DocBook(Buffer const * buffer, ostream & os) const
336 string incfile(params_.cparams.getContents());
338 // Do nothing if no file name has been specified
342 if (loadIfNeeded()) {
343 Buffer * tmp = bufferlist.getBuffer(getFileName());
345 // write it to a file (so far the complete file)
346 string writefile = ChangeExtension(getFileName(), ".sgml");
347 if (!buffer->tmppath.empty() && !buffer->niceFile) {
348 incfile = subst(incfile, '/','@');
349 writefile = AddName(buffer->tmppath, incfile);
351 writefile = getFileName();
352 if (IsLyXFilename(getFileName()))
353 writefile = ChangeExtension(writefile, ".sgml");
355 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
356 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
358 tmp->makeDocBookFile(writefile, buffer->niceFile, true);
362 os << "<inlinegraphic fileref=\"" << '&' << include_label << ';'
363 << "\" format=\"linespecific\">"
364 << "</inlinegraphic>";
366 os << '&' << include_label << ';';
372 void InsetInclude::Validate(LaTeXFeatures & features) const
375 string incfile(params_.cparams.getContents());
378 Buffer const * const b = bufferlist.getBuffer(getMasterFilename());
380 if (b && !b->tmppath.empty() && b->niceFile) {
381 incfile = subst(incfile, '/','@');
382 writefile = AddName(b->tmppath, incfile);
384 writefile = getFileName();
386 if (IsLyXFilename(getFileName()))
387 writefile = ChangeExtension(writefile, ".sgml");
389 features.IncludedFiles[include_label] = writefile;
392 features.verbatim = true;
394 // Here we must do the fun stuff...
395 // Load the file in the include if it needs
397 if (loadIfNeeded()) {
399 Buffer const * const tmp = bufferlist.getBuffer(getFileName());
401 tmp->validate(features);
406 vector<string> const InsetInclude::getLabelList() const
410 if (loadIfNeeded()) {
411 Buffer * tmp = bufferlist.getBuffer(getFileName());
412 tmp->setParentName("");
413 l = tmp->getLabelList();
414 tmp->setParentName(getMasterFilename());
421 vector<pair<string,string> > const InsetInclude::getKeys() const
423 vector<pair<string,string> > keys;
425 if (loadIfNeeded()) {
426 Buffer * tmp = bufferlist.getBuffer(getFileName());
427 tmp->setParentName("");
428 keys = tmp->getBibkeyList();
429 tmp->setParentName(getMasterFilename());