commit 619d04c023e9cdc2237c10c75530e0e3b37b6030
Author: Juergen Spitzmueller <[email protected]>
Date:   Sat Oct 17 09:10:45 2026 +0200

    \cprotect inline listings with special chars
    
    (cherry picked from commit 5778bb97cd5af44e3bb697e4fb5c980c8e754e36)
---
 src/insets/InsetListings.cpp | 28 ++++++++++++++++++++++++++++
 src/insets/InsetListings.h   |  3 ++-
 status.25x                   |  2 ++
 3 files changed, 32 insertions(+), 1 deletion(-)

diff --git a/src/insets/InsetListings.cpp b/src/insets/InsetListings.cpp
index 9a57622892..abed0759d9 100644
--- a/src/insets/InsetListings.cpp
+++ b/src/insets/InsetListings.cpp
@@ -627,6 +627,34 @@ void InsetListings::validate(LaTeXFeatures & features) 
const
 }
 
 
+bool InsetListings::needsCProtection(bool const maintext, bool const fragile) 
const
+{
+       // displayed listings always need it.
+       if (!params().isInline())
+               return true;
+
+       // Inline listings need cprotection if they contain specific chars
+       int const nchars_escape = 9;
+       static char_type const chars_escape[nchars_escape] = {
+               '&', '_', '$', '%', '#', '^', '{', '}', '\\'};
+
+       ParagraphList const & pars = paragraphs();
+       pit_type pend = pit_type(paragraphs().size());
+
+       for (pit_type pit = 0; pit != pend; ++pit) {
+               Paragraph const & par = pars[size_type(pit)];
+               if (par.needsCProtection(fragile))
+                       return true;
+               docstring const par_str = par.asString();
+               for (int k = 0; k < nchars_escape; k++) {
+                       if (!maintext && contains(par_str, chars_escape[k]))
+                               return true;
+               }
+       }
+       return false;
+}
+
+
 bool InsetListings::showInsetDialog(BufferView * bv) const
 {
        bv->showDialog("listings", params2string(params()),
diff --git a/src/insets/InsetListings.h b/src/insets/InsetListings.h
index bd3833460d..8f823f114b 100644
--- a/src/insets/InsetListings.h
+++ b/src/insets/InsetListings.h
@@ -45,7 +45,8 @@ public:
        ///
        bool isEnvironment() const override { return !params().isInline(); }
        ///
-       bool needsCProtection(bool const, bool const) const override { return 
!params().isInline(); }
+       bool needsCProtection(bool const maintext = false,
+                             bool const fragile = false) const override;
 private:
        ///
        bool isLabeled() const override { return true; }
diff --git a/status.25x b/status.25x
index da12974cf0..f01fcaeaf7 100644
--- a/status.25x
+++ b/status.25x
@@ -57,6 +57,8 @@ What's new
   
 - Fix missing buffer update with argument auto-insertion (bug 13259).
 
+- Handle nested inline listings with special chars by \cprotect'ing them.
+
 
 * USER INTERFACE
 
-- 
lyx-cvs mailing list
[email protected]
https://lists.lyx.org/mailman/listinfo/lyx-cvs

Reply via email to