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"
22 #include "DispatchResult.h"
23 #include "ErrorList.h"
26 #include "FuncRequest.h"
27 #include "FuncStatus.h"
28 #include "LaTeXFeatures.h"
29 #include "LayoutFile.h"
34 #include "MetricsInfo.h"
35 #include "OutputParams.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 "support/convert.h"
50 #include "support/debug.h"
51 #include "support/docstream.h"
52 #include "support/FileNameList.h"
53 #include "support/filetools.h"
54 #include "support/gettext.h"
55 #include "support/lassert.h"
56 #include "support/lstrings.h" // contains
57 #include "support/lyxalgo.h"
59 #include <boost/bind.hpp>
62 using namespace lyx::support;
66 namespace Alert = frontend::Alert;
71 docstring const uniqueID()
73 static unsigned int seed = 1000;
74 return "file" + convert<docstring>(++seed);
78 /// the type of inclusion
80 INCLUDE, VERB, INPUT, VERBAST, LISTINGS, NONE
84 Types type(string const & s)
88 if (s == "verbatiminput")
90 if (s == "verbatiminput*")
92 if (s == "lstinputlisting")
100 Types type(InsetCommandParams const & params)
102 return type(params.getCmdName());
106 bool isListings(InsetCommandParams const & params)
108 return type(params) == LISTINGS;
112 bool isVerbatim(InsetCommandParams const & params)
114 Types const t = type(params);
115 return t == VERB || t == VERBAST;
119 bool isInputOrInclude(InsetCommandParams const & params)
121 Types const t = type(params);
122 return t == INPUT || t == INCLUDE;
126 FileName const masterFileName(Buffer const & buffer)
128 return buffer.masterBuffer()->fileName();
132 void add_preview(RenderMonitoredPreview &, InsetInclude const &, Buffer const &);
135 string const parentFilename(Buffer const & buffer)
137 return buffer.absFileName();
141 FileName const includedFilename(Buffer const & buffer,
142 InsetCommandParams const & params)
144 return makeAbsPath(to_utf8(params["filename"]),
145 onlyPath(parentFilename(buffer)));
149 InsetLabel * createLabel(docstring const & label_str)
151 if (label_str.empty())
153 InsetCommandParams icp(LABEL_CODE);
154 icp["name"] = label_str;
155 return new InsetLabel(icp);
161 InsetInclude::InsetInclude(InsetCommandParams const & p)
162 : InsetCommand(p, "include"), include_label(uniqueID()),
163 preview_(new RenderMonitoredPreview(this)), failedtoload_(false),
164 set_label_(false), label_(0)
166 preview_->fileChanged(boost::bind(&InsetInclude::fileChanged, this));
168 if (isListings(params())) {
169 InsetListingsParams listing_params(to_utf8(p["lstparams"]));
170 label_ = createLabel(from_utf8(listing_params.getParamValue("label")));
175 InsetInclude::InsetInclude(InsetInclude const & other)
176 : InsetCommand(other), include_label(other.include_label),
177 preview_(new RenderMonitoredPreview(this)), failedtoload_(false),
178 set_label_(false), label_(0)
180 preview_->fileChanged(boost::bind(&InsetInclude::fileChanged, this));
183 label_ = new InsetLabel(*other.label_);
187 InsetInclude::~InsetInclude()
193 void InsetInclude::setBuffer(Buffer & buffer)
195 InsetCommand::setBuffer(buffer);
197 label_->setBuffer(buffer);
201 ParamInfo const & InsetInclude::findInfo(string const & /* cmdName */)
204 // This is only correct for the case of listings, but it'll do for now.
205 // In the other cases, this second parameter should just be empty.
206 static ParamInfo param_info_;
207 if (param_info_.empty()) {
208 param_info_.add("filename", ParamInfo::LATEX_REQUIRED);
209 param_info_.add("lstparams", ParamInfo::LATEX_OPTIONAL);
215 bool InsetInclude::isCompatibleCommand(string const & s)
217 return type(s) != NONE;
221 void InsetInclude::doDispatch(Cursor & cur, FuncRequest & cmd)
223 LASSERT(cur.buffer() == &buffer(), return);
224 switch (cmd.action) {
226 case LFUN_INSET_EDIT: {
227 editIncluded(to_utf8(params()["filename"]));
231 case LFUN_INSET_MODIFY: {
232 InsetCommandParams p(INCLUDE_CODE);
233 if (cmd.getArg(0) == "changetype") {
234 InsetCommand::doDispatch(cur, cmd);
237 InsetCommand::string2params("include", to_utf8(cmd.argument()), p);
238 if (!p.getCmdName().empty()) {
240 InsetListingsParams new_params(to_utf8(p["lstparams"]));
241 docstring const new_label =
242 from_utf8(new_params.getParamValue("label"));
245 old_label = label_->getParam("name");
246 if (new_label.empty())
248 else if (label_ && old_label != new_label) {
249 label_->updateCommand(new_label);
250 // the label might have been adapted (duplicate)
251 if (new_label != label_->getParam("name")) {
252 new_params.addParam("label",
253 "{" + to_utf8(label_->getParam("name")) + "}");
254 p["lstparams"] = from_utf8(new_params.params());
256 } else if (old_label != new_label) {
257 label_ = createLabel(new_label);
258 label_->setBuffer(buffer());
268 //pass everything else up the chain
270 InsetCommand::doDispatch(cur, cmd);
276 void InsetInclude::editIncluded(string const & file)
278 string const ext = support::getExtension(file);
280 FuncRequest fr(LFUN_BUFFER_CHILD_OPEN, file);
283 // tex file or other text file in verbatim mode
284 formats.edit(buffer(),
285 support::makeAbsPath(file, support::onlyPath(buffer().absFileName())),
290 bool InsetInclude::getStatus(Cursor & cur, FuncRequest const & cmd,
291 FuncStatus & flag) const
293 switch (cmd.action) {
295 case LFUN_INSET_EDIT:
296 case LFUN_INSET_MODIFY:
297 flag.setEnabled(true);
301 return InsetCommand::getStatus(cur, cmd, flag);
306 void InsetInclude::setParams(InsetCommandParams const & p)
308 InsetCommand::setParams(p);
311 if (preview_->monitoring())
312 preview_->stopMonitoring();
314 if (type(params()) == INPUT)
315 add_preview(*preview_, *this, buffer());
317 buffer().updateBibfilesCache();
321 docstring InsetInclude::screenLabel() const
325 switch (type(params())) {
327 temp = buffer().B_("Input");
330 temp = buffer().B_("Verbatim Input");
333 temp = buffer().B_("Verbatim Input*");
336 temp = buffer().B_("Include");
339 temp = listings_label_;
342 LASSERT(false, /**/);
347 if (params()["filename"].empty())
350 temp += from_utf8(onlyFilename(to_utf8(params()["filename"])));
356 Buffer * InsetInclude::getChildBuffer(Buffer const & buffer) const
358 InsetCommandParams const & p = params();
359 if (isVerbatim(p) || isListings(p))
362 string const included_file = includedFilename(buffer, p).absFilename();
363 if (!isLyXFilename(included_file))
366 Buffer * childBuffer = loadIfNeeded(buffer);
368 // FIXME: recursive includes
369 return (childBuffer == &buffer) ? 0 : childBuffer;
373 Buffer * InsetInclude::loadIfNeeded(Buffer const & parent) const
375 InsetCommandParams const & p = params();
379 if (isVerbatim(p) || isListings(p))
382 string const parent_filename = parent.absFileName();
383 FileName const included_file = makeAbsPath(to_utf8(p["filename"]),
384 onlyPath(parent_filename));
386 if (!isLyXFilename(included_file.absFilename()))
389 Buffer * child = theBufferList().getBuffer(included_file);
391 // the readonly flag can/will be wrong, not anymore I think.
392 if (!included_file.exists())
395 child = theBufferList().newBuffer(included_file.absFilename());
397 // Buffer creation is not possible.
400 if (!child->loadLyXFile(included_file)) {
401 failedtoload_ = true;
402 //close the buffer we just opened
403 theBufferList().release(child);
407 if (!child->errorList("Parse").empty()) {
408 // FIXME: Do something.
411 child->setParent(&parent);
416 int InsetInclude::latex(odocstream & os, OutputParams const & runparams) const
418 string incfile = to_utf8(params()["filename"]);
420 // Do nothing if no file name has been specified
424 FileName const included_file = includedFilename(buffer(), params());
426 // Check we're not trying to include ourselves.
427 // FIXME RECURSIVE INCLUDE
428 // This isn't sufficient, as the inclusion could be downstream.
429 // But it'll have to do for now.
430 if (isInputOrInclude(params()) &&
431 buffer().absFileName() == included_file.absFilename())
433 Alert::error(_("Recursive input"),
434 bformat(_("Attempted to include file %1$s in itself! "
435 "Ignoring inclusion."), from_utf8(incfile)));
439 Buffer const * const masterBuffer = buffer().masterBuffer();
441 // if incfile is relative, make it relative to the master
443 if (!FileName(incfile).isAbsolute()) {
445 incfile = to_utf8(makeRelPath(from_utf8(included_file.absFilename()),
446 from_utf8(masterBuffer->filePath())));
449 // write it to a file (so far the complete file)
453 if (type(params()) == LISTINGS) {
454 exportfile = incfile;
455 mangled = DocFileName(included_file).mangledFilename();
457 exportfile = changeExtension(incfile, ".tex");
458 mangled = DocFileName(changeExtension(included_file.absFilename(), ".tex")).
462 FileName const writefile(makeAbsPath(mangled, masterBuffer->temppath()));
466 else if (!isValidLaTeXFilename(incfile)) {
467 frontend::Alert::warning(_("Invalid filename"),
468 _("The following filename is likely to cause trouble "
469 "when running the exported file through LaTeX: ") +
472 LYXERR(Debug::LATEX, "incfile:" << incfile);
473 LYXERR(Debug::LATEX, "exportfile:" << exportfile);
474 LYXERR(Debug::LATEX, "writefile:" << writefile);
476 if (runparams.inComment || runparams.dryrun) {
477 //Don't try to load or copy the file if we're
478 //in a comment or doing a dryrun
479 } else if (isInputOrInclude(params()) &&
480 isLyXFilename(included_file.absFilename())) {
481 //if it's a LyX file and we're inputting or including,
482 //try to load it so we can write the associated latex
483 if (!loadIfNeeded(buffer()))
486 Buffer * tmp = theBufferList().getBuffer(included_file);
488 if (tmp->params().baseClass() != masterBuffer->params().baseClass()) {
490 docstring text = bformat(_("Included file `%1$s'\n"
491 "has textclass `%2$s'\n"
492 "while parent file has textclass `%3$s'."),
493 included_file.displayName(),
494 from_utf8(tmp->params().documentClass().name()),
495 from_utf8(masterBuffer->params().documentClass().name()));
496 Alert::warning(_("Different textclasses"), text);
499 // Make sure modules used in child are all included in master
500 //FIXME It might be worth loading the children's modules into the master
501 //over in BufferParams rather than doing this check.
502 list<string> const masterModules = masterBuffer->params().getModules();
503 list<string> const childModules = tmp->params().getModules();
504 list<string>::const_iterator it = childModules.begin();
505 list<string>::const_iterator end = childModules.end();
506 for (; it != end; ++it) {
507 string const module = *it;
508 list<string>::const_iterator found =
509 find(masterModules.begin(), masterModules.end(), module);
510 if (found == masterModules.end()) {
511 docstring text = bformat(_("Included file `%1$s'\n"
512 "uses module `%2$s'\n"
513 "which is not used in parent file."),
514 included_file.displayName(), from_utf8(module));
515 Alert::warning(_("Module not found"), text);
519 tmp->markDepClean(masterBuffer->temppath());
521 // FIXME: handle non existing files
522 // FIXME: Second argument is irrelevant!
523 // since only_body is true, makeLaTeXFile will not look at second
524 // argument. Should we set it to string(), or should makeLaTeXFile
525 // make use of it somehow? (JMarc 20031002)
526 // The included file might be written in a different encoding
527 Encoding const * const oldEnc = runparams.encoding;
528 runparams.encoding = &tmp->params().encoding();
529 tmp->makeLaTeXFile(writefile,
530 masterFileName(buffer()).onlyPath().absFilename(),
532 runparams.encoding = oldEnc;
534 // In this case, it's not a LyX file, so we copy the file
535 // to the temp dir, so that .aux files etc. are not created
536 // in the original dir. Files included by this file will be
537 // found via input@path, see ../Buffer.cpp.
538 unsigned long const checksum_in = included_file.checksum();
539 unsigned long const checksum_out = writefile.checksum();
541 if (checksum_in != checksum_out) {
542 if (!included_file.copyTo(writefile)) {
545 to_utf8(bformat(_("Could not copy the file\n%1$s\n"
546 "into the temporary directory."),
547 from_utf8(included_file.absFilename()))));
553 string const tex_format = (runparams.flavor == OutputParams::LATEX) ?
554 "latex" : "pdflatex";
555 if (isVerbatim(params())) {
556 incfile = latex_path(incfile);
558 os << '\\' << from_ascii(params().getCmdName()) << '{'
559 << from_utf8(incfile) << '}';
560 } else if (type(params()) == INPUT) {
561 runparams.exportdata->addExternalFile(tex_format, writefile,
564 // \input wants file with extension (default is .tex)
565 if (!isLyXFilename(included_file.absFilename())) {
566 incfile = latex_path(incfile);
568 os << '\\' << from_ascii(params().getCmdName())
569 << '{' << from_utf8(incfile) << '}';
571 incfile = changeExtension(incfile, ".tex");
572 incfile = latex_path(incfile);
574 os << '\\' << from_ascii(params().getCmdName())
575 << '{' << from_utf8(incfile) << '}';
577 } else if (type(params()) == LISTINGS) {
578 os << '\\' << from_ascii(params().getCmdName());
579 string const opt = to_utf8(params()["lstparams"]);
580 // opt is set in QInclude dialog and should have passed validation.
581 InsetListingsParams params(opt);
582 if (!params.params().empty())
583 os << "[" << from_utf8(params.params()) << "]";
584 os << '{' << from_utf8(incfile) << '}';
586 runparams.exportdata->addExternalFile(tex_format, writefile,
589 // \include don't want extension and demands that the
590 // file really have .tex
591 incfile = changeExtension(incfile, string());
592 incfile = latex_path(incfile);
594 os << '\\' << from_ascii(params().getCmdName()) << '{'
595 << from_utf8(incfile) << '}';
602 int InsetInclude::plaintext(odocstream & os, OutputParams const &) const
604 if (isVerbatim(params()) || isListings(params())) {
605 os << '[' << screenLabel() << '\n';
606 // FIXME: We don't know the encoding of the file, default to UTF-8.
607 os << includedFilename(buffer(), params()).fileContents("UTF-8");
609 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
611 docstring const str = '[' + screenLabel() + ']';
618 int InsetInclude::docbook(odocstream & os, OutputParams const & runparams) const
620 string incfile = to_utf8(params()["filename"]);
622 // Do nothing if no file name has been specified
626 string const included_file = includedFilename(buffer(), params()).absFilename();
628 // Check we're not trying to include ourselves.
629 // FIXME RECURSIVE INCLUDE
630 // This isn't sufficient, as the inclusion could be downstream.
631 // But it'll have to do for now.
632 if (buffer().absFileName() == included_file) {
633 Alert::error(_("Recursive input"),
634 bformat(_("Attempted to include file %1$s in itself! "
635 "Ignoring inclusion."), from_utf8(incfile)));
639 // write it to a file (so far the complete file)
640 string const exportfile = changeExtension(incfile, ".sgml");
641 DocFileName writefile(changeExtension(included_file, ".sgml"));
643 if (loadIfNeeded(buffer())) {
644 Buffer * tmp = theBufferList().getBuffer(FileName(included_file));
646 string const mangled = writefile.mangledFilename();
647 writefile = makeAbsPath(mangled,
648 buffer().masterBuffer()->temppath());
652 LYXERR(Debug::LATEX, "incfile:" << incfile);
653 LYXERR(Debug::LATEX, "exportfile:" << exportfile);
654 LYXERR(Debug::LATEX, "writefile:" << writefile);
656 tmp->makeDocBookFile(writefile, runparams, true);
659 runparams.exportdata->addExternalFile("docbook", writefile,
661 runparams.exportdata->addExternalFile("docbook-xml", writefile,
664 if (isVerbatim(params()) || isListings(params())) {
665 os << "<inlinegraphic fileref=\""
666 << '&' << include_label << ';'
667 << "\" format=\"linespecific\">";
669 os << '&' << include_label << ';';
675 void InsetInclude::validate(LaTeXFeatures & features) const
677 string incfile = to_utf8(params()["filename"]);
680 LASSERT(&buffer() == &features.buffer(), /**/);
682 string const included_file =
683 includedFilename(buffer(), params()).absFilename();
685 if (isLyXFilename(included_file))
686 writefile = changeExtension(included_file, ".sgml");
688 writefile = included_file;
690 if (!features.runparams().nice && !isVerbatim(params()) && !isListings(params())) {
691 incfile = DocFileName(writefile).mangledFilename();
692 writefile = makeAbsPath(incfile,
693 buffer().masterBuffer()->temppath()).absFilename();
696 features.includeFile(include_label, writefile);
698 if (isVerbatim(params()))
699 features.require("verbatim");
700 else if (isListings(params()))
701 features.require("listings");
703 // Here we must do the fun stuff...
704 // Load the file in the include if it needs
706 if (loadIfNeeded(buffer())) {
708 Buffer * const tmp = theBufferList().getBuffer(FileName(included_file));
709 // make sure the buffer isn't us
710 // FIXME RECURSIVE INCLUDES
711 // This is not sufficient, as recursive includes could be
712 // more than a file away. But it will do for now.
713 if (tmp && tmp != &buffer()) {
714 // We must temporarily change features.buffer,
715 // otherwise it would always be the master buffer,
716 // and nested includes would not work.
717 features.setBuffer(*tmp);
718 tmp->validate(features);
719 features.setBuffer(buffer());
725 void InsetInclude::fillWithBibKeys(BiblioInfo & keys,
726 InsetIterator const & /*di*/) const
728 if (loadIfNeeded(buffer())) {
729 string const included_file = includedFilename(buffer(), params()).absFilename();
730 Buffer * tmp = theBufferList().getBuffer(FileName(included_file));
731 BiblioInfo const & newkeys = tmp->localBibInfo();
732 keys.mergeBiblioInfo(newkeys);
737 void InsetInclude::updateBibfilesCache()
739 Buffer * const tmp = getChildBuffer(buffer());
742 tmp->updateBibfilesCache();
743 tmp->setParent(&buffer());
748 support::FileNameList const &
749 InsetInclude::getBibfilesCache(Buffer const & buffer) const
751 Buffer * const tmp = getChildBuffer(buffer);
754 support::FileNameList const & cache = tmp->getBibfilesCache();
755 tmp->setParent(&buffer);
758 static support::FileNameList const empty;
763 void InsetInclude::metrics(MetricsInfo & mi, Dimension & dim) const
765 LASSERT(mi.base.bv, /**/);
767 bool use_preview = false;
768 if (RenderPreview::status() != LyXRC::PREVIEW_OFF) {
769 graphics::PreviewImage const * pimage =
770 preview_->getPreviewImage(mi.base.bv->buffer());
771 use_preview = pimage && pimage->image();
775 preview_->metrics(mi, dim);
779 button_.update(screenLabel(), true);
781 button_.metrics(mi, dim);
784 Box b(0, dim.wid, -dim.asc, dim.des);
789 void InsetInclude::draw(PainterInfo & pi, int x, int y) const
791 LASSERT(pi.base.bv, /**/);
793 bool use_preview = false;
794 if (RenderPreview::status() != LyXRC::PREVIEW_OFF) {
795 graphics::PreviewImage const * pimage =
796 preview_->getPreviewImage(pi.base.bv->buffer());
797 use_preview = pimage && pimage->image();
801 preview_->draw(pi, x, y);
803 button_.draw(pi, x, y);
807 docstring InsetInclude::contextMenu(BufferView const &, int, int) const
809 return from_ascii("context-include");
813 Inset::DisplayType InsetInclude::display() const
815 return type(params()) == INPUT ? Inline : AlignCenter;
824 void InsetInclude::fileChanged() const
826 Buffer const * const buffer = updateFrontend();
830 preview_->removePreview(*buffer);
831 add_preview(*preview_.get(), *this, *buffer);
832 preview_->startLoading(*buffer);
838 bool preview_wanted(InsetCommandParams const & params, Buffer const & buffer)
840 FileName const included_file = includedFilename(buffer, params);
842 return type(params) == INPUT && params.preview() &&
843 included_file.isReadableFile();
847 docstring latexString(InsetInclude const & inset)
850 // We don't need to set runparams.encoding since this will be done
851 // by latex() anyway.
852 OutputParams runparams(0);
853 runparams.flavor = OutputParams::LATEX;
854 inset.latex(os, runparams);
860 void add_preview(RenderMonitoredPreview & renderer, InsetInclude const & inset,
861 Buffer const & buffer)
863 InsetCommandParams const & params = inset.params();
864 if (RenderPreview::status() != LyXRC::PREVIEW_OFF &&
865 preview_wanted(params, buffer)) {
866 renderer.setAbsFile(includedFilename(buffer, params));
867 docstring const snippet = latexString(inset);
868 renderer.addPreview(snippet, buffer);
875 void InsetInclude::addPreview(graphics::PreviewLoader & ploader) const
877 Buffer const & buffer = ploader.buffer();
878 if (!preview_wanted(params(), buffer))
880 preview_->setAbsFile(includedFilename(buffer, params()));
881 docstring const snippet = latexString(*this);
882 preview_->addPreview(snippet, ploader);
886 void InsetInclude::addToToc(DocIterator const & cpit)
888 TocBackend & backend = buffer().tocBackend();
890 if (isListings(params())) {
892 label_->addToToc(cpit);
894 InsetListingsParams p(to_utf8(params()["lstparams"]));
895 string caption = p.getParamValue("caption");
898 Toc & toc = backend.toc("listing");
899 docstring str = convert<docstring>(toc.size() + 1)
900 + ". " + from_utf8(caption);
901 DocIterator pit = cpit;
902 toc.push_back(TocItem(pit, 0, str));
905 Buffer const * const childbuffer = getChildBuffer(buffer());
909 Toc & toc = backend.toc("child");
910 docstring str = childbuffer->fileName().displayName();
911 toc.push_back(TocItem(cpit, 0, str));
913 TocList & toclist = backend.tocs();
914 childbuffer->tocBackend().update();
915 TocList const & childtoclist = childbuffer->tocBackend().tocs();
916 TocList::const_iterator it = childtoclist.begin();
917 TocList::const_iterator const end = childtoclist.end();
918 for(; it != end; ++it)
919 toclist[it->first].insert(toclist[it->first].end(),
920 it->second.begin(), it->second.end());
924 void InsetInclude::updateLabels(ParIterator const & it)
926 Buffer const * const childbuffer = getChildBuffer(buffer());
928 childbuffer->updateLabels(true);
931 if (!isListings(params()))
935 label_->updateLabels(it);
937 InsetListingsParams const par(to_utf8(params()["lstparams"]));
938 if (par.getParamValue("caption").empty()) {
939 listings_label_ = buffer().B_("Program Listing");
942 Buffer const & master = *buffer().masterBuffer();
943 Counters & counters = master.params().documentClass().counters();
944 docstring const cnt = from_ascii("listing");
945 listings_label_ = master.B_("Program Listing");
946 if (counters.hasCounter(cnt)) {
948 listings_label_ += " " + convert<docstring>(counters.value(cnt));