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 "support/debug.h"
23 #include "DispatchResult.h"
25 #include "FuncRequest.h"
26 #include "FuncStatus.h"
27 #include "support/gettext.h"
28 #include "LaTeXFeatures.h"
32 #include "MetricsInfo.h"
33 #include "OutputParams.h"
34 #include "TocBackend.h"
35 #include "TextClass.h"
37 #include "frontends/alert.h"
38 #include "frontends/Painter.h"
40 #include "graphics/PreviewImage.h"
41 #include "graphics/PreviewLoader.h"
43 #include "insets/RenderPreview.h"
44 #include "insets/InsetListingsParams.h"
46 #include "support/docstream.h"
47 #include "support/FileNameList.h"
48 #include "support/filetools.h"
49 #include "support/lstrings.h" // contains
50 #include "support/lyxalgo.h"
51 #include "support/convert.h"
53 #include <boost/bind.hpp>
56 using namespace lyx::support;
60 namespace Alert = frontend::Alert;
65 docstring const uniqueID()
67 static unsigned int seed = 1000;
68 return "file" + convert<docstring>(++seed);
72 /// the type of inclusion
74 INCLUDE, VERB, INPUT, VERBAST, LISTINGS, NONE
78 Types type(string const & s)
82 if (s == "verbatiminput")
84 if (s == "verbatiminput*")
86 if (s == "lstinputlisting")
94 Types type(InsetCommandParams const & params)
96 return type(params.getCmdName());
100 bool isListings(InsetCommandParams const & params)
102 return type(params) == LISTINGS;
106 bool isVerbatim(InsetCommandParams const & params)
108 Types const t = type(params);
109 return t == VERB || t == VERBAST;
113 bool isInputOrInclude(InsetCommandParams const & params)
115 Types const t = type(params);
116 return t == INPUT || t == INCLUDE;
122 InsetInclude::InsetInclude(InsetCommandParams const & p)
123 : InsetCommand(p, "include"), include_label(uniqueID()),
124 preview_(new RenderMonitoredPreview(this)), set_label_(false)
126 preview_->fileChanged(boost::bind(&InsetInclude::fileChanged, this));
130 InsetInclude::InsetInclude(InsetInclude const & other)
131 : InsetCommand(other), include_label(other.include_label),
132 preview_(new RenderMonitoredPreview(this)), set_label_(false)
134 preview_->fileChanged(boost::bind(&InsetInclude::fileChanged, this));
138 CommandInfo const * InsetInclude::findInfo(string const & /* cmdName */)
141 // This is only correct for the case of listings, but it'll do for now.
142 // In the other cases, this second parameter should just be empty.
143 static const char * const paramnames[] = {"filename", "lstparams", ""};
144 static const bool isoptional[] = {false, true};
145 static const CommandInfo info = {2, paramnames, isoptional};
150 bool InsetInclude::isCompatibleCommand(string const & s)
152 return type(s) != NONE;
156 void InsetInclude::doDispatch(Cursor & cur, FuncRequest & cmd)
158 switch (cmd.action) {
160 case LFUN_INSET_MODIFY: {
161 InsetCommandParams p(INCLUDE_CODE);
162 InsetCommandMailer::string2params("include", to_utf8(cmd.argument()), p);
163 if (!p.getCmdName().empty()) {
165 InsetListingsParams par_old(to_utf8(params()["lstparams"]));
166 InsetListingsParams par_new(to_utf8(p["lstparams"]));
167 if (par_old.getParamValue("label") !=
168 par_new.getParamValue("label")
169 && !par_new.getParamValue("label").empty())
170 cur.bv().buffer().changeRefsIfUnique(
171 from_utf8(par_old.getParamValue("label")),
172 from_utf8(par_new.getParamValue("label")),
175 set(p, cur.buffer());
176 cur.buffer().updateBibfilesCache();
182 //pass everything else up the chain
184 InsetCommand::doDispatch(cur, cmd);
192 FileName const masterFileName(Buffer const & buffer)
194 return buffer.masterBuffer()->fileName();
198 string const parentFilename(Buffer const & buffer)
200 return buffer.absFileName();
204 FileName const includedFilename(Buffer const & buffer,
205 InsetCommandParams const & params)
207 return makeAbsPath(to_utf8(params["filename"]),
208 onlyPath(parentFilename(buffer)));
212 void add_preview(RenderMonitoredPreview &, InsetInclude const &, Buffer const &);
217 void InsetInclude::set(InsetCommandParams const & p, Buffer const & buffer)
222 if (preview_->monitoring())
223 preview_->stopMonitoring();
225 if (type(params()) == INPUT)
226 add_preview(*preview_, *this, buffer);
230 Inset * InsetInclude::clone() const
232 return new InsetInclude(*this);
236 docstring const InsetInclude::getScreenLabel(Buffer const & buf) const
240 switch (type(params())) {
242 temp = buf.B_("Input");
245 temp = buf.B_("Verbatim Input");
248 temp = buf.B_("Verbatim Input*");
251 temp = buf.B_("Include");
254 temp = listings_label_;
262 if (params()["filename"].empty())
265 temp += from_utf8(onlyFilename(to_utf8(params()["filename"])));
271 /// return the child buffer if the file is a LyX doc and is loaded
272 Buffer * getChildBuffer(Buffer const & buffer, InsetCommandParams const & params)
274 if (isVerbatim(params) || isListings(params))
277 string const included_file = includedFilename(buffer, params).absFilename();
278 if (!isLyXFilename(included_file))
281 Buffer * childBuffer = theBufferList().getBuffer(included_file);
283 //FIXME RECURSIVE INCLUDES
284 if (childBuffer == & buffer)
290 /// return true if the file is or got loaded.
291 Buffer * loadIfNeeded(Buffer const & parent, InsetCommandParams const & params)
293 if (isVerbatim(params) || isListings(params))
296 string const parent_filename = parent.absFileName();
297 FileName const included_file = makeAbsPath(to_utf8(params["filename"]),
298 onlyPath(parent_filename));
300 if (!isLyXFilename(included_file.absFilename()))
303 Buffer * child = theBufferList().getBuffer(included_file.absFilename());
305 // the readonly flag can/will be wrong, not anymore I think.
306 if (!included_file.exists())
309 child = theBufferList().newBuffer(included_file.absFilename());
311 // Buffer creation is not possible.
314 if (!child->loadLyXFile(included_file)) {
315 //close the buffer we just opened
316 theBufferList().release(child);
320 child->setParent(&parent);
325 void resetParentBuffer(Buffer const * parent, InsetCommandParams const & params,
328 if (isVerbatim(params) || isListings(params))
331 string const parent_filename = parent->absFileName();
332 FileName const included_file = makeAbsPath(to_utf8(params["filename"]),
333 onlyPath(parent_filename));
335 if (!isLyXFilename(included_file.absFilename()))
338 Buffer * child = theBufferList().getBuffer(included_file.absFilename());
339 // File not opened, nothing to close.
343 // Child document has a different parent, don't close it.
344 if (child->parent() != parent)
350 theBufferList().release(child);
352 updateLabels(*child);
356 int InsetInclude::latex(Buffer const & buffer, odocstream & os,
357 OutputParams const & runparams) const
359 string incfile(to_utf8(params()["filename"]));
361 // Do nothing if no file name has been specified
365 FileName const included_file = includedFilename(buffer, params());
367 //Check we're not trying to include ourselves.
368 //FIXME RECURSIVE INCLUDE
369 //This isn't sufficient, as the inclusion could be downstream.
370 //But it'll have to do for now.
371 if (isInputOrInclude(params()) &&
372 buffer.absFileName() == included_file.absFilename())
374 Alert::error(_("Recursive input"),
375 bformat(_("Attempted to include file %1$s in itself! "
376 "Ignoring inclusion."), from_utf8(incfile)));
380 Buffer const * const masterBuffer = buffer.masterBuffer();
382 // if incfile is relative, make it relative to the master
384 if (!FileName(incfile).isAbsolute()) {
386 incfile = to_utf8(makeRelPath(from_utf8(included_file.absFilename()),
387 from_utf8(masterBuffer->filePath())));
390 // write it to a file (so far the complete file)
391 string const exportfile = changeExtension(incfile, ".tex");
392 string const mangled =
393 DocFileName(changeExtension(included_file.absFilename(),".tex")).
395 FileName const writefile(makeAbsPath(mangled, masterBuffer->temppath()));
399 else if (!isValidLaTeXFilename(incfile)) {
400 frontend::Alert::warning(_("Invalid filename"),
401 _("The following filename is likely to cause trouble "
402 "when running the exported file through LaTeX: ") +
405 LYXERR(Debug::LATEX, "incfile:" << incfile);
406 LYXERR(Debug::LATEX, "exportfile:" << exportfile);
407 LYXERR(Debug::LATEX, "writefile:" << writefile);
409 if (runparams.inComment || runparams.dryrun) {
410 //Don't try to load or copy the file if we're
411 //in a comment or doing a dryrun
412 } else if (isInputOrInclude(params()) &&
413 isLyXFilename(included_file.absFilename())) {
414 //if it's a LyX file and we're inputting or including,
415 //try to load it so we can write the associated latex
416 if (!loadIfNeeded(buffer, params()))
419 Buffer * tmp = theBufferList().getBuffer(included_file.absFilename());
421 if (tmp->params().getBaseClass() != masterBuffer->params().getBaseClass()) {
423 docstring text = bformat(_("Included file `%1$s'\n"
424 "has textclass `%2$s'\n"
425 "while parent file has textclass `%3$s'."),
426 included_file.displayName(),
427 from_utf8(tmp->params().getTextClass().name()),
428 from_utf8(masterBuffer->params().getTextClass().name()));
429 Alert::warning(_("Different textclasses"), text);
433 // Make sure modules used in child are all included in master
434 //FIXME It might be worth loading the children's modules into the master
435 //over in BufferParams rather than doing this check.
436 vector<string> const masterModules = masterBuffer->params().getModules();
437 vector<string> const childModules = tmp->params().getModules();
438 vector<string>::const_iterator it = childModules.begin();
439 vector<string>::const_iterator end = childModules.end();
440 for (; it != end; ++it) {
441 string const module = *it;
442 vector<string>::const_iterator found =
443 find(masterModules.begin(), masterModules.end(), module);
444 if (found != masterModules.end()) {
445 docstring text = bformat(_("Included file `%1$s'\n"
446 "uses module `%2$s'\n"
447 "which is not used in parent file."),
448 included_file.displayName(), from_utf8(module));
449 Alert::warning(_("Module not found"), text);
453 tmp->markDepClean(masterBuffer->temppath());
455 // FIXME: handle non existing files
456 // FIXME: Second argument is irrelevant!
457 // since only_body is true, makeLaTeXFile will not look at second
458 // argument. Should we set it to string(), or should makeLaTeXFile
459 // make use of it somehow? (JMarc 20031002)
460 // The included file might be written in a different encoding
461 Encoding const * const oldEnc = runparams.encoding;
462 runparams.encoding = &tmp->params().encoding();
463 tmp->makeLaTeXFile(writefile,
464 masterFileName(buffer).onlyPath().absFilename(),
466 runparams.encoding = oldEnc;
468 // In this case, it's not a LyX file, so we copy the file
469 // to the temp dir, so that .aux files etc. are not created
470 // in the original dir. Files included by this file will be
471 // found via input@path, see ../Buffer.cpp.
472 unsigned long const checksum_in = included_file.checksum();
473 unsigned long const checksum_out = writefile.checksum();
475 if (checksum_in != checksum_out) {
476 if (!included_file.copyTo(writefile)) {
479 to_utf8(bformat(_("Could not copy the file\n%1$s\n"
480 "into the temporary directory."),
481 from_utf8(included_file.absFilename()))));
487 string const tex_format = (runparams.flavor == OutputParams::LATEX) ?
488 "latex" : "pdflatex";
489 if (isVerbatim(params())) {
490 incfile = latex_path(incfile);
492 os << '\\' << from_ascii(params().getCmdName()) << '{'
493 << from_utf8(incfile) << '}';
494 } else if (type(params()) == INPUT) {
495 runparams.exportdata->addExternalFile(tex_format, writefile,
498 // \input wants file with extension (default is .tex)
499 if (!isLyXFilename(included_file.absFilename())) {
500 incfile = latex_path(incfile);
502 os << '\\' << from_ascii(params().getCmdName())
503 << '{' << from_utf8(incfile) << '}';
505 incfile = changeExtension(incfile, ".tex");
506 incfile = latex_path(incfile);
508 os << '\\' << from_ascii(params().getCmdName())
509 << '{' << from_utf8(incfile) << '}';
511 } else if (type(params()) == LISTINGS) {
512 os << '\\' << from_ascii(params().getCmdName());
513 string const opt = to_utf8(params()["lstparams"]);
514 // opt is set in QInclude dialog and should have passed validation.
515 InsetListingsParams params(opt);
516 if (!params.params().empty())
517 os << "[" << from_utf8(params.params()) << "]";
518 os << '{' << from_utf8(incfile) << '}';
520 runparams.exportdata->addExternalFile(tex_format, writefile,
523 // \include don't want extension and demands that the
524 // file really have .tex
525 incfile = changeExtension(incfile, string());
526 incfile = latex_path(incfile);
528 os << '\\' << from_ascii(params().getCmdName()) << '{'
529 << from_utf8(incfile) << '}';
536 int InsetInclude::plaintext(Buffer const & buffer, odocstream & os,
537 OutputParams const &) const
539 if (isVerbatim(params()) || isListings(params())) {
540 os << '[' << getScreenLabel(buffer) << '\n';
541 // FIXME: We don't know the encoding of the file, default to UTF-8.
542 os << includedFilename(buffer, params()).fileContents("UTF-8");
544 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
546 docstring const str = '[' + getScreenLabel(buffer) + ']';
553 int InsetInclude::docbook(Buffer const & buffer, odocstream & os,
554 OutputParams const & runparams) const
556 string incfile = to_utf8(params()["filename"]);
558 // Do nothing if no file name has been specified
562 string const included_file = includedFilename(buffer, params()).absFilename();
564 //Check we're not trying to include ourselves.
565 //FIXME RECURSIVE INCLUDE
566 //This isn't sufficient, as the inclusion could be downstream.
567 //But it'll have to do for now.
568 if (buffer.absFileName() == included_file) {
569 Alert::error(_("Recursive input"),
570 bformat(_("Attempted to include file %1$s in itself! "
571 "Ignoring inclusion."), from_utf8(incfile)));
575 // write it to a file (so far the complete file)
576 string const exportfile = changeExtension(incfile, ".sgml");
577 DocFileName writefile(changeExtension(included_file, ".sgml"));
579 if (loadIfNeeded(buffer, params())) {
580 Buffer * tmp = theBufferList().getBuffer(included_file);
582 string const mangled = writefile.mangledFilename();
583 writefile = makeAbsPath(mangled,
584 buffer.masterBuffer()->temppath());
588 LYXERR(Debug::LATEX, "incfile:" << incfile);
589 LYXERR(Debug::LATEX, "exportfile:" << exportfile);
590 LYXERR(Debug::LATEX, "writefile:" << writefile);
592 tmp->makeDocBookFile(writefile, runparams, true);
595 runparams.exportdata->addExternalFile("docbook", writefile,
597 runparams.exportdata->addExternalFile("docbook-xml", writefile,
600 if (isVerbatim(params()) || isListings(params())) {
601 os << "<inlinegraphic fileref=\""
602 << '&' << include_label << ';'
603 << "\" format=\"linespecific\">";
605 os << '&' << include_label << ';';
611 void InsetInclude::validate(LaTeXFeatures & features) const
613 string incfile = to_utf8(params()["filename"]);
616 Buffer const & buffer = features.buffer();
618 string const included_file = includedFilename(buffer, params()).absFilename();
620 if (isLyXFilename(included_file))
621 writefile = changeExtension(included_file, ".sgml");
623 writefile = included_file;
625 if (!features.runparams().nice && !isVerbatim(params()) && !isListings(params())) {
626 incfile = DocFileName(writefile).mangledFilename();
627 writefile = makeAbsPath(incfile,
628 buffer.masterBuffer()->temppath()).absFilename();
631 features.includeFile(include_label, writefile);
633 if (isVerbatim(params()))
634 features.require("verbatim");
635 else if (isListings(params()))
636 features.require("listings");
638 // Here we must do the fun stuff...
639 // Load the file in the include if it needs
641 if (loadIfNeeded(buffer, params())) {
643 Buffer * const tmp = theBufferList().getBuffer(included_file);
644 // make sure the buffer isn't us
645 // FIXME RECURSIVE INCLUDES
646 // This is not sufficient, as recursive includes could be
647 // more than a file away. But it will do for now.
648 if (tmp && tmp != & buffer) {
649 // We must temporarily change features.buffer,
650 // otherwise it would always be the master buffer,
651 // and nested includes would not work.
652 features.setBuffer(*tmp);
653 tmp->validate(features);
654 features.setBuffer(buffer);
660 void InsetInclude::getLabelList(Buffer const & buffer,
661 vector<docstring> & list) const
663 if (isListings(params())) {
664 InsetListingsParams p(to_utf8(params()["lstparams"]));
665 string label = p.getParamValue("label");
667 list.push_back(from_utf8(label));
669 else if (loadIfNeeded(buffer, params())) {
670 string const included_file = includedFilename(buffer, params()).absFilename();
671 Buffer * tmp = theBufferList().getBuffer(included_file);
673 tmp->getLabelList(list);
674 tmp->setParent(const_cast<Buffer *>(&buffer));
679 void InsetInclude::fillWithBibKeys(Buffer const & buffer,
680 BiblioInfo & keys, InsetIterator const & /*di*/) const
682 if (loadIfNeeded(buffer, params())) {
683 string const included_file = includedFilename(buffer, params()).absFilename();
684 Buffer * tmp = theBufferList().getBuffer(included_file);
685 //FIXME This is kind of a dirty hack and should be made reasonable.
687 keys.fillWithBibKeys(tmp);
688 tmp->setParent(&buffer);
693 void InsetInclude::updateBibfilesCache(Buffer const & buffer)
695 Buffer * const tmp = getChildBuffer(buffer, params());
698 tmp->updateBibfilesCache();
699 tmp->setParent(&buffer);
705 InsetInclude::getBibfilesCache(Buffer const & buffer) const
707 Buffer * const tmp = getChildBuffer(buffer, params());
710 FileNameList const & cache = tmp->getBibfilesCache();
711 tmp->setParent(&buffer);
714 static FileNameList const empty;
719 void InsetInclude::metrics(MetricsInfo & mi, Dimension & dim) const
721 BOOST_ASSERT(mi.base.bv);
723 bool use_preview = false;
724 if (RenderPreview::status() != LyXRC::PREVIEW_OFF) {
725 graphics::PreviewImage const * pimage =
726 preview_->getPreviewImage(mi.base.bv->buffer());
727 use_preview = pimage && pimage->image();
731 preview_->metrics(mi, dim);
735 button_.update(getScreenLabel(mi.base.bv->buffer()),
738 button_.metrics(mi, dim);
741 Box b(0, dim.wid, -dim.asc, dim.des);
746 void InsetInclude::draw(PainterInfo & pi, int x, int y) const
748 BOOST_ASSERT(pi.base.bv);
750 bool use_preview = false;
751 if (RenderPreview::status() != LyXRC::PREVIEW_OFF) {
752 graphics::PreviewImage const * pimage =
753 preview_->getPreviewImage(pi.base.bv->buffer());
754 use_preview = pimage && pimage->image();
758 preview_->draw(pi, x, y);
760 button_.draw(pi, x, y);
764 Inset::DisplayType InsetInclude::display() const
766 return type(params()) == INPUT ? Inline : AlignCenter;
775 void InsetInclude::fileChanged() const
777 Buffer const * const buffer = updateFrontend();
781 preview_->removePreview(*buffer);
782 add_preview(*preview_.get(), *this, *buffer);
783 preview_->startLoading(*buffer);
789 bool preview_wanted(InsetCommandParams const & params, Buffer const & buffer)
791 FileName const included_file = includedFilename(buffer, params);
793 return type(params) == INPUT && params.preview() &&
794 included_file.isReadableFile();
798 docstring const latex_string(InsetInclude const & inset, Buffer const & buffer)
801 // We don't need to set runparams.encoding since this will be done
802 // by latex() anyway.
803 OutputParams runparams(0);
804 runparams.flavor = OutputParams::LATEX;
805 inset.latex(buffer, os, runparams);
811 void add_preview(RenderMonitoredPreview & renderer, InsetInclude const & inset,
812 Buffer const & buffer)
814 InsetCommandParams const & params = inset.params();
815 if (RenderPreview::status() != LyXRC::PREVIEW_OFF &&
816 preview_wanted(params, buffer)) {
817 renderer.setAbsFile(includedFilename(buffer, params));
818 docstring const snippet = latex_string(inset, buffer);
819 renderer.addPreview(snippet, buffer);
826 void InsetInclude::addPreview(graphics::PreviewLoader & ploader) const
828 Buffer const & buffer = ploader.buffer();
829 if (preview_wanted(params(), buffer)) {
830 preview_->setAbsFile(includedFilename(buffer, params()));
831 docstring const snippet = latex_string(*this, buffer);
832 preview_->addPreview(snippet, ploader);
837 void InsetInclude::addToToc(TocList & toclist, Buffer const & buffer,
838 ParConstIterator const & pit) const
840 if (isListings(params())) {
841 InsetListingsParams p(to_utf8(params()["lstparams"]));
842 string caption = p.getParamValue("caption");
845 Toc & toc = toclist["listing"];
846 docstring const str = convert<docstring>(toc.size() + 1)
847 + ". " + from_utf8(caption);
848 // This inset does not have a valid ParConstIterator
849 // so it has to use the iterator of its parent paragraph
850 toc.push_back(TocItem(pit, 0, str));
853 Buffer const * const childbuffer = getChildBuffer(buffer, params());
857 TocList const & childtoclist = childbuffer->tocBackend().tocs();
858 TocList::const_iterator it = childtoclist.begin();
859 TocList::const_iterator const end = childtoclist.end();
860 for(; it != end; ++it)
861 toclist[it->first].insert(toclist[it->first].end(),
862 it->second.begin(), it->second.end());
866 void InsetInclude::updateLabels(Buffer const & buffer, ParIterator const &)
868 Buffer const * const childbuffer = getChildBuffer(buffer, params());
870 lyx::updateLabels(*childbuffer, true);
871 else if (isListings(params())) {
872 InsetListingsParams const par(to_utf8(params()["lstparams"]));
873 if (par.getParamValue("caption").empty())
874 listings_label_.clear();
876 Counters & counters = buffer.params().getTextClass().counters();
877 docstring const cnt = from_ascii("listing");
878 if (counters.hasCounter(cnt)) {
880 listings_label_ = buffer.B_("Program Listing ")
881 + convert<docstring>(counters.value(cnt));
883 listings_label_ = buffer.B_("Program Listing");
889 void InsetInclude::registerEmbeddedFiles(Buffer const & buffer,
890 EmbeddedFiles & files) const
892 // include and input are temprarily not considered.
894 if (isVerbatim(params()) || isListings(params()))
895 files.registerFile(includedFilename(buffer, params()).absFilename(),