From eb28cb8755439d05714dd3a933bb7c94785c9452 Mon Sep 17 00:00:00 2001 From: Abdelrazak Younes Date: Thu, 22 May 2008 14:01:33 +0000 Subject: [PATCH] Unify Paragraph::asString() methods. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@24882 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/Cursor.cpp | 5 ++++- src/CutAndPaste.cpp | 4 ++-- src/Paragraph.cpp | 28 +++++++--------------------- src/Paragraph.h | 27 ++++++++++++++++++--------- src/Text.cpp | 4 ++-- src/TocBackend.cpp | 8 ++++---- src/insets/InsetBibitem.cpp | 2 +- src/insets/InsetBranch.cpp | 2 +- src/insets/InsetCaption.cpp | 2 +- src/insets/InsetFlex.cpp | 2 +- 10 files changed, 41 insertions(+), 43 deletions(-) diff --git a/src/Cursor.cpp b/src/Cursor.cpp index b385f025a1..b662faa82f 100644 --- a/src/Cursor.cpp +++ b/src/Cursor.cpp @@ -1850,11 +1850,14 @@ void Cursor::errorMessage(docstring const & msg) const } -docstring Cursor::selectionAsString(bool label) const +docstring Cursor::selectionAsString(bool with_label) const { if (!selection()) return docstring(); + int const label = with_label + ? AS_STR_LABEL | AS_STR_INSETS : AS_STR_INSETS; + if (inTexted()) { ParagraphList const & pars = text()->paragraphs(); diff --git a/src/CutAndPaste.cpp b/src/CutAndPaste.cpp index a535a8255d..506313fe82 100644 --- a/src/CutAndPaste.cpp +++ b/src/CutAndPaste.cpp @@ -552,7 +552,7 @@ vector availableSelections() ParagraphList::const_iterator pit = pars.begin(); ParagraphList::const_iterator pend = pars.end(); for (; pit != pend; ++pit) { - asciiSel += pit->asString(false); + asciiSel += pit->asString(AS_STR_INSETS); if (asciiSel.size() > 25) { asciiSel.replace(22, docstring::npos, from_ascii("...")); @@ -770,7 +770,7 @@ void clearCutStack() docstring selection(size_t sel_index) { return sel_index < theCuts.size() - ? theCuts[sel_index].first.back().asString(false) + ? theCuts[sel_index].first.back().asString(AS_STR_INSETS) : docstring(); } diff --git a/src/Paragraph.cpp b/src/Paragraph.cpp index 4ad0d59be0..1e86b52fd3 100644 --- a/src/Paragraph.cpp +++ b/src/Paragraph.cpp @@ -2302,40 +2302,26 @@ bool Paragraph::isMultiLingual(BufferParams const & bparams) const } -docstring const Paragraph::printableString(bool label) const +docstring Paragraph::asString(int options) const { - odocstringstream os; - if (label && !params().labelString().empty()) - os << params().labelString() << ' '; - pos_type end = size(); - for (pos_type i = 0; i < end; ++i) { - char_type const c = d->text_[i]; - if (isPrintable(c)) - os.put(c); - } - return os.str(); -} - - -docstring Paragraph::asString(bool label) const -{ - return asString(0, size(), label); + return asString(0, size(), options); } -docstring Paragraph::asString(pos_type beg, pos_type end, bool label) const +docstring Paragraph::asString(pos_type beg, pos_type end, int options) const { - odocstringstream os; - if (beg == 0 && label && !d->params_.labelString().empty()) + if (beg == 0 + && options & AS_STR_LABEL + && !d->params_.labelString().empty()) os << d->params_.labelString() << ' '; for (pos_type i = beg; i < end; ++i) { char_type const c = d->text_[i]; if (isPrintable(c)) os.put(c); - else if (c == META_INSET) + else if (c == META_INSET && options & AS_STR_INSETS) getInset(i)->textString(os); } diff --git a/src/Paragraph.h b/src/Paragraph.h index cdf2e58d97..a75ec79111 100644 --- a/src/Paragraph.h +++ b/src/Paragraph.h @@ -72,6 +72,15 @@ enum TextCase { }; +/// +enum AsStringParameter +{ + AS_STR_NONE = 0, ///< No option, only printable characters. + AS_STR_LABEL = 1, ///< Prefix with paragraph label. + AS_STR_INSETS = 2 ///< Go into insets. +}; + + /// A Paragraph holds all text, attributes and insets in a text paragraph class Paragraph { @@ -98,15 +107,15 @@ public: bool isMultiLingual(BufferParams const &) const; /// Convert the paragraph to a string. - /// This method doesn't go inside insets, only printable characters in this - /// paragraph are used. - /// Used for building the table of contents - docstring const printableString(bool label) const; - - /// Convert the paragraph to a string. - docstring asString(bool label) const; - /// - docstring asString(pos_type beg, pos_type end, bool label) const; + /// \param AsStringParameter options. This can contain any combination of + /// asStringParameter values. Valid examples: + /// asString(AS_STR_LABEL) + /// asString(AS_STR_LABEL | AS_STR_INSETS) + /// asString(AS_STR_INSETS) + docstring asString(int options = AS_STR_NONE) const; + /// + docstring asString(pos_type beg, pos_type end, + int options = AS_STR_NONE) const; /// void write(std::ostream &, BufferParams const &, diff --git a/src/Text.cpp b/src/Text.cpp index c324b572f5..19b3ead091 100644 --- a/src/Text.cpp +++ b/src/Text.cpp @@ -1377,7 +1377,7 @@ docstring Text::getPossibleLabel(Cursor & cur) const Layout const * layout = &(pars_[pit].layout()); docstring text; - docstring par_text = pars_[pit].asString(false); + docstring par_text = pars_[pit].asString(); string piece; // the return string of math matrices might contain linebreaks par_text = subst(par_text, '\n', '-'); @@ -1527,7 +1527,7 @@ docstring Text::previousWord(CursorSlice const & sl) const return docstring(); Paragraph const & par = sl.paragraph(); - return par.asString(from.pos(), to.pos(), false); + return par.asString(from.pos(), to.pos()); } diff --git a/src/TocBackend.cpp b/src/TocBackend.cpp index 0e20e5eb00..1ec56700e8 100644 --- a/src/TocBackend.cpp +++ b/src/TocBackend.cpp @@ -134,7 +134,7 @@ void TocBackend::updateItem(DocIterator const & dit) *static_cast(inset).paragraphs().begin(); if (!par.labelString().empty()) tocstring = par.labelString() + ' '; - tocstring += inset_par.printableString(false); + tocstring += inset_par.asString(); break; } } @@ -142,7 +142,7 @@ void TocBackend::updateItem(DocIterator const & dit) int const toclevel = par.layout().toclevel; if (toclevel != Layout::NOT_IN_TOC && toclevel >= min_toclevel && tocstring.empty()) - tocstring = par.printableString(true); + tocstring = par.asString(AS_STR_LABEL); const_cast(*toc_item).str_ = tocstring; } @@ -181,7 +181,7 @@ void TocBackend::update() *static_cast(inset).paragraphs().begin(); if (!pit->labelString().empty()) tocstring = pit->labelString() + ' '; - tocstring += par.printableString(false); + tocstring += par.asString(); break; } default: @@ -196,7 +196,7 @@ void TocBackend::update() pit.pos() = 0; // insert this into the table of contents if (tocstring.empty()) - tocstring = pit->printableString(true); + tocstring = pit->asString(AS_STR_LABEL); toc.push_back(TocItem(pit, toclevel - min_toclevel, tocstring)); } diff --git a/src/insets/InsetBibitem.cpp b/src/insets/InsetBibitem.cpp index 8f235819df..3978c60df8 100644 --- a/src/insets/InsetBibitem.cpp +++ b/src/insets/InsetBibitem.cpp @@ -231,7 +231,7 @@ void InsetBibitem::fillWithBibKeys(BiblioInfo & keys, InsetIterator const & it) keyvalmap[from_ascii("label")] = getParam("label"); DocIterator doc_it(it); doc_it.forwardPos(); - keyvalmap[from_ascii("ref")] = doc_it.paragraph().asString(false); + keyvalmap[from_ascii("ref")] = doc_it.paragraph().asString(); keys[key] = keyvalmap; } diff --git a/src/insets/InsetBranch.cpp b/src/insets/InsetBranch.cpp index 050a3c9c5d..85e5a2a472 100644 --- a/src/insets/InsetBranch.cpp +++ b/src/insets/InsetBranch.cpp @@ -235,7 +235,7 @@ int InsetBranch::docbook(odocstream & os, void InsetBranch::textString(odocstream & os) const { if (isBranchSelected()) - os << paragraphs().begin()->asString(true); + os << paragraphs().begin()->asString(AS_STR_LABEL | AS_STR_INSETS); } diff --git a/src/insets/InsetCaption.cpp b/src/insets/InsetCaption.cpp index f658b6173f..c3fb149bf9 100644 --- a/src/insets/InsetCaption.cpp +++ b/src/insets/InsetCaption.cpp @@ -111,7 +111,7 @@ void InsetCaption::addToToc(DocIterator const & cpit) pit.push_back(CursorSlice(*this)); Toc & toc = buffer().tocBackend().toc(type_); - docstring const str = full_label_ + ". " + text_.getPar(0).printableString(false); + docstring const str = full_label_ + ". " + text_.getPar(0).asString(); toc.push_back(TocItem(pit, 0, str)); } diff --git a/src/insets/InsetFlex.cpp b/src/insets/InsetFlex.cpp index 1972f52295..717fb2b100 100644 --- a/src/insets/InsetFlex.cpp +++ b/src/insets/InsetFlex.cpp @@ -110,7 +110,7 @@ int InsetFlex::docbook(odocstream & os, OutputParams const & runparams) const void InsetFlex::textString(odocstream & os) const { - os << paragraphs().begin()->asString(true); + os << paragraphs().begin()->asString(AS_STR_LABEL | AS_STR_INSETS); } -- 2.39.2