From: Jean-Marc Lasgouttes Date: Mon, 27 Dec 2004 16:30:27 +0000 (+0000) Subject: implement Word counting (bug 728) X-Git-Tag: 1.6.10~14719 X-Git-Url: https://git.lyx.org/gitweb/?a=commitdiff_plain;h=a839021d22d03e0e976ba27f05b19ab02b47f7c1;p=features.git implement Word counting (bug 728) git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@9402 a592a061-630c-0410-9148-cb99ea01b6c8 --- diff --git a/lib/ChangeLog b/lib/ChangeLog index 6882acb8dc..10dfcc80e4 100644 --- a/lib/ChangeLog +++ b/lib/ChangeLog @@ -1,3 +1,7 @@ +2004-12-22 Jean-Marc Lasgouttes + + * ui/stdmenus.ui: add entry for words-count + 2004-12-17 Jürgen Spitzmüller * Makefile.am: honor the textrm_*.xpm renaming. diff --git a/lib/ui/stdmenus.ui b/lib/ui/stdmenus.ui index ed5b41a0ed..8228f6a7bb 100644 --- a/lib/ui/stdmenus.ui +++ b/lib/ui/stdmenus.ui @@ -402,6 +402,7 @@ Menuset Menu "tools" Item "Spellchecker...|S" "dialog-show spellchecker" OptItem "Thesaurus...|T" "thesaurus-entry" + Item "Count Words|W" "words-count" OptItem "Check TeX|h" "buffer-chktex" Item "TeX Information...|I" "dialog-show texinfo" Separator diff --git a/src/BufferView_pimpl.C b/src/BufferView_pimpl.C index 35298307d7..08d3e03a6e 100644 --- a/src/BufferView_pimpl.C +++ b/src/BufferView_pimpl.C @@ -958,6 +958,7 @@ FuncStatus BufferView::Pimpl::getStatus(FuncRequest const & cmd) case LFUN_MARK_ON: case LFUN_SETMARK: case LFUN_CENTER: + case LFUN_WORDS_COUNT: flag.enabled(true); break; @@ -1130,6 +1131,35 @@ bool BufferView::Pimpl::dispatch(FuncRequest const & cmd) bv_->center(); break; + case LFUN_WORDS_COUNT: { + DocIterator from, to; + if (cur.selection()) { + from = cur.selectionBegin(); + to = cur.selectionEnd(); + } else { + from = doc_iterator_begin(bv_->buffer()->inset()); + to = doc_iterator_end(bv_->buffer()->inset()); + } + int const count = countWords(from, to); + string message; + if (count != 1) { + if (cur.selection()) + message = bformat(_("%1$s words in selection."), + tostr(count)); + else + message = bformat(_("%1$s words in document."), + tostr(count)); + } + else { + if (cur.selection()) + message = _("One word in selection."); + else + message = _("One word in document."); + } + + Alert::information(_("Count words"), message); + } + break; default: return false; } diff --git a/src/ChangeLog b/src/ChangeLog index 79a8c5dd8a..35c1287325 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,14 @@ +2004-12-22 Jean-Marc Lasgouttes + + * buffer_funcs.C (countWords): new function. Counts words between + two iterators. + + * BufferView_pimpl.C (getStatus, dispatch): handle + LFUN_WORDS_COUNT. + + * LyXAction.C (init): + * lfuns.h: add LFUN_WORDS_COUNT. + 2004-12-19 Angus Leeming * buffer.C (save): s/slashify_path/internal_path/. diff --git a/src/LyXAction.C b/src/LyXAction.C index e4b582b128..a5ea3dcfe6 100644 --- a/src/LyXAction.C +++ b/src/LyXAction.C @@ -339,6 +339,7 @@ void LyXAction::init() { LFUN_INSET_REFRESH, "", Noop }, { LFUN_NEXTBUFFER, "buffer-next", ReadOnly }, { LFUN_PREVIOUSBUFFER, "buffer-previous", ReadOnly }, + { LFUN_WORDS_COUNT, "words-count", ReadOnly }, { LFUN_NOACTION, "", Noop } }; diff --git a/src/buffer_funcs.C b/src/buffer_funcs.C index 2f41213184..5d28338ac9 100644 --- a/src/buffer_funcs.C +++ b/src/buffer_funcs.C @@ -17,6 +17,7 @@ #include "buffer.h" #include "bufferlist.h" #include "bufferparams.h" +#include "dociterator.h" #include "errorlist.h" #include "gettext.h" #include "LaTeX.h" @@ -228,3 +229,26 @@ string const BufferFormat(Buffer const & buffer) else return "latex"; } + + +int countWords(DocIterator const & from, DocIterator const & to) +{ + int count = 0; + bool inword = false; + for (DocIterator dit = from ; dit != to ; dit.forwardPos()) { + // Copied and adapted from isLetter() in ControlSpellChecker + if (dit.inTexted() + && dit.pos() != dit.lastpos() + && dit.paragraph().isLetter(dit.pos()) + && !isDeletedText(dit.paragraph(), dit.pos())) { + if (!inword) { + ++count; + inword = true; + } + } else if (inword) + inword = false; + } + + return count; +} + diff --git a/src/buffer_funcs.h b/src/buffer_funcs.h index 7d741eaf37..db433142ec 100644 --- a/src/buffer_funcs.h +++ b/src/buffer_funcs.h @@ -16,8 +16,9 @@ class Buffer; -class TeXErrors; +class DocIterator; class ErrorList; +class TeXErrors; /** * Loads a LyX file \c filename into \c Buffer @@ -38,4 +39,8 @@ void bufferErrors(Buffer const &, TeXErrors const &); /// void bufferErrors(Buffer const &, ErrorList const &); +/// Count the number of words in the text between these two iterators +int countWords(DocIterator const & from, DocIterator const & to); + + #endif // BUFFER_FUNCS_H diff --git a/src/lfuns.h b/src/lfuns.h index d614278d5f..6b0341f122 100644 --- a/src/lfuns.h +++ b/src/lfuns.h @@ -352,6 +352,8 @@ enum kb_action { LFUN_INSET_REFRESH, LFUN_NEXTBUFFER, LFUN_PREVIOUSBUFFER, + // 270 + LFUN_WORDS_COUNT, LFUN_LASTACTION // end of the table }; diff --git a/status b/status index 4bf2e30683..a84652ab56 100644 --- a/status +++ b/status @@ -79,3 +79,5 @@ Move as much code as possible from the Menu frontends to the Backend. (xforms): replace the Combox code with a native xforms widget, for eventual inclusion in the xforms library itself. + +Add a basic Word Count feature.