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)
8 * \author Bo Peng (embedding stuff)
10 * Full author contact details are available in file CREDITS.
15 #include "InsetInclude.h"
17 #include "BaseClassList.h"
19 #include "buffer_funcs.h"
20 #include "BufferList.h"
21 #include "BufferParams.h"
22 #include "BufferView.h"
24 #include "DispatchResult.h"
25 #include "ErrorList.h"
27 #include "FuncRequest.h"
28 #include "FuncStatus.h"
29 #include "LaTeXFeatures.h"
33 #include "MetricsInfo.h"
34 #include "OutputParams.h"
35 #include "TextClass.h"
36 #include "TocBackend.h"
38 #include "frontends/alert.h"
39 #include "frontends/Painter.h"
41 #include "graphics/PreviewImage.h"
42 #include "graphics/PreviewLoader.h"
44 #include "insets/RenderPreview.h"
45 #include "insets/InsetListingsParams.h"
47 #include "support/debug.h"
48 #include "support/docstream.h"
49 #include "support/ExceptionMessage.h"
50 #include "support/FileNameList.h"
51 #include "support/filetools.h"
52 #include "support/gettext.h"
53 #include "support/lstrings.h" // contains
54 #include "support/lyxalgo.h"
55 #include "support/convert.h"
57 #include <boost/bind.hpp>
60 using namespace lyx::support;
64 namespace Alert = frontend::Alert;
69 docstring const uniqueID()
71 static unsigned int seed = 1000;
72 return "file" + convert<docstring>(++seed);
76 /// the type of inclusion
78 INCLUDE, VERB, INPUT, VERBAST, LISTINGS, NONE
82 Types type(string const & s)
86 if (s == "verbatiminput")
88 if (s == "verbatiminput*")
90 if (s == "lstinputlisting")
98 Types type(InsetCommandParams const & params)
100 return type(params.getCmdName());
104 bool isListings(InsetCommandParams const & params)
106 return type(params) == LISTINGS;
110 bool isVerbatim(InsetCommandParams const & params)
112 Types const t = type(params);
113 return t == VERB || t == VERBAST;
117 bool isInputOrInclude(InsetCommandParams const & params)
119 Types const t = type(params);
120 return t == INPUT || t == INCLUDE;
124 FileName const masterFileName(Buffer const & buffer)
126 return buffer.masterBuffer()->fileName();
130 void add_preview(RenderMonitoredPreview &, InsetInclude const &, Buffer const &);
133 string const parentFilename(Buffer const & buffer)
135 return buffer.absFileName();
139 EmbeddedFile const includedFilename(Buffer const & buffer,
140 InsetCommandParams const & params)
142 // it is not a good idea to create this EmbeddedFile object
143 // each time, but there seems to be no easy way around.
144 EmbeddedFile file(to_utf8(params["filename"]),
145 onlyPath(parentFilename(buffer)));
146 file.setEmbed(!params["embed"].empty());
147 file.enable(buffer.embedded(), &buffer);
154 InsetInclude::InsetInclude(InsetCommandParams const & p)
155 : InsetCommand(p, "include"), include_label(uniqueID()),
156 preview_(new RenderMonitoredPreview(this)), set_label_(false)
158 preview_->fileChanged(boost::bind(&InsetInclude::fileChanged, this));
162 InsetInclude::InsetInclude(InsetInclude const & other)
163 : InsetCommand(other), include_label(other.include_label),
164 preview_(new RenderMonitoredPreview(this)), set_label_(false)
166 preview_->fileChanged(boost::bind(&InsetInclude::fileChanged, this));
170 InsetInclude::~InsetInclude()
172 if (isVerbatim(params()) || isListings(params()))
176 string const parent_filename = buffer().absFileName();
177 FileName const included_file = makeAbsPath(to_utf8(params()["filename"]),
178 onlyPath(parent_filename));
180 if (!isLyXFilename(included_file.absFilename()))
183 Buffer * child = theBufferList().getBuffer(included_file.absFilename());
184 // File not opened, nothing to close.
188 // Child document has a different parent, don't close it.
189 if (child->parent() != &buffer())
193 theBufferList().release(child);
197 ParamInfo const & InsetInclude::findInfo(string const & /* cmdName */)
200 // This is only correct for the case of listings, but it'll do for now.
201 // In the other cases, this second parameter should just be empty.
202 static ParamInfo param_info_;
203 if (param_info_.empty()) {
204 param_info_.add("filename", ParamInfo::LATEX_REQUIRED);
205 param_info_.add("lstparams", ParamInfo::LATEX_OPTIONAL);
206 param_info_.add("embed", ParamInfo::LYX_INTERNAL);
212 bool InsetInclude::isCompatibleCommand(string const & s)
214 return type(s) != NONE;
218 void InsetInclude::doDispatch(Cursor & cur, FuncRequest & cmd)
220 BOOST_ASSERT(&cur.buffer() == &buffer());
221 switch (cmd.action) {
223 case LFUN_INSET_MODIFY: {
224 InsetCommandParams p(INCLUDE_CODE);
225 InsetCommandMailer::string2params("include", to_utf8(cmd.argument()), p);
226 if (!p.getCmdName().empty()) {
228 InsetListingsParams par_old(to_utf8(params()["lstparams"]));
229 InsetListingsParams par_new(to_utf8(p["lstparams"]));
230 if (par_old.getParamValue("label") !=
231 par_new.getParamValue("label")
232 && !par_new.getParamValue("label").empty())
233 cur.bv().buffer().changeRefsIfUnique(
234 from_utf8(par_old.getParamValue("label")),
235 from_utf8(par_new.getParamValue("label")),
239 // the embed parameter passed back from the dialog
240 // is "true" or "false", we need to change it.
241 if (p["embed"] == _("false"))
244 p["embed"] = from_utf8(EmbeddedFile(to_utf8(p["filename"]),
245 onlyPath(parentFilename(cur.buffer()))).inzipName());
247 includedFilename(cur.buffer(), p);
248 } catch (ExceptionMessage const & message) {
249 Alert::error(message.title_, message.details_);
250 // do not set parameter if an error happens
254 buffer().updateBibfilesCache();
260 //pass everything else up the chain
262 InsetCommand::doDispatch(cur, cmd);
268 void InsetInclude::setParams(InsetCommandParams const & p)
270 InsetCommand::setParams(p);
273 if (preview_->monitoring())
274 preview_->stopMonitoring();
276 if (type(params()) == INPUT)
277 add_preview(*preview_, *this, buffer());
281 docstring InsetInclude::screenLabel() const
285 switch (type(params())) {
287 temp = buffer().B_("Input");
290 temp = buffer().B_("Verbatim Input");
293 temp = buffer().B_("Verbatim Input*");
296 temp = buffer().B_("Include");
299 temp = listings_label_;
307 if (params()["filename"].empty())
310 temp += from_utf8(onlyFilename(to_utf8(params()["filename"])));
312 if (!params()["embed"].empty())
313 temp += _(" (embedded)");
318 /// return the child buffer if the file is a LyX doc and is loaded
319 Buffer * getChildBuffer(Buffer const & buffer, InsetCommandParams const & params)
321 if (isVerbatim(params) || isListings(params))
324 string const included_file = includedFilename(buffer, params).absFilename();
325 if (!isLyXFilename(included_file))
328 Buffer * childBuffer = loadIfNeeded(buffer, params);
330 // FIXME: recursive includes
331 return (childBuffer == &buffer) ? 0 : childBuffer;
335 /// return true if the file is or got loaded.
336 Buffer * loadIfNeeded(Buffer const & parent, InsetCommandParams const & params)
338 if (isVerbatim(params) || isListings(params))
341 string const parent_filename = parent.absFileName();
342 FileName const included_file = makeAbsPath(to_utf8(params["filename"]),
343 onlyPath(parent_filename));
345 if (!isLyXFilename(included_file.absFilename()))
348 Buffer * child = theBufferList().getBuffer(included_file.absFilename());
350 // the readonly flag can/will be wrong, not anymore I think.
351 if (!included_file.exists())
354 child = theBufferList().newBuffer(included_file.absFilename());
356 // Buffer creation is not possible.
359 if (!child->loadLyXFile(included_file)) {
360 //close the buffer we just opened
361 theBufferList().release(child);
365 if (!child->errorList("Parse").empty()) {
366 // FIXME: Do something.
369 child->setParent(&parent);
374 int InsetInclude::latex(odocstream & os, OutputParams const & runparams) const
376 string incfile = to_utf8(params()["filename"]);
378 // Do nothing if no file name has been specified
382 FileName const included_file =
383 includedFilename(buffer(), params()).availableFile();
385 // Check we're not trying to include ourselves.
386 // FIXME RECURSIVE INCLUDE
387 // This isn't sufficient, as the inclusion could be downstream.
388 // But it'll have to do for now.
389 if (isInputOrInclude(params()) &&
390 buffer().absFileName() == included_file.absFilename())
392 Alert::error(_("Recursive input"),
393 bformat(_("Attempted to include file %1$s in itself! "
394 "Ignoring inclusion."), from_utf8(incfile)));
398 Buffer const * const masterBuffer = buffer().masterBuffer();
400 // if incfile is relative, make it relative to the master
402 if (!FileName(incfile).isAbsolute()) {
404 incfile = to_utf8(makeRelPath(from_utf8(included_file.absFilename()),
405 from_utf8(masterBuffer->filePath())));
408 // write it to a file (so far the complete file)
409 string const exportfile = changeExtension(incfile, ".tex");
410 string const mangled =
411 DocFileName(changeExtension(included_file.absFilename(),".tex")).
413 FileName const writefile(makeAbsPath(mangled, masterBuffer->temppath()));
417 else if (!isValidLaTeXFilename(incfile)) {
418 frontend::Alert::warning(_("Invalid filename"),
419 _("The following filename is likely to cause trouble "
420 "when running the exported file through LaTeX: ") +
423 LYXERR(Debug::LATEX, "incfile:" << incfile);
424 LYXERR(Debug::LATEX, "exportfile:" << exportfile);
425 LYXERR(Debug::LATEX, "writefile:" << writefile);
427 if (runparams.inComment || runparams.dryrun) {
428 //Don't try to load or copy the file if we're
429 //in a comment or doing a dryrun
430 } else if (isInputOrInclude(params()) &&
431 isLyXFilename(included_file.absFilename())) {
432 //if it's a LyX file and we're inputting or including,
433 //try to load it so we can write the associated latex
434 if (!loadIfNeeded(buffer(), params()))
437 Buffer * tmp = theBufferList().getBuffer(included_file.absFilename());
439 if (tmp->params().baseClass() != masterBuffer->params().baseClass()) {
441 docstring text = bformat(_("Included file `%1$s'\n"
442 "has textclass `%2$s'\n"
443 "while parent file has textclass `%3$s'."),
444 included_file.displayName(),
445 from_utf8(tmp->params().documentClass().name()),
446 from_utf8(masterBuffer->params().documentClass().name()));
447 Alert::warning(_("Different textclasses"), text);
451 // Make sure modules used in child are all included in master
452 //FIXME It might be worth loading the children's modules into the master
453 //over in BufferParams rather than doing this check.
454 vector<string> const masterModules = masterBuffer->params().getModules();
455 vector<string> const childModules = tmp->params().getModules();
456 vector<string>::const_iterator it = childModules.begin();
457 vector<string>::const_iterator end = childModules.end();
458 for (; it != end; ++it) {
459 string const module = *it;
460 vector<string>::const_iterator found =
461 find(masterModules.begin(), masterModules.end(), module);
462 if (found != masterModules.end()) {
463 docstring text = bformat(_("Included file `%1$s'\n"
464 "uses module `%2$s'\n"
465 "which is not used in parent file."),
466 included_file.displayName(), from_utf8(module));
467 Alert::warning(_("Module not found"), text);
471 tmp->markDepClean(masterBuffer->temppath());
473 // FIXME: handle non existing files
474 // FIXME: Second argument is irrelevant!
475 // since only_body is true, makeLaTeXFile will not look at second
476 // argument. Should we set it to string(), or should makeLaTeXFile
477 // make use of it somehow? (JMarc 20031002)
478 // The included file might be written in a different encoding
479 Encoding const * const oldEnc = runparams.encoding;
480 runparams.encoding = &tmp->params().encoding();
481 tmp->makeLaTeXFile(writefile,
482 masterFileName(buffer()).onlyPath().absFilename(),
484 runparams.encoding = oldEnc;
486 // In this case, it's not a LyX file, so we copy the file
487 // to the temp dir, so that .aux files etc. are not created
488 // in the original dir. Files included by this file will be
489 // found via input@path, see ../Buffer.cpp.
490 unsigned long const checksum_in = included_file.checksum();
491 unsigned long const checksum_out = writefile.checksum();
493 if (checksum_in != checksum_out) {
494 if (!included_file.copyTo(writefile)) {
497 to_utf8(bformat(_("Could not copy the file\n%1$s\n"
498 "into the temporary directory."),
499 from_utf8(included_file.absFilename()))));
505 string const tex_format = (runparams.flavor == OutputParams::LATEX) ?
506 "latex" : "pdflatex";
507 if (isVerbatim(params())) {
508 incfile = latex_path(incfile);
510 os << '\\' << from_ascii(params().getCmdName()) << '{'
511 << from_utf8(incfile) << '}';
512 } else if (type(params()) == INPUT) {
513 runparams.exportdata->addExternalFile(tex_format, writefile,
516 // \input wants file with extension (default is .tex)
517 if (!isLyXFilename(included_file.absFilename())) {
518 incfile = latex_path(incfile);
520 os << '\\' << from_ascii(params().getCmdName())
521 << '{' << from_utf8(incfile) << '}';
523 incfile = changeExtension(incfile, ".tex");
524 incfile = latex_path(incfile);
526 os << '\\' << from_ascii(params().getCmdName())
527 << '{' << from_utf8(incfile) << '}';
529 } else if (type(params()) == LISTINGS) {
530 os << '\\' << from_ascii(params().getCmdName());
531 string const opt = to_utf8(params()["lstparams"]);
532 // opt is set in QInclude dialog and should have passed validation.
533 InsetListingsParams params(opt);
534 if (!params.params().empty())
535 os << "[" << from_utf8(params.params()) << "]";
536 os << '{' << from_utf8(incfile) << '}';
538 runparams.exportdata->addExternalFile(tex_format, writefile,
541 // \include don't want extension and demands that the
542 // file really have .tex
543 incfile = changeExtension(incfile, string());
544 incfile = latex_path(incfile);
546 os << '\\' << from_ascii(params().getCmdName()) << '{'
547 << from_utf8(incfile) << '}';
554 int InsetInclude::plaintext(odocstream & os, OutputParams const &) const
556 if (isVerbatim(params()) || isListings(params())) {
557 os << '[' << screenLabel() << '\n';
558 // FIXME: We don't know the encoding of the file, default to UTF-8.
559 os << includedFilename(buffer(), params()).fileContents("UTF-8");
561 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
563 docstring const str = '[' + screenLabel() + ']';
570 int InsetInclude::docbook(odocstream & os, OutputParams const & runparams) const
572 string incfile = to_utf8(params()["filename"]);
574 // Do nothing if no file name has been specified
578 string const included_file = includedFilename(buffer(), params()).absFilename();
580 // Check we're not trying to include ourselves.
581 // FIXME RECURSIVE INCLUDE
582 // This isn't sufficient, as the inclusion could be downstream.
583 // But it'll have to do for now.
584 if (buffer().absFileName() == included_file) {
585 Alert::error(_("Recursive input"),
586 bformat(_("Attempted to include file %1$s in itself! "
587 "Ignoring inclusion."), from_utf8(incfile)));
591 // write it to a file (so far the complete file)
592 string const exportfile = changeExtension(incfile, ".sgml");
593 DocFileName writefile(changeExtension(included_file, ".sgml"));
595 if (loadIfNeeded(buffer(), params())) {
596 Buffer * tmp = theBufferList().getBuffer(included_file);
598 string const mangled = writefile.mangledFilename();
599 writefile = makeAbsPath(mangled,
600 buffer().masterBuffer()->temppath());
604 LYXERR(Debug::LATEX, "incfile:" << incfile);
605 LYXERR(Debug::LATEX, "exportfile:" << exportfile);
606 LYXERR(Debug::LATEX, "writefile:" << writefile);
608 tmp->makeDocBookFile(writefile, runparams, true);
611 runparams.exportdata->addExternalFile("docbook", writefile,
613 runparams.exportdata->addExternalFile("docbook-xml", writefile,
616 if (isVerbatim(params()) || isListings(params())) {
617 os << "<inlinegraphic fileref=\""
618 << '&' << include_label << ';'
619 << "\" format=\"linespecific\">";
621 os << '&' << include_label << ';';
627 void InsetInclude::validate(LaTeXFeatures & features) const
629 string incfile = to_utf8(params()["filename"]);
632 BOOST_ASSERT(&buffer() == &features.buffer());
634 string const included_file =
635 includedFilename(buffer(), params()).availableFile().absFilename();
637 if (isLyXFilename(included_file))
638 writefile = changeExtension(included_file, ".sgml");
640 writefile = included_file;
642 if (!features.runparams().nice && !isVerbatim(params()) && !isListings(params())) {
643 incfile = DocFileName(writefile).mangledFilename();
644 writefile = makeAbsPath(incfile,
645 buffer().masterBuffer()->temppath()).absFilename();
648 features.includeFile(include_label, writefile);
650 if (isVerbatim(params()))
651 features.require("verbatim");
652 else if (isListings(params()))
653 features.require("listings");
655 // Here we must do the fun stuff...
656 // Load the file in the include if it needs
658 if (loadIfNeeded(buffer(), params())) {
660 Buffer * const tmp = theBufferList().getBuffer(included_file);
661 // make sure the buffer isn't us
662 // FIXME RECURSIVE INCLUDES
663 // This is not sufficient, as recursive includes could be
664 // more than a file away. But it will do for now.
665 if (tmp && tmp != &buffer()) {
666 // We must temporarily change features.buffer,
667 // otherwise it would always be the master buffer,
668 // and nested includes would not work.
669 features.setBuffer(*tmp);
670 tmp->validate(features);
671 features.setBuffer(buffer());
677 void InsetInclude::getLabelList(vector<docstring> & list) const
679 if (isListings(params())) {
680 InsetListingsParams p(to_utf8(params()["lstparams"]));
681 string label = p.getParamValue("label");
683 list.push_back(from_utf8(label));
685 else if (loadIfNeeded(buffer(), params())) {
686 string const included_file = includedFilename(buffer(), params()).absFilename();
687 Buffer * tmp = theBufferList().getBuffer(included_file);
689 tmp->getLabelList(list);
690 tmp->setParent(const_cast<Buffer *>(&buffer()));
695 void InsetInclude::fillWithBibKeys(BiblioInfo & keys,
696 InsetIterator const & /*di*/) const
698 if (loadIfNeeded(buffer(), params())) {
699 string const included_file = includedFilename(buffer(), params()).absFilename();
700 Buffer * tmp = theBufferList().getBuffer(included_file);
701 //FIXME This is kind of a dirty hack and should be made reasonable.
703 keys.fillWithBibKeys(tmp);
704 tmp->setParent(&buffer());
709 void InsetInclude::updateBibfilesCache()
711 Buffer * const tmp = getChildBuffer(buffer(), params());
714 tmp->updateBibfilesCache();
715 tmp->setParent(&buffer());
720 EmbeddedFileList const &
721 InsetInclude::getBibfilesCache(Buffer const & buffer) const
723 Buffer * const tmp = getChildBuffer(buffer, params());
726 EmbeddedFileList const & cache = tmp->getBibfilesCache();
727 tmp->setParent(&buffer);
730 static EmbeddedFileList const empty;
735 void InsetInclude::metrics(MetricsInfo & mi, Dimension & dim) const
737 BOOST_ASSERT(mi.base.bv);
739 bool use_preview = false;
740 if (RenderPreview::status() != LyXRC::PREVIEW_OFF) {
741 graphics::PreviewImage const * pimage =
742 preview_->getPreviewImage(mi.base.bv->buffer());
743 use_preview = pimage && pimage->image();
747 preview_->metrics(mi, dim);
751 button_.update(screenLabel(), true);
753 button_.metrics(mi, dim);
756 Box b(0, dim.wid, -dim.asc, dim.des);
761 void InsetInclude::draw(PainterInfo & pi, int x, int y) const
763 BOOST_ASSERT(pi.base.bv);
765 bool use_preview = false;
766 if (RenderPreview::status() != LyXRC::PREVIEW_OFF) {
767 graphics::PreviewImage const * pimage =
768 preview_->getPreviewImage(pi.base.bv->buffer());
769 use_preview = pimage && pimage->image();
773 preview_->draw(pi, x, y);
775 button_.draw(pi, x, y);
779 Inset::DisplayType InsetInclude::display() const
781 return type(params()) == INPUT ? Inline : AlignCenter;
790 void InsetInclude::fileChanged() const
792 Buffer const * const buffer = updateFrontend();
796 preview_->removePreview(*buffer);
797 add_preview(*preview_.get(), *this, *buffer);
798 preview_->startLoading(*buffer);
804 bool preview_wanted(InsetCommandParams const & params, Buffer const & buffer)
806 FileName const included_file = includedFilename(buffer, params);
808 return type(params) == INPUT && params.preview() &&
809 included_file.isReadableFile();
813 docstring latexString(InsetInclude const & inset)
816 // We don't need to set runparams.encoding since this will be done
817 // by latex() anyway.
818 OutputParams runparams(0);
819 runparams.flavor = OutputParams::LATEX;
820 inset.latex(os, runparams);
826 void add_preview(RenderMonitoredPreview & renderer, InsetInclude const & inset,
827 Buffer const & buffer)
829 InsetCommandParams const & params = inset.params();
830 if (RenderPreview::status() != LyXRC::PREVIEW_OFF &&
831 preview_wanted(params, buffer)) {
832 renderer.setAbsFile(includedFilename(buffer, params));
833 docstring const snippet = latexString(inset);
834 renderer.addPreview(snippet, buffer);
841 void InsetInclude::addPreview(graphics::PreviewLoader & ploader) const
843 Buffer const & buffer = ploader.buffer();
844 if (!preview_wanted(params(), buffer))
846 preview_->setAbsFile(includedFilename(buffer, params()));
847 docstring const snippet = latexString(*this);
848 preview_->addPreview(snippet, ploader);
852 void InsetInclude::addToToc(ParConstIterator const & cpit) const
854 if (isListings(params())) {
855 InsetListingsParams p(to_utf8(params()["lstparams"]));
856 string caption = p.getParamValue("caption");
859 Toc & toc = buffer().tocBackend().toc("listing");
860 docstring const str = convert<docstring>(toc.size() + 1)
861 + ". " + from_utf8(caption);
862 ParConstIterator pit = cpit;
863 pit.push_back(*this);
864 toc.push_back(TocItem(pit, 0, str));
867 Buffer const * const childbuffer = getChildBuffer(buffer(), params());
871 TocList & toclist = buffer().tocBackend().tocs();
872 TocList const & childtoclist = childbuffer->tocBackend().tocs();
873 TocList::const_iterator it = childtoclist.begin();
874 TocList::const_iterator const end = childtoclist.end();
875 for(; it != end; ++it)
876 toclist[it->first].insert(toclist[it->first].end(),
877 it->second.begin(), it->second.end());
881 void InsetInclude::updateLabels(ParIterator const &)
883 Buffer const * const childbuffer = getChildBuffer(buffer(), params());
885 lyx::updateLabels(*childbuffer, true);
888 if (!isListings(params()))
891 InsetListingsParams const par(to_utf8(params()["lstparams"]));
892 if (par.getParamValue("caption").empty()) {
893 listings_label_.clear();
896 Counters & counters = buffer().params().documentClass().counters();
897 docstring const cnt = from_ascii("listing");
898 listings_label_ = buffer().B_("Program Listing");
899 if (counters.hasCounter(cnt)) {
901 listings_label_ += " " + convert<docstring>(counters.value(cnt));
906 void InsetInclude::registerEmbeddedFiles(EmbeddedFileList & files) const
908 files.registerFile(includedFilename(buffer(), params()), this, buffer());
912 void InsetInclude::updateEmbeddedFile(EmbeddedFile const & file)
914 InsetCommandParams p = params();
915 p["filename"] = from_utf8(file.outputFilename());
916 p["embed"] = file.embedded() ? from_utf8(file.inzipName()) : docstring();