]> git.lyx.org Git - features.git/commitdiff
implement Word counting (bug 728)
authorJean-Marc Lasgouttes <lasgouttes@lyx.org>
Mon, 27 Dec 2004 16:30:27 +0000 (16:30 +0000)
committerJean-Marc Lasgouttes <lasgouttes@lyx.org>
Mon, 27 Dec 2004 16:30:27 +0000 (16:30 +0000)
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@9402 a592a061-630c-0410-9148-cb99ea01b6c8

lib/ChangeLog
lib/ui/stdmenus.ui
src/BufferView_pimpl.C
src/ChangeLog
src/LyXAction.C
src/buffer_funcs.C
src/buffer_funcs.h
src/lfuns.h
status

index 6882acb8dc12e9034e36dc7f6ffceac715b00fb9..10dfcc80e4a9ff89443e892a7d5f485428b9bb40 100644 (file)
@@ -1,3 +1,7 @@
+2004-12-22  Jean-Marc Lasgouttes  <lasgouttes@lyx.org>
+
+       * ui/stdmenus.ui: add entry for words-count
+
 2004-12-17  Jürgen Spitzmüller  <j.spitzmueller@gmx.de>
 
        * Makefile.am: honor the textrm_*.xpm renaming.
index ed5b41a0edac5d9c03a35a27d33bc95a6b357e29..8228f6a7bb41c72ce3d017a740bea6f04d8a6cd7 100644 (file)
@@ -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
index 35298307d71f5f6032730590ba305bb250643e3a..08d3e03a6e31667d56a3c0354bfb4d6359ed940d 100644 (file)
@@ -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;
        }
index 79a8c5dd8ae6e14b3d21c79c4333794ef896b77a..35c128732556284bb6770618f04afa8c492e01e2 100644 (file)
@@ -1,3 +1,14 @@
+2004-12-22  Jean-Marc Lasgouttes  <lasgouttes@lyx.org>
+
+       * 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  <leeming@lyx.org>
 
        * buffer.C (save): s/slashify_path/internal_path/.
index e4b582b128c13d89cd6efc7bd77ce707cc7c1e6f..a5ea3dcfe697d264d33ea6a54ee99a214cfeab8b 100644 (file)
@@ -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 }
        };
 
index 2f41213184713f006c812cc18a806e95ec0ffe36..5d28338ac9a81ba7a1a02630c6a3bdd25c3efc92 100644 (file)
@@ -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;
+}
+
index 7d741eaf3721fed416ce76c85fe49a4de8281657..db433142ece6cbefb54f2fac51d81f052ef897f4 100644 (file)
@@ -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
index d614278d5f553963fdeaf52f622cef75462abb75..6b0341f122d3101a7ac4ea0c4fd80a8aa1b08ad1 100644 (file)
@@ -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 4bf2e30683215f849c2d53f5fb659286f264161b..a84652ab56b63c894edcd8b71e54d34bd2e11180 100644 (file)
--- 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.