]> git.lyx.org Git - lyx.git/blobdiff - src/insets/InsetInclude.cpp
Stupid bug fix.
[lyx.git] / src / insets / InsetInclude.cpp
index 0af02f9deb87f1724cf6ef37f428dfaa7085595e..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 "LayoutFile.h"
 #include "Buffer.h"
 #include "buffer_funcs.h"
 #include "BufferList.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 "insets/InsetListingsParams.h"
 #include "insets/RenderPreview.h"
 
-#include "support/assert.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>
 
@@ -138,19 +138,14 @@ 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.setInzipName(to_utf8(params["embed"]));
-       file.enable(buffer.embedded(), buffer, false);
-       return file;
+       return makeAbsPath(to_utf8(params["filename"]),
+                       onlyPath(parentFilename(buffer)));
 }
 
+
 InsetLabel * createLabel(docstring const & label_str)
 {
        if (label_str.empty())
@@ -190,45 +185,11 @@ InsetInclude::InsetInclude(InsetInclude const & other)
 InsetInclude::~InsetInclude()
 {
        delete label_;
-       if (isVerbatim(params()) || isListings(params()))
-               return;
-
-
-       string const parent_filename = buffer().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() != &buffer())
-               return;
-
-       //close the buffer.
-       theBufferList().release(child);
 }
 
 
 void InsetInclude::setBuffer(Buffer & buffer)
 {
-       if (buffer_) {
-               try {
-                       EmbeddedFile file_from = includedFilename(*buffer_, params());
-                       EmbeddedFile file_to = file_from.copyTo(buffer);
-                       if (file_to.embedded())
-                               setParam("embed", from_utf8(file_to.inzipName()));
-               } catch (ExceptionMessage const & message) {
-                       Alert::error(message.title_, message.details_);
-                       // failed to embed
-                       setParam("embed", docstring());
-               }
-       }
        InsetCommand::setBuffer(buffer);
        if (label_)
                label_->setBuffer(buffer);
@@ -244,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_;
 }
@@ -261,9 +221,18 @@ void InsetInclude::doDispatch(Cursor & cur, FuncRequest & cmd)
        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);
-               InsetCommand::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 new_params(to_utf8(p["lstparams"]));
@@ -292,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);
@@ -338,8 +337,6 @@ docstring InsetInclude::screenLabel() const
        else
                temp += from_utf8(onlyFilename(to_utf8(params()["filename"])));
 
-       if (!params()["embed"].empty())
-               temp += _(" (embedded)");
        return temp;
 }
 
@@ -408,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
@@ -660,7 +656,7 @@ void InsetInclude::validate(LaTeXFeatures & features) const
        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");
@@ -708,10 +704,8 @@ void InsetInclude::fillWithBibKeys(BiblioInfo & keys,
        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);
        }
 }
 
@@ -727,17 +721,17 @@ void InsetInclude::updateBibfilesCache()
 }
 
 
-EmbeddedFileList 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;
 }
 
@@ -786,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;
@@ -859,9 +859,8 @@ void InsetInclude::addPreview(graphics::PreviewLoader & ploader) const
 }
 
 
-void InsetInclude::addToToc(ParConstIterator const & cpit) const
+void InsetInclude::addToToc(DocIterator const & cpit)
 {
-       bool const embedded_status = !params()["embed"].empty();
        TocBackend & backend = buffer().tocBackend();
 
        if (isListings(params())) {
@@ -875,33 +874,20 @@ void InsetInclude::addToToc(ParConstIterator const & cpit) const
                Toc & toc = backend.toc("listing");
                docstring str = convert<docstring>(toc.size() + 1)
                        + ". " +  from_utf8(caption);
-               if (embedded_status) {
-                       backend.toc("embedded").push_back(TocItem(cpit, 0, str));
-                       str += _(" (embedded)");
-               }
-               ParConstIterator pit = cpit;
-               pit.push_back(*this);
+               DocIterator pit = cpit;
                toc.push_back(TocItem(pit, 0, str));
                return;
        }
        Buffer const * const childbuffer = getChildBuffer(buffer(), params());
-       if (!childbuffer) {
-               if (embedded_status) 
-                       // Add it to the embedded list nonetheless.
-                       backend.toc("embedded").push_back(TocItem(cpit, 0,
-                               params()["filename"]));
+       if (!childbuffer)
                return;
-       }
 
        Toc & toc = backend.toc("child");
        docstring str = childbuffer->fileName().displayName();
-       if (embedded_status) {
-               backend.toc("embedded").push_back(TocItem(cpit, 0, str));
-               str += _(" (embedded)");
-       }
        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();
@@ -926,12 +912,13 @@ void InsetInclude::updateLabels(ParIterator const & 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));
@@ -939,17 +926,4 @@ void InsetInclude::updateLabels(ParIterator const & it)
 }
 
 
-void InsetInclude::registerEmbeddedFiles(EmbeddedFileList & files) const
-{
-       files.registerFile(includedFilename(buffer(), params()), this, buffer());
-}
-
-
-void InsetInclude::updateEmbeddedFile(EmbeddedFile const & file)
-{
-       setParam("filename", from_utf8(file.outputFilename(buffer().filePath())));
-       setParam("embed", file.embedded() ? from_utf8(file.inzipName()) : docstring());
-}
-
-
 } // namespace lyx