Uwe Stöhr wrote: > > this will be only informative counter. one reason is the different views > > what is to be counted, > > LyX should display the following (as Word and other word processors do): > > - number of words > - number of characters except spaces > - number of characters including spaces > - number of paragraphs
FWIW, I had a go at this feature myself some time ago and implemented at least (2) and (3) (chars with and without blanks). Also, I've renamed the lfun to "statistics", which strikes me more appropriate. Attached the patch. Pavel, maybe you can use something of it. Jürgen
Index: src/LyXAction.cpp =================================================================== --- src/LyXAction.cpp (Revision 22429) +++ src/LyXAction.cpp (Arbeitskopie) @@ -369,7 +369,7 @@ { 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 }, Index: src/buffer_funcs.h =================================================================== --- src/buffer_funcs.h (Revision 22429) +++ src/buffer_funcs.h (Arbeitskopie) @@ -44,6 +44,9 @@ /// 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); Index: src/BufferView.cpp =================================================================== --- src/BufferView.cpp (Revision 22429) +++ src/BufferView.cpp (Arbeitskopie) @@ -787,7 +787,7 @@ 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 @@ break; } - case LFUN_WORDS_COUNT: { + case LFUN_STATISTICS: { DocIterator from, to; if (cur.selection()) { from = cur.selectionBegin(); @@ -1143,24 +1143,30 @@ 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 = _("The selection contains:"); + else + message = _("The document contains:"); + 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; Index: src/buffer_funcs.cpp =================================================================== --- src/buffer_funcs.cpp (Revision 22429) +++ src/buffer_funcs.cpp (Arbeitskopie) @@ -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 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 (isLetterChar(c) || isDigit(c)) + ++chars; + else if (isSpace(c) && with_blanks) + ++blanks; + } + } + } + + return chars + blanks; +} + + namespace { depth_type getDepth(DocIterator const & it) Index: src/lfuns.h =================================================================== --- src/lfuns.h (Revision 22429) +++ src/lfuns.h (Arbeitskopie) @@ -552,7 +552,7 @@ LFUN_INSET_REFRESH, LFUN_BUFFER_NEXT, LFUN_BUFFER_PREVIOUS, - LFUN_WORDS_COUNT, + LFUN_STATISTICS, // jspitzm renamed 20070828 // 260 LFUN_CHANGES_OUTPUT, // jspitzm 20050121 LFUN_BIBTEX_DATABASE_ADD,