commit 16ca5290c0d619df23406275e6698594b90ce7ab
Author: Enrico Forestieri <[email protected]>
Date:   Sun Jul 22 22:22:13 2018 +0200

    Fix bug #11203
    
    Minted does not have a language option but it is possible to enter
    this option in the LyX interface for compatibility with the listings
    package, and also for letting to enter a language not present in the
    gui. So, this option is only used for properly specifying a language
    in a listing, unless it is entered in the document settings dialog.
    This case was not foreseen and thus the option was being passed to
    the package as is, causing havoc. With this commit the option is
    still available but is used to set a default language for a new
    listing in place of the default "tex" language used so far.
---
 src/BufferParams.cpp              |   15 +++++++++++++--
 src/frontends/qt4/GuiListings.cpp |   15 ++++++++++++---
 src/insets/InsetListings.cpp      |   16 ++++++++++++++--
 3 files changed, 39 insertions(+), 7 deletions(-)

diff --git a/src/BufferParams.cpp b/src/BufferParams.cpp
index 867f045..3819e6d 100644
--- a/src/BufferParams.cpp
+++ b/src/BufferParams.cpp
@@ -2264,7 +2264,18 @@ bool BufferParams::writeLaTeX(otexstream & os, 
LaTeXFeatures & features,
                else
                        os << "\\usepackage{listings}\n";
        }
-       if (!listings_params.empty()) {
+       string lst_params = listings_params;
+       // If minted, do not output the language option (bug 11203)
+       if (use_minted && contains(lst_params, "language=")) {
+               vector<string> opts =
+                       getVectorFromString(lst_params, ",", false);
+               for (size_t i = 0; i < opts.size(); ++i) {
+                       if (prefixIs(opts[i], "language="))
+                               opts.erase(opts.begin() + i--);
+               }
+               lst_params = getStringFromVector(opts, ",");
+       }
+       if (!lst_params.empty()) {
                if (use_minted)
                        os << "\\setminted{";
                else
@@ -2272,7 +2283,7 @@ bool BufferParams::writeLaTeX(otexstream & os, 
LaTeXFeatures & features,
                // do not test validity because listings_params is
                // supposed to be valid
                string par =
-                       
InsetListingsParams(listings_params).separatedParams(true);
+                       InsetListingsParams(lst_params).separatedParams(true);
                os << from_utf8(par);
                os << "}\n";
        }
diff --git a/src/frontends/qt4/GuiListings.cpp 
b/src/frontends/qt4/GuiListings.cpp
index 85a79b5..43d3691 100644
--- a/src/frontends/qt4/GuiListings.cpp
+++ b/src/frontends/qt4/GuiListings.cpp
@@ -319,9 +319,18 @@ string GuiListings::construct_params()
        InsetListingsParams par;
        par.setMinted(use_minted);
        if (use_minted) {
-               if (language == "no language" && !contains(extra, "language="))
-                       par.addParam("language", "TeX");
-               else
+               if (language == "no language" && !contains(extra, "language=")) 
{
+                       string const & blp = buffer().params().listings_params;
+                       size_t start = blp.find("language=");
+                       if (start != string::npos) {
+                               start += strlen("language=");
+                               size_t len = blp.find(",", start);
+                               if (len != string::npos)
+                                       len -= start;
+                               par.addParam("language", blp.substr(start, 
len));
+                       } else
+                               par.addParam("language", "TeX");
+               } else
                        par.addParam("language", language);
        } else if (language != "no language" && !contains(extra, "language=")) {
                if (dialect.empty())
diff --git a/src/insets/InsetListings.cpp b/src/insets/InsetListings.cpp
index c403ab8..ec9ad72 100644
--- a/src/insets/InsetListings.cpp
+++ b/src/insets/InsetListings.cpp
@@ -169,8 +169,20 @@ void InsetListings::latex(otexstream & os, OutputParams 
const & runparams) const
                param_string = getStringFromVector(opts, ",");
        }
        // Minted needs a language specification
-       if (minted_language.empty())
-               minted_language = "TeX";
+       if (minted_language.empty()) {
+               // If a language has been set globally, use that,
+               // otherwise use TeX by default
+               string const & blp = buffer().params().listings_params;
+               size_t start = blp.find("language=");
+               if (start != string::npos) {
+                       start += strlen("language=");
+                       size_t len = blp.find(",", start);
+                       if (len != string::npos)
+                               len -= start;
+                       minted_language = blp.substr(start, len);
+               } else
+                       minted_language = "TeX";
+       }
 
        // get the paragraphs. We can not output them directly to given 
odocstream
        // because we can not yet determine the delimiter character of 
\lstinline

Reply via email to