]> 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 7be13bcfbdc211868be1af509b01f6686a410ec0..f75e155d4454a3034ed6578c814d4635a645b50f 100644 (file)
@@ -187,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;
@@ -265,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())
@@ -277,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;
@@ -427,8 +436,7 @@ bool InsetListings::getStatus(Cursor & cur, FuncRequest const & cmd,
                        return true;
                case LFUN_CAPTION_INSERT: {
                        // the inset outputs at most one caption
-                       if (params().isInline() || !params().isFloat()
-                                               || getCaptionInset()) {
+                       if (params().isInline() || getCaptionInset()) {
                                status.setEnabled(false);
                                return true;
                        }
@@ -451,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);
 }
 
@@ -503,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;
 }