]> git.lyx.org Git - lyx.git/blobdiff - src/insets/InsetInclude.cpp
Stupid bug fix.
[lyx.git] / src / insets / InsetInclude.cpp
index 5d9cd9426b2619093973cf42e88bc719b244c341..38e61ae59e63a3c8f5da3342e163c5595caec14c 100644 (file)
@@ -5,7 +5,6 @@
  *
  * \author Lars Gullik Bjønnes
  * \author Richard Heck (conversion to InsetCommand)
- * \author Bo Peng (embedding stuff)
  *
  * Full author contact details are available in file CREDITS.
  */
@@ -14,7 +13,6 @@
 
 #include "InsetInclude.h"
 
-#include "BaseClassList.h"
 #include "Buffer.h"
 #include "buffer_funcs.h"
 #include "BufferList.h"
 #include "BufferView.h"
 #include "Cursor.h"
 #include "DispatchResult.h"
+#include "ErrorList.h"
 #include "Exporter.h"
+#include "Format.h"
 #include "FuncRequest.h"
 #include "FuncStatus.h"
 #include "LaTeXFeatures.h"
+#include "LayoutFile.h"
 #include "LyX.h"
+#include "LyXFunc.h"
 #include "LyXRC.h"
 #include "Lexer.h"
 #include "MetricsInfo.h"
 #include "graphics/PreviewImage.h"
 #include "graphics/PreviewLoader.h"
 
-#include "insets/RenderPreview.h"
+#include "insets/InsetLabel.h"
 #include "insets/InsetListingsParams.h"
+#include "insets/RenderPreview.h"
 
+#include "support/convert.h"
 #include "support/debug.h"
 #include "support/docstream.h"
-#include "support/ExceptionMessage.h"
 #include "support/FileNameList.h"
 #include "support/filetools.h"
 #include "support/gettext.h"
+#include "support/lassert.h"
 #include "support/lstrings.h" // contains
 #include "support/lyxalgo.h"
-#include "support/convert.h"
 
 #include <boost/bind.hpp>
 
@@ -135,16 +138,21 @@ string const parentFilename(Buffer const & buffer)
 }
 
 
-EmbeddedFile const includedFilename(Buffer const & buffer,
+FileName const includedFilename(Buffer const & buffer,
                              InsetCommandParams const & params)
 {
-       // it is not a good idea to create this EmbeddedFile object
-       // each time, but there seems to be no easy way around.
-       EmbeddedFile file(to_utf8(params["filename"]),
-              onlyPath(parentFilename(buffer)));
-       file.setEmbed(!params["embed"].empty());
-       file.enable(buffer.embedded(), &buffer);
-       return file;
+       return makeAbsPath(to_utf8(params["filename"]),
+                       onlyPath(parentFilename(buffer)));
+}
+
+
+InsetLabel * createLabel(docstring const & label_str)
+{
+       if (label_str.empty())
+               return 0;
+       InsetCommandParams icp(LABEL_CODE);
+       icp["name"] = label_str;
+       return new InsetLabel(icp);
 }
 
 } // namespace anon
@@ -152,17 +160,39 @@ EmbeddedFile const includedFilename(Buffer const & buffer,
 
 InsetInclude::InsetInclude(InsetCommandParams const & p)
        : InsetCommand(p, "include"), include_label(uniqueID()),
-         preview_(new RenderMonitoredPreview(this)), set_label_(false)
+         preview_(new RenderMonitoredPreview(this)), set_label_(false), label_(0)
 {
        preview_->fileChanged(boost::bind(&InsetInclude::fileChanged, this));
+
+       if (isListings(params())) {
+               InsetListingsParams listing_params(to_utf8(p["lstparams"]));
+               label_ = createLabel(from_utf8(listing_params.getParamValue("label")));
+       }
 }
 
 
 InsetInclude::InsetInclude(InsetInclude const & other)
        : InsetCommand(other), include_label(other.include_label),
-         preview_(new RenderMonitoredPreview(this)), set_label_(false)
+         preview_(new RenderMonitoredPreview(this)), set_label_(false), label_(0)
 {
        preview_->fileChanged(boost::bind(&InsetInclude::fileChanged, this));
+
+       if (other.label_)
+               label_ = new InsetLabel(*other.label_);
+}
+
+
+InsetInclude::~InsetInclude()
+{
+       delete label_;
+}
+
+
+void InsetInclude::setBuffer(Buffer & buffer)
+{
+       InsetCommand::setBuffer(buffer);
+       if (label_)
+               label_->setBuffer(buffer);
 }
 
 
@@ -175,7 +205,6 @@ ParamInfo const & InsetInclude::findInfo(string const & /* cmdName */)
        if (param_info_.empty()) {
                param_info_.add("filename", ParamInfo::LATEX_REQUIRED);
                param_info_.add("lstparams", ParamInfo::LATEX_OPTIONAL);
-               param_info_.add("embed", ParamInfo::LYX_INTERNAL);
        }
        return param_info_;
 }
