commit 676a0639c505d52336e3228c44c2515ccbbaf34a
Author: Guillaume Munch <g...@lyx.org>
Date:   Sat Sep 24 00:49:00 2016 +0200

    Use otexstringstream for the captions of InsetCaptionables
    
    * Enable TexRow for InsetListings caption.
    
    * Move getCaption* from InsetText to InsetCaptionable.
    
    * Clean-up caption generation for InsetFloat.
---
 src/insets/InsetCaptionable.cpp |   52 +++++++++++++++++++++++++++++++++++++++
 src/insets/InsetCaptionable.h   |   10 +++++--
 src/insets/InsetFloat.cpp       |   21 +++------------
 src/insets/InsetFloat.h         |    7 +++--
 src/insets/InsetIndex.cpp       |    2 +
 src/insets/InsetListings.cpp    |   29 +++++++++++----------
 src/insets/InsetListings.h      |    5 ++-
 src/insets/InsetText.cpp        |   47 -----------------------------------
 src/insets/InsetText.h          |    6 ----
 9 files changed, 88 insertions(+), 91 deletions(-)

diff --git a/src/insets/InsetCaptionable.cpp b/src/insets/InsetCaptionable.cpp
index c105d5e..94326f4 100644
--- a/src/insets/InsetCaptionable.cpp
+++ b/src/insets/InsetCaptionable.cpp
@@ -15,13 +15,19 @@
 
 #include "InsetCaptionable.h"
 
+#include "InsetCaption.h"
+
 #include "Buffer.h"
 #include "BufferParams.h"
 #include "BufferView.h"
 #include "FloatList.h"
+#include "InsetList.h"
+#include "output_xhtml.h"
 #include "TextClass.h"
 #include "TocBackend.h"
 
+#include "support/docstream.h"
+
 using namespace std;
 
 
@@ -44,6 +50,52 @@ docstring InsetCaptionable::floatName(string const & type) 
const
 }
 
 
