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/FileName.h"
59 #include "support/FileNameList.h"
60 #include "support/filetools.h"
61 #include "support/gettext.h"
62 #include "support/lassert.h"
63 #include "support/lstrings.h" // contains
64 #include "support/lyxalgo.h"
65 #include "support/mutex.h"
66 #include "support/ExceptionMessage.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" || s == "inputminted")
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(ltrim(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);
168 char_type replaceCommaInBraces(docstring & params)
170 // Code point from private use area
171 char_type private_char = 0xE000;
173 for (char_type & c : params) {
178 else if (c == ',' && count)
187 InsetInclude::InsetInclude(Buffer * buf, InsetCommandParams const & p)
188 : InsetCommand(buf, p), include_label(uniqueID()),
189 preview_(make_unique<RenderMonitoredPreview>(this)), failedtoload_(false),
190 set_label_(false), label_(nullptr), child_buffer_(nullptr), file_exist_(false),
191 recursion_error_(false)
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_(nullptr), child_buffer_(nullptr),
207 file_exist_(other.file_exist_),recursion_error_(other.recursion_error_)
209 preview_->connect([=](){ fileChanged(); });
212 label_ = new InsetLabel(*other.label_);
216 InsetInclude::~InsetInclude()
222 void InsetInclude::setBuffer(Buffer & buffer)
224 InsetCommand::setBuffer(buffer);
226 label_->setBuffer(buffer);
230 void InsetInclude::setChildBuffer(Buffer * buffer)
232 child_buffer_ = buffer;
236 ParamInfo const & InsetInclude::findInfo(string const & /* cmdName */)
239 // This is only correct for the case of listings, but it'll do for now.
240 // In the other cases, this second parameter should just be empty.
241 static ParamInfo param_info_;
242 if (param_info_.empty()) {
243 param_info_.add("filename", ParamInfo::LATEX_REQUIRED);
244 param_info_.add("lstparams", ParamInfo::LATEX_OPTIONAL);
245 param_info_.add("literal", ParamInfo::LYX_INTERNAL);
251 bool InsetInclude::isCompatibleCommand(string const & s)
253 return type(s) != NONE;
257 bool InsetInclude::needsCProtection(bool const /*maintext*/, bool const fragile) const
259 // We need to \cprotect all types in fragile context
264 void InsetInclude::doDispatch(Cursor & cur, FuncRequest & cmd)
266 switch (cmd.action()) {
268 case LFUN_INSET_EDIT: {
269 editIncluded(ltrim(to_utf8(params()["filename"])));
273 case LFUN_INSET_MODIFY: {
274 // It should be OK just to invalidate the cache in setParams()
276 // child_buffer_ = 0;
277 InsetCommandParams p(INCLUDE_CODE);
278 if (cmd.getArg(0) == "changetype") {
280 InsetCommand::doDispatch(cur, cmd);
283 InsetCommand::string2params(to_utf8(cmd.argument()), p);
284 if (!p.getCmdName().empty()) {
286 InsetListingsParams new_params(to_utf8(p["lstparams"]));
287 docstring const new_label =
288 from_utf8(new_params.getParamValue("label"));
290 if (new_label.empty()) {
296 old_label = label_->getParam("name");
298 label_ = createLabel(buffer_, new_label);
299 label_->setBuffer(buffer());
302 if (new_label != old_label) {
303 label_->updateLabelAndRefs(new_label, &cur);
304 // the label might have been adapted (duplicate)
305 if (new_label != label_->getParam("name")) {
306 new_params.addParam("label", "{" +
307 to_utf8(label_->getParam("name")) + "}", true);
308 p["lstparams"] = from_utf8(new_params.params());
315 cur.forceBufferUpdate();
317 cur.noScreenUpdate();
321 case LFUN_MOUSE_RELEASE: {
322 if (cmd.modifier() == ControlModifier) {
323 FileName const incfile = includedFileName(buffer(), params());
324 string const & incname = incfile.absFileName();
325 editIncluded(incname);
331 //pass everything else up the chain
333 InsetCommand::doDispatch(cur, cmd);
339 void InsetInclude::editIncluded(string const & f)
341 if (isLyXFileName(f)) {
342 FuncRequest fr(LFUN_BUFFER_CHILD_OPEN, f);
345 // tex file or other text file in verbatim mode
346 theFormats().edit(buffer(),
347 support::makeAbsPath(f, support::onlyPath(buffer().absFileName())),
352 bool InsetInclude::getStatus(Cursor & cur, FuncRequest const & cmd,
353 FuncStatus & flag) const
355 switch (cmd.action()) {
357 case LFUN_INSET_EDIT:
358 flag.setEnabled(true);
361 case LFUN_INSET_MODIFY:
362 if (cmd.getArg(0) == "changetype")
363 return InsetCommand::getStatus(cur, cmd, flag);
365 flag.setEnabled(true);
369 return InsetCommand::getStatus(cur, cmd, flag);
374 void InsetInclude::setParams(InsetCommandParams const & p)
376 // invalidate the cache
377 child_buffer_ = nullptr;
379 // reset in order to allow loading new file
380 failedtoload_ = false;
381 recursion_error_ = false;
383 InsetCommand::setParams(p);
386 if (preview_->monitoring())
387 preview_->stopMonitoring();
389 if (type(params()) == INPUT)
390 add_preview(*preview_, *this, buffer());
394 bool InsetInclude::isChildIncluded() const
396 std::list<std::string> includeonlys =
397 buffer().params().getIncludedChildren();
398 if (includeonlys.empty())
400 return (std::find(includeonlys.begin(),
402 ltrim(to_utf8(params()["filename"]))) != includeonlys.end());
406 docstring InsetInclude::screenLabel() const
408 docstring pre = file_exist_ ? docstring() : _("FILE MISSING:");
412 switch (type(params())) {
414 temp = buffer().B_("Input");
417 temp = buffer().B_("Verbatim");
420 temp = buffer().B_("Verbatim*");
423 if (isChildIncluded())
424 temp = buffer().B_("Include");
426 temp += buffer().B_("Include (excluded)");
429 temp = listings_label_;
432 LASSERT(false, temp = buffer().B_("Unknown"));
438 if (ltrim(params()["filename"]).empty())
441 temp += from_utf8(onlyFileName(ltrim(to_utf8(params()["filename"]))));
443 return pre.empty() ? temp : pre + from_ascii(" ") + temp;
447 Buffer * InsetInclude::loadIfNeeded() const
449 // This is for background export and preview. We don't even want to
450 // try to load the cloned child document again.
451 if (buffer().isClone())
452 return child_buffer_;
454 // Don't try to load it again if we failed before.
455 if (failedtoload_ || isVerbatim(params()) || isListings(params()))
458 FileName const included_file = includedFileName(buffer(), params());
459 // Use cached Buffer if possible.
460 if (child_buffer_ != nullptr) {
461 if (theBufferList().isLoaded(child_buffer_)
462 // additional sanity check: make sure the Buffer really is
463 // associated with the file we want.
464 && child_buffer_ == theBufferList().getBuffer(included_file))
465 return child_buffer_;
466 // Buffer vanished, so invalidate cache and try to reload.
467 child_buffer_ = nullptr;
470 if (!isLyXFileName(included_file.absFileName()))
473 Buffer * child = theBufferList().getBuffer(included_file);
474 if (checkForRecursiveInclude(child))
478 // the readonly flag can/will be wrong, not anymore I think.
479 if (!included_file.exists()) {
480 failedtoload_ = true;
484 child = theBufferList().newBuffer(included_file.absFileName());
486 // Buffer creation is not possible.
489 buffer().pushIncludedBuffer(child);
490 // Set parent before loading, such that macros can be tracked
491 child->setParent(&buffer());
493 if (child->loadLyXFile() != Buffer::ReadSuccess) {
494 failedtoload_ = true;
495 child->setParent(nullptr);
496 //close the buffer we just opened
497 theBufferList().release(child);
498 buffer().popIncludedBuffer();
502 buffer().popIncludedBuffer();
503 if (!child->errorList("Parse").empty()) {
504 // FIXME: Do something.
507 // The file was already loaded, so, simply
508 // inform parent buffer about local macros.
509 Buffer const * parent = &buffer();
510 child->setParent(parent);
512 child->listMacroNames(macros);
513 MacroNameSet::const_iterator cit = macros.begin();
514 MacroNameSet::const_iterator end = macros.end();
515 for (; cit != end; ++cit)
516 parent->usermacros.insert(*cit);
519 // Cache the child buffer.
520 child_buffer_ = child;
525 bool InsetInclude::checkForRecursiveInclude(
526 Buffer const * cbuf, bool silent) const
528 if (recursion_error_)
531 if (!buffer().isBufferIncluded(cbuf))
535 docstring const msg = _("The file\n%1$s\n has attempted to include itself.\n"
536 "The document set will not work properly until this is fixed!");
537 frontend::Alert::warning(_("Recursive Include"),
538 bformat(msg, from_utf8(cbuf->fileName().absFileName())));
540 recursion_error_ = true;
545 void InsetInclude::latex(otexstream & os, OutputParams const & runparams) const
547 string incfile = ltrim(to_utf8(params()["filename"]));
549 // Warn if no file name has been specified
550 if (incfile.empty()) {
551 frontend::Alert::warning(_("No file name specified"),
552 _("An included file name is empty.\n"
553 "Ignoring Inclusion"),
557 // Warn if file doesn't exist
558 if (!includedFileExist()) {
559 frontend::Alert::warning(_("Included file not found"),
560 bformat(_("The included file\n"
562 "has not been found. LyX will ignore the inclusion."),
568 FileName const included_file = includedFileName(buffer(), params());
569 Buffer const * const masterBuffer = buffer().masterBuffer();
571 // if incfile is relative, make it relative to the master
573 if (!FileName::isAbsolute(incfile)) {
575 incfile = to_utf8(makeRelPath(from_utf8(included_file.absFileName()),
576 from_utf8(masterBuffer->filePath())));
579 string exppath = incfile;
580 if (!runparams.export_folder.empty()) {
581 exppath = makeAbsPath(exppath, runparams.export_folder).realPath();
584 // write it to a file (so far the complete file)
588 if (type(params()) == LISTINGS) {
589 exportfile = exppath;
590 mangled = DocFileName(included_file).mangledFileName();
592 exportfile = changeExtension(exppath, ".tex");
593 mangled = DocFileName(changeExtension(included_file.absFileName(), ".tex")).
599 else if (!runparams.silent)
600 ; // no warning wanted
601 else if (!isValidLaTeXFileName(incfile)) {
602 frontend::Alert::warning(_("Invalid filename"),
603 _("The following filename will cause troubles "
604 "when running the exported file through LaTeX: ") +
606 } else if (!isValidDVIFileName(incfile)) {
607 frontend::Alert::warning(_("Problematic filename for DVI"),
608 _("The following filename can cause troubles "
609 "when running the exported file through LaTeX "
610 "and opening the resulting DVI: ") +
611 from_utf8(incfile), true);
614 FileName const writefile(makeAbsPath(mangled, runparams.for_preview ?
615 buffer().temppath() : masterBuffer->temppath()));
617 LYXERR(Debug::LATEX, "incfile:" << incfile);
618 LYXERR(Debug::LATEX, "exportfile:" << exportfile);
619 LYXERR(Debug::LATEX, "writefile:" << writefile);
621 string const tex_format = flavor2format(runparams.flavor);
623 switch (type(params())) {
626 incfile = latex_path(incfile);
628 os << '\\' << from_ascii(params().getCmdName()) << '{'
629 << from_utf8(incfile) << '}';
633 runparams.exportdata->addExternalFile(tex_format, writefile,
636 // \input wants file with extension (default is .tex)
637 if (!isLyXFileName(included_file.absFileName())) {
638 incfile = latex_path(incfile);
640 os << '\\' << from_ascii(params().getCmdName())
641 << '{' << from_utf8(incfile) << '}';
643 incfile = changeExtension(incfile, ".tex");
644 incfile = latex_path(incfile);
646 os << '\\' << from_ascii(params().getCmdName())
647 << '{' << from_utf8(incfile) << '}';
652 // Here, listings and minted have slightly different behaviors.
653 // Using listings, it is always possible to have a caption,
654 // even for non-floats. Using minted, only floats can have a
655 // caption. So, with minted we use the following strategy.
656 // If a caption was specified but the float parameter was not,
657 // we ourselves add a caption above the listing (because the
658 // listing comes from a file and might span several pages).
659 // Otherwise, if float was specified, the floating listing
660 // environment provided by minted is used. In either case, the
661 // label parameter is taken as the label by which the float
662 // can be referenced, otherwise it will have the meaning
663 // intended by minted. In this last case, the label will
664 // serve as a sort of caption that, however, will be shown
665 // by minted only if the frame parameter is also specified.
666 bool const use_minted = buffer().params().use_minted;
667 runparams.exportdata->addExternalFile(tex_format, writefile,
669 string const opt = to_utf8(params()["lstparams"]);
670 // opt is set in QInclude dialog and should have passed validation.
671 InsetListingsParams lstparams(opt);
672 docstring parameters = from_utf8(lstparams.params());
677 bool isfloat = lstparams.isFloat();
678 // We are going to split parameters at commas, so
679 // replace commas that are not parameter separators
680 // with a code point from the private use area
681 char_type comma = replaceCommaInBraces(parameters);
682 // Get float placement, language, caption, and
683 // label, then remove the relative options if minted.
684 vector<docstring> opts =
685 getVectorFromString(parameters, from_ascii(","), false);
686 vector<docstring> latexed_opts;
687 for (size_t i = 0; i < opts.size(); ++i) {
688 // Restore replaced commas
689 opts[i] = subst(opts[i], comma, ',');
690 if (use_minted && prefixIs(opts[i], from_ascii("float"))) {
691 if (prefixIs(opts[i], from_ascii("float=")))
692 placement = opts[i].substr(6);
693 opts.erase(opts.begin() + i--);
694 } else if (use_minted && prefixIs(opts[i], from_ascii("language="))) {
695 language = opts[i].substr(9);
696 opts.erase(opts.begin() + i--);
697 } else if (prefixIs(opts[i], from_ascii("caption="))) {
698 caption = params().prepareCommand(runparams, trim(opts[i].substr(8), "{}"),
699 ParamInfo::HANDLING_LATEXIFY);
700 opts.erase(opts.begin() + i--);
702 latexed_opts.push_back(from_ascii("caption={") + caption + "}");
703 } else if (prefixIs(opts[i], from_ascii("label="))) {
704 label = params().prepareCommand(runparams, trim(opts[i].substr(6), "{}"),
705 ParamInfo::HANDLING_ESCAPE);
706 opts.erase(opts.begin() + i--);
708 latexed_opts.push_back(from_ascii("label={") + label + "}");
710 if (use_minted && !label.empty()) {
711 if (isfloat || !caption.empty())
712 label = trim(label, "{}");
714 opts.push_back(from_ascii("label=") + label);
717 if (!latexed_opts.empty())
718 opts.insert(opts.end(), latexed_opts.begin(), latexed_opts.end());
719 parameters = getStringFromVector(opts, from_ascii(","));
720 if (language.empty())
721 language = from_ascii("TeX");
722 if (use_minted && isfloat) {
723 os << breakln << "\\begin{listing}";
724 if (!placement.empty())
725 os << '[' << placement << "]";
727 } else if (use_minted && !caption.empty()) {
728 os << breakln << "\\lyxmintcaption[t]{" << caption;
730 os << "\\label{" << label << "}";
733 os << (use_minted ? "\\inputminted" : "\\lstinputlisting");
734 if (!parameters.empty())
735 os << "[" << parameters << "]";
737 os << '{' << ascii_lowercase(language) << '}';
738 os << '{' << incfile << '}';
739 if (use_minted && isfloat) {
740 if (!caption.empty())
741 os << breakln << "\\caption{" << caption << "}";
743 os << breakln << "\\label{" << label << "}";
744 os << breakln << "\\end{listing}\n";
749 runparams.exportdata->addExternalFile(tex_format, writefile,
752 // \include don't want extension and demands that the
753 // file really have .tex
754 incfile = changeExtension(incfile, string());
755 incfile = latex_path(incfile);
757 os << '\\' << from_ascii(params().getCmdName()) << '{'
758 << from_utf8(incfile) << '}';
765 if (runparams.inComment || runparams.dryrun)
766 // Don't try to load or copy the file if we're
767 // in a comment or doing a dryrun
770 if (!isInputOrInclude(params()) ||
771 !isLyXFileName(included_file.absFileName())) {
772 // In this case, it's not a LyX file, so we copy the file
773 // to the temp dir, so that .aux files etc. are not created
774 // in the original dir. Files included by this file will be
775 // found via either the environment variable TEXINPUTS, or
776 // input@path, see ../Buffer.cpp.
777 unsigned long const checksum_in = included_file.checksum();
778 unsigned long const checksum_out = writefile.checksum();
779 if (checksum_in != checksum_out) {
780 if (!included_file.copyTo(writefile)) {
783 to_utf8(bformat(_("Could not copy the file\n%1$s\n"
784 "into the temporary directory."),
785 from_utf8(included_file.absFileName()))));
792 // it's a LyX file and we're inputting or including, so
793 // try to load it so we can write the associated latex
794 Buffer * tmp = loadIfNeeded();
796 if (!runparams.silent) {
797 docstring text = bformat(_("Could not load included "
799 "Please, check whether it actually exists."),
800 included_file.displayName());
801 throw ExceptionMessage(ErrorException, _("Error: "),
807 if (recursion_error_)
810 if (!runparams.silent) {
811 if (tmp->params().baseClass() != masterBuffer->params().baseClass()) {
813 docstring text = bformat(_("Included file `%1$s'\n"
814 "has textclass `%2$s'\n"
815 "while parent file has textclass `%3$s'."),
816 included_file.displayName(),
817 from_utf8(tmp->params().documentClass().name()),
818 from_utf8(masterBuffer->params().documentClass().name()));
819 Alert::warning(_("Different textclasses"), text, true);
822 string const child_tf = tmp->params().useNonTeXFonts ? "true" : "false";
823 string const master_tf = masterBuffer->params().useNonTeXFonts ? "true" : "false";
824 if (tmp->params().useNonTeXFonts != masterBuffer->params().useNonTeXFonts) {
825 docstring text = bformat(_("Included file `%1$s'\n"
826 "has use-non-TeX-fonts set to `%2$s'\n"
827 "while parent file has use-non-TeX-fonts set to `%3$s'."),
828 included_file.displayName(),
830 from_utf8(master_tf));
831 Alert::warning(_("Different use-non-TeX-fonts settings"), text, true);
833 else if (tmp->params().inputenc != masterBuffer->params().inputenc) {
834 docstring text = bformat(_("Included file `%1$s'\n"
835 "uses input encoding \"%2$s\" [%3$s]\n"
836 "while parent file uses input encoding \"%4$s\" [%5$s]."),
837 included_file.displayName(),
838 _(tmp->params().inputenc),
839 from_utf8(tmp->params().encoding().guiName()),
840 _(masterBuffer->params().inputenc),
841 from_utf8(masterBuffer->params().encoding().guiName()));
842 Alert::warning(_("Different LaTeX input encodings"), text, true);
845 // Make sure modules used in child are all included in master
846 // FIXME It might be worth loading the children's modules into the master
847 // over in BufferParams rather than doing this check.
848 LayoutModuleList const masterModules = masterBuffer->params().getModules();
849 LayoutModuleList const childModules = tmp->params().getModules();
850 LayoutModuleList::const_iterator it = childModules.begin();
851 LayoutModuleList::const_iterator end = childModules.end();
852 for (; it != end; ++it) {
853 string const module = *it;
854 LayoutModuleList::const_iterator found =
855 find(masterModules.begin(), masterModules.end(), module);
856 if (found == masterModules.end()) {
857 docstring text = bformat(_("Included file `%1$s'\n"
858 "uses module `%2$s'\n"
859 "which is not used in parent file."),
860 included_file.displayName(), from_utf8(module));
861 Alert::warning(_("Module not found"), text, true);
866 tmp->markDepClean(masterBuffer->temppath());
868 // Don't assume the child's format is latex
869 string const inc_format = tmp->params().bufferFormat();
870 FileName const tmpwritefile(changeExtension(writefile.absFileName(),
871 theFormats().extension(inc_format)));
873 // FIXME: handle non existing files
874 // The included file might be written in a different encoding
876 Encoding const * const oldEnc = runparams.encoding;
877 Language const * const oldLang = runparams.master_language;
878 // If the master uses non-TeX fonts (XeTeX, LuaTeX),
879 // the children must be encoded in plain utf8!
880 if (masterBuffer->params().useNonTeXFonts)
881 runparams.encoding = encodings.fromLyXName("utf8-plain");
883 runparams.encoding = oldEnc;
884 else runparams.encoding = &tmp->params().encoding();
885 runparams.master_language = buffer().params().language;
886 runparams.par_begin = 0;
887 runparams.par_end = tmp->paragraphs().size();
888 runparams.is_child = true;
889 Buffer::ExportStatus retval =
890 tmp->makeLaTeXFile(tmpwritefile, masterFileName(buffer()).
891 onlyPath().absFileName(), runparams, Buffer::OnlyBody);
892 if (retval == Buffer::ExportKilled && buffer().isClone() &&
893 buffer().isExporting()) {
894 // We really shouldn't get here, I don't think.
895 LYXERR0("No conversion exception?");
896 throw ConversionException();
898 else if (retval != Buffer::ExportSuccess) {
899 if (!runparams.silent) {
900 docstring msg = bformat(_("Included file `%1$s' "
901 "was not exported correctly.\n "
902 "LaTeX export is probably incomplete."),
903 included_file.displayName());
904 ErrorList const & el = tmp->errorList("Export");
906 msg = bformat(from_ascii("%1$s\n\n%2$s\n\n%3$s"),
907 msg, el.begin()->error, el.begin()->description);
908 throw ExceptionMessage(ErrorException, _("Error: "), msg);
911 runparams.encoding = oldEnc;
912 runparams.master_language = oldLang;
913 runparams.is_child = false;
915 // If needed, use converters to produce a latex file from the child
916 if (tmpwritefile != writefile) {
918 Converters::RetVal const conv_retval =
919 theConverters().convert(tmp, tmpwritefile, writefile,
920 included_file, inc_format, tex_format, el);
921 if (conv_retval == Converters::KILLED && buffer().isClone() &&
922 buffer().isExporting()) {
923 // We really shouldn't get here, I don't think.
924 LYXERR0("No conversion exception?");
925 throw ConversionException();
926 } else if (conv_retval != Converters::SUCCESS && !runparams.silent) {
927 docstring msg = bformat(_("Included file `%1$s' "
928 "was not exported correctly.\n "
929 "LaTeX export is probably incomplete."),
930 included_file.displayName());
932 msg = bformat(from_ascii("%1$s\n\n%2$s\n\n%3$s"),
933 msg, el.begin()->error, el.begin()->description);
934 throw ExceptionMessage(ErrorException, _("Error: "), msg);
940 docstring InsetInclude::xhtml(XMLStream & xs, OutputParams const & rp) const
945 // For verbatim and listings, we just include the contents of the file as-is.
946 // In the case of listings, we wrap it in <pre>.
947 bool const listing = isListings(params());
948 if (listing || isVerbatim(params())) {
950 xs << xml::StartTag("pre");
951 // FIXME: We don't know the encoding of the file, default to UTF-8.
952 xs << includedFileName(buffer(), params()).fileContents("UTF-8");
954 xs << xml::EndTag("pre");
958 // We don't (yet) know how to Input or Include non-LyX files.
959 // (If we wanted to get really arcane, we could run some tex2html
960 // converter on the included file. But that's just masochistic.)
961 FileName const included_file = includedFileName(buffer(), params());
962 if (!isLyXFileName(included_file.absFileName())) {
964 frontend::Alert::warning(_("Unsupported Inclusion"),
965 bformat(_("LyX does not know how to include non-LyX files when "
966 "generating HTML output. Offending file:\n%1$s"),
967 ltrim(params()["filename"])));
971 // In the other cases, we will generate the HTML and include it.
973 Buffer const * const ibuf = loadIfNeeded();
977 if (recursion_error_)
980 // are we generating only some paragraphs, or all of them?
981 bool const all_pars = !rp.dryrun ||
982 (rp.par_begin == 0 &&
983 rp.par_end == (int)buffer().text().paragraphs().size());
985 OutputParams op = rp;
989 ibuf->writeLyXHTMLSource(xs.os(), op, Buffer::IncludedFile);
991 xs << XMLStream::ESCAPE_NONE << "<!-- Included file: ";
992 xs << from_utf8(included_file.absFileName());
993 xs << XMLStream::ESCAPE_NONE << " -->";
1000 int InsetInclude::plaintext(odocstringstream & os,
1001 OutputParams const & op, size_t) const
1003 // just write the filename if we're making a tooltip or toc entry,
1004 // or are generating this for advanced search
1005 if (op.for_tooltip || op.for_toc || op.for_search) {
1006 os << '[' << screenLabel() << '\n'
1007 << ltrim(getParam("filename")) << "\n]";
1008 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
1011 if (isVerbatim(params()) || isListings(params())) {
1012 if (op.for_search) {
1013 os << '[' << screenLabel() << ']';
1016 os << '[' << screenLabel() << '\n'
1017 // FIXME: We don't know the encoding of the file, default to UTF-8.
1018 << includedFileName(buffer(), params()).fileContents("UTF-8")
1021 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
1024 Buffer const * const ibuf = loadIfNeeded();
1026 docstring const str = '[' + screenLabel() + ']';
1031 if (recursion_error_)
1034 writePlaintextFile(*ibuf, os, op);
1039 void InsetInclude::docbook(XMLStream & xs, OutputParams const & rp) const
1044 // For verbatim and listings, we just include the contents of the file as-is.
1045 bool const verbatim = isVerbatim(params());
1046 bool const listing = isListings(params());
1047 if (listing || verbatim) {
1049 xs << xml::StartTag("programlisting");
1051 xs << xml::StartTag("literallayout");
1053 // FIXME: We don't know the encoding of the file, default to UTF-8.
1054 xs << includedFileName(buffer(), params()).fileContents("UTF-8");
1057 xs << xml::EndTag("programlisting");
1059 xs << xml::EndTag("literallayout");
1064 // We don't know how to input or include non-LyX files. Input it as a comment.
1065 FileName const included_file = includedFileName(buffer(), params());
1066 if (!isLyXFileName(included_file.absFileName())) {
1068 frontend::Alert::warning(_("Unsupported Inclusion"),
1069 bformat(_("LyX does not know how to process included non-LyX files when "
1070 "generating DocBook output. The content of the file will be output as a "
1071 "comment. Offending file:\n%1$s"),
1072 ltrim(params()["filename"])));
1074 // Read the file, output it wrapped into comments.
1075 xs << XMLStream::ESCAPE_NONE << "<!-- Included file: ";
1076 xs << from_utf8(included_file.absFileName());
1077 xs << XMLStream::ESCAPE_NONE << " -->";
1079 xs << XMLStream::ESCAPE_NONE << "<!-- ";
1080 xs << included_file.fileContents("UTF-8");
1081 xs << XMLStream::ESCAPE_NONE << " -->";
1083 xs << XMLStream::ESCAPE_NONE << "<!-- End of included file: ";
1084 xs << from_utf8(included_file.absFileName());
1085 xs << XMLStream::ESCAPE_NONE << " -->";
1088 // In the other cases, we generate the DocBook version and include it.
1089 Buffer const * const ibuf = loadIfNeeded();
1093 if (recursion_error_)
1096 // are we generating only some paragraphs, or all of them?
1097 bool const all_pars = !rp.dryrun ||
1098 (rp.par_begin == 0 &&
1099 rp.par_end == (int) buffer().text().paragraphs().size());
1101 OutputParams op = rp;
1105 op.inInclude = true;
1106 op.docbook_in_par = false;
1107 ibuf->writeDocBookSource(xs.os(), op, Buffer::IncludedFile);
1109 xs << XMLStream::ESCAPE_NONE << "<!-- Included file: ";
1110 xs << from_utf8(included_file.absFileName());
1111 xs << XMLStream::ESCAPE_NONE << " -->";
1116 void InsetInclude::validate(LaTeXFeatures & features) const
1118 LATTEST(&buffer() == &features.buffer());
1120 string incfile = ltrim(to_utf8(params()["filename"]));
1121 string const included_file =
1122 includedFileName(buffer(), params()).absFileName();
1125 if (isLyXFileName(included_file))
1126 writefile = changeExtension(included_file, ".sgml");
1128 writefile = included_file;
1130 if (!features.runparams().nice && !isVerbatim(params()) && !isListings(params())) {
1131 incfile = DocFileName(writefile).mangledFileName();
1132 writefile = makeAbsPath(incfile,
1133 buffer().masterBuffer()->temppath()).absFileName();
1136 features.includeFile(include_label, writefile);
1138 features.useInsetLayout(getLayout());
1139 if (isVerbatim(params()))
1140 features.require("verbatim");
1141 else if (isListings(params())) {
1142 if (buffer().params().use_minted) {
1143 features.require("minted");
1144 string const opts = to_utf8(params()["lstparams"]);
1145 InsetListingsParams lstpars(opts);
1146 if (!lstpars.isFloat() && contains(opts, "caption="))
1147 features.require("lyxmintcaption");
1149 features.require("listings");
1152 // Here we must do the fun stuff...
1153 // Load the file in the include if it needs
1155 Buffer * const tmp = loadIfNeeded();
1159 // the file is loaded
1160 if (checkForRecursiveInclude(tmp))
1162 buffer().pushIncludedBuffer(tmp);
1164 // We must temporarily change features.buffer,
1165 // otherwise it would always be the master buffer,
1166 // and nested includes would not work.
1167 features.setBuffer(*tmp);
1168 // Maybe this is already a child
1169 bool const is_child =
1170 features.runparams().is_child;
1171 features.runparams().is_child = true;
1172 tmp->validate(features);
1173 features.runparams().is_child = is_child;
1174 features.setBuffer(buffer());
1176 buffer().popIncludedBuffer();
1180 void InsetInclude::collectBibKeys(InsetIterator const & /*di*/, FileNameList & checkedFiles) const
1182 Buffer * ibuf = loadIfNeeded();
1186 if (checkForRecursiveInclude(ibuf))
1188 buffer().pushIncludedBuffer(ibuf);
1189 ibuf->collectBibKeys(checkedFiles);
1190 buffer().popIncludedBuffer();
1194 void InsetInclude::metrics(MetricsInfo & mi, Dimension & dim) const
1196 LBUFERR(mi.base.bv);
1198 bool use_preview = false;
1199 if (RenderPreview::previewText()) {
1200 graphics::PreviewImage const * pimage =
1201 preview_->getPreviewImage(mi.base.bv->buffer());
1202 use_preview = pimage && pimage->image();
1206 preview_->metrics(mi, dim);
1210 button_.update(screenLabel(), true, false, !file_exist_ || recursion_error_);
1212 button_.metrics(mi, dim);
1215 Box b(0, dim.wid, -dim.asc, dim.des);
1220 void InsetInclude::draw(PainterInfo & pi, int x, int y) const
1222 LBUFERR(pi.base.bv);
1224 bool use_preview = false;
1225 if (RenderPreview::previewText()) {
1226 graphics::PreviewImage const * pimage =
1227 preview_->getPreviewImage(pi.base.bv->buffer());
1228 use_preview = pimage && pimage->image();
1232 preview_->draw(pi, x, y);
1234 button_.draw(pi, x, y);
1238 void InsetInclude::write(ostream & os) const
1240 params().Write(os, &buffer());
1244 string InsetInclude::contextMenuName() const
1246 return "context-include";
1250 Inset::RowFlags InsetInclude::rowFlags() const
1252 return type(params()) == INPUT ? Inline : Display;
1256 docstring InsetInclude::layoutName() const
1258 if (isListings(params()))
1259 return from_ascii("IncludeListings");
1260 return InsetCommand::layoutName();
1268 void InsetInclude::fileChanged() const
1270 Buffer const * const buffer = updateFrontend();
1274 preview_->removePreview(*buffer);
1275 add_preview(*preview_, *this, *buffer);
1276 preview_->startLoading(*buffer);
1282 bool preview_wanted(InsetCommandParams const & params, Buffer const & buffer)
1284 FileName const included_file = includedFileName(buffer, params);
1286 return type(params) == INPUT && params.preview() &&
1287 included_file.isReadableFile();
1291 docstring latexString(InsetInclude const & inset)
1293 odocstringstream ods;
1295 // We don't need to set runparams.encoding since this will be done
1296 // by latex() anyway.
1297 OutputParams runparams(nullptr);
1298 runparams.flavor = OutputParams::LATEX;
1299 runparams.for_preview = true;
1300 inset.latex(os, runparams);
1306 void add_preview(RenderMonitoredPreview & renderer, InsetInclude const & inset,
1307 Buffer const & buffer)
1309 InsetCommandParams const & params = inset.params();
1310 if (RenderPreview::previewText() && preview_wanted(params, buffer)) {
1311 renderer.setAbsFile(includedFileName(buffer, params));
1312 docstring const snippet = latexString(inset);
1313 renderer.addPreview(snippet, buffer);
1320 void InsetInclude::addPreview(DocIterator const & /*inset_pos*/,
1321 graphics::PreviewLoader & ploader) const
1323 Buffer const & buffer = ploader.buffer();
1324 if (!preview_wanted(params(), buffer))
1326 preview_->setAbsFile(includedFileName(buffer, params()));
1327 docstring const snippet = latexString(*this);
1328 preview_->addPreview(snippet, ploader);
1332 void InsetInclude::addToToc(DocIterator const & cpit, bool output_active,
1333 UpdateType utype, TocBackend & backend) const
1335 if (isListings(params())) {
1337 label_->addToToc(cpit, output_active, utype, backend);
1338 TocBuilder & b = backend.builder("listing");
1339 b.pushItem(cpit, screenLabel(), output_active);
1340 InsetListingsParams p(to_utf8(params()["lstparams"]));
1341 b.argumentItem(from_utf8(p.getParamValue("caption")));
1343 } else if (isVerbatim(params())) {
1344 TocBuilder & b = backend.builder("child");
1345 b.pushItem(cpit, screenLabel(), output_active);
1348 Buffer const * const childbuffer = loadIfNeeded();
1350 TocBuilder & b = backend.builder("child");
1351 string const fname = ltrim(to_utf8(params()["filename"]));
1352 // mark non-existent childbuffer with FILE MISSING
1353 docstring const str = (childbuffer ? from_ascii("") : _("FILE MISSING: "))
1354 + from_utf8(onlyFileName(fname)) + " (" + from_utf8(fname) + ")";
1355 b.pushItem(cpit, str, output_active);
1361 if (checkForRecursiveInclude(childbuffer))
1363 buffer().pushIncludedBuffer(childbuffer);
1364 // Update the child's tocBackend. The outliner uses the master's, but
1365 // the navigation menu uses the child's.
1366 childbuffer->tocBackend().update(output_active, utype);
1367 // Include Tocs from children
1368 childbuffer->inset().addToToc(DocIterator(), output_active, utype,
1370 buffer().popIncludedBuffer();
1371 // Copy missing outliner names (though the user has been warned against
1372 // having different document class and module selection between master
1374 for (auto const & name
1375 : childbuffer->params().documentClass().outlinerNames())
1376 backend.addName(name.first, translateIfPossible(name.second));
1381 void InsetInclude::updateCommand()
1386 docstring old_label = label_->getParam("name");
1387 label_->updateLabel(old_label);
1388 // the label might have been adapted (duplicate)
1389 docstring new_label = label_->getParam("name");
1390 if (old_label == new_label)
1393 // update listings parameters...
1394 InsetCommandParams p(INCLUDE_CODE);
1396 InsetListingsParams par(to_utf8(params()["lstparams"]));
1397 par.addParam("label", "{" + to_utf8(new_label) + "}", true);
1398 p["lstparams"] = from_utf8(par.params());
1403 void InsetInclude::updateBuffer(ParIterator const & it, UpdateType utype, bool const deleted)
1405 file_exist_ = includedFileExist();
1406 Buffer const * const childbuffer = loadIfNeeded();
1408 if (!checkForRecursiveInclude(childbuffer))
1409 childbuffer->updateBuffer(Buffer::UpdateChildOnly, utype);
1410 button_.update(screenLabel(), true, false, recursion_error_);
1414 button_.update(screenLabel(), true, false, !file_exist_);
1416 if (!isListings(params()))
1420 label_->updateBuffer(it, utype, deleted);
1422 InsetListingsParams const par(to_utf8(params()["lstparams"]));
1423 if (par.getParamValue("caption").empty()) {
1424 listings_label_ = buffer().B_("Program Listing");
1427 Buffer const & master = *buffer().masterBuffer();
1428 Counters & counters = master.params().documentClass().counters();
1429 docstring const cnt = from_ascii("listing");
1430 listings_label_ = master.B_("Program Listing");
1431 if (counters.hasCounter(cnt)) {
1432 counters.step(cnt, utype);
1433 listings_label_ += " " + convert<docstring>(counters.value(cnt));
1438 bool InsetInclude::includedFileExist() const
1440 // check whether the included file exist
1441 string incFileName = ltrim(to_utf8(params()["filename"]));
1443 support::makeAbsPath(incFileName,
1444 support::onlyPath(buffer().absFileName()));