]> git.lyx.org Git - lyx.git/blobdiff - src/insets/InsetInclude.cpp
Allow listing the lyx file itself, fix bug 3707
[lyx.git] / src / insets / InsetInclude.cpp
index 27254975dd587192271390c9ebc9b9fa77622dc1..eca2ff60cd9eafb193c5f1f19101e859b1db2370 100644 (file)
@@ -62,6 +62,7 @@ using support::copy;
 using support::DocFileName;
 using support::FileName;
 using support::getFileContents;
+using support::getVectorFromString;
 using support::isFileReadable;
 using support::isLyXFilename;
 using support::latex_path;
@@ -70,6 +71,7 @@ using support::makeDisplayPath;
 using support::makeRelPath;
 using support::onlyFilename;
 using support::onlyPath;
+using support::prefixIs;
 using support::subst;
 using support::sum;
 
@@ -79,6 +81,7 @@ using std::auto_ptr;
 using std::istringstream;
 using std::ostream;
 using std::ostringstream;
+using std::vector;
 
 namespace Alert = frontend::Alert;
 namespace fs = boost::filesystem;
@@ -92,6 +95,12 @@ docstring const uniqueID()
        return "file" + convert<docstring>(++seed);
 }
 
+
+bool isListings(InsetCommandParams const & params)
+{
+       return params.getCmdName() == "lstinputlisting";
+}
+
 } // namespace anon
 
 
@@ -129,6 +138,17 @@ void InsetInclude::doDispatch(Cursor & cur, FuncRequest & cmd)
                InsetCommandParams p("include");
                InsetIncludeMailer::string2params(to_utf8(cmd.argument()), p);
                if (!p.getCmdName().empty()) {
+                       if (isListings(p)){
+                               InsetListingsParams par_old(params().getOptions());
+                               InsetListingsParams par_new(p.getOptions());
+                               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")),
+                                               Inset::REF_CODE);
+                       }
                        set(p, cur.buffer());
                        cur.buffer().updateBibfilesCache();
                } else
@@ -141,7 +161,8 @@ void InsetInclude::doDispatch(Cursor & cur, FuncRequest & cmd)
                break;
 
        case LFUN_MOUSE_RELEASE:
-               InsetIncludeMailer(*this).showDialog(&cur.bv());
+               if (!cur.selection()) 
+                       InsetIncludeMailer(*this).showDialog(&cur.bv());
                break;
 
        default:
@@ -209,12 +230,6 @@ bool isVerbatim(InsetCommandParams const & params)
 }
 
 
-bool isListings(InsetCommandParams const & params)
-{
-       return params.getCmdName() == "lstinputlisting";
-}
-
-
 string const masterFilename(Buffer const & buffer)
 {
        return buffer.getMasterBuffer()->fileName();
@@ -349,7 +364,12 @@ Buffer * getChildBuffer(Buffer const & buffer, InsetCommandParams const & params
        if (!isLyXFilename(included_file))
                return 0;
 
-       return theBufferList().getBuffer(included_file);
+       Buffer * childBuffer = theBufferList().getBuffer(included_file);
+       
+       //FIXME RECURSIVE INCLUDES
+       if (childBuffer == & buffer)
+               return 0;
+       else return childBuffer;
 }
 
 
@@ -391,6 +411,18 @@ int InsetInclude::latex(Buffer const & buffer, odocstream & os,
                return 0;
 
        FileName const included_file(includedFilename(buffer, params_));
+       
+       //Check we're not trying to include ourselves.
+       //FIXME RECURSIVE INCLUDE
+       //This isn't sufficient, as the inclusion could be downstream.
+       //But it'll have to do for now.
+       if (!isListings(params_) && buffer.fileName() == included_file.toFilesystemEncoding()) {
+               Alert::error(_("Recursive input"), 
+                              bformat(_("Attempted to include file %1$s in itself! "
+                              "Ignoring inclusion."), from_utf8(incfile)));
+               return 0;
+       }
+
        Buffer const * const m_buffer = buffer.getMasterBuffer();
 
        // if incfile is relative, make it relative to the master
@@ -546,6 +578,17 @@ int InsetInclude::docbook(Buffer const & buffer, odocstream & os,
 
        string const included_file = includedFilename(buffer, params_).absFilename();
 
+       //Check we're not trying to include ourselves.
+       //FIXME RECURSIVE INCLUDE
+       //This isn't sufficient, as the inclusion could be downstream.
+       //But it'll have to do for now.
+       if (buffer.fileName() == included_file) {
+               Alert::error(_("Recursive input"), 
+                              bformat(_("Attempted to include file %1$s in itself! "
+                              "Ignoring inclusion."), from_utf8(incfile)));
+               return 0;
+       }
+
        // write it to a file (so far the complete file)
        string const exportfile = changeExtension(incfile, ".sgml");
        DocFileName writefile(changeExtension(included_file, ".sgml"));
@@ -615,7 +658,11 @@ void InsetInclude::validate(LaTeXFeatures & features) const
        if (loadIfNeeded(buffer, params_)) {
                // a file got loaded
                Buffer * const tmp = theBufferList().getBuffer(included_file);
-               if (tmp) {
+               // make sure the buffer isn't us
+               // FIXME RECURSIVE INCLUDES
+               // This is not sufficient, as recursive includes could be
+               // more than a file away. But it will do for now.
+               if (tmp && tmp != & buffer) {
                        // We must temporarily change features.buffer,
                        // otherwise it would always be the master buffer,
                        // and nested includes would not work.
@@ -630,7 +677,13 @@ void InsetInclude::validate(LaTeXFeatures & features) const
 void InsetInclude::getLabelList(Buffer const & buffer,
                                std::vector<docstring> & list) const
 {
-       if (loadIfNeeded(buffer, params_)) {
+       if (isListings(params_)) {
+               InsetListingsParams params(params_.getOptions());
+               string label = params.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->setParentName("");
@@ -729,9 +782,10 @@ void InsetInclude::draw(PainterInfo & pi, int x, int y) const
                button_.draw(pi, x, y);
 }
 
-bool InsetInclude::display() const
+
+Inset::DisplayType InsetInclude::display() const
 {
-       return type(params_) != INPUT;
+       return type(params_) == INPUT ? Inline : AlignCenter;
 }