]> git.lyx.org Git - features.git/blobdiff - src/insets/InsetListings.cpp
Revert "Mark some intentional fall-throughs (in a way understandable to gcc)"
[features.git] / src / insets / InsetListings.cpp
index 0693b5af848c8d34d3f383c3c654000491ef8027..f75e155d4454a3034ed6578c814d4635a645b50f 100644 (file)
@@ -72,6 +72,15 @@ Inset::DisplayType InsetListings::display() const
 }
 
 
+docstring InsetListings::layoutName() const
+{
+       if (buffer().params().use_minted)
+               return from_ascii("MintedListings");
+       else
+               return from_ascii("Listings");
+}
+
+
 void InsetListings::write(ostream & os) const
 {
        os << "listings" << "\n";
@@ -125,7 +134,7 @@ void InsetListings::latex(otexstream & os, OutputParams const & runparams) const
                // then remove the relative options.
                vector<string> opts =
                        getVectorFromString(param_string, ",", false);
-               for (int i = 0; i < opts.size(); ++i) {
+               for (size_t i = 0; i < opts.size(); ++i) {
                        if (prefixIs(opts[i], "float")) {
                                if (prefixIs(opts[i], "float="))
                                        float_placement = opts[i].substr(6);
@@ -178,6 +187,9 @@ void InsetListings::latex(otexstream & os, OutputParams const & runparams) const
                encoding_switched = true;
        }
 
+       bool const captionfirst = !isfloat && par->isInset(0)
+                               && par->getInset(0)->lyxCode() == CAPTION_CODE;
+
        while (par != end) {
                pos_type siz = par->size();
                bool captionline = false;
@@ -256,7 +268,7 @@ void InsetListings::latex(otexstream & os, OutputParams const & runparams) const
                        os << "\\mintinline";
                        if (!param_string.empty())
                                os << "[" << from_utf8(param_string) << "]";
-                       os << "{" << minted_language << "}";
+                       os << "{" << ascii_lowercase(minted_language) << "}";
                } else {
                        os << "\\lstinline";
                        if (!param_string.empty())
@@ -268,23 +280,29 @@ void InsetListings::latex(otexstream & os, OutputParams const & runparams) const
                }
                os << delim << code << delim;
        } else if (use_minted) {
+               OutputParams rp = runparams;
+               rp.moving_arg = true;
+               TexString caption = getCaption(rp);
                if (isfloat) {
                        os << breakln << "\\begin{listing}";
                        if (!float_placement.empty())
                                os << '[' << float_placement << "]";
+               } else if (captionfirst && !caption.str.empty()) {
+                       os << breakln << "\\lyxmintcaption[t]{"
+                          << move(caption) << "}\n";
                }
                os << breakln << "\\begin{minted}";
                if (!param_string.empty())
                        os << "[" << param_string << "]";
-               os << "{" << minted_language << "}\n"
+               os << "{" << ascii_lowercase(minted_language) << "}\n"
                   << code << breakln << "\\end{minted}\n";
                if (isfloat) {
-                       OutputParams rp = runparams;
-                       rp.moving_arg = true;
-                       TexString caption = getCaption(rp);
                        if (!caption.str.empty())
                                os << "\\caption{" << move(caption) << "}\n";
                        os << "\\end{listing}\n";
+               } else if (!captionfirst && !caption.str.empty()) {
+                       os << breakln << "\\lyxmintcaption[b]{"
+                          << move(caption) << "}";
                }
        } else {
                OutputParams rp = runparams;
@@ -441,14 +459,18 @@ docstring const InsetListings::buttonLabel(BufferView const & bv) const
 
 void InsetListings::validate(LaTeXFeatures & features) const
 {
-       if (buffer().params().use_minted)
-               features.require("minted");
-       else
-               features.require("listings");
        features.useInsetLayout(getLayout());
        string param_string = params().params();
-       if (param_string.find("\\color") != string::npos)
-               features.require("color");
+       if (buffer().params().use_minted) {
+               features.require("minted");
+               OutputParams rp = features.runparams();
+               if (!params().isFloat() && !getCaption(rp).str.empty())
+                       features.require("lyxmintcaption");
+       } else {
+               features.require("listings");
+               if (contains(param_string, "\\color"))
+                       features.require("color");
+       }
        InsetCaptionable::validate(features);
 }
 
@@ -493,7 +515,11 @@ TexString InsetListings::getCaption(OutputParams const & runparams) const
        // TexString validity: the substitution preserves the number of newlines.
        // Moreover we assume that $2 does not contain newlines, so that the texrow
        // information remains accurate.
-       cap.str = from_utf8(regex_replace(to_utf8(cap.str), reg, new_cap));
+       // Replace '\n' with an improbable character from Private Use Area-A
+       // and then return to '\n' after the regex replacement.
+       docstring const capstr = subst(cap.str, char_type('\n'), 0xffffd);
+       cap.str = subst(from_utf8(regex_replace(to_utf8(capstr), reg, new_cap)),
+                       0xffffd, char_type('\n'));
        return cap;
 }