@@ -189,41 +218,36 @@ bool InsetInclude::isCompatibleCommand(string const & s)
 
 void InsetInclude::doDispatch(Cursor & cur, FuncRequest & cmd)
 {
-       BOOST_ASSERT(&cur.buffer() == &buffer());
+       LASSERT(&cur.buffer() == &buffer(), /**/);
        switch (cmd.action) {
 
+       case LFUN_INSET_EDIT: {
+               editIncluded(to_utf8(params()["filename"]));
+               break;
+       }
+
        case LFUN_INSET_MODIFY: {
                InsetCommandParams p(INCLUDE_CODE);
-               InsetCommandMailer::string2params("include", to_utf8(cmd.argument()), p);
+               if (cmd.getArg(0) == "changetype") {
+                       InsetCommand::doDispatch(cur, cmd);
+                       p = params();
+               } else
+                       InsetCommand::string2params("include", to_utf8(cmd.argument()), p);
                if (!p.getCmdName().empty()) {
                        if (isListings(p)){
-                               InsetListingsParams par_old(to_utf8(params()["lstparams"]));
-                               InsetListingsParams par_new(to_utf8(p["lstparams"]));
-                               if (par_old.getParamValue("label") !=
-                                   par_new.getParamValue("label")
-                                   && !par_new.getParamValue("label").empty())
-                                       cur.bv().buffer().changeRefsIfUnique(
-                                               from_utf8(par_old.getParamValue("label")),
-                                               from_utf8(par_new.getParamValue("label")),
-                                               REF_CODE);
-                       }
-                       try {
-                               // the embed parameter passed back from the dialog
-                               // is "true" or "false", we need to change it.
-                               if (p["embed"] == _("false"))
-                                       p["embed"].clear();
-                               else
-                                       p["embed"] = from_utf8(EmbeddedFile(to_utf8(p["filename"]),
-                                               onlyPath(parentFilename(cur.buffer()))).inzipName());
-                               // test parameter
-                               includedFilename(cur.buffer(), p);
-                       } catch (ExceptionMessage const & message) {
-                               Alert::error(message.title_, message.details_);
-                               // do not set parameter if an error happens
-                               break;
+                               InsetListingsParams new_params(to_utf8(p["lstparams"]));
+                               docstring const label_str = from_utf8(new_params.getParamValue("label"));
+                               if (label_str.empty())
+                                       delete label_;
+                               else if (label_)
+                                       label_->updateCommand(label_str);
+                               else {
+                                       label_ = createLabel(label_str);
+                                       label_->setBuffer(buffer());
+                                       label_->initView();
+                               }
                        }
                        setParams(p);
-                       buffer().updateBibfilesCache();
                } else
                        cur.noUpdate();
                break;
@@ -237,6 +261,36 @@ void InsetInclude::doDispatch(Cursor & cur, FuncRequest & cmd)
 }
 
 
+void InsetInclude::editIncluded(string const & file)
+{
+       string const ext = support::getExtension(file);
+       if (ext == "lyx") {
+               FuncRequest fr(LFUN_BUFFER_CHILD_OPEN, file);
+               lyx::dispatch(fr);
+       } else
+               // tex file or other text file in verbatim mode
+               formats.edit(buffer(),
+                       support::makeAbsPath(file, support::onlyPath(buffer().absFileName())),
+                       "text");
+}
+
+
+bool InsetInclude::getStatus(Cursor & cur, FuncRequest const & cmd,
+               FuncStatus & flag) const
+{
+       switch (cmd.action) {
+
+       case LFUN_INSET_EDIT:
+       case LFUN_INSET_MODIFY:
+               flag.setEnabled(true);
+               return true;
+
+       default:
+               return InsetCommand::getStatus(cur, cmd, flag);
+       }
+}
+
+
 void InsetInclude::setParams(InsetCommandParams const & p)
 {
        InsetCommand::setParams(p);
@@ -247,12 +301,8 @@ void InsetInclude::setParams(InsetCommandParams const & p)
 
        if (type(params()) == INPUT)
                add_preview(*preview_, *this, buffer());
-}
 
-
-Inset * InsetInclude::clone() const
-{
-       return new InsetInclude(*this);
+       buffer().updateBibfilesCache();
 }
 
 
@@ -277,7 +327,7 @@ docstring InsetInclude::screenLabel() const
                        temp = listings_label_;
                        break;
                case NONE:
-                       BOOST_ASSERT(false);
+                       LASSERT(false, /**/);
        }
 
        temp += ": ";
@@ -287,8 +337,6 @@ docstring InsetInclude::screenLabel() const
        else
                temp += from_utf8(onlyFilename(to_utf8(params()["filename"])));
 
-       if (!params()["embed"].empty())
-               temp += _(" (embedded)");
        return temp;
 }
 
