]> git.lyx.org Git - lyx.git/blobdiff - src/insets/InsetListings.cpp
fix http://bugzilla.lyx.org/show_bug.cgi?id=4998
[lyx.git] / src / insets / InsetListings.cpp
index 5bf4e8cbd73960ba1539334322e49cb7b9cc674e..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,29 +43,19 @@ 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(Buffer const & buf, InsetListingsParams const & par)
+       : InsetCollapsable(buf, par.status())
 {}
 
 
-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);
 }
 
 
@@ -71,7 +67,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
@@ -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();
 }