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"
17 #include "buffer_funcs.h"
18 #include "BufferList.h"
19 #include "BufferParams.h"
20 #include "BufferView.h"
22 #include "DispatchResult.h"
23 #include "ErrorList.h"
26 #include "FuncRequest.h"
27 #include "FuncStatus.h"
28 #include "LaTeXFeatures.h"
29 #include "LayoutFile.h"
30 #include "LayoutModuleList.h"
34 #include "MetricsInfo.h"
35 #include "output_xhtml.h"
36 #include "OutputParams.h"
37 #include "TextClass.h"
38 #include "TocBackend.h"
40 #include "frontends/alert.h"
41 #include "frontends/Painter.h"
43 #include "graphics/PreviewImage.h"
44 #include "graphics/PreviewLoader.h"
46 #include "insets/InsetLabel.h"
47 #include "insets/InsetListingsParams.h"
48 #include "insets/RenderPreview.h"
50 #include "support/convert.h"
51 #include "support/debug.h"
52 #include "support/docstream.h"
53 #include "support/FileNameList.h"
54 #include "support/filetools.h"
55 #include "support/gettext.h"
56 #include "support/lassert.h"
57 #include "support/lstrings.h" // contains
58 #include "support/lyxalgo.h"
60 #include <boost/bind.hpp>
63 using namespace lyx::support;
67 namespace Alert = frontend::Alert;
72 docstring const uniqueID()
74 static unsigned int seed = 1000;
75 return "file" + convert<docstring>(++seed);
79 /// the type of inclusion
81 INCLUDE, VERB, INPUT, VERBAST, LISTINGS, NONE
85 Types type(string const & s)
89 if (s == "verbatiminput")
91 if (s == "verbatiminput*")
93 if (s == "lstinputlisting")
101 Types type(InsetCommandParams const & params)
103 return type(params.getCmdName());
107 bool isListings(InsetCommandParams const & params)
109 return type(params) == LISTINGS;
113 bool isVerbatim(InsetCommandParams const & params)
115 Types const t = type(params);
116 return t == VERB || t == VERBAST;
120 bool isInputOrInclude(InsetCommandParams const & params)
122 Types const t = type(params);
123 return t == INPUT || t == INCLUDE;
127 FileName const masterFileName(Buffer const & buffer)
129 return buffer.masterBuffer()->fileName();
133 void add_preview(RenderMonitoredPreview &, InsetInclude const &, Buffer const &);
136 string const parentFilename(Buffer const & buffer)
138 return buffer.absFileName();
142 FileName const includedFilename(Buffer const & buffer,
143 InsetCommandParams const & params)
145 return makeAbsPath(to_utf8(params["filename"]),
146 onlyPath(parentFilename(buffer)));
150 InsetLabel * createLabel(Buffer * buf, docstring const & label_str)
152 if (label_str.empty())
154 InsetCommandParams icp(LABEL_CODE);
155 icp["name"] = label_str;
156 return new InsetLabel(buf, icp);
162 InsetInclude::InsetInclude(Buffer * buf, InsetCommandParams const & p)
163 : InsetCommand(buf, p, "include"), include_label(uniqueID()),
164 preview_(new RenderMonitoredPreview(this)), failedtoload_(false),
165 set_label_(false), label_(0), child_buffer_(0)
167 preview_->fileChanged(boost::bind(&InsetInclude::fileChanged, this));
169 if (isListings(params())) {
170 InsetListingsParams listing_params(to_utf8(p["lstparams"]));
171 label_ = createLabel(buffer_, from_utf8(listing_params.getParamValue("label")));
176 InsetInclude::InsetInclude(InsetInclude const & other)
177 : InsetCommand(other), include_label(other.include_label),
178 preview_(new RenderMonitoredPreview(this)), failedtoload_(false),
179 set_label_(false), label_(0), child_buffer_(0)
181 preview_->fileChanged(boost::bind(&InsetInclude::fileChanged, this));
184 label_ = new InsetLabel(*other.label_);
188 InsetInclude::~InsetInclude()
194 void InsetInclude::setBuffer(Buffer & buffer)
196 InsetCommand::setBuffer(buffer);
198 label_->setBuffer(buffer);
202 void InsetInclude::setChildBuffer(Buffer * buffer)
204 child_buffer_ = buffer;
208 ParamInfo const & InsetInclude::findInfo(string const & /* cmdName */)
211 // This is only correct for the case of listings, but it'll do for now.
212 // In the other cases, this second parameter should just be empty.
213 static ParamInfo param_info_;
214 if (param_info_.empty()) {
215 param_info_.add("filename", ParamInfo::LATEX_REQUIRED);
216 param_info_.add("lstparams", ParamInfo::LATEX_OPTIONAL);
222 bool InsetInclude::isCompatibleCommand(string const & s)
224 return type(s) != NONE;
228 void InsetInclude::doDispatch(Cursor & cur, FuncRequest & cmd)
230 switch (cmd.action) {
232 case LFUN_INSET_EDIT: {
233 editIncluded(to_utf8(params()["filename"]));
237 case LFUN_INSET_MODIFY: {
238 // It should be OK just to invalidate the cache is setParams()
240 // child_buffer_ = 0;
241 InsetCommandParams p(INCLUDE_CODE);
242 if (cmd.getArg(0) == "changetype") {
243 InsetCommand::doDispatch(cur, cmd);
246 InsetCommand::string2params("include", to_utf8(cmd.argument()), p);
247 if (!p.getCmdName().empty()) {
249 InsetListingsParams new_params(to_utf8(p["lstparams"]));
250 docstring const new_label =
251 from_utf8(new_params.getParamValue("label"));
253 if (new_label.empty()) {
259 old_label = label_->getParam("name");
261 label_ = createLabel(buffer_, new_label);
262 label_->setBuffer(buffer());
265 if (new_label != old_label) {
266 label_->updateCommand(new_label);
267 // the label might have been adapted (duplicate)
268 if (new_label != label_->getParam("name")) {
269 new_params.addParam("label", "{" +
270 to_utf8(label_->getParam("name")) + "}", true);
271 p["lstparams"] = from_utf8(new_params.params());
282 //pass everything else up the chain
284 InsetCommand::doDispatch(cur, cmd);
290 void InsetInclude::editIncluded(string const & file)
292 string const ext = support::getExtension(file);
294 FuncRequest fr(LFUN_BUFFER_CHILD_OPEN, file);
297 // tex file or other text file in verbatim mode
298 formats.edit(buffer(),
299 support::makeAbsPath(file, support::onlyPath(buffer().absFileName())),
304 bool InsetInclude::getStatus(Cursor & cur, FuncRequest const & cmd,
305 FuncStatus & flag) const
307 switch (cmd.action) {
309 case LFUN_INSET_EDIT:
310 flag.setEnabled(true);
313 case LFUN_INSET_MODIFY:
314 if (cmd.getArg(0) == "changetype")
315 return InsetCommand::getStatus(cur, cmd, flag);
317 flag.setEnabled(true);
321 return InsetCommand::getStatus(cur, cmd, flag);
326 void InsetInclude::setParams(InsetCommandParams const & p)
328 // invalidate the cache
331 InsetCommand::setParams(p);
334 if (preview_->monitoring())
335 preview_->stopMonitoring();
337 if (type(params()) == INPUT)
338 add_preview(*preview_, *this, buffer());
340 buffer().updateBibfilesCache();
344 bool InsetInclude::isChildIncluded() const
346 std::list<std::string> includeonlys =
347 buffer().params().getIncludedChildren();
348 if (includeonlys.empty())
350 return (std::find(includeonlys.begin(),
352 to_utf8(params()["filename"])) != includeonlys.end());
356 docstring InsetInclude::screenLabel() const
360 switch (type(params())) {
362 temp = buffer().B_("Input");
365 temp = buffer().B_("Verbatim Input");
368 temp = buffer().B_("Verbatim Input*");
371 if (isChildIncluded())
372 temp = buffer().B_("Include");
374 temp += buffer().B_("Include (excluded)");
377 temp = listings_label_;
380 LASSERT(false, /**/);
385 if (params()["filename"].empty())
388 temp += from_utf8(onlyFilename(to_utf8(params()["filename"])));
394 Buffer * InsetInclude::getChildBuffer() const
396 Buffer * childBuffer = loadIfNeeded();
398 // FIXME: recursive includes
399 return (childBuffer == &buffer()) ? 0 : childBuffer;
403 Buffer * InsetInclude::loadIfNeeded() const
405 // This is for background export and preview. We don't want to load the
406 // cloned child document again.
407 if (child_buffer_ && theBufferList().isLoaded(child_buffer_)
408 && child_buffer_->isClone())
409 return child_buffer_;
411 // Don't try to load it again if we failed before.
412 if (failedtoload_ || isVerbatim(params()) || isListings(params()))
415 // Use cached Buffer if possible.
416 if (child_buffer_ != 0) {
417 if (theBufferList().isLoaded(child_buffer_))
418 return child_buffer_;
419 // Buffer vanished, so invalidate cache and try to reload.
423 string const parent_filename = buffer().absFileName();
424 FileName const included_file =
425 makeAbsPath(to_utf8(params()["filename"]), onlyPath(parent_filename));
427 if (!isLyXFilename(included_file.absFilename()))
430 Buffer * child = theBufferList().getBuffer(included_file);
432 // the readonly flag can/will be wrong, not anymore I think.
433 if (!included_file.exists())
436 child = theBufferList().newBuffer(included_file.absFilename());
438 // Buffer creation is not possible.
441 if (!child->loadLyXFile(included_file)) {
442 failedtoload_ = true;
443 //close the buffer we just opened
444 theBufferList().release(child);
448 if (!child->errorList("Parse").empty()) {
449 // FIXME: Do something.
452 child->setParent(&buffer());
453 // Cache the child buffer.
454 child_buffer_ = child;
459 int InsetInclude::latex(odocstream & os, OutputParams const & runparams) const
461 string incfile = to_utf8(params()["filename"]);
463 // Do nothing if no file name has been specified
467 FileName const included_file = includedFilename(buffer(), params());
469 // Check we're not trying to include ourselves.
470 // FIXME RECURSIVE INCLUDE
471 // This isn't sufficient, as the inclusion could be downstream.
472 // But it'll have to do for now.
473 if (isInputOrInclude(params()) &&
474 buffer().absFileName() == included_file.absFilename())
476 Alert::error(_("Recursive input"),
477 bformat(_("Attempted to include file %1$s in itself! "
478 "Ignoring inclusion."), from_utf8(incfile)));
482 Buffer const * const masterBuffer = buffer().masterBuffer();
484 // if incfile is relative, make it relative to the master
486 if (!FileName::isAbsolute(incfile)) {
488 incfile = to_utf8(makeRelPath(from_utf8(included_file.absFilename()),
489 from_utf8(masterBuffer->filePath())));
492 // write it to a file (so far the complete file)
496 if (type(params()) == LISTINGS) {
497 exportfile = incfile;
498 mangled = DocFileName(included_file).mangledFilename();
500 exportfile = changeExtension(incfile, ".tex");
501 mangled = DocFileName(changeExtension(included_file.absFilename(), ".tex")).
505 FileName const writefile(makeAbsPath(mangled, masterBuffer->temppath()));
509 else if (!isValidLaTeXFilename(incfile)) {
510 frontend::Alert::warning(_("Invalid filename"),
511 _("The following filename is likely to cause trouble "
512 "when running the exported file through LaTeX: ") +
515 LYXERR(Debug::LATEX, "incfile:" << incfile);
516 LYXERR(Debug::LATEX, "exportfile:" << exportfile);
517 LYXERR(Debug::LATEX, "writefile:" << writefile);
519 if (runparams.inComment || runparams.dryrun) {
520 //Don't try to load or copy the file if we're
521 //in a comment or doing a dryrun
522 } else if (isInputOrInclude(params()) &&
523 isLyXFilename(included_file.absFilename())) {
524 //if it's a LyX file and we're inputting or including,
525 //try to load it so we can write the associated latex
529 Buffer * tmp = theBufferList().getBuffer(included_file);
531 if (tmp->params().baseClass() != masterBuffer->params().baseClass()) {
533 docstring text = bformat(_("Included file `%1$s'\n"
534 "has textclass `%2$s'\n"
535 "while parent file has textclass `%3$s'."),
536 included_file.displayName(),
537 from_utf8(tmp->params().documentClass().name()),
538 from_utf8(masterBuffer->params().documentClass().name()));
539 Alert::warning(_("Different textclasses"), text, true);
542 // Make sure modules used in child are all included in master
543 // FIXME It might be worth loading the children's modules into the master
544 // over in BufferParams rather than doing this check.
545 LayoutModuleList const masterModules = masterBuffer->params().getModules();
546 LayoutModuleList const childModules = tmp->params().getModules();
547 LayoutModuleList::const_iterator it = childModules.begin();
548 LayoutModuleList::const_iterator end = childModules.end();
549 for (; it != end; ++it) {
550 string const module = *it;
551 LayoutModuleList::const_iterator found =
552 find(masterModules.begin(), masterModules.end(), module);
553 if (found == masterModules.end()) {
554 docstring text = bformat(_("Included file `%1$s'\n"
555 "uses module `%2$s'\n"
556 "which is not used in parent file."),
557 included_file.displayName(), from_utf8(module));
558 Alert::warning(_("Module not found"), text);
562 tmp->markDepClean(masterBuffer->temppath());
564 // FIXME: handle non existing files
565 // FIXME: Second argument is irrelevant!
566 // since only_body is true, makeLaTeXFile will not look at second
567 // argument. Should we set it to string(), or should makeLaTeXFile
568 // make use of it somehow? (JMarc 20031002)
569 // The included file might be written in a different encoding
571 Encoding const * const oldEnc = runparams.encoding;
572 Language const * const oldLang = runparams.master_language;
573 runparams.encoding = &tmp->params().encoding();
574 runparams.master_language = buffer().params().language;
575 tmp->makeLaTeXFile(writefile,
576 masterFileName(buffer()).onlyPath().absFilename(),
578 runparams.encoding = oldEnc;
579 runparams.master_language = oldLang;
581 // In this case, it's not a LyX file, so we copy the file
582 // to the temp dir, so that .aux files etc. are not created
583 // in the original dir. Files included by this file will be
584 // found via input@path, see ../Buffer.cpp.
585 unsigned long const checksum_in = included_file.checksum();
586 unsigned long const checksum_out = writefile.checksum();
588 if (checksum_in != checksum_out) {
589 if (!included_file.copyTo(writefile)) {
592 to_utf8(bformat(_("Could not copy the file\n%1$s\n"
593 "into the temporary directory."),
594 from_utf8(included_file.absFilename()))));
600 string const tex_format = (runparams.flavor == OutputParams::LATEX) ?
601 "latex" : "pdflatex";
602 switch (type(params())) {
605 incfile = latex_path(incfile);
607 os << '\\' << from_ascii(params().getCmdName()) << '{'
608 << from_utf8(incfile) << '}';
612 runparams.exportdata->addExternalFile(tex_format, writefile,
615 // \input wants file with extension (default is .tex)
616 if (!isLyXFilename(included_file.absFilename())) {
617 incfile = latex_path(incfile);
619 os << '\\' << from_ascii(params().getCmdName())
620 << '{' << from_utf8(incfile) << '}';
622 incfile = changeExtension(incfile, ".tex");
623 incfile = latex_path(incfile);
625 os << '\\' << from_ascii(params().getCmdName())
626 << '{' << from_utf8(incfile) << '}';
631 os << '\\' << from_ascii(params().getCmdName());
632 string const opt = to_utf8(params()["lstparams"]);
633 // opt is set in QInclude dialog and should have passed validation.
634 InsetListingsParams params(opt);
635 if (!params.params().empty())
636 os << "[" << from_utf8(params.params()) << "]";
637 os << '{' << from_utf8(incfile) << '}';
641 runparams.exportdata->addExternalFile(tex_format, writefile,
644 // \include don't want extension and demands that the
645 // file really have .tex
646 incfile = changeExtension(incfile, string());
647 incfile = latex_path(incfile);
649 os << '\\' << from_ascii(params().getCmdName()) << '{'
650 << from_utf8(incfile) << '}';
661 docstring InsetInclude::xhtml(XHTMLStream & xs, OutputParams const &rp) const
666 // For verbatim and listings, we just include the contents of the file as-is.
667 // In the case of listings, we wrap it in <pre>.
668 bool const listing = isListings(params());
669 if (listing || isVerbatim(params())) {
671 xs << html::StartTag("pre");
672 // FIXME: We don't know the encoding of the file, default to UTF-8.
673 xs << includedFilename(buffer(), params()).fileContents("UTF-8");
675 xs << html::EndTag("pre");
679 // We don't (yet) know how to Input or Include non-LyX files.
680 // (If we wanted to get really arcane, we could run some tex2html
681 // converter on the included file. But that's just masochistic.)
682 string const parent_filename = buffer().absFileName();
683 FileName const included_file =
684 makeAbsPath(to_utf8(params()["filename"]), onlyPath(parent_filename));
685 if (!isLyXFilename(included_file.absFilename())) {
686 frontend::Alert::warning(_("Unsupported Inclusion"),
687 bformat(_("LyX does not know how to include non-LyX files when "
688 "generating HTML output. Offending file:\n%1$s"),
689 params()["filename"]));
693 // In the other cases, we will generate the HTML and include it.
695 // Check we're not trying to include ourselves.
696 // FIXME RECURSIVE INCLUDE
697 if (buffer().absFileName() == included_file.absFilename()) {
698 Alert::error(_("Recursive input"),
699 bformat(_("Attempted to include file %1$s in itself! "
700 "Ignoring inclusion."), params()["filename"]));
704 Buffer const * const ibuf = loadIfNeeded();
707 ibuf->writeLyXHTMLSource(xs.os(), rp, true);
712 int InsetInclude::plaintext(odocstream & os, OutputParams const &) const
714 if (isVerbatim(params()) || isListings(params())) {
715 os << '[' << screenLabel() << '\n';
716 // FIXME: We don't know the encoding of the file, default to UTF-8.
717 os << includedFilename(buffer(), params()).fileContents("UTF-8");
719 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
721 docstring const str = '[' + screenLabel() + ']';
728 int InsetInclude::docbook(odocstream & os, OutputParams const & runparams) const
730 string incfile = to_utf8(params()["filename"]);
732 // Do nothing if no file name has been specified
736 string const included_file = includedFilename(buffer(), params()).absFilename();
738 // Check we're not trying to include ourselves.
739 // FIXME RECURSIVE INCLUDE
740 // This isn't sufficient, as the inclusion could be downstream.
741 // But it'll have to do for now.
742 if (buffer().absFileName() == included_file) {
743 Alert::error(_("Recursive input"),
744 bformat(_("Attempted to include file %1$s in itself! "
745 "Ignoring inclusion."), from_utf8(incfile)));
749 // write it to a file (so far the complete file)
750 string const exportfile = changeExtension(incfile, ".sgml");
751 DocFileName writefile(changeExtension(included_file, ".sgml"));
753 if (loadIfNeeded()) {
754 Buffer * tmp = theBufferList().getBuffer(FileName(included_file));
756 string const mangled = writefile.mangledFilename();
757 writefile = makeAbsPath(mangled,
758 buffer().masterBuffer()->temppath());
762 LYXERR(Debug::LATEX, "incfile:" << incfile);
763 LYXERR(Debug::LATEX, "exportfile:" << exportfile);
764 LYXERR(Debug::LATEX, "writefile:" << writefile);
766 tmp->makeDocBookFile(writefile, runparams, true);
769 runparams.exportdata->addExternalFile("docbook", writefile,
771 runparams.exportdata->addExternalFile("docbook-xml", writefile,
774 if (isVerbatim(params()) || isListings(params())) {
775 os << "<inlinegraphic fileref=\""
776 << '&' << include_label << ';'
777 << "\" format=\"linespecific\">";
779 os << '&' << include_label << ';';
785 void InsetInclude::validate(LaTeXFeatures & features) const
787 string incfile = to_utf8(params()["filename"]);
790 LASSERT(&buffer() == &features.buffer(), /**/);
792 string const included_file =
793 includedFilename(buffer(), params()).absFilename();
795 if (isLyXFilename(included_file))
796 writefile = changeExtension(included_file, ".sgml");
798 writefile = included_file;
800 if (!features.runparams().nice && !isVerbatim(params()) && !isListings(params())) {
801 incfile = DocFileName(writefile).mangledFilename();
802 writefile = makeAbsPath(incfile,
803 buffer().masterBuffer()->temppath()).absFilename();
806 features.includeFile(include_label, writefile);
808 if (isVerbatim(params()))
809 features.require("verbatim");
810 else if (isListings(params()))
811 features.require("listings");
813 // Here we must do the fun stuff...
814 // Load the file in the include if it needs
816 if (loadIfNeeded()) {
818 Buffer * const tmp = theBufferList().getBuffer(FileName(included_file));
819 // make sure the buffer isn't us
820 // FIXME RECURSIVE INCLUDES
821 // This is not sufficient, as recursive includes could be
822 // more than a file away. But it will do for now.
823 if (tmp && tmp != &buffer()) {
824 // We must temporarily change features.buffer,
825 // otherwise it would always be the master buffer,
826 // and nested includes would not work.
827 features.setBuffer(*tmp);
828 tmp->validate(features);
829 features.setBuffer(buffer());
835 void InsetInclude::fillWithBibKeys(BiblioInfo & keys,
836 InsetIterator const & /*di*/) const
838 if (loadIfNeeded()) {
839 string const included_file = includedFilename(buffer(), params()).absFilename();
840 Buffer * tmp = theBufferList().getBuffer(FileName(included_file));
841 BiblioInfo const & newkeys = tmp->localBibInfo();
842 keys.mergeBiblioInfo(newkeys);
847 void InsetInclude::updateBibfilesCache()
849 Buffer const * const child = getChildBuffer();
851 child->updateBibfilesCache(Buffer::UpdateChildOnly);
855 support::FileNameList const &
856 InsetInclude::getBibfilesCache() const
858 Buffer const * const child = getChildBuffer();
860 return child->getBibfilesCache(Buffer::UpdateChildOnly);
862 static support::FileNameList const empty;
867 void InsetInclude::metrics(MetricsInfo & mi, Dimension & dim) const
869 LASSERT(mi.base.bv, /**/);
871 bool use_preview = false;
872 if (RenderPreview::status() != LyXRC::PREVIEW_OFF) {
873 graphics::PreviewImage const * pimage =
874 preview_->getPreviewImage(mi.base.bv->buffer());
875 use_preview = pimage && pimage->image();
879 preview_->metrics(mi, dim);
883 button_.update(screenLabel(), true);
885 button_.metrics(mi, dim);
888 Box b(0, dim.wid, -dim.asc, dim.des);
893 void InsetInclude::draw(PainterInfo & pi, int x, int y) const
895 LASSERT(pi.base.bv, /**/);
897 bool use_preview = false;
898 if (RenderPreview::status() != LyXRC::PREVIEW_OFF) {
899 graphics::PreviewImage const * pimage =
900 preview_->getPreviewImage(pi.base.bv->buffer());
901 use_preview = pimage && pimage->image();
905 preview_->draw(pi, x, y);
907 button_.draw(pi, x, y);
911 docstring InsetInclude::contextMenu(BufferView const &, int, int) const
913 return from_ascii("context-include");
917 Inset::DisplayType InsetInclude::display() const
919 return type(params()) == INPUT ? Inline : AlignCenter;
928 void InsetInclude::fileChanged() const
930 Buffer const * const buffer = updateFrontend();
934 preview_->removePreview(*buffer);
935 add_preview(*preview_.get(), *this, *buffer);
936 preview_->startLoading(*buffer);
942 bool preview_wanted(InsetCommandParams const & params, Buffer const & buffer)
944 FileName const included_file = includedFilename(buffer, params);
946 return type(params) == INPUT && params.preview() &&
947 included_file.isReadableFile();
951 docstring latexString(InsetInclude const & inset)
954 // We don't need to set runparams.encoding since this will be done
955 // by latex() anyway.
956 OutputParams runparams(0);
957 runparams.flavor = OutputParams::LATEX;
958 inset.latex(os, runparams);
964 void add_preview(RenderMonitoredPreview & renderer, InsetInclude const & inset,
965 Buffer const & buffer)
967 InsetCommandParams const & params = inset.params();
968 if (RenderPreview::status() != LyXRC::PREVIEW_OFF &&
969 preview_wanted(params, buffer)) {
970 renderer.setAbsFile(includedFilename(buffer, params));
971 docstring const snippet = latexString(inset);
972 renderer.addPreview(snippet, buffer);
979 void InsetInclude::addPreview(DocIterator const & /*inset_pos*/,
980 graphics::PreviewLoader & ploader) const
982 Buffer const & buffer = ploader.buffer();
983 if (!preview_wanted(params(), buffer))
985 preview_->setAbsFile(includedFilename(buffer, params()));
986 docstring const snippet = latexString(*this);
987 preview_->addPreview(snippet, ploader);
991 void InsetInclude::addToToc(DocIterator const & cpit)
993 TocBackend & backend = buffer().tocBackend();
995 if (isListings(params())) {
997 label_->addToToc(cpit);
999 InsetListingsParams p(to_utf8(params()["lstparams"]));
1000 string caption = p.getParamValue("caption");
1001 if (caption.empty())
1003 Toc & toc = backend.toc("listing");
1004 docstring str = convert<docstring>(toc.size() + 1)
1005 + ". " + from_utf8(caption);
1006 DocIterator pit = cpit;
1007 toc.push_back(TocItem(pit, 0, str));
1010 Buffer const * const childbuffer = getChildBuffer();
1014 Toc & toc = backend.toc("child");
1015 docstring str = childbuffer->fileName().displayName();
1016 toc.push_back(TocItem(cpit, 0, str));
1018 TocList & toclist = backend.tocs();
1019 childbuffer->tocBackend().update();
1020 TocList const & childtoclist = childbuffer->tocBackend().tocs();
1021 TocList::const_iterator it = childtoclist.begin();
1022 TocList::const_iterator const end = childtoclist.end();
1023 for(; it != end; ++it)
1024 toclist[it->first].insert(toclist[it->first].end(),
1025 it->second.begin(), it->second.end());
1029 void InsetInclude::updateCommand()
1034 docstring old_label = label_->getParam("name");
1035 label_->updateCommand(old_label, false);
1036 // the label might have been adapted (duplicate)
1037 docstring new_label = label_->getParam("name");
1038 if (old_label == new_label)
1041 // update listings parameters...
1042 InsetCommandParams p(INCLUDE_CODE);
1044 InsetListingsParams par(to_utf8(params()["lstparams"]));
1045 par.addParam("label", "{" + to_utf8(new_label) + "}", true);
1046 p["lstparams"] = from_utf8(par.params());
1050 void InsetInclude::updateLabels(ParIterator const & it, UpdateType utype)
1052 Buffer const * const childbuffer = getChildBuffer();
1054 childbuffer->updateLabels(Buffer::UpdateChildOnly, utype);
1057 if (!isListings(params()))
1061 label_->updateLabels(it, utype);
1063 InsetListingsParams const par(to_utf8(params()["lstparams"]));
1064 if (par.getParamValue("caption").empty()) {
1065 listings_label_ = buffer().B_("Program Listing");
1068 Buffer const & master = *buffer().masterBuffer();
1069 Counters & counters = master.params().documentClass().counters();
1070 docstring const cnt = from_ascii("listing");
1071 listings_label_ = master.B_("Program Listing");
1072 if (counters.hasCounter(cnt)) {
1073 counters.step(cnt, utype);
1074 listings_label_ += " " + convert<docstring>(counters.value(cnt));