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"
24 #include "ErrorList.h"
27 #include "FuncRequest.h"
28 #include "FuncStatus.h"
29 #include "LaTeXFeatures.h"
30 #include "LayoutFile.h"
31 #include "LayoutModuleList.h"
35 #include "MetricsInfo.h"
36 #include "output_xhtml.h"
37 #include "OutputParams.h"
38 #include "TextClass.h"
39 #include "TocBackend.h"
41 #include "frontends/alert.h"
42 #include "frontends/Painter.h"
44 #include "graphics/PreviewImage.h"
45 #include "graphics/PreviewLoader.h"
47 #include "insets/InsetLabel.h"
48 #include "insets/InsetListingsParams.h"
49 #include "insets/RenderPreview.h"
51 #include "support/convert.h"
52 #include "support/debug.h"
53 #include "support/docstream.h"
54 #include "support/FileNameList.h"
55 #include "support/filetools.h"
56 #include "support/gettext.h"
57 #include "support/lassert.h"
58 #include "support/lstrings.h" // contains
59 #include "support/lyxalgo.h"
61 #include "support/bind.h"
64 using namespace lyx::support;
68 namespace Alert = frontend::Alert;
73 docstring const uniqueID()
75 static unsigned int seed = 1000;
76 return "file" + convert<docstring>(++seed);
80 /// the type of inclusion
82 INCLUDE, VERB, INPUT, VERBAST, LISTINGS, NONE
86 Types type(string const & s)
90 if (s == "verbatiminput")
92 if (s == "verbatiminput*")
94 if (s == "lstinputlisting")
102 Types type(InsetCommandParams const & params)
104 return type(params.getCmdName());
108 bool isListings(InsetCommandParams const & params)
110 return type(params) == LISTINGS;
114 bool isVerbatim(InsetCommandParams const & params)
116 Types const t = type(params);
117 return t == VERB || t == VERBAST;
121 bool isInputOrInclude(InsetCommandParams const & params)
123 Types const t = type(params);
124 return t == INPUT || t == INCLUDE;
128 FileName const masterFileName(Buffer const & buffer)
130 return buffer.masterBuffer()->fileName();
134 void add_preview(RenderMonitoredPreview &, InsetInclude const &, Buffer const &);
137 string const parentFileName(Buffer const & buffer)
139 return buffer.absFileName();
143 FileName const includedFileName(Buffer const & buffer,
144 InsetCommandParams const & params)
146 return makeAbsPath(to_utf8(params["filename"]),
147 onlyPath(parentFileName(buffer)));
151 InsetLabel * createLabel(Buffer * buf, docstring const & label_str)
153 if (label_str.empty())
155 InsetCommandParams icp(LABEL_CODE);
156 icp["name"] = label_str;
157 return new InsetLabel(buf, icp);
163 InsetInclude::InsetInclude(Buffer * buf, InsetCommandParams const & p)
164 : InsetCommand(buf, p), include_label(uniqueID()),
165 preview_(new RenderMonitoredPreview(this)), failedtoload_(false),
166 set_label_(false), label_(0), child_buffer_(0)
168 preview_->fileChanged(bind(&InsetInclude::fileChanged, this));
170 if (isListings(params())) {
171 InsetListingsParams listing_params(to_utf8(p["lstparams"]));
172 label_ = createLabel(buffer_, 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)), failedtoload_(false),
180 set_label_(false), label_(0), child_buffer_(0)
182 preview_->fileChanged(bind(&InsetInclude::fileChanged, this));
185 label_ = new InsetLabel(*other.label_);
189 InsetInclude::~InsetInclude()
191 if (isBufferLoaded())
192 buffer().invalidateBibfileCache();
197 void InsetInclude::setBuffer(Buffer & buffer)
199 InsetCommand::setBuffer(buffer);
201 label_->setBuffer(buffer);
205 void InsetInclude::setChildBuffer(Buffer * buffer)
207 child_buffer_ = buffer;
211 ParamInfo const & InsetInclude::findInfo(string const & /* cmdName */)
214 // This is only correct for the case of listings, but it'll do for now.
215 // In the other cases, this second parameter should just be empty.
216 static ParamInfo param_info_;
217 if (param_info_.empty()) {
218 param_info_.add("filename", ParamInfo::LATEX_REQUIRED);
219 param_info_.add("lstparams", ParamInfo::LATEX_OPTIONAL);
225 bool InsetInclude::isCompatibleCommand(string const & s)
227 return type(s) != NONE;
231 void InsetInclude::doDispatch(Cursor & cur, FuncRequest & cmd)
233 switch (cmd.action()) {
235 case LFUN_INSET_EDIT: {
236 editIncluded(to_utf8(params()["filename"]));
240 case LFUN_INSET_MODIFY: {
241 // It should be OK just to invalidate the cache in setParams()
243 // child_buffer_ = 0;
244 InsetCommandParams p(INCLUDE_CODE);
245 if (cmd.getArg(0) == "changetype") {
247 InsetCommand::doDispatch(cur, cmd);
250 InsetCommand::string2params(to_utf8(cmd.argument()), p);
251 if (!p.getCmdName().empty()) {
253 InsetListingsParams new_params(to_utf8(p["lstparams"]));
254 docstring const new_label =
255 from_utf8(new_params.getParamValue("label"));
257 if (new_label.empty()) {
263 old_label = label_->getParam("name");
265 label_ = createLabel(buffer_, new_label);
266 label_->setBuffer(buffer());
269 if (new_label != old_label) {
270 label_->updateCommand(new_label);
271 // the label might have been adapted (duplicate)
272 if (new_label != label_->getParam("name")) {
273 new_params.addParam("label", "{" +
274 to_utf8(label_->getParam("name")) + "}", true);
275 p["lstparams"] = from_utf8(new_params.params());
282 cur.forceBufferUpdate();
284 cur.noScreenUpdate();
288 //pass everything else up the chain
290 InsetCommand::doDispatch(cur, cmd);
296 void InsetInclude::editIncluded(string const & file)
298 string const ext = support::getExtension(file);
300 FuncRequest fr(LFUN_BUFFER_CHILD_OPEN, file);
303 // tex file or other text file in verbatim mode
304 formats.edit(buffer(),
305 support::makeAbsPath(file, support::onlyPath(buffer().absFileName())),
310 bool InsetInclude::getStatus(Cursor & cur, FuncRequest const & cmd,
311 FuncStatus & flag) const
313 switch (cmd.action()) {
315 case LFUN_INSET_EDIT:
316 flag.setEnabled(true);
319 case LFUN_INSET_MODIFY:
320 if (cmd.getArg(0) == "changetype")
321 return InsetCommand::getStatus(cur, cmd, flag);
323 flag.setEnabled(true);
327 return InsetCommand::getStatus(cur, cmd, flag);
332 void InsetInclude::setParams(InsetCommandParams const & p)
334 // invalidate the cache
337 InsetCommand::setParams(p);
340 if (preview_->monitoring())
341 preview_->stopMonitoring();
343 if (type(params()) == INPUT)
344 add_preview(*preview_, *this, buffer());
346 buffer().invalidateBibfileCache();
350 bool InsetInclude::isChildIncluded() const
352 std::list<std::string> includeonlys =
353 buffer().params().getIncludedChildren();
354 if (includeonlys.empty())
356 return (std::find(includeonlys.begin(),
358 to_utf8(params()["filename"])) != includeonlys.end());
362 docstring InsetInclude::screenLabel() const
366 switch (type(params())) {
368 temp = buffer().B_("Input");
371 temp = buffer().B_("Verbatim Input");
374 temp = buffer().B_("Verbatim Input*");
377 if (isChildIncluded())
378 temp = buffer().B_("Include");
380 temp += buffer().B_("Include (excluded)");
383 temp = listings_label_;
386 LASSERT(false, /**/);
391 if (params()["filename"].empty())
394 temp += from_utf8(onlyFileName(to_utf8(params()["filename"])));
400 Buffer * InsetInclude::getChildBuffer() const
402 Buffer * childBuffer = loadIfNeeded();
404 // FIXME: recursive includes
405 return (childBuffer == &buffer()) ? 0 : childBuffer;
409 Buffer * InsetInclude::loadIfNeeded() const
411 // This is for background export and preview. We don't even want to
412 // try to load the cloned child document again.
413 if (buffer().isClone())
414 return child_buffer_;
416 // Don't try to load it again if we failed before.
417 if (failedtoload_ || isVerbatim(params()) || isListings(params()))
420 FileName const included_file = includedFileName(buffer(), params());
421 // Use cached Buffer if possible.
422 if (child_buffer_ != 0) {
423 if (theBufferList().isLoaded(child_buffer_)
424 // additional sanity check: make sure the Buffer really is
425 // associated with the file we want.
426 && child_buffer_ == theBufferList().getBuffer(included_file))
427 return child_buffer_;
428 // Buffer vanished, so invalidate cache and try to reload.
432 if (!isLyXFileName(included_file.absFileName()))
435 Buffer * child = theBufferList().getBuffer(included_file);
437 // the readonly flag can/will be wrong, not anymore I think.
438 if (!included_file.exists())
441 child = theBufferList().newBuffer(included_file.absFileName());
443 // Buffer creation is not possible.
446 if (child->loadLyXFile() != Buffer::ReadSuccess) {
447 failedtoload_ = true;
448 //close the buffer we just opened
449 theBufferList().release(child);
453 if (!child->errorList("Parse").empty()) {
454 // FIXME: Do something.
457 child->setParent(&buffer());
458 // Cache the child buffer.
459 child_buffer_ = child;
464 int InsetInclude::latex(odocstream & os, OutputParams const & runparams) const
466 string incfile = to_utf8(params()["filename"]);
468 // Do nothing if no file name has been specified
472 FileName const included_file = includedFileName(buffer(), params());
474 // Check we're not trying to include ourselves.
475 // FIXME RECURSIVE INCLUDE
476 // This isn't sufficient, as the inclusion could be downstream.
477 // But it'll have to do for now.
478 if (isInputOrInclude(params()) &&
479 buffer().absFileName() == included_file.absFileName())
481 Alert::error(_("Recursive input"),
482 bformat(_("Attempted to include file %1$s in itself! "
483 "Ignoring inclusion."), from_utf8(incfile)));
487 Buffer const * const masterBuffer = buffer().masterBuffer();
489 // if incfile is relative, make it relative to the master
491 if (!FileName::isAbsolute(incfile)) {
493 incfile = to_utf8(makeRelPath(from_utf8(included_file.absFileName()),
494 from_utf8(masterBuffer->filePath())));
497 // write it to a file (so far the complete file)
501 if (type(params()) == LISTINGS) {
502 exportfile = incfile;
503 mangled = DocFileName(included_file).mangledFileName();
505 exportfile = changeExtension(incfile, ".tex");
506 mangled = DocFileName(changeExtension(included_file.absFileName(), ".tex")).
510 FileName const writefile(makeAbsPath(mangled, masterBuffer->temppath()));
514 else if (!isValidLaTeXFileName(incfile)) {
515 frontend::Alert::warning(_("Invalid filename"),
516 _("The following filename will cause troubles "
517 "when running the exported file through LaTeX: ") +
520 else if (!isValidDVIFileName(incfile)) {
521 frontend::Alert::warning(_("Problematic filename for DVI"),
522 _("The following filename can cause troubles "
523 "when running the exported file through LaTeX "
524 "and opening the resulting DVI: ") +
525 from_utf8(incfile), true);
527 LYXERR(Debug::LATEX, "incfile:" << incfile);
528 LYXERR(Debug::LATEX, "exportfile:" << exportfile);
529 LYXERR(Debug::LATEX, "writefile:" << writefile);
531 if (runparams.inComment || runparams.dryrun) {
532 //Don't try to load or copy the file if we're
533 //in a comment or doing a dryrun
534 } else if (isInputOrInclude(params()) &&
535 isLyXFileName(included_file.absFileName())) {
536 // if it's a LyX file and we're inputting or including,
537 // try to load it so we can write the associated latex
539 Buffer * tmp = loadIfNeeded();
543 if (tmp->params().baseClass() != masterBuffer->params().baseClass()) {
545 docstring text = bformat(_("Included file `%1$s'\n"
546 "has textclass `%2$s'\n"
547 "while parent file has textclass `%3$s'."),
548 included_file.displayName(),
549 from_utf8(tmp->params().documentClass().name()),
550 from_utf8(masterBuffer->params().documentClass().name()));
551 Alert::warning(_("Different textclasses"), text, true);
554 // Make sure modules used in child are all included in master
555 // FIXME It might be worth loading the children's modules into the master
556 // over in BufferParams rather than doing this check.
557 LayoutModuleList const masterModules = masterBuffer->params().getModules();
558 LayoutModuleList const childModules = tmp->params().getModules();
559 LayoutModuleList::const_iterator it = childModules.begin();
560 LayoutModuleList::const_iterator end = childModules.end();
561 for (; it != end; ++it) {
562 string const module = *it;
563 LayoutModuleList::const_iterator found =
564 find(masterModules.begin(), masterModules.end(), module);
565 if (found == masterModules.end()) {
566 docstring text = bformat(_("Included file `%1$s'\n"
567 "uses module `%2$s'\n"
568 "which is not used in parent file."),
569 included_file.displayName(), from_utf8(module));
570 Alert::warning(_("Module not found"), text);
574 tmp->markDepClean(masterBuffer->temppath());
576 // FIXME: handle non existing files
577 // FIXME: Second argument is irrelevant!
578 // since only_body is true, makeLaTeXFile will not look at second
579 // argument. Should we set it to string(), or should makeLaTeXFile
580 // make use of it somehow? (JMarc 20031002)
581 // The included file might be written in a different encoding
583 Encoding const * const oldEnc = runparams.encoding;
584 Language const * const oldLang = runparams.master_language;
585 // If the master has full unicode flavor (XeTeX, LuaTeX),
586 // the children must be encoded in plain utf8!
587 runparams.encoding = runparams.isFullUnicode() ?
588 encodings.fromLyXName("utf8-plain")
589 : &tmp->params().encoding();
590 runparams.master_language = buffer().params().language;
591 tmp->makeLaTeXFile(writefile,
592 masterFileName(buffer()).onlyPath().absFileName(),
594 runparams.encoding = oldEnc;
595 runparams.master_language = oldLang;
597 // In this case, it's not a LyX file, so we copy the file
598 // to the temp dir, so that .aux files etc. are not created
599 // in the original dir. Files included by this file will be
600 // found via input@path, see ../Buffer.cpp.
601 unsigned long const checksum_in = included_file.checksum();
602 unsigned long const checksum_out = writefile.checksum();
604 if (checksum_in != checksum_out) {
605 if (!included_file.copyTo(writefile)) {
608 to_utf8(bformat(_("Could not copy the file\n%1$s\n"
609 "into the temporary directory."),
610 from_utf8(included_file.absFileName()))));
616 string const tex_format = (runparams.flavor == OutputParams::LATEX) ?
617 "latex" : "pdflatex";
618 switch (type(params())) {
621 incfile = latex_path(incfile);
623 os << '\\' << from_ascii(params().getCmdName()) << '{'
624 << from_utf8(incfile) << '}';
628 runparams.exportdata->addExternalFile(tex_format, writefile,
631 // \input wants file with extension (default is .tex)
632 if (!isLyXFileName(included_file.absFileName())) {
633 incfile = latex_path(incfile);
635 os << '\\' << from_ascii(params().getCmdName())
636 << '{' << from_utf8(incfile) << '}';
638 incfile = changeExtension(incfile, ".tex");
639 incfile = latex_path(incfile);
641 os << '\\' << from_ascii(params().getCmdName())
642 << '{' << from_utf8(incfile) << '}';
647 os << '\\' << from_ascii(params().getCmdName());
648 string const opt = to_utf8(params()["lstparams"]);
649 // opt is set in QInclude dialog and should have passed validation.
650 InsetListingsParams params(opt);
651 if (!params.params().empty())
652 os << "[" << from_utf8(params.params()) << "]";
653 os << '{' << from_utf8(incfile) << '}';
657 runparams.exportdata->addExternalFile(tex_format, writefile,
660 // \include don't want extension and demands that the
661 // file really have .tex
662 incfile = changeExtension(incfile, string());
663 incfile = latex_path(incfile);
665 os << '\\' << from_ascii(params().getCmdName()) << '{'
666 << from_utf8(incfile) << '}';
677 docstring InsetInclude::xhtml(XHTMLStream & xs, OutputParams const &rp) const
682 // For verbatim and listings, we just include the contents of the file as-is.
683 // In the case of listings, we wrap it in <pre>.
684 bool const listing = isListings(params());
685 if (listing || isVerbatim(params())) {
687 xs << html::StartTag("pre");
688 // FIXME: We don't know the encoding of the file, default to UTF-8.
689 xs << includedFileName(buffer(), params()).fileContents("UTF-8");
691 xs << html::EndTag("pre");
695 // We don't (yet) know how to Input or Include non-LyX files.
696 // (If we wanted to get really arcane, we could run some tex2html
697 // converter on the included file. But that's just masochistic.)
698 FileName const included_file = includedFileName(buffer(), params());
699 if (!isLyXFileName(included_file.absFileName())) {
700 frontend::Alert::warning(_("Unsupported Inclusion"),
701 bformat(_("LyX does not know how to include non-LyX files when "
702 "generating HTML output. Offending file:\n%1$s"),
703 params()["filename"]));
707 // In the other cases, we will generate the HTML and include it.
709 // Check we're not trying to include ourselves.
710 // FIXME RECURSIVE INCLUDE
711 if (buffer().absFileName() == included_file.absFileName()) {
712 Alert::error(_("Recursive input"),
713 bformat(_("Attempted to include file %1$s in itself! "
714 "Ignoring inclusion."), params()["filename"]));
718 Buffer const * const ibuf = loadIfNeeded();
721 ibuf->writeLyXHTMLSource(xs.os(), rp, true);
726 int InsetInclude::plaintext(odocstream & os, OutputParams const &) const
728 if (isVerbatim(params()) || isListings(params())) {
729 os << '[' << screenLabel() << '\n';
730 // FIXME: We don't know the encoding of the file, default to UTF-8.
731 os << includedFileName(buffer(), params()).fileContents("UTF-8");
733 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
735 docstring const str = '[' + screenLabel() + ']';
742 int InsetInclude::docbook(odocstream & os, OutputParams const & runparams) const
744 string incfile = to_utf8(params()["filename"]);
746 // Do nothing if no file name has been specified
750 string const included_file = includedFileName(buffer(), params()).absFileName();
752 // Check we're not trying to include ourselves.
753 // FIXME RECURSIVE INCLUDE
754 // This isn't sufficient, as the inclusion could be downstream.
755 // But it'll have to do for now.
756 if (buffer().absFileName() == included_file) {
757 Alert::error(_("Recursive input"),
758 bformat(_("Attempted to include file %1$s in itself! "
759 "Ignoring inclusion."), from_utf8(incfile)));
763 // write it to a file (so far the complete file)
764 string const exportfile = changeExtension(incfile, ".sgml");
765 DocFileName writefile(changeExtension(included_file, ".sgml"));
767 Buffer * tmp = loadIfNeeded();
769 string const mangled = writefile.mangledFileName();
770 writefile = makeAbsPath(mangled,
771 buffer().masterBuffer()->temppath());
775 LYXERR(Debug::LATEX, "incfile:" << incfile);
776 LYXERR(Debug::LATEX, "exportfile:" << exportfile);
777 LYXERR(Debug::LATEX, "writefile:" << writefile);
779 tmp->makeDocBookFile(writefile, runparams, true);
782 runparams.exportdata->addExternalFile("docbook", writefile,
784 runparams.exportdata->addExternalFile("docbook-xml", writefile,
787 if (isVerbatim(params()) || isListings(params())) {
788 os << "<inlinegraphic fileref=\""
789 << '&' << include_label << ';'
790 << "\" format=\"linespecific\">";
792 os << '&' << include_label << ';';
798 void InsetInclude::validate(LaTeXFeatures & features) const
800 string incfile = to_utf8(params()["filename"]);
803 LASSERT(&buffer() == &features.buffer(), /**/);
805 string const included_file =
806 includedFileName(buffer(), params()).absFileName();
808 if (isLyXFileName(included_file))
809 writefile = changeExtension(included_file, ".sgml");
811 writefile = included_file;
813 if (!features.runparams().nice && !isVerbatim(params()) && !isListings(params())) {
814 incfile = DocFileName(writefile).mangledFileName();
815 writefile = makeAbsPath(incfile,
816 buffer().masterBuffer()->temppath()).absFileName();
819 features.includeFile(include_label, writefile);
821 if (isVerbatim(params()))
822 features.require("verbatim");
823 else if (isListings(params()))
824 features.require("listings");
826 // Here we must do the fun stuff...
827 // Load the file in the include if it needs
829 Buffer * const tmp = loadIfNeeded();
831 // the file is loaded
832 // make sure the buffer isn't us
833 // FIXME RECURSIVE INCLUDES
834 // This is not sufficient, as recursive includes could be
835 // more than a file away. But it will do for now.
836 if (tmp && tmp != &buffer()) {
837 // We must temporarily change features.buffer,
838 // otherwise it would always be the master buffer,
839 // and nested includes would not work.
840 features.setBuffer(*tmp);
841 tmp->validate(features);
842 features.setBuffer(buffer());
848 void InsetInclude::collectBibKeys(InsetIterator const & /*di*/) const
850 Buffer * child = loadIfNeeded();
853 child->collectBibKeys();
857 void InsetInclude::metrics(MetricsInfo & mi, Dimension & dim) const
859 LASSERT(mi.base.bv, /**/);
861 bool use_preview = false;
862 if (RenderPreview::status() != LyXRC::PREVIEW_OFF) {
863 graphics::PreviewImage const * pimage =
864 preview_->getPreviewImage(mi.base.bv->buffer());
865 use_preview = pimage && pimage->image();
869 preview_->metrics(mi, dim);
873 button_.update(screenLabel(), true);
875 button_.metrics(mi, dim);
878 Box b(0, dim.wid, -dim.asc, dim.des);
883 void InsetInclude::draw(PainterInfo & pi, int x, int y) const
885 LASSERT(pi.base.bv, /**/);
887 bool use_preview = false;
888 if (RenderPreview::status() != LyXRC::PREVIEW_OFF) {
889 graphics::PreviewImage const * pimage =
890 preview_->getPreviewImage(pi.base.bv->buffer());
891 use_preview = pimage && pimage->image();
895 preview_->draw(pi, x, y);
897 button_.draw(pi, x, y);
901 docstring InsetInclude::contextMenuName() const
903 return from_ascii("context-include");
907 Inset::DisplayType InsetInclude::display() const
909 return type(params()) == INPUT ? Inline : AlignCenter;
918 void InsetInclude::fileChanged() const
920 Buffer const * const buffer = updateFrontend();
924 preview_->removePreview(*buffer);
925 add_preview(*preview_.get(), *this, *buffer);
926 preview_->startLoading(*buffer);
932 bool preview_wanted(InsetCommandParams const & params, Buffer const & buffer)
934 FileName const included_file = includedFileName(buffer, params);
936 return type(params) == INPUT && params.preview() &&
937 included_file.isReadableFile();
941 docstring latexString(InsetInclude const & inset)
944 // We don't need to set runparams.encoding since this will be done
945 // by latex() anyway.
946 OutputParams runparams(0);
947 runparams.flavor = OutputParams::LATEX;
948 inset.latex(os, runparams);
954 void add_preview(RenderMonitoredPreview & renderer, InsetInclude const & inset,
955 Buffer const & buffer)
957 InsetCommandParams const & params = inset.params();
958 if (RenderPreview::status() != LyXRC::PREVIEW_OFF &&
959 preview_wanted(params, buffer)) {
960 renderer.setAbsFile(includedFileName(buffer, params));
961 docstring const snippet = latexString(inset);
962 renderer.addPreview(snippet, buffer);
969 void InsetInclude::addPreview(DocIterator const & /*inset_pos*/,
970 graphics::PreviewLoader & ploader) const
972 Buffer const & buffer = ploader.buffer();
973 if (!preview_wanted(params(), buffer))
975 preview_->setAbsFile(includedFileName(buffer, params()));
976 docstring const snippet = latexString(*this);
977 preview_->addPreview(snippet, ploader);
981 void InsetInclude::addToToc(DocIterator const & cpit)
983 TocBackend & backend = buffer().tocBackend();
985 if (isListings(params())) {
987 label_->addToToc(cpit);
989 InsetListingsParams p(to_utf8(params()["lstparams"]));
990 string caption = p.getParamValue("caption");
993 Toc & toc = backend.toc("listing");
994 docstring str = convert<docstring>(toc.size() + 1)
995 + ". " + from_utf8(caption);
996 DocIterator pit = cpit;
997 toc.push_back(TocItem(pit, 0, str));
1000 Buffer const * const childbuffer = getChildBuffer();
1004 Toc & toc = backend.toc("child");
1005 docstring str = childbuffer->fileName().displayName();
1006 toc.push_back(TocItem(cpit, 0, str));
1008 TocList & toclist = backend.tocs();
1009 childbuffer->tocBackend().update();
1010 TocList const & childtoclist = childbuffer->tocBackend().tocs();
1011 TocList::const_iterator it = childtoclist.begin();
1012 TocList::const_iterator const end = childtoclist.end();
1013 for(; it != end; ++it)
1014 toclist[it->first].insert(toclist[it->first].end(),
1015 it->second.begin(), it->second.end());
1019 void InsetInclude::updateCommand()
1024 docstring old_label = label_->getParam("name");
1025 label_->updateCommand(old_label, false);
1026 // the label might have been adapted (duplicate)
1027 docstring new_label = label_->getParam("name");
1028 if (old_label == new_label)
1031 // update listings parameters...
1032 InsetCommandParams p(INCLUDE_CODE);
1034 InsetListingsParams par(to_utf8(params()["lstparams"]));
1035 par.addParam("label", "{" + to_utf8(new_label) + "}", true);
1036 p["lstparams"] = from_utf8(par.params());
1040 void InsetInclude::updateBuffer(ParIterator const & it, UpdateType utype)
1042 Buffer const * const childbuffer = getChildBuffer();
1044 childbuffer->updateBuffer(Buffer::UpdateChildOnly, utype);
1047 if (!isListings(params()))
1051 label_->updateBuffer(it, utype);
1053 InsetListingsParams const par(to_utf8(params()["lstparams"]));
1054 if (par.getParamValue("caption").empty()) {
1055 listings_label_ = buffer().B_("Program Listing");
1058 Buffer const & master = *buffer().masterBuffer();
1059 Counters & counters = master.params().documentClass().counters();
1060 docstring const cnt = from_ascii("listing");
1061 listings_label_ = master.B_("Program Listing");
1062 if (counters.hasCounter(cnt)) {
1063 counters.step(cnt, utype);
1064 listings_label_ += " " + convert<docstring>(counters.value(cnt));