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 string const unique_id() {
36 static unsigned int seed = 1000;
38 std::ostringstream ost;
39 ost << "file" << ++seed;
41 // Needed if we use lyxstring.
42 return ost.str().c_str();
48 InsetInclude::InsetInclude(InsetIncludeParams const & p)
50 include_label = unique_id();
52 params_.buffer = p.buffer;
56 InsetInclude::InsetInclude(InsetCommandParams const & p, Buffer const & b)
58 include_label = unique_id();
64 InsetInclude::~InsetInclude()
70 InsetInclude::InsetIncludeParams const & InsetInclude::params() const
76 void InsetInclude::setFromParams(InsetIncludeParams const & p)
78 params_.cparams.setContents(p.cparams.getContents());
79 params_.noload = p.noload;
80 if (params_.flag == p.flag)
83 params_.flag = p.flag;
87 switch (params_.flag) {
92 command="verbatiminput";
98 command="verbatiminput*";
102 params_.cparams.setCmdName(command);
106 Inset * InsetInclude::Clone(Buffer const & buffer) const
108 InsetIncludeParams p(params_);
111 return new InsetInclude (p);
115 void InsetInclude::Edit(BufferView * bv, int, int, unsigned int)
117 bv->owner()->getDialogs()->showInclude(this);
121 void InsetInclude::Write(Buffer const *, ostream & os) const
123 os << "Include " << params_.cparams.getCommand() << "\n";
127 void InsetInclude::Read(Buffer const *, LyXLex & lex)
129 params_.cparams.Read(lex);
131 if (params_.cparams.getCmdName() == "include")
132 params_.flag = INCLUDE;
133 else if (params_.cparams.getCmdName() == "input")
134 params_.flag = INPUT;
135 /* FIXME: is this logic necessary now ? */
136 else if (contains(params_.cparams.getCmdName(), "verbatim")) {
138 if (params_.cparams.getCmdName() == "verbatiminput*")
139 params_.flag = VERBAST;
144 bool InsetInclude::display() const
146 return !(params_.flag == INPUT);
150 string const InsetInclude::getScreenLabel() const
154 switch (params_.flag) {
155 case INPUT: temp += _("Input"); break;
156 case VERB: temp += _("Verbatim Input"); break;
157 case VERBAST: temp += _("Verbatim Input*"); break;
158 case INCLUDE: temp += _("Include"); break;
163 if (params_.cparams.getContents().empty())
166 temp += params_.cparams.getContents();
172 string const InsetInclude::getRelFileBaseName() const
174 return OnlyFilename(ChangeExtension(params_.cparams.getContents(), string()));
178 string const InsetInclude::getFileName() const
180 return MakeAbsPath(params_.cparams.getContents(),
181 OnlyPath(getMasterFilename()));
185 string const InsetInclude::getMasterFilename() const
187 return params_.buffer->fileName();
191 bool InsetInclude::loadIfNeeded() const
193 if (params_.noload || isVerbatim())
196 if (!IsLyXFilename(getFileName()))
199 if (bufferlist.exists(getFileName()))
202 // the readonly flag can/will be wrong, not anymore I think.
203 FileInfo finfo(getFileName());
204 bool const ro = !finfo.writable();
205 return bufferlist.readFile(getFileName(), ro) != 0;
209 int InsetInclude::Latex(Buffer const * buffer, ostream & os,
210 bool /*fragile*/, bool /*fs*/) const
212 string incfile(params_.cparams.getContents());
214 // Do nothing if no file name has been specified
218 if (loadIfNeeded()) {
219 Buffer * tmp = bufferlist.getBuffer(getFileName());
221 // FIXME: this should be a GUI warning
222 if (tmp->params.textclass != buffer->params.textclass) {
223 lyxerr << "WARNING: Included file `"
224 << MakeDisplayPath(getFileName())
225 << "' has textclass `"
226 << textclasslist.NameOfClass(tmp->params.textclass)
227 << "' while parent file has textclass `"
228 << textclasslist.NameOfClass(buffer->params.textclass)
233 // write it to a file (so far the complete file)
234 string writefile = ChangeExtension(getFileName(), ".tex");
236 if (!buffer->tmppath.empty()
237 && !buffer->niceFile) {
238 incfile = subst(incfile, '/','@');
240 incfile = subst(incfile, ':', '$');
242 writefile = AddName(buffer->tmppath, incfile);
244 writefile = getFileName();
245 writefile = ChangeExtension(writefile, ".tex");
246 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
247 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
249 tmp->markDepClean(buffer->tmppath);
251 tmp->makeLaTeXFile(writefile,
252 OnlyPath(getMasterFilename()),
253 buffer->niceFile, true);
257 os << '\\' << params_.cparams.getCmdName() << '{' << incfile << '}';
258 } else if (params_.flag == INPUT) {
259 // \input wants file with extension (default is .tex)
260 if (!IsLyXFilename(getFileName())) {
261 os << '\\' << params_.cparams.getCmdName() << '{' << incfile << '}';
263 os << '\\' << params_.cparams.getCmdName() << '{'
264 << ChangeExtension(incfile, ".tex")
268 // \include don't want extension and demands that the
269 // file really have .tex
270 os << '\\' << params_.cparams.getCmdName() << '{'
271 << ChangeExtension(incfile, string())
279 int InsetInclude::Ascii(Buffer const *, std::ostream & os, int) const
282 os << GetFileContents(getFileName());
287 int InsetInclude::Linuxdoc(Buffer const * buffer, ostream & os) const
289 string incfile(params_.cparams.getContents());
291 // Do nothing if no file name has been specified
295 if (loadIfNeeded()) {
296 Buffer * tmp = bufferlist.getBuffer(getFileName());
298 // write it to a file (so far the complete file)
299 string writefile = ChangeExtension(getFileName(), ".sgml");
300 if (!buffer->tmppath.empty() && !buffer->niceFile) {
301 incfile = subst(incfile, '/','@');
302 writefile = AddName(buffer->tmppath, incfile);
304 writefile = getFileName();
306 if (IsLyXFilename(getFileName()))
307 writefile = ChangeExtension(writefile, ".sgml");
309 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
310 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
312 tmp->makeLinuxDocFile(writefile, buffer->niceFile, true);
316 os << "<inlinegraphic fileref=\"" << '&' << include_label << ';'
317 << "\" format=\"linespecific\">"
318 << "</inlinegraphic>";
320 os << '&' << include_label << ';';
326 int InsetInclude::DocBook(Buffer const * buffer, ostream & os) const
328 string incfile(params_.cparams.getContents());
330 // Do nothing if no file name has been specified
334 if (loadIfNeeded()) {
335 Buffer * tmp = bufferlist.getBuffer(getFileName());
337 // write it to a file (so far the complete file)
338 string writefile = ChangeExtension(getFileName(), ".sgml");
339 if (!buffer->tmppath.empty() && !buffer->niceFile) {
340 incfile = subst(incfile, '/','@');
341 writefile = AddName(buffer->tmppath, incfile);
343 writefile = getFileName();
344 if (IsLyXFilename(getFileName()))
345 writefile = ChangeExtension(writefile, ".sgml");
347 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
348 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
350 tmp->makeDocBookFile(writefile, buffer->niceFile, true);
354 os << "<inlinegraphic fileref=\"" << '&' << include_label << ';'
355 << "\" format=\"linespecific\">"
356 << "</inlinegraphic>";
358 os << '&' << include_label << ';';
364 void InsetInclude::Validate(LaTeXFeatures & features) const
367 string incfile(params_.cparams.getContents());
370 Buffer const & b = *params_.buffer;
372 if (!b.tmppath.empty() && b.niceFile) {
373 incfile = subst(incfile, '/','@');
374 writefile = AddName(b.tmppath, incfile);
376 writefile = getFileName();
378 if (IsLyXFilename(getFileName()))
379 writefile = ChangeExtension(writefile, ".sgml");
381 features.IncludedFiles[include_label] = writefile;
384 features.verbatim = true;
386 // Here we must do the fun stuff...
387 // Load the file in the include if it needs
389 if (loadIfNeeded()) {
391 Buffer * tmp = bufferlist.getBuffer(getFileName());
392 tmp->validate(features);
397 vector<string> const InsetInclude::getLabelList() const
401 if (loadIfNeeded()) {
402 Buffer * tmp = bufferlist.getBuffer(getFileName());
403 tmp->setParentName("");
404 l = tmp->getLabelList();
405 tmp->setParentName(getMasterFilename());
412 vector<pair<string,string> > const InsetInclude::getKeys() const
414 vector<pair<string,string> > keys;
416 if (loadIfNeeded()) {
417 Buffer * tmp = bufferlist.getBuffer(getFileName());
418 tmp->setParentName("");
419 keys = tmp->getBibkeyList();
420 tmp->setParentName(getMasterFilename());