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/ExceptionMessage.h"
48 #include "support/FileNameList.h"
49 #include "support/filetools.h"
50 #include "support/lstrings.h" // contains
51 #include "support/lyxalgo.h"
52 #include "support/convert.h"
54 #include <boost/bind.hpp>
57 using namespace lyx::support;
61 namespace Alert = frontend::Alert;
66 docstring const uniqueID()
68 static unsigned int seed = 1000;
69 return "file" + convert<docstring>(++seed);
73 /// the type of inclusion
75 INCLUDE, VERB, INPUT, VERBAST, LISTINGS, NONE
79 Types type(string const & s)
83 if (s == "verbatiminput")
85 if (s == "verbatiminput*")
87 if (s == "lstinputlisting")
95 Types type(InsetCommandParams const & params)
97 return type(params.getCmdName());
101 bool isListings(InsetCommandParams const & params)
103 return type(params) == LISTINGS;
107 bool isVerbatim(InsetCommandParams const & params)
109 Types const t = type(params);
110 return t == VERB || t == VERBAST;
114 bool isInputOrInclude(InsetCommandParams const & params)
116 Types const t = type(params);
117 return t == INPUT || t == INCLUDE;
121 FileName const masterFileName(Buffer const & buffer)
123 return buffer.masterBuffer()->fileName();
127 void add_preview(RenderMonitoredPreview &, InsetInclude const &, Buffer const &);
130 string const parentFilename(Buffer const & buffer)
132 return buffer.absFileName();
136 EmbeddedFile const includedFilename(Buffer const & buffer,
137 InsetCommandParams const & params)
139 // it is not a good idea to create this EmbeddedFile object
140 // each time, but there seems to be no easy way around.
141 EmbeddedFile file(to_utf8(params["filename"]),
142 onlyPath(parentFilename(buffer)));
143 file.setEmbed(!params["embed"].empty());
144 file.enable(buffer.embedded(), &buffer);
151 InsetInclude::InsetInclude(InsetCommandParams const & p)
152 : InsetCommand(p, "include"), include_label(uniqueID()),
153 preview_(new RenderMonitoredPreview(this)), set_label_(false)
155 preview_->fileChanged(boost::bind(&InsetInclude::fileChanged, this));
159 InsetInclude::InsetInclude(InsetInclude const & other)
160 : InsetCommand(other), include_label(other.include_label),
161 preview_(new RenderMonitoredPreview(this)), set_label_(false)
163 preview_->fileChanged(boost::bind(&InsetInclude::fileChanged, this));
167 CommandInfo const * InsetInclude::findInfo(string const & /* cmdName */)
170 // This is only correct for the case of listings, but it'll do for now.
171 // In the other cases, this second parameter should just be empty.
172 static const char * const paramnames[] = {"filename", "embed", "lstparams", ""};
173 static const bool isoptional[] = {false, false, true};
174 static const CommandInfo info = {3, paramnames, isoptional};
179 bool InsetInclude::isCompatibleCommand(string const & s)
181 return type(s) != NONE;
185 void InsetInclude::doDispatch(Cursor & cur, FuncRequest & cmd)
187 switch (cmd.action) {
189 case LFUN_INSET_MODIFY: {
190 InsetCommandParams p(INCLUDE_CODE);
191 InsetCommandMailer::string2params("include", to_utf8(cmd.argument()), p);
192 if (!p.getCmdName().empty()) {
194 InsetListingsParams par_old(to_utf8(params()["lstparams"]));
195 InsetListingsParams par_new(to_utf8(p["lstparams"]));
196 if (par_old.getParamValue("label") !=
197 par_new.getParamValue("label")
198 && !par_new.getParamValue("label").empty())
199 cur.bv().buffer().changeRefsIfUnique(
200 from_utf8(par_old.getParamValue("label")),
201 from_utf8(par_new.getParamValue("label")),
205 // the embed parameter passed back from the dialog
206 // is "true" or "false", we need to change it.
207 if (p["embed"] == _("false"))
210 p["embed"] = from_utf8(EmbeddedFile(to_utf8(p["filename"]),
211 onlyPath(parentFilename(cur.buffer()))).inzipName());
213 includedFilename(cur.buffer(), p);
214 } catch (ExceptionMessage const & message) {
215 Alert::error(message.title_, message.details_);
216 // do not set parameter if an error happens
219 set(p, cur.buffer());
220 cur.buffer().updateBibfilesCache();
226 //pass everything else up the chain
228 InsetCommand::doDispatch(cur, cmd);
234 void InsetInclude::set(InsetCommandParams const & p, Buffer const & buffer)
239 if (preview_->monitoring())
240 preview_->stopMonitoring();
242 if (type(params()) == INPUT)
243 add_preview(*preview_, *this, buffer);
247 Inset * InsetInclude::clone() const
249 return new InsetInclude(*this);
253 docstring const InsetInclude::getScreenLabel(Buffer const & buf) const
257 switch (type(params())) {
259 temp = buf.B_("Input");
262 temp = buf.B_("Verbatim Input");
265 temp = buf.B_("Verbatim Input*");
268 temp = buf.B_("Include");
271 temp = listings_label_;
279 if (params()["filename"].empty())
282 temp += from_utf8(onlyFilename(to_utf8(params()["filename"])));
284 if (!params()["embed"].empty())
285 temp += _(" (embedded)");
290 /// return the child buffer if the file is a LyX doc and is loaded
291 Buffer * getChildBuffer(Buffer const & buffer, InsetCommandParams const & params)
293 if (isVerbatim(params) || isListings(params))
296 string const included_file = includedFilename(buffer, params).absFilename();
297 if (!isLyXFilename(included_file))
300 Buffer * childBuffer = theBufferList().getBuffer(included_file);
302 //FIXME RECURSIVE INCLUDES
303 if (childBuffer == & buffer)
309 /// return true if the file is or got loaded.
310 Buffer * loadIfNeeded(Buffer const & parent, InsetCommandParams const & params)
312 if (isVerbatim(params) || isListings(params))
315 string const parent_filename = parent.absFileName();
316 FileName const included_file = makeAbsPath(to_utf8(params["filename"]),
317 onlyPath(parent_filename));
319 if (!isLyXFilename(included_file.absFilename()))
322 Buffer * child = theBufferList().getBuffer(included_file.absFilename());
324 // the readonly flag can/will be wrong, not anymore I think.
325 if (!included_file.exists())
328 child = theBufferList().newBuffer(included_file.absFilename());
330 // Buffer creation is not possible.
333 if (!child->loadLyXFile(included_file)) {
334 //close the buffer we just opened
335 theBufferList().release(child);
339 child->setParent(&parent);
344 void resetParentBuffer(Buffer const * parent, InsetCommandParams const & params,
347 if (isVerbatim(params) || isListings(params))
350 string const parent_filename = parent->absFileName();
351 FileName const included_file = makeAbsPath(to_utf8(params["filename"]),
352 onlyPath(parent_filename));
354 if (!isLyXFilename(included_file.absFilename()))
357 Buffer * child = theBufferList().getBuffer(included_file.absFilename());
358 // File not opened, nothing to close.
362 // Child document has a different parent, don't close it.
363 if (child->parent() != parent)
369 theBufferList().release(child);
371 updateLabels(*child);
375 int InsetInclude::latex(Buffer const & buffer, odocstream & os,
376 OutputParams const & runparams) const
378 string incfile(to_utf8(params()["filename"]));
380 // Do nothing if no file name has been specified
384 FileName const included_file = includedFilename(buffer, params()).availableFile();
386 //Check we're not trying to include ourselves.
387 //FIXME RECURSIVE INCLUDE
388 //This isn't sufficient, as the inclusion could be downstream.
389 //But it'll have to do for now.
390 if (isInputOrInclude(params()) &&
391 buffer.absFileName() == included_file.absFilename())
393 Alert::error(_("Recursive input"),
394 bformat(_("Attempted to include file %1$s in itself! "
395 "Ignoring inclusion."), from_utf8(incfile)));
399 Buffer const * const masterBuffer = buffer.masterBuffer();
401 // if incfile is relative, make it relative to the master
403 if (!FileName(incfile).isAbsolute()) {
405 incfile = to_utf8(makeRelPath(from_utf8(included_file.absFilename()),
406 from_utf8(masterBuffer->filePath())));
409 // write it to a file (so far the complete file)
410 string const exportfile = changeExtension(incfile, ".tex");
411 string const mangled =
412 DocFileName(changeExtension(included_file.absFilename(),".tex")).
414 FileName const writefile(makeAbsPath(mangled, masterBuffer->temppath()));
418 else if (!isValidLaTeXFilename(incfile)) {
419 frontend::Alert::warning(_("Invalid filename"),
420 _("The following filename is likely to cause trouble "
421 "when running the exported file through LaTeX: ") +
424 LYXERR(Debug::LATEX, "incfile:" << incfile);
425 LYXERR(Debug::LATEX, "exportfile:" << exportfile);
426 LYXERR(Debug::LATEX, "writefile:" << writefile);
428 if (runparams.inComment || runparams.dryrun) {
429 //Don't try to load or copy the file if we're
430 //in a comment or doing a dryrun
431 } else if (isInputOrInclude(params()) &&
432 isLyXFilename(included_file.absFilename())) {
433 //if it's a LyX file and we're inputting or including,
434 //try to load it so we can write the associated latex
435 if (!loadIfNeeded(buffer, params()))
438 Buffer * tmp = theBufferList().getBuffer(included_file.absFilename());
440 if (tmp->params().getBaseClass() != masterBuffer->params().getBaseClass()) {
442 docstring text = bformat(_("Included file `%1$s'\n"
443 "has textclass `%2$s'\n"
444 "while parent file has textclass `%3$s'."),
445 included_file.displayName(),
446 from_utf8(tmp->params().getTextClass().name()),
447 from_utf8(masterBuffer->params().getTextClass().name()));
448 Alert::warning(_("Different textclasses"), text);
452 // Make sure modules used in child are all included in master
453 //FIXME It might be worth loading the children's modules into the master
454 //over in BufferParams rather than doing this check.
455 vector<string> const masterModules = masterBuffer->params().getModules();
456 vector<string> const childModules = tmp->params().getModules();
457 vector<string>::const_iterator it = childModules.begin();
458 vector<string>::const_iterator end = childModules.end();
459 for (; it != end; ++it) {
460 string const module = *it;
461 vector<string>::const_iterator found =
462 find(masterModules.begin(), masterModules.end(), module);
463 if (found != masterModules.end()) {
464 docstring text = bformat(_("Included file `%1$s'\n"
465 "uses module `%2$s'\n"
466 "which is not used in parent file."),
467 included_file.displayName(), from_utf8(module));
468 Alert::warning(_("Module not found"), text);
472 tmp->markDepClean(masterBuffer->temppath());
474 // FIXME: handle non existing files
475 // FIXME: Second argument is irrelevant!
476 // since only_body is true, makeLaTeXFile will not look at second
477 // argument. Should we set it to string(), or should makeLaTeXFile
478 // make use of it somehow? (JMarc 20031002)
479 // The included file might be written in a different encoding
480 Encoding const * const oldEnc = runparams.encoding;
481 runparams.encoding = &tmp->params().encoding();
482 tmp->makeLaTeXFile(writefile,
483 masterFileName(buffer).onlyPath().absFilename(),
485 runparams.encoding = oldEnc;
487 // In this case, it's not a LyX file, so we copy the file
488 // to the temp dir, so that .aux files etc. are not created
489 // in the original dir. Files included by this file will be
490 // found via input@path, see ../Buffer.cpp.
491 unsigned long const checksum_in = included_file.checksum();
492 unsigned long const checksum_out = writefile.checksum();
494 if (checksum_in != checksum_out) {
495 if (!included_file.copyTo(writefile)) {
498 to_utf8(bformat(_("Could not copy the file\n%1$s\n"
499 "into the temporary directory."),
500 from_utf8(included_file.absFilename()))));
506 string const tex_format = (runparams.flavor == OutputParams::LATEX) ?
507 "latex" : "pdflatex";
508 if (isVerbatim(params())) {
509 incfile = latex_path(incfile);
511 os << '\\' << from_ascii(params().getCmdName()) << '{'
512 << from_utf8(incfile) << '}';
513 } else if (type(params()) == INPUT) {
514 runparams.exportdata->addExternalFile(tex_format, writefile,
517 // \input wants file with extension (default is .tex)
518 if (!isLyXFilename(included_file.absFilename())) {
519 incfile = latex_path(incfile);
521 os << '\\' << from_ascii(params().getCmdName())
522 << '{' << from_utf8(incfile) << '}';
524 incfile = changeExtension(incfile, ".tex");
525 incfile = latex_path(incfile);
527 os << '\\' << from_ascii(params().getCmdName())
528 << '{' << from_utf8(incfile) << '}';
530 } else if (type(params()) == LISTINGS) {
531 os << '\\' << from_ascii(params().getCmdName());
532 string const opt = to_utf8(params()["lstparams"]);
533 // opt is set in QInclude dialog and should have passed validation.
534 InsetListingsParams params(opt);
535 if (!params.params().empty())
536 os << "[" << from_utf8(params.params()) << "]";
537 os << '{' << from_utf8(incfile) << '}';
539 runparams.exportdata->addExternalFile(tex_format, writefile,
542 // \include don't want extension and demands that the
543 // file really have .tex
544 incfile = changeExtension(incfile, string());
545 incfile = latex_path(incfile);
547 os << '\\' << from_ascii(params().getCmdName()) << '{'
548 << from_utf8(incfile) << '}';
555 int InsetInclude::plaintext(Buffer const & buffer, odocstream & os,
556 OutputParams const &) const
558 if (isVerbatim(params()) || isListings(params())) {
559 os << '[' << getScreenLabel(buffer) << '\n';
560 // FIXME: We don't know the encoding of the file, default to UTF-8.
561 os << includedFilename(buffer, params()).fileContents("UTF-8");
563 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
565 docstring const str = '[' + getScreenLabel(buffer) + ']';
572 int InsetInclude::docbook(Buffer const & buffer, odocstream & os,
573 OutputParams const & runparams) const
575 string incfile = to_utf8(params()["filename"]);
577 // Do nothing if no file name has been specified
581 string const included_file = includedFilename(buffer, params()).absFilename();
583 //Check we're not trying to include ourselves.
584 //FIXME RECURSIVE INCLUDE
585 //This isn't sufficient, as the inclusion could be downstream.
586 //But it'll have to do for now.
587 if (buffer.absFileName() == included_file) {
588 Alert::error(_("Recursive input"),
589 bformat(_("Attempted to include file %1$s in itself! "
590 "Ignoring inclusion."), from_utf8(incfile)));
594 // write it to a file (so far the complete file)
595 string const exportfile = changeExtension(incfile, ".sgml");
596 DocFileName writefile(changeExtension(included_file, ".sgml"));
598 if (loadIfNeeded(buffer, params())) {
599 Buffer * tmp = theBufferList().getBuffer(included_file);
601 string const mangled = writefile.mangledFilename();
602 writefile = makeAbsPath(mangled,
603 buffer.masterBuffer()->temppath());
607 LYXERR(Debug::LATEX, "incfile:" << incfile);
608 LYXERR(Debug::LATEX, "exportfile:" << exportfile);
609 LYXERR(Debug::LATEX, "writefile:" << writefile);
611 tmp->makeDocBookFile(writefile, runparams, true);
614 runparams.exportdata->addExternalFile("docbook", writefile,
616 runparams.exportdata->addExternalFile("docbook-xml", writefile,
619 if (isVerbatim(params()) || isListings(params())) {
620 os << "<inlinegraphic fileref=\""
621 << '&' << include_label << ';'
622 << "\" format=\"linespecific\">";
624 os << '&' << include_label << ';';
630 void InsetInclude::validate(LaTeXFeatures & features) const
632 string incfile = to_utf8(params()["filename"]);
635 Buffer const & buffer = features.buffer();
637 string const included_file = includedFilename(buffer, params()).availableFile().absFilename();
639 if (isLyXFilename(included_file))
640 writefile = changeExtension(included_file, ".sgml");
642 writefile = included_file;
644 if (!features.runparams().nice && !isVerbatim(params()) && !isListings(params())) {
645 incfile = DocFileName(writefile).mangledFilename();
646 writefile = makeAbsPath(incfile,
647 buffer.masterBuffer()->temppath()).absFilename();
650 features.includeFile(include_label, writefile);
652 if (isVerbatim(params()))
653 features.require("verbatim");
654 else if (isListings(params()))
655 features.require("listings");
657 // Here we must do the fun stuff...
658 // Load the file in the include if it needs
660 if (loadIfNeeded(buffer, params())) {
662 Buffer * const tmp = theBufferList().getBuffer(included_file);
663 // make sure the buffer isn't us
664 // FIXME RECURSIVE INCLUDES
665 // This is not sufficient, as recursive includes could be
666 // more than a file away. But it will do for now.
667 if (tmp && tmp != & buffer) {
668 // We must temporarily change features.buffer,
669 // otherwise it would always be the master buffer,
670 // and nested includes would not work.
671 features.setBuffer(*tmp);
672 tmp->validate(features);
673 features.setBuffer(buffer);
679 void InsetInclude::getLabelList(Buffer const & buffer,
680 vector<docstring> & list) const
682 if (isListings(params())) {
683 InsetListingsParams p(to_utf8(params()["lstparams"]));
684 string label = p.getParamValue("label");
686 list.push_back(from_utf8(label));
688 else if (loadIfNeeded(buffer, params())) {
689 string const included_file = includedFilename(buffer, params()).absFilename();
690 Buffer * tmp = theBufferList().getBuffer(included_file);
692 tmp->getLabelList(list);
693 tmp->setParent(const_cast<Buffer *>(&buffer));
698 void InsetInclude::fillWithBibKeys(Buffer const & buffer,
699 BiblioInfo & keys, InsetIterator const & /*di*/) const
701 if (loadIfNeeded(buffer, params())) {
702 string const included_file = includedFilename(buffer, params()).absFilename();
703 Buffer * tmp = theBufferList().getBuffer(included_file);
704 //FIXME This is kind of a dirty hack and should be made reasonable.
706 keys.fillWithBibKeys(tmp);
707 tmp->setParent(&buffer);
712 void InsetInclude::updateBibfilesCache(Buffer const & buffer)
714 Buffer * const tmp = getChildBuffer(buffer, params());
717 tmp->updateBibfilesCache();
718 tmp->setParent(&buffer);
723 EmbeddedFileList const &
724 InsetInclude::getBibfilesCache(Buffer const & buffer) const
726 Buffer * const tmp = getChildBuffer(buffer, params());
729 EmbeddedFileList const & cache = tmp->getBibfilesCache();
730 tmp->setParent(&buffer);
733 static EmbeddedFileList const empty;
738 void InsetInclude::metrics(MetricsInfo & mi, Dimension & dim) const
740 BOOST_ASSERT(mi.base.bv);
742 bool use_preview = false;
743 if (RenderPreview::status() != LyXRC::PREVIEW_OFF) {
744 graphics::PreviewImage const * pimage =
745 preview_->getPreviewImage(mi.base.bv->buffer());
746 use_preview = pimage && pimage->image();
750 preview_->metrics(mi, dim);
754 button_.update(getScreenLabel(mi.base.bv->buffer()),
757 button_.metrics(mi, dim);
760 Box b(0, dim.wid, -dim.asc, dim.des);
765 void InsetInclude::draw(PainterInfo & pi, int x, int y) const
767 BOOST_ASSERT(pi.base.bv);
769 bool use_preview = false;
770 if (RenderPreview::status() != LyXRC::PREVIEW_OFF) {
771 graphics::PreviewImage const * pimage =
772 preview_->getPreviewImage(pi.base.bv->buffer());
773 use_preview = pimage && pimage->image();
777 preview_->draw(pi, x, y);
779 button_.draw(pi, x, y);
783 Inset::DisplayType InsetInclude::display() const
785 return type(params()) == INPUT ? Inline : AlignCenter;
794 void InsetInclude::fileChanged() const
796 Buffer const * const buffer = updateFrontend();
800 preview_->removePreview(*buffer);
801 add_preview(*preview_.get(), *this, *buffer);
802 preview_->startLoading(*buffer);
808 bool preview_wanted(InsetCommandParams const & params, Buffer const & buffer)
810 FileName const included_file = includedFilename(buffer, params);
812 return type(params) == INPUT && params.preview() &&
813 included_file.isReadableFile();
817 docstring const latex_string(InsetInclude const & inset, Buffer const & buffer)
820 // We don't need to set runparams.encoding since this will be done
821 // by latex() anyway.
822 OutputParams runparams(0);
823 runparams.flavor = OutputParams::LATEX;
824 inset.latex(buffer, os, runparams);
830 void add_preview(RenderMonitoredPreview & renderer, InsetInclude const & inset,
831 Buffer const & buffer)
833 InsetCommandParams const & params = inset.params();
834 if (RenderPreview::status() != LyXRC::PREVIEW_OFF &&
835 preview_wanted(params, buffer)) {
836 renderer.setAbsFile(includedFilename(buffer, params));
837 docstring const snippet = latex_string(inset, buffer);
838 renderer.addPreview(snippet, buffer);
845 void InsetInclude::addPreview(graphics::PreviewLoader & ploader) const
847 Buffer const & buffer = ploader.buffer();
848 if (preview_wanted(params(), buffer)) {
849 preview_->setAbsFile(includedFilename(buffer, params()));
850 docstring const snippet = latex_string(*this, buffer);
851 preview_->addPreview(snippet, ploader);
856 void InsetInclude::addToToc(Buffer const & buffer,
857 ParConstIterator const & cpit) const
859 if (isListings(params())) {
860 InsetListingsParams p(to_utf8(params()["lstparams"]));
861 string caption = p.getParamValue("caption");
864 Toc & toc = buffer.tocBackend().toc("listing");
865 docstring const str = convert<docstring>(toc.size() + 1)
866 + ". " + from_utf8(caption);
867 ParConstIterator pit = cpit;
868 pit.push_back(*this);
869 toc.push_back(TocItem(pit, 0, str));
872 Buffer const * const childbuffer = getChildBuffer(buffer, params());
876 TocList & toclist = buffer.tocBackend().tocs();
877 TocList const & childtoclist = childbuffer->tocBackend().tocs();
878 TocList::const_iterator it = childtoclist.begin();
879 TocList::const_iterator const end = childtoclist.end();
880 for(; it != end; ++it)
881 toclist[it->first].insert(toclist[it->first].end(),
882 it->second.begin(), it->second.end());
886 void InsetInclude::updateLabels(Buffer const & buffer, ParIterator const &)
888 Buffer const * const childbuffer = getChildBuffer(buffer, params());
890 lyx::updateLabels(*childbuffer, true);
891 else if (isListings(params())) {
892 InsetListingsParams const par(to_utf8(params()["lstparams"]));
893 if (par.getParamValue("caption").empty())
894 listings_label_.clear();
896 Counters & counters = buffer.params().getTextClass().counters();
897 docstring const cnt = from_ascii("listing");
898 if (counters.hasCounter(cnt)) {
900 listings_label_ = buffer.B_("Program Listing ")
901 + convert<docstring>(counters.value(cnt));
903 listings_label_ = buffer.B_("Program Listing");
909 void InsetInclude::registerEmbeddedFiles(Buffer const & buffer,
910 EmbeddedFileList & files) const
912 files.registerFile(includedFilename(buffer, params()), this, buffer);
916 void InsetInclude::updateEmbeddedFile(Buffer const & buf,
917 EmbeddedFile const & file)
919 InsetCommandParams p = params();
920 p["filename"] = from_utf8(file.outputFilename());
921 p["embed"] = file.embedded() ? from_utf8(file.inzipName()) : docstring();