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"
68 #include "support/bind.h"
71 using namespace lyx::support;
75 namespace Alert = frontend::Alert;
80 docstring const uniqueID()
82 static unsigned int seed = 1000;
84 Mutex::Locker lock(&mutex);
85 return "file" + convert<docstring>(++seed);
89 /// the type of inclusion
91 INCLUDE, VERB, INPUT, VERBAST, LISTINGS, NONE
95 Types type(string const & s)
99 if (s == "verbatiminput")
101 if (s == "verbatiminput*")
103 if (s == "lstinputlisting" || s == "inputminted")
111 Types type(InsetCommandParams const & params)
113 return type(params.getCmdName());
117 bool isListings(InsetCommandParams const & params)
119 return type(params) == LISTINGS;
123 bool isVerbatim(InsetCommandParams const & params)
125 Types const t = type(params);
126 return t == VERB || t == VERBAST;
130 bool isInputOrInclude(InsetCommandParams const & params)
132 Types const t = type(params);
133 return t == INPUT || t == INCLUDE;
137 FileName const masterFileName(Buffer const & buffer)
139 return buffer.masterBuffer()->fileName();
143 void add_preview(RenderMonitoredPreview &, InsetInclude const &, Buffer const &);
146 string const parentFileName(Buffer const & buffer)
148 return buffer.absFileName();
152 FileName const includedFileName(Buffer const & buffer,
153 InsetCommandParams const & params)
155 return makeAbsPath(ltrim(to_utf8(params["filename"])),
156 onlyPath(parentFileName(buffer)));
160 InsetLabel * createLabel(Buffer * buf, docstring const & label_str)
162 if (label_str.empty())
164 InsetCommandParams icp(LABEL_CODE);
165 icp["name"] = label_str;
166 return new InsetLabel(buf, icp);
170 char_type replaceCommaInBraces(docstring & params)
172 // Code point from private use area
173 char_type private_char = 0xE000;
175 for (char_type & c : params) {
180 else if (c == ',' && count)
189 InsetInclude::InsetInclude(Buffer * buf, InsetCommandParams const & p)
190 : InsetCommand(buf, p), include_label(uniqueID()),
191 preview_(make_unique<RenderMonitoredPreview>(this)), failedtoload_(false),
192 set_label_(false), label_(0), child_buffer_(0), file_exist_(false)
194 preview_->connect([=](){ fileChanged(); });
196 if (isListings(params())) {
197 InsetListingsParams listing_params(to_utf8(p["lstparams"]));
198 label_ = createLabel(buffer_, from_utf8(listing_params.getParamValue("label")));
199 } else if (isInputOrInclude(params()) && buf)
204 InsetInclude::InsetInclude(InsetInclude const & other)
205 : InsetCommand(other), include_label(other.include_label),
206 preview_(make_unique<RenderMonitoredPreview>(this)), failedtoload_(false),
207 set_label_(false), label_(0), child_buffer_(0), file_exist_(other.file_exist_)
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 //pass everything else up the chain
323 InsetCommand::doDispatch(cur, cmd);
329 void InsetInclude::editIncluded(string const & f)
331 if (isLyXFileName(f)) {
332 FuncRequest fr(LFUN_BUFFER_CHILD_OPEN, f);
335 // tex file or other text file in verbatim mode
336 theFormats().edit(buffer(),
337 support::makeAbsPath(f, support::onlyPath(buffer().absFileName())),
342 bool InsetInclude::getStatus(Cursor & cur, FuncRequest const & cmd,
343 FuncStatus & flag) const
345 switch (cmd.action()) {
347 case LFUN_INSET_EDIT:
348 flag.setEnabled(true);
351 case LFUN_INSET_MODIFY:
352 if (cmd.getArg(0) == "changetype")
353 return InsetCommand::getStatus(cur, cmd, flag);
355 flag.setEnabled(true);
359 return InsetCommand::getStatus(cur, cmd, flag);
364 void InsetInclude::setParams(InsetCommandParams const & p)
366 // invalidate the cache
369 InsetCommand::setParams(p);
372 if (preview_->monitoring())
373 preview_->stopMonitoring();
375 if (type(params()) == INPUT)
376 add_preview(*preview_, *this, buffer());
380 bool InsetInclude::isChildIncluded() const
382 std::list<std::string> includeonlys =
383 buffer().params().getIncludedChildren();
384 if (includeonlys.empty())
386 return (std::find(includeonlys.begin(),
388 ltrim(to_utf8(params()["filename"]))) != includeonlys.end());
392 docstring InsetInclude::screenLabel() const
394 docstring pre = file_exist_ ? docstring() : _("FILE MISSING:");
398 switch (type(params())) {
400 temp = buffer().B_("Input");
403 temp = buffer().B_("Verbatim Input");
406 temp = buffer().B_("Verbatim Input*");
409 if (isChildIncluded())
410 temp = buffer().B_("Include");
412 temp += buffer().B_("Include (excluded)");
415 temp = listings_label_;
418 LASSERT(false, temp = buffer().B_("Unknown"));
424 if (ltrim(params()["filename"]).empty())
427 temp += from_utf8(onlyFileName(ltrim(to_utf8(params()["filename"]))));
429 return pre.empty() ? temp : pre + from_ascii(" ") + temp;
433 Buffer * InsetInclude::getChildBuffer() const
435 Buffer * childBuffer = loadIfNeeded();
437 // FIXME RECURSIVE INCLUDE
438 // This isn't sufficient, as the inclusion could be downstream.
439 // But it'll have to do for now.
440 return (childBuffer == &buffer()) ? 0 : childBuffer;
444 Buffer * InsetInclude::loadIfNeeded() const
446 // This is for background export and preview. We don't even want to
447 // try to load the cloned child document again.
448 if (buffer().isClone())
449 return child_buffer_;
451 // Don't try to load it again if we failed before.
452 if (failedtoload_ || isVerbatim(params()) || isListings(params()))
455 FileName const included_file = includedFileName(buffer(), params());
456 // Use cached Buffer if possible.
457 if (child_buffer_ != 0) {
458 if (theBufferList().isLoaded(child_buffer_)
459 // additional sanity check: make sure the Buffer really is
460 // associated with the file we want.
461 && child_buffer_ == theBufferList().getBuffer(included_file))
462 return child_buffer_;
463 // Buffer vanished, so invalidate cache and try to reload.
467 if (!isLyXFileName(included_file.absFileName()))
470 Buffer * child = theBufferList().getBuffer(included_file);
472 // the readonly flag can/will be wrong, not anymore I think.
473 if (!included_file.exists())
476 child = theBufferList().newBuffer(included_file.absFileName());
478 // Buffer creation is not possible.
481 // Set parent before loading, such that macros can be tracked
482 child->setParent(&buffer());
484 if (child->loadLyXFile() != Buffer::ReadSuccess) {
485 failedtoload_ = true;
487 //close the buffer we just opened
488 theBufferList().release(child);
492 if (!child->errorList("Parse").empty()) {
493 // FIXME: Do something.
496 // The file was already loaded, so, simply
497 // inform parent buffer about local macros.
498 Buffer const * parent = &buffer();
499 child->setParent(parent);
501 child->listMacroNames(macros);
502 MacroNameSet::const_iterator cit = macros.begin();
503 MacroNameSet::const_iterator end = macros.end();
504 for (; cit != end; ++cit)
505 parent->usermacros.insert(*cit);
508 // Cache the child buffer.
509 child_buffer_ = child;
514 void InsetInclude::latex(otexstream & os, OutputParams const & runparams) const
516 string incfile = ltrim(to_utf8(params()["filename"]));
518 // Warn if no file name has been specified
519 if (incfile.empty()) {
520 frontend::Alert::warning(_("No file name specified"),
521 _("An included file name is empty.\n"
522 "Ignoring Inclusion"),
526 // Warn if file doesn't exist
527 if (!includedFileExist()) {
528 frontend::Alert::warning(_("Included file not found"),
529 bformat(_("The included file\n"
531 "has not been found. LyX will ignore the inclusion."),
537 FileName const included_file = includedFileName(buffer(), params());
539 // Check we're not trying to include ourselves.
540 // FIXME RECURSIVE INCLUDE
541 // This isn't sufficient, as the inclusion could be downstream.
542 // But it'll have to do for now.
543 if (isInputOrInclude(params()) &&
544 buffer().absFileName() == included_file.absFileName())
546 Alert::error(_("Recursive input"),
547 bformat(_("Attempted to include file %1$s in itself! "
548 "Ignoring inclusion."), from_utf8(incfile)));
552 Buffer const * const masterBuffer = buffer().masterBuffer();
554 // if incfile is relative, make it relative to the master
556 if (!FileName::isAbsolute(incfile)) {
558 incfile = to_utf8(makeRelPath(from_utf8(included_file.absFileName()),
559 from_utf8(masterBuffer->filePath())));
562 string exppath = incfile;
563 if (!runparams.export_folder.empty()) {
564 exppath = makeAbsPath(exppath, runparams.export_folder).realPath();
565 FileName(exppath).onlyPath().createPath();
568 // write it to a file (so far the complete file)
572 if (type(params()) == LISTINGS) {
573 exportfile = exppath;
574 mangled = DocFileName(included_file).mangledFileName();
576 exportfile = changeExtension(exppath, ".tex");
577 mangled = DocFileName(changeExtension(included_file.absFileName(), ".tex")).
583 else if (!runparams.silent)
584 ; // no warning wanted
585 else if (!isValidLaTeXFileName(incfile)) {
586 frontend::Alert::warning(_("Invalid filename"),
587 _("The following filename will cause troubles "
588 "when running the exported file through LaTeX: ") +
590 } else if (!isValidDVIFileName(incfile)) {
591 frontend::Alert::warning(_("Problematic filename for DVI"),
592 _("The following filename can cause troubles "
593 "when running the exported file through LaTeX "
594 "and opening the resulting DVI: ") +
595 from_utf8(incfile), true);
598 FileName const writefile(makeAbsPath(mangled, runparams.for_preview ?
599 buffer().temppath() : masterBuffer->temppath()));
601 LYXERR(Debug::LATEX, "incfile:" << incfile);
602 LYXERR(Debug::LATEX, "exportfile:" << exportfile);
603 LYXERR(Debug::LATEX, "writefile:" << writefile);
605 string const tex_format = flavor2format(runparams.flavor);
607 switch (type(params())) {
610 incfile = latex_path(incfile);
612 os << '\\' << from_ascii(params().getCmdName()) << '{'
613 << from_utf8(incfile) << '}';
617 runparams.exportdata->addExternalFile(tex_format, writefile,
620 // \input wants file with extension (default is .tex)
621 if (!isLyXFileName(included_file.absFileName())) {
622 incfile = latex_path(incfile);
624 os << '\\' << from_ascii(params().getCmdName())
625 << '{' << from_utf8(incfile) << '}';
627 incfile = changeExtension(incfile, ".tex");
628 incfile = latex_path(incfile);
630 os << '\\' << from_ascii(params().getCmdName())
631 << '{' << from_utf8(incfile) << '}';
636 // Here, listings and minted have sligthly different behaviors.
637 // Using listings, it is always possible to have a caption,
638 // even for non-floats. Using minted, only floats can have a
639 // caption. So, with minted we use the following strategy.
640 // If a caption was specified but the float parameter was not,
641 // we ourselves add a caption above the listing (because the
642 // listing comes from a file and might span several pages).
643 // Otherwise, if float was specified, the floating listing
644 // environment provided by minted is used. In either case, the
645 // label parameter is taken as the label by which the float
646 // can be referenced, otherwise it will have the meaning
647 // intended by minted. In this last case, the label will
648 // serve as a sort of caption that, however, will be shown
649 // by minted only if the frame parameter is also specified.
650 bool const use_minted = buffer().params().use_minted;
651 runparams.exportdata->addExternalFile(tex_format, writefile,
653 string const opt = to_utf8(params()["lstparams"]);
654 // opt is set in QInclude dialog and should have passed validation.
655 InsetListingsParams lstparams(opt);
656 docstring parameters = from_utf8(lstparams.params());
661 bool isfloat = lstparams.isFloat();
662 // We are going to split parameters at commas, so
663 // replace commas that are not parameter separators
664 // with a code point from the private use area
665 char_type comma = replaceCommaInBraces(parameters);
666 // Get float placement, language, caption, and
667 // label, then remove the relative options if minted.
668 vector<docstring> opts =
669 getVectorFromString(parameters, from_ascii(","), false);
670 vector<docstring> latexed_opts;
671 for (size_t i = 0; i < opts.size(); ++i) {
672 // Restore replaced commas
673 opts[i] = subst(opts[i], comma, ',');
674 if (use_minted && prefixIs(opts[i], from_ascii("float"))) {
675 if (prefixIs(opts[i], from_ascii("float=")))
676 placement = opts[i].substr(6);
677 opts.erase(opts.begin() + i--);
678 } else if (use_minted && prefixIs(opts[i], from_ascii("language="))) {
679 language = opts[i].substr(9);
680 opts.erase(opts.begin() + i--);
681 } else if (prefixIs(opts[i], from_ascii("caption="))) {
682 caption = params().prepareCommand(runparams, trim(opts[i].substr(8), "{}"),
683 ParamInfo::HANDLING_LATEXIFY);
684 opts.erase(opts.begin() + i--);
686 latexed_opts.push_back(from_ascii("caption={") + caption + "}");
687 } else if (prefixIs(opts[i], from_ascii("label="))) {
688 label = params().prepareCommand(runparams, trim(opts[i].substr(6), "{}"),
689 ParamInfo::HANDLING_ESCAPE);
690 opts.erase(opts.begin() + i--);
692 latexed_opts.push_back(from_ascii("label={") + label + "}");
694 if (use_minted && !label.empty()) {
695 if (isfloat || !caption.empty())
696 label = trim(label, "{}");
698 opts.push_back(from_ascii("label=") + label);
701 if (!latexed_opts.empty())
702 opts.insert(opts.end(), latexed_opts.begin(), latexed_opts.end());
703 parameters = getStringFromVector(opts, from_ascii(","));
704 if (language.empty())
705 language = from_ascii("TeX");
706 if (use_minted && isfloat) {
707 os << breakln << "\\begin{listing}";
708 if (!placement.empty())
709 os << '[' << placement << "]";
711 } else if (use_minted && !caption.empty()) {
712 os << breakln << "\\lyxmintcaption[t]{" << caption;
714 os << "\\label{" << label << "}";
717 os << (use_minted ? "\\inputminted" : "\\lstinputlisting");
718 if (!parameters.empty())
719 os << "[" << parameters << "]";
721 os << '{' << ascii_lowercase(language) << '}';
722 os << '{' << incfile << '}';
723 if (use_minted && isfloat) {
724 if (!caption.empty())
725 os << breakln << "\\caption{" << caption << "}";
727 os << breakln << "\\label{" << label << "}";
728 os << breakln << "\\end{listing}\n";
733 runparams.exportdata->addExternalFile(tex_format, writefile,
736 // \include don't want extension and demands that the
737 // file really have .tex
738 incfile = changeExtension(incfile, string());
739 incfile = latex_path(incfile);
741 os << '\\' << from_ascii(params().getCmdName()) << '{'
742 << from_utf8(incfile) << '}';
749 if (runparams.inComment || runparams.dryrun)
750 // Don't try to load or copy the file if we're
751 // in a comment or doing a dryrun
754 if (isInputOrInclude(params()) &&
755 isLyXFileName(included_file.absFileName())) {
756 // if it's a LyX file and we're inputting or including,
757 // try to load it so we can write the associated latex
759 Buffer * tmp = loadIfNeeded();
761 if (!runparams.silent) {
762 docstring text = bformat(_("Could not load included "
764 "Please, check whether it actually exists."),
765 included_file.displayName());
766 throw ExceptionMessage(ErrorException, _("Error: "),
772 if (!runparams.silent) {
773 if (tmp->params().baseClass() != masterBuffer->params().baseClass()) {
775 docstring text = bformat(_("Included file `%1$s'\n"
776 "has textclass `%2$s'\n"
777 "while parent file has textclass `%3$s'."),
778 included_file.displayName(),
779 from_utf8(tmp->params().documentClass().name()),
780 from_utf8(masterBuffer->params().documentClass().name()));
781 Alert::warning(_("Different textclasses"), text, true);
784 string const child_tf = tmp->params().useNonTeXFonts ? "true" : "false";
785 string const master_tf = masterBuffer->params().useNonTeXFonts ? "true" : "false";
786 if (tmp->params().useNonTeXFonts != masterBuffer->params().useNonTeXFonts) {
787 docstring text = bformat(_("Included file `%1$s'\n"
788 "has use-non-TeX-fonts set to `%2$s'\n"
789 "while parent file has use-non-TeX-fonts set to `%3$s'."),
790 included_file.displayName(),
792 from_utf8(master_tf));
793 Alert::warning(_("Different use-non-TeX-fonts settings"), text, true);
795 else if (tmp->params().inputenc != masterBuffer->params().inputenc) {
796 docstring text = bformat(_("Included file `%1$s'\n"
797 "uses input encoding \"%2$s\" [%3$s]\n"
798 "while parent file uses input encoding \"%4$s\" [%5$s]."),
799 included_file.displayName(),
800 _(tmp->params().inputenc),
801 from_utf8(tmp->params().encoding().guiName()),
802 _(masterBuffer->params().inputenc),
803 from_utf8(masterBuffer->params().encoding().guiName()));
804 Alert::warning(_("Different LaTeX input encodings"), text, true);
807 // Make sure modules used in child are all included in master
808 // FIXME It might be worth loading the children's modules into the master
809 // over in BufferParams rather than doing this check.
810 LayoutModuleList const masterModules = masterBuffer->params().getModules();
811 LayoutModuleList const childModules = tmp->params().getModules();
812 LayoutModuleList::const_iterator it = childModules.begin();
813 LayoutModuleList::const_iterator end = childModules.end();
814 for (; it != end; ++it) {
815 string const module = *it;
816 LayoutModuleList::const_iterator found =
817 find(masterModules.begin(), masterModules.end(), module);
818 if (found == masterModules.end()) {
819 docstring text = bformat(_("Included file `%1$s'\n"
820 "uses module `%2$s'\n"
821 "which is not used in parent file."),
822 included_file.displayName(), from_utf8(module));
823 Alert::warning(_("Module not found"), text, true);
828 tmp->markDepClean(masterBuffer->temppath());
830 // Don't assume the child's format is latex
831 string const inc_format = tmp->params().bufferFormat();
832 FileName const tmpwritefile(changeExtension(writefile.absFileName(),
833 theFormats().extension(inc_format)));
835 // FIXME: handle non existing files
836 // The included file might be written in a different encoding
838 Encoding const * const oldEnc = runparams.encoding;
839 Language const * const oldLang = runparams.master_language;
840 // If the master uses non-TeX fonts (XeTeX, LuaTeX),
841 // the children must be encoded in plain utf8!
842 runparams.encoding = masterBuffer->params().useNonTeXFonts ?
843 encodings.fromLyXName("utf8-plain") : oldEnc;
844 runparams.master_language = buffer().params().language;
845 runparams.par_begin = 0;
846 runparams.par_end = tmp->paragraphs().size();
847 runparams.is_child = true;
848 Buffer::ExportStatus retval =
849 tmp->makeLaTeXFile(tmpwritefile, masterFileName(buffer()).
850 onlyPath().absFileName(), runparams, Buffer::OnlyBody);
851 if (retval == Buffer::ExportKilled && buffer().isClone() &&
852 buffer().isExporting()) {
853 // We really shouldn't get here, I don't think.
854 LYXERR0("No conversion exception?");
855 throw ConversionException();
857 else if (retval != Buffer::ExportSuccess) {
858 if (!runparams.silent) {
859 docstring msg = bformat(_("Included file `%1$s' "
860 "was not exported correctly.\n "
861 "LaTeX export is probably incomplete."),
862 included_file.displayName());
863 ErrorList const & el = tmp->errorList("Export");
865 msg = bformat(from_ascii("%1$s\n\n%2$s\n\n%3$s"),
866 msg, el.begin()->error, el.begin()->description);
867 throw ExceptionMessage(ErrorException, _("Error: "), msg);
870 runparams.encoding = oldEnc;
871 runparams.master_language = oldLang;
872 runparams.is_child = false;
874 // If needed, use converters to produce a latex file from the child
875 if (tmpwritefile != writefile) {
877 Converters::RetVal const retval =
878 theConverters().convert(tmp, tmpwritefile, writefile,
879 included_file, inc_format, tex_format, el);
880 if (retval == Converters::KILLED && buffer().isClone() &&
881 buffer().isExporting()) {
882 // We really shouldn't get here, I don't think.
883 LYXERR0("No conversion exception?");
884 throw ConversionException();
885 } else if (retval != Converters::SUCCESS && !runparams.silent) {
886 docstring msg = bformat(_("Included file `%1$s' "
887 "was not exported correctly.\n "
888 "LaTeX export is probably incomplete."),
889 included_file.displayName());
891 msg = bformat(from_ascii("%1$s\n\n%2$s\n\n%3$s"),
892 msg, el.begin()->error, el.begin()->description);
893 throw ExceptionMessage(ErrorException, _("Error: "), msg);
897 // In this case, it's not a LyX file, so we copy the file
898 // to the temp dir, so that .aux files etc. are not created
899 // in the original dir. Files included by this file will be
900 // found via either the environment variable TEXINPUTS, or
901 // input@path, see ../Buffer.cpp.
902 unsigned long const checksum_in = included_file.checksum();
903 unsigned long const checksum_out = writefile.checksum();
905 if (checksum_in != checksum_out) {
906 if (!included_file.copyTo(writefile)) {
909 to_utf8(bformat(_("Could not copy the file\n%1$s\n"
910 "into the temporary directory."),
911 from_utf8(included_file.absFileName()))));
919 docstring InsetInclude::xhtml(XHTMLStream & xs, OutputParams const & rp) const
924 // For verbatim and listings, we just include the contents of the file as-is.
925 // In the case of listings, we wrap it in <pre>.
926 bool const listing = isListings(params());
927 if (listing || isVerbatim(params())) {
929 xs << html::StartTag("pre");
930 // FIXME: We don't know the encoding of the file, default to UTF-8.
931 xs << includedFileName(buffer(), params()).fileContents("UTF-8");
933 xs << html::EndTag("pre");
937 // We don't (yet) know how to Input or Include non-LyX files.
938 // (If we wanted to get really arcane, we could run some tex2html
939 // converter on the included file. But that's just masochistic.)
940 FileName const included_file = includedFileName(buffer(), params());
941 if (!isLyXFileName(included_file.absFileName())) {
943 frontend::Alert::warning(_("Unsupported Inclusion"),
944 bformat(_("LyX does not know how to include non-LyX files when "
945 "generating HTML output. Offending file:\n%1$s"),
946 ltrim(params()["filename"])));
950 // In the other cases, we will generate the HTML and include it.
952 // Check we're not trying to include ourselves.
953 // FIXME RECURSIVE INCLUDE
954 if (buffer().absFileName() == included_file.absFileName()) {
955 Alert::error(_("Recursive input"),
956 bformat(_("Attempted to include file %1$s in itself! "
957 "Ignoring inclusion."), ltrim(params()["filename"])));
961 Buffer const * const ibuf = loadIfNeeded();
965 // are we generating only some paragraphs, or all of them?
966 bool const all_pars = !rp.dryrun ||
967 (rp.par_begin == 0 &&
968 rp.par_end == (int)buffer().text().paragraphs().size());
970 OutputParams op = rp;
974 ibuf->writeLyXHTMLSource(xs.os(), op, Buffer::IncludedFile);
976 xs << XHTMLStream::ESCAPE_NONE
977 << "<!-- Included file: "
978 << from_utf8(included_file.absFileName())
979 << XHTMLStream::ESCAPE_NONE
985 int InsetInclude::plaintext(odocstringstream & os,
986 OutputParams const & op, size_t) const
988 // just write the filename if we're making a tooltip or toc entry,
989 // or are generating this for advanced search
990 if (op.for_tooltip || op.for_toc || op.for_search) {
991 os << '[' << screenLabel() << '\n'
992 << ltrim(getParam("filename")) << "\n]";
993 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
996 if (isVerbatim(params()) || isListings(params())) {
998 os << '[' << screenLabel() << ']';
1001 os << '[' << screenLabel() << '\n'
1002 // FIXME: We don't know the encoding of the file, default to UTF-8.
1003 << includedFileName(buffer(), params()).fileContents("UTF-8")
1006 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
1009 Buffer const * const ibuf = loadIfNeeded();
1011 docstring const str = '[' + screenLabel() + ']';
1015 writePlaintextFile(*ibuf, os, op);
1020 int InsetInclude::docbook(odocstream & os, OutputParams const & runparams) const
1022 string incfile = ltrim(to_utf8(params()["filename"]));
1024 // Do nothing if no file name has been specified
1025 if (incfile.empty())
1028 string const included_file = includedFileName(buffer(), params()).absFileName();
1030 // Check we're not trying to include ourselves.
1031 // FIXME RECURSIVE INCLUDE
1032 // This isn't sufficient, as the inclusion could be downstream.
1033 // But it'll have to do for now.
1034 if (buffer().absFileName() == included_file) {
1035 Alert::error(_("Recursive input"),
1036 bformat(_("Attempted to include file %1$s in itself! "
1037 "Ignoring inclusion."), from_utf8(incfile)));
1041 string exppath = incfile;
1042 if (!runparams.export_folder.empty()) {
1043 exppath = makeAbsPath(exppath, runparams.export_folder).realPath();
1044 FileName(exppath).onlyPath().createPath();
1047 // write it to a file (so far the complete file)
1048 string const exportfile = changeExtension(exppath, ".sgml");
1049 DocFileName writefile(changeExtension(included_file, ".sgml"));
1051 Buffer * tmp = loadIfNeeded();
1053 string const mangled = writefile.mangledFileName();
1054 writefile = makeAbsPath(mangled,
1055 buffer().masterBuffer()->temppath());
1056 if (!runparams.nice)
1059 LYXERR(Debug::LATEX, "incfile:" << incfile);
1060 LYXERR(Debug::LATEX, "exportfile:" << exportfile);
1061 LYXERR(Debug::LATEX, "writefile:" << writefile);
1063 tmp->makeDocBookFile(writefile, runparams, Buffer::OnlyBody);
1066 runparams.exportdata->addExternalFile("docbook", writefile,
1068 runparams.exportdata->addExternalFile("docbook-xml", writefile,
1071 if (isVerbatim(params()) || isListings(params())) {
1072 os << "<inlinegraphic fileref=\""
1073 << '&' << include_label << ';'
1074 << "\" format=\"linespecific\">";
1076 os << '&' << include_label << ';';
1082 void InsetInclude::validate(LaTeXFeatures & features) const
1084 LATTEST(&buffer() == &features.buffer());
1086 string incfile = ltrim(to_utf8(params()["filename"]));
1087 string const included_file =
1088 includedFileName(buffer(), params()).absFileName();
1091 if (isLyXFileName(included_file))
1092 writefile = changeExtension(included_file, ".sgml");
1094 writefile = included_file;
1096 if (!features.runparams().nice && !isVerbatim(params()) && !isListings(params())) {
1097 incfile = DocFileName(writefile).mangledFileName();
1098 writefile = makeAbsPath(incfile,
1099 buffer().masterBuffer()->temppath()).absFileName();
1102 features.includeFile(include_label, writefile);
1104 features.useInsetLayout(getLayout());
1105 if (isVerbatim(params()))
1106 features.require("verbatim");
1107 else if (isListings(params())) {
1108 if (buffer().params().use_minted) {
1109 features.require("minted");
1110 string const opts = to_utf8(params()["lstparams"]);
1111 InsetListingsParams lstpars(opts);
1112 if (!lstpars.isFloat() && contains(opts, "caption="))
1113 features.require("lyxmintcaption");
1115 features.require("listings");
1118 // Here we must do the fun stuff...
1119 // Load the file in the include if it needs
1121 Buffer * const tmp = loadIfNeeded();
1123 // the file is loaded
1124 // make sure the buffer isn't us
1125 // FIXME RECURSIVE INCLUDES
1126 // This is not sufficient, as recursive includes could be
1127 // more than a file away. But it will do for now.
1128 if (tmp && tmp != &buffer()) {
1129 // We must temporarily change features.buffer,
1130 // otherwise it would always be the master buffer,
1131 // and nested includes would not work.
1132 features.setBuffer(*tmp);
1133 // Maybe this is already a child
1134 bool const is_child =
1135 features.runparams().is_child;
1136 features.runparams().is_child = true;
1137 tmp->validate(features);
1138 features.runparams().is_child = is_child;
1139 features.setBuffer(buffer());
1145 void InsetInclude::collectBibKeys(InsetIterator const & /*di*/, FileNameList & checkedFiles) const
1147 Buffer * child = loadIfNeeded();
1150 // FIXME RECURSIVE INCLUDE
1151 // This isn't sufficient, as the inclusion could be downstream.
1152 // But it'll have to do for now.
1153 if (child == &buffer())
1155 child->collectBibKeys(checkedFiles);
1159 void InsetInclude::metrics(MetricsInfo & mi, Dimension & dim) const
1161 LBUFERR(mi.base.bv);
1163 bool use_preview = false;
1164 if (RenderPreview::previewText()) {
1165 graphics::PreviewImage const * pimage =
1166 preview_->getPreviewImage(mi.base.bv->buffer());
1167 use_preview = pimage && pimage->image();
1171 preview_->metrics(mi, dim);
1175 button_.update(screenLabel(), true, false);
1177 button_.metrics(mi, dim);
1180 Box b(0, dim.wid, -dim.asc, dim.des);
1185 void InsetInclude::draw(PainterInfo & pi, int x, int y) const
1187 LBUFERR(pi.base.bv);
1189 bool use_preview = false;
1190 if (RenderPreview::previewText()) {
1191 graphics::PreviewImage const * pimage =
1192 preview_->getPreviewImage(pi.base.bv->buffer());
1193 use_preview = pimage && pimage->image();
1197 preview_->draw(pi, x, y);
1199 button_.draw(pi, x, y);
1203 void InsetInclude::write(ostream & os) const
1205 params().Write(os, &buffer());
1209 string InsetInclude::contextMenuName() const
1211 return "context-include";
1215 Inset::DisplayType InsetInclude::display() const
1217 return type(params()) == INPUT ? Inline : AlignCenter;
1221 docstring InsetInclude::layoutName() const
1223 if (isListings(params()))
1224 return from_ascii("IncludeListings");
1225 return InsetCommand::layoutName();
1233 void InsetInclude::fileChanged() const
1235 Buffer const * const buffer = updateFrontend();
1239 preview_->removePreview(*buffer);
1240 add_preview(*preview_, *this, *buffer);
1241 preview_->startLoading(*buffer);
1247 bool preview_wanted(InsetCommandParams const & params, Buffer const & buffer)
1249 FileName const included_file = includedFileName(buffer, params);
1251 return type(params) == INPUT && params.preview() &&
1252 included_file.isReadableFile();
1256 docstring latexString(InsetInclude const & inset)
1258 odocstringstream ods;
1260 // We don't need to set runparams.encoding since this will be done
1261 // by latex() anyway.
1262 OutputParams runparams(0);
1263 runparams.flavor = OutputParams::LATEX;
1264 runparams.for_preview = true;
1265 inset.latex(os, runparams);
1271 void add_preview(RenderMonitoredPreview & renderer, InsetInclude const & inset,
1272 Buffer const & buffer)
1274 InsetCommandParams const & params = inset.params();
1275 if (RenderPreview::previewText() && preview_wanted(params, buffer)) {
1276 renderer.setAbsFile(includedFileName(buffer, params));
1277 docstring const snippet = latexString(inset);
1278 renderer.addPreview(snippet, buffer);
1285 void InsetInclude::addPreview(DocIterator const & /*inset_pos*/,
1286 graphics::PreviewLoader & ploader) const
1288 Buffer const & buffer = ploader.buffer();
1289 if (!preview_wanted(params(), buffer))
1291 preview_->setAbsFile(includedFileName(buffer, params()));
1292 docstring const snippet = latexString(*this);
1293 preview_->addPreview(snippet, ploader);
1297 void InsetInclude::addToToc(DocIterator const & cpit, bool output_active,
1298 UpdateType utype, TocBackend & backend) const
1300 if (isListings(params())) {
1302 label_->addToToc(cpit, output_active, utype, backend);
1303 TocBuilder & b = backend.builder("listing");
1304 b.pushItem(cpit, screenLabel(), output_active);
1305 InsetListingsParams p(to_utf8(params()["lstparams"]));
1306 b.argumentItem(from_utf8(p.getParamValue("caption")));
1309 Buffer const * const childbuffer = getChildBuffer();
1311 TocBuilder & b = backend.builder("child");
1312 docstring str = childbuffer ? childbuffer->fileName().displayName()
1314 b.pushItem(cpit, str, output_active);
1320 // Update the child's tocBackend. The outliner uses the master's, but
1321 // the navigation menu uses the child's.
1322 childbuffer->tocBackend().update(output_active, utype);
1323 // Include Tocs from children
1324 childbuffer->inset().addToToc(DocIterator(), output_active, utype,
1326 //Copy missing outliner names (though the user has been warned against
1327 //having different document class and module selection between master
1329 for (pair<string, docstring> const & name
1330 : childbuffer->params().documentClass().outlinerNames())
1331 backend.addName(name.first, translateIfPossible(name.second));
1336 void InsetInclude::updateCommand()
1341 docstring old_label = label_->getParam("name");
1342 label_->updateLabel(old_label);
1343 // the label might have been adapted (duplicate)
1344 docstring new_label = label_->getParam("name");
1345 if (old_label == new_label)
1348 // update listings parameters...
1349 InsetCommandParams p(INCLUDE_CODE);
1351 InsetListingsParams par(to_utf8(params()["lstparams"]));
1352 par.addParam("label", "{" + to_utf8(new_label) + "}", true);
1353 p["lstparams"] = from_utf8(par.params());
1358 void InsetInclude::updateBuffer(ParIterator const & it, UpdateType utype)
1360 file_exist_ = includedFileExist();
1362 button_.update(screenLabel(), true, false);
1364 Buffer const * const childbuffer = getChildBuffer();
1366 childbuffer->updateBuffer(Buffer::UpdateChildOnly, utype);
1369 if (!isListings(params()))
1373 label_->updateBuffer(it, utype);
1375 InsetListingsParams const par(to_utf8(params()["lstparams"]));
1376 if (par.getParamValue("caption").empty()) {
1377 listings_label_ = buffer().B_("Program Listing");
1380 Buffer const & master = *buffer().masterBuffer();
1381 Counters & counters = master.params().documentClass().counters();
1382 docstring const cnt = from_ascii("listing");
1383 listings_label_ = master.B_("Program Listing");
1384 if (counters.hasCounter(cnt)) {
1385 counters.step(cnt, utype);
1386 listings_label_ += " " + convert<docstring>(counters.value(cnt));
1391 bool InsetInclude::includedFileExist() const
1393 // check whether the included file exist
1394 string incFileName = ltrim(to_utf8(params()["filename"]));
1396 support::makeAbsPath(incFileName,
1397 support::onlyPath(buffer().absFileName()));