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
8 * Full author contact details are available in file CREDITS.
13 #include "InsetInclude.h"
16 #include "buffer_funcs.h"
17 #include "BufferList.h"
18 #include "BufferParams.h"
19 #include "BufferView.h"
22 #include "DispatchResult.h"
24 #include "FuncRequest.h"
25 #include "FuncStatus.h"
27 #include "LaTeXFeatures.h"
31 #include "MetricsInfo.h"
32 #include "OutputParams.h"
33 #include "TocBackend.h"
35 #include "frontends/alert.h"
36 #include "frontends/Painter.h"
38 #include "graphics/PreviewImage.h"
39 #include "graphics/PreviewLoader.h"
41 #include "insets/RenderPreview.h"
42 #include "insets/InsetListingsParams.h"
44 #include "support/filetools.h"
45 #include "support/lstrings.h" // contains
46 #include "support/lyxalgo.h"
47 #include "support/lyxlib.h"
48 #include "support/convert.h"
50 #include <boost/bind.hpp>
55 using support::addName;
56 using support::absolutePath;
57 using support::bformat;
58 using support::changeExtension;
59 using support::contains;
61 using support::DocFileName;
62 using support::FileName;
63 using support::getVectorFromString;
64 using support::isLyXFilename;
65 using support::isValidLaTeXFilename;
66 using support::latex_path;
67 using support::makeAbsPath;
68 using support::makeDisplayPath;
69 using support::makeRelPath;
70 using support::onlyFilename;
71 using support::onlyPath;
72 using support::prefixIs;
79 using std::istringstream;
81 using std::ostringstream;
84 namespace Alert = frontend::Alert;
89 docstring const uniqueID()
91 static unsigned int seed = 1000;
92 return "file" + convert<docstring>(++seed);
96 /// the type of inclusion
106 Types type(InsetCommandParams const & params)
108 string const command_name = params.getCmdName();
110 if (command_name == "input")
112 if (command_name == "verbatiminput")
114 if (command_name == "verbatiminput*")
116 if (command_name == "lstinputlisting")
122 bool isListings(InsetCommandParams const & params)
124 return type(params) == LISTINGS;
128 bool isVerbatim(InsetCommandParams const & params)
130 Types const t = type(params);
131 return (t == VERB) || (t == VERBAST);
135 bool isInputOrInclude(InsetCommandParams const & params)
137 Types const t = type(params);
138 return (t == INPUT) || (t == INCLUDE);
144 InsetInclude::InsetInclude(InsetCommandParams const & p)
145 : InsetCommand(p, "include"), include_label(uniqueID()),
146 preview_(new RenderMonitoredPreview(this)), set_label_(false)
148 preview_->fileChanged(boost::bind(&InsetInclude::fileChanged, this));
152 InsetInclude::InsetInclude(InsetInclude const & other)
153 : InsetCommand(other), include_label(other.include_label),
154 preview_(new RenderMonitoredPreview(this)), set_label_(false)
156 preview_->fileChanged(boost::bind(&InsetInclude::fileChanged, this));
160 void InsetInclude::doDispatch(Cursor & cur, FuncRequest & cmd)
162 switch (cmd.action) {
164 case LFUN_INSET_MODIFY: {
165 InsetCommandParams p(INCLUDE_CODE);
166 InsetCommandMailer::string2params("include", to_utf8(cmd.argument()), p);
167 if (!p.getCmdName().empty()) {
169 InsetListingsParams par_old(params().getOptions());
170 InsetListingsParams par_new(p.getOptions());
171 if (par_old.getParamValue("label") !=
172 par_new.getParamValue("label")
173 && !par_new.getParamValue("label").empty())
174 cur.bv().buffer().changeRefsIfUnique(
175 from_utf8(par_old.getParamValue("label")),
176 from_utf8(par_new.getParamValue("label")),
179 set(p, cur.buffer());
180 cur.buffer().updateBibfilesCache();
186 //pass everything else up the chain
188 InsetCommand::doDispatch(cur, cmd);
196 string const masterFilename(Buffer const & buffer)
198 return buffer.masterBuffer()->absFileName();
202 string const parentFilename(Buffer const & buffer)
204 return buffer.absFileName();
208 FileName const includedFilename(Buffer const & buffer,
209 InsetCommandParams const & params)
211 return makeAbsPath(to_utf8(params["filename"]),
212 onlyPath(parentFilename(buffer)));
216 void add_preview(RenderMonitoredPreview &, InsetInclude const &, Buffer const &);
221 void InsetInclude::set(InsetCommandParams const & p, Buffer const & buffer)
226 if (preview_->monitoring())
227 preview_->stopMonitoring();
229 if (type(params()) == INPUT)
230 add_preview(*preview_, *this, buffer);
234 Inset * InsetInclude::clone() const
236 return new InsetInclude(*this);
240 docstring const InsetInclude::getScreenLabel(Buffer const & buf) const
244 switch (type(params())) {
246 temp = buf.B_("Input");
249 temp = buf.B_("Verbatim Input");
252 temp = buf.B_("Verbatim Input*");
255 temp = buf.B_("Include");
258 temp = listings_label_;
264 if (params()["filename"].empty())
267 temp += from_utf8(onlyFilename(to_utf8(params()["filename"])));
275 /// return the child buffer if the file is a LyX doc and is loaded
276 Buffer * getChildBuffer(Buffer const & buffer, InsetCommandParams const & params)
278 if (isVerbatim(params) || isListings(params))
281 string const included_file = includedFilename(buffer, params).absFilename();
282 if (!isLyXFilename(included_file))
285 Buffer * childBuffer = theBufferList().getBuffer(included_file);
287 //FIXME RECURSIVE INCLUDES
288 if (childBuffer == & buffer)
297 /// return true if the file is or got loaded.
298 Buffer * loadIfNeeded(Buffer const & parent, InsetCommandParams const & params)
300 if (isVerbatim(params) || isListings(params))
303 string const parent_filename = parent.absFileName();
304 FileName const included_file = makeAbsPath(to_utf8(params["filename"]),
305 onlyPath(parent_filename));
307 if (!isLyXFilename(included_file.absFilename()))
310 Buffer * child = theBufferList().getBuffer(included_file.absFilename());
312 // the readonly flag can/will be wrong, not anymore I think.
313 if (!included_file.exists())
316 child = theBufferList().newBuffer(included_file.absFilename());
317 if (!child->loadLyXFile(included_file)) {
318 //close the buffer we just opened
319 theBufferList().close(child, false);
323 child->setParentName(parent_filename);
328 int InsetInclude::latex(Buffer const & buffer, odocstream & os,
329 OutputParams const & runparams) const
331 string incfile(to_utf8(params()["filename"]));
333 // Do nothing if no file name has been specified
337 FileName const included_file = includedFilename(buffer, params());
339 //Check we're not trying to include ourselves.
340 //FIXME RECURSIVE INCLUDE
341 //This isn't sufficient, as the inclusion could be downstream.
342 //But it'll have to do for now.
343 if (isInputOrInclude(params()) &&
344 buffer.absFileName() == included_file.absFilename())
346 Alert::error(_("Recursive input"),
347 bformat(_("Attempted to include file %1$s in itself! "
348 "Ignoring inclusion."), from_utf8(incfile)));
352 Buffer const * const masterBuffer = buffer.masterBuffer();
354 // if incfile is relative, make it relative to the master
356 if (!absolutePath(incfile)) {
358 incfile = to_utf8(makeRelPath(from_utf8(included_file.absFilename()),
359 from_utf8(masterBuffer->filePath())));
362 // write it to a file (so far the complete file)
363 string const exportfile = changeExtension(incfile, ".tex");
364 string const mangled =
365 DocFileName(changeExtension(included_file.absFilename(),".tex")).
367 FileName const writefile(makeAbsPath(mangled, masterBuffer->temppath()));
371 else if (!isValidLaTeXFilename(incfile)) {
372 frontend::Alert::warning(_("Invalid filename"),
373 _("The following filename is likely to cause trouble "
374 "when running the exported file through LaTeX: ") +
377 LYXERR(Debug::LATEX) << "incfile:" << incfile << endl;
378 LYXERR(Debug::LATEX) << "exportfile:" << exportfile << endl;
379 LYXERR(Debug::LATEX) << "writefile:" << writefile << endl;
381 if (runparams.inComment || runparams.dryrun) {
382 //Don't try to load or copy the file if we're
383 //in a comment or doing a dryrun
384 } else if (isInputOrInclude(params()) &&
385 isLyXFilename(included_file.absFilename())) {
386 //if it's a LyX file and we're inputting or including,
387 //try to load it so we can write the associated latex
388 if (!loadIfNeeded(buffer, params()))
391 Buffer * tmp = theBufferList().getBuffer(included_file.absFilename());
393 if (tmp->params().getBaseClass() != masterBuffer->params().getBaseClass()) {
395 docstring text = bformat(_("Included file `%1$s'\n"
396 "has textclass `%2$s'\n"
397 "while parent file has textclass `%3$s'."),
398 makeDisplayPath(included_file.absFilename()),
399 from_utf8(tmp->params().getTextClass().name()),
400 from_utf8(masterBuffer->params().getTextClass().name()));
401 Alert::warning(_("Different textclasses"), text);
405 // Make sure modules used in child are all included in master
406 //FIXME It might be worth loading the children's modules into the master
407 //over in BufferParams rather than doing this check.
408 vector<string> const masterModules = masterBuffer->params().getModules();
409 vector<string> const childModules = tmp->params().getModules();
410 vector<string>::const_iterator it = childModules.begin();
411 vector<string>::const_iterator end = childModules.end();
412 for (; it != end; ++it) {
413 string const module = *it;
414 vector<string>::const_iterator found =
415 find(masterModules.begin(), masterModules.end(), module);
416 if (found != masterModules.end()) {
417 docstring text = bformat(_("Included file `%1$s'\n"
418 "uses module `%2$s'\n"
419 "which is not used in parent file."),
420 makeDisplayPath(included_file.absFilename()), from_utf8(module));
421 Alert::warning(_("Module not found"), text);
425 tmp->markDepClean(masterBuffer->temppath());
427 // FIXME: handle non existing files
428 // FIXME: Second argument is irrelevant!
429 // since only_body is true, makeLaTeXFile will not look at second
430 // argument. Should we set it to string(), or should makeLaTeXFile
431 // make use of it somehow? (JMarc 20031002)
432 // The included file might be written in a different encoding
433 Encoding const * const oldEnc = runparams.encoding;
434 runparams.encoding = &tmp->params().encoding();
435 tmp->makeLaTeXFile(writefile,
436 onlyPath(masterFilename(buffer)),
438 runparams.encoding = oldEnc;
440 // In this case, it's not a LyX file, so we copy the file
441 // to the temp dir, so that .aux files etc. are not created
442 // in the original dir. Files included by this file will be
443 // found via input@path, see ../Buffer.cpp.
444 unsigned long const checksum_in = sum(included_file);
445 unsigned long const checksum_out = sum(writefile);
447 if (checksum_in != checksum_out) {
448 if (!copy(included_file, writefile)) {
451 << to_utf8(bformat(_("Could not copy the file\n%1$s\n"
452 "into the temporary directory."),
453 from_utf8(included_file.absFilename())))
460 string const tex_format = (runparams.flavor == OutputParams::LATEX) ?
461 "latex" : "pdflatex";
462 if (isVerbatim(params())) {
463 incfile = latex_path(incfile);
465 os << '\\' << from_ascii(params().getCmdName()) << '{'
466 << from_utf8(incfile) << '}';
467 } else if (type(params()) == INPUT) {
468 runparams.exportdata->addExternalFile(tex_format, writefile,
471 // \input wants file with extension (default is .tex)
472 if (!isLyXFilename(included_file.absFilename())) {
473 incfile = latex_path(incfile);
475 os << '\\' << from_ascii(params().getCmdName())
476 << '{' << from_utf8(incfile) << '}';
478 incfile = changeExtension(incfile, ".tex");
479 incfile = latex_path(incfile);
481 os << '\\' << from_ascii(params().getCmdName())
482 << '{' << from_utf8(incfile) << '}';
484 } else if (type(params()) == LISTINGS) {
485 os << '\\' << from_ascii(params().getCmdName());
486 string opt = params().getOptions();
487 // opt is set in QInclude dialog and should have passed validation.
488 InsetListingsParams params(opt);
489 if (!params.params().empty())
490 os << "[" << from_utf8(params.params()) << "]";
491 os << '{' << from_utf8(incfile) << '}';
493 runparams.exportdata->addExternalFile(tex_format, writefile,
496 // \include don't want extension and demands that the
497 // file really have .tex
498 incfile = changeExtension(incfile, string());
499 incfile = latex_path(incfile);
501 os << '\\' << from_ascii(params().getCmdName()) << '{'
502 << from_utf8(incfile) << '}';
509 int InsetInclude::plaintext(Buffer const & buffer, odocstream & os,
510 OutputParams const &) const
512 if (isVerbatim(params()) || isListings(params())) {
513 os << '[' << getScreenLabel(buffer) << '\n';
514 // FIXME: We don't know the encoding of the file
515 docstring const str =
516 from_utf8(includedFilename(buffer, params()).fileContents());
519 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
521 docstring const str = '[' + getScreenLabel(buffer) + ']';
528 int InsetInclude::docbook(Buffer const & buffer, odocstream & os,
529 OutputParams const & runparams) const
531 string incfile = to_utf8(params()["filename"]);
533 // Do nothing if no file name has been specified
537 string const included_file = includedFilename(buffer, params()).absFilename();
539 //Check we're not trying to include ourselves.
540 //FIXME RECURSIVE INCLUDE
541 //This isn't sufficient, as the inclusion could be downstream.
542 //But it'll have to do for now.
543 if (buffer.absFileName() == included_file) {
544 Alert::error(_("Recursive input"),
545 bformat(_("Attempted to include file %1$s in itself! "
546 "Ignoring inclusion."), from_utf8(incfile)));
550 // write it to a file (so far the complete file)
551 string const exportfile = changeExtension(incfile, ".sgml");
552 DocFileName writefile(changeExtension(included_file, ".sgml"));
554 if (loadIfNeeded(buffer, params())) {
555 Buffer * tmp = theBufferList().getBuffer(included_file);
557 string const mangled = writefile.mangledFilename();
558 writefile = makeAbsPath(mangled,
559 buffer.masterBuffer()->temppath());
563 LYXERR(Debug::LATEX) << "incfile:" << incfile << endl;
564 LYXERR(Debug::LATEX) << "exportfile:" << exportfile << endl;
565 LYXERR(Debug::LATEX) << "writefile:" << writefile << endl;
567 tmp->makeDocBookFile(writefile, runparams, true);
570 runparams.exportdata->addExternalFile("docbook", writefile,
572 runparams.exportdata->addExternalFile("docbook-xml", writefile,
575 if (isVerbatim(params()) || isListings(params())) {
576 os << "<inlinegraphic fileref=\""
577 << '&' << include_label << ';'
578 << "\" format=\"linespecific\">";
580 os << '&' << include_label << ';';
586 void InsetInclude::validate(LaTeXFeatures & features) const
588 string incfile(to_utf8(params()["filename"]));
591 Buffer const & buffer = features.buffer();
593 string const included_file = includedFilename(buffer, params()).absFilename();
595 if (isLyXFilename(included_file))
596 writefile = changeExtension(included_file, ".sgml");
598 writefile = included_file;
600 if (!features.runparams().nice && !isVerbatim(params()) && !isListings(params())) {
601 incfile = DocFileName(writefile).mangledFilename();
602 writefile = makeAbsPath(incfile,
603 buffer.masterBuffer()->temppath()).absFilename();
606 features.includeFile(include_label, writefile);
608 if (isVerbatim(params()))
609 features.require("verbatim");
610 else if (isListings(params()))
611 features.require("listings");
613 // Here we must do the fun stuff...
614 // Load the file in the include if it needs
616 if (loadIfNeeded(buffer, params())) {
618 Buffer * const tmp = theBufferList().getBuffer(included_file);
619 // make sure the buffer isn't us
620 // FIXME RECURSIVE INCLUDES
621 // This is not sufficient, as recursive includes could be
622 // more than a file away. But it will do for now.
623 if (tmp && tmp != & buffer) {
624 // We must temporarily change features.buffer,
625 // otherwise it would always be the master buffer,
626 // and nested includes would not work.
627 features.setBuffer(*tmp);
628 tmp->validate(features);
629 features.setBuffer(buffer);
635 void InsetInclude::getLabelList(Buffer const & buffer,
636 std::vector<docstring> & list) const
638 if (isListings(params())) {
639 InsetListingsParams p(params().getOptions());
640 string label = p.getParamValue("label");
642 list.push_back(from_utf8(label));
644 else if (loadIfNeeded(buffer, params())) {
645 string const included_file = includedFilename(buffer, params()).absFilename();
646 Buffer * tmp = theBufferList().getBuffer(included_file);
647 tmp->setParentName("");
648 tmp->getLabelList(list);
649 tmp->setParentName(parentFilename(buffer));
654 void InsetInclude::fillWithBibKeys(Buffer const & buffer,
655 BiblioInfo & keys, InsetIterator const & /*di*/) const
657 if (loadIfNeeded(buffer, params())) {
658 string const included_file = includedFilename(buffer, params()).absFilename();
659 Buffer * tmp = theBufferList().getBuffer(included_file);
660 //FIXME This is kind of a dirty hack and should be made reasonable.
661 tmp->setParentName("");
662 keys.fillWithBibKeys(tmp);
663 tmp->setParentName(parentFilename(buffer));
668 void InsetInclude::updateBibfilesCache(Buffer const & buffer)
670 Buffer * const tmp = getChildBuffer(buffer, params());
672 tmp->setParentName("");
673 tmp->updateBibfilesCache();
674 tmp->setParentName(parentFilename(buffer));
679 std::vector<FileName> const &
680 InsetInclude::getBibfilesCache(Buffer const & buffer) const
682 Buffer * const tmp = getChildBuffer(buffer, params());
684 tmp->setParentName("");
685 std::vector<FileName> const & cache = tmp->getBibfilesCache();
686 tmp->setParentName(parentFilename(buffer));
689 static std::vector<FileName> const empty;
694 void InsetInclude::metrics(MetricsInfo & mi, Dimension & dim) const
696 BOOST_ASSERT(mi.base.bv);
698 bool use_preview = false;
699 if (RenderPreview::status() != LyXRC::PREVIEW_OFF) {
700 graphics::PreviewImage const * pimage =
701 preview_->getPreviewImage(mi.base.bv->buffer());
702 use_preview = pimage && pimage->image();
706 preview_->metrics(mi, dim);
710 button_.update(getScreenLabel(mi.base.bv->buffer()),
713 button_.metrics(mi, dim);
716 Box b(0, dim.wid, -dim.asc, dim.des);
721 void InsetInclude::draw(PainterInfo & pi, int x, int y) const
723 BOOST_ASSERT(pi.base.bv);
725 bool use_preview = false;
726 if (RenderPreview::status() != LyXRC::PREVIEW_OFF) {
727 graphics::PreviewImage const * pimage =
728 preview_->getPreviewImage(pi.base.bv->buffer());
729 use_preview = pimage && pimage->image();
733 preview_->draw(pi, x, y);
735 button_.draw(pi, x, y);
739 Inset::DisplayType InsetInclude::display() const
741 return type(params()) == INPUT ? Inline : AlignCenter;
750 void InsetInclude::fileChanged() const
752 Buffer const * const buffer_ptr = LyX::cref().updateInset(this);
756 Buffer const & buffer = *buffer_ptr;
757 preview_->removePreview(buffer);
758 add_preview(*preview_.get(), *this, buffer);
759 preview_->startLoading(buffer);
765 bool preview_wanted(InsetCommandParams const & params, Buffer const & buffer)
767 FileName const included_file = includedFilename(buffer, params);
769 return type(params) == INPUT && params.preview() &&
770 included_file.isFileReadable();
774 docstring const latex_string(InsetInclude const & inset, Buffer const & buffer)
777 // We don't need to set runparams.encoding since this will be done
778 // by latex() anyway.
779 OutputParams runparams(0);
780 runparams.flavor = OutputParams::LATEX;
781 inset.latex(buffer, os, runparams);
787 void add_preview(RenderMonitoredPreview & renderer, InsetInclude const & inset,
788 Buffer const & buffer)
790 InsetCommandParams const & params = inset.params();
791 if (RenderPreview::status() != LyXRC::PREVIEW_OFF &&
792 preview_wanted(params, buffer)) {
793 renderer.setAbsFile(includedFilename(buffer, params));
794 docstring const snippet = latex_string(inset, buffer);
795 renderer.addPreview(snippet, buffer);
802 void InsetInclude::addPreview(graphics::PreviewLoader & ploader) const
804 Buffer const & buffer = ploader.buffer();
805 if (preview_wanted(params(), buffer)) {
806 preview_->setAbsFile(includedFilename(buffer, params()));
807 docstring const snippet = latex_string(*this, buffer);
808 preview_->addPreview(snippet, ploader);
813 void InsetInclude::addToToc(TocList & toclist, Buffer const & buffer,
814 ParConstIterator const & pit) const
816 if (isListings(params())) {
817 InsetListingsParams p(params().getOptions());
818 string caption = p.getParamValue("caption");
821 Toc & toc = toclist["listing"];
822 docstring const str = convert<docstring>(toc.size() + 1)
823 + ". " + from_utf8(caption);
824 // This inset does not have a valid ParConstIterator
825 // so it has to use the iterator of its parent paragraph
826 toc.push_back(TocItem(pit, 0, str));
829 Buffer const * const childbuffer = getChildBuffer(buffer, params());
833 TocList const & childtoclist = childbuffer->tocBackend().tocs();
834 TocList::const_iterator it = childtoclist.begin();
835 TocList::const_iterator const end = childtoclist.end();
836 for(; it != end; ++it)
837 toclist[it->first].insert(toclist[it->first].end(),
838 it->second.begin(), it->second.end());
842 void InsetInclude::updateLabels(Buffer const & buffer, ParIterator const &)
844 Buffer const * const childbuffer = getChildBuffer(buffer, params());
846 lyx::updateLabels(*childbuffer, true);
847 else if (isListings(params())) {
848 InsetListingsParams const par = params().getOptions();
849 if (par.getParamValue("caption").empty())
850 listings_label_.clear();
852 Counters & counters = buffer.params().getTextClass().counters();
853 docstring const cnt = from_ascii("listing");
854 if (counters.hasCounter(cnt)) {
856 listings_label_ = buffer.B_("Program Listing ")
857 + convert<docstring>(counters.value(cnt));
859 listings_label_ = buffer.B_("Program Listing");
865 void InsetInclude::registerEmbeddedFiles(Buffer const & buffer,
866 EmbeddedFiles & files) const
868 // include and input are temprarily not considered.
869 if (isVerbatim(params()) || isListings(params()))
870 files.registerFile(includedFilename(buffer, params()).absFilename(),