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") {
246 InsetCommand::doDispatch(cur, cmd);
249 InsetCommand::string2params(to_utf8(cmd.argument()), p);
250 if (!p.getCmdName().empty()) {
252 InsetListingsParams new_params(to_utf8(p["lstparams"]));
253 docstring const new_label =
254 from_utf8(new_params.getParamValue("label"));
256 if (new_label.empty()) {
262 old_label = label_->getParam("name");
264 label_ = createLabel(buffer_, new_label);
265 label_->setBuffer(buffer());
268 if (new_label != old_label) {
269 label_->updateCommand(new_label);
270 // the label might have been adapted (duplicate)
271 if (new_label != label_->getParam("name")) {
272 new_params.addParam("label", "{" +
273 to_utf8(label_->getParam("name")) + "}", true);
274 p["lstparams"] = from_utf8(new_params.params());
280 cur.forceBufferUpdate();
282 cur.noScreenUpdate();
286 //pass everything else up the chain
288 InsetCommand::doDispatch(cur, cmd);
294 void InsetInclude::editIncluded(string const & file)
296 string const ext = support::getExtension(file);
298 FuncRequest fr(LFUN_BUFFER_CHILD_OPEN, file);
301 // tex file or other text file in verbatim mode
302 formats.edit(buffer(),
303 support::makeAbsPath(file, support::onlyPath(buffer().absFileName())),
308 bool InsetInclude::getStatus(Cursor & cur, FuncRequest const & cmd,
309 FuncStatus & flag) const
311 switch (cmd.action()) {
313 case LFUN_INSET_EDIT:
314 flag.setEnabled(true);
317 case LFUN_INSET_MODIFY:
318 if (cmd.getArg(0) == "changetype")
319 return InsetCommand::getStatus(cur, cmd, flag);
321 flag.setEnabled(true);
325 return InsetCommand::getStatus(cur, cmd, flag);
330 void InsetInclude::setParams(InsetCommandParams const & p)
332 // invalidate the cache
335 InsetCommand::setParams(p);
338 if (preview_->monitoring())
339 preview_->stopMonitoring();
341 if (type(params()) == INPUT)
342 add_preview(*preview_, *this, buffer());
344 buffer().invalidateBibfileCache();
348 bool InsetInclude::isChildIncluded() const
350 std::list<std::string> includeonlys =
351 buffer().params().getIncludedChildren();
352 if (includeonlys.empty())
354 return (std::find(includeonlys.begin(),
356 to_utf8(params()["filename"])) != includeonlys.end());
360 docstring InsetInclude::screenLabel() const
364 switch (type(params())) {
366 temp = buffer().B_("Input");
369 temp = buffer().B_("Verbatim Input");
372 temp = buffer().B_("Verbatim Input*");
375 if (isChildIncluded())
376 temp = buffer().B_("Include");
378 temp += buffer().B_("Include (excluded)");
381 temp = listings_label_;
384 LASSERT(false, /**/);
389 if (params()["filename"].empty())
392 temp += from_utf8(onlyFileName(to_utf8(params()["filename"])));
398 Buffer * InsetInclude::getChildBuffer() const
400 Buffer * childBuffer = loadIfNeeded();
402 // FIXME: recursive includes
403 return (childBuffer == &buffer()) ? 0 : childBuffer;
407 Buffer * InsetInclude::loadIfNeeded() const
409 // This is for background export and preview. We don't even want to
410 // try to load the cloned child document again.
411 if (buffer().isClone())
412 return child_buffer_;
414 // Don't try to load it again if we failed before.
415 if (failedtoload_ || isVerbatim(params()) || isListings(params()))
418 FileName const included_file = includedFileName(buffer(), params());
419 // Use cached Buffer if possible.
420 if (child_buffer_ != 0) {
421 if (theBufferList().isLoaded(child_buffer_)
422 // additional sanity check: make sure the Buffer really is
423 // associated with the file we want.
424 && child_buffer_ == theBufferList().getBuffer(included_file))
425 return child_buffer_;
426 // Buffer vanished, so invalidate cache and try to reload.
430 if (!isLyXFileName(included_file.absFileName()))
433 Buffer * child = theBufferList().getBuffer(included_file);
435 // the readonly flag can/will be wrong, not anymore I think.
436 if (!included_file.exists())
439 child = theBufferList().newBuffer(included_file.absFileName());
441 // Buffer creation is not possible.
444 if (child->loadLyXFile() != Buffer::ReadSuccess) {
445 failedtoload_ = true;
446 //close the buffer we just opened
447 theBufferList().release(child);
451 if (!child->errorList("Parse").empty()) {
452 // FIXME: Do something.
455 child->setParent(&buffer());
456 // Cache the child buffer.
457 child_buffer_ = child;
462 int InsetInclude::latex(odocstream & os, OutputParams const & runparams) const
464 string incfile = to_utf8(params()["filename"]);
466 // Do nothing if no file name has been specified
470 FileName const included_file = includedFileName(buffer(), params());
472 // Check we're not trying to include ourselves.
473 // FIXME RECURSIVE INCLUDE
474 // This isn't sufficient, as the inclusion could be downstream.
475 // But it'll have to do for now.
476 if (isInputOrInclude(params()) &&
477 buffer().absFileName() == included_file.absFileName())
479 Alert::error(_("Recursive input"),
480 bformat(_("Attempted to include file %1$s in itself! "
481 "Ignoring inclusion."), from_utf8(incfile)));
485 Buffer const * const masterBuffer = buffer().masterBuffer();
487 // if incfile is relative, make it relative to the master
489 if (!FileName::isAbsolute(incfile)) {
491 incfile = to_utf8(makeRelPath(from_utf8(included_file.absFileName()),
492 from_utf8(masterBuffer->filePath())));
495 // write it to a file (so far the complete file)
499 if (type(params()) == LISTINGS) {
500 exportfile = incfile;
501 mangled = DocFileName(included_file).mangledFileName();
503 exportfile = changeExtension(incfile, ".tex");
504 mangled = DocFileName(changeExtension(included_file.absFileName(), ".tex")).
508 FileName const writefile(makeAbsPath(mangled, masterBuffer->temppath()));
512 else if (!isValidLaTeXFileName(incfile)) {
513 frontend::Alert::warning(_("Invalid filename"),
514 _("The following filename will cause troubles "
515 "when running the exported file through LaTeX: ") +
518 else if (!isValidDVIFileName(incfile)) {
519 frontend::Alert::warning(_("Problematic filename for DVI"),
520 _("The following filename can cause troubles "
521 "when running the exported file through LaTeX "
522 "and opening the resulting DVI: ") +
523 from_utf8(incfile), true);
525 LYXERR(Debug::LATEX, "incfile:" << incfile);
526 LYXERR(Debug::LATEX, "exportfile:" << exportfile);
527 LYXERR(Debug::LATEX, "writefile:" << writefile);
529 if (runparams.inComment || runparams.dryrun) {
530 //Don't try to load or copy the file if we're
531 //in a comment or doing a dryrun
532 } else if (isInputOrInclude(params()) &&
533 isLyXFileName(included_file.absFileName())) {
534 // if it's a LyX file and we're inputting or including,
535 // try to load it so we can write the associated latex
537 Buffer * tmp = loadIfNeeded();
541 if (tmp->params().baseClass() != masterBuffer->params().baseClass()) {
543 docstring text = bformat(_("Included file `%1$s'\n"
544 "has textclass `%2$s'\n"
545 "while parent file has textclass `%3$s'."),
546 included_file.displayName(),
547 from_utf8(tmp->params().documentClass().name()),
548 from_utf8(masterBuffer->params().documentClass().name()));
549 Alert::warning(_("Different textclasses"), text, true);
552 // Make sure modules used in child are all included in master
553 // FIXME It might be worth loading the children's modules into the master
554 // over in BufferParams rather than doing this check.
555 LayoutModuleList const masterModules = masterBuffer->params().getModules();
556 LayoutModuleList const childModules = tmp->params().getModules();
557 LayoutModuleList::const_iterator it = childModules.begin();
558 LayoutModuleList::const_iterator end = childModules.end();
559 for (; it != end; ++it) {
560 string const module = *it;
561 LayoutModuleList::const_iterator found =
562 find(masterModules.begin(), masterModules.end(), module);
563 if (found == masterModules.end()) {
564 docstring text = bformat(_("Included file `%1$s'\n"
565 "uses module `%2$s'\n"
566 "which is not used in parent file."),
567 included_file.displayName(), from_utf8(module));
568 Alert::warning(_("Module not found"), text);
572 tmp->markDepClean(masterBuffer->temppath());
574 // FIXME: handle non existing files
575 // FIXME: Second argument is irrelevant!
576 // since only_body is true, makeLaTeXFile will not look at second
577 // argument. Should we set it to string(), or should makeLaTeXFile
578 // make use of it somehow? (JMarc 20031002)
579 // The included file might be written in a different encoding
581 Encoding const * const oldEnc = runparams.encoding;
582 Language const * const oldLang = runparams.master_language;
583 // If the master has full unicode flavor (XeTeX, LuaTeX),
584 // the children must be encoded in plain utf8!
585 runparams.encoding = runparams.isFullUnicode() ?
586 encodings.fromLyXName("utf8-plain")
587 : &tmp->params().encoding();
588 runparams.master_language = buffer().params().language;
589 tmp->makeLaTeXFile(writefile,
590 masterFileName(buffer()).onlyPath().absFileName(),
592 runparams.encoding = oldEnc;
593 runparams.master_language = oldLang;
595 // In this case, it's not a LyX file, so we copy the file
596 // to the temp dir, so that .aux files etc. are not created
597 // in the original dir. Files included by this file will be
598 // found via input@path, see ../Buffer.cpp.
599 unsigned long const checksum_in = included_file.checksum();
600 unsigned long const checksum_out = writefile.checksum();
602 if (checksum_in != checksum_out) {
603 if (!included_file.copyTo(writefile)) {
606 to_utf8(bformat(_("Could not copy the file\n%1$s\n"
607 "into the temporary directory."),
608 from_utf8(included_file.absFileName()))));
614 string const tex_format = (runparams.flavor == OutputParams::LATEX) ?
615 "latex" : "pdflatex";
616 switch (type(params())) {
619 incfile = latex_path(incfile);
621 os << '\\' << from_ascii(params().getCmdName()) << '{'
622 << from_utf8(incfile) << '}';
626 runparams.exportdata->addExternalFile(tex_format, writefile,
629 // \input wants file with extension (default is .tex)
630 if (!isLyXFileName(included_file.absFileName())) {
631 incfile = latex_path(incfile);
633 os << '\\' << from_ascii(params().getCmdName())
634 << '{' << from_utf8(incfile) << '}';
636 incfile = changeExtension(incfile, ".tex");
637 incfile = latex_path(incfile);
639 os << '\\' << from_ascii(params().getCmdName())
640 << '{' << from_utf8(incfile) << '}';
645 os << '\\' << from_ascii(params().getCmdName());
646 string const opt = to_utf8(params()["lstparams"]);
647 // opt is set in QInclude dialog and should have passed validation.
648 InsetListingsParams params(opt);
649 if (!params.params().empty())
650 os << "[" << from_utf8(params.params()) << "]";
651 os << '{' << from_utf8(incfile) << '}';
655 runparams.exportdata->addExternalFile(tex_format, writefile,
658 // \include don't want extension and demands that the
659 // file really have .tex
660 incfile = changeExtension(incfile, string());
661 incfile = latex_path(incfile);
663 os << '\\' << from_ascii(params().getCmdName()) << '{'
664 << from_utf8(incfile) << '}';
675 docstring InsetInclude::xhtml(XHTMLStream & xs, OutputParams const &rp) const
680 // For verbatim and listings, we just include the contents of the file as-is.
681 // In the case of listings, we wrap it in <pre>.
682 bool const listing = isListings(params());
683 if (listing || isVerbatim(params())) {
685 xs << html::StartTag("pre");
686 // FIXME: We don't know the encoding of the file, default to UTF-8.
687 xs << includedFileName(buffer(), params()).fileContents("UTF-8");
689 xs << html::EndTag("pre");
693 // We don't (yet) know how to Input or Include non-LyX files.
694 // (If we wanted to get really arcane, we could run some tex2html
695 // converter on the included file. But that's just masochistic.)
696 FileName const included_file = includedFileName(buffer(), params());
697 if (!isLyXFileName(included_file.absFileName())) {
698 frontend::Alert::warning(_("Unsupported Inclusion"),
699 bformat(_("LyX does not know how to include non-LyX files when "
700 "generating HTML output. Offending file:\n%1$s"),
701 params()["filename"]));
705 // In the other cases, we will generate the HTML and include it.
707 // Check we're not trying to include ourselves.
708 // FIXME RECURSIVE INCLUDE
709 if (buffer().absFileName() == included_file.absFileName()) {
710 Alert::error(_("Recursive input"),
711 bformat(_("Attempted to include file %1$s in itself! "
712 "Ignoring inclusion."), params()["filename"]));
716 Buffer const * const ibuf = loadIfNeeded();
719 ibuf->writeLyXHTMLSource(xs.os(), rp, true);
724 int InsetInclude::plaintext(odocstream & os, OutputParams const &) const
726 if (isVerbatim(params()) || isListings(params())) {
727 os << '[' << screenLabel() << '\n';
728 // FIXME: We don't know the encoding of the file, default to UTF-8.
729 os << includedFileName(buffer(), params()).fileContents("UTF-8");
731 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
733 docstring const str = '[' + screenLabel() + ']';
740 int InsetInclude::docbook(odocstream & os, OutputParams const & runparams) const
742 string incfile = to_utf8(params()["filename"]);
744 // Do nothing if no file name has been specified
748 string const included_file = includedFileName(buffer(), params()).absFileName();
750 // Check we're not trying to include ourselves.
751 // FIXME RECURSIVE INCLUDE
752 // This isn't sufficient, as the inclusion could be downstream.
753 // But it'll have to do for now.
754 if (buffer().absFileName() == included_file) {
755 Alert::error(_("Recursive input"),
756 bformat(_("Attempted to include file %1$s in itself! "
757 "Ignoring inclusion."), from_utf8(incfile)));
761 // write it to a file (so far the complete file)
762 string const exportfile = changeExtension(incfile, ".sgml");
763 DocFileName writefile(changeExtension(included_file, ".sgml"));
765 Buffer * tmp = loadIfNeeded();
767 string const mangled = writefile.mangledFileName();
768 writefile = makeAbsPath(mangled,
769 buffer().masterBuffer()->temppath());
773 LYXERR(Debug::LATEX, "incfile:" << incfile);
774 LYXERR(Debug::LATEX, "exportfile:" << exportfile);
775 LYXERR(Debug::LATEX, "writefile:" << writefile);
777 tmp->makeDocBookFile(writefile, runparams, true);
780 runparams.exportdata->addExternalFile("docbook", writefile,
782 runparams.exportdata->addExternalFile("docbook-xml", writefile,
785 if (isVerbatim(params()) || isListings(params())) {
786 os << "<inlinegraphic fileref=\""
787 << '&' << include_label << ';'
788 << "\" format=\"linespecific\">";
790 os << '&' << include_label << ';';
796 void InsetInclude::validate(LaTeXFeatures & features) const
798 string incfile = to_utf8(params()["filename"]);
801 LASSERT(&buffer() == &features.buffer(), /**/);
803 string const included_file =
804 includedFileName(buffer(), params()).absFileName();
806 if (isLyXFileName(included_file))
807 writefile = changeExtension(included_file, ".sgml");
809 writefile = included_file;
811 if (!features.runparams().nice && !isVerbatim(params()) && !isListings(params())) {
812 incfile = DocFileName(writefile).mangledFileName();
813 writefile = makeAbsPath(incfile,
814 buffer().masterBuffer()->temppath()).absFileName();
817 features.includeFile(include_label, writefile);
819 if (isVerbatim(params()))
820 features.require("verbatim");
821 else if (isListings(params()))
822 features.require("listings");
824 // Here we must do the fun stuff...
825 // Load the file in the include if it needs
827 Buffer * const tmp = loadIfNeeded();
829 // the file is loaded
830 // make sure the buffer isn't us
831 // FIXME RECURSIVE INCLUDES
832 // This is not sufficient, as recursive includes could be
833 // more than a file away. But it will do for now.
834 if (tmp && tmp != &buffer()) {
835 // We must temporarily change features.buffer,
836 // otherwise it would always be the master buffer,
837 // and nested includes would not work.
838 features.setBuffer(*tmp);
839 tmp->validate(features);
840 features.setBuffer(buffer());
846 void InsetInclude::fillWithBibKeys(BiblioInfo & keys,
847 InsetIterator const & /*di*/) const
849 Buffer * child = loadIfNeeded();
852 child->fillWithBibKeys(keys);
856 void InsetInclude::metrics(MetricsInfo & mi, Dimension & dim) const
858 LASSERT(mi.base.bv, /**/);
860 bool use_preview = false;
861 if (RenderPreview::status() != LyXRC::PREVIEW_OFF) {
862 graphics::PreviewImage const * pimage =
863 preview_->getPreviewImage(mi.base.bv->buffer());
864 use_preview = pimage && pimage->image();
868 preview_->metrics(mi, dim);
872 button_.update(screenLabel(), true);
874 button_.metrics(mi, dim);
877 Box b(0, dim.wid, -dim.asc, dim.des);
882 void InsetInclude::draw(PainterInfo & pi, int x, int y) const
884 LASSERT(pi.base.bv, /**/);
886 bool use_preview = false;
887 if (RenderPreview::status() != LyXRC::PREVIEW_OFF) {
888 graphics::PreviewImage const * pimage =
889 preview_->getPreviewImage(pi.base.bv->buffer());
890 use_preview = pimage && pimage->image();
894 preview_->draw(pi, x, y);
896 button_.draw(pi, x, y);
900 docstring InsetInclude::contextMenu(BufferView const &, int, int) const
902 return from_ascii("context-include");
906 Inset::DisplayType InsetInclude::display() const
908 return type(params()) == INPUT ? Inline : AlignCenter;
917 void InsetInclude::fileChanged() const
919 Buffer const * const buffer = updateFrontend();
923 preview_->removePreview(*buffer);
924 add_preview(*preview_.get(), *this, *buffer);
925 preview_->startLoading(*buffer);
931 bool preview_wanted(InsetCommandParams const & params, Buffer const & buffer)
933 FileName const included_file = includedFileName(buffer, params);
935 return type(params) == INPUT && params.preview() &&
936 included_file.isReadableFile();
940 docstring latexString(InsetInclude const & inset)
943 // We don't need to set runparams.encoding since this will be done
944 // by latex() anyway.
945 OutputParams runparams(0);
946 runparams.flavor = OutputParams::LATEX;
947 inset.latex(os, runparams);
953 void add_preview(RenderMonitoredPreview & renderer, InsetInclude const & inset,
954 Buffer const & buffer)
956 InsetCommandParams const & params = inset.params();
957 if (RenderPreview::status() != LyXRC::PREVIEW_OFF &&
958 preview_wanted(params, buffer)) {
959 renderer.setAbsFile(includedFileName(buffer, params));
960 docstring const snippet = latexString(inset);
961 renderer.addPreview(snippet, buffer);
968 void InsetInclude::addPreview(DocIterator const & /*inset_pos*/,
969 graphics::PreviewLoader & ploader) const
971 Buffer const & buffer = ploader.buffer();
972 if (!preview_wanted(params(), buffer))
974 preview_->setAbsFile(includedFileName(buffer, params()));
975 docstring const snippet = latexString(*this);
976 preview_->addPreview(snippet, ploader);
980 void InsetInclude::addToToc(DocIterator const & cpit)
982 TocBackend & backend = buffer().tocBackend();
984 if (isListings(params())) {
986 label_->addToToc(cpit);
988 InsetListingsParams p(to_utf8(params()["lstparams"]));
989 string caption = p.getParamValue("caption");
992 Toc & toc = backend.toc("listing");
993 docstring str = convert<docstring>(toc.size() + 1)
994 + ". " + from_utf8(caption);
995 DocIterator pit = cpit;
996 toc.push_back(TocItem(pit, 0, str));
999 Buffer const * const childbuffer = getChildBuffer();
1003 Toc & toc = backend.toc("child");
1004 docstring str = childbuffer->fileName().displayName();
1005 toc.push_back(TocItem(cpit, 0, str));
1007 TocList & toclist = backend.tocs();
1008 childbuffer->tocBackend().update();
1009 TocList const & childtoclist = childbuffer->tocBackend().tocs();
1010 TocList::const_iterator it = childtoclist.begin();
1011 TocList::const_iterator const end = childtoclist.end();
1012 for(; it != end; ++it)
1013 toclist[it->first].insert(toclist[it->first].end(),
1014 it->second.begin(), it->second.end());
1018 void InsetInclude::updateCommand()
1023 docstring old_label = label_->getParam("name");
1024 label_->updateCommand(old_label, false);
1025 // the label might have been adapted (duplicate)
1026 docstring new_label = label_->getParam("name");
1027 if (old_label == new_label)
1030 // update listings parameters...
1031 InsetCommandParams p(INCLUDE_CODE);
1033 InsetListingsParams par(to_utf8(params()["lstparams"]));
1034 par.addParam("label", "{" + to_utf8(new_label) + "}", true);
1035 p["lstparams"] = from_utf8(par.params());
1039 void InsetInclude::updateBuffer(ParIterator const & it, UpdateType utype)
1041 Buffer const * const childbuffer = getChildBuffer();
1043 childbuffer->updateBuffer(Buffer::UpdateChildOnly, utype);
1046 if (!isListings(params()))
1050 label_->updateBuffer(it, utype);
1052 InsetListingsParams const par(to_utf8(params()["lstparams"]));
1053 if (par.getParamValue("caption").empty()) {
1054 listings_label_ = buffer().B_("Program Listing");
1057 Buffer const & master = *buffer().masterBuffer();
1058 Counters & counters = master.params().documentClass().counters();
1059 docstring const cnt = from_ascii("listing");
1060 listings_label_ = master.B_("Program Listing");
1061 if (counters.hasCounter(cnt)) {
1062 counters.step(cnt, utype);
1063 listings_label_ += " " + convert<docstring>(counters.value(cnt));