]> git.lyx.org Git - lyx.git/blobdiff - src/insets/InsetListings.cpp
Fix GRAPHICS_EDIT of InsetGraphics
[lyx.git] / src / insets / InsetListings.cpp
index 14997900881971b9e67c0b2bc00922b44e999392..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,6 +39,7 @@ using namespace lyx::support;
 
 namespace lyx {
 
+using boost::regex;
 
 char const lstinline_delimiters[] =
        "!*()-=+|;:'\"`,<.>/?QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm";
@@ -276,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));
                        }
                }
        }