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"
65 #include "support/ExceptionMessage.h"
67 #include "support/bind.h"
70 using namespace lyx::support;
74 namespace Alert = frontend::Alert;
79 docstring const uniqueID()
81 static unsigned int seed = 1000;
83 Mutex::Locker lock(&mutex);
84 return "file" + convert<docstring>(++seed);
88 /// the type of inclusion
90 INCLUDE, VERB, INPUT, VERBAST, LISTINGS, NONE
94 Types type(string const & s)
98 if (s == "verbatiminput")
100 if (s == "verbatiminput*")
102 if (s == "lstinputlisting" || s == "inputminted")
110 Types type(InsetCommandParams const & params)
112 return type(params.getCmdName());
116 bool isListings(InsetCommandParams const & params)
118 return type(params) == LISTINGS;
122 bool isVerbatim(InsetCommandParams const & params)
124 Types const t = type(params);
125 return t == VERB || t == VERBAST;
129 bool isInputOrInclude(InsetCommandParams const & params)
131 Types const t = type(params);
132 return t == INPUT || t == INCLUDE;
136 FileName const masterFileName(Buffer const & buffer)
138 return buffer.masterBuffer()->fileName();
142 void add_preview(RenderMonitoredPreview &, InsetInclude const &, Buffer const &);
145 string const parentFileName(Buffer const & buffer)
147 return buffer.absFileName();
151 FileName const includedFileName(Buffer const & buffer,
152 InsetCommandParams const & params)
154 return makeAbsPath(to_utf8(params["filename"]),
155 onlyPath(parentFileName(buffer)));
159 InsetLabel * createLabel(Buffer * buf, docstring const & label_str)
161 if (label_str.empty())
163 InsetCommandParams icp(LABEL_CODE);
164 icp["name"] = label_str;
165 return new InsetLabel(buf, icp);
169 char_type replaceCommaInBraces(docstring & params)
171 // Code point from private use area
172 char_type private_char = 0xE000;
174 for (char_type & c : params) {
179 else if (c == ',' && count)
188 InsetInclude::InsetInclude(Buffer * buf, InsetCommandParams const & p)
189 : InsetCommand(buf, p), include_label(uniqueID()),
190 preview_(make_unique<RenderMonitoredPreview>(this)), failedtoload_(false),
191 set_label_(false), label_(0), child_buffer_(0)
193 preview_->connect([=](){ fileChanged(); });
195 if (isListings(params())) {
196 InsetListingsParams listing_params(to_utf8(p["lstparams"]));
197 label_ = createLabel(buffer_, from_utf8(listing_params.getParamValue("label")));
198 } else if (isInputOrInclude(params()) && buf)
203 InsetInclude::InsetInclude(InsetInclude const & other)
204 : InsetCommand(other), include_label(other.include_label),
205 preview_(make_unique<RenderMonitoredPreview>(this)), failedtoload_(false),
206 set_label_(false), label_(0), child_buffer_(0)
208 preview_->connect([=](){ fileChanged(); });
211 label_ = new InsetLabel(*other.label_);
215 InsetInclude::~InsetInclude()
221 void InsetInclude::setBuffer(Buffer & buffer)
223 InsetCommand::setBuffer(buffer);
225 label_->setBuffer(buffer);
229 void InsetInclude::setChildBuffer(Buffer * buffer)
231 child_buffer_ = buffer;
235 ParamInfo const & InsetInclude::findInfo(string const & /* cmdName */)
238 // This is only correct for the case of listings, but it'll do for now.
239 // In the other cases, this second parameter should just be empty.
240 static ParamInfo param_info_;
241 if (param_info_.empty()) {
242 param_info_.add("filename", ParamInfo::LATEX_REQUIRED);
243 param_info_.add("lstparams", ParamInfo::LATEX_OPTIONAL);
249 bool InsetInclude::isCompatibleCommand(string const & s)
251 return type(s) != NONE;
255 void InsetInclude::doDispatch(Cursor & cur, FuncRequest & cmd)
257 switch (cmd.action()) {
259 case LFUN_INSET_EDIT: {
260 editIncluded(to_utf8(params()["filename"]));
264 case LFUN_INSET_MODIFY: {
265 // It should be OK just to invalidate the cache in setParams()
267 // child_buffer_ = 0;
268 InsetCommandParams p(INCLUDE_CODE);
269 if (cmd.getArg(0) == "changetype") {
271 InsetCommand::doDispatch(cur, cmd);
274 InsetCommand::string2params(to_utf8(cmd.argument()), p);
275 if (!p.getCmdName().empty()) {
277 InsetListingsParams new_params(to_utf8(p["lstparams"]));
278 docstring const new_label =
279 from_utf8(new_params.getParamValue("label"));
281 if (new_label.empty()) {
287 old_label = label_->getParam("name");
289 label_ = createLabel(buffer_, new_label);
290 label_->setBuffer(buffer());
293 if (new_label != old_label) {
294 label_->updateLabelAndRefs(new_label, &cur);
295 // the label might have been adapted (duplicate)
296 if (new_label != label_->getParam("name")) {
297 new_params.addParam("label", "{" +
298 to_utf8(label_->getParam("name")) + "}", true);
299 p["lstparams"] = from_utf8(new_params.params());
306 cur.forceBufferUpdate();
308 cur.noScreenUpdate();
312 //pass everything else up the chain
314 InsetCommand::doDispatch(cur, cmd);
320 void InsetInclude::editIncluded(string const & file)
322 string const ext = support::getExtension(file);
324 FuncRequest fr(LFUN_BUFFER_CHILD_OPEN, file);
327 // tex file or other text file in verbatim mode
328 theFormats().edit(buffer(),
329 support::makeAbsPath(file, support::onlyPath(buffer().absFileName())),
334 bool InsetInclude::getStatus(Cursor & cur, FuncRequest const & cmd,
335 FuncStatus & flag) const
337 switch (cmd.action()) {
339 case LFUN_INSET_EDIT:
340 flag.setEnabled(true);
343 case LFUN_INSET_MODIFY:
344 if (cmd.getArg(0) == "changetype")
345 return InsetCommand::getStatus(cur, cmd, flag);
347 flag.setEnabled(true);
351 return InsetCommand::getStatus(cur, cmd, flag);
356 void InsetInclude::setParams(InsetCommandParams const & p)
358 // invalidate the cache
361 InsetCommand::setParams(p);
364 if (preview_->monitoring())
365 preview_->stopMonitoring();
367 if (type(params()) == INPUT)
368 add_preview(*preview_, *this, buffer());
372 bool InsetInclude::isChildIncluded() const
374 std::list<std::string> includeonlys =
375 buffer().params().getIncludedChildren();
376 if (includeonlys.empty())
378 return (std::find(includeonlys.begin(),
380 to_utf8(params()["filename"])) != includeonlys.end());
384 docstring InsetInclude::screenLabel() const
388 switch (type(params())) {
390 temp = buffer().B_("Input");
393 temp = buffer().B_("Verbatim Input");
396 temp = buffer().B_("Verbatim Input*");
399 if (isChildIncluded())
400 temp = buffer().B_("Include");
402 temp += buffer().B_("Include (excluded)");
405 temp = listings_label_;
408 LASSERT(false, temp = buffer().B_("Unknown"));
414 if (params()["filename"].empty())
417 temp += from_utf8(onlyFileName(to_utf8(params()["filename"])));
423 Buffer * InsetInclude::getChildBuffer() const
425 Buffer * childBuffer = loadIfNeeded();
427 // FIXME RECURSIVE INCLUDE
428 // This isn't sufficient, as the inclusion could be downstream.
429 // But it'll have to do for now.
430 return (childBuffer == &buffer()) ? 0 : childBuffer;
434 Buffer * InsetInclude::loadIfNeeded() const
436 // This is for background export and preview. We don't even want to
437 // try to load the cloned child document again.
438 if (buffer().isClone())
439 return child_buffer_;
441 // Don't try to load it again if we failed before.
442 if (failedtoload_ || isVerbatim(params()) || isListings(params()))
445 FileName const included_file = includedFileName(buffer(), params());
446 // Use cached Buffer if possible.
447 if (child_buffer_ != 0) {
448 if (theBufferList().isLoaded(child_buffer_)
449 // additional sanity check: make sure the Buffer really is
450 // associated with the file we want.
451 && child_buffer_ == theBufferList().getBuffer(included_file))
452 return child_buffer_;
453 // Buffer vanished, so invalidate cache and try to reload.
457 if (!isLyXFileName(included_file.absFileName()))
460 Buffer * child = theBufferList().getBuffer(included_file);
462 // the readonly flag can/will be wrong, not anymore I think.
463 if (!included_file.exists()) {
464 failedtoload_ = true;
468 child = theBufferList().newBuffer(included_file.absFileName());
470 // Buffer creation is not possible.
473 // Set parent before loading, such that macros can be tracked
474 child->setParent(&buffer());
476 if (child->loadLyXFile() != Buffer::ReadSuccess) {
477 failedtoload_ = true;
479 //close the buffer we just opened
480 theBufferList().release(child);
484 if (!child->errorList("Parse").empty()) {
485 // FIXME: Do something.
488 // The file was already loaded, so, simply
489 // inform parent buffer about local macros.
490 Buffer const * parent = &buffer();
491 child->setParent(parent);
493 child->listMacroNames(macros);
494 MacroNameSet::const_iterator cit = macros.begin();
495 MacroNameSet::const_iterator end = macros.end();
496 for (; cit != end; ++cit)
497 parent->usermacros.insert(*cit);
500 // Cache the child buffer.
501 child_buffer_ = child;
506 void InsetInclude::latex(otexstream & os, OutputParams const & runparams) const
508 string incfile = to_utf8(params()["filename"]);
510 // Do nothing if no file name has been specified
514 FileName const included_file = includedFileName(buffer(), params());
516 // Check we're not trying to include ourselves.
517 // FIXME RECURSIVE INCLUDE
518 // This isn't sufficient, as the inclusion could be downstream.
519 // But it'll have to do for now.
520 if (isInputOrInclude(params()) &&
521 buffer().absFileName() == included_file.absFileName())
523 Alert::error(_("Recursive input"),
524 bformat(_("Attempted to include file %1$s in itself! "
525 "Ignoring inclusion."), from_utf8(incfile)));
529 Buffer const * const masterBuffer = buffer().masterBuffer();
531 // if incfile is relative, make it relative to the master
533 if (!FileName::isAbsolute(incfile)) {
535 incfile = to_utf8(makeRelPath(from_utf8(included_file.absFileName()),
536 from_utf8(masterBuffer->filePath())));
539 string exppath = incfile;
540 if (!runparams.export_folder.empty()) {
541 exppath = makeAbsPath(exppath, runparams.export_folder).realPath();
544 // write it to a file (so far the complete file)
548 if (type(params()) == LISTINGS) {
549 exportfile = exppath;
550 mangled = DocFileName(included_file).mangledFileName();
552 exportfile = changeExtension(exppath, ".tex");
553 mangled = DocFileName(changeExtension(included_file.absFileName(), ".tex")).
559 else if (!runparams.silent)
560 ; // no warning wanted
561 else if (!isValidLaTeXFileName(incfile)) {
562 frontend::Alert::warning(_("Invalid filename"),
563 _("The following filename will cause troubles "
564 "when running the exported file through LaTeX: ") +
566 } else if (!isValidDVIFileName(incfile)) {
567 frontend::Alert::warning(_("Problematic filename for DVI"),
568 _("The following filename can cause troubles "
569 "when running the exported file through LaTeX "
570 "and opening the resulting DVI: ") +
571 from_utf8(incfile), true);
574 FileName const writefile(makeAbsPath(mangled, runparams.for_preview ?
575 buffer().temppath() : masterBuffer->temppath()));
577 LYXERR(Debug::LATEX, "incfile:" << incfile);
578 LYXERR(Debug::LATEX, "exportfile:" << exportfile);
579 LYXERR(Debug::LATEX, "writefile:" << writefile);
581 string const tex_format = flavor2format(runparams.flavor);
583 switch (type(params())) {
586 incfile = latex_path(incfile);
588 os << '\\' << from_ascii(params().getCmdName()) << '{'
589 << from_utf8(incfile) << '}';
593 runparams.exportdata->addExternalFile(tex_format, writefile,
596 // \input wants file with extension (default is .tex)
597 if (!isLyXFileName(included_file.absFileName())) {
598 incfile = latex_path(incfile);
600 os << '\\' << from_ascii(params().getCmdName())
601 << '{' << from_utf8(incfile) << '}';
603 incfile = changeExtension(incfile, ".tex");
604 incfile = latex_path(incfile);
606 os << '\\' << from_ascii(params().getCmdName())
607 << '{' << from_utf8(incfile) << '}';
612 // Here, listings and minted have sligthly different behaviors.
613 // Using listings, it is always possible to have a caption,
614 // even for non-floats. Using minted, only floats can have a
615 // caption. So, with minted we use the following strategy.
616 // If a caption was specified but the float parameter was not,
617 // we ourselves add a caption above the listing (because the
618 // listing comes from a file and might span several pages).
619 // Otherwise, if float was specified, the floating listing
620 // environment provided by minted is used. In either case, the
621 // label parameter is taken as the label by which the float
622 // can be referenced, otherwise it will have the meaning
623 // intended by minted. In this last case, the label will
624 // serve as a sort of caption that, however, will be shown
625 // by minted only if the frame parameter is also specified.
626 bool const use_minted = buffer().params().use_minted;
627 runparams.exportdata->addExternalFile(tex_format, writefile,
629 string const opt = to_utf8(params()["lstparams"]);
630 // opt is set in QInclude dialog and should have passed validation.
631 InsetListingsParams lstparams(opt);
632 docstring parameters = from_utf8(lstparams.params());
637 bool isfloat = lstparams.isFloat();
638 // We are going to split parameters at commas, so
639 // replace commas that are not parameter separators
640 // with a code point from the private use area
641 char_type comma = replaceCommaInBraces(parameters);
642 // Get float placement, language, caption, and
643 // label, then remove the relative options if minted.
644 vector<docstring> opts =
645 getVectorFromString(parameters, from_ascii(","), false);
646 vector<docstring> latexed_opts;
647 for (size_t i = 0; i < opts.size(); ++i) {
648 // Restore replaced commas
649 opts[i] = subst(opts[i], comma, ',');
650 if (use_minted && prefixIs(opts[i], from_ascii("float"))) {
651 if (prefixIs(opts[i], from_ascii("float=")))
652 placement = opts[i].substr(6);
653 opts.erase(opts.begin() + i--);
654 } else if (use_minted && prefixIs(opts[i], from_ascii("language="))) {
655 language = opts[i].substr(9);
656 opts.erase(opts.begin() + i--);
657 } else if (prefixIs(opts[i], from_ascii("caption="))) {
658 // FIXME We should use HANDLING_LATEXIFY here,
659 // but that's a file format change (see #10455).
660 caption = opts[i].substr(8);
661 opts.erase(opts.begin() + i--);
663 latexed_opts.push_back(from_ascii("caption=") + caption);
664 } else if (prefixIs(opts[i], from_ascii("label="))) {
665 label = params().prepareCommand(runparams, trim(opts[i].substr(6), "{}"),
666 ParamInfo::HANDLING_ESCAPE);
667 opts.erase(opts.begin() + i--);
669 latexed_opts.push_back(from_ascii("label={") + label + "}");
671 if (use_minted && !label.empty()) {
672 if (isfloat || !caption.empty())
673 label = trim(label, "{}");
675 opts.push_back(from_ascii("label=") + label);
678 if (!latexed_opts.empty())
679 opts.insert(opts.end(), latexed_opts.begin(), latexed_opts.end());
680 parameters = getStringFromVector(opts, from_ascii(","));
681 if (language.empty())
682 language = from_ascii("TeX");
683 if (use_minted && isfloat) {
684 os << breakln << "\\begin{listing}";
685 if (!placement.empty())
686 os << '[' << placement << "]";
688 } else if (use_minted && !caption.empty()) {
689 os << breakln << "\\lyxmintcaption[t]{" << caption;
691 os << "\\label{" << label << "}";
694 os << (use_minted ? "\\inputminted" : "\\lstinputlisting");
695 if (!parameters.empty())
696 os << "[" << parameters << "]";
698 os << '{' << ascii_lowercase(language) << '}';
699 os << '{' << incfile << '}';
700 if (use_minted && isfloat) {
701 if (!caption.empty())
702 os << breakln << "\\caption{" << caption << "}";
704 os << breakln << "\\label{" << label << "}";
705 os << breakln << "\\end{listing}\n";
710 runparams.exportdata->addExternalFile(tex_format, writefile,
713 // \include don't want extension and demands that the
714 // file really have .tex
715 incfile = changeExtension(incfile, string());
716 incfile = latex_path(incfile);
718 os << '\\' << from_ascii(params().getCmdName()) << '{'
719 << from_utf8(incfile) << '}';
726 if (runparams.inComment || runparams.dryrun)
727 // Don't try to load or copy the file if we're
728 // in a comment or doing a dryrun
731 if (isInputOrInclude(params()) &&
732 isLyXFileName(included_file.absFileName())) {
733 // if it's a LyX file and we're inputting or including,
734 // try to load it so we can write the associated latex
736 Buffer * tmp = loadIfNeeded();
738 if (!runparams.silent) {
739 docstring text = bformat(_("Could not load included "
741 "Please, check whether it actually exists."),
742 included_file.displayName());
743 throw ExceptionMessage(ErrorException, _("Error: "),
749 if (!runparams.silent) {
750 if (tmp->params().baseClass() != masterBuffer->params().baseClass()) {
752 docstring text = bformat(_("Included file `%1$s'\n"
753 "has textclass `%2$s'\n"
754 "while parent file has textclass `%3$s'."),
755 included_file.displayName(),
756 from_utf8(tmp->params().documentClass().name()),
757 from_utf8(masterBuffer->params().documentClass().name()));
758 Alert::warning(_("Different textclasses"), text, true);
761 string const child_tf = tmp->params().useNonTeXFonts ? "true" : "false";
762 string const master_tf = masterBuffer->params().useNonTeXFonts ? "true" : "false";
763 if (tmp->params().useNonTeXFonts != masterBuffer->params().useNonTeXFonts) {
764 docstring text = bformat(_("Included file `%1$s'\n"
765 "has use-non-TeX-fonts set to `%2$s'\n"
766 "while parent file has use-non-TeX-fonts set to `%3$s'."),
767 included_file.displayName(),
769 from_utf8(master_tf));
770 Alert::warning(_("Different use-non-TeX-fonts settings"), text, true);
773 // Make sure modules used in child are all included in master
774 // FIXME It might be worth loading the children's modules into the master
775 // over in BufferParams rather than doing this check.
776 LayoutModuleList const masterModules = masterBuffer->params().getModules();
777 LayoutModuleList const childModules = tmp->params().getModules();
778 LayoutModuleList::const_iterator it = childModules.begin();
779 LayoutModuleList::const_iterator end = childModules.end();
780 for (; it != end; ++it) {
781 string const module = *it;
782 LayoutModuleList::const_iterator found =
783 find(masterModules.begin(), masterModules.end(), module);
784 if (found == masterModules.end()) {
785 docstring text = bformat(_("Included file `%1$s'\n"
786 "uses module `%2$s'\n"
787 "which is not used in parent file."),
788 included_file.displayName(), from_utf8(module));
789 Alert::warning(_("Module not found"), text, true);
794 tmp->markDepClean(masterBuffer->temppath());
796 // Don't assume the child's format is latex
797 string const inc_format = tmp->params().bufferFormat();
798 FileName const tmpwritefile(changeExtension(writefile.absFileName(),
799 theFormats().extension(inc_format)));
801 // FIXME: handle non existing files
802 // The included file might be written in a different encoding
804 Encoding const * const oldEnc = runparams.encoding;
805 Language const * const oldLang = runparams.master_language;
806 // If the master uses non-TeX fonts (XeTeX, LuaTeX),
807 // the children must be encoded in plain utf8!
808 runparams.encoding = masterBuffer->params().useNonTeXFonts ?
809 encodings.fromLyXName("utf8-plain")
810 : &tmp->params().encoding();
811 runparams.master_language = buffer().params().language;
812 runparams.par_begin = 0;
813 runparams.par_end = tmp->paragraphs().size();
814 runparams.is_child = true;
815 if (!tmp->makeLaTeXFile(tmpwritefile, masterFileName(buffer()).
816 onlyPath().absFileName(), runparams, Buffer::OnlyBody)) {
817 if (!runparams.silent) {
818 docstring msg = bformat(_("Included file `%1$s' "
819 "was not exported correctly.\n "
820 "LaTeX export is probably incomplete."),
821 included_file.displayName());
822 ErrorList const & el = tmp->errorList("Export");
824 msg = bformat(from_ascii("%1$s\n\n%2$s\n\n%3$s"),
825 msg, el.begin()->error,
826 el.begin()->description);
827 throw ExceptionMessage(ErrorException, _("Error: "),
831 runparams.encoding = oldEnc;
832 runparams.master_language = oldLang;
833 runparams.is_child = false;
835 // If needed, use converters to produce a latex file from the child
836 if (tmpwritefile != writefile) {
839 theConverters().convert(tmp, tmpwritefile, writefile,
841 inc_format, tex_format, el);
843 if (!success && !runparams.silent) {
844 docstring msg = bformat(_("Included file `%1$s' "
845 "was not exported correctly.\n "
846 "LaTeX export is probably incomplete."),
847 included_file.displayName());
849 msg = bformat(from_ascii("%1$s\n\n%2$s\n\n%3$s"),
850 msg, el.begin()->error,
851 el.begin()->description);
852 throw ExceptionMessage(ErrorException, _("Error: "),
857 // In this case, it's not a LyX file, so we copy the file
858 // to the temp dir, so that .aux files etc. are not created
859 // in the original dir. Files included by this file will be
860 // found via either the environment variable TEXINPUTS, or
861 // input@path, see ../Buffer.cpp.
862 unsigned long const checksum_in = included_file.checksum();
863 unsigned long const checksum_out = writefile.checksum();
865 if (checksum_in != checksum_out) {
866 if (!included_file.copyTo(writefile)) {
869 to_utf8(bformat(_("Could not copy the file\n%1$s\n"
870 "into the temporary directory."),
871 from_utf8(included_file.absFileName()))));
879 docstring InsetInclude::xhtml(XHTMLStream & xs, OutputParams const & rp) const
884 // For verbatim and listings, we just include the contents of the file as-is.
885 // In the case of listings, we wrap it in <pre>.
886 bool const listing = isListings(params());
887 if (listing || isVerbatim(params())) {
889 xs << html::StartTag("pre");
890 // FIXME: We don't know the encoding of the file, default to UTF-8.
891 xs << includedFileName(buffer(), params()).fileContents("UTF-8");
893 xs << html::EndTag("pre");
897 // We don't (yet) know how to Input or Include non-LyX files.
898 // (If we wanted to get really arcane, we could run some tex2html
899 // converter on the included file. But that's just masochistic.)
900 FileName const included_file = includedFileName(buffer(), params());
901 if (!isLyXFileName(included_file.absFileName())) {
903 frontend::Alert::warning(_("Unsupported Inclusion"),
904 bformat(_("LyX does not know how to include non-LyX files when "
905 "generating HTML output. Offending file:\n%1$s"),
906 params()["filename"]));
910 // In the other cases, we will generate the HTML and include it.
912 // Check we're not trying to include ourselves.
913 // FIXME RECURSIVE INCLUDE
914 if (buffer().absFileName() == included_file.absFileName()) {
915 Alert::error(_("Recursive input"),
916 bformat(_("Attempted to include file %1$s in itself! "
917 "Ignoring inclusion."), params()["filename"]));
921 Buffer const * const ibuf = loadIfNeeded();
925 // are we generating only some paragraphs, or all of them?
926 bool const all_pars = !rp.dryrun ||
927 (rp.par_begin == 0 &&
928 rp.par_end == (int)buffer().text().paragraphs().size());
930 OutputParams op = rp;
934 ibuf->writeLyXHTMLSource(xs.os(), op, Buffer::IncludedFile);
936 xs << XHTMLStream::ESCAPE_NONE
937 << "<!-- Included file: "
938 << from_utf8(included_file.absFileName())
939 << XHTMLStream::ESCAPE_NONE
945 int InsetInclude::plaintext(odocstringstream & os,
946 OutputParams const & op, size_t) const
948 // just write the filename if we're making a tooltip or toc entry,
949 // or are generating this for advanced search
950 if (op.for_tooltip || op.for_toc || op.for_search) {
951 os << '[' << screenLabel() << '\n'
952 << getParam("filename") << "\n]";
953 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
956 if (isVerbatim(params()) || isListings(params())) {
957 os << '[' << screenLabel() << '\n'
958 // FIXME: We don't know the encoding of the file, default to UTF-8.
959 << includedFileName(buffer(), params()).fileContents("UTF-8")
961 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
964 Buffer const * const ibuf = loadIfNeeded();
966 docstring const str = '[' + screenLabel() + ']';
970 writePlaintextFile(*ibuf, os, op);
975 int InsetInclude::docbook(odocstream & os, OutputParams const & runparams) const
977 string incfile = to_utf8(params()["filename"]);
979 // Do nothing if no file name has been specified
983 string const included_file = includedFileName(buffer(), params()).absFileName();
985 // Check we're not trying to include ourselves.
986 // FIXME RECURSIVE INCLUDE
987 // This isn't sufficient, as the inclusion could be downstream.
988 // But it'll have to do for now.
989 if (buffer().absFileName() == included_file) {
990 Alert::error(_("Recursive input"),
991 bformat(_("Attempted to include file %1$s in itself! "
992 "Ignoring inclusion."), from_utf8(incfile)));
996 string exppath = incfile;
997 if (!runparams.export_folder.empty()) {
998 exppath = makeAbsPath(exppath, runparams.export_folder).realPath();
999 FileName(exppath).onlyPath().createPath();
1002 // write it to a file (so far the complete file)
1003 string const exportfile = changeExtension(exppath, ".sgml");
1004 DocFileName writefile(changeExtension(included_file, ".sgml"));
1006 Buffer * tmp = loadIfNeeded();
1008 string const mangled = writefile.mangledFileName();
1009 writefile = makeAbsPath(mangled,
1010 buffer().masterBuffer()->temppath());
1011 if (!runparams.nice)
1014 LYXERR(Debug::LATEX, "incfile:" << incfile);
1015 LYXERR(Debug::LATEX, "exportfile:" << exportfile);
1016 LYXERR(Debug::LATEX, "writefile:" << writefile);
1018 tmp->makeDocBookFile(writefile, runparams, Buffer::OnlyBody);
1021 runparams.exportdata->addExternalFile("docbook", writefile,
1023 runparams.exportdata->addExternalFile("docbook-xml", writefile,
1026 if (isVerbatim(params()) || isListings(params())) {
1027 os << "<inlinegraphic fileref=\""
1028 << '&' << include_label << ';'
1029 << "\" format=\"linespecific\">";
1031 os << '&' << include_label << ';';
1037 void InsetInclude::validate(LaTeXFeatures & features) const
1039 LATTEST(&buffer() == &features.buffer());
1041 string incfile = to_utf8(params()["filename"]);
1042 string const included_file =
1043 includedFileName(buffer(), params()).absFileName();
1046 if (isLyXFileName(included_file))
1047 writefile = changeExtension(included_file, ".sgml");
1049 writefile = included_file;
1051 if (!features.runparams().nice && !isVerbatim(params()) && !isListings(params())) {
1052 incfile = DocFileName(writefile).mangledFileName();
1053 writefile = makeAbsPath(incfile,
1054 buffer().masterBuffer()->temppath()).absFileName();
1057 features.includeFile(include_label, writefile);
1059 features.useInsetLayout(getLayout());
1060 if (isVerbatim(params()))
1061 features.require("verbatim");
1062 else if (isListings(params())) {
1063 if (buffer().params().use_minted) {
1064 features.require("minted");
1065 string const opts = to_utf8(params()["lstparams"]);
1066 InsetListingsParams lstpars(opts);
1067 if (!lstpars.isFloat() && contains(opts, "caption="))
1068 features.require("lyxmintcaption");
1070 features.require("listings");
1073 // Here we must do the fun stuff...
1074 // Load the file in the include if it needs
1076 Buffer * const tmp = loadIfNeeded();
1078 // the file is loaded
1079 // make sure the buffer isn't us
1080 // FIXME RECURSIVE INCLUDES
1081 // This is not sufficient, as recursive includes could be
1082 // more than a file away. But it will do for now.
1083 if (tmp && tmp != &buffer()) {
1084 // We must temporarily change features.buffer,
1085 // otherwise it would always be the master buffer,
1086 // and nested includes would not work.
1087 features.setBuffer(*tmp);
1088 // Maybe this is already a child
1089 bool const is_child =
1090 features.runparams().is_child;
1091 features.runparams().is_child = true;
1092 tmp->validate(features);
1093 features.runparams().is_child = is_child;
1094 features.setBuffer(buffer());
1100 void InsetInclude::collectBibKeys(InsetIterator const & /*di*/, FileNameList & checkedFiles) const
1102 Buffer * child = loadIfNeeded();
1105 // FIXME RECURSIVE INCLUDE
1106 // This isn't sufficient, as the inclusion could be downstream.
1107 // But it'll have to do for now.
1108 if (child == &buffer())
1110 child->collectBibKeys(checkedFiles);
1114 void InsetInclude::metrics(MetricsInfo & mi, Dimension & dim) const
1116 LBUFERR(mi.base.bv);
1118 bool use_preview = false;
1119 if (RenderPreview::previewText()) {
1120 graphics::PreviewImage const * pimage =
1121 preview_->getPreviewImage(mi.base.bv->buffer());
1122 use_preview = pimage && pimage->image();
1126 preview_->metrics(mi, dim);
1130 button_.update(screenLabel(), true, false);
1132 button_.metrics(mi, dim);
1135 Box b(0, dim.wid, -dim.asc, dim.des);
1140 void InsetInclude::draw(PainterInfo & pi, int x, int y) const
1142 LBUFERR(pi.base.bv);
1144 bool use_preview = false;
1145 if (RenderPreview::previewText()) {
1146 graphics::PreviewImage const * pimage =
1147 preview_->getPreviewImage(pi.base.bv->buffer());
1148 use_preview = pimage && pimage->image();
1152 preview_->draw(pi, x, y);
1154 button_.draw(pi, x, y);
1158 void InsetInclude::write(ostream & os) const
1160 params().Write(os, &buffer());
1164 string InsetInclude::contextMenuName() const
1166 return "context-include";
1170 Inset::DisplayType InsetInclude::display() const
1172 return type(params()) == INPUT ? Inline : AlignCenter;
1176 docstring InsetInclude::layoutName() const
1178 if (isListings(params()))
1179 return from_ascii("IncludeListings");
1180 return InsetCommand::layoutName();
1188 void InsetInclude::fileChanged() const
1190 Buffer const * const buffer = updateFrontend();
1194 preview_->removePreview(*buffer);
1195 add_preview(*preview_, *this, *buffer);
1196 preview_->startLoading(*buffer);
1202 bool preview_wanted(InsetCommandParams const & params, Buffer const & buffer)
1204 FileName const included_file = includedFileName(buffer, params);
1206 return type(params) == INPUT && params.preview() &&
1207 included_file.isReadableFile();
1211 docstring latexString(InsetInclude const & inset)
1213 odocstringstream ods;
1215 // We don't need to set runparams.encoding since this will be done
1216 // by latex() anyway.
1217 OutputParams runparams(0);
1218 runparams.flavor = OutputParams::LATEX;
1219 runparams.for_preview = true;
1220 inset.latex(os, runparams);
1226 void add_preview(RenderMonitoredPreview & renderer, InsetInclude const & inset,
1227 Buffer const & buffer)
1229 InsetCommandParams const & params = inset.params();
1230 if (RenderPreview::previewText() && preview_wanted(params, buffer)) {
1231 renderer.setAbsFile(includedFileName(buffer, params));
1232 docstring const snippet = latexString(inset);
1233 renderer.addPreview(snippet, buffer);
1240 void InsetInclude::addPreview(DocIterator const & /*inset_pos*/,
1241 graphics::PreviewLoader & ploader) const
1243 Buffer const & buffer = ploader.buffer();
1244 if (!preview_wanted(params(), buffer))
1246 preview_->setAbsFile(includedFileName(buffer, params()));
1247 docstring const snippet = latexString(*this);
1248 preview_->addPreview(snippet, ploader);
1252 void InsetInclude::addToToc(DocIterator const & cpit, bool output_active,
1253 UpdateType utype, TocBackend & backend) const
1255 if (isListings(params())) {
1257 label_->addToToc(cpit, output_active, utype, backend);
1258 TocBuilder & b = backend.builder("listing");
1259 b.pushItem(cpit, screenLabel(), output_active);
1260 InsetListingsParams p(to_utf8(params()["lstparams"]));
1261 b.argumentItem(from_utf8(p.getParamValue("caption")));
1263 } else if (isVerbatim(params())) {
1264 TocBuilder & b = backend.builder("child");
1265 b.pushItem(cpit, screenLabel(), output_active);
1268 Buffer const * const childbuffer = getChildBuffer();
1270 TocBuilder & b = backend.builder("child");
1271 docstring str = childbuffer ? childbuffer->fileName().displayName()
1273 b.pushItem(cpit, str, output_active);
1279 // Update the child's tocBackend. The outliner uses the master's, but
1280 // the navigation menu uses the child's.
1281 childbuffer->tocBackend().update(output_active, utype);
1282 // Include Tocs from children
1283 childbuffer->inset().addToToc(DocIterator(), output_active, utype,
1285 //Copy missing outliner names (though the user has been warned against
1286 //having different document class and module selection between master
1288 for (pair<string, docstring> const & name
1289 : childbuffer->params().documentClass().outlinerNames())
1290 backend.addName(name.first, translateIfPossible(name.second));
1295 void InsetInclude::updateCommand()
1300 docstring old_label = label_->getParam("name");
1301 label_->updateLabel(old_label);
1302 // the label might have been adapted (duplicate)
1303 docstring new_label = label_->getParam("name");
1304 if (old_label == new_label)
1307 // update listings parameters...
1308 InsetCommandParams p(INCLUDE_CODE);
1310 InsetListingsParams par(to_utf8(params()["lstparams"]));
1311 par.addParam("label", "{" + to_utf8(new_label) + "}", true);
1312 p["lstparams"] = from_utf8(par.params());
1317 void InsetInclude::updateBuffer(ParIterator const & it, UpdateType utype)
1319 button_.update(screenLabel(), true, false);
1321 Buffer const * const childbuffer = getChildBuffer();
1323 childbuffer->updateBuffer(Buffer::UpdateChildOnly, utype);
1326 if (!isListings(params()))
1330 label_->updateBuffer(it, utype);
1332 InsetListingsParams const par(to_utf8(params()["lstparams"]));
1333 if (par.getParamValue("caption").empty()) {
1334 listings_label_ = buffer().B_("Program Listing");
1337 Buffer const & master = *buffer().masterBuffer();
1338 Counters & counters = master.params().documentClass().counters();
1339 docstring const cnt = from_ascii("listing");
1340 listings_label_ = master.B_("Program Listing");
1341 if (counters.hasCounter(cnt)) {
1342 counters.step(cnt, utype);
1343 listings_label_ += " " + convert<docstring>(counters.value(cnt));