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 "support/bind.h"
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(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(bind(&InsetInclude::fileChanged, this));
184 label_ = new InsetLabel(*other.label_);
188 InsetInclude::~InsetInclude()
190 if (isBufferLoaded())
191 buffer().invalidateBibfileCache();
196 void InsetInclude::setBuffer(Buffer & buffer)
198 InsetCommand::setBuffer(buffer);
200 label_->setBuffer(buffer);
204 void InsetInclude::setChildBuffer(Buffer * buffer)
206 child_buffer_ = buffer;
210 ParamInfo const & InsetInclude::findInfo(string const & /* cmdName */)
213 // This is only correct for the case of listings, but it'll do for now.
214 // In the other cases, this second parameter should just be empty.
215 static ParamInfo param_info_;
216 if (param_info_.empty()) {
217 param_info_.add("filename", ParamInfo::LATEX_REQUIRED);
218 param_info_.add("lstparams", ParamInfo::LATEX_OPTIONAL);
224 bool InsetInclude::isCompatibleCommand(string const & s)
226 return type(s) != NONE;
230 void InsetInclude::doDispatch(Cursor & cur, FuncRequest & cmd)
232 switch (cmd.action()) {
234 case LFUN_INSET_EDIT: {
235 editIncluded(to_utf8(params()["filename"]));
239 case LFUN_INSET_MODIFY: {
240 // It should be OK just to invalidate the cache in setParams()
242 // child_buffer_ = 0;
243 InsetCommandParams p(INCLUDE_CODE);
244 if (cmd.getArg(0) == "changetype") {
245 InsetCommand::doDispatch(cur, cmd);
248 InsetCommand::string2params("include", to_utf8(cmd.argument()), p);
249 if (!p.getCmdName().empty()) {
251 InsetListingsParams new_params(to_utf8(p["lstparams"]));
252 docstring const new_label =
253 from_utf8(new_params.getParamValue("label"));
255 if (new_label.empty()) {
261 old_label = label_->getParam("name");
263 label_ = createLabel(buffer_, new_label);
264 label_->setBuffer(buffer());
267 if (new_label != old_label) {
268 label_->updateCommand(new_label);
269 // the label might have been adapted (duplicate)
270 if (new_label != label_->getParam("name")) {
271 new_params.addParam("label", "{" +
272 to_utf8(label_->getParam("name")) + "}", true);
273 p["lstparams"] = from_utf8(new_params.params());
279 cur.forceBufferUpdate();
281 cur.noScreenUpdate();
285 //pass everything else up the chain
287 InsetCommand::doDispatch(cur, cmd);
293 void InsetInclude::editIncluded(string const & file)
295 string const ext = support::getExtension(file);
297 FuncRequest fr(LFUN_BUFFER_CHILD_OPEN, file);
300 // tex file or other text file in verbatim mode
301 formats.edit(buffer(),
302 support::makeAbsPath(file, support::onlyPath(buffer().absFileName())),
307 bool InsetInclude::getStatus(Cursor & cur, FuncRequest const & cmd,
308 FuncStatus & flag) const
310 switch (cmd.action()) {
312 case LFUN_INSET_EDIT:
313 flag.setEnabled(true);
316 case LFUN_INSET_MODIFY:
317 if (cmd.getArg(0) == "changetype")
318 return InsetCommand::getStatus(cur, cmd, flag);
320 flag.setEnabled(true);
324 return InsetCommand::getStatus(cur, cmd, flag);
329 void InsetInclude::setParams(InsetCommandParams const & p)
331 // invalidate the cache
334 InsetCommand::setParams(p);
337 if (preview_->monitoring())
338 preview_->stopMonitoring();
340 if (type(params()) == INPUT)
341 add_preview(*preview_, *this, buffer());
343 buffer().invalidateBibfileCache();
347 bool InsetInclude::isChildIncluded() const
349 std::list<std::string> includeonlys =
350 buffer().params().getIncludedChildren();
351 if (includeonlys.empty())
353 return (std::find(includeonlys.begin(),
355 to_utf8(params()["filename"])) != includeonlys.end());
359 docstring InsetInclude::screenLabel() const
363 switch (type(params())) {
365 temp = buffer().B_("Input");
368 temp = buffer().B_("Verbatim Input");
371 temp = buffer().B_("Verbatim Input*");
374 if (isChildIncluded())
375 temp = buffer().B_("Include");
377 temp += buffer().B_("Include (excluded)");
380 temp = listings_label_;
383 LASSERT(false, /**/);
388 if (params()["filename"].empty())
391 temp += from_utf8(onlyFileName(to_utf8(params()["filename"])));
397 Buffer * InsetInclude::getChildBuffer() const
399 Buffer * childBuffer = loadIfNeeded();
401 // FIXME: recursive includes
402 return (childBuffer == &buffer()) ? 0 : childBuffer;
406 Buffer * InsetInclude::loadIfNeeded() const
408 // This is for background export and preview. We don't even want to
409 // try to load the cloned child document again.
410 if (buffer().isClone())
411 return child_buffer_;
413 // Don't try to load it again if we failed before.
414 if (failedtoload_ || isVerbatim(params()) || isListings(params()))
417 FileName const included_file = includedFileName(buffer(), params());
418 // Use cached Buffer if possible.
419 if (child_buffer_ != 0) {
420 if (theBufferList().isLoaded(child_buffer_)
421 // additional sanity check: make sure the Buffer really is
422 // associated with the file we want.
423 && child_buffer_ == theBufferList().getBuffer(included_file))
424 return child_buffer_;
425 // Buffer vanished, so invalidate cache and try to reload.
429 if (!isLyXFileName(included_file.absFileName()))
432 Buffer * child = theBufferList().getBuffer(included_file);
434 // the readonly flag can/will be wrong, not anymore I think.
435 if (!included_file.exists())
438 child = theBufferList().newBuffer(included_file.absFileName());
440 // Buffer creation is not possible.
443 if (!child->loadLyXFile(included_file)) {
444 failedtoload_ = true;
445 //close the buffer we just opened
446 theBufferList().release(child);
450 if (!child->errorList("Parse").empty()) {
451 // FIXME: Do something.
454 child->setParent(&buffer());
455 // Cache the child buffer.
456 child_buffer_ = child;
461 int InsetInclude::latex(odocstream & os, OutputParams const & runparams) const
463 string incfile = to_utf8(params()["filename"]);
465 // Do nothing if no file name has been specified
469 FileName const included_file = includedFileName(buffer(), params());
471 // Check we're not trying to include ourselves.
472 // FIXME RECURSIVE INCLUDE
473 // This isn't sufficient, as the inclusion could be downstream.
474 // But it'll have to do for now.
475 if (isInputOrInclude(params()) &&
476 buffer().absFileName() == included_file.absFileName())
478 Alert::error(_("Recursive input"),
479 bformat(_("Attempted to include file %1$s in itself! "
480 "Ignoring inclusion."), from_utf8(incfile)));
484 Buffer const * const masterBuffer = buffer().masterBuffer();
486 // if incfile is relative, make it relative to the master
488 if (!FileName::isAbsolute(incfile)) {
490 incfile = to_utf8(makeRelPath(from_utf8(included_file.absFileName()),
491 from_utf8(masterBuffer->filePath())));
494 // write it to a file (so far the complete file)
498 if (type(params()) == LISTINGS) {
499 exportfile = incfile;
500 mangled = DocFileName(included_file).mangledFileName();
502 exportfile = changeExtension(incfile, ".tex");
503 mangled = DocFileName(changeExtension(included_file.absFileName(), ".tex")).
507 FileName const writefile(makeAbsPath(mangled, masterBuffer->temppath()));
511 else if (!isValidLaTeXFileName(incfile)) {
512 frontend::Alert::warning(_("Invalid filename"),
513 _("The following filename will cause troubles "
514 "when running the exported file through LaTeX: ") +
517 else if (!isValidDVIFileName(incfile)) {
518 frontend::Alert::warning(_("Problematic filename for DVI"),
519 _("The following filename can cause troubles "
520 "when running the exported file through LaTeX "
521 "and opening the resulting DVI: ") +
522 from_utf8(incfile), true);
524 LYXERR(Debug::LATEX, "incfile:" << incfile);
525 LYXERR(Debug::LATEX, "exportfile:" << exportfile);
526 LYXERR(Debug::LATEX, "writefile:" << writefile);
528 if (runparams.inComment || runparams.dryrun) {
529 //Don't try to load or copy the file if we're
530 //in a comment or doing a dryrun
531 } else if (isInputOrInclude(params()) &&
532 isLyXFileName(included_file.absFileName())) {
533 // if it's a LyX file and we're inputting or including,
534 // try to load it so we can write the associated latex
536 Buffer * tmp = loadIfNeeded();
540 if (tmp->params().baseClass() != masterBuffer->params().baseClass()) {
542 docstring text = bformat(_("Included file `%1$s'\n"
543 "has textclass `%2$s'\n"
544 "while parent file has textclass `%3$s'."),
545 included_file.displayName(),
546 from_utf8(tmp->params().documentClass().name()),
547 from_utf8(masterBuffer->params().documentClass().name()));
548 Alert::warning(_("Different textclasses"), text, true);
551 // Make sure modules used in child are all included in master
552 // FIXME It might be worth loading the children's modules into the master
553 // over in BufferParams rather than doing this check.
554 LayoutModuleList const masterModules = masterBuffer->params().getModules();
555 LayoutModuleList const childModules = tmp->params().getModules();
556 LayoutModuleList::const_iterator it = childModules.begin();
557 LayoutModuleList::const_iterator end = childModules.end();
558 for (; it != end; ++it) {
559 string const module = *it;
560 LayoutModuleList::const_iterator found =
561 find(masterModules.begin(), masterModules.end(), module);
562 if (found == masterModules.end()) {
563 docstring text = bformat(_("Included file `%1$s'\n"
564 "uses module `%2$s'\n"
565 "which is not used in parent file."),
566 included_file.displayName(), from_utf8(module));
567 Alert::warning(_("Module not found"), text);
571 tmp->markDepClean(masterBuffer->temppath());
573 // FIXME: handle non existing files
574 // FIXME: Second argument is irrelevant!
575 // since only_body is true, makeLaTeXFile will not look at second
576 // argument. Should we set it to string(), or should makeLaTeXFile
577 // make use of it somehow? (JMarc 20031002)
578 // The included file might be written in a different encoding
580 Encoding const * const oldEnc = runparams.encoding;
581 Language const * const oldLang = runparams.master_language;
582 runparams.encoding = &tmp->params().encoding();
583 runparams.master_language = buffer().params().language;
584 tmp->makeLaTeXFile(writefile,
585 masterFileName(buffer()).onlyPath().absFileName(),
587 runparams.encoding = oldEnc;
588 runparams.master_language = oldLang;
590 // In this case, it's not a LyX file, so we copy the file
591 // to the temp dir, so that .aux files etc. are not created
592 // in the original dir. Files included by this file will be
593 // found via input@path, see ../Buffer.cpp.
594 unsigned long const checksum_in = included_file.checksum();
595 unsigned long const checksum_out = writefile.checksum();
597 if (checksum_in != checksum_out) {
598 if (!included_file.copyTo(writefile)) {
601 to_utf8(bformat(_("Could not copy the file\n%1$s\n"
602 "into the temporary directory."),
603 from_utf8(included_file.absFileName()))));
609 string const tex_format = (runparams.flavor == OutputParams::LATEX) ?
610 "latex" : "pdflatex";
611 switch (type(params())) {
614 incfile = latex_path(incfile);
616 os << '\\' << from_ascii(params().getCmdName()) << '{'
617 << from_utf8(incfile) << '}';
621 runparams.exportdata->addExternalFile(tex_format, writefile,
624 // \input wants file with extension (default is .tex)
625 if (!isLyXFileName(included_file.absFileName())) {
626 incfile = latex_path(incfile);
628 os << '\\' << from_ascii(params().getCmdName())
629 << '{' << from_utf8(incfile) << '}';
631 incfile = changeExtension(incfile, ".tex");
632 incfile = latex_path(incfile);
634 os << '\\' << from_ascii(params().getCmdName())
635 << '{' << from_utf8(incfile) << '}';
640 os << '\\' << from_ascii(params().getCmdName());
641 string const opt = to_utf8(params()["lstparams"]);
642 // opt is set in QInclude dialog and should have passed validation.
643 InsetListingsParams params(opt);
644 if (!params.params().empty())
645 os << "[" << from_utf8(params.params()) << "]";
646 os << '{' << from_utf8(incfile) << '}';
650 runparams.exportdata->addExternalFile(tex_format, writefile,
653 // \include don't want extension and demands that the
654 // file really have .tex
655 incfile = changeExtension(incfile, string());
656 incfile = latex_path(incfile);
658 os << '\\' << from_ascii(params().getCmdName()) << '{'
659 << from_utf8(incfile) << '}';
670 docstring InsetInclude::xhtml(XHTMLStream & xs, OutputParams const &rp) const
675 // For verbatim and listings, we just include the contents of the file as-is.
676 // In the case of listings, we wrap it in <pre>.
677 bool const listing = isListings(params());
678 if (listing || isVerbatim(params())) {
680 xs << html::StartTag("pre");
681 // FIXME: We don't know the encoding of the file, default to UTF-8.
682 xs << includedFileName(buffer(), params()).fileContents("UTF-8");
684 xs << html::EndTag("pre");
688 // We don't (yet) know how to Input or Include non-LyX files.
689 // (If we wanted to get really arcane, we could run some tex2html
690 // converter on the included file. But that's just masochistic.)
691 FileName const included_file = includedFileName(buffer(), params());
692 if (!isLyXFileName(included_file.absFileName())) {
693 frontend::Alert::warning(_("Unsupported Inclusion"),
694 bformat(_("LyX does not know how to include non-LyX files when "
695 "generating HTML output. Offending file:\n%1$s"),
696 params()["filename"]));
700 // In the other cases, we will generate the HTML and include it.
702 // Check we're not trying to include ourselves.
703 // FIXME RECURSIVE INCLUDE
704 if (buffer().absFileName() == included_file.absFileName()) {
705 Alert::error(_("Recursive input"),
706 bformat(_("Attempted to include file %1$s in itself! "
707 "Ignoring inclusion."), params()["filename"]));
711 Buffer const * const ibuf = loadIfNeeded();
714 ibuf->writeLyXHTMLSource(xs.os(), rp, true);
719 int InsetInclude::plaintext(odocstream & os, OutputParams const &) const
721 if (isVerbatim(params()) || isListings(params())) {
722 os << '[' << screenLabel() << '\n';
723 // FIXME: We don't know the encoding of the file, default to UTF-8.
724 os << includedFileName(buffer(), params()).fileContents("UTF-8");
726 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
728 docstring const str = '[' + screenLabel() + ']';
735 int InsetInclude::docbook(odocstream & os, OutputParams const & runparams) const
737 string incfile = to_utf8(params()["filename"]);
739 // Do nothing if no file name has been specified
743 string const included_file = includedFileName(buffer(), params()).absFileName();
745 // Check we're not trying to include ourselves.
746 // FIXME RECURSIVE INCLUDE
747 // This isn't sufficient, as the inclusion could be downstream.
748 // But it'll have to do for now.
749 if (buffer().absFileName() == included_file) {
750 Alert::error(_("Recursive input"),
751 bformat(_("Attempted to include file %1$s in itself! "
752 "Ignoring inclusion."), from_utf8(incfile)));
756 // write it to a file (so far the complete file)
757 string const exportfile = changeExtension(incfile, ".sgml");
758 DocFileName writefile(changeExtension(included_file, ".sgml"));
760 Buffer * tmp = loadIfNeeded();
762 string const mangled = writefile.mangledFileName();
763 writefile = makeAbsPath(mangled,
764 buffer().masterBuffer()->temppath());
768 LYXERR(Debug::LATEX, "incfile:" << incfile);
769 LYXERR(Debug::LATEX, "exportfile:" << exportfile);
770 LYXERR(Debug::LATEX, "writefile:" << writefile);
772 tmp->makeDocBookFile(writefile, runparams, true);
775 runparams.exportdata->addExternalFile("docbook", writefile,
777 runparams.exportdata->addExternalFile("docbook-xml", writefile,
780 if (isVerbatim(params()) || isListings(params())) {
781 os << "<inlinegraphic fileref=\""
782 << '&' << include_label << ';'
783 << "\" format=\"linespecific\">";
785 os << '&' << include_label << ';';
791 void InsetInclude::validate(LaTeXFeatures & features) const
793 string incfile = to_utf8(params()["filename"]);
796 LASSERT(&buffer() == &features.buffer(), /**/);
798 string const included_file =
799 includedFileName(buffer(), params()).absFileName();
801 if (isLyXFileName(included_file))
802 writefile = changeExtension(included_file, ".sgml");
804 writefile = included_file;
806 if (!features.runparams().nice && !isVerbatim(params()) && !isListings(params())) {
807 incfile = DocFileName(writefile).mangledFileName();
808 writefile = makeAbsPath(incfile,
809 buffer().masterBuffer()->temppath()).absFileName();
812 features.includeFile(include_label, writefile);
814 if (isVerbatim(params()))
815 features.require("verbatim");
816 else if (isListings(params()))
817 features.require("listings");
819 // Here we must do the fun stuff...
820 // Load the file in the include if it needs
822 Buffer * const tmp = loadIfNeeded();
824 // the file is loaded
825 // make sure the buffer isn't us
826 // FIXME RECURSIVE INCLUDES
827 // This is not sufficient, as recursive includes could be
828 // more than a file away. But it will do for now.
829 if (tmp && tmp != &buffer()) {
830 // We must temporarily change features.buffer,
831 // otherwise it would always be the master buffer,
832 // and nested includes would not work.
833 features.setBuffer(*tmp);
834 tmp->validate(features);
835 features.setBuffer(buffer());
841 void InsetInclude::fillWithBibKeys(BiblioInfo & keys,
842 InsetIterator const & /*di*/) const
844 Buffer * child = loadIfNeeded();
847 child->fillWithBibKeys(keys);
851 void InsetInclude::metrics(MetricsInfo & mi, Dimension & dim) const
853 LASSERT(mi.base.bv, /**/);
855 bool use_preview = false;
856 if (RenderPreview::status() != LyXRC::PREVIEW_OFF) {
857 graphics::PreviewImage const * pimage =
858 preview_->getPreviewImage(mi.base.bv->buffer());
859 use_preview = pimage && pimage->image();
863 preview_->metrics(mi, dim);
867 button_.update(screenLabel(), true);
869 button_.metrics(mi, dim);
872 Box b(0, dim.wid, -dim.asc, dim.des);
877 void InsetInclude::draw(PainterInfo & pi, int x, int y) const
879 LASSERT(pi.base.bv, /**/);
881 bool use_preview = false;
882 if (RenderPreview::status() != LyXRC::PREVIEW_OFF) {
883 graphics::PreviewImage const * pimage =
884 preview_->getPreviewImage(pi.base.bv->buffer());
885 use_preview = pimage && pimage->image();
889 preview_->draw(pi, x, y);
891 button_.draw(pi, x, y);
895 docstring InsetInclude::contextMenu(BufferView const &, int, int) const
897 return from_ascii("context-include");
901 Inset::DisplayType InsetInclude::display() const
903 return type(params()) == INPUT ? Inline : AlignCenter;
912 void InsetInclude::fileChanged() const
914 Buffer const * const buffer = updateFrontend();
918 preview_->removePreview(*buffer);
919 add_preview(*preview_.get(), *this, *buffer);
920 preview_->startLoading(*buffer);
926 bool preview_wanted(InsetCommandParams const & params, Buffer const & buffer)
928 FileName const included_file = includedFileName(buffer, params);
930 return type(params) == INPUT && params.preview() &&
931 included_file.isReadableFile();
935 docstring latexString(InsetInclude const & inset)
938 // We don't need to set runparams.encoding since this will be done
939 // by latex() anyway.
940 OutputParams runparams(0);
941 runparams.flavor = OutputParams::LATEX;
942 inset.latex(os, runparams);
948 void add_preview(RenderMonitoredPreview & renderer, InsetInclude const & inset,
949 Buffer const & buffer)
951 InsetCommandParams const & params = inset.params();
952 if (RenderPreview::status() != LyXRC::PREVIEW_OFF &&
953 preview_wanted(params, buffer)) {
954 renderer.setAbsFile(includedFileName(buffer, params));
955 docstring const snippet = latexString(inset);
956 renderer.addPreview(snippet, buffer);
963 void InsetInclude::addPreview(DocIterator const & /*inset_pos*/,
964 graphics::PreviewLoader & ploader) const
966 Buffer const & buffer = ploader.buffer();
967 if (!preview_wanted(params(), buffer))
969 preview_->setAbsFile(includedFileName(buffer, params()));
970 docstring const snippet = latexString(*this);
971 preview_->addPreview(snippet, ploader);
975 void InsetInclude::addToToc(DocIterator const & cpit)
977 TocBackend & backend = buffer().tocBackend();
979 if (isListings(params())) {
981 label_->addToToc(cpit);
983 InsetListingsParams p(to_utf8(params()["lstparams"]));
984 string caption = p.getParamValue("caption");
987 Toc & toc = backend.toc("listing");
988 docstring str = convert<docstring>(toc.size() + 1)
989 + ". " + from_utf8(caption);
990 DocIterator pit = cpit;
991 toc.push_back(TocItem(pit, 0, str));
994 Buffer const * const childbuffer = getChildBuffer();
998 Toc & toc = backend.toc("child");
999 docstring str = childbuffer->fileName().displayName();
1000 toc.push_back(TocItem(cpit, 0, str));
1002 TocList & toclist = backend.tocs();
1003 childbuffer->tocBackend().update();
1004 TocList const & childtoclist = childbuffer->tocBackend().tocs();
1005 TocList::const_iterator it = childtoclist.begin();
1006 TocList::const_iterator const end = childtoclist.end();
1007 for(; it != end; ++it)
1008 toclist[it->first].insert(toclist[it->first].end(),
1009 it->second.begin(), it->second.end());
1013 void InsetInclude::updateCommand()
1018 docstring old_label = label_->getParam("name");
1019 label_->updateCommand(old_label, false);
1020 // the label might have been adapted (duplicate)
1021 docstring new_label = label_->getParam("name");
1022 if (old_label == new_label)
1025 // update listings parameters...
1026 InsetCommandParams p(INCLUDE_CODE);
1028 InsetListingsParams par(to_utf8(params()["lstparams"]));
1029 par.addParam("label", "{" + to_utf8(new_label) + "}", true);
1030 p["lstparams"] = from_utf8(par.params());
1034 void InsetInclude::updateBuffer(ParIterator const & it, UpdateType utype)
1036 Buffer const * const childbuffer = getChildBuffer();
1038 childbuffer->updateBuffer(Buffer::UpdateChildOnly, utype);
1041 if (!isListings(params()))
1045 label_->updateBuffer(it, utype);
1047 InsetListingsParams const par(to_utf8(params()["lstparams"]));
1048 if (par.getParamValue("caption").empty()) {
1049 listings_label_ = buffer().B_("Program Listing");
1052 Buffer const & master = *buffer().masterBuffer();
1053 Counters & counters = master.params().documentClass().counters();
1054 docstring const cnt = from_ascii("listing");
1055 listings_label_ = master.B_("Program Listing");
1056 if (counters.hasCounter(cnt)) {
1057 counters.step(cnt, utype);
1058 listings_label_ += " " + convert<docstring>(counters.value(cnt));