@@ -303,7 +351,7 @@ Buffer * getChildBuffer(Buffer const & buffer, InsetCommandParams const & params
        if (!isLyXFilename(included_file))
                return 0;
 
-       Buffer * childBuffer = theBufferList().getBuffer(included_file);
+       Buffer * childBuffer = loadIfNeeded(buffer, params); 
 
        // FIXME: recursive includes
        return (childBuffer == &buffer) ? 0 : childBuffer;
@@ -339,43 +387,16 @@ Buffer * loadIfNeeded(Buffer const & parent, InsetCommandParams const & params)
                        theBufferList().release(child);
                        return 0;
                }
+       
+               if (!child->errorList("Parse").empty()) {
+                       // FIXME: Do something.
+               }
        }
        child->setParent(&parent);
        return child;
 }
 
 
-void resetParentBuffer(Buffer const * parent, InsetCommandParams const & params,
-       bool close_it)
-{
-       if (isVerbatim(params) || isListings(params))
-               return;
-
-       string const parent_filename = parent->absFileName();
-       FileName const included_file = makeAbsPath(to_utf8(params["filename"]),
-                          onlyPath(parent_filename));
-
-       if (!isLyXFilename(included_file.absFilename()))
-               return;
-
-       Buffer * child = theBufferList().getBuffer(included_file.absFilename());
-       // File not opened, nothing to close.
-       if (!child)
-               return;
-
-       // Child document has a different parent, don't close it.
-       if (child->parent() != parent)
-               return;
-
-       //close the buffer.
-       child->setParent(0);
-       if (close_it)
-               theBufferList().release(child);
-       else
-               updateLabels(*child);
-}
-
-
 int InsetInclude::latex(odocstream & os, OutputParams const & runparams) const
 {
        string incfile = to_utf8(params()["filename"]);
@@ -384,8 +405,7 @@ int InsetInclude::latex(odocstream & os, OutputParams const & runparams) const
        if (incfile.empty())
                return 0;
 
-       FileName const included_file =
-               includedFilename(buffer(), params()).availableFile();
+       FileName const included_file = includedFilename(buffer(), params());
 
        // Check we're not trying to include ourselves.
        // FIXME RECURSIVE INCLUDE
@@ -444,13 +464,12 @@ int InsetInclude::latex(odocstream & os, OutputParams const & runparams) const
                if (tmp->params().baseClass() != masterBuffer->params().baseClass()) {
                        // FIXME UNICODE
                        docstring text = bformat(_("Included file `%1$s'\n"
-                                               "has textclass `%2$s'\n"
-                                                            "while parent file has textclass `%3$s'."),
-                                             included_file.displayName(),
-                                             from_utf8(tmp->params().documentClass().name()),
-                                             from_utf8(masterBuffer->params().documentClass().name()));
+                               "has textclass `%2$s'\n"
+                               "while parent file has textclass `%3$s'."),
+                               included_file.displayName(),
+                               from_utf8(tmp->params().documentClass().name()),
+                               from_utf8(masterBuffer->params().documentClass().name()));
                        Alert::warning(_("Different textclasses"), text);
