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
This commit is contained in:
Pavel Sanda 2008-01-08 15:18:00 +00:00
parent 8379c2b70f
commit 922aa7b1e8
7 changed files with 68 additions and 22 deletions

View File

@ -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

View File

@ -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

View File

@ -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;

View File

@ -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 },

View File

@ -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)

View File

@ -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);

View File

@ -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,