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"
18 #include "buffer_funcs.h"
19 #include "BufferList.h"
20 #include "BufferParams.h"
21 #include "BufferView.h"
23 #include "DispatchResult.h"
25 #include "FuncRequest.h"
26 #include "FuncStatus.h"
27 #include "LaTeXFeatures.h"
31 #include "MetricsInfo.h"
32 #include "OutputParams.h"
33 #include "TextClass.h"
34 #include "TocBackend.h"
36 #include "frontends/alert.h"
37 #include "frontends/Painter.h"
39 #include "graphics/PreviewImage.h"
40 #include "graphics/PreviewLoader.h"
42 #include "insets/RenderPreview.h"
43 #include "insets/InsetListingsParams.h"
45 #include "support/debug.h"
46 #include "support/docstream.h"
47 #include "support/ExceptionMessage.h"
48 #include "support/FileNameList.h"
49 #include "support/filetools.h"
50 #include "support/gettext.h"
51 #include "support/lstrings.h" // contains
52 #include "support/lyxalgo.h"
53 #include "support/convert.h"
55 #include <boost/bind.hpp>
58 using namespace lyx::support;
62 namespace Alert = frontend::Alert;
67 docstring const uniqueID()
69 static unsigned int seed = 1000;
70 return "file" + convert<docstring>(++seed);
74 /// the type of inclusion
76 INCLUDE, VERB, INPUT, VERBAST, LISTINGS, NONE
80 Types type(string const & s)
84 if (s == "verbatiminput")
86 if (s == "verbatiminput*")
88 if (s == "lstinputlisting")
96 Types type(InsetCommandParams const & params)
98 return type(params.getCmdName());
102 bool isListings(InsetCommandParams const & params)
104 return type(params) == LISTINGS;
108 bool isVerbatim(InsetCommandParams const & params)
110 Types const t = type(params);
111 return t == VERB || t == VERBAST;
115 bool isInputOrInclude(InsetCommandParams const & params)
117 Types const t = type(params);
118 return t == INPUT || t == INCLUDE;
122 FileName const masterFileName(Buffer const & buffer)
124 return buffer.masterBuffer()->fileName();
128 void add_preview(RenderMonitoredPreview &, InsetInclude const &, Buffer const &);
131 string const parentFilename(Buffer const & buffer)
133 return buffer.absFileName();
137 EmbeddedFile const includedFilename(Buffer const & buffer,
138 InsetCommandParams const & params)
140 // it is not a good idea to create this EmbeddedFile object
141 // each time, but there seems to be no easy way around.
142 EmbeddedFile file(to_utf8(params["filename"]),
143 onlyPath(parentFilename(buffer)));
144 file.setEmbed(!params["embed"].empty());
145 file.enable(buffer.embedded(), &buffer);
152 InsetInclude::InsetInclude(InsetCommandParams const & p)
153 : InsetCommand(p, "include"), include_label(uniqueID()),
154 preview_(new RenderMonitoredPreview(this)), set_label_(false)
156 preview_->fileChanged(boost::bind(&InsetInclude::fileChanged, this));
160 InsetInclude::InsetInclude(InsetInclude const & other)
161 : InsetCommand(other), include_label(other.include_label),
162 preview_(new RenderMonitoredPreview(this)), set_label_(false)
164 preview_->fileChanged(boost::bind(&InsetInclude::fileChanged, this));
168 ParamInfo const & InsetInclude::findInfo(string const & /* cmdName */)
171 // This is only correct for the case of listings, but it'll do for now.
172 // In the other cases, this second parameter should just be empty.
173 static ParamInfo param_info_;
174 if (param_info_.empty()) {
175 param_info_.add("filename", false);
176 param_info_.add("embed", false);
177 param_info_.add("lstparams", true);
183 bool InsetInclude::isCompatibleCommand(string const & s)
185 return type(s) != NONE;
189 void InsetInclude::doDispatch(Cursor & cur, FuncRequest & cmd)
191 switch (cmd.action) {
193 case LFUN_INSET_MODIFY: {
194 InsetCommandParams p(INCLUDE_CODE);
195 InsetCommandMailer::string2params("include", to_utf8(cmd.argument()), p);
196 if (!p.getCmdName().empty()) {
198 InsetListingsParams par_old(to_utf8(params()["lstparams"]));
199 InsetListingsParams par_new(to_utf8(p["lstparams"]));
200 if (par_old.getParamValue("label") !=
201 par_new.getParamValue("label")
202 && !par_new.getParamValue("label").empty())
203 cur.bv().buffer().changeRefsIfUnique(
204 from_utf8(par_old.getParamValue("label")),
205 from_utf8(par_new.getParamValue("label")),
209 // the embed parameter passed back from the dialog
210 // is "true" or "false", we need to change it.
211 if (p["embed"] == _("false"))
214 p["embed"] = from_utf8(EmbeddedFile(to_utf8(p["filename"]),
215 onlyPath(parentFilename(cur.buffer()))).inzipName());
217 includedFilename(cur.buffer(), p);
218 } catch (ExceptionMessage const & message) {
219 Alert::error(message.title_, message.details_);
220 // do not set parameter if an error happens
223 set(p, cur.buffer());
224 cur.buffer().updateBibfilesCache();
230 //pass everything else up the chain
232 InsetCommand::doDispatch(cur, cmd);
238 void InsetInclude::set(InsetCommandParams const & p, Buffer const & buffer)
243 if (preview_->monitoring())
244 preview_->stopMonitoring();
246 if (type(params()) == INPUT)
247 add_preview(*preview_, *this, buffer);
251 Inset * InsetInclude::clone() const
253 return new InsetInclude(*this);
257 docstring const InsetInclude::getScreenLabel(Buffer const & buf) const
261 switch (type(params())) {
263 temp = buf.B_("Input");
266 temp = buf.B_("Verbatim Input");
269 temp = buf.B_("Verbatim Input*");
272 temp = buf.B_("Include");
275 temp = listings_label_;
283 if (params()["filename"].empty())
286 temp += from_utf8(onlyFilename(to_utf8(params()["filename"])));
288 if (!params()["embed"].empty())
289 temp += _(" (embedded)");
294 /// return the child buffer if the file is a LyX doc and is loaded
295 Buffer * getChildBuffer(Buffer const & buffer, InsetCommandParams const & params)
297 if (isVerbatim(params) || isListings(params))
300 string const included_file = includedFilename(buffer, params).absFilename();
301 if (!isLyXFilename(included_file))
304 Buffer * childBuffer = theBufferList().getBuffer(included_file);
306 //FIXME RECURSIVE INCLUDES
307 if (childBuffer == & buffer)
313 /// return true if the file is or got loaded.
314 Buffer * loadIfNeeded(Buffer const & parent, InsetCommandParams const & params)
316 if (isVerbatim(params) || isListings(params))
319 string const parent_filename = parent.absFileName();
320 FileName const included_file = makeAbsPath(to_utf8(params["filename"]),
321 onlyPath(parent_filename));
323 if (!isLyXFilename(included_file.absFilename()))
326 Buffer * child = theBufferList().getBuffer(included_file.absFilename());
328 // the readonly flag can/will be wrong, not anymore I think.
329 if (!included_file.exists())
332 child = theBufferList().newBuffer(included_file.absFilename());
334 // Buffer creation is not possible.
337 if (!child->loadLyXFile(included_file)) {
338 //close the buffer we just opened
339 theBufferList().release(child);
343 child->setParent(&parent);
348 void resetParentBuffer(Buffer const * parent, InsetCommandParams const & params,
351 if (isVerbatim(params) || isListings(params))
354 string const parent_filename = parent->absFileName();
355 FileName const included_file = makeAbsPath(to_utf8(params["filename"]),
356 onlyPath(parent_filename));
358 if (!isLyXFilename(included_file.absFilename()))
361 Buffer * child = theBufferList().getBuffer(included_file.absFilename());
362 // File not opened, nothing to close.
366 // Child document has a different parent, don't close it.
367 if (child->parent() != parent)
373 theBufferList().release(child);
375 updateLabels(*child);
379 int InsetInclude::latex(Buffer const & buffer, odocstream & os,
380 OutputParams const & runparams) const
382 string incfile(to_utf8(params()["filename"]));
384 // Do nothing if no file name has been specified
388 FileName const included_file = includedFilename(buffer, params()).availableFile();
390 //Check we're not trying to include ourselves.
391 //FIXME RECURSIVE INCLUDE
392 //This isn't sufficient, as the inclusion could be downstream.
393 //But it'll have to do for now.
394 if (isInputOrInclude(params()) &&
395 buffer.absFileName() == included_file.absFilename())
397 Alert::error(_("Recursive input"),
398 bformat(_("Attempted to include file %1$s in itself! "
399 "Ignoring inclusion."), from_utf8(incfile)));
403 Buffer const * const masterBuffer = buffer.masterBuffer();
405 // if incfile is relative, make it relative to the master
407 if (!FileName(incfile).isAbsolute()) {
409 incfile = to_utf8(makeRelPath(from_utf8(included_file.absFilename()),
410 from_utf8(masterBuffer->filePath())));
413 // write it to a file (so far the complete file)
414 string const exportfile = changeExtension(incfile, ".tex");
415 string const mangled =
416 DocFileName(changeExtension(included_file.absFilename(),".tex")).
418 FileName const writefile(makeAbsPath(mangled, masterBuffer->temppath()));
422 else if (!isValidLaTeXFilename(incfile)) {
423 frontend::Alert::warning(_("Invalid filename"),
424 _("The following filename is likely to cause trouble "
425 "when running the exported file through LaTeX: ") +
428 LYXERR(Debug::LATEX, "incfile:" << incfile);
429 LYXERR(Debug::LATEX, "exportfile:" << exportfile);
430 LYXERR(Debug::LATEX, "writefile:" << writefile);
432 if (runparams.inComment || runparams.dryrun) {
433 //Don't try to load or copy the file if we're
434 //in a comment or doing a dryrun
435 } else if (isInputOrInclude(params()) &&
436 isLyXFilename(included_file.absFilename())) {
437 //if it's a LyX file and we're inputting or including,
438 //try to load it so we can write the associated latex
439 if (!loadIfNeeded(buffer, params()))
442 Buffer * tmp = theBufferList().getBuffer(included_file.absFilename());
444 if (tmp->params().getBaseClass() != masterBuffer->params().getBaseClass()) {
446 docstring text = bformat(_("Included file `%1$s'\n"
447 "has textclass `%2$s'\n"
448 "while parent file has textclass `%3$s'."),
449 included_file.displayName(),
450 from_utf8(tmp->params().getTextClass().name()),
451 from_utf8(masterBuffer->params().getTextClass().name()));
452 Alert::warning(_("Different textclasses"), text);
456 // Make sure modules used in child are all included in master
457 //FIXME It might be worth loading the children's modules into the master
458 //over in BufferParams rather than doing this check.
459 vector<string> const masterModules = masterBuffer->params().getModules();
460 vector<string> const childModules = tmp->params().getModules();
461 vector<string>::const_iterator it = childModules.begin();
462 vector<string>::const_iterator end = childModules.end();
463 for (; it != end; ++it) {
464 string const module = *it;
465 vector<string>::const_iterator found =
466 find(masterModules.begin(), masterModules.end(), module);
467 if (found != masterModules.end()) {
468 docstring text = bformat(_("Included file `%1$s'\n"
469 "uses module `%2$s'\n"
470 "which is not used in parent file."),
471 included_file.displayName(), from_utf8(module));
472 Alert::warning(_("Module not found"), text);
476 tmp->markDepClean(masterBuffer->temppath());
478 // FIXME: handle non existing files
479 // FIXME: Second argument is irrelevant!
480 // since only_body is true, makeLaTeXFile will not look at second
481 // argument. Should we set it to string(), or should makeLaTeXFile
482 // make use of it somehow? (JMarc 20031002)
483 // The included file might be written in a different encoding
484 Encoding const * const oldEnc = runparams.encoding;
485 runparams.encoding = &tmp->params().encoding();
486 tmp->makeLaTeXFile(writefile,
487 masterFileName(buffer).onlyPath().absFilename(),
489 runparams.encoding = oldEnc;
491 // In this case, it's not a LyX file, so we copy the file
492 // to the temp dir, so that .aux files etc. are not created
493 // in the original dir. Files included by this file will be
494 // found via input@path, see ../Buffer.cpp.
495 unsigned long const checksum_in = included_file.checksum();
496 unsigned long const checksum_out = writefile.checksum();
498 if (checksum_in != checksum_out) {
499 if (!included_file.copyTo(writefile)) {
502 to_utf8(bformat(_("Could not copy the file\n%1$s\n"
503 "into the temporary directory."),
504 from_utf8(included_file.absFilename()))));
510 string const tex_format = (runparams.flavor == OutputParams::LATEX) ?
511 "latex" : "pdflatex";
512 if (isVerbatim(params())) {
513 incfile = latex_path(incfile);
515 os << '\\' << from_ascii(params().getCmdName()) << '{'
516 << from_utf8(incfile) << '}';
517 } else if (type(params()) == INPUT) {
518 runparams.exportdata->addExternalFile(tex_format, writefile,
521 // \input wants file with extension (default is .tex)
522 if (!isLyXFilename(included_file.absFilename())) {
523 incfile = latex_path(incfile);
525 os << '\\' << from_ascii(params().getCmdName())
526 << '{' << from_utf8(incfile) << '}';
528 incfile = changeExtension(incfile, ".tex");
529 incfile = latex_path(incfile);
531 os << '\\' << from_ascii(params().getCmdName())
532 << '{' << from_utf8(incfile) << '}';
534 } else if (type(params()) == LISTINGS) {
535 os << '\\' << from_ascii(params().getCmdName());
536 string const opt = to_utf8(params()["lstparams"]);
537 // opt is set in QInclude dialog and should have passed validation.
538 InsetListingsParams params(opt);
539 if (!params.params().empty())
540 os << "[" << from_utf8(params.params()) << "]";
541 os << '{' << from_utf8(incfile) << '}';
543 runparams.exportdata->addExternalFile(tex_format, writefile,
546 // \include don't want extension and demands that the
547 // file really have .tex
548 incfile = changeExtension(incfile, string());
549 incfile = latex_path(incfile);
551 os << '\\' << from_ascii(params().getCmdName()) << '{'
552 << from_utf8(incfile) << '}';
559 int InsetInclude::plaintext(Buffer const & buffer, odocstream & os,
560 OutputParams const &) const
562 if (isVerbatim(params()) || isListings(params())) {
563 os << '[' << getScreenLabel(buffer) << '\n';
564 // FIXME: We don't know the encoding of the file, default to UTF-8.
565 os << includedFilename(buffer, params()).fileContents("UTF-8");
567 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
569 docstring const str = '[' + getScreenLabel(buffer) + ']';
576 int InsetInclude::docbook(Buffer const & buffer, odocstream & os,
577 OutputParams const & runparams) const
579 string incfile = to_utf8(params()["filename"]);
581 // Do nothing if no file name has been specified
585 string const included_file = includedFilename(buffer, params()).absFilename();
587 //Check we're not trying to include ourselves.
588 //FIXME RECURSIVE INCLUDE
589 //This isn't sufficient, as the inclusion could be downstream.
590 //But it'll have to do for now.
591 if (buffer.absFileName() == included_file) {
592 Alert::error(_("Recursive input"),
593 bformat(_("Attempted to include file %1$s in itself! "
594 "Ignoring inclusion."), from_utf8(incfile)));
598 // write it to a file (so far the complete file)
599 string const exportfile = changeExtension(incfile, ".sgml");
600 DocFileName writefile(changeExtension(included_file, ".sgml"));
602 if (loadIfNeeded(buffer, params())) {
603 Buffer * tmp = theBufferList().getBuffer(included_file);
605 string const mangled = writefile.mangledFilename();
606 writefile = makeAbsPath(mangled,
607 buffer.masterBuffer()->temppath());
611 LYXERR(Debug::LATEX, "incfile:" << incfile);
612 LYXERR(Debug::LATEX, "exportfile:" << exportfile);
613 LYXERR(Debug::LATEX, "writefile:" << writefile);
615 tmp->makeDocBookFile(writefile, runparams, true);
618 runparams.exportdata->addExternalFile("docbook", writefile,
620 runparams.exportdata->addExternalFile("docbook-xml", writefile,
623 if (isVerbatim(params()) || isListings(params())) {
624 os << "<inlinegraphic fileref=\""
625 << '&' << include_label << ';'
626 << "\" format=\"linespecific\">";
628 os << '&' << include_label << ';';
634 void InsetInclude::validate(LaTeXFeatures & features) const
636 string incfile = to_utf8(params()["filename"]);
639 Buffer const & buffer = features.buffer();
641 string const included_file = includedFilename(buffer, params()).availableFile().absFilename();
643 if (isLyXFilename(included_file))
644 writefile = changeExtension(included_file, ".sgml");
646 writefile = included_file;
648 if (!features.runparams().nice && !isVerbatim(params()) && !isListings(params())) {
649 incfile = DocFileName(writefile).mangledFilename();
650 writefile = makeAbsPath(incfile,
651 buffer.masterBuffer()->temppath()).absFilename();
654 features.includeFile(include_label, writefile);
656 if (isVerbatim(params()))
657 features.require("verbatim");
658 else if (isListings(params()))
659 features.require("listings");
661 // Here we must do the fun stuff...
662 // Load the file in the include if it needs
664 if (loadIfNeeded(buffer, params())) {
666 Buffer * const tmp = theBufferList().getBuffer(included_file);
667 // make sure the buffer isn't us
668 // FIXME RECURSIVE INCLUDES
669 // This is not sufficient, as recursive includes could be
670 // more than a file away. But it will do for now.
671 if (tmp && tmp != & buffer) {
672 // We must temporarily change features.buffer,
673 // otherwise it would always be the master buffer,
674 // and nested includes would not work.
675 features.setBuffer(*tmp);
676 tmp->validate(features);
677 features.setBuffer(buffer);
683 void InsetInclude::getLabelList(Buffer const & buffer,
684 vector<docstring> & list) const
686 if (isListings(params())) {
687 InsetListingsParams p(to_utf8(params()["lstparams"]));
688 string label = p.getParamValue("label");
690 list.push_back(from_utf8(label));
692 else if (loadIfNeeded(buffer, params())) {
693 string const included_file = includedFilename(buffer, params()).absFilename();
694 Buffer * tmp = theBufferList().getBuffer(included_file);
696 tmp->getLabelList(list);
697 tmp->setParent(const_cast<Buffer *>(&buffer));
702 void InsetInclude::fillWithBibKeys(Buffer const & buffer,
703 BiblioInfo & keys, InsetIterator const & /*di*/) const
705 if (loadIfNeeded(buffer, params())) {
706 string const included_file = includedFilename(buffer, params()).absFilename();
707 Buffer * tmp = theBufferList().getBuffer(included_file);
708 //FIXME This is kind of a dirty hack and should be made reasonable.
710 keys.fillWithBibKeys(tmp);
711 tmp->setParent(&buffer);
716 void InsetInclude::updateBibfilesCache(Buffer const & buffer)
718 Buffer * const tmp = getChildBuffer(buffer, params());
721 tmp->updateBibfilesCache();
722 tmp->setParent(&buffer);
727 EmbeddedFileList const &
728 InsetInclude::getBibfilesCache(Buffer const & buffer) const
730 Buffer * const tmp = getChildBuffer(buffer, params());
733 EmbeddedFileList const & cache = tmp->getBibfilesCache();
734 tmp->setParent(&buffer);
737 static EmbeddedFileList const empty;
742 void InsetInclude::metrics(MetricsInfo & mi, Dimension & dim) const
744 BOOST_ASSERT(mi.base.bv);
746 bool use_preview = false;
747 if (RenderPreview::status() != LyXRC::PREVIEW_OFF) {
748 graphics::PreviewImage const * pimage =
749 preview_->getPreviewImage(mi.base.bv->buffer());
750 use_preview = pimage && pimage->image();
754 preview_->metrics(mi, dim);
758 button_.update(getScreenLabel(mi.base.bv->buffer()),
761 button_.metrics(mi, dim);
764 Box b(0, dim.wid, -dim.asc, dim.des);
769 void InsetInclude::draw(PainterInfo & pi, int x, int y) const
771 BOOST_ASSERT(pi.base.bv);
773 bool use_preview = false;
774 if (RenderPreview::status() != LyXRC::PREVIEW_OFF) {
775 graphics::PreviewImage const * pimage =
776 preview_->getPreviewImage(pi.base.bv->buffer());
777 use_preview = pimage && pimage->image();
781 preview_->draw(pi, x, y);
783 button_.draw(pi, x, y);
787 Inset::DisplayType InsetInclude::display() const
789 return type(params()) == INPUT ? Inline : AlignCenter;
798 void InsetInclude::fileChanged() const
800 Buffer const * const buffer = updateFrontend();
804 preview_->removePreview(*buffer);
805 add_preview(*preview_.get(), *this, *buffer);
806 preview_->startLoading(*buffer);
812 bool preview_wanted(InsetCommandParams const & params, Buffer const & buffer)
814 FileName const included_file = includedFilename(buffer, params);
816 return type(params) == INPUT && params.preview() &&
817 included_file.isReadableFile();
821 docstring const latex_string(InsetInclude const & inset, Buffer const & buffer)
824 // We don't need to set runparams.encoding since this will be done
825 // by latex() anyway.
826 OutputParams runparams(0);
827 runparams.flavor = OutputParams::LATEX;
828 inset.latex(buffer, os, runparams);
834 void add_preview(RenderMonitoredPreview & renderer, InsetInclude const & inset,
835 Buffer const & buffer)
837 InsetCommandParams const & params = inset.params();
838 if (RenderPreview::status() != LyXRC::PREVIEW_OFF &&
839 preview_wanted(params, buffer)) {
840 renderer.setAbsFile(includedFilename(buffer, params));
841 docstring const snippet = latex_string(inset, buffer);
842 renderer.addPreview(snippet, buffer);
849 void InsetInclude::addPreview(graphics::PreviewLoader & ploader) const
851 Buffer const & buffer = ploader.buffer();
852 if (!preview_wanted(params(), buffer))
854 preview_->setAbsFile(includedFilename(buffer, params()));
855 docstring const snippet = latex_string(*this, buffer);
856 preview_->addPreview(snippet, ploader);
860 void InsetInclude::addToToc(Buffer const & buffer,
861 ParConstIterator const & cpit) const
863 if (isListings(params())) {
864 InsetListingsParams p(to_utf8(params()["lstparams"]));
865 string caption = p.getParamValue("caption");
868 Toc & toc = buffer.tocBackend().toc("listing");
869 docstring const str = convert<docstring>(toc.size() + 1)
870 + ". " + from_utf8(caption);
871 ParConstIterator pit = cpit;
872 pit.push_back(*this);
873 toc.push_back(TocItem(pit, 0, str));
876 Buffer const * const childbuffer = getChildBuffer(buffer, params());
880 TocList & toclist = buffer.tocBackend().tocs();
881 TocList const & childtoclist = childbuffer->tocBackend().tocs();
882 TocList::const_iterator it = childtoclist.begin();
883 TocList::const_iterator const end = childtoclist.end();
884 for(; it != end; ++it)
885 toclist[it->first].insert(toclist[it->first].end(),
886 it->second.begin(), it->second.end());
890 void InsetInclude::updateLabels(Buffer const & buffer, ParIterator const &)
892 Buffer const * const childbuffer = getChildBuffer(buffer, params());
894 lyx::updateLabels(*childbuffer, true);
897 if (!isListings(params()))
900 InsetListingsParams const par(to_utf8(params()["lstparams"]));
901 if (par.getParamValue("caption").empty()) {
902 listings_label_.clear();
905 Counters & counters = buffer.params().getTextClass().counters();
906 docstring const cnt = from_ascii("listing");
907 listings_label_ = buffer.B_("Program Listing");
908 if (counters.hasCounter(cnt)) {
910 listings_label_ += " " + convert<docstring>(counters.value(cnt));
915 void InsetInclude::registerEmbeddedFiles(Buffer const & buffer,
916 EmbeddedFileList & files) const
918 files.registerFile(includedFilename(buffer, params()), this, buffer);
922 void InsetInclude::updateEmbeddedFile(Buffer const & buf,
923 EmbeddedFile const & file)
925 InsetCommandParams p = params();
926 p["filename"] = from_utf8(file.outputFilename());
927 p["embed"] = file.embedded() ? from_utf8(file.inzipName()) : docstring();