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"
23 #include "DispatchResult.h"
25 #include "FuncRequest.h"
26 #include "FuncStatus.h"
28 #include "LaTeXFeatures.h"
32 #include "MetricsInfo.h"
33 #include "OutputParams.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/filetools.h"
46 #include "support/lstrings.h" // contains
47 #include "support/lyxalgo.h"
48 #include "support/lyxlib.h"
49 #include "support/convert.h"
51 #include <boost/bind.hpp>
56 using support::addName;
57 using support::absolutePath;
58 using support::bformat;
59 using support::changeExtension;
60 using support::contains;
62 using support::DocFileName;
63 using support::FileName;
64 using support::getVectorFromString;
65 using support::isLyXFilename;
66 using support::isValidLaTeXFilename;
67 using support::latex_path;
68 using support::makeAbsPath;
69 using support::makeDisplayPath;
70 using support::makeRelPath;
71 using support::onlyFilename;
72 using support::onlyPath;
73 using support::prefixIs;
80 using std::istringstream;
82 using std::ostringstream;
85 namespace Alert = frontend::Alert;
90 docstring const uniqueID()
92 static unsigned int seed = 1000;
93 return "file" + convert<docstring>(++seed);
97 /// the type of inclusion
107 Types type(InsetCommandParams const & params)
109 string const command_name = params.getCmdName();
111 if (command_name == "input")
113 if (command_name == "verbatiminput")
115 if (command_name == "verbatiminput*")
117 if (command_name == "lstinputlisting")
123 bool isListings(InsetCommandParams const & params)
125 return type(params) == LISTINGS;
129 bool isVerbatim(InsetCommandParams const & params)
131 Types const t = type(params);
132 return t == VERB || t == VERBAST;
136 bool isInputOrInclude(InsetCommandParams const & params)
138 Types const t = type(params);
139 return t == INPUT || t == INCLUDE;
145 InsetInclude::InsetInclude(InsetCommandParams const & p)
146 : InsetCommand(p, "include"), include_label(uniqueID()),
147 preview_(new RenderMonitoredPreview(this)), set_label_(false)
149 preview_->fileChanged(boost::bind(&InsetInclude::fileChanged, this));
153 InsetInclude::InsetInclude(InsetInclude const & other)
154 : InsetCommand(other), include_label(other.include_label),
155 preview_(new RenderMonitoredPreview(this)), set_label_(false)
157 preview_->fileChanged(boost::bind(&InsetInclude::fileChanged, this));
161 void InsetInclude::doDispatch(Cursor & cur, FuncRequest & cmd)
163 switch (cmd.action) {
165 case LFUN_INSET_MODIFY: {
166 InsetCommandParams p(INCLUDE_CODE);
167 InsetCommandMailer::string2params("include", to_utf8(cmd.argument()), p);
168 if (!p.getCmdName().empty()) {
170 InsetListingsParams par_old(to_utf8(params()["lstparams"]));
171 InsetListingsParams par_new(to_utf8(p["lstparams"]));
172 if (par_old.getParamValue("label") !=
173 par_new.getParamValue("label")
174 && !par_new.getParamValue("label").empty())
175 cur.bv().buffer().changeRefsIfUnique(
176 from_utf8(par_old.getParamValue("label")),
177 from_utf8(par_new.getParamValue("label")),
180 set(p, cur.buffer());
181 cur.buffer().updateBibfilesCache();
187 //pass everything else up the chain
189 InsetCommand::doDispatch(cur, cmd);
197 string const masterFilename(Buffer const & buffer)
199 return buffer.masterBuffer()->absFileName();
203 string const parentFilename(Buffer const & buffer)
205 return buffer.absFileName();
209 FileName const includedFilename(Buffer const & buffer,
210 InsetCommandParams const & params)
212 return makeAbsPath(to_utf8(params["filename"]),
213 onlyPath(parentFilename(buffer)));
217 void add_preview(RenderMonitoredPreview &, InsetInclude const &, Buffer const &);
222 void InsetInclude::set(InsetCommandParams const & p, Buffer const & buffer)
227 if (preview_->monitoring())
228 preview_->stopMonitoring();
230 if (type(params()) == INPUT)
231 add_preview(*preview_, *this, buffer);
235 Inset * InsetInclude::clone() const
237 return new InsetInclude(*this);
241 docstring const InsetInclude::getScreenLabel(Buffer const & buf) const
245 switch (type(params())) {
247 temp = buf.B_("Input");
250 temp = buf.B_("Verbatim Input");
253 temp = buf.B_("Verbatim Input*");
256 temp = buf.B_("Include");
259 temp = listings_label_;
265 if (params()["filename"].empty())
268 temp += from_utf8(onlyFilename(to_utf8(params()["filename"])));
276 /// return the child buffer if the file is a LyX doc and is loaded
277 Buffer * getChildBuffer(Buffer const & buffer, InsetCommandParams const & params)
279 if (isVerbatim(params) || isListings(params))
282 string const included_file = includedFilename(buffer, params).absFilename();
283 if (!isLyXFilename(included_file))
286 Buffer * childBuffer = theBufferList().getBuffer(included_file);
288 //FIXME RECURSIVE INCLUDES
289 if (childBuffer == & buffer)
298 /// return true if the file is or got loaded.
299 Buffer * loadIfNeeded(Buffer const & parent, InsetCommandParams const & params)
301 if (isVerbatim(params) || isListings(params))
304 string const parent_filename = parent.absFileName();
305 FileName const included_file = makeAbsPath(to_utf8(params["filename"]),
306 onlyPath(parent_filename));
308 if (!isLyXFilename(included_file.absFilename()))
311 Buffer * child = theBufferList().getBuffer(included_file.absFilename());
313 // the readonly flag can/will be wrong, not anymore I think.
314 if (!included_file.exists())
317 child = theBufferList().newBuffer(included_file.absFilename());
318 if (!child->loadLyXFile(included_file)) {
319 //close the buffer we just opened
320 theBufferList().close(child, false);
324 child->setParentName(parent_filename);
329 int InsetInclude::latex(Buffer const & buffer, odocstream & os,
330 OutputParams const & runparams) const
332 string incfile(to_utf8(params()["filename"]));
334 // Do nothing if no file name has been specified
338 FileName const included_file = includedFilename(buffer, params());
340 //Check we're not trying to include ourselves.
341 //FIXME RECURSIVE INCLUDE
342 //This isn't sufficient, as the inclusion could be downstream.
343 //But it'll have to do for now.
344 if (isInputOrInclude(params()) &&
345 buffer.absFileName() == included_file.absFilename())
347 Alert::error(_("Recursive input"),
348 bformat(_("Attempted to include file %1$s in itself! "
349 "Ignoring inclusion."), from_utf8(incfile)));
353 Buffer const * const masterBuffer = buffer.masterBuffer();
355 // if incfile is relative, make it relative to the master
357 if (!absolutePath(incfile)) {
359 incfile = to_utf8(makeRelPath(from_utf8(included_file.absFilename()),
360 from_utf8(masterBuffer->filePath())));
363 // write it to a file (so far the complete file)
364 string const exportfile = changeExtension(incfile, ".tex");
365 string const mangled =
366 DocFileName(changeExtension(included_file.absFilename(),".tex")).
368 FileName const writefile(makeAbsPath(mangled, masterBuffer->temppath()));
372 else if (!isValidLaTeXFilename(incfile)) {
373 frontend::Alert::warning(_("Invalid filename"),
374 _("The following filename is likely to cause trouble "
375 "when running the exported file through LaTeX: ") +
378 LYXERR(Debug::LATEX) << "incfile:" << incfile << endl;
379 LYXERR(Debug::LATEX) << "exportfile:" << exportfile << endl;
380 LYXERR(Debug::LATEX) << "writefile:" << writefile << endl;
382 if (runparams.inComment || runparams.dryrun) {
383 //Don't try to load or copy the file if we're
384 //in a comment or doing a dryrun
385 } else if (isInputOrInclude(params()) &&
386 isLyXFilename(included_file.absFilename())) {
387 //if it's a LyX file and we're inputting or including,
388 //try to load it so we can write the associated latex
389 if (!loadIfNeeded(buffer, params()))
392 Buffer * tmp = theBufferList().getBuffer(included_file.absFilename());
394 if (tmp->params().getBaseClass() != masterBuffer->params().getBaseClass()) {
396 docstring text = bformat(_("Included file `%1$s'\n"
397 "has textclass `%2$s'\n"
398 "while parent file has textclass `%3$s'."),
399 makeDisplayPath(included_file.absFilename()),
400 from_utf8(tmp->params().getTextClass().name()),
401 from_utf8(masterBuffer->params().getTextClass().name()));
402 Alert::warning(_("Different textclasses"), text);
406 // Make sure modules used in child are all included in master
407 //FIXME It might be worth loading the children's modules into the master
408 //over in BufferParams rather than doing this check.
409 vector<string> const masterModules = masterBuffer->params().getModules();
410 vector<string> const childModules = tmp->params().getModules();
411 vector<string>::const_iterator it = childModules.begin();
412 vector<string>::const_iterator end = childModules.end();
413 for (; it != end; ++it) {
414 string const module = *it;
415 vector<string>::const_iterator found =
416 find(masterModules.begin(), masterModules.end(), module);
417 if (found != masterModules.end()) {
418 docstring text = bformat(_("Included file `%1$s'\n"
419 "uses module `%2$s'\n"
420 "which is not used in parent file."),
421 makeDisplayPath(included_file.absFilename()), from_utf8(module));
422 Alert::warning(_("Module not found"), text);
426 tmp->markDepClean(masterBuffer->temppath());
428 // FIXME: handle non existing files
429 // FIXME: Second argument is irrelevant!
430 // since only_body is true, makeLaTeXFile will not look at second
431 // argument. Should we set it to string(), or should makeLaTeXFile
432 // make use of it somehow? (JMarc 20031002)
433 // The included file might be written in a different encoding
434 Encoding const * const oldEnc = runparams.encoding;
435 runparams.encoding = &tmp->params().encoding();
436 tmp->makeLaTeXFile(writefile,
437 onlyPath(masterFilename(buffer)),
439 runparams.encoding = oldEnc;
441 // In this case, it's not a LyX file, so we copy the file
442 // to the temp dir, so that .aux files etc. are not created
443 // in the original dir. Files included by this file will be
444 // found via input@path, see ../Buffer.cpp.
445 unsigned long const checksum_in = sum(included_file);
446 unsigned long const checksum_out = sum(writefile);
448 if (checksum_in != checksum_out) {
449 if (!copy(included_file, writefile)) {
452 << to_utf8(bformat(_("Could not copy the file\n%1$s\n"
453 "into the temporary directory."),
454 from_utf8(included_file.absFilename())))
461 string const tex_format = (runparams.flavor == OutputParams::LATEX) ?
462 "latex" : "pdflatex";
463 if (isVerbatim(params())) {
464 incfile = latex_path(incfile);
466 os << '\\' << from_ascii(params().getCmdName()) << '{'
467 << from_utf8(incfile) << '}';
468 } else if (type(params()) == INPUT) {
469 runparams.exportdata->addExternalFile(tex_format, writefile,
472 // \input wants file with extension (default is .tex)
473 if (!isLyXFilename(included_file.absFilename())) {
474 incfile = latex_path(incfile);
476 os << '\\' << from_ascii(params().getCmdName())
477 << '{' << from_utf8(incfile) << '}';
479 incfile = changeExtension(incfile, ".tex");
480 incfile = latex_path(incfile);
482 os << '\\' << from_ascii(params().getCmdName())
483 << '{' << from_utf8(incfile) << '}';
485 } else if (type(params()) == LISTINGS) {
486 os << '\\' << from_ascii(params().getCmdName());
487 string const opt = to_utf8(params()["lstparams"]);
488 // opt is set in QInclude dialog and should have passed validation.
489 InsetListingsParams params(opt);
490 if (!params.params().empty())
491 os << "[" << from_utf8(params.params()) << "]";
492 os << '{' << from_utf8(incfile) << '}';
494 runparams.exportdata->addExternalFile(tex_format, writefile,
497 // \include don't want extension and demands that the
498 // file really have .tex
499 incfile = changeExtension(incfile, string());
500 incfile = latex_path(incfile);
502 os << '\\' << from_ascii(params().getCmdName()) << '{'
503 << from_utf8(incfile) << '}';
510 int InsetInclude::plaintext(Buffer const & buffer, odocstream & os,
511 OutputParams const &) const
513 if (isVerbatim(params()) || isListings(params())) {
514 os << '[' << getScreenLabel(buffer) << '\n';
515 // FIXME: We don't know the encoding of the file
516 docstring const str =
517 from_utf8(includedFilename(buffer, params()).fileContents());
520 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
522 docstring const str = '[' + getScreenLabel(buffer) + ']';
529 int InsetInclude::docbook(Buffer const & buffer, odocstream & os,
530 OutputParams const & runparams) const
532 string incfile = to_utf8(params()["filename"]);
534 // Do nothing if no file name has been specified
538 string const included_file = includedFilename(buffer, params()).absFilename();
540 //Check we're not trying to include ourselves.
541 //FIXME RECURSIVE INCLUDE
542 //This isn't sufficient, as the inclusion could be downstream.
543 //But it'll have to do for now.
544 if (buffer.absFileName() == included_file) {
545 Alert::error(_("Recursive input"),
546 bformat(_("Attempted to include file %1$s in itself! "
547 "Ignoring inclusion."), from_utf8(incfile)));
551 // write it to a file (so far the complete file)
552 string const exportfile = changeExtension(incfile, ".sgml");
553 DocFileName writefile(changeExtension(included_file, ".sgml"));
555 if (loadIfNeeded(buffer, params())) {
556 Buffer * tmp = theBufferList().getBuffer(included_file);
558 string const mangled = writefile.mangledFilename();
559 writefile = makeAbsPath(mangled,
560 buffer.masterBuffer()->temppath());
564 LYXERR(Debug::LATEX) << "incfile:" << incfile << endl;
565 LYXERR(Debug::LATEX) << "exportfile:" << exportfile << endl;
566 LYXERR(Debug::LATEX) << "writefile:" << writefile << endl;
568 tmp->makeDocBookFile(writefile, runparams, true);
571 runparams.exportdata->addExternalFile("docbook", writefile,
573 runparams.exportdata->addExternalFile("docbook-xml", writefile,
576 if (isVerbatim(params()) || isListings(params())) {
577 os << "<inlinegraphic fileref=\""
578 << '&' << include_label << ';'
579 << "\" format=\"linespecific\">";
581 os << '&' << include_label << ';';
587 void InsetInclude::validate(LaTeXFeatures & features) const
589 string incfile = to_utf8(params()["filename"]);
592 Buffer const & buffer = features.buffer();
594 string const included_file = includedFilename(buffer, params()).absFilename();
596 if (isLyXFilename(included_file))
597 writefile = changeExtension(included_file, ".sgml");
599 writefile = included_file;
601 if (!features.runparams().nice && !isVerbatim(params()) && !isListings(params())) {
602 incfile = DocFileName(writefile).mangledFilename();
603 writefile = makeAbsPath(incfile,
604 buffer.masterBuffer()->temppath()).absFilename();
607 features.includeFile(include_label, writefile);
609 if (isVerbatim(params()))
610 features.require("verbatim");
611 else if (isListings(params()))
612 features.require("listings");
614 // Here we must do the fun stuff...
615 // Load the file in the include if it needs
617 if (loadIfNeeded(buffer, params())) {
619 Buffer * const tmp = theBufferList().getBuffer(included_file);
620 // make sure the buffer isn't us
621 // FIXME RECURSIVE INCLUDES
622 // This is not sufficient, as recursive includes could be
623 // more than a file away. But it will do for now.
624 if (tmp && tmp != & buffer) {
625 // We must temporarily change features.buffer,
626 // otherwise it would always be the master buffer,
627 // and nested includes would not work.
628 features.setBuffer(*tmp);
629 tmp->validate(features);
630 features.setBuffer(buffer);
636 void InsetInclude::getLabelList(Buffer const & buffer,
637 std::vector<docstring> & list) const
639 if (isListings(params())) {
640 InsetListingsParams p(to_utf8(params()["lstparams"]));
641 string label = p.getParamValue("label");
643 list.push_back(from_utf8(label));
645 else if (loadIfNeeded(buffer, params())) {
646 string const included_file = includedFilename(buffer, params()).absFilename();
647 Buffer * tmp = theBufferList().getBuffer(included_file);
648 tmp->setParentName("");
649 tmp->getLabelList(list);
650 tmp->setParentName(parentFilename(buffer));
655 void InsetInclude::fillWithBibKeys(Buffer const & buffer,
656 BiblioInfo & keys, InsetIterator const & /*di*/) const
658 if (loadIfNeeded(buffer, params())) {
659 string const included_file = includedFilename(buffer, params()).absFilename();
660 Buffer * tmp = theBufferList().getBuffer(included_file);
661 //FIXME This is kind of a dirty hack and should be made reasonable.
662 tmp->setParentName("");
663 keys.fillWithBibKeys(tmp);
664 tmp->setParentName(parentFilename(buffer));
669 void InsetInclude::updateBibfilesCache(Buffer const & buffer)
671 Buffer * const tmp = getChildBuffer(buffer, params());
673 tmp->setParentName("");
674 tmp->updateBibfilesCache();
675 tmp->setParentName(parentFilename(buffer));
680 std::vector<FileName> const &
681 InsetInclude::getBibfilesCache(Buffer const & buffer) const
683 Buffer * const tmp = getChildBuffer(buffer, params());
685 tmp->setParentName("");
686 std::vector<FileName> const & cache = tmp->getBibfilesCache();
687 tmp->setParentName(parentFilename(buffer));
690 static std::vector<FileName> const empty;
695 void InsetInclude::metrics(MetricsInfo & mi, Dimension & dim) const
697 BOOST_ASSERT(mi.base.bv);
699 bool use_preview = false;
700 if (RenderPreview::status() != LyXRC::PREVIEW_OFF) {
701 graphics::PreviewImage const * pimage =
702 preview_->getPreviewImage(mi.base.bv->buffer());
703 use_preview = pimage && pimage->image();
707 preview_->metrics(mi, dim);
711 button_.update(getScreenLabel(mi.base.bv->buffer()),
714 button_.metrics(mi, dim);
717 Box b(0, dim.wid, -dim.asc, dim.des);
722 void InsetInclude::draw(PainterInfo & pi, int x, int y) const
724 BOOST_ASSERT(pi.base.bv);
726 bool use_preview = false;
727 if (RenderPreview::status() != LyXRC::PREVIEW_OFF) {
728 graphics::PreviewImage const * pimage =
729 preview_->getPreviewImage(pi.base.bv->buffer());
730 use_preview = pimage && pimage->image();
734 preview_->draw(pi, x, y);
736 button_.draw(pi, x, y);
740 Inset::DisplayType InsetInclude::display() const
742 return type(params()) == INPUT ? Inline : AlignCenter;
751 void InsetInclude::fileChanged() const
753 Buffer const * const buffer_ptr = LyX::cref().updateInset(this);
757 Buffer const & buffer = *buffer_ptr;
758 preview_->removePreview(buffer);
759 add_preview(*preview_.get(), *this, buffer);
760 preview_->startLoading(buffer);
766 bool preview_wanted(InsetCommandParams const & params, Buffer const & buffer)
768 FileName const included_file = includedFilename(buffer, params);
770 return type(params) == INPUT && params.preview() &&
771 included_file.isFileReadable();
775 docstring const latex_string(InsetInclude const & inset, Buffer const & buffer)
778 // We don't need to set runparams.encoding since this will be done
779 // by latex() anyway.
780 OutputParams runparams(0);
781 runparams.flavor = OutputParams::LATEX;
782 inset.latex(buffer, os, runparams);
788 void add_preview(RenderMonitoredPreview & renderer, InsetInclude const & inset,
789 Buffer const & buffer)
791 InsetCommandParams const & params = inset.params();
792 if (RenderPreview::status() != LyXRC::PREVIEW_OFF &&
793 preview_wanted(params, buffer)) {
794 renderer.setAbsFile(includedFilename(buffer, params));
795 docstring const snippet = latex_string(inset, buffer);
796 renderer.addPreview(snippet, buffer);
803 void InsetInclude::addPreview(graphics::PreviewLoader & ploader) const
805 Buffer const & buffer = ploader.buffer();
806 if (preview_wanted(params(), buffer)) {
807 preview_->setAbsFile(includedFilename(buffer, params()));
808 docstring const snippet = latex_string(*this, buffer);
809 preview_->addPreview(snippet, ploader);
814 void InsetInclude::addToToc(TocList & toclist, Buffer const & buffer,
815 ParConstIterator const & pit) const
817 if (isListings(params())) {
818 InsetListingsParams p(to_utf8(params()["lstparams"]));
819 string caption = p.getParamValue("caption");
822 Toc & toc = toclist["listing"];
823 docstring const str = convert<docstring>(toc.size() + 1)
824 + ". " + from_utf8(caption);
825 // This inset does not have a valid ParConstIterator
826 // so it has to use the iterator of its parent paragraph
827 toc.push_back(TocItem(pit, 0, str));
830 Buffer const * const childbuffer = getChildBuffer(buffer, params());
834 TocList const & childtoclist = childbuffer->tocBackend().tocs();
835 TocList::const_iterator it = childtoclist.begin();
836 TocList::const_iterator const end = childtoclist.end();
837 for(; it != end; ++it)
838 toclist[it->first].insert(toclist[it->first].end(),
839 it->second.begin(), it->second.end());
843 void InsetInclude::updateLabels(Buffer const & buffer, ParIterator const &)
845 Buffer const * const childbuffer = getChildBuffer(buffer, params());
847 lyx::updateLabels(*childbuffer, true);
848 else if (isListings(params())) {
849 InsetListingsParams const par(to_utf8(params()["lstparams"]));
850 if (par.getParamValue("caption").empty())
851 listings_label_.clear();
853 Counters & counters = buffer.params().getTextClass().counters();
854 docstring const cnt = from_ascii("listing");
855 if (counters.hasCounter(cnt)) {
857 listings_label_ = buffer.B_("Program Listing ")
858 + convert<docstring>(counters.value(cnt));
860 listings_label_ = buffer.B_("Program Listing");
866 void InsetInclude::registerEmbeddedFiles(Buffer const & buffer,
867 EmbeddedFiles & files) const
869 // include and input are temprarily not considered.
870 if (isVerbatim(params()) || isListings(params()))
871 files.registerFile(includedFilename(buffer, params()).absFilename(),