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 Kimberly 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"
24 #include "ErrorList.h"
27 #include "FuncRequest.h"
28 #include "FuncStatus.h"
29 #include "LaTeXFeatures.h"
30 #include "LayoutModuleList.h"
32 #include "MetricsInfo.h"
33 #include "output_plaintext.h"
34 #include "output_xhtml.h"
35 #include "texstream.h"
36 #include "TextClass.h"
37 #include "TocBackend.h"
39 #include "frontends/alert.h"
40 #include "frontends/Painter.h"
42 #include "graphics/PreviewImage.h"
43 #include "graphics/PreviewLoader.h"
45 #include "insets/InsetLabel.h"
46 #include "insets/InsetListingsParams.h"
47 #include "insets/RenderPreview.h"
49 #include "mathed/MacroTable.h"
51 #include "support/convert.h"
52 #include "support/debug.h"
53 #include "support/docstream.h"
54 #include "support/FileName.h"
55 #include "support/FileNameList.h"
56 #include "support/filetools.h"
57 #include "support/gettext.h"
58 #include "support/lassert.h"
59 #include "support/lstrings.h" // contains
60 #include "support/mutex.h"
61 #include "support/ExceptionMessage.h"
64 using namespace lyx::support;
68 namespace Alert = frontend::Alert;
73 docstring const uniqueID()
75 static unsigned int seed = 1000;
77 Mutex::Locker lock(&mutex);
78 return "file" + convert<docstring>(++seed);
82 /// the type of inclusion
84 INCLUDE, VERB, INPUT, VERBAST, LISTINGS, NONE
88 Types type(string const & s)
92 if (s == "verbatiminput")
94 if (s == "verbatiminput*")
96 if (s == "lstinputlisting" || s == "inputminted")
104 Types type(InsetCommandParams const & params)
106 return type(params.getCmdName());
110 bool isListings(InsetCommandParams const & params)
112 return type(params) == LISTINGS;
116 bool isVerbatim(InsetCommandParams const & params)
118 Types const t = type(params);
119 return t == VERB || t == VERBAST;
123 bool isInputOrInclude(InsetCommandParams const & params)
125 Types const t = type(params);
126 return t == INPUT || t == INCLUDE;
130 FileName const masterFileName(Buffer const & buffer)
132 return buffer.masterBuffer()->fileName();
136 void add_preview(RenderMonitoredPreview &, InsetInclude const &, Buffer const &);
139 string const parentFileName(Buffer const & buffer)
141 return buffer.absFileName();
145 FileName const includedFileName(Buffer const & buffer,
146 InsetCommandParams const & params)
148 return makeAbsPath(ltrim(to_utf8(params["filename"])),
149 onlyPath(parentFileName(buffer)));
153 InsetLabel * createLabel(Buffer * buf, docstring const & label_str)
155 if (label_str.empty())
157 InsetCommandParams icp(LABEL_CODE);
158 icp["name"] = label_str;
159 return new InsetLabel(buf, icp);
163 char_type replaceCommaInBraces(docstring & params)
165 // Code point from private use area
166 char_type private_char = 0xE000;
168 for (char_type & c : params) {
173 else if (c == ',' && count)
182 InsetInclude::InsetInclude(Buffer * buf, InsetCommandParams const & p)
183 : InsetCommand(buf, p), include_label(uniqueID()),
184 preview_(make_unique<RenderMonitoredPreview>(this)), failedtoload_(false),
185 label_(nullptr), child_buffer_(nullptr), file_exist_(false),
186 recursion_error_(false)
188 preview_->connect([this](){ fileChanged(); });
190 if (isListings(params())) {
191 InsetListingsParams listing_params(to_utf8(p["lstparams"]));
192 label_ = createLabel(buffer_, from_utf8(listing_params.getParamValue("label")));
193 } else if (isInputOrInclude(params()) && buf)
198 InsetInclude::InsetInclude(InsetInclude const & other)
199 : InsetCommand(other), include_label(other.include_label),
200 preview_(make_unique<RenderMonitoredPreview>(this)), failedtoload_(false),
201 label_(nullptr), child_buffer_(nullptr),
202 file_exist_(other.file_exist_),recursion_error_(other.recursion_error_)
204 preview_->connect([this](){ fileChanged(); });
207 label_ = new InsetLabel(*other.label_);
211 InsetInclude::~InsetInclude()
217 void InsetInclude::setBuffer(Buffer & buffer)
219 InsetCommand::setBuffer(buffer);
221 label_->setBuffer(buffer);
225 void InsetInclude::setChildBuffer(Buffer * buffer)
227 child_buffer_ = buffer;
231 ParamInfo const & InsetInclude::findInfo(string const & /* cmdName */)
234 // This is only correct for the case of listings, but it'll do for now.
235 // In the other cases, this second parameter should just be empty.
236 static ParamInfo param_info_;
237 if (param_info_.empty()) {
238 param_info_.add("filename", ParamInfo::LATEX_REQUIRED);
239 param_info_.add("lstparams", ParamInfo::LATEX_OPTIONAL);
240 param_info_.add("literal", ParamInfo::LYX_INTERNAL);
246 bool InsetInclude::isCompatibleCommand(string const & s)
248 return type(s) != NONE;
252 bool InsetInclude::needsCProtection(bool const /*maintext*/, bool const fragile) const
254 // We need to \cprotect all types in fragile context
259 void InsetInclude::doDispatch(Cursor & cur, FuncRequest & cmd)
261 switch (cmd.action()) {
263 case LFUN_INSET_EDIT: {
264 editIncluded(ltrim(to_utf8(params()["filename"])));
268 case LFUN_INSET_MODIFY: {
269 // It should be OK just to invalidate the cache in setParams()
271 // child_buffer_ = 0;
272 InsetCommandParams p(INCLUDE_CODE);
273 if (cmd.getArg(0) == "changetype") {
275 InsetCommand::doDispatch(cur, cmd);
278 InsetCommand::string2params(to_utf8(cmd.argument()), p);
279 if (!p.getCmdName().empty()) {
281 InsetListingsParams new_params(to_utf8(p["lstparams"]));
282 docstring const new_label =
283 from_utf8(new_params.getParamValue("label"));
285 if (new_label.empty()) {
291 old_label = label_->getParam("name");
293 label_ = createLabel(buffer_, new_label);
294 label_->setBuffer(buffer());
297 if (new_label != old_label) {
298 label_->updateLabelAndRefs(new_label, &cur);
299 // the label might have been adapted (duplicate)
300 if (new_label != label_->getParam("name")) {
301 new_params.addParam("label", "{" +
302 to_utf8(label_->getParam("name")) + "}", true);
303 p["lstparams"] = from_utf8(new_params.params());
310 cur.forceBufferUpdate();
312 cur.noScreenUpdate();
316 case LFUN_MOUSE_RELEASE: {
317 if (cmd.modifier() == ControlModifier) {
318 FileName const incfile = includedFileName(buffer(), params());
319 string const & incname = incfile.absFileName();
320 editIncluded(incname);
326 //pass everything else up the chain
328 InsetCommand::doDispatch(cur, cmd);
334 void InsetInclude::editIncluded(string const & f)
336 if (isLyXFileName(f)) {
337 FuncRequest fr(LFUN_BUFFER_CHILD_OPEN, f);
340 // tex file or other text file in verbatim mode
341 theFormats().edit(buffer(),
342 support::makeAbsPath(f, support::onlyPath(buffer().absFileName())),
347 bool InsetInclude::getStatus(Cursor & cur, FuncRequest const & cmd,
348 FuncStatus & flag) const
350 switch (cmd.action()) {
352 case LFUN_INSET_EDIT:
353 flag.setEnabled(true);
356 case LFUN_INSET_MODIFY:
357 if (cmd.getArg(0) == "changetype")
358 return InsetCommand::getStatus(cur, cmd, flag);
360 flag.setEnabled(true);
364 return InsetCommand::getStatus(cur, cmd, flag);
369 void InsetInclude::setParams(InsetCommandParams const & p)
371 // invalidate the cache
372 child_buffer_ = nullptr;
374 // reset in order to allow loading new file
375 failedtoload_ = false;
376 recursion_error_ = false;
378 InsetCommand::setParams(p);
380 if (preview_->monitoring())
381 preview_->stopMonitoring();
383 if (type(params()) == INPUT)
384 add_preview(*preview_, *this, buffer());
388 bool InsetInclude::isChildIncluded() const
390 std::list<std::string> includeonlys =
391 buffer().params().getIncludedChildren();
392 if (includeonlys.empty())
394 return (std::find(includeonlys.begin(),
396 ltrim(to_utf8(params()["filename"]))) != includeonlys.end());
400 docstring InsetInclude::screenLabel() const
402 docstring pre = file_exist_ ? docstring() : _("MISSING:");
406 switch (type(params())) {
408 temp = buffer().B_("Input");
411 temp = buffer().B_("Verbatim");
414 temp = buffer().B_("Verbatim*");
417 if (isChildIncluded())
418 temp = buffer().B_("Include");
420 temp += buffer().B_("Include (excluded)");
423 temp = listings_label_;
426 LASSERT(false, temp = buffer().B_("Unknown"));
432 if (ltrim(params()["filename"]).empty())
435 temp += from_utf8(onlyFileName(ltrim(to_utf8(params()["filename"]))));
437 return pre.empty() ? temp : pre + from_ascii(" ") + temp;
441 Buffer * InsetInclude::loadIfNeeded() const
443 // This is for background export and preview. We don't even want to
444 // try to load the cloned child document again.
445 if (buffer().isClone())
446 return child_buffer_;
448 // Don't try to load it again if we failed before.
449 if (failedtoload_ || isVerbatim(params()) || isListings(params()))
452 FileName const included_file = includedFileName(buffer(), params());
453 // Use cached Buffer if possible.
454 if (child_buffer_ != nullptr) {
455 if (theBufferList().isLoaded(child_buffer_)
456 // additional sanity check: make sure the Buffer really is
457 // associated with the file we want.
458 && child_buffer_ == theBufferList().getBuffer(included_file))
459 return child_buffer_;
460 // Buffer vanished, so invalidate cache and try to reload.
461 child_buffer_ = nullptr;
464 if (!isLyXFileName(included_file.absFileName()))
467 Buffer * child = theBufferList().getBuffer(included_file);
468 if (checkForRecursiveInclude(child))
472 // the readonly flag can/will be wrong, not anymore I think.
473 if (!included_file.exists()) {
474 failedtoload_ = true;
478 child = theBufferList().newBuffer(included_file.absFileName());
480 // Buffer creation is not possible.
483 buffer().pushIncludedBuffer(child);
484 // Set parent before loading, such that macros can be tracked
485 child->setParent(&buffer());
487 if (child->loadLyXFile() != Buffer::ReadSuccess) {
488 failedtoload_ = true;
489 child->setParent(nullptr);
490 //close the buffer we just opened
491 theBufferList().release(child);
492 buffer().popIncludedBuffer();
496 buffer().popIncludedBuffer();
497 if (!child->errorList("Parse").empty()) {
498 // FIXME: Do something.
501 // The file was already loaded, so, simply
502 // inform parent buffer about local macros.
503 Buffer const * parent = &buffer();
504 child->setParent(parent);
506 child->listMacroNames(macros);
507 MacroNameSet::const_iterator cit = macros.begin();
508 MacroNameSet::const_iterator end = macros.end();
509 for (; cit != end; ++cit)
510 parent->usermacros.insert(*cit);
513 // Cache the child buffer.
514 child_buffer_ = child;
519 bool InsetInclude::checkForRecursiveInclude(
520 Buffer const * cbuf, bool silent) const
522 if (recursion_error_)
525 if (!buffer().isBufferIncluded(cbuf))
529 docstring const msg = _("The file\n%1$s\n has attempted to include itself.\n"
530 "The document set will not work properly until this is fixed!");
531 frontend::Alert::warning(_("Recursive Include"),
532 bformat(msg, from_utf8(cbuf->fileName().absFileName())));
534 recursion_error_ = true;
539 void InsetInclude::latex(otexstream & os, OutputParams const & runparams) const
541 string incfile = ltrim(to_utf8(params()["filename"]));
543 // Warn if no file name has been specified
544 if (incfile.empty()) {
545 frontend::Alert::warning(_("No file name specified"),
546 _("An included file name is empty.\n"
547 "Ignoring Inclusion"),
551 // Warn if file doesn't exist
552 if (!includedFileExist()) {
553 frontend::Alert::warning(_("Included file not found"),
554 bformat(_("The included file\n"
556 "has not been found. LyX will ignore the inclusion."),
562 FileName const included_file = includedFileName(buffer(), params());
563 Buffer const * const masterBuffer = buffer().masterBuffer();
565 if (runparams.inDeletedInset) {
566 // We cannot strike-out whole children,
567 // so we just output a note.
569 << bformat(buffer().B_("[INCLUDED FILE %1$s DELETED!]"),
570 from_utf8(included_file.onlyFileName()))
575 // if incfile is relative, make it relative to the master
577 if (!FileName::isAbsolute(incfile)) {
579 incfile = to_utf8(makeRelPath(from_utf8(included_file.absFileName()),
580 from_utf8(masterBuffer->filePath())));
583 string exppath = incfile;
584 if (!runparams.export_folder.empty()) {
585 exppath = makeAbsPath(exppath, runparams.export_folder).realPath();
588 // write it to a file (so far the complete file)
592 if (type(params()) == LISTINGS) {
593 exportfile = exppath;
594 mangled = DocFileName(included_file).mangledFileName();
596 exportfile = changeExtension(exppath, ".tex");
597 mangled = DocFileName(changeExtension(included_file.absFileName(), ".tex")).
603 else if (!runparams.silent)
604 ; // no warning wanted
605 else if (!isValidLaTeXFileName(incfile)) {
606 frontend::Alert::warning(_("Invalid filename"),
607 _("The following filename will cause troubles "
608 "when running the exported file through LaTeX: ") +
610 } else if (!isValidDVIFileName(incfile)) {
611 frontend::Alert::warning(_("Problematic filename for DVI"),
612 _("The following filename can cause troubles "
613 "when running the exported file through LaTeX "
614 "and opening the resulting DVI: ") +
615 from_utf8(incfile), true);
618 FileName const writefile(makeAbsPath(mangled, runparams.for_preview ?
619 buffer().temppath() : masterBuffer->temppath()));
621 LYXERR(Debug::LATEX, "incfile:" << incfile);
622 LYXERR(Debug::LATEX, "exportfile:" << exportfile);
623 LYXERR(Debug::LATEX, "writefile:" << writefile);
625 string const tex_format = flavor2format(runparams.flavor);
627 switch (type(params())) {
630 incfile = latex_path(incfile);
632 os << '\\' << from_ascii(params().getCmdName()) << '{'
633 << from_utf8(incfile) << '}';
637 runparams.exportdata->addExternalFile(tex_format, writefile,
640 // \input wants file with extension (default is .tex)
641 if (!isLyXFileName(included_file.absFileName())) {
642 incfile = latex_path(incfile);
644 os << '\\' << from_ascii(params().getCmdName())
645 << '{' << from_utf8(incfile) << '}';
647 incfile = changeExtension(incfile, ".tex");
648 incfile = latex_path(incfile);
650 os << '\\' << from_ascii(params().getCmdName())
651 << '{' << from_utf8(incfile) << '}';
656 // Here, listings and minted have slightly different behaviors.
657 // Using listings, it is always possible to have a caption,
658 // even for non-floats. Using minted, only floats can have a
659 // caption. So, with minted we use the following strategy.
660 // If a caption was specified but the float parameter was not,
661 // we ourselves add a caption above the listing (because the
662 // listing comes from a file and might span several pages).
663 // Otherwise, if float was specified, the floating listing
664 // environment provided by minted is used. In either case, the
665 // label parameter is taken as the label by which the float
666 // can be referenced, otherwise it will have the meaning
667 // intended by minted. In this last case, the label will
668 // serve as a sort of caption that, however, will be shown
669 // by minted only if the frame parameter is also specified.
670 bool const use_minted = buffer().params().use_minted;
671 runparams.exportdata->addExternalFile(tex_format, writefile,
673 string const opt = to_utf8(params()["lstparams"]);
674 // opt is set in QInclude dialog and should have passed validation.
675 InsetListingsParams lstparams(opt);
676 docstring parameters = from_utf8(lstparams.params());
681 bool isfloat = lstparams.isFloat();
682 // We are going to split parameters at commas, so
683 // replace commas that are not parameter separators
684 // with a code point from the private use area
685 char_type comma = replaceCommaInBraces(parameters);
686 // Get float placement, language, caption, and
687 // label, then remove the relative options if minted.
688 vector<docstring> opts =
689 getVectorFromString(parameters, from_ascii(","), false);
690 vector<docstring> latexed_opts;
691 for (size_t i = 0; i < opts.size(); ++i) {
692 // Restore replaced commas
693 opts[i] = subst(opts[i], comma, ',');
694 if (use_minted && prefixIs(opts[i], from_ascii("float"))) {
695 if (prefixIs(opts[i], from_ascii("float=")))
696 placement = opts[i].substr(6);
697 opts.erase(opts.begin() + i--);
698 } else if (use_minted && prefixIs(opts[i], from_ascii("language="))) {
699 language = opts[i].substr(9);
700 opts.erase(opts.begin() + i--);
701 } else if (prefixIs(opts[i], from_ascii("caption="))) {
702 caption = params().prepareCommand(runparams, trim(opts[i].substr(8), "{}"),
703 ParamInfo::HANDLING_LATEXIFY);
704 opts.erase(opts.begin() + i--);
706 latexed_opts.push_back(from_ascii("caption={") + caption + "}");
707 } else if (prefixIs(opts[i], from_ascii("label="))) {
708 label = params().prepareCommand(runparams, trim(opts[i].substr(6), "{}"),
709 ParamInfo::HANDLING_ESCAPE);
710 opts.erase(opts.begin() + i--);
712 latexed_opts.push_back(from_ascii("label={") + label + "}");
714 if (use_minted && !label.empty()) {
715 if (isfloat || !caption.empty())
716 label = trim(label, "{}");
718 opts.push_back(from_ascii("label=") + label);
721 if (!latexed_opts.empty())
722 opts.insert(opts.end(), latexed_opts.begin(), latexed_opts.end());
723 parameters = getStringFromVector(opts, from_ascii(","));
724 if (language.empty())
725 language = from_ascii("TeX");
726 if (use_minted && isfloat) {
727 os << breakln << "\\begin{listing}";
728 if (!placement.empty())
729 os << '[' << placement << "]";
731 } else if (use_minted && !caption.empty()) {
732 os << breakln << "\\lyxmintcaption[t]{" << caption;
734 os << "\\label{" << label << "}";
737 os << (use_minted ? "\\inputminted" : "\\lstinputlisting");
738 if (!parameters.empty())
739 os << "[" << parameters << "]";
741 os << '{' << ascii_lowercase(language) << '}';
742 os << '{' << incfile << '}';
743 if (use_minted && isfloat) {
744 if (!caption.empty())
745 os << breakln << "\\caption{" << caption << "}";
747 os << breakln << "\\label{" << label << "}";
748 os << breakln << "\\end{listing}\n";
753 runparams.exportdata->addExternalFile(tex_format, writefile,
756 // \include don't want extension and demands that the
757 // file really have .tex
758 incfile = changeExtension(incfile, string());
759 incfile = latex_path(incfile);
761 os << '\\' << from_ascii(params().getCmdName()) << '{'
762 << from_utf8(incfile) << '}';
769 if (runparams.inComment || runparams.dryrun)
770 // Don't try to load or copy the file if we're
771 // in a comment or doing a dryrun
774 if (!isInputOrInclude(params()) ||
775 !isLyXFileName(included_file.absFileName())) {
776 // In this case, it's not a LyX file, so we copy the file
777 // to the temp dir, so that .aux files etc. are not created
778 // in the original dir. Files included by this file will be
779 // found via either the environment variable TEXINPUTS, or
780 // input@path, see ../Buffer.cpp.
781 unsigned long const checksum_in = included_file.checksum();
782 unsigned long const checksum_out = writefile.checksum();
783 if (checksum_in != checksum_out) {
784 if (!included_file.copyTo(writefile)) {
787 to_utf8(bformat(_("Could not copy the file\n%1$s\n"
788 "into the temporary directory."),
789 from_utf8(included_file.absFileName()))));
796 // it's a LyX file and we're inputting or including, so
797 // try to load it so we can write the associated latex
798 Buffer * tmp = loadIfNeeded();
800 if (!runparams.silent) {
801 docstring text = bformat(_("Could not load included "
803 "Please, check whether it actually exists."),
804 included_file.displayName());
805 throw ExceptionMessage(ErrorException, _("Error: "),
811 if (recursion_error_)
814 if (!runparams.silent) {
815 if (tmp->params().baseClass() != masterBuffer->params().baseClass()) {
817 docstring text = bformat(_("Included file `%1$s'\n"
818 "has textclass `%2$s'\n"
819 "while parent file has textclass `%3$s'."),
820 included_file.displayName(),
821 from_utf8(tmp->params().documentClass().name()),
822 from_utf8(masterBuffer->params().documentClass().name()));
823 Alert::warning(_("Different textclasses"), text, true);
826 string const child_tf = tmp->params().useNonTeXFonts ? "true" : "false";
827 string const master_tf = masterBuffer->params().useNonTeXFonts ? "true" : "false";
828 if (tmp->params().useNonTeXFonts != masterBuffer->params().useNonTeXFonts) {
829 docstring text = bformat(_("Included file `%1$s'\n"
830 "has use-non-TeX-fonts set to `%2$s'\n"
831 "while parent file has use-non-TeX-fonts set to `%3$s'."),
832 included_file.displayName(),
834 from_utf8(master_tf));
835 Alert::warning(_("Different use-non-TeX-fonts settings"), text, true);
837 else if (tmp->params().inputenc != masterBuffer->params().inputenc) {
838 docstring text = bformat(_("Included file `%1$s'\n"
839 "uses input encoding \"%2$s\" [%3$s]\n"
840 "while parent file uses input encoding \"%4$s\" [%5$s]."),
841 included_file.displayName(),
842 _(tmp->params().inputenc),
843 from_utf8(tmp->params().encoding().guiName()),
844 _(masterBuffer->params().inputenc),
845 from_utf8(masterBuffer->params().encoding().guiName()));
846 Alert::warning(_("Different LaTeX input encodings"), text, true);
849 // Make sure modules used in child are all included in master
850 // FIXME It might be worth loading the children's modules into the master
851 // over in BufferParams rather than doing this check.
852 LayoutModuleList const masterModules = masterBuffer->params().getModules();
853 LayoutModuleList const childModules = tmp->params().getModules();
854 LayoutModuleList::const_iterator it = childModules.begin();
855 LayoutModuleList::const_iterator end = childModules.end();
856 for (; it != end; ++it) {
857 string const module = *it;
858 LayoutModuleList::const_iterator found =
859 find(masterModules.begin(), masterModules.end(), module);
860 if (found == masterModules.end()) {
861 docstring text = bformat(_("Included file `%1$s'\n"
862 "uses module `%2$s'\n"
863 "which is not used in parent file."),
864 included_file.displayName(), from_utf8(module));
865 Alert::warning(_("Module not found"), text, true);
870 tmp->markDepClean(masterBuffer->temppath());
872 // Don't assume the child's format is latex
873 string const inc_format = tmp->params().bufferFormat();
874 FileName const tmpwritefile(changeExtension(writefile.absFileName(),
875 theFormats().extension(inc_format)));
877 // FIXME: handle non existing files
878 // The included file might be written in a different encoding
880 Encoding const * const oldEnc = runparams.encoding;
881 Language const * const oldLang = runparams.master_language;
882 // If the master uses non-TeX fonts (XeTeX, LuaTeX),
883 // the children must be encoded in plain utf8!
884 if (masterBuffer->params().useNonTeXFonts)
885 runparams.encoding = encodings.fromLyXName("utf8-plain");
887 runparams.encoding = oldEnc;
888 else runparams.encoding = &tmp->params().encoding();
889 runparams.master_language = buffer().params().language;
890 runparams.par_begin = 0;
891 runparams.par_end = tmp->paragraphs().size();
892 runparams.is_child = true;
893 Buffer::ExportStatus retval =
894 tmp->makeLaTeXFile(tmpwritefile, masterFileName(buffer()).
895 onlyPath().absFileName(), runparams, Buffer::OnlyBody);
896 if (retval == Buffer::ExportKilled && buffer().isClone() &&
897 buffer().isExporting()) {
898 // We really shouldn't get here, I don't think.
899 LYXERR0("No conversion exception?");
900 throw ConversionException();
902 else if (retval != Buffer::ExportSuccess) {
903 if (!runparams.silent) {
904 docstring msg = bformat(_("Included file `%1$s' "
905 "was not exported correctly.\n "
906 "LaTeX export is probably incomplete."),
907 included_file.displayName());
908 ErrorList const & el = tmp->errorList("Export");
910 msg = bformat(from_ascii("%1$s\n\n%2$s\n\n%3$s"),
911 msg, el.begin()->error, el.begin()->description);
912 throw ExceptionMessage(ErrorException, _("Error: "), msg);
915 runparams.encoding = oldEnc;
916 runparams.master_language = oldLang;
917 runparams.is_child = false;
919 // If needed, use converters to produce a latex file from the child
920 if (tmpwritefile != writefile) {
922 Converters::RetVal const conv_retval =
923 theConverters().convert(tmp, tmpwritefile, writefile,
924 included_file, inc_format, tex_format, el);
925 if (conv_retval == Converters::KILLED && buffer().isClone() &&
926 buffer().isExporting()) {
927 // We really shouldn't get here, I don't think.
928 LYXERR0("No conversion exception?");
929 throw ConversionException();
930 } else if (conv_retval != Converters::SUCCESS && !runparams.silent) {
931 docstring msg = bformat(_("Included file `%1$s' "
932 "was not exported correctly.\n "
933 "LaTeX export is probably incomplete."),
934 included_file.displayName());
936 msg = bformat(from_ascii("%1$s\n\n%2$s\n\n%3$s"),
937 msg, el.begin()->error, el.begin()->description);
938 throw ExceptionMessage(ErrorException, _("Error: "), msg);
944 docstring InsetInclude::xhtml(XMLStream & xs, OutputParams const & rp) const
949 // For verbatim and listings, we just include the contents of the file as-is.
950 // In the case of listings, we wrap it in <pre>.
951 bool const listing = isListings(params());
952 if (listing || isVerbatim(params())) {
954 xs << xml::StartTag("pre");
955 // FIXME: We don't know the encoding of the file, default to UTF-8.
956 xs << includedFileName(buffer(), params()).fileContents("UTF-8");
958 xs << xml::EndTag("pre");
962 // We don't (yet) know how to Input or Include non-LyX files.
963 // (If we wanted to get really arcane, we could run some tex2html
964 // converter on the included file. But that's just masochistic.)
965 FileName const included_file = includedFileName(buffer(), params());
966 if (!isLyXFileName(included_file.absFileName())) {
968 frontend::Alert::warning(_("Unsupported Inclusion"),
969 bformat(_("LyX does not know how to include non-LyX files when "
970 "generating HTML output. Offending file:\n%1$s"),
971 ltrim(params()["filename"])));
975 // In the other cases, we will generate the HTML and include it.
977 Buffer const * const ibuf = loadIfNeeded();
981 if (recursion_error_)
984 // are we generating only some paragraphs, or all of them?
985 bool const all_pars = !rp.dryrun ||
986 (rp.par_begin == 0 &&
987 rp.par_end == (int)buffer().text().paragraphs().size());
989 OutputParams op = rp;
993 ibuf->writeLyXHTMLSource(xs.os(), op, Buffer::IncludedFile);
995 xs << XMLStream::ESCAPE_NONE << "<!-- Included file: ";
996 xs << from_utf8(included_file.absFileName());
997 xs << XMLStream::ESCAPE_NONE << " -->";
1004 int InsetInclude::plaintext(odocstringstream & os,
1005 OutputParams const & op, size_t) const
1007 // just write the filename if we're making a tooltip or toc entry,
1008 // or are generating this for advanced search
1009 if (op.for_tooltip || op.for_toc || op.for_searchAdv != OutputParams::NoSearch) {
1010 os << '[' << screenLabel() << '\n'
1011 << ltrim(getParam("filename")) << "\n]";
1012 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
1015 if (isVerbatim(params()) || isListings(params())) {
1016 if (op.for_searchAdv != OutputParams::NoSearch) {
1017 os << '[' << screenLabel() << ']';
1020 os << '[' << screenLabel() << '\n'
1021 // FIXME: We don't know the encoding of the file, default to UTF-8.
1022 << includedFileName(buffer(), params()).fileContents("UTF-8")
1025 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
1028 Buffer const * const ibuf = loadIfNeeded();
1030 docstring const str = '[' + screenLabel() + ']';
1035 if (recursion_error_)
1038 writePlaintextFile(*ibuf, os, op);
1043 void InsetInclude::docbook(XMLStream & xs, OutputParams const & rp) const
1048 // For verbatim and listings, we just include the contents of the file as-is.
1049 bool const verbatim = isVerbatim(params());
1050 bool const listing = isListings(params());
1051 if (listing || verbatim) {
1053 xs << xml::StartTag("programlisting");
1055 xs << xml::StartTag("literallayout");
1057 // FIXME: We don't know the encoding of the file, default to UTF-8.
1058 xs << includedFileName(buffer(), params()).fileContents("UTF-8");
1061 xs << xml::EndTag("programlisting");
1063 xs << xml::EndTag("literallayout");
1068 // We don't know how to input or include non-LyX files. Input it as a comment.
1069 FileName const included_file = includedFileName(buffer(), params());
1070 if (!isLyXFileName(included_file.absFileName())) {
1072 frontend::Alert::warning(_("Unsupported Inclusion"),
1073 bformat(_("LyX does not know how to process included non-LyX files when "
1074 "generating DocBook output. The content of the file will be output as a "
1075 "comment. Offending file:\n%1$s"),
1076 ltrim(params()["filename"])));
1078 // Read the file, output it wrapped into comments.
1079 xs << XMLStream::ESCAPE_NONE << "<!-- Included file: ";
1080 xs << from_utf8(included_file.absFileName());
1081 xs << XMLStream::ESCAPE_NONE << " -->";
1083 xs << XMLStream::ESCAPE_NONE << "<!-- ";
1084 xs << included_file.fileContents("UTF-8");
1085 xs << XMLStream::ESCAPE_NONE << " -->";
1087 xs << XMLStream::ESCAPE_NONE << "<!-- End of included file: ";
1088 xs << from_utf8(included_file.absFileName());
1089 xs << XMLStream::ESCAPE_NONE << " -->";
1092 // In the other cases, we generate the DocBook version and include it.
1093 Buffer const * const ibuf = loadIfNeeded();
1097 if (recursion_error_)
1100 // are we generating only some paragraphs, or all of them?
1101 bool const all_pars = !rp.dryrun ||
1102 (rp.par_begin == 0 &&
1103 rp.par_end == (int) buffer().text().paragraphs().size());
1105 OutputParams op = rp;
1109 op.inInclude = true;
1110 op.docbook_in_par = false;
1111 ibuf->writeDocBookSource(xs.os(), op, Buffer::IncludedFile);
1113 xs << XMLStream::ESCAPE_NONE << "<!-- Included file: ";
1114 xs << from_utf8(included_file.absFileName());
1115 xs << XMLStream::ESCAPE_NONE << " -->";
1120 void InsetInclude::validate(LaTeXFeatures & features) const
1122 LATTEST(&buffer() == &features.buffer());
1124 string incfile = ltrim(to_utf8(params()["filename"]));
1125 string const included_file =
1126 includedFileName(buffer(), params()).absFileName();
1129 if (isLyXFileName(included_file))
1130 writefile = changeExtension(included_file, ".sgml");
1132 writefile = included_file;
1134 if (!features.runparams().nice && !isVerbatim(params()) && !isListings(params())) {
1135 incfile = DocFileName(writefile).mangledFileName();
1136 writefile = makeAbsPath(incfile,
1137 buffer().masterBuffer()->temppath()).absFileName();
1140 features.includeFile(include_label, writefile);
1142 features.useInsetLayout(getLayout());
1143 if (isVerbatim(params()))
1144 features.require("verbatim");
1145 else if (isListings(params())) {
1146 if (buffer().params().use_minted) {
1147 features.require("minted");
1148 string const opts = to_utf8(params()["lstparams"]);
1149 InsetListingsParams lstpars(opts);
1150 if (!lstpars.isFloat() && contains(opts, "caption="))
1151 features.require("lyxmintcaption");
1153 features.require("listings");
1156 // Here we must do the fun stuff...
1157 // Load the file in the include if it needs
1159 Buffer * const tmp = loadIfNeeded();
1163 // the file is loaded
1164 if (checkForRecursiveInclude(tmp))
1166 buffer().pushIncludedBuffer(tmp);
1168 // We must temporarily change features.buffer,
1169 // otherwise it would always be the master buffer,
1170 // and nested includes would not work.
1171 features.setBuffer(*tmp);
1172 // Maybe this is already a child
1173 bool const is_child =
1174 features.runparams().is_child;
1175 features.runparams().is_child = true;
1176 tmp->validate(features);
1177 features.runparams().is_child = is_child;
1178 features.setBuffer(buffer());
1180 buffer().popIncludedBuffer();
1184 void InsetInclude::collectBibKeys(InsetIterator const & /*di*/, FileNameList & checkedFiles) const
1186 Buffer * ibuf = loadIfNeeded();
1190 if (checkForRecursiveInclude(ibuf))
1192 buffer().pushIncludedBuffer(ibuf);
1193 ibuf->collectBibKeys(checkedFiles);
1194 buffer().popIncludedBuffer();
1198 bool InsetInclude::inheritFont() const
1200 return !isVerbatim(params());
1204 void InsetInclude::metrics(MetricsInfo & mi, Dimension & dim) const
1206 LBUFERR(mi.base.bv);
1208 bool use_preview = false;
1209 if (RenderPreview::previewText()) {
1210 graphics::PreviewImage const * pimage =
1211 preview_->getPreviewImage(mi.base.bv->buffer());
1212 use_preview = pimage && pimage->image();
1216 preview_->metrics(mi, dim);
1218 setBroken(!file_exist_ || recursion_error_);
1219 InsetCommand::metrics(mi, dim);
1224 void InsetInclude::draw(PainterInfo & pi, int x, int y) const
1226 LBUFERR(pi.base.bv);
1228 bool use_preview = false;
1229 if (RenderPreview::previewText()) {
1230 graphics::PreviewImage const * pimage =
1231 preview_->getPreviewImage(pi.base.bv->buffer());
1232 use_preview = pimage && pimage->image();
1236 preview_->draw(pi, x, y);
1238 InsetCommand::draw(pi, x, y);
1242 void InsetInclude::write(ostream & os) const
1244 params().Write(os, &buffer());
1248 string InsetInclude::contextMenuName() const
1250 return "context-include";
1254 Inset::RowFlags InsetInclude::rowFlags() const
1256 return type(params()) == INPUT ? Inline : Display;
1260 docstring InsetInclude::layoutName() const
1262 if (isListings(params()))
1263 return from_ascii("IncludeListings");
1264 return InsetCommand::layoutName();
1272 void InsetInclude::fileChanged() const
1274 Buffer const * const buffer = updateFrontend();
1278 preview_->removePreview(*buffer);
1279 add_preview(*preview_, *this, *buffer);
1280 preview_->startLoading(*buffer);
1286 bool preview_wanted(InsetCommandParams const & params, Buffer const & buffer)
1288 FileName const included_file = includedFileName(buffer, params);
1290 return type(params) == INPUT && params.preview() &&
1291 included_file.isReadableFile();
1295 docstring latexString(InsetInclude const & inset)
1297 odocstringstream ods;
1299 // We don't need to set runparams.encoding since this will be done
1300 // by latex() anyway.
1301 OutputParams runparams(nullptr);
1302 runparams.flavor = Flavor::LaTeX;
1303 runparams.for_preview = true;
1304 inset.latex(os, runparams);
1310 void add_preview(RenderMonitoredPreview & renderer, InsetInclude const & inset,
1311 Buffer const & buffer)
1313 InsetCommandParams const & params = inset.params();
1314 if (RenderPreview::previewText() && preview_wanted(params, buffer)) {
1315 renderer.setAbsFile(includedFileName(buffer, params));
1318 // InsetInclude::latex() throws if generation of LaTeX
1319 // fails, e.g. if lyx2lyx fails because file is too
1320 // new, or knitr fails.
1321 snippet = latexString(inset);
1323 // remove current preview because it is likely
1324 // associated with the previous included file name
1325 renderer.removePreview(buffer);
1326 LYXERR0("Preview of include failed.");
1329 renderer.addPreview(snippet, buffer);
1336 void InsetInclude::addPreview(DocIterator const & /*inset_pos*/,
1337 graphics::PreviewLoader & ploader) const
1339 Buffer const & buffer = ploader.buffer();
1340 if (!preview_wanted(params(), buffer))
1342 preview_->setAbsFile(includedFileName(buffer, params()));
1343 docstring const snippet = latexString(*this);
1344 preview_->addPreview(snippet, ploader);
1348 void InsetInclude::addToToc(DocIterator const & cpit, bool output_active,
1349 UpdateType utype, TocBackend & backend) const
1351 if (isListings(params())) {
1353 label_->addToToc(cpit, output_active, utype, backend);
1354 TocBuilder & b = backend.builder("listing");
1355 b.pushItem(cpit, screenLabel(), output_active);
1356 InsetListingsParams p(to_utf8(params()["lstparams"]));
1357 b.argumentItem(from_utf8(p.getParamValue("caption")));
1361 if (isVerbatim(params())) {
1362 TocBuilder & b = backend.builder("child");
1363 b.pushItem(cpit, screenLabel(), output_active);
1368 Buffer const * const childbuffer = loadIfNeeded();
1370 TocBuilder & b = backend.builder("child");
1371 string const fname = ltrim(to_utf8(params()["filename"]));
1372 // mark non-existent file with MISSING
1373 docstring const str = (file_exist_ ? from_ascii("") : _("MISSING: "))
1374 + from_utf8(onlyFileName(fname)) + " (" + from_utf8(fname) + ")";
1375 b.pushItem(cpit, str, output_active);
1381 if (checkForRecursiveInclude(childbuffer))
1383 buffer().pushIncludedBuffer(childbuffer);
1384 // Update the child's tocBackend. The outliner uses the master's, but
1385 // the navigation menu uses the child's.
1386 childbuffer->tocBackend().update(output_active, utype);
1387 // Include Tocs from children
1388 childbuffer->inset().addToToc(DocIterator(), output_active, utype,
1390 buffer().popIncludedBuffer();
1391 // Copy missing outliner names (though the user has been warned against
1392 // having different document class and module selection between master
1394 for (auto const & name
1395 : childbuffer->params().documentClass().outlinerNames())
1396 backend.addName(name.first, translateIfPossible(name.second));
1400 void InsetInclude::updateCommand()
1405 docstring old_label = label_->getParam("name");
1406 label_->updateLabel(old_label);
1407 // the label might have been adapted (duplicate)
1408 docstring new_label = label_->getParam("name");
1409 if (old_label == new_label)
1412 // update listings parameters...
1413 InsetCommandParams p(INCLUDE_CODE);
1415 InsetListingsParams par(to_utf8(params()["lstparams"]));
1416 par.addParam("label", "{" + to_utf8(new_label) + "}", true);
1417 p["lstparams"] = from_utf8(par.params());
1422 void InsetInclude::updateBuffer(ParIterator const & it, UpdateType utype, bool const deleted)
1424 file_exist_ = includedFileExist();
1425 Buffer const * const childbuffer = loadIfNeeded();
1427 if (!checkForRecursiveInclude(childbuffer))
1428 childbuffer->updateBuffer(Buffer::UpdateChildOnly, utype);
1432 if (!isListings(params()))
1436 label_->updateBuffer(it, utype, deleted);
1438 InsetListingsParams const par(to_utf8(params()["lstparams"]));
1439 if (par.getParamValue("caption").empty()) {
1440 listings_label_ = buffer().B_("Program Listing");
1443 Buffer const & master = *buffer().masterBuffer();
1444 Counters & counters = master.params().documentClass().counters();
1445 docstring const cnt = from_ascii("listing");
1446 listings_label_ = master.B_("Program Listing");
1447 if (counters.hasCounter(cnt)) {
1448 counters.step(cnt, utype);
1449 listings_label_ += " " + convert<docstring>(counters.value(cnt));
1454 bool InsetInclude::includedFileExist() const
1456 // check whether the included file exist
1457 string incFileName = ltrim(to_utf8(params()["filename"]));
1459 support::makeAbsPath(incFileName,
1460 support::onlyPath(buffer().absFileName()));