From 12bb380941437fe8a48774e7ceb5f17034dc3317 Mon Sep 17 00:00:00 2001 From: Richard Kimberly Heck Date: Sun, 10 Jan 2021 01:43:34 -0500 Subject: [PATCH] Fix bug #5972: Count words in citations (approximately). --- src/Buffer.cpp | 6 ++++++ src/frontends/qt/GuiApplication.cpp | 1 + src/insets/Inset.h | 3 +++ src/insets/InsetCitation.cpp | 16 ++++++++++++++-- src/insets/InsetCitation.h | 2 ++ 5 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/Buffer.cpp b/src/Buffer.cpp index 445503993e..40c91b8ce9 100644 --- a/src/Buffer.cpp +++ b/src/Buffer.cpp @@ -5365,6 +5365,12 @@ void Buffer::Impl::updateStatistics(DocIterator & from, DocIterator & to, bool s } else if (ins && ins->isSpace()) ++blank_count_; + else if (ins) { + pair words = ins->isWords(); + char_count_ += words.first; + word_count_ += words.second; + inword = false; + } else { char_type const c = par.getChar(pos); if (isPrintableNonspace(c)) diff --git a/src/frontends/qt/GuiApplication.cpp b/src/frontends/qt/GuiApplication.cpp index b0ec64ee09..0016f6d0dc 100644 --- a/src/frontends/qt/GuiApplication.cpp +++ b/src/frontends/qt/GuiApplication.cpp @@ -1056,6 +1056,7 @@ GuiApplication::GuiApplication(int & argc, char ** argv) setDesktopFileName(lyx_package); #endif + // FIXME Deprecated. Should use QRandomGenerator since 5.10 qsrand(QDateTime::currentDateTime().toTime_t()); // Install LyX translator for missing Qt translations diff --git a/src/insets/Inset.h b/src/insets/Inset.h index ab2c535e46..824ba46172 100644 --- a/src/insets/Inset.h +++ b/src/insets/Inset.h @@ -466,6 +466,9 @@ public: /// is this equivalent to a space (which is BTW different from /// a line separator)? virtual bool isSpace() const { return false; } + /// returns chars, words if the inset is equivalent to such, otherwise + /// (0,0), which should be interpreted as 'false' + virtual std::pair isWords() const { return std::pair(0, 0); } /// does this inset try to use all available space (like \\hfill does)? virtual bool isHfill() const { return false; } diff --git a/src/insets/InsetCitation.cpp b/src/insets/InsetCitation.cpp index ead9d3a8a0..6672ea1260 100644 --- a/src/insets/InsetCitation.cpp +++ b/src/insets/InsetCitation.cpp @@ -37,6 +37,7 @@ #include "support/FileNameList.h" #include "support/gettext.h" #include "support/lstrings.h" +#include "support/textutils.h" #include #include @@ -594,7 +595,7 @@ int InsetCitation::plaintext(odocstringstream & os, if (cmd == "nocite") return 0; - docstring const label = generateLabel(false); + docstring const label = generateLabel(); os << label; return label.size(); } @@ -658,7 +659,7 @@ docstring InsetCitation::xhtml(XMLStream & xs, OutputParams const &) const void InsetCitation::toString(odocstream & os) const { odocstringstream ods; - plaintext(ods, OutputParams(0)); + plaintext(ods, OutputParams(nullptr)); os << ods.str(); } @@ -775,6 +776,17 @@ void InsetCitation::latex(otexstream & os, OutputParams const & runparams) const os << "}"; } +pair InsetCitation::isWords() const +{ + docstring const label = generateLabel(false); + int words = 1; + for (auto const & c : label) { + if (lyx::isSpace(c)) + words++; + } + return pair(label.size(), words); +} + string InsetCitation::contextMenuName() const { diff --git a/src/insets/InsetCitation.h b/src/insets/InsetCitation.h index 3d97bbf058..f41ef5475d 100644 --- a/src/insets/InsetCitation.h +++ b/src/insets/InsetCitation.h @@ -101,6 +101,8 @@ public: bool openCitationPossible() const; /// search and open citation source void openCitation(); + /// + std::pair isWords() const override; private: /// tries to make a pretty label and makes a basic one if not