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

    \cprotect inline listings with special chars
---
 src/insets/InsetListings.cpp | 28 ++++++++++++++++++++++++++++
 src/insets/InsetListings.h   |  3 ++-
 2 files changed, 30 insertions(+), 1 deletion(-)

diff --git a/src/insets/InsetListings.cpp b/src/insets/InsetListings.cpp
index 2557cd7657..22d67f138a 100644
--- a/src/insets/InsetListings.cpp
+++ b/src/insets/InsetListings.cpp
@@ -644,6 +644,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 fc1023b630..4491843e35 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;
        ///
        void setInline(bool const b) { params().setInline(b); }
 private:
-- 
lyx-cvs mailing list
[email protected]
https://lists.lyx.org/mailman/listinfo/lyx-cvs

Reply via email to