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"
21 #include "Converter.h"
23 #include "DispatchResult.h"
25 #include "ErrorList.h"
28 #include "FuncRequest.h"
29 #include "FuncStatus.h"
30 #include "LaTeXFeatures.h"
31 #include "LayoutFile.h"
32 #include "LayoutModuleList.h"
35 #include "MetricsInfo.h"
36 #include "output_plaintext.h"
37 #include "output_xhtml.h"
38 #include "OutputParams.h"
39 #include "texstream.h"
40 #include "TextClass.h"
41 #include "TocBackend.h"
43 #include "frontends/alert.h"
44 #include "frontends/Painter.h"
46 #include "graphics/PreviewImage.h"
47 #include "graphics/PreviewLoader.h"
49 #include "insets/InsetLabel.h"
50 #include "insets/InsetListingsParams.h"
51 #include "insets/RenderPreview.h"
53 #include "mathed/MacroTable.h"
55 #include "support/convert.h"
56 #include "support/debug.h"
57 #include "support/docstream.h"
58 #include "support/FileNameList.h"
59 #include "support/filetools.h"
60 #include "support/gettext.h"
61 #include "support/lassert.h"
62 #include "support/lstrings.h" // contains
63 #include "support/lyxalgo.h"
64 #include "support/mutex.h"
66 #include "support/bind.h"
69 using namespace lyx::support;
73 namespace Alert = frontend::Alert;
78 docstring const uniqueID()
80 static unsigned int seed = 1000;
82 Mutex::Locker lock(&mutex);
83 return "file" + convert<docstring>(++seed);
87 /// the type of inclusion
89 INCLUDE, VERB, INPUT, VERBAST, LISTINGS, NONE
93 Types type(string const & s)
97 if (s == "verbatiminput")
99 if (s == "verbatiminput*")
101 if (s == "lstinputlisting")
109 Types type(InsetCommandParams const & params)
111 return type(params.getCmdName());
115 bool isListings(InsetCommandParams const & params)
117 return type(params) == LISTINGS;
121 bool isVerbatim(InsetCommandParams const & params)
123 Types const t = type(params);
124 return t == VERB || t == VERBAST;
128 bool isInputOrInclude(InsetCommandParams const & params)
130 Types const t = type(params);
131 return t == INPUT || t == INCLUDE;
135 FileName const masterFileName(Buffer const & buffer)
137 return buffer.masterBuffer()->fileName();
141 void add_preview(RenderMonitoredPreview &, InsetInclude const &, Buffer const &);
144 string const parentFileName(Buffer const & buffer)
146 return buffer.absFileName();
150 FileName const includedFileName(Buffer const & buffer,
151 InsetCommandParams const & params)
153 return makeAbsPath(to_utf8(params["filename"]),
154 onlyPath(parentFileName(buffer)));
158 InsetLabel * createLabel(Buffer * buf, docstring const & label_str)
160 if (label_str.empty())
162 InsetCommandParams icp(LABEL_CODE);
163 icp["name"] = label_str;
164 return new InsetLabel(buf, icp);
170 InsetInclude::InsetInclude(Buffer * buf, InsetCommandParams const & p)
171 : InsetCommand(buf, p), include_label(uniqueID()),
172 preview_(new RenderMonitoredPreview(this)), failedtoload_(false),
173 set_label_(false), label_(0), child_buffer_(0)
175 preview_->fileChanged(bind(&InsetInclude::fileChanged, this));
177 if (isListings(params())) {
178 InsetListingsParams listing_params(to_utf8(p["lstparams"]));
179 label_ = createLabel(buffer_, from_utf8(listing_params.getParamValue("label")));
180 } else if (isInputOrInclude(params()) && buf)
185 InsetInclude::InsetInclude(InsetInclude const & other)
186 : InsetCommand(other), include_label(other.include_label),
187 preview_(new RenderMonitoredPreview(this)), failedtoload_(false),
188 set_label_(false), label_(0), child_buffer_(0)
190 preview_->fileChanged(bind(&InsetInclude::fileChanged, this));
193 label_ = new InsetLabel(*other.label_);
197 InsetInclude::~InsetInclude()
199 if (isBufferLoaded())
200 buffer().invalidateBibfileCache();
205 void InsetInclude::setBuffer(Buffer & buffer)
207 InsetCommand::setBuffer(buffer);
209 label_->setBuffer(buffer);
213 void InsetInclude::setChildBuffer(Buffer * buffer)
215 child_buffer_ = buffer;
219 ParamInfo const & InsetInclude::findInfo(string const & /* cmdName */)
222 // This is only correct for the case of listings, but it'll do for now.
223 // In the other cases, this second parameter should just be empty.
224 static ParamInfo param_info_;
225 if (param_info_.empty()) {
226 param_info_.add("filename", ParamInfo::LATEX_REQUIRED);
227 param_info_.add("lstparams", ParamInfo::LATEX_OPTIONAL);
233 bool InsetInclude::isCompatibleCommand(string const & s)
235 return type(s) != NONE;
239 void InsetInclude::doDispatch(Cursor & cur, FuncRequest & cmd)
241 switch (cmd.action()) {
243 case LFUN_INSET_EDIT: {
244 editIncluded(to_utf8(params()["filename"]));
248 case LFUN_INSET_MODIFY: {
249 // It should be OK just to invalidate the cache in setParams()
251 // child_buffer_ = 0;
252 InsetCommandParams p(INCLUDE_CODE);
253 if (cmd.getArg(0) == "changetype") {
255 InsetCommand::doDispatch(cur, cmd);
258 InsetCommand::string2params(to_utf8(cmd.argument()), p);
259 if (!p.getCmdName().empty()) {
261 InsetListingsParams new_params(to_utf8(p["lstparams"]));
262 docstring const new_label =
263 from_utf8(new_params.getParamValue("label"));
265 if (new_label.empty()) {
271 old_label = label_->getParam("name");
273 label_ = createLabel(buffer_, new_label);
274 label_->setBuffer(buffer());
277 if (new_label != old_label) {
278 label_->updateLabelAndRefs(new_label, &cur);
279 // the label might have been adapted (duplicate)
280 if (new_label != label_->getParam("name")) {
281 new_params.addParam("label", "{" +
282 to_utf8(label_->getParam("name")) + "}", true);
283 p["lstparams"] = from_utf8(new_params.params());
290 cur.forceBufferUpdate();
292 cur.noScreenUpdate();
296 //pass everything else up the chain
298 InsetCommand::doDispatch(cur, cmd);
304 void InsetInclude::editIncluded(string const & file)
306 string const ext = support::getExtension(file);
308 FuncRequest fr(LFUN_BUFFER_CHILD_OPEN, file);
311 // tex file or other text file in verbatim mode
312 formats.edit(buffer(),
313 support::makeAbsPath(file, support::onlyPath(buffer().absFileName())),
318 bool InsetInclude::getStatus(Cursor & cur, FuncRequest const & cmd,
319 FuncStatus & flag) const
321 switch (cmd.action()) {
323 case LFUN_INSET_EDIT:
324 flag.setEnabled(true);
327 case LFUN_INSET_MODIFY:
328 if (cmd.getArg(0) == "changetype")
329 return InsetCommand::getStatus(cur, cmd, flag);
331 flag.setEnabled(true);
335 return InsetCommand::getStatus(cur, cmd, flag);
340 void InsetInclude::setParams(InsetCommandParams const & p)
342 // invalidate the cache
345 InsetCommand::setParams(p);
348 if (preview_->monitoring())
349 preview_->stopMonitoring();
351 if (type(params()) == INPUT)
352 add_preview(*preview_, *this, buffer());
354 buffer().invalidateBibfileCache();
358 bool InsetInclude::isChildIncluded() const
360 std::list<std::string> includeonlys =
361 buffer().params().getIncludedChildren();
362 if (includeonlys.empty())
364 return (std::find(includeonlys.begin(),
366 to_utf8(params()["filename"])) != includeonlys.end());
370 docstring InsetInclude::screenLabel() const
374 switch (type(params())) {
376 temp = buffer().B_("Input");
379 temp = buffer().B_("Verbatim Input");
382 temp = buffer().B_("Verbatim Input*");
385 if (isChildIncluded())
386 temp = buffer().B_("Include");
388 temp += buffer().B_("Include (excluded)");
391 temp = listings_label_;
394 LASSERT(false, temp = buffer().B_("Unknown"));
400 if (params()["filename"].empty())
403 temp += from_utf8(onlyFileName(to_utf8(params()["filename"])));
409 Buffer * InsetInclude::getChildBuffer() const
411 Buffer * childBuffer = loadIfNeeded();
413 // FIXME RECURSIVE INCLUDE
414 // This isn't sufficient, as the inclusion could be downstream.
415 // But it'll have to do for now.
416 return (childBuffer == &buffer()) ? 0 : childBuffer;
420 Buffer * InsetInclude::loadIfNeeded() const
422 // This is for background export and preview. We don't even want to
423 // try to load the cloned child document again.
424 if (buffer().isClone())
425 return child_buffer_;
427 // Don't try to load it again if we failed before.
428 if (failedtoload_ || isVerbatim(params()) || isListings(params()))
431 FileName const included_file = includedFileName(buffer(), params());
432 // Use cached Buffer if possible.
433 if (child_buffer_ != 0) {
434 if (theBufferList().isLoaded(child_buffer_)
435 // additional sanity check: make sure the Buffer really is
436 // associated with the file we want.
437 && child_buffer_ == theBufferList().getBuffer(included_file))
438 return child_buffer_;
439 // Buffer vanished, so invalidate cache and try to reload.
443 if (!isLyXFileName(included_file.absFileName()))
446 Buffer * child = theBufferList().getBuffer(included_file);
448 // the readonly flag can/will be wrong, not anymore I think.
449 if (!included_file.exists())
452 child = theBufferList().newBuffer(included_file.absFileName());
454 // Buffer creation is not possible.
457 // Set parent before loading, such that macros can be tracked
458 child->setParent(&buffer());
460 if (child->loadLyXFile() != Buffer::ReadSuccess) {
461 failedtoload_ = true;
463 //close the buffer we just opened
464 theBufferList().release(child);
468 if (!child->errorList("Parse").empty()) {
469 // FIXME: Do something.
472 // The file was already loaded, so, simply
473 // inform parent buffer about local macros.
474 Buffer const * parent = &buffer();
475 child->setParent(parent);
477 child->listMacroNames(macros);
478 MacroNameSet::const_iterator cit = macros.begin();
479 MacroNameSet::const_iterator end = macros.end();
480 for (; cit != end; ++cit)
481 parent->usermacros.insert(*cit);
484 // Cache the child buffer.
485 child_buffer_ = child;
490 void InsetInclude::latex(otexstream & os, OutputParams const & runparams) const
492 string incfile = to_utf8(params()["filename"]);
494 // Do nothing if no file name has been specified
498 FileName const included_file = includedFileName(buffer(), params());
500 // Check we're not trying to include ourselves.
501 // FIXME RECURSIVE INCLUDE
502 // This isn't sufficient, as the inclusion could be downstream.
503 // But it'll have to do for now.
504 if (isInputOrInclude(params()) &&
505 buffer().absFileName() == included_file.absFileName())
507 Alert::error(_("Recursive input"),
508 bformat(_("Attempted to include file %1$s in itself! "
509 "Ignoring inclusion."), from_utf8(incfile)));
513 Buffer const * const masterBuffer = buffer().masterBuffer();
515 // if incfile is relative, make it relative to the master
517 if (!FileName::isAbsolute(incfile)) {
519 incfile = to_utf8(makeRelPath(from_utf8(included_file.absFileName()),
520 from_utf8(masterBuffer->filePath())));
523 string exppath = incfile;
524 if (!runparams.export_folder.empty()) {
525 exppath = makeAbsPath(exppath, runparams.export_folder).realPath();
526 FileName(exppath).onlyPath().createPath();
529 // write it to a file (so far the complete file)
533 if (type(params()) == LISTINGS) {
534 exportfile = exppath;
535 mangled = DocFileName(included_file).mangledFileName();
537 exportfile = changeExtension(exppath, ".tex");
538 mangled = DocFileName(changeExtension(included_file.absFileName(), ".tex")).
544 else if (!runparams.silent)
545 ; // no warning wanted
546 else if (!isValidLaTeXFileName(incfile)) {
547 frontend::Alert::warning(_("Invalid filename"),
548 _("The following filename will cause troubles "
549 "when running the exported file through LaTeX: ") +
551 } else if (!isValidDVIFileName(incfile)) {
552 frontend::Alert::warning(_("Problematic filename for DVI"),
553 _("The following filename can cause troubles "
554 "when running the exported file through LaTeX "
555 "and opening the resulting DVI: ") +
556 from_utf8(incfile), true);
559 FileName const writefile(makeAbsPath(mangled, runparams.for_preview ?
560 buffer().temppath() : masterBuffer->temppath()));
562 LYXERR(Debug::LATEX, "incfile:" << incfile);
563 LYXERR(Debug::LATEX, "exportfile:" << exportfile);
564 LYXERR(Debug::LATEX, "writefile:" << writefile);
566 string const tex_format = flavor2format(runparams.flavor);
568 switch (type(params())) {
571 incfile = latex_path(incfile);
573 os << '\\' << from_ascii(params().getCmdName()) << '{'
574 << from_utf8(incfile) << '}';
578 runparams.exportdata->addExternalFile(tex_format, writefile,
581 // \input wants file with extension (default is .tex)
582 if (!isLyXFileName(included_file.absFileName())) {
583 incfile = latex_path(incfile);
585 os << '\\' << from_ascii(params().getCmdName())
586 << '{' << from_utf8(incfile) << '}';
588 incfile = changeExtension(incfile, ".tex");
589 incfile = latex_path(incfile);
591 os << '\\' << from_ascii(params().getCmdName())
592 << '{' << from_utf8(incfile) << '}';
597 runparams.exportdata->addExternalFile(tex_format, writefile,
599 os << '\\' << from_ascii(params().getCmdName());
600 string const opt = to_utf8(params()["lstparams"]);
601 // opt is set in QInclude dialog and should have passed validation.
602 InsetListingsParams params(opt);
603 if (!params.params().empty())
604 os << "[" << from_utf8(params.params()) << "]";
605 os << '{' << from_utf8(incfile) << '}';
609 runparams.exportdata->addExternalFile(tex_format, writefile,
612 // \include don't want extension and demands that the
613 // file really have .tex
614 incfile = changeExtension(incfile, string());
615 incfile = latex_path(incfile);
617 os << '\\' << from_ascii(params().getCmdName()) << '{'
618 << from_utf8(incfile) << '}';
625 if (runparams.inComment || runparams.dryrun)
626 // Don't try to load or copy the file if we're
627 // in a comment or doing a dryrun
630 if (isInputOrInclude(params()) &&
631 isLyXFileName(included_file.absFileName())) {
632 // if it's a LyX file and we're inputting or including,
633 // try to load it so we can write the associated latex
635 Buffer * tmp = loadIfNeeded();
637 if (!runparams.silent) {
638 docstring text = bformat(_("Could not load included "
640 "Please, check whether it actually exists."),
641 included_file.displayName());
642 Alert::warning(_("Missing included file"), text);
647 if (!runparams.silent) {
648 if (tmp->params().baseClass() != masterBuffer->params().baseClass()) {
650 docstring text = bformat(_("Included file `%1$s'\n"
651 "has textclass `%2$s'\n"
652 "while parent file has textclass `%3$s'."),
653 included_file.displayName(),
654 from_utf8(tmp->params().documentClass().name()),
655 from_utf8(masterBuffer->params().documentClass().name()));
656 Alert::warning(_("Different textclasses"), text, true);
659 // Make sure modules used in child are all included in master
660 // FIXME It might be worth loading the children's modules into the master
661 // over in BufferParams rather than doing this check.
662 LayoutModuleList const masterModules = masterBuffer->params().getModules();
663 LayoutModuleList const childModules = tmp->params().getModules();
664 LayoutModuleList::const_iterator it = childModules.begin();
665 LayoutModuleList::const_iterator end = childModules.end();
666 for (; it != end; ++it) {
667 string const module = *it;
668 LayoutModuleList::const_iterator found =
669 find(masterModules.begin(), masterModules.end(), module);
670 if (found == masterModules.end()) {
671 docstring text = bformat(_("Included file `%1$s'\n"
672 "uses module `%2$s'\n"
673 "which is not used in parent file."),
674 included_file.displayName(), from_utf8(module));
675 Alert::warning(_("Module not found"), text);
680 tmp->markDepClean(masterBuffer->temppath());
682 // Don't assume the child's format is latex
683 string const inc_format = tmp->params().bufferFormat();
684 FileName const tmpwritefile(changeExtension(writefile.absFileName(),
685 formats.extension(inc_format)));
687 // FIXME: handle non existing files
688 // The included file might be written in a different encoding
690 Encoding const * const oldEnc = runparams.encoding;
691 Language const * const oldLang = runparams.master_language;
692 // If the master uses non-TeX fonts (XeTeX, LuaTeX),
693 // the children must be encoded in plain utf8!
694 runparams.encoding = masterBuffer->params().useNonTeXFonts ?
695 encodings.fromLyXName("utf8-plain")
696 : &tmp->params().encoding();
697 runparams.master_language = buffer().params().language;
698 runparams.par_begin = 0;
699 runparams.par_end = tmp->paragraphs().size();
700 runparams.is_child = true;
701 if (!tmp->makeLaTeXFile(tmpwritefile, masterFileName(buffer()).
702 onlyPath().absFileName(), runparams, Buffer::OnlyBody)) {
703 if (!runparams.silent) {
704 docstring msg = bformat(_("Included file `%1$s' "
705 "was not exported correctly.\nWarning: "
706 "LaTeX export is probably incomplete."),
707 included_file.displayName());
708 ErrorList const & el = tmp->errorList("Export");
710 msg = bformat(from_ascii("%1$s\n\n%2$s\n\n%3$s"),
711 msg, el.begin()->error,
712 el.begin()->description);
713 Alert::warning(_("Export failure"), msg);
716 runparams.encoding = oldEnc;
717 runparams.master_language = oldLang;
718 runparams.is_child = false;
720 // If needed, use converters to produce a latex file from the child
721 if (tmpwritefile != writefile) {
724 theConverters().convert(tmp, tmpwritefile, writefile,
726 inc_format, tex_format, el);
728 if (!success && !runparams.silent) {
729 docstring msg = bformat(_("Included file `%1$s' "
730 "was not exported correctly.\nWarning: "
731 "LaTeX export is probably incomplete."),
732 included_file.displayName());
734 msg = bformat(from_ascii("%1$s\n\n%2$s\n\n%3$s"),
735 msg, el.begin()->error,
736 el.begin()->description);
737 Alert::warning(_("Export failure"), msg);
741 // In this case, it's not a LyX file, so we copy the file
742 // to the temp dir, so that .aux files etc. are not created
743 // in the original dir. Files included by this file will be
744 // found via either the environment variable TEXINPUTS, or
745 // input@path, see ../Buffer.cpp.
746 unsigned long const checksum_in = included_file.checksum();
747 unsigned long const checksum_out = writefile.checksum();
749 if (checksum_in != checksum_out) {
750 if (!included_file.copyTo(writefile)) {
753 to_utf8(bformat(_("Could not copy the file\n%1$s\n"
754 "into the temporary directory."),
755 from_utf8(included_file.absFileName()))));
763 docstring InsetInclude::xhtml(XHTMLStream & xs, OutputParams const & rp) const
768 // For verbatim and listings, we just include the contents of the file as-is.
769 // In the case of listings, we wrap it in <pre>.
770 bool const listing = isListings(params());
771 if (listing || isVerbatim(params())) {
773 xs << html::StartTag("pre");
774 // FIXME: We don't know the encoding of the file, default to UTF-8.
775 xs << includedFileName(buffer(), params()).fileContents("UTF-8");
777 xs << html::EndTag("pre");
781 // We don't (yet) know how to Input or Include non-LyX files.
782 // (If we wanted to get really arcane, we could run some tex2html
783 // converter on the included file. But that's just masochistic.)
784 FileName const included_file = includedFileName(buffer(), params());
785 if (!isLyXFileName(included_file.absFileName())) {
787 frontend::Alert::warning(_("Unsupported Inclusion"),
788 bformat(_("LyX does not know how to include non-LyX files when "
789 "generating HTML output. Offending file:\n%1$s"),
790 params()["filename"]));
794 // In the other cases, we will generate the HTML and include it.
796 // Check we're not trying to include ourselves.
797 // FIXME RECURSIVE INCLUDE
798 if (buffer().absFileName() == included_file.absFileName()) {
799 Alert::error(_("Recursive input"),
800 bformat(_("Attempted to include file %1$s in itself! "
801 "Ignoring inclusion."), params()["filename"]));
805 Buffer const * const ibuf = loadIfNeeded();
809 // are we generating only some paragraphs, or all of them?
810 bool const all_pars = !rp.dryrun ||
811 (rp.par_begin == 0 &&
812 rp.par_end == (int)buffer().text().paragraphs().size());
814 OutputParams op = rp;
818 ibuf->writeLyXHTMLSource(xs.os(), op, Buffer::IncludedFile);
820 xs << XHTMLStream::ESCAPE_NONE
821 << "<!-- Included file: "
822 << from_utf8(included_file.absFileName())
823 << XHTMLStream::ESCAPE_NONE
829 int InsetInclude::plaintext(odocstringstream & os,
830 OutputParams const & op, size_t) const
832 // just write the filename if we're making a tooltip or toc entry,
833 // or are generating this for advanced search
834 if (op.for_tooltip || op.for_toc || op.for_search) {
835 os << '[' << screenLabel() << '\n'
836 << getParam("filename") << "\n]";
837 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
840 if (isVerbatim(params()) || isListings(params())) {
841 os << '[' << screenLabel() << '\n'
842 // FIXME: We don't know the encoding of the file, default to UTF-8.
843 << includedFileName(buffer(), params()).fileContents("UTF-8")
845 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
848 Buffer const * const ibuf = loadIfNeeded();
850 docstring const str = '[' + screenLabel() + ']';
854 writePlaintextFile(*ibuf, os, op);
859 int InsetInclude::docbook(odocstream & os, OutputParams const & runparams) const
861 string incfile = to_utf8(params()["filename"]);
863 // Do nothing if no file name has been specified
867 string const included_file = includedFileName(buffer(), params()).absFileName();
869 // Check we're not trying to include ourselves.
870 // FIXME RECURSIVE INCLUDE
871 // This isn't sufficient, as the inclusion could be downstream.
872 // But it'll have to do for now.
873 if (buffer().absFileName() == included_file) {
874 Alert::error(_("Recursive input"),
875 bformat(_("Attempted to include file %1$s in itself! "
876 "Ignoring inclusion."), from_utf8(incfile)));
880 string exppath = incfile;
881 if (!runparams.export_folder.empty()) {
882 exppath = makeAbsPath(exppath, runparams.export_folder).realPath();
883 FileName(exppath).onlyPath().createPath();
886 // write it to a file (so far the complete file)
887 string const exportfile = changeExtension(exppath, ".sgml");
888 DocFileName writefile(changeExtension(included_file, ".sgml"));
890 Buffer * tmp = loadIfNeeded();
892 string const mangled = writefile.mangledFileName();
893 writefile = makeAbsPath(mangled,
894 buffer().masterBuffer()->temppath());
898 LYXERR(Debug::LATEX, "incfile:" << incfile);
899 LYXERR(Debug::LATEX, "exportfile:" << exportfile);
900 LYXERR(Debug::LATEX, "writefile:" << writefile);
902 tmp->makeDocBookFile(writefile, runparams, Buffer::OnlyBody);
905 runparams.exportdata->addExternalFile("docbook", writefile,
907 runparams.exportdata->addExternalFile("docbook-xml", writefile,
910 if (isVerbatim(params()) || isListings(params())) {
911 os << "<inlinegraphic fileref=\""
912 << '&' << include_label << ';'
913 << "\" format=\"linespecific\">";
915 os << '&' << include_label << ';';
921 void InsetInclude::validate(LaTeXFeatures & features) const
923 LATTEST(&buffer() == &features.buffer());
925 string incfile = to_utf8(params()["filename"]);
926 string const included_file =
927 includedFileName(buffer(), params()).absFileName();
930 if (isLyXFileName(included_file))
931 writefile = changeExtension(included_file, ".sgml");
933 writefile = included_file;
935 if (!features.runparams().nice && !isVerbatim(params()) && !isListings(params())) {
936 incfile = DocFileName(writefile).mangledFileName();
937 writefile = makeAbsPath(incfile,
938 buffer().masterBuffer()->temppath()).absFileName();
941 features.includeFile(include_label, writefile);
943 features.useInsetLayout(getLayout());
944 if (isVerbatim(params()))
945 features.require("verbatim");
946 else if (isListings(params()))
947 features.require("listings");
949 // Here we must do the fun stuff...
950 // Load the file in the include if it needs
952 Buffer * const tmp = loadIfNeeded();
954 // the file is loaded
955 // make sure the buffer isn't us
956 // FIXME RECURSIVE INCLUDES
957 // This is not sufficient, as recursive includes could be
958 // more than a file away. But it will do for now.
959 if (tmp && tmp != &buffer()) {
960 // We must temporarily change features.buffer,
961 // otherwise it would always be the master buffer,
962 // and nested includes would not work.
963 features.setBuffer(*tmp);
964 // Maybe this is already a child
965 bool const is_child =
966 features.runparams().is_child;
967 features.runparams().is_child = true;
968 tmp->validate(features);
969 features.runparams().is_child = is_child;
970 features.setBuffer(buffer());
976 void InsetInclude::collectBibKeys(InsetIterator const & /*di*/) const
978 Buffer * child = loadIfNeeded();
981 // FIXME RECURSIVE INCLUDE
982 // This isn't sufficient, as the inclusion could be downstream.
983 // But it'll have to do for now.
984 if (child == &buffer())
986 child->collectBibKeys();
990 void InsetInclude::metrics(MetricsInfo & mi, Dimension & dim) const
994 bool use_preview = false;
995 if (RenderPreview::previewText()) {
996 graphics::PreviewImage const * pimage =
997 preview_->getPreviewImage(mi.base.bv->buffer());
998 use_preview = pimage && pimage->image();
1002 preview_->metrics(mi, dim);
1006 button_.update(screenLabel(), true);
1008 button_.metrics(mi, dim);
1011 Box b(0, dim.wid, -dim.asc, dim.des);
1016 void InsetInclude::draw(PainterInfo & pi, int x, int y) const
1018 LBUFERR(pi.base.bv);
1020 bool use_preview = false;
1021 if (RenderPreview::previewText()) {
1022 graphics::PreviewImage const * pimage =
1023 preview_->getPreviewImage(pi.base.bv->buffer());
1024 use_preview = pimage && pimage->image();
1028 preview_->draw(pi, x, y);
1030 button_.draw(pi, x, y);
1034 void InsetInclude::write(ostream & os) const
1036 params().Write(os, &buffer());
1040 string InsetInclude::contextMenuName() const
1042 return "context-include";
1046 Inset::DisplayType InsetInclude::display() const
1048 return type(params()) == INPUT ? Inline : AlignCenter;
1052 docstring InsetInclude::layoutName() const
1054 if (isListings(params()))
1055 return from_ascii("IncludeListings");
1056 return InsetCommand::layoutName();
1064 void InsetInclude::fileChanged() const
1066 Buffer const * const buffer = updateFrontend();
1070 preview_->removePreview(*buffer);
1071 add_preview(*preview_, *this, *buffer);
1072 preview_->startLoading(*buffer);
1078 bool preview_wanted(InsetCommandParams const & params, Buffer const & buffer)
1080 FileName const included_file = includedFileName(buffer, params);
1082 return type(params) == INPUT && params.preview() &&
1083 included_file.isReadableFile();
1087 docstring latexString(InsetInclude const & inset)
1089 odocstringstream ods;
1090 otexstream os(ods, false);
1091 // We don't need to set runparams.encoding since this will be done
1092 // by latex() anyway.
1093 OutputParams runparams(0);
1094 runparams.flavor = OutputParams::LATEX;
1095 runparams.for_preview = true;
1096 inset.latex(os, runparams);
1102 void add_preview(RenderMonitoredPreview & renderer, InsetInclude const & inset,
1103 Buffer const & buffer)
1105 InsetCommandParams const & params = inset.params();
1106 if (RenderPreview::previewText() && preview_wanted(params, buffer)) {
1107 renderer.setAbsFile(includedFileName(buffer, params));
1108 docstring const snippet = latexString(inset);
1109 renderer.addPreview(snippet, buffer);
1116 void InsetInclude::addPreview(DocIterator const & /*inset_pos*/,
1117 graphics::PreviewLoader & ploader) const
1119 Buffer const & buffer = ploader.buffer();
1120 if (!preview_wanted(params(), buffer))
1122 preview_->setAbsFile(includedFileName(buffer, params()));
1123 docstring const snippet = latexString(*this);
1124 preview_->addPreview(snippet, ploader);
1128 void InsetInclude::addToToc(DocIterator const & cpit, bool output_active,
1129 UpdateType utype) const
1131 TocBackend & backend = buffer().tocBackend();
1133 if (isListings(params())) {
1135 label_->addToToc(cpit, output_active, utype);
1137 InsetListingsParams p(to_utf8(params()["lstparams"]));
1138 string caption = p.getParamValue("caption");
1139 if (caption.empty())
1141 shared_ptr<Toc> toc = backend.toc("listing");
1142 docstring str = convert<docstring>(toc->size() + 1)
1143 + ". " + from_utf8(caption);
1144 DocIterator pit = cpit;
1145 toc->push_back(TocItem(pit, 0, str, output_active));
1147 Buffer const * const childbuffer = getChildBuffer();
1151 shared_ptr<Toc> toc = backend.toc("child");
1152 docstring str = childbuffer->fileName().displayName();
1153 toc->push_back(TocItem(cpit, 0, str, output_active));
1155 childbuffer->tocBackend().update(output_active, utype);
1156 TocList const & childtoclist = childbuffer->tocBackend().tocs();
1157 TocList::const_iterator it = childtoclist.begin();
1158 TocList::const_iterator const end = childtoclist.end();
1159 for(; it != end; ++it) {
1160 shared_ptr<Toc> toc = backend.toc(it->first);
1161 toc->insert(toc->end(), it->second->begin(), it->second->end());
1167 void InsetInclude::updateCommand()
1172 docstring old_label = label_->getParam("name");
1173 label_->updateLabel(old_label);
1174 // the label might have been adapted (duplicate)
1175 docstring new_label = label_->getParam("name");
1176 if (old_label == new_label)
1179 // update listings parameters...
1180 InsetCommandParams p(INCLUDE_CODE);
1182 InsetListingsParams par(to_utf8(params()["lstparams"]));
1183 par.addParam("label", "{" + to_utf8(new_label) + "}", true);
1184 p["lstparams"] = from_utf8(par.params());
1189 void InsetInclude::updateBuffer(ParIterator const & it, UpdateType utype)
1191 button_.update(screenLabel(), true);
1193 Buffer const * const childbuffer = getChildBuffer();
1195 childbuffer->updateBuffer(Buffer::UpdateChildOnly, utype);
1198 if (!isListings(params()))
1202 label_->updateBuffer(it, utype);
1204 InsetListingsParams const par(to_utf8(params()["lstparams"]));
1205 if (par.getParamValue("caption").empty()) {
1206 listings_label_ = buffer().B_("Program Listing");
1209 Buffer const & master = *buffer().masterBuffer();
1210 Counters & counters = master.params().documentClass().counters();
1211 docstring const cnt = from_ascii("listing");
1212 listings_label_ = master.B_("Program Listing");
1213 if (counters.hasCounter(cnt)) {
1214 counters.step(cnt, utype);
1215 listings_label_ += " " + convert<docstring>(counters.value(cnt));