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"
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 <boost/bind.hpp>
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"), include_label(uniqueID()),
165 preview_(new RenderMonitoredPreview(this)), failedtoload_(false),
166 set_label_(false), label_(0), child_buffer_(0)
168 preview_->fileChanged(boost::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(boost::bind(&InsetInclude::fileChanged, this));
185 label_ = new InsetLabel(*other.label_);
189 InsetInclude::~InsetInclude()
195 void InsetInclude::setBuffer(Buffer & buffer)
197 InsetCommand::setBuffer(buffer);
199 label_->setBuffer(buffer);
203 void InsetInclude::setChildBuffer(Buffer * buffer)
205 child_buffer_ = buffer;
209 ParamInfo const & InsetInclude::findInfo(string const & /* cmdName */)
212 // This is only correct for the case of listings, but it'll do for now.
213 // In the other cases, this second parameter should just be empty.
214 static ParamInfo param_info_;
215 if (param_info_.empty()) {
216 param_info_.add("filename", ParamInfo::LATEX_REQUIRED);
217 param_info_.add("lstparams", ParamInfo::LATEX_OPTIONAL);
223 bool InsetInclude::isCompatibleCommand(string const & s)
225 return type(s) != NONE;
229 void InsetInclude::doDispatch(Cursor & cur, FuncRequest & cmd)
231 switch (cmd.action) {
233 case LFUN_INSET_EDIT: {
234 editIncluded(to_utf8(params()["filename"]));
238 case LFUN_INSET_MODIFY: {
239 // It should be OK just to invalidate the cache is setParams()
241 // child_buffer_ = 0;
242 InsetCommandParams p(INCLUDE_CODE);
243 if (cmd.getArg(0) == "changetype") {
244 InsetCommand::doDispatch(cur, cmd);
247 InsetCommand::string2params("include", to_utf8(cmd.argument()), p);
248 if (!p.getCmdName().empty()) {
250 InsetListingsParams new_params(to_utf8(p["lstparams"]));
251 docstring const new_label =
252 from_utf8(new_params.getParamValue("label"));
254 if (new_label.empty()) {
260 old_label = label_->getParam("name");
262 label_ = createLabel(buffer_, new_label);
263 label_->setBuffer(buffer());
266 if (new_label != old_label) {
267 label_->updateCommand(new_label);
268 // the label might have been adapted (duplicate)
269 if (new_label != label_->getParam("name")) {
270 new_params.addParam("label", "{" +
271 to_utf8(label_->getParam("name")) + "}", true);
272 p["lstparams"] = from_utf8(new_params.params());
283 //pass everything else up the chain
285 InsetCommand::doDispatch(cur, cmd);
291 void InsetInclude::editIncluded(string const & file)
293 string const ext = support::getExtension(file);
295 FuncRequest fr(LFUN_BUFFER_CHILD_OPEN, file);
298 // tex file or other text file in verbatim mode
299 formats.edit(buffer(),
300 support::makeAbsPath(file, support::onlyPath(buffer().absFileName())),
305 bool InsetInclude::getStatus(Cursor & cur, FuncRequest const & cmd,
306 FuncStatus & flag) const
308 switch (cmd.action) {
310 case LFUN_INSET_EDIT:
311 flag.setEnabled(true);
314 case LFUN_INSET_MODIFY:
315 if (cmd.getArg(0) == "changetype")
316 return InsetCommand::getStatus(cur, cmd, flag);
318 flag.setEnabled(true);
322 return InsetCommand::getStatus(cur, cmd, flag);
327 void InsetInclude::setParams(InsetCommandParams const & p)
329 // invalidate the cache
332 InsetCommand::setParams(p);
335 if (preview_->monitoring())
336 preview_->stopMonitoring();
338 if (type(params()) == INPUT)
339 add_preview(*preview_, *this, buffer());
341 buffer().updateBibfilesCache();
345 bool InsetInclude::isChildIncluded() const
347 std::list<std::string> includeonlys =
348 buffer().params().getIncludedChildren();
349 if (includeonlys.empty())
351 return (std::find(includeonlys.begin(),
353 to_utf8(params()["filename"])) != includeonlys.end());
357 docstring InsetInclude::screenLabel() const
361 switch (type(params())) {
363 temp = buffer().B_("Input");
366 temp = buffer().B_("Verbatim Input");
369 temp = buffer().B_("Verbatim Input*");
372 if (isChildIncluded())
373 temp = buffer().B_("Include");
375 temp += buffer().B_("Include (excluded)");
378 temp = listings_label_;
381 LASSERT(false, /**/);
386 if (params()["filename"].empty())
389 temp += from_utf8(onlyFilename(to_utf8(params()["filename"])));
395 Buffer * InsetInclude::getChildBuffer() const
397 Buffer * childBuffer = loadIfNeeded();
399 // FIXME: recursive includes
400 return (childBuffer == &buffer()) ? 0 : childBuffer;
404 Buffer * InsetInclude::loadIfNeeded() const
406 // This is for background export and preview. We don't want to load the
407 // cloned child document again.
408 if (child_buffer_ && 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, bool out)
1052 Buffer const * const childbuffer = getChildBuffer();
1054 childbuffer->updateLabels(Buffer::UpdateChildOnly, out);
1057 if (!isListings(params()))
1061 label_->updateLabels(it, out);
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)) {
1074 listings_label_ += " " + convert<docstring>(counters.value(cnt));