]> git.lyx.org Git - lyx.git/blobdiff - src/insets/InsetListings.cpp
Change string
[lyx.git] / src / insets / InsetListings.cpp
index 5bf4e8cbd73960ba1539334322e49cb7b9cc674e..2768b831da270634d08b25c3d79c843fe9f32f74 100644 (file)
@@ -30,6 +30,8 @@
 #include "support/docstream.h"
 #include "support/lstrings.h"
 
+#include <boost/regex.hpp>
+
 #include <sstream>
 
 using namespace std;
@@ -37,26 +39,16 @@ using namespace lyx::support;
 
 namespace lyx {
 
+using boost::regex;
 
 char const lstinline_delimiters[] =
        "!*()-=+|;:'\"`,<.>/?QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm";
 
-InsetListings::InsetListings(BufferParams const & bp, InsetListingsParams const & par)
-       : InsetCollapsable(bp, par.status())
-{}
-
-
-InsetListings::InsetListings(InsetListings const & in)
-       : InsetCollapsable(in), params_(in.params_)
+InsetListings::InsetListings(Buffer const & buf, InsetListingsParams const & par)
+       : InsetCollapsable(buf, par.status())
 {}
 
 
-Inset * InsetListings::clone() const
-{
-       return new InsetListings(*this);
-}
-
-
 InsetListings::~InsetListings()
 {
        InsetListingsMailer(*this).hideDialog();
@@ -71,7 +63,7 @@ Inset::DisplayType InsetListings::display() const
 
 void InsetListings::updateLabels(ParIterator const & it)
 {
-       Counters & cnts = buffer().params().textClass().counters();
+       Counters & cnts = buffer().params().documentClass().counters();
        string const saveflt = cnts.current_float();
 
        // Tell to captions what the current float is
@@ -287,7 +279,22 @@ docstring InsetListings::getCaption(OutputParams const & runparams) const
                                        static_cast<InsetCaption *>(it->inset);
                                ins->getOptArg(ods, runparams);
                                ins->getArgument(ods, runparams);
-                               return ods.str();
+                               // the caption may contain \label{} but the listings
+                               // package prefer caption={}, label={}
+                               docstring cap = ods.str();
+                               if (!contains(to_utf8(cap), "\\label{"))
+                                       return cap;
+                               // convert from
+                               //     blah1\label{blah2} blah3
+                               // to
+                               //     blah1 blah3},label={blah2
+                               // to form options
+                               //     caption={blah1 blah3},label={blah2}
+                               //
+                               // NOTE that } is not allowed in blah2.
+                               regex const reg("(.*)\\\\label\\{(.*?)\\}(.*)");
+                               string const new_cap("\\1\\3},label={\\2");
+                               return from_utf8(regex_replace(to_utf8(cap), reg, new_cap));
                        }
                }
        }