+InsetCaption const * InsetCaptionable::getCaptionInset() const
+{
+       ParagraphList::const_iterator pit = paragraphs().begin();
+       for (; pit != paragraphs().end(); ++pit) {
+               InsetList::const_iterator it = pit->insetList().begin();
+               for (; it != pit->insetList().end(); ++it) {
+                       Inset & inset = *it->inset;
+                       if (inset.lyxCode() == CAPTION_CODE) {
+                               InsetCaption const * ins =
+                                       static_cast<InsetCaption const 
*>(it->inset);
+                               return ins;
+                       }
+               }
+       }
+       return 0;
+}
+
+
+docstring InsetCaptionable::getCaptionText(OutputParams const & runparams) 
const
+{
+       InsetCaption const * ins = getCaptionInset();
+       if (ins == 0)
+               return docstring();
+
+       odocstringstream ods;
+       ins->getCaptionAsPlaintext(ods, runparams);
+       return ods.str();
+}
+
+
+docstring InsetCaptionable::getCaptionHTML(OutputParams const & runparams) 
const
+{
+       InsetCaption const * ins = getCaptionInset();
+       if (ins == 0)
+               return docstring();
+
+       odocstringstream ods;
+       XHTMLStream xs(ods);
+       docstring def = ins->getCaptionAsHTML(xs, runparams);
+       if (!def.empty())
+               // should already have been escaped
+               xs << XHTMLStream::ESCAPE_NONE << def << '\n';
+       return ods.str();
+}
+
+
 void InsetCaptionable::addToToc(DocIterator const & cpit, bool output_active,
                                                                UpdateType 
utype) const
 {
diff --git a/src/insets/InsetCaptionable.h b/src/insets/InsetCaptionable.h
index 94202e4..5e06d77 100644
--- a/src/insets/InsetCaptionable.h
+++ b/src/insets/InsetCaptionable.h
@@ -23,9 +23,7 @@ namespace lyx {
 class InsetCaptionable : public InsetCollapsable
 {
 public:
-       InsetCaptionable(Buffer * buffer)
-               : InsetCollapsable(buffer), caption_type_("senseless") {}
-       InsetCaptionable(Buffer * buffer, std::string const & type)
+       InsetCaptionable(Buffer * buffer, std::string const & type = 
"senseless")
                : InsetCollapsable(buffer), caption_type_(type) {}
        ///
        std::string const & captionType() const { return caption_type_; }
@@ -34,6 +32,12 @@ public:
        ///
 protected:
        ///
+       InsetCaption const * getCaptionInset() const;
+       ///
+       docstring getCaptionText(OutputParams const &) const;
+       ///
+       docstring getCaptionHTML(OutputParams const &) const;
+       ///
        virtual void setCaptionType(std::string const & type);
        /// are our captions subcaptions?
        virtual bool hasSubCaptions(ParIterator const &) const { return false; }
diff --git a/src/insets/InsetFloat.cpp b/src/insets/InsetFloat.cpp
index 5d966af..1030b45 100644
--- a/src/insets/InsetFloat.cpp
+++ b/src/insets/InsetFloat.cpp
@@ -330,7 +330,7 @@ void InsetFloat::latex(otexstream & os, OutputParams const 
& runparams_in) const
 
                OutputParams rp = runparams_in;
                rp.moving_arg = true;
-               getCaption(os, rp);
+               os << getCaption(rp);
                os << '{';
                // The main argument is the contents of the float. This is not 
a moving argument.
                if (!paragraphs().empty())
@@ -494,25 +494,13 @@ bool InsetFloat::allowsCaptionVariation(std::string const 
& newtype) const
 }
 
 
-docstring InsetFloat::getCaption(OutputParams const & runparams) const
+TexString InsetFloat::getCaption(OutputParams const & runparams) const
 {
-       odocstringstream ods;
-       otexstream os(ods);
-       getCaption(os, runparams);
-       return ods.str();
-}
-
-
-void InsetFloat::getCaption(otexstream & os,
-                                                       OutputParams const & 
runparams) const
-{
-       if (paragraphs().empty())
-               return;
-
        InsetCaption const * ins = getCaptionInset();
        if (ins == 0)
-               return;
+               return TexString();
 
+       otexstringstream os;
        ins->getArgs(os, runparams);
 
        os << '[';
@@ -525,6 +513,7 @@ void InsetFloat::getCaption(otexstream & os,
                arg = '{' + arg + '}';
        os << move(ts);
        os << ']';
+       return os.release();
 }
 
 
diff --git a/src/insets/InsetFloat.h b/src/insets/InsetFloat.h
index 9c20267..7317ab8 100644
--- a/src/insets/InsetFloat.h
+++ b/src/insets/InsetFloat.h
@@ -18,6 +18,9 @@
 
 namespace lyx {
 
+class TexString;
+
+
 class InsetFloatParams
 {
 public:
@@ -107,9 +110,7 @@ private:
        ///
        Inset * clone() const { return new InsetFloat(*this); }
        ///
-       docstring getCaption(OutputParams const &) const;
-       ///
-       void getCaption(otexstream & os, OutputParams const & runparams) const;
+       TexString getCaption(OutputParams const &) const;
 
        InsetFloatParams params_;
 };
diff --git a/src/insets/InsetIndex.cpp b/src/insets/InsetIndex.cpp
index 5c8a9dd..f8dca3b 100644
--- a/src/insets/InsetIndex.cpp
+++ b/src/insets/InsetIndex.cpp
@@ -76,6 +76,8 @@ void InsetIndex::latex(otexstream & os, OutputParams const & 
runparams_in) const
 
        // get contents of InsetText as LaTeX and plaintext
        odocstringstream ourlatex;
+       // FIXME: do Tex/Row correspondence (I don't currently understand what 
is
+       // being generated from latexstr below)
        otexstream ots(ourlatex);
        InsetText::latex(ots, runparams);
        odocstringstream ourplain;
diff --git a/src/insets/InsetListings.cpp b/src/insets/InsetListings.cpp
index 67e5234..8211ba3 100644
--- a/src/insets/InsetListings.cpp
+++ b/src/insets/InsetListings.cpp
@@ -30,6 +30,7 @@
 #include "output_xhtml.h"
 #include "OutputParams.h"
 #include "TextClass.h"
+#include "TexRow.h"
 #include "texstream.h"
 
 #include "support/debug.h"
@@ -222,13 +223,13 @@ void InsetListings::latex(otexstream & os, OutputParams 
const & runparams) const
        } else {
                OutputParams rp = runparams;
                rp.moving_arg = true;
-               docstring const caption = getCaption(rp);
-               if (param_string.empty() && caption.empty())
+               TexString caption = getCaption(rp);
+               if (param_string.empty() && caption.str.empty())
                        os << breakln << "\\begin{lstlisting}\n";
                else {
                        os << breakln << "\\begin{lstlisting}[";
-                       if (!caption.empty()) {
-                               os << "caption={" << caption << '}';
+                       if (!caption.str.empty()) {
+                               os << "caption={" << move(caption) << '}';
                                if (!param_string.empty())
                                        os << ',';
                        }
@@ -388,17 +389,13 @@ bool InsetListings::showInsetDialog(BufferView * bv) const
 }
 
 
-docstring InsetListings::getCaption(OutputParams const & runparams) const
+TexString InsetListings::getCaption(OutputParams const & runparams) const
 {
-       if (paragraphs().empty())
-               return docstring();
-
        InsetCaption const * ins = getCaptionInset();
        if (ins == 0)
-               return docstring();
+               return TexString();
 
-       odocstringstream ods;
-       otexstream os(ods);
+       otexstringstream os;
        ins->getArgs(os, runparams);
        ins->getArgument(os, runparams);
 
@@ -407,8 +404,8 @@ docstring InsetListings::getCaption(OutputParams const & 
runparams) const
 
        // the caption may contain \label{} but the listings
        // package prefer caption={}, label={}
-       docstring cap = ods.str();
-       if (!contains(to_utf8(cap), "\\label{"))
+       TexString cap = os.release();
+       if (!contains(cap.str, from_ascii("\\label{")))
                return cap;
        // convert from
        //     blah1\label{blah2} blah3
@@ -420,7 +417,11 @@ docstring InsetListings::getCaption(OutputParams const & 
runparams) const
        // NOTE that } is not allowed in blah2.
        regex const reg("(.*)\\\\label\\{(.*?)\\}(.*)");
        string const new_cap("$1$3},label={$2");
-       return from_utf8(regex_replace(to_utf8(cap), reg, new_cap));
+       // 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));
+       return cap;
 }
 
 
diff --git a/src/insets/InsetListings.h b/src/insets/InsetListings.h
index 84acbb9..83debff 100644
--- a/src/insets/InsetListings.h
+++ b/src/insets/InsetListings.h
@@ -18,6 +18,7 @@
 namespace lyx {
 
 class LaTeXFeatures;
+class TexString;
 
 /////////////////////////////////////////////////////////////////////////
 //
@@ -75,9 +76,9 @@ private:
        ///
        docstring const buttonLabel(BufferView const & bv) const;
        ///
-       docstring getCaption(OutputParams const &) const;
+       TexString getCaption(OutputParams const &) const;
        ///
-       bool insetAllowed(InsetCode c) const { return c == CAPTION_CODE; } 
+       bool insetAllowed(InsetCode c) const { return c == CAPTION_CODE; }
 
        ///
        InsetListingsParams params_;
diff --git a/src/insets/InsetText.cpp b/src/insets/InsetText.cpp
index df4f6e2..41427ce 100644
--- a/src/insets/InsetText.cpp
+++ b/src/insets/InsetText.cpp
@@ -27,7 +27,6 @@
 #include "ErrorList.h"
 #include "FuncRequest.h"
 #include "FuncStatus.h"
-#include "InsetCaption.h"
 #include "InsetList.h"
 #include "Intl.h"
 #include "Language.h"
@@ -1019,52 +1018,6 @@ docstring InsetText::toolTipText(docstring prefix, 
size_t const len) const
 }
 
 
-InsetCaption const * InsetText::getCaptionInset() const
-{
-       ParagraphList::const_iterator pit = paragraphs().begin();
-       for (; pit != paragraphs().end(); ++pit) {
-               InsetList::const_iterator it = pit->insetList().begin();
-               for (; it != pit->insetList().end(); ++it) {
-                       Inset & inset = *it->inset;
-                       if (inset.lyxCode() == CAPTION_CODE) {
-                               InsetCaption const * ins =
-                                       static_cast<InsetCaption const 
*>(it->inset);
-                               return ins;
-                       }
-               }
-       }
-       return 0;
-}
-
-
-docstring InsetText::getCaptionText(OutputParams const & runparams) const
-{
-       InsetCaption const * ins = getCaptionInset();
-       if (ins == 0)
-               return docstring();
-
-       odocstringstream ods;
-       ins->getCaptionAsPlaintext(ods, runparams);
-       return ods.str();
-}
-
-
-docstring InsetText::getCaptionHTML(OutputParams const & runparams) const
-{
-       InsetCaption const * ins = getCaptionInset();
-       if (ins == 0)
-               return docstring();
-
-       odocstringstream ods;
-       XHTMLStream xs(ods);
-       docstring def = ins->getCaptionAsHTML(xs, runparams);
-       if (!def.empty())
-               // should already have been escaped
-               xs << XHTMLStream::ESCAPE_NONE << def << '\n';
-       return ods.str();
-}
-
-
 InsetText::XHTMLOptions operator|(InsetText::XHTMLOptions a1, 
InsetText::XHTMLOptions a2)
 {
        return static_cast<InsetText::XHTMLOptions>((int)a1 | (int)a2);
diff --git a/src/insets/InsetText.h b/src/insets/InsetText.h
index ac0cf69..be4767f 100644
--- a/src/insets/InsetText.h
+++ b/src/insets/InsetText.h
@@ -220,12 +220,6 @@ public:
        void doDispatch(Cursor & cur, FuncRequest & cmd);
 protected:
        ///
-       InsetCaption const * getCaptionInset() const;
-       ///
-       docstring getCaptionText(OutputParams const &) const;
-       ///
-       docstring getCaptionHTML(OutputParams const &) const;
-       ///
        void iterateForToc(DocIterator const & cdit, bool output_active,
                                           UpdateType utype) const;
 private:

Reply via email to