From 922aa7b1e8f94620d7825b2797e8eb3831773ef1 Mon Sep 17 00:00:00 2001 From: Pavel Sanda Date: Tue, 8 Jan 2008 15:18:00 +0000 Subject: [PATCH] Add characters counting. Introduce LFUN_STATISTICS. see http://www.mail-archive.com/lyx-devel@lists.lyx.org/msg134033.html . git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@22433 a592a061-630c-0410-9148-cb99ea01b6c8 --- lib/ui/classic.ui | 2 +- lib/ui/stdmenus.inc | 2 +- src/BufferView.cpp | 42 ++++++++++++++++++++++++------------------ src/LyXAction.cpp | 2 +- src/buffer_funcs.cpp | 28 ++++++++++++++++++++++++++++ src/buffer_funcs.h | 3 +++ src/lfuns.h | 11 ++++++++++- 7 files changed, 68 insertions(+), 22 deletions(-) diff --git a/lib/ui/classic.ui b/lib/ui/classic.ui index b4b6039a1d..9c68ab150b 100644 --- a/lib/ui/classic.ui +++ b/lib/ui/classic.ui @@ -103,7 +103,7 @@ Menuset #Item "Read Only" "buffer-toggle-read-only" Item "Spellchecker...|S" "dialog-show spellchecker" Item "Thesaurus..." "thesaurus-entry" - Item "Count Words|W" "words-count" + Item "Statistics...|i" "statistics" Item "Check TeX|h" "buffer-chktex" Submenu "Change Tracking|g" "edit_change" Separator diff --git a/lib/ui/stdmenus.inc b/lib/ui/stdmenus.inc index 5eb863ae56..1ed7123e89 100644 --- a/lib/ui/stdmenus.inc +++ b/lib/ui/stdmenus.inc @@ -491,7 +491,7 @@ Menuset Menu "tools" Item "Spellchecker...|S" "dialog-show spellchecker" OptItem "Thesaurus...|T" "thesaurus-entry" - Item "Count Words|W" "words-count" + Item "Statistics...|a" "statistics" OptItem "Check TeX|h" "buffer-chktex" Item "TeX Information|I" "dialog-show texinfo" Separator diff --git a/src/BufferView.cpp b/src/BufferView.cpp index c6c9d924cb..628e684728 100644 --- a/src/BufferView.cpp +++ b/src/BufferView.cpp @@ -787,7 +787,7 @@ FuncStatus BufferView::getStatus(FuncRequest const & cmd) case LFUN_SCREEN_RECENTER: case LFUN_BIBTEX_DATABASE_ADD: case LFUN_BIBTEX_DATABASE_DEL: - case LFUN_WORDS_COUNT: + case LFUN_STATISTICS: case LFUN_NEXT_INSET_TOGGLE: flag.enabled(true); break; @@ -1134,7 +1134,7 @@ bool BufferView::dispatch(FuncRequest const & cmd) break; } - case LFUN_WORDS_COUNT: { + case LFUN_STATISTICS: { DocIterator from, to; if (cur.selection()) { from = cur.selectionBegin(); @@ -1143,24 +1143,30 @@ bool BufferView::dispatch(FuncRequest const & cmd) from = doc_iterator_begin(buffer_.inset()); to = doc_iterator_end(buffer_.inset()); } - int const count = countWords(from, to); + int const words = countWords(from, to); + int const chars = countChars(from, to, false); + int const chars_blanks = countChars(from, to, true); docstring message; - if (count != 1) { - if (cur.selection()) - message = bformat(_("%1$d words in selection."), - count); - else - message = bformat(_("%1$d words in document."), - count); - } - else { - if (cur.selection()) - message = _("One word in selection."); - else - message = _("One word in document."); - } + if (cur.selection()) + message = _("Statistics for the selection:\n"); + else + message = _("Statistics for the document:\n"); + if (words != 1) + message += bformat(_("\n%1$d words"), words); + else + message += _("\nOne word"); + if (chars_blanks != 1) + message += bformat(_("\n%1$d characters (including blanks)"), + chars_blanks); + else + message += _("\nOne character (including blanks)"); + if (chars != 1) + message += bformat(_("\n%1$d characters (excluding blanks)"), + chars); + else + message += _("\nOne character (excluding blanks)"); - Alert::information(_("Count words"), message); + Alert::information(_("Statistics"), message); } break; diff --git a/src/LyXAction.cpp b/src/LyXAction.cpp index de2d41e7a3..243e2f9e9f 100644 --- a/src/LyXAction.cpp +++ b/src/LyXAction.cpp @@ -369,7 +369,7 @@ void LyXAction::init() { LFUN_INSET_REFRESH, "", Noop, Hidden }, { LFUN_BUFFER_NEXT, "buffer-next", ReadOnly, Buffer }, { LFUN_BUFFER_PREVIOUS, "buffer-previous", ReadOnly, Buffer }, - { LFUN_WORDS_COUNT, "words-count", ReadOnly, System }, + { LFUN_STATISTICS, "statistics", ReadOnly, System }, { LFUN_FINISHED_FORWARD, "", ReadOnly, Hidden }, { LFUN_FINISHED_BACKWARD, "", ReadOnly, Hidden }, { LFUN_FINISHED_RIGHT, "", ReadOnly, Hidden }, diff --git a/src/buffer_funcs.cpp b/src/buffer_funcs.cpp index d38e3aaaa7..23ad4eb128 100644 --- a/src/buffer_funcs.cpp +++ b/src/buffer_funcs.cpp @@ -49,6 +49,7 @@ #include "support/filetools.h" #include "support/gettext.h" #include "support/lstrings.h" +#include "support/textutils.h" using namespace std; using namespace lyx::support; @@ -182,6 +183,33 @@ int countWords(DocIterator const & from, DocIterator const & to) } +int countChars(DocIterator const & from, DocIterator const & to, bool with_blanks) +{ + int chars = 0; + int blanks = 0; + for (DocIterator dit = from ; dit != to ; dit.forwardPos()) { + if (dit.inTexted() + && dit.pos() != dit.lastpos() + && !dit.paragraph().isDeleted(dit.pos())) { + if (dit.paragraph().isInset(dit.pos())) { + if (dit.paragraph().getInset(dit.pos())->isLetter()) + ++chars; + else if (dit.paragraph().getInset(dit.pos())->isSpace() && with_blanks) + ++blanks; + } else { + char_type const c = dit.paragraph().getChar(dit.pos()); + if (isPrintableNonspace(c)) + ++chars; + else if (isSpace(c) && with_blanks) + ++blanks; + } + } + } + + return chars + blanks; +} + + namespace { depth_type getDepth(DocIterator const & it) diff --git a/src/buffer_funcs.h b/src/buffer_funcs.h index 061dc80840..b5be3490d6 100644 --- a/src/buffer_funcs.h +++ b/src/buffer_funcs.h @@ -44,6 +44,9 @@ Buffer * newUnnamedFile(std::string const & templatename, /// Count the number of words in the text between these two iterators int countWords(DocIterator const & from, DocIterator const & to); +/// Count the number of chars in the text between these two iterators +int countChars(DocIterator const & from, DocIterator const & to, bool with_blanks); + /// updates all counters void updateLabels(Buffer const &, bool childonly = false); diff --git a/src/lfuns.h b/src/lfuns.h index 698c60736e..c6babe6d18 100644 --- a/src/lfuns.h +++ b/src/lfuns.h @@ -552,7 +552,16 @@ enum kb_action { LFUN_INSET_REFRESH, LFUN_BUFFER_NEXT, LFUN_BUFFER_PREVIOUS, - LFUN_WORDS_COUNT, +/** + * LFUN_STATISTICS + * \li Action: Count the statistics (number of words and characters) + in the document or in the given selection. + * \li Notion: Note that this function gives the number of words/chars written, + not the number of characters which will be typesetted. + * \li Syntax: words-count + * \li Origin: lasgouttes, Jan 27 2004; ps, Jan 8 2008 + */ + LFUN_STATISTICS, // 260 LFUN_CHANGES_OUTPUT, // jspitzm 20050121 LFUN_BIBTEX_DATABASE_ADD,