2 * \file InsetInclude.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
7 * \author Richard Heck (conversion to InsetCommand)
8 * \author Bo Peng (embedding stuff)
10 * Full author contact details are available in file CREDITS.
15 #include "InsetInclude.h"
17 #include "LayoutFile.h"
19 #include "buffer_funcs.h"
20 #include "BufferList.h"
21 #include "BufferParams.h"
22 #include "BufferView.h"
24 #include "DispatchResult.h"
25 #include "ErrorList.h"
27 #include "FuncRequest.h"
28 #include "FuncStatus.h"
29 #include "LaTeXFeatures.h"
33 #include "MetricsInfo.h"
34 #include "OutputParams.h"
35 #include "TextClass.h"
36 #include "TocBackend.h"
38 #include "frontends/alert.h"
39 #include "frontends/Painter.h"
41 #include "graphics/PreviewImage.h"
42 #include "graphics/PreviewLoader.h"
44 #include "insets/InsetLabel.h"
45 #include "insets/InsetListingsParams.h"
46 #include "insets/RenderPreview.h"
48 #include "support/debug.h"
49 #include "support/docstream.h"
50 #include "support/ExceptionMessage.h"
51 #include "support/FileNameList.h"
52 #include "support/filetools.h"
53 #include "support/gettext.h"
54 #include "support/lstrings.h" // contains
55 #include "support/lyxalgo.h"
56 #include "support/convert.h"
58 #include <boost/bind.hpp>
61 using namespace lyx::support;
65 namespace Alert = frontend::Alert;
70 docstring const uniqueID()
72 static unsigned int seed = 1000;
73 return "file" + convert<docstring>(++seed);
77 /// the type of inclusion
79 INCLUDE, VERB, INPUT, VERBAST, LISTINGS, NONE
83 Types type(string const & s)
87 if (s == "verbatiminput")
89 if (s == "verbatiminput*")
91 if (s == "lstinputlisting")
99 Types type(InsetCommandParams const & params)
101 return type(params.getCmdName());
105 bool isListings(InsetCommandParams const & params)
107 return type(params) == LISTINGS;
111 bool isVerbatim(InsetCommandParams const & params)
113 Types const t = type(params);
114 return t == VERB || t == VERBAST;
118 bool isInputOrInclude(InsetCommandParams const & params)
120 Types const t = type(params);
121 return t == INPUT || t == INCLUDE;
125 FileName const masterFileName(Buffer const & buffer)
127 return buffer.masterBuffer()->fileName();
131 void add_preview(RenderMonitoredPreview &, InsetInclude const &, Buffer const &);
134 string const parentFilename(Buffer const & buffer)
136 return buffer.absFileName();
140 EmbeddedFile const includedFilename(Buffer const & buffer,
141 InsetCommandParams const & params)
143 // it is not a good idea to create this EmbeddedFile object
144 // each time, but there seems to be no easy way around.
145 EmbeddedFile file(to_utf8(params["filename"]),
146 onlyPath(parentFilename(buffer)));
147 file.setEmbed(!params["embed"].empty());
148 file.enable(buffer.embedded(), &buffer, false);
152 InsetLabel * createLabel(docstring const & label_str)
154 if (label_str.empty())
156 InsetCommandParams icp(LABEL_CODE);
157 icp["name"] = label_str;
158 return new InsetLabel(icp);
164 InsetInclude::InsetInclude(InsetCommandParams const & p)
165 : InsetCommand(p, "include"), include_label(uniqueID()),
166 preview_(new RenderMonitoredPreview(this)), set_label_(false), label_(0)
168 preview_->fileChanged(boost::bind(&InsetInclude::fileChanged, this));
170 if (isListings(params())) {
171 InsetListingsParams listing_params(to_utf8(p["lstparams"]));
172 label_ = createLabel(from_utf8(listing_params.getParamValue("label")));
177 InsetInclude::InsetInclude(InsetInclude const & other)
178 : InsetCommand(other), include_label(other.include_label),
179 preview_(new RenderMonitoredPreview(this)), set_label_(false), label_(0)
181 preview_->fileChanged(boost::bind(&InsetInclude::fileChanged, this));
184 label_ = new InsetLabel(*other.label_);
188 InsetInclude::~InsetInclude()
191 if (isVerbatim(params()) || isListings(params()))
195 string const parent_filename = buffer().absFileName();
196 FileName const included_file = makeAbsPath(to_utf8(params()["filename"]),
197 onlyPath(parent_filename));
199 if (!isLyXFilename(included_file.absFilename()))
202 Buffer * child = theBufferList().getBuffer(included_file.absFilename());
203 // File not opened, nothing to close.
207 // Child document has a different parent, don't close it.
208 if (child->parent() != &buffer())
212 theBufferList().release(child);
216 void InsetInclude::setBuffer(Buffer & buffer)
220 label_->setBuffer(buffer);
225 ParamInfo const & InsetInclude::findInfo(string const & /* cmdName */)
228 // This is only correct for the case of listings, but it'll do for now.
229 // In the other cases, this second parameter should just be empty.
230 static ParamInfo param_info_;
231 if (param_info_.empty()) {
232 param_info_.add("filename", ParamInfo::LATEX_REQUIRED);
233 param_info_.add("lstparams", ParamInfo::LATEX_OPTIONAL);
234 param_info_.add("embed", ParamInfo::LYX_INTERNAL);
240 bool InsetInclude::isCompatibleCommand(string const & s)
242 return type(s) != NONE;
246 void InsetInclude::doDispatch(Cursor & cur, FuncRequest & cmd)
248 BOOST_ASSERT(&cur.buffer() == &buffer());
249 switch (cmd.action) {
251 case LFUN_INSET_MODIFY: {
252 InsetCommandParams p(INCLUDE_CODE);
253 InsetCommandMailer::string2params("include", to_utf8(cmd.argument()), p);
254 if (!p.getCmdName().empty()) {
255 Buffer const & buf = cur.buffer();
257 InsetListingsParams new_params(to_utf8(p["lstparams"]));
258 docstring const label_str = from_utf8(new_params.getParamValue("label"));
259 if (label_str.empty())
262 label_->updateCommand(label_str);
264 label_ = createLabel(label_str);
265 label_->setBuffer(buffer());
270 // the embed parameter passed back from the dialog
271 // is "true" or "false", we need to change it.
272 if (p["embed"] == _("false"))
275 p["embed"] = from_utf8(EmbeddedFile(to_utf8(p["filename"]),
276 onlyPath(parentFilename(buf))).inzipName());
278 includedFilename(cur.buffer(), p);
279 } catch (ExceptionMessage const & message) {
280 Alert::error(message.title_, message.details_);
281 // do not set parameter if an error happens
285 buffer().updateBibfilesCache();
291 //pass everything else up the chain
293 InsetCommand::doDispatch(cur, cmd);
299 void InsetInclude::setParams(InsetCommandParams const & p)
301 InsetCommand::setParams(p);
304 if (preview_->monitoring())
305 preview_->stopMonitoring();
307 if (type(params()) == INPUT)
308 add_preview(*preview_, *this, buffer());
312 docstring InsetInclude::screenLabel() const
316 switch (type(params())) {
318 temp = buffer().B_("Input");
321 temp = buffer().B_("Verbatim Input");
324 temp = buffer().B_("Verbatim Input*");
327 temp = buffer().B_("Include");
330 temp = listings_label_;
338 if (params()["filename"].empty())
341 temp += from_utf8(onlyFilename(to_utf8(params()["filename"])));
343 if (!params()["embed"].empty())
344 temp += _(" (embedded)");
349 /// return the child buffer if the file is a LyX doc and is loaded
350 Buffer * getChildBuffer(Buffer const & buffer, InsetCommandParams const & params)
352 if (isVerbatim(params) || isListings(params))
355 string const included_file = includedFilename(buffer, params).absFilename();
356 if (!isLyXFilename(included_file))
359 Buffer * childBuffer = loadIfNeeded(buffer, params);
361 // FIXME: recursive includes
362 return (childBuffer == &buffer) ? 0 : childBuffer;
366 /// return true if the file is or got loaded.
367 Buffer * loadIfNeeded(Buffer const & parent, InsetCommandParams const & params)
369 if (isVerbatim(params) || isListings(params))
372 string const parent_filename = parent.absFileName();
373 FileName const included_file = makeAbsPath(to_utf8(params["filename"]),
374 onlyPath(parent_filename));
376 if (!isLyXFilename(included_file.absFilename()))
379 Buffer * child = theBufferList().getBuffer(included_file.absFilename());
381 // the readonly flag can/will be wrong, not anymore I think.
382 if (!included_file.exists())
385 child = theBufferList().newBuffer(included_file.absFilename());
387 // Buffer creation is not possible.
390 if (!child->loadLyXFile(included_file)) {
391 //close the buffer we just opened
392 theBufferList().release(child);
396 if (!child->errorList("Parse").empty()) {
397 // FIXME: Do something.
400 child->setParent(&parent);
405 int InsetInclude::latex(odocstream & os, OutputParams const & runparams) const
407 string incfile = to_utf8(params()["filename"]);
409 // Do nothing if no file name has been specified
413 FileName const included_file =
414 includedFilename(buffer(), params()).availableFile();
416 // Check we're not trying to include ourselves.
417 // FIXME RECURSIVE INCLUDE
418 // This isn't sufficient, as the inclusion could be downstream.
419 // But it'll have to do for now.
420 if (isInputOrInclude(params()) &&
421 buffer().absFileName() == included_file.absFilename())
423 Alert::error(_("Recursive input"),
424 bformat(_("Attempted to include file %1$s in itself! "
425 "Ignoring inclusion."), from_utf8(incfile)));
429 Buffer const * const masterBuffer = buffer().masterBuffer();
431 // if incfile is relative, make it relative to the master
433 if (!FileName(incfile).isAbsolute()) {
435 incfile = to_utf8(makeRelPath(from_utf8(included_file.absFilename()),
436 from_utf8(masterBuffer->filePath())));
439 // write it to a file (so far the complete file)
440 string const exportfile = changeExtension(incfile, ".tex");
441 string const mangled =
442 DocFileName(changeExtension(included_file.absFilename(),".tex")).
444 FileName const writefile(makeAbsPath(mangled, masterBuffer->temppath()));
448 else if (!isValidLaTeXFilename(incfile)) {
449 frontend::Alert::warning(_("Invalid filename"),
450 _("The following filename is likely to cause trouble "
451 "when running the exported file through LaTeX: ") +
454 LYXERR(Debug::LATEX, "incfile:" << incfile);
455 LYXERR(Debug::LATEX, "exportfile:" << exportfile);
456 LYXERR(Debug::LATEX, "writefile:" << writefile);
458 if (runparams.inComment || runparams.dryrun) {
459 //Don't try to load or copy the file if we're
460 //in a comment or doing a dryrun
461 } else if (isInputOrInclude(params()) &&
462 isLyXFilename(included_file.absFilename())) {
463 //if it's a LyX file and we're inputting or including,
464 //try to load it so we can write the associated latex
465 if (!loadIfNeeded(buffer(), params()))
468 Buffer * tmp = theBufferList().getBuffer(included_file.absFilename());
470 if (tmp->params().baseClass() != masterBuffer->params().baseClass()) {
472 docstring text = bformat(_("Included file `%1$s'\n"
473 "has textclass `%2$s'\n"
474 "while parent file has textclass `%3$s'."),
475 included_file.displayName(),
476 from_utf8(tmp->params().documentClass().name()),
477 from_utf8(masterBuffer->params().documentClass().name()));
478 Alert::warning(_("Different textclasses"), text);
482 // Make sure modules used in child are all included in master
483 //FIXME It might be worth loading the children's modules into the master
484 //over in BufferParams rather than doing this check.
485 vector<string> const masterModules = masterBuffer->params().getModules();
486 vector<string> const childModules = tmp->params().getModules();
487 vector<string>::const_iterator it = childModules.begin();
488 vector<string>::const_iterator end = childModules.end();
489 for (; it != end; ++it) {
490 string const module = *it;
491 vector<string>::const_iterator found =
492 find(masterModules.begin(), masterModules.end(), module);
493 if (found != masterModules.end()) {
494 docstring text = bformat(_("Included file `%1$s'\n"
495 "uses module `%2$s'\n"
496 "which is not used in parent file."),
497 included_file.displayName(), from_utf8(module));
498 Alert::warning(_("Module not found"), text);
502 tmp->markDepClean(masterBuffer->temppath());
504 // FIXME: handle non existing files
505 // FIXME: Second argument is irrelevant!
506 // since only_body is true, makeLaTeXFile will not look at second
507 // argument. Should we set it to string(), or should makeLaTeXFile
508 // make use of it somehow? (JMarc 20031002)
509 // The included file might be written in a different encoding
510 Encoding const * const oldEnc = runparams.encoding;
511 runparams.encoding = &tmp->params().encoding();
512 tmp->makeLaTeXFile(writefile,
513 masterFileName(buffer()).onlyPath().absFilename(),
515 runparams.encoding = oldEnc;
517 // In this case, it's not a LyX file, so we copy the file
518 // to the temp dir, so that .aux files etc. are not created
519 // in the original dir. Files included by this file will be
520 // found via input@path, see ../Buffer.cpp.
521 unsigned long const checksum_in = included_file.checksum();
522 unsigned long const checksum_out = writefile.checksum();
524 if (checksum_in != checksum_out) {
525 if (!included_file.copyTo(writefile)) {
528 to_utf8(bformat(_("Could not copy the file\n%1$s\n"
529 "into the temporary directory."),
530 from_utf8(included_file.absFilename()))));
536 string const tex_format = (runparams.flavor == OutputParams::LATEX) ?
537 "latex" : "pdflatex";
538 if (isVerbatim(params())) {
539 incfile = latex_path(incfile);
541 os << '\\' << from_ascii(params().getCmdName()) << '{'
542 << from_utf8(incfile) << '}';
543 } else if (type(params()) == INPUT) {
544 runparams.exportdata->addExternalFile(tex_format, writefile,
547 // \input wants file with extension (default is .tex)
548 if (!isLyXFilename(included_file.absFilename())) {
549 incfile = latex_path(incfile);
551 os << '\\' << from_ascii(params().getCmdName())
552 << '{' << from_utf8(incfile) << '}';
554 incfile = changeExtension(incfile, ".tex");
555 incfile = latex_path(incfile);
557 os << '\\' << from_ascii(params().getCmdName())
558 << '{' << from_utf8(incfile) << '}';
560 } else if (type(params()) == LISTINGS) {
561 os << '\\' << from_ascii(params().getCmdName());
562 string const opt = to_utf8(params()["lstparams"]);
563 // opt is set in QInclude dialog and should have passed validation.
564 InsetListingsParams params(opt);
565 if (!params.params().empty())
566 os << "[" << from_utf8(params.params()) << "]";
567 os << '{' << from_utf8(incfile) << '}';
569 runparams.exportdata->addExternalFile(tex_format, writefile,
572 // \include don't want extension and demands that the
573 // file really have .tex
574 incfile = changeExtension(incfile, string());
575 incfile = latex_path(incfile);
577 os << '\\' << from_ascii(params().getCmdName()) << '{'
578 << from_utf8(incfile) << '}';
585 int InsetInclude::plaintext(odocstream & os, OutputParams const &) const
587 if (isVerbatim(params()) || isListings(params())) {
588 os << '[' << screenLabel() << '\n';
589 // FIXME: We don't know the encoding of the file, default to UTF-8.
590 os << includedFilename(buffer(), params()).fileContents("UTF-8");
592 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
594 docstring const str = '[' + screenLabel() + ']';
601 int InsetInclude::docbook(odocstream & os, OutputParams const & runparams) const
603 string incfile = to_utf8(params()["filename"]);
605 // Do nothing if no file name has been specified
609 string const included_file = includedFilename(buffer(), params()).absFilename();
611 // Check we're not trying to include ourselves.
612 // FIXME RECURSIVE INCLUDE
613 // This isn't sufficient, as the inclusion could be downstream.
614 // But it'll have to do for now.
615 if (buffer().absFileName() == included_file) {
616 Alert::error(_("Recursive input"),
617 bformat(_("Attempted to include file %1$s in itself! "
618 "Ignoring inclusion."), from_utf8(incfile)));
622 // write it to a file (so far the complete file)
623 string const exportfile = changeExtension(incfile, ".sgml");
624 DocFileName writefile(changeExtension(included_file, ".sgml"));
626 if (loadIfNeeded(buffer(), params())) {
627 Buffer * tmp = theBufferList().getBuffer(included_file);
629 string const mangled = writefile.mangledFilename();
630 writefile = makeAbsPath(mangled,
631 buffer().masterBuffer()->temppath());
635 LYXERR(Debug::LATEX, "incfile:" << incfile);
636 LYXERR(Debug::LATEX, "exportfile:" << exportfile);
637 LYXERR(Debug::LATEX, "writefile:" << writefile);
639 tmp->makeDocBookFile(writefile, runparams, true);
642 runparams.exportdata->addExternalFile("docbook", writefile,
644 runparams.exportdata->addExternalFile("docbook-xml", writefile,
647 if (isVerbatim(params()) || isListings(params())) {
648 os << "<inlinegraphic fileref=\""
649 << '&' << include_label << ';'
650 << "\" format=\"linespecific\">";
652 os << '&' << include_label << ';';
658 void InsetInclude::validate(LaTeXFeatures & features) const
660 string incfile = to_utf8(params()["filename"]);
663 BOOST_ASSERT(&buffer() == &features.buffer());
665 string const included_file =
666 includedFilename(buffer(), params()).availableFile().absFilename();
668 if (isLyXFilename(included_file))
669 writefile = changeExtension(included_file, ".sgml");
671 writefile = included_file;
673 if (!features.runparams().nice && !isVerbatim(params()) && !isListings(params())) {
674 incfile = DocFileName(writefile).mangledFilename();
675 writefile = makeAbsPath(incfile,
676 buffer().masterBuffer()->temppath()).absFilename();
679 features.includeFile(include_label, writefile);
681 if (isVerbatim(params()))
682 features.require("verbatim");
683 else if (isListings(params()))
684 features.require("listings");
686 // Here we must do the fun stuff...
687 // Load the file in the include if it needs
689 if (loadIfNeeded(buffer(), params())) {
691 Buffer * const tmp = theBufferList().getBuffer(included_file);
692 // make sure the buffer isn't us
693 // FIXME RECURSIVE INCLUDES
694 // This is not sufficient, as recursive includes could be
695 // more than a file away. But it will do for now.
696 if (tmp && tmp != &buffer()) {
697 // We must temporarily change features.buffer,
698 // otherwise it would always be the master buffer,
699 // and nested includes would not work.
700 features.setBuffer(*tmp);
701 tmp->validate(features);
702 features.setBuffer(buffer());
708 void InsetInclude::fillWithBibKeys(BiblioInfo & keys,
709 InsetIterator const & /*di*/) const
711 if (loadIfNeeded(buffer(), params())) {
712 string const included_file = includedFilename(buffer(), params()).absFilename();
713 Buffer * tmp = theBufferList().getBuffer(included_file);
714 //FIXME This is kind of a dirty hack and should be made reasonable.
716 keys.fillWithBibKeys(tmp);
717 tmp->setParent(&buffer());
722 void InsetInclude::updateBibfilesCache()
724 Buffer * const tmp = getChildBuffer(buffer(), params());
727 tmp->updateBibfilesCache();
728 tmp->setParent(&buffer());
733 EmbeddedFileList const &
734 InsetInclude::getBibfilesCache(Buffer const & buffer) const
736 Buffer * const tmp = getChildBuffer(buffer, params());
739 EmbeddedFileList const & cache = tmp->getBibfilesCache();
740 tmp->setParent(&buffer);
743 static EmbeddedFileList const empty;
748 void InsetInclude::metrics(MetricsInfo & mi, Dimension & dim) const
750 BOOST_ASSERT(mi.base.bv);
752 bool use_preview = false;
753 if (RenderPreview::status() != LyXRC::PREVIEW_OFF) {
754 graphics::PreviewImage const * pimage =
755 preview_->getPreviewImage(mi.base.bv->buffer());
756 use_preview = pimage && pimage->image();
760 preview_->metrics(mi, dim);
764 button_.update(screenLabel(), true);
766 button_.metrics(mi, dim);
769 Box b(0, dim.wid, -dim.asc, dim.des);
774 void InsetInclude::draw(PainterInfo & pi, int x, int y) const
776 BOOST_ASSERT(pi.base.bv);
778 bool use_preview = false;
779 if (RenderPreview::status() != LyXRC::PREVIEW_OFF) {
780 graphics::PreviewImage const * pimage =
781 preview_->getPreviewImage(pi.base.bv->buffer());
782 use_preview = pimage && pimage->image();
786 preview_->draw(pi, x, y);
788 button_.draw(pi, x, y);
792 Inset::DisplayType InsetInclude::display() const
794 return type(params()) == INPUT ? Inline : AlignCenter;
803 void InsetInclude::fileChanged() const
805 Buffer const * const buffer = updateFrontend();
809 preview_->removePreview(*buffer);
810 add_preview(*preview_.get(), *this, *buffer);
811 preview_->startLoading(*buffer);
817 bool preview_wanted(InsetCommandParams const & params, Buffer const & buffer)
819 FileName const included_file = includedFilename(buffer, params);
821 return type(params) == INPUT && params.preview() &&
822 included_file.isReadableFile();
826 docstring latexString(InsetInclude const & inset)
829 // We don't need to set runparams.encoding since this will be done
830 // by latex() anyway.
831 OutputParams runparams(0);
832 runparams.flavor = OutputParams::LATEX;
833 inset.latex(os, runparams);
839 void add_preview(RenderMonitoredPreview & renderer, InsetInclude const & inset,
840 Buffer const & buffer)
842 InsetCommandParams const & params = inset.params();
843 if (RenderPreview::status() != LyXRC::PREVIEW_OFF &&
844 preview_wanted(params, buffer)) {
845 renderer.setAbsFile(includedFilename(buffer, params));
846 docstring const snippet = latexString(inset);
847 renderer.addPreview(snippet, buffer);
854 void InsetInclude::addPreview(graphics::PreviewLoader & ploader) const
856 Buffer const & buffer = ploader.buffer();
857 if (!preview_wanted(params(), buffer))
859 preview_->setAbsFile(includedFilename(buffer, params()));
860 docstring const snippet = latexString(*this);
861 preview_->addPreview(snippet, ploader);
865 void InsetInclude::addToToc(ParConstIterator const & cpit) const
867 if (isListings(params())) {
869 label_->addToToc(cpit);
871 InsetListingsParams p(to_utf8(params()["lstparams"]));
872 string caption = p.getParamValue("caption");
875 Toc & toc = buffer().tocBackend().toc("listing");
876 docstring const str = convert<docstring>(toc.size() + 1)
877 + ". " + from_utf8(caption);
878 ParConstIterator pit = cpit;
879 pit.push_back(*this);
880 toc.push_back(TocItem(pit, 0, str));
883 Buffer const * const childbuffer = getChildBuffer(buffer(), params());
887 Toc & toc = buffer().tocBackend().toc("child");
888 toc.push_back(TocItem(cpit, 0, childbuffer->fileName().displayName()));
890 TocList & toclist = buffer().tocBackend().tocs();
891 TocList const & childtoclist = childbuffer->tocBackend().tocs();
892 TocList::const_iterator it = childtoclist.begin();
893 TocList::const_iterator const end = childtoclist.end();
894 for(; it != end; ++it)
895 toclist[it->first].insert(toclist[it->first].end(),
896 it->second.begin(), it->second.end());
900 void InsetInclude::updateLabels(ParIterator const & it)
902 Buffer const * const childbuffer = getChildBuffer(buffer(), params());
904 lyx::updateLabels(*childbuffer, true);
907 if (!isListings(params()))
911 label_->updateLabels(it);
913 InsetListingsParams const par(to_utf8(params()["lstparams"]));
914 if (par.getParamValue("caption").empty()) {
915 listings_label_.clear();
918 Counters & counters = buffer().params().documentClass().counters();
919 docstring const cnt = from_ascii("listing");
920 listings_label_ = buffer().B_("Program Listing");
921 if (counters.hasCounter(cnt)) {
923 listings_label_ += " " + convert<docstring>(counters.value(cnt));
928 void InsetInclude::registerEmbeddedFiles(EmbeddedFileList & files) const
930 files.registerFile(includedFilename(buffer(), params()), this, buffer());
934 void InsetInclude::updateEmbeddedFile(EmbeddedFile const & file)
936 InsetCommandParams p = params();
937 p["filename"] = from_utf8(file.outputFilename());
938 p["embed"] = file.embedded() ? from_utf8(file.inzipName()) : docstring();