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>
51 #include <boost/filesystem/operations.hpp>
56 // Implementation is in LyX.cpp
57 extern void dispatch(FuncRequest const & action);
59 using support::addName;
60 using support::absolutePath;
61 using support::bformat;
62 using support::changeExtension;
63 using support::contains;
65 using support::DocFileName;
66 using support::FileName;
67 using support::getFileContents;
68 using support::getVectorFromString;
69 using support::isFileReadable;
70 using support::isLyXFilename;
71 using support::latex_path;
72 using support::makeAbsPath;
73 using support::makeDisplayPath;
74 using support::makeRelPath;
75 using support::onlyFilename;
76 using support::onlyPath;
77 using support::prefixIs;
84 using std::istringstream;
86 using std::ostringstream;
89 namespace Alert = frontend::Alert;
90 namespace fs = boost::filesystem;
95 docstring const uniqueID()
97 static unsigned int seed = 1000;
98 return "file" + convert<docstring>(++seed);
102 bool isListings(InsetCommandParams const & params)
104 return params.getCmdName() == "lstinputlisting";
110 InsetInclude::InsetInclude(InsetCommandParams const & p)
111 : params_(p), include_label(uniqueID()),
112 preview_(new RenderMonitoredPreview(this)),
113 set_label_(false), counter_(0)
115 preview_->fileChanged(boost::bind(&InsetInclude::fileChanged, this));
119 InsetInclude::InsetInclude(InsetInclude const & other)
121 params_(other.params_),
122 include_label(other.include_label),
123 preview_(new RenderMonitoredPreview(this)),
124 set_label_(false), counter_(0)
126 preview_->fileChanged(boost::bind(&InsetInclude::fileChanged, this));
130 InsetInclude::~InsetInclude()
132 InsetIncludeMailer(*this).hideDialog();
136 void InsetInclude::doDispatch(Cursor & cur, FuncRequest & cmd)
138 switch (cmd.action) {
140 case LFUN_INSET_MODIFY: {
141 InsetCommandParams p("include");
142 InsetIncludeMailer::string2params(to_utf8(cmd.argument()), p);
143 if (!p.getCmdName().empty()) {
145 InsetListingsParams par_old(params().getOptions());
146 InsetListingsParams par_new(p.getOptions());
147 if (par_old.getParamValue("label") !=
148 par_new.getParamValue("label")
149 && !par_new.getParamValue("label").empty())
150 cur.bv().buffer()->changeRefsIfUnique(
151 from_utf8(par_old.getParamValue("label")),
152 from_utf8(par_new.getParamValue("label")),
155 set(p, cur.buffer());
156 cur.buffer().updateBibfilesCache();
162 case LFUN_INSET_DIALOG_UPDATE:
163 InsetIncludeMailer(*this).updateDialog(&cur.bv());
166 case LFUN_MOUSE_RELEASE:
167 if (!cur.selection())
168 InsetIncludeMailer(*this).showDialog(&cur.bv());
172 Inset::doDispatch(cur, cmd);
178 bool InsetInclude::getStatus(Cursor & cur, FuncRequest const & cmd,
179 FuncStatus & flag) const
181 switch (cmd.action) {
183 case LFUN_INSET_MODIFY:
184 case LFUN_INSET_DIALOG_UPDATE:
189 return Inset::getStatus(cur, cmd, flag);
194 InsetCommandParams const & InsetInclude::params() const
202 /// the type of inclusion
212 Types type(InsetCommandParams const & params)
214 string const command_name = params.getCmdName();
216 if (command_name == "input")
218 if (command_name == "verbatiminput")
220 if (command_name == "verbatiminput*")
222 if (command_name == "lstinputlisting")
228 bool isVerbatim(InsetCommandParams const & params)
230 string const command_name = params.getCmdName();
231 return command_name == "verbatiminput" ||
232 command_name == "verbatiminput*";
236 bool isInputOrInclude(InsetCommandParams const & params)
238 Types const t = type(params);
239 return (t == INPUT) || (t == INCLUDE);
243 string const masterFilename(Buffer const & buffer)
245 return buffer.getMasterBuffer()->fileName();
249 string const parentFilename(Buffer const & buffer)
251 return buffer.fileName();
255 FileName const includedFilename(Buffer const & buffer,
256 InsetCommandParams const & params)
258 return makeAbsPath(to_utf8(params["filename"]),
259 onlyPath(parentFilename(buffer)));
263 void add_preview(RenderMonitoredPreview &, InsetInclude const &, Buffer const &);
268 void InsetInclude::set(InsetCommandParams const & p, Buffer const & buffer)
273 if (preview_->monitoring())
274 preview_->stopMonitoring();
276 if (type(params_) == INPUT)
277 add_preview(*preview_, *this, buffer);
281 auto_ptr<Inset> InsetInclude::doClone() const
283 return auto_ptr<Inset>(new InsetInclude(*this));
287 void InsetInclude::write(Buffer const &, ostream & os) const
293 void InsetInclude::write(ostream & os) const
295 os << "Include " << to_utf8(params_.getCommand()) << '\n'
296 << "preview " << convert<string>(params_.preview()) << '\n';
300 void InsetInclude::read(Buffer const &, Lexer & lex)
306 void InsetInclude::read(Lexer & lex)
310 string const command = lex.getString();
311 params_.scanCommand(command);
316 token = lex.getString();
317 if (token == "\\end_inset")
319 if (token == "preview") {
321 params_.preview(lex.getBool());
323 lex.printError("Unknown parameter name `$$Token' for command " + params_.getCmdName());
325 if (token != "\\end_inset") {
326 lex.printError("Missing \\end_inset at this point. "
332 docstring const InsetInclude::getScreenLabel(Buffer const & buf) const
336 switch (type(params_)) {
338 temp += buf.B_("Input");
341 temp += buf.B_("Verbatim Input");
344 temp += buf.B_("Verbatim Input*");
347 temp += buf.B_("Include");
351 temp += buf.B_("Program Listing ") + convert<docstring>(counter_);
353 temp += buf.B_("Program Listing");
360 if (params_["filename"].empty())
363 temp += from_utf8(onlyFilename(to_utf8(params_["filename"])));
371 /// return the child buffer if the file is a LyX doc and is loaded
372 Buffer * getChildBuffer(Buffer const & buffer, InsetCommandParams const & params)
374 if (isVerbatim(params) || isListings(params))
377 string const included_file = includedFilename(buffer, params).absFilename();
378 if (!isLyXFilename(included_file))
381 Buffer * childBuffer = theBufferList().getBuffer(included_file);
383 //FIXME RECURSIVE INCLUDES
384 if (childBuffer == & buffer)
391 /// return true if the file is or got loaded.
392 bool loadIfNeeded(Buffer const & buffer, InsetCommandParams const & params)
394 if (isVerbatim(params) || isListings(params))
397 FileName const included_file = includedFilename(buffer, params);
398 if (!isLyXFilename(included_file.absFilename()))
401 Buffer * buf = theBufferList().getBuffer(included_file.absFilename());
403 // the readonly flag can/will be wrong, not anymore I think.
404 if (!fs::exists(included_file.toFilesystemEncoding()))
406 lyx::dispatch(FuncRequest(LFUN_BUFFER_CHILD_OPEN,
407 included_file.absFilename() + "|true"));
408 buf = theBufferList().getBuffer(included_file.absFilename());
411 buf->setParentName(parentFilename(buffer));
419 int InsetInclude::latex(Buffer const & buffer, odocstream & os,
420 OutputParams const & runparams) const
422 string incfile(to_utf8(params_["filename"]));
424 // Do nothing if no file name has been specified
428 FileName const included_file(includedFilename(buffer, params_));
430 //Check we're not trying to include ourselves.
431 //FIXME RECURSIVE INCLUDE
432 //This isn't sufficient, as the inclusion could be downstream.
433 //But it'll have to do for now.
434 if (isInputOrInclude(params_) &&
435 buffer.fileName() == included_file.absFilename())
437 Alert::error(_("Recursive input"),
438 bformat(_("Attempted to include file %1$s in itself! "
439 "Ignoring inclusion."), from_utf8(incfile)));
443 Buffer const * const m_buffer = buffer.getMasterBuffer();
445 // if incfile is relative, make it relative to the master
447 if (!absolutePath(incfile)) {
449 incfile = to_utf8(makeRelPath(from_utf8(included_file.absFilename()),
450 from_utf8(m_buffer->filePath())));
453 // write it to a file (so far the complete file)
454 string const exportfile = changeExtension(incfile, ".tex");
455 string const mangled =
456 DocFileName(changeExtension(included_file.absFilename(),".tex")).
458 FileName const writefile(makeAbsPath(mangled, m_buffer->temppath()));
462 LYXERR(Debug::LATEX) << "incfile:" << incfile << endl;
463 LYXERR(Debug::LATEX) << "exportfile:" << exportfile << endl;
464 LYXERR(Debug::LATEX) << "writefile:" << writefile << endl;
466 if (runparams.inComment || runparams.dryrun) {
467 //Don't try to load or copy the file if we're
468 //in a comment or doing a dryrun
469 } else if (isInputOrInclude(params_) &&
470 isLyXFilename(included_file.absFilename())) {
471 //if it's a LyX file and we're inputting or including,
472 //try to load it so we can write the associated latex
473 if (!loadIfNeeded(buffer, params_))
476 Buffer * tmp = theBufferList().getBuffer(included_file.absFilename());
478 if (tmp->params().textclass != m_buffer->params().textclass) {
480 docstring text = bformat(_("Included file `%1$s'\n"
481 "has textclass `%2$s'\n"
482 "while parent file has textclass `%3$s'."),
483 makeDisplayPath(included_file.absFilename()),
484 from_utf8(tmp->params().getTextClass().name()),
485 from_utf8(m_buffer->params().getTextClass().name()));
486 Alert::warning(_("Different textclasses"), text);
490 tmp->markDepClean(m_buffer->temppath());
493 #warning handle non existing files
494 #warning Second argument is irrelevant!
495 // since only_body is true, makeLaTeXFile will not look at second
496 // argument. Should we set it to string(), or should makeLaTeXFile
497 // make use of it somehow? (JMarc 20031002)
499 // The included file might be written in a different encoding
500 Encoding const * const oldEnc = runparams.encoding;
501 runparams.encoding = &tmp->params().encoding();
502 tmp->makeLaTeXFile(writefile,
503 onlyPath(masterFilename(buffer)),
505 runparams.encoding = oldEnc;
507 // In this case, it's not a LyX file, so we copy the file
508 // to the temp dir, so that .aux files etc. are not created
509 // in the original dir. Files included by this file will be
510 // found via input@path, see ../Buffer.cpp.
511 unsigned long const checksum_in = sum(included_file);
512 unsigned long const checksum_out = sum(writefile);
514 if (checksum_in != checksum_out) {
515 if (!copy(included_file, writefile)) {
518 << to_utf8(bformat(_("Could not copy the file\n%1$s\n"
519 "into the temporary directory."),
520 from_utf8(included_file.absFilename())))
527 string const tex_format = (runparams.flavor == OutputParams::LATEX) ?
528 "latex" : "pdflatex";
529 if (isVerbatim(params_)) {
530 incfile = latex_path(incfile);
532 os << '\\' << from_ascii(params_.getCmdName()) << '{'
533 << from_utf8(incfile) << '}';
534 } else if (type(params_) == INPUT) {
535 runparams.exportdata->addExternalFile(tex_format, writefile,
538 // \input wants file with extension (default is .tex)
539 if (!isLyXFilename(included_file.absFilename())) {
540 incfile = latex_path(incfile);
542 os << '\\' << from_ascii(params_.getCmdName())
543 << '{' << from_utf8(incfile) << '}';
545 incfile = changeExtension(incfile, ".tex");
546 incfile = latex_path(incfile);
548 os << '\\' << from_ascii(params_.getCmdName())
549 << '{' << from_utf8(incfile) << '}';
551 } else if (type(params_) == LISTINGS) {
552 os << '\\' << from_ascii(params_.getCmdName());
553 string opt = params_.getOptions();
554 // opt is set in QInclude dialog and should have passed validation.
555 InsetListingsParams params(opt);
556 if (!params.params().empty())
557 os << "[" << from_utf8(params.params()) << "]";
558 os << '{' << from_utf8(incfile) << '}';
560 runparams.exportdata->addExternalFile(tex_format, writefile,
563 // \include don't want extension and demands that the
564 // file really have .tex
565 incfile = changeExtension(incfile, string());
566 incfile = latex_path(incfile);
568 os << '\\' << from_ascii(params_.getCmdName()) << '{'
569 << from_utf8(incfile) << '}';
576 int InsetInclude::plaintext(Buffer const & buffer, odocstream & os,
577 OutputParams const &) const
579 if (isVerbatim(params_) || isListings(params_)) {
580 os << '[' << getScreenLabel(buffer) << '\n';
581 // FIXME: We don't know the encoding of the file
582 docstring const str =
583 from_utf8(getFileContents(includedFilename(buffer, params_)));
586 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
588 docstring const str = '[' + getScreenLabel(buffer) + ']';
595 int InsetInclude::docbook(Buffer const & buffer, odocstream & os,
596 OutputParams const & runparams) const
598 string incfile = to_utf8(params_["filename"]);
600 // Do nothing if no file name has been specified
604 string const included_file = includedFilename(buffer, params_).absFilename();
606 //Check we're not trying to include ourselves.
607 //FIXME RECURSIVE INCLUDE
608 //This isn't sufficient, as the inclusion could be downstream.
609 //But it'll have to do for now.
610 if (buffer.fileName() == included_file) {
611 Alert::error(_("Recursive input"),
612 bformat(_("Attempted to include file %1$s in itself! "
613 "Ignoring inclusion."), from_utf8(incfile)));
617 // write it to a file (so far the complete file)
618 string const exportfile = changeExtension(incfile, ".sgml");
619 DocFileName writefile(changeExtension(included_file, ".sgml"));
621 if (loadIfNeeded(buffer, params_)) {
622 Buffer * tmp = theBufferList().getBuffer(included_file);
624 string const mangled = writefile.mangledFilename();
625 writefile = makeAbsPath(mangled,
626 buffer.getMasterBuffer()->temppath());
630 LYXERR(Debug::LATEX) << "incfile:" << incfile << endl;
631 LYXERR(Debug::LATEX) << "exportfile:" << exportfile << endl;
632 LYXERR(Debug::LATEX) << "writefile:" << writefile << endl;
634 tmp->makeDocBookFile(writefile, runparams, true);
637 runparams.exportdata->addExternalFile("docbook", writefile,
639 runparams.exportdata->addExternalFile("docbook-xml", writefile,
642 if (isVerbatim(params_) || isListings(params_)) {
643 os << "<inlinegraphic fileref=\""
644 << '&' << include_label << ';'
645 << "\" format=\"linespecific\">";
647 os << '&' << include_label << ';';
653 void InsetInclude::validate(LaTeXFeatures & features) const
655 string incfile(to_utf8(params_["filename"]));
658 Buffer const & buffer = features.buffer();
660 string const included_file = includedFilename(buffer, params_).absFilename();
662 if (isLyXFilename(included_file))
663 writefile = changeExtension(included_file, ".sgml");
665 writefile = included_file;
667 if (!features.runparams().nice && !isVerbatim(params_) && !isListings(params_)) {
668 incfile = DocFileName(writefile).mangledFilename();
669 writefile = makeAbsPath(incfile,
670 buffer.getMasterBuffer()->temppath()).absFilename();
673 features.includeFile(include_label, writefile);
675 if (isVerbatim(params_))
676 features.require("verbatim");
677 else if (isListings(params_))
678 features.require("listings");
680 // Here we must do the fun stuff...
681 // Load the file in the include if it needs
683 if (loadIfNeeded(buffer, params_)) {
685 Buffer * const tmp = theBufferList().getBuffer(included_file);
686 // make sure the buffer isn't us
687 // FIXME RECURSIVE INCLUDES
688 // This is not sufficient, as recursive includes could be
689 // more than a file away. But it will do for now.
690 if (tmp && tmp != & buffer) {
691 // We must temporarily change features.buffer,
692 // otherwise it would always be the master buffer,
693 // and nested includes would not work.
694 features.setBuffer(*tmp);
695 tmp->validate(features);
696 features.setBuffer(buffer);
702 void InsetInclude::getLabelList(Buffer const & buffer,
703 std::vector<docstring> & list) const
705 if (isListings(params_)) {
706 InsetListingsParams params(params_.getOptions());
707 string label = params.getParamValue("label");
709 list.push_back(from_utf8(label));
711 else if (loadIfNeeded(buffer, params_)) {
712 string const included_file = includedFilename(buffer, params_).absFilename();
713 Buffer * tmp = theBufferList().getBuffer(included_file);
714 tmp->setParentName("");
715 tmp->getLabelList(list);
716 tmp->setParentName(parentFilename(buffer));
721 void InsetInclude::fillWithBibKeys(Buffer const & buffer,
722 std::vector<std::pair<string, docstring> > & keys) const
724 if (loadIfNeeded(buffer, params_)) {
725 string const included_file = includedFilename(buffer, params_).absFilename();
726 Buffer * tmp = theBufferList().getBuffer(included_file);
727 tmp->setParentName("");
728 tmp->fillWithBibKeys(keys);
729 tmp->setParentName(parentFilename(buffer));
734 void InsetInclude::updateBibfilesCache(Buffer const & buffer)
736 Buffer * const tmp = getChildBuffer(buffer, params_);
738 tmp->setParentName("");
739 tmp->updateBibfilesCache();
740 tmp->setParentName(parentFilename(buffer));
745 std::vector<FileName> const &
746 InsetInclude::getBibfilesCache(Buffer const & buffer) const
748 Buffer * const tmp = getChildBuffer(buffer, params_);
750 tmp->setParentName("");
751 std::vector<FileName> const & cache = tmp->getBibfilesCache();
752 tmp->setParentName(parentFilename(buffer));
755 static std::vector<FileName> const empty;
760 bool InsetInclude::metrics(MetricsInfo & mi, Dimension & dim) const
762 BOOST_ASSERT(mi.base.bv && mi.base.bv->buffer());
764 bool use_preview = false;
765 if (RenderPreview::status() != LyXRC::PREVIEW_OFF) {
766 graphics::PreviewImage const * pimage =
767 preview_->getPreviewImage(*mi.base.bv->buffer());
768 use_preview = pimage && pimage->image();
772 preview_->metrics(mi, dim);
776 button_.update(getScreenLabel(*mi.base.bv->buffer()),
779 button_.metrics(mi, dim);
782 Box b(0, dim.wid, -dim.asc, dim.des);
785 bool const changed = dim_ != dim;
791 void InsetInclude::draw(PainterInfo & pi, int x, int y) const
793 setPosCache(pi, x, y);
795 BOOST_ASSERT(pi.base.bv && pi.base.bv->buffer());
797 bool use_preview = false;
798 if (RenderPreview::status() != LyXRC::PREVIEW_OFF) {
799 graphics::PreviewImage const * pimage =
800 preview_->getPreviewImage(*pi.base.bv->buffer());
801 use_preview = pimage && pimage->image();
805 preview_->draw(pi, x, y);
807 button_.draw(pi, x, y);
811 Inset::DisplayType InsetInclude::display() const
813 return type(params_) == INPUT ? Inline : AlignCenter;
822 void InsetInclude::fileChanged() const
824 Buffer const * const buffer_ptr = LyX::cref().updateInset(this);
828 Buffer const & buffer = *buffer_ptr;
829 preview_->removePreview(buffer);
830 add_preview(*preview_.get(), *this, buffer);
831 preview_->startLoading(buffer);
837 bool preview_wanted(InsetCommandParams const & params, Buffer const & buffer)
839 FileName const included_file = includedFilename(buffer, params);
841 return type(params) == INPUT && params.preview() &&
842 isFileReadable(included_file);
846 docstring const latex_string(InsetInclude const & inset, Buffer const & buffer)
849 // We don't need to set runparams.encoding since this will be done
850 // by latex() anyway.
851 OutputParams runparams(0);
852 runparams.flavor = OutputParams::LATEX;
853 inset.latex(buffer, os, runparams);
859 void add_preview(RenderMonitoredPreview & renderer, InsetInclude const & inset,
860 Buffer const & buffer)
862 InsetCommandParams const & params = inset.params();
863 if (RenderPreview::status() != LyXRC::PREVIEW_OFF &&
864 preview_wanted(params, buffer)) {
865 renderer.setAbsFile(includedFilename(buffer, params));
866 docstring const snippet = latex_string(inset, buffer);
867 renderer.addPreview(snippet, buffer);
874 void InsetInclude::addPreview(graphics::PreviewLoader & ploader) const
876 Buffer const & buffer = ploader.buffer();
877 if (preview_wanted(params(), buffer)) {
878 preview_->setAbsFile(includedFilename(buffer, params()));
879 docstring const snippet = latex_string(*this, buffer);
880 preview_->addPreview(snippet, ploader);
885 void InsetInclude::addToToc(TocList & toclist, Buffer const & buffer, ParConstIterator & pit) const
887 if (isListings(params_)) {
888 InsetListingsParams params(params_.getOptions());
889 string caption = params.getParamValue("caption");
890 if (!caption.empty()) {
891 Toc & toc = toclist["listing"];
892 docstring const str = convert<docstring>(toc.size() + 1)
893 + ". " + from_utf8(caption);
894 // This inset does not have a valid ParConstIterator
895 // so it has to use the iterator of its parent paragraph
896 toc.push_back(TocItem(pit, 0, str));
900 Buffer const * const childbuffer = getChildBuffer(buffer, params_);
904 TocList const & childtoclist = childbuffer->tocBackend().tocs();
905 TocList::const_iterator it = childtoclist.begin();
906 TocList::const_iterator const end = childtoclist.end();
907 for(; it != end; ++it)
908 toclist[it->first].insert(toclist[it->first].end(),
909 it->second.begin(), it->second.end());
913 void InsetInclude::updateLabels(Buffer const & buffer) const
915 Buffer const * const childbuffer = getChildBuffer(buffer, params_);
919 lyx::updateLabels(*childbuffer, true);
923 void InsetInclude::updateCounter(Counters & counters)
925 if (!isListings(params_))
928 InsetListingsParams const par = params_.getOptions();
929 if (par.getParamValue("caption").empty())
932 counters.step(from_ascii("listing"));
933 counter_ = counters.value(from_ascii("listing"));
938 string const InsetIncludeMailer::name_("include");
940 InsetIncludeMailer::InsetIncludeMailer(InsetInclude & inset)
945 string const InsetIncludeMailer::inset2string(Buffer const &) const
947 return params2string(inset_.params());
951 void InsetIncludeMailer::string2params(string const & in,
952 InsetCommandParams & params)
958 istringstream data(in);
964 if (!lex || name != name_)
965 return print_mailer_error("InsetIncludeMailer", in, 1, name_);
967 // This is part of the inset proper that is usually swallowed
968 // by Text::readInset
971 if (!lex || id != "Include")
972 return print_mailer_error("InsetIncludeMailer", in, 2, "Include");
974 InsetInclude inset(params);
976 params = inset.params();
981 InsetIncludeMailer::params2string(InsetCommandParams const & params)
983 InsetInclude inset(params);
985 data << name_ << ' ';
987 data << "\\end_inset\n";