]> git.lyx.org Git - lyx.git/blobdiff - src/insets/InsetListings.cpp
Revert http://www.lyx.org/trac/changeset/25553 and try better fix for bug
[lyx.git] / src / insets / InsetListings.cpp
index 0b63ec28e6783c45f3bd06a9da710068096c7d42..a2cb9851a40148092aa1b67baa144682e04a0c6a 100644 (file)
 #include <config.h>
 
 #include "InsetListings.h"
-#include "InsetCaption.h"
 
 #include "Buffer.h"
+#include "BufferView.h"
 #include "BufferParams.h"
 #include "Counters.h"
 #include "Cursor.h"
 #include "DispatchResult.h"
 #include "FuncRequest.h"
 #include "FuncStatus.h"
-#include "support/gettext.h"
+#include "InsetCaption.h"
 #include "InsetList.h"
 #include "Language.h"
 #include "MetricsInfo.h"
 #include "TextClass.h"
 
+#include "support/debug.h"
 #include "support/docstream.h"
+#include "support/gettext.h"
 #include "support/lstrings.h"
 
+#include "frontends/Application.h"
+
+#include <boost/regex.hpp>
+
 #include <sstream>
 
 using namespace std;
@@ -37,6 +43,7 @@ using namespace lyx::support;
 
 namespace lyx {
 
+using boost::regex;
 
 char const lstinline_delimiters[] =
        "!*()-=+|;:'\"`,<.>/?QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm";
@@ -46,20 +53,9 @@ InsetListings::InsetListings(Buffer const & buf, InsetListingsParams const & par
 {}
 
 
-InsetListings::InsetListings(InsetListings const & in)
-       : InsetCollapsable(in), params_(in.params_)
-{}
-
-
-Inset * InsetListings::clone() const
-{
-       return new InsetListings(*this);
-}
-
-
 InsetListings::~InsetListings()
 {
-       InsetListingsMailer(*this).hideDialog();
+       hideDialogs("listings", this);
 }
 
 
@@ -104,7 +100,7 @@ void InsetListings::read(Lexer & lex)
 {
        while (lex.isOK()) {
                lex.next();
-               string const token = lex.getString();
+               string token = lex.getString();
                if (token == "lstparams") {
                        lex.next();
                        string const value = lex.getString();
@@ -161,7 +157,7 @@ int InsetListings::latex(odocstream & os, OutputParams const & runparams) const
                }
        }
        if (isInline) {
-                char const * delimiter = lstinline_delimiters;
+               char const * delimiter = lstinline_delimiters;
                for (; delimiter != '\0'; ++delimiter)
                        if (!contains(code, *delimiter))
                                break;
@@ -205,25 +201,23 @@ int InsetListings::latex(odocstream & os, OutputParams const & runparams) const
 }
 
 
+docstring InsetListings::contextMenu(BufferView const &, int, int) const
+{
+       return from_ascii("context-listings");
+}
+
+
 void InsetListings::doDispatch(Cursor & cur, FuncRequest & cmd)
 {
        switch (cmd.action) {
 
        case LFUN_INSET_MODIFY: {
-               InsetListingsMailer::string2params(to_utf8(cmd.argument()), params());
+               InsetListings::string2params(to_utf8(cmd.argument()), params());
                break;
        }
        case LFUN_INSET_DIALOG_UPDATE:
-               InsetListingsMailer(*this).updateDialog(&cur.bv());
-               break;
-       case LFUN_MOUSE_RELEASE: {
-               if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
-                       InsetListingsMailer(*this).showDialog(&cur.bv());
-                       break;
-               }
-               InsetCollapsable::doDispatch(cur, cmd);
+               cur.bv().updateDialog("listings", params2string(params()));
                break;
-       }
        default:
                InsetCollapsable::doDispatch(cur, cmd);
                break;
@@ -235,11 +229,12 @@ bool InsetListings::getStatus(Cursor & cur, FuncRequest const & cmd,
        FuncStatus & status) const
 {
        switch (cmd.action) {
+               case LFUN_INSET_MODIFY:
                case LFUN_INSET_DIALOG_UPDATE:
-                       status.enabled(true);
+                       status.setEnabled(true);
                        return true;
                case LFUN_CAPTION_INSERT:
-                       status.enabled(!params().isInline());
+                       status.setEnabled(!params().isInline());
                        return true;
                default:
                        return InsetCollapsable::getStatus(cur, cmd, status);
@@ -266,7 +261,8 @@ void InsetListings::validate(LaTeXFeatures & features) const
 
 bool InsetListings::showInsetDialog(BufferView * bv) const
 {
-       InsetListingsMailer(const_cast<InsetListings &>(*this)).showDialog(bv);
+       bv->showDialog("listings", params2string(params()),
+               const_cast<InsetListings *>(this));
        return true;
 }
 
@@ -287,7 +283,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));
                        }
                }
        }
@@ -295,27 +306,14 @@ docstring InsetListings::getCaption(OutputParams const & runparams) const
 }
 
 
-string const InsetListingsMailer::name_("listings");
-
-InsetListingsMailer::InsetListingsMailer(InsetListings & inset)
-       : inset_(inset)
-{}
-
-
-string const InsetListingsMailer::inset2string(Buffer const &) const
-{
-       return params2string(inset_.params());
-}
-
-
-void InsetListingsMailer::string2params(string const & in,
+void InsetListings::string2params(string const & in,
                                   InsetListingsParams & params)
 {
        params = InsetListingsParams();
        if (in.empty())
                return;
        istringstream data(in);
-       Lexer lex(0, 0);
+       Lexer lex;
        lex.setStream(data);
        // discard "listings", which is only used to determine inset
        lex.next();
@@ -323,11 +321,10 @@ void InsetListingsMailer::string2params(string const & in,
 }
 
 
-string const
-InsetListingsMailer::params2string(InsetListingsParams const & params)
+string InsetListings::params2string(InsetListingsParams const & params)
 {
        ostringstream data;
-       data << name_ << " ";
+       data << "listings" << ' ';
        params.write(data);
        return data.str();
 }