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)
9 * Full author contact details are available in file CREDITS.
14 #include "InsetInclude.h"
16 #include "LayoutFile.h"
18 #include "buffer_funcs.h"
19 #include "BufferList.h"
20 #include "BufferParams.h"
21 #include "BufferView.h"
23 #include "DispatchResult.h"
24 #include "ErrorList.h"
26 #include "FuncRequest.h"
27 #include "FuncStatus.h"
28 #include "LaTeXFeatures.h"
32 #include "MetricsInfo.h"
33 #include "OutputParams.h"
34 #include "TextClass.h"
35 #include "TocBackend.h"
37 #include "frontends/alert.h"
38 #include "frontends/Painter.h"
40 #include "graphics/PreviewImage.h"
41 #include "graphics/PreviewLoader.h"
43 #include "insets/InsetLabel.h"
44 #include "insets/InsetListingsParams.h"
45 #include "insets/RenderPreview.h"
47 #include "support/assert.h"
48 #include "support/debug.h"
49 #include "support/docstream.h"
50 #include "support/FileNameList.h"
51 #include "support/filetools.h"
52 #include "support/gettext.h"
53 #include "support/lstrings.h" // contains
54 #include "support/lyxalgo.h"
55 #include "support/convert.h"
57 #include <boost/bind.hpp>
60 using namespace lyx::support;
64 namespace Alert = frontend::Alert;
69 docstring const uniqueID()
71 static unsigned int seed = 1000;
72 return "file" + convert<docstring>(++seed);
76 /// the type of inclusion
78 INCLUDE, VERB, INPUT, VERBAST, LISTINGS, NONE
82 Types type(string const & s)
86 if (s == "verbatiminput")
88 if (s == "verbatiminput*")
90 if (s == "lstinputlisting")
98 Types type(InsetCommandParams const & params)
100 return type(params.getCmdName());
104 bool isListings(InsetCommandParams const & params)
106 return type(params) == LISTINGS;
110 bool isVerbatim(InsetCommandParams const & params)
112 Types const t = type(params);
113 return t == VERB || t == VERBAST;
117 bool isInputOrInclude(InsetCommandParams const & params)
119 Types const t = type(params);
120 return t == INPUT || t == INCLUDE;
124 FileName const masterFileName(Buffer const & buffer)
126 return buffer.masterBuffer()->fileName();
130 void add_preview(RenderMonitoredPreview &, InsetInclude const &, Buffer const &);
133 string const parentFilename(Buffer const & buffer)
135 return buffer.absFileName();
139 FileName const includedFilename(Buffer const & buffer,
140 InsetCommandParams const & params)
142 return makeAbsPath(to_utf8(params["filename"]),
143 onlyPath(parentFilename(buffer)));
147 InsetLabel * createLabel(docstring const & label_str)
149 if (label_str.empty())
151 InsetCommandParams icp(LABEL_CODE);
152 icp["name"] = label_str;
153 return new InsetLabel(icp);
159 InsetInclude::InsetInclude(InsetCommandParams const & p)
160 : InsetCommand(p, "include"), include_label(uniqueID()),
161 preview_(new RenderMonitoredPreview(this)), set_label_(false), label_(0)
163 preview_->fileChanged(boost::bind(&InsetInclude::fileChanged, this));
165 if (isListings(params())) {
166 InsetListingsParams listing_params(to_utf8(p["lstparams"]));
167 label_ = createLabel(from_utf8(listing_params.getParamValue("label")));
172 InsetInclude::InsetInclude(InsetInclude const & other)
173 : InsetCommand(other), include_label(other.include_label),
174 preview_(new RenderMonitoredPreview(this)), set_label_(false), label_(0)
176 preview_->fileChanged(boost::bind(&InsetInclude::fileChanged, this));
179 label_ = new InsetLabel(*other.label_);
183 InsetInclude::~InsetInclude()
186 if (isVerbatim(params()) || isListings(params()))
190 string const parent_filename = buffer().absFileName();
191 FileName const included_file = makeAbsPath(to_utf8(params()["filename"]),
192 onlyPath(parent_filename));
194 if (!isLyXFilename(included_file.absFilename()))
197 Buffer * child = theBufferList().getBuffer(included_file.absFilename());
198 // File not opened, nothing to close.
202 // Child document has a different parent, don't close it.
203 if (child->parent() != &buffer())
207 theBufferList().release(child);
211 void InsetInclude::setBuffer(Buffer & buffer)
213 InsetCommand::setBuffer(buffer);
215 label_->setBuffer(buffer);
219 ParamInfo const & InsetInclude::findInfo(string const & /* cmdName */)
222 // This is only correct for the case of listings, but it'll do for now.
223 // In the other cases, this second parameter should just be empty.
224 static ParamInfo param_info_;
225 if (param_info_.empty()) {
226 param_info_.add("filename", ParamInfo::LATEX_REQUIRED);
227 param_info_.add("lstparams", ParamInfo::LATEX_OPTIONAL);
233 bool InsetInclude::isCompatibleCommand(string const & s)
235 return type(s) != NONE;
239 void InsetInclude::doDispatch(Cursor & cur, FuncRequest & cmd)
241 LASSERT(&cur.buffer() == &buffer(), /**/);
242 switch (cmd.action) {
244 case LFUN_INSET_MODIFY: {
245 InsetCommandParams p(INCLUDE_CODE);
246 if (cmd.getArg(0) == "changetype") {
247 InsetCommand::doDispatch(cur, cmd);
250 InsetCommand::string2params("include", to_utf8(cmd.argument()), p);
251 if (!p.getCmdName().empty()) {
253 InsetListingsParams new_params(to_utf8(p["lstparams"]));
254 docstring const label_str = from_utf8(new_params.getParamValue("label"));
255 if (label_str.empty())
258 label_->updateCommand(label_str);
260 label_ = createLabel(label_str);
261 label_->setBuffer(buffer());
271 //pass everything else up the chain
273 InsetCommand::doDispatch(cur, cmd);
279 void InsetInclude::setParams(InsetCommandParams const & p)
281 InsetCommand::setParams(p);
284 if (preview_->monitoring())
285 preview_->stopMonitoring();
287 if (type(params()) == INPUT)
288 add_preview(*preview_, *this, buffer());
290 buffer().updateBibfilesCache();
294 docstring InsetInclude::screenLabel() const
298 switch (type(params())) {
300 temp = buffer().B_("Input");
303 temp = buffer().B_("Verbatim Input");
306 temp = buffer().B_("Verbatim Input*");
309 temp = buffer().B_("Include");
312 temp = listings_label_;
315 LASSERT(false, /**/);
320 if (params()["filename"].empty())
323 temp += from_utf8(onlyFilename(to_utf8(params()["filename"])));
329 /// return the child buffer if the file is a LyX doc and is loaded
330 Buffer * getChildBuffer(Buffer const & buffer, InsetCommandParams const & params)
332 if (isVerbatim(params) || isListings(params))
335 string const included_file = includedFilename(buffer, params).absFilename();
336 if (!isLyXFilename(included_file))
339 Buffer * childBuffer = loadIfNeeded(buffer, params);
341 // FIXME: recursive includes
342 return (childBuffer == &buffer) ? 0 : childBuffer;
346 /// return true if the file is or got loaded.
347 Buffer * loadIfNeeded(Buffer const & parent, InsetCommandParams const & params)
349 if (isVerbatim(params) || isListings(params))
352 string const parent_filename = parent.absFileName();
353 FileName const included_file = makeAbsPath(to_utf8(params["filename"]),
354 onlyPath(parent_filename));
356 if (!isLyXFilename(included_file.absFilename()))
359 Buffer * child = theBufferList().getBuffer(included_file.absFilename());
361 // the readonly flag can/will be wrong, not anymore I think.
362 if (!included_file.exists())
365 child = theBufferList().newBuffer(included_file.absFilename());
367 // Buffer creation is not possible.
370 if (!child->loadLyXFile(included_file)) {
371 //close the buffer we just opened
372 theBufferList().release(child);
376 if (!child->errorList("Parse").empty()) {
377 // FIXME: Do something.
380 child->setParent(&parent);
385 int InsetInclude::latex(odocstream & os, OutputParams const & runparams) const
387 string incfile = to_utf8(params()["filename"]);
389 // Do nothing if no file name has been specified
393 FileName const included_file = includedFilename(buffer(), params());
395 // Check we're not trying to include ourselves.
396 // FIXME RECURSIVE INCLUDE
397 // This isn't sufficient, as the inclusion could be downstream.
398 // But it'll have to do for now.
399 if (isInputOrInclude(params()) &&
400 buffer().absFileName() == included_file.absFilename())
402 Alert::error(_("Recursive input"),
403 bformat(_("Attempted to include file %1$s in itself! "
404 "Ignoring inclusion."), from_utf8(incfile)));
408 Buffer const * const masterBuffer = buffer().masterBuffer();
410 // if incfile is relative, make it relative to the master
412 if (!FileName(incfile).isAbsolute()) {
414 incfile = to_utf8(makeRelPath(from_utf8(included_file.absFilename()),
415 from_utf8(masterBuffer->filePath())));
418 // write it to a file (so far the complete file)
419 string const exportfile = changeExtension(incfile, ".tex");
420 string const mangled =
421 DocFileName(changeExtension(included_file.absFilename(),".tex")).
423 FileName const writefile(makeAbsPath(mangled, masterBuffer->temppath()));
427 else if (!isValidLaTeXFilename(incfile)) {
428 frontend::Alert::warning(_("Invalid filename"),
429 _("The following filename is likely to cause trouble "
430 "when running the exported file through LaTeX: ") +
433 LYXERR(Debug::LATEX, "incfile:" << incfile);
434 LYXERR(Debug::LATEX, "exportfile:" << exportfile);
435 LYXERR(Debug::LATEX, "writefile:" << writefile);
437 if (runparams.inComment || runparams.dryrun) {
438 //Don't try to load or copy the file if we're
439 //in a comment or doing a dryrun
440 } else if (isInputOrInclude(params()) &&
441 isLyXFilename(included_file.absFilename())) {
442 //if it's a LyX file and we're inputting or including,
443 //try to load it so we can write the associated latex
444 if (!loadIfNeeded(buffer(), params()))
447 Buffer * tmp = theBufferList().getBuffer(included_file.absFilename());
449 if (tmp->params().baseClass() != masterBuffer->params().baseClass()) {
451 docstring text = bformat(_("Included file `%1$s'\n"
452 "has textclass `%2$s'\n"
453 "while parent file has textclass `%3$s'."),
454 included_file.displayName(),
455 from_utf8(tmp->params().documentClass().name()),
456 from_utf8(masterBuffer->params().documentClass().name()));
457 Alert::warning(_("Different textclasses"), text);
460 // Make sure modules used in child are all included in master
461 //FIXME It might be worth loading the children's modules into the master
462 //over in BufferParams rather than doing this check.
463 vector<string> const masterModules = masterBuffer->params().getModules();
464 vector<string> const childModules = tmp->params().getModules();
465 vector<string>::const_iterator it = childModules.begin();
466 vector<string>::const_iterator end = childModules.end();
467 for (; it != end; ++it) {
468 string const module = *it;
469 vector<string>::const_iterator found =
470 find(masterModules.begin(), masterModules.end(), module);
471 if (found == masterModules.end()) {
472 docstring text = bformat(_("Included file `%1$s'\n"
473 "uses module `%2$s'\n"
474 "which is not used in parent file."),
475 included_file.displayName(), from_utf8(module));
476 Alert::warning(_("Module not found"), text);
480 tmp->markDepClean(masterBuffer->temppath());
482 // FIXME: handle non existing files
483 // FIXME: Second argument is irrelevant!
484 // since only_body is true, makeLaTeXFile will not look at second
485 // argument. Should we set it to string(), or should makeLaTeXFile
486 // make use of it somehow? (JMarc 20031002)
487 // The included file might be written in a different encoding
488 Encoding const * const oldEnc = runparams.encoding;
489 runparams.encoding = &tmp->params().encoding();
490 tmp->makeLaTeXFile(writefile,
491 masterFileName(buffer()).onlyPath().absFilename(),
493 runparams.encoding = oldEnc;
495 // In this case, it's not a LyX file, so we copy the file
496 // to the temp dir, so that .aux files etc. are not created
497 // in the original dir. Files included by this file will be
498 // found via input@path, see ../Buffer.cpp.
499 unsigned long const checksum_in = included_file.checksum();
500 unsigned long const checksum_out = writefile.checksum();
502 if (checksum_in != checksum_out) {
503 if (!included_file.copyTo(writefile)) {
506 to_utf8(bformat(_("Could not copy the file\n%1$s\n"
507 "into the temporary directory."),
508 from_utf8(included_file.absFilename()))));
514 string const tex_format = (runparams.flavor == OutputParams::LATEX) ?
515 "latex" : "pdflatex";
516 if (isVerbatim(params())) {
517 incfile = latex_path(incfile);
519 os << '\\' << from_ascii(params().getCmdName()) << '{'
520 << from_utf8(incfile) << '}';
521 } else if (type(params()) == INPUT) {
522 runparams.exportdata->addExternalFile(tex_format, writefile,
525 // \input wants file with extension (default is .tex)
526 if (!isLyXFilename(included_file.absFilename())) {
527 incfile = latex_path(incfile);
529 os << '\\' << from_ascii(params().getCmdName())
530 << '{' << from_utf8(incfile) << '}';
532 incfile = changeExtension(incfile, ".tex");
533 incfile = latex_path(incfile);
535 os << '\\' << from_ascii(params().getCmdName())
536 << '{' << from_utf8(incfile) << '}';
538 } else if (type(params()) == LISTINGS) {
539 os << '\\' << from_ascii(params().getCmdName());
540 string const opt = to_utf8(params()["lstparams"]);
541 // opt is set in QInclude dialog and should have passed validation.
542 InsetListingsParams params(opt);
543 if (!params.params().empty())
544 os << "[" << from_utf8(params.params()) << "]";
545 os << '{' << from_utf8(incfile) << '}';
547 runparams.exportdata->addExternalFile(tex_format, writefile,
550 // \include don't want extension and demands that the
551 // file really have .tex
552 incfile = changeExtension(incfile, string());
553 incfile = latex_path(incfile);
555 os << '\\' << from_ascii(params().getCmdName()) << '{'
556 << from_utf8(incfile) << '}';
563 int InsetInclude::plaintext(odocstream & os, OutputParams const &) const
565 if (isVerbatim(params()) || isListings(params())) {
566 os << '[' << screenLabel() << '\n';
567 // FIXME: We don't know the encoding of the file, default to UTF-8.
568 os << includedFilename(buffer(), params()).fileContents("UTF-8");
570 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
572 docstring const str = '[' + screenLabel() + ']';
579 int InsetInclude::docbook(odocstream & os, OutputParams const & runparams) const
581 string incfile = to_utf8(params()["filename"]);
583 // Do nothing if no file name has been specified
587 string const included_file = includedFilename(buffer(), params()).absFilename();
589 // Check we're not trying to include ourselves.
590 // FIXME RECURSIVE INCLUDE
591 // This isn't sufficient, as the inclusion could be downstream.
592 // But it'll have to do for now.
593 if (buffer().absFileName() == included_file) {
594 Alert::error(_("Recursive input"),
595 bformat(_("Attempted to include file %1$s in itself! "
596 "Ignoring inclusion."), from_utf8(incfile)));
600 // write it to a file (so far the complete file)
601 string const exportfile = changeExtension(incfile, ".sgml");
602 DocFileName writefile(changeExtension(included_file, ".sgml"));
604 if (loadIfNeeded(buffer(), params())) {
605 Buffer * tmp = theBufferList().getBuffer(included_file);
607 string const mangled = writefile.mangledFilename();
608 writefile = makeAbsPath(mangled,
609 buffer().masterBuffer()->temppath());
613 LYXERR(Debug::LATEX, "incfile:" << incfile);
614 LYXERR(Debug::LATEX, "exportfile:" << exportfile);
615 LYXERR(Debug::LATEX, "writefile:" << writefile);
617 tmp->makeDocBookFile(writefile, runparams, true);
620 runparams.exportdata->addExternalFile("docbook", writefile,
622 runparams.exportdata->addExternalFile("docbook-xml", writefile,
625 if (isVerbatim(params()) || isListings(params())) {
626 os << "<inlinegraphic fileref=\""
627 << '&' << include_label << ';'
628 << "\" format=\"linespecific\">";
630 os << '&' << include_label << ';';
636 void InsetInclude::validate(LaTeXFeatures & features) const
638 string incfile = to_utf8(params()["filename"]);
641 LASSERT(&buffer() == &features.buffer(), /**/);
643 string const included_file =
644 includedFilename(buffer(), params()).absFilename();
646 if (isLyXFilename(included_file))
647 writefile = changeExtension(included_file, ".sgml");
649 writefile = included_file;
651 if (!features.runparams().nice && !isVerbatim(params()) && !isListings(params())) {
652 incfile = DocFileName(writefile).mangledFilename();
653 writefile = makeAbsPath(incfile,
654 buffer().masterBuffer()->temppath()).absFilename();
657 features.includeFile(include_label, writefile);
659 if (isVerbatim(params()))
660 features.require("verbatim");
661 else if (isListings(params()))
662 features.require("listings");
664 // Here we must do the fun stuff...
665 // Load the file in the include if it needs
667 if (loadIfNeeded(buffer(), params())) {
669 Buffer * const tmp = theBufferList().getBuffer(included_file);
670 // make sure the buffer isn't us
671 // FIXME RECURSIVE INCLUDES
672 // This is not sufficient, as recursive includes could be
673 // more than a file away. But it will do for now.
674 if (tmp && tmp != &buffer()) {
675 // We must temporarily change features.buffer,
676 // otherwise it would always be the master buffer,
677 // and nested includes would not work.
678 features.setBuffer(*tmp);
679 tmp->validate(features);
680 features.setBuffer(buffer());
686 void InsetInclude::fillWithBibKeys(BiblioInfo & keys,
687 InsetIterator const & /*di*/) const
689 if (loadIfNeeded(buffer(), params())) {
690 string const included_file = includedFilename(buffer(), params()).absFilename();
691 Buffer * tmp = theBufferList().getBuffer(included_file);
692 BiblioInfo const & newkeys = tmp->localBibInfo();
693 keys.mergeBiblioInfo(newkeys);
698 void InsetInclude::updateBibfilesCache()
700 Buffer * const tmp = getChildBuffer(buffer(), params());
703 tmp->updateBibfilesCache();
704 tmp->setParent(&buffer());
709 support::FileNameList const &
710 InsetInclude::getBibfilesCache(Buffer const & buffer) const
712 Buffer * const tmp = getChildBuffer(buffer, params());
715 support::FileNameList const & cache = tmp->getBibfilesCache();
716 tmp->setParent(&buffer);
719 static support::FileNameList const empty;
724 void InsetInclude::metrics(MetricsInfo & mi, Dimension & dim) const
726 LASSERT(mi.base.bv, /**/);
728 bool use_preview = false;
729 if (RenderPreview::status() != LyXRC::PREVIEW_OFF) {
730 graphics::PreviewImage const * pimage =
731 preview_->getPreviewImage(mi.base.bv->buffer());
732 use_preview = pimage && pimage->image();
736 preview_->metrics(mi, dim);
740 button_.update(screenLabel(), true);
742 button_.metrics(mi, dim);
745 Box b(0, dim.wid, -dim.asc, dim.des);
750 void InsetInclude::draw(PainterInfo & pi, int x, int y) const
752 LASSERT(pi.base.bv, /**/);
754 bool use_preview = false;
755 if (RenderPreview::status() != LyXRC::PREVIEW_OFF) {
756 graphics::PreviewImage const * pimage =
757 preview_->getPreviewImage(pi.base.bv->buffer());
758 use_preview = pimage && pimage->image();
762 preview_->draw(pi, x, y);
764 button_.draw(pi, x, y);
768 docstring InsetInclude::contextMenu(BufferView const &, int, int) const
770 return from_ascii("context-include");
774 Inset::DisplayType InsetInclude::display() const
776 return type(params()) == INPUT ? Inline : AlignCenter;
785 void InsetInclude::fileChanged() const
787 Buffer const * const buffer = updateFrontend();
791 preview_->removePreview(*buffer);
792 add_preview(*preview_.get(), *this, *buffer);
793 preview_->startLoading(*buffer);
799 bool preview_wanted(InsetCommandParams const & params, Buffer const & buffer)
801 FileName const included_file = includedFilename(buffer, params);
803 return type(params) == INPUT && params.preview() &&
804 included_file.isReadableFile();
808 docstring latexString(InsetInclude const & inset)
811 // We don't need to set runparams.encoding since this will be done
812 // by latex() anyway.
813 OutputParams runparams(0);
814 runparams.flavor = OutputParams::LATEX;
815 inset.latex(os, runparams);
821 void add_preview(RenderMonitoredPreview & renderer, InsetInclude const & inset,
822 Buffer const & buffer)
824 InsetCommandParams const & params = inset.params();
825 if (RenderPreview::status() != LyXRC::PREVIEW_OFF &&
826 preview_wanted(params, buffer)) {
827 renderer.setAbsFile(includedFilename(buffer, params));
828 docstring const snippet = latexString(inset);
829 renderer.addPreview(snippet, buffer);
836 void InsetInclude::addPreview(graphics::PreviewLoader & ploader) const
838 Buffer const & buffer = ploader.buffer();
839 if (!preview_wanted(params(), buffer))
841 preview_->setAbsFile(includedFilename(buffer, params()));
842 docstring const snippet = latexString(*this);
843 preview_->addPreview(snippet, ploader);
847 void InsetInclude::addToToc(ParConstIterator const & cpit) const
849 TocBackend & backend = buffer().tocBackend();
851 if (isListings(params())) {
853 label_->addToToc(cpit);
855 InsetListingsParams p(to_utf8(params()["lstparams"]));
856 string caption = p.getParamValue("caption");
859 Toc & toc = backend.toc("listing");
860 docstring str = convert<docstring>(toc.size() + 1)
861 + ". " + from_utf8(caption);
862 ParConstIterator pit = cpit;
863 pit.push_back(*this);
864 toc.push_back(TocItem(pit, 0, str));
867 Buffer const * const childbuffer = getChildBuffer(buffer(), params());
871 Toc & toc = backend.toc("child");
872 docstring str = childbuffer->fileName().displayName();
873 toc.push_back(TocItem(cpit, 0, str));
875 TocList & toclist = backend.tocs();
876 TocList const & childtoclist = childbuffer->tocBackend().tocs();
877 TocList::const_iterator it = childtoclist.begin();
878 TocList::const_iterator const end = childtoclist.end();
879 for(; it != end; ++it)
880 toclist[it->first].insert(toclist[it->first].end(),
881 it->second.begin(), it->second.end());
885 void InsetInclude::updateLabels(ParIterator const & it)
887 Buffer const * const childbuffer = getChildBuffer(buffer(), params());
889 lyx::updateLabels(*childbuffer, true);
892 if (!isListings(params()))
896 label_->updateLabels(it);
898 InsetListingsParams const par(to_utf8(params()["lstparams"]));
899 if (par.getParamValue("caption").empty()) {
900 listings_label_.clear();
903 Counters & counters = buffer().params().documentClass().counters();
904 docstring const cnt = from_ascii("listing");
905 listings_label_ = buffer().B_("Program Listing");
906 if (counters.hasCounter(cnt)) {
908 listings_label_ += " " + convert<docstring>(counters.value(cnt));