-                       //return 0;
                }
 
                // Make sure modules used in child are all included in master
@@ -464,11 +483,11 @@ int InsetInclude::latex(odocstream & os, OutputParams const & runparams) const
                        string const module = *it;
                        vector<string>::const_iterator found =
                                find(masterModules.begin(), masterModules.end(), module);
-                       if (found != masterModules.end()) {
+                       if (found == masterModules.end()) {
                                docstring text = bformat(_("Included file `%1$s'\n"
-                                                       "uses module `%2$s'\n"
-                                                       "which is not used in parent file."),
-                                      included_file.displayName(), from_utf8(module));
+                                       "uses module `%2$s'\n"
+                                       "which is not used in parent file."),
+                                       included_file.displayName(), from_utf8(module));
                                Alert::warning(_("Module not found"), text);
                        }
                }
@@ -634,10 +653,10 @@ void InsetInclude::validate(LaTeXFeatures & features) const
        string incfile = to_utf8(params()["filename"]);
        string writefile;
 
-       BOOST_ASSERT(&buffer() == &features.buffer());
+       LASSERT(&buffer() == &features.buffer(), /**/);
 
        string const included_file =
-               includedFilename(buffer(), params()).availableFile().absFilename();
+               includedFilename(buffer(), params()).absFilename();
 
        if (isLyXFilename(included_file))
                writefile = changeExtension(included_file, ".sgml");
@@ -679,34 +698,14 @@ void InsetInclude::validate(LaTeXFeatures & features) const
 }
 
 
-void InsetInclude::getLabelList(vector<docstring> & list) const
-{
-       if (isListings(params())) {
-               InsetListingsParams p(to_utf8(params()["lstparams"]));
-               string label = p.getParamValue("label");
-               if (!label.empty())
-                       list.push_back(from_utf8(label));
-       }
-       else if (loadIfNeeded(buffer(), params())) {
-               string const included_file = includedFilename(buffer(), params()).absFilename();
-               Buffer * tmp = theBufferList().getBuffer(included_file);
-               tmp->setParent(0);
-               tmp->getLabelList(list);
-               tmp->setParent(const_cast<Buffer *>(&buffer()));
-       }
-}
-
-
 void InsetInclude::fillWithBibKeys(BiblioInfo & keys,
        InsetIterator const & /*di*/) const
 {
        if (loadIfNeeded(buffer(), params())) {
                string const included_file = includedFilename(buffer(), params()).absFilename();
                Buffer * tmp = theBufferList().getBuffer(included_file);
-               //FIXME This is kind of a dirty hack and should be made reasonable.
-               tmp->setParent(0);
-               keys.fillWithBibKeys(tmp);
-               tmp->setParent(&buffer());
+               BiblioInfo const & newkeys = tmp->localBibInfo();
+               keys.mergeBiblioInfo(newkeys);
        }
 }
 
@@ -722,24 +721,24 @@ void InsetInclude::updateBibfilesCache()
 }
 
 
-EmbeddedFileList const &
-InsetInclude::getBibfilesCache(Buffer const & buffer) const
+support::FileNameList const &
+       InsetInclude::getBibfilesCache(Buffer const & buffer) const
 {
        Buffer * const tmp = getChildBuffer(buffer, params());
        if (tmp) {
                tmp->setParent(0);
-               EmbeddedFileList const & cache = tmp->getBibfilesCache();
+               support::FileNameList const & cache = tmp->getBibfilesCache();
                tmp->setParent(&buffer);
                return cache;
        }
-       static EmbeddedFileList const empty;
+       static support::FileNameList const empty;
        return empty;
 }
 
 
 void InsetInclude::metrics(MetricsInfo & mi, Dimension & dim) const
 {
-       BOOST_ASSERT(mi.base.bv);
+       LASSERT(mi.base.bv, /**/);
 
        bool use_preview = false;
        if (RenderPreview::status() != LyXRC::PREVIEW_OFF) {
@@ -765,7 +764,7 @@ void InsetInclude::metrics(MetricsInfo & mi, Dimension & dim) const
 
 void InsetInclude::draw(PainterInfo & pi, int x, int y) const
 {
-       BOOST_ASSERT(pi.base.bv);
+       LASSERT(pi.base.bv, /**/);
 
        bool use_preview = false;
        if (RenderPreview::status() != LyXRC::PREVIEW_OFF) {
@@ -781,6 +780,12 @@ void InsetInclude::draw(PainterInfo & pi, int x, int y) const
 }
 
 
+docstring InsetInclude::contextMenu(BufferView const &, int, int) const
+{
+       return from_ascii("context-include");
+}
+
+
 Inset::DisplayType InsetInclude::display() const
 {
        return type(params()) == INPUT ? Inline : AlignCenter;
@@ -854,18 +859,22 @@ void InsetInclude::addPreview(graphics::PreviewLoader & ploader) const
 }
 
 
-void InsetInclude::addToToc(ParConstIterator const & cpit) const
+void InsetInclude::addToToc(DocIterator const & cpit)
 {
+       TocBackend & backend = buffer().tocBackend();
+
        if (isListings(params())) {
+               if (label_)
+                       label_->addToToc(cpit);
+
                InsetListingsParams p(to_utf8(params()["lstparams"]));
                string caption = p.getParamValue("caption");
                if (caption.empty())
                        return;
-               Toc & toc = buffer().tocBackend().toc("listing");
-               docstring const str = convert<docstring>(toc.size() + 1)
+               Toc & toc = backend.toc("listing");
+               docstring str = convert<docstring>(toc.size() + 1)
                        + ". " +  from_utf8(caption);
-               ParConstIterator pit = cpit;
-               pit.push_back(*this);
+               DocIterator pit = cpit;
                toc.push_back(TocItem(pit, 0, str));
                return;
        }
@@ -873,17 +882,22 @@ void InsetInclude::addToToc(ParConstIterator const & cpit) const
        if (!childbuffer)
                return;
 
-       TocList & toclist = buffer().tocBackend().tocs();
+       Toc & toc = backend.toc("child");
+       docstring str = childbuffer->fileName().displayName();
+       toc.push_back(TocItem(cpit, 0, str));
+
+       TocList & toclist = backend.tocs();
+       childbuffer->tocBackend().update();
        TocList const & childtoclist = childbuffer->tocBackend().tocs();
        TocList::const_iterator it = childtoclist.begin();
        TocList::const_iterator const end = childtoclist.end();
        for(; it != end; ++it)
                toclist[it->first].insert(toclist[it->first].end(),
-                               it->second.begin(), it->second.end());
+                       it->second.begin(), it->second.end());
 }
 
 
-void InsetInclude::updateLabels(ParIterator const &)
+void InsetInclude::updateLabels(ParIterator const & it)
 {
        Buffer const * const childbuffer = getChildBuffer(buffer(), params());
        if (childbuffer) {
@@ -893,14 +907,18 @@ void InsetInclude::updateLabels(ParIterator const &)
        if (!isListings(params()))
                return;
 
+       if (label_)
+               label_->updateLabels(it);
+
        InsetListingsParams const par(to_utf8(params()["lstparams"]));
        if (par.getParamValue("caption").empty()) {
-               listings_label_.clear();
+               listings_label_ = buffer().B_("Program Listing");
                return;
        }
-       Counters & counters = buffer().params().documentClass().counters();
+       Buffer const & master = *buffer().masterBuffer();
+       Counters & counters = master.params().documentClass().counters();
        docstring const cnt = from_ascii("listing");
-       listings_label_ = buffer().B_("Program Listing");
+       listings_label_ = master.B_("Program Listing");
        if (counters.hasCounter(cnt)) {
                counters.step(cnt);
                listings_label_ += " " + convert<docstring>(counters.value(cnt));
@@ -908,19 +926,4 @@ void InsetInclude::updateLabels(ParIterator const &)
 }
 
 
-void InsetInclude::registerEmbeddedFiles(EmbeddedFileList & files) const
-{
-       files.registerFile(includedFilename(buffer(), params()), this, buffer());
-}
-
-
-void InsetInclude::updateEmbeddedFile(EmbeddedFile const & file)
-{
-       InsetCommandParams p = params();
-       p["filename"] = from_utf8(file.outputFilename());
-       p["embed"] = file.embedded() ? from_utf8(file.inzipName()) : docstring();
-       setParams(p);
-}
-
-
 } // namespace lyx