> > Is there any chance a character counter could be implemented into Lyx? It
> > would be of great help when writing university papers
>
> > such counter is quite simple. do we want it ?
>
> Absolutely! It was reported that this is a killer feature in some fields: 

this will be only informative counter. one reason is the different views
what is to be counted, the second one and much harder to implement is
the different number of actually typesetted chars by latex.
(maybe we can give such warning in the dialog?)

if nobody objects wrt the contrains above i will commit this later.
pavel
diff --git a/src/BufferView.cpp b/src/BufferView.cpp
index 19e16da..d765d7f 100644
--- a/src/BufferView.cpp
+++ b/src/BufferView.cpp
@@ -1143,24 +1143,26 @@ bool BufferView::dispatch(FuncRequest const & cmd)
                        from = doc_iterator_begin(buffer_.inset());
                        to = doc_iterator_end(buffer_.inset());
                }
-               int const count = countWords(from, to);
+               pair<int, int> const count = countWords(from, to);
                docstring message;
-               if (count != 1) {
+               if (count.first != 1) {
                        if (cur.selection())
-                               message = bformat(_("%1$d words in selection."),
-                                         count);
+                               message = bformat(_("%1$d words, %2$d 
characters in selection."),
+                                         count.first, count.second);
                                else
-                                       message = bformat(_("%1$d words in 
document."),
-                                                         count);
+                                       message = bformat(_("%1$d words, %2$d 
characters in document."),
+                                                         count.first, 
count.second);
                }
                else {
                        if (cur.selection())
-                               message = _("One word in selection.");
+                               message = bformat(_("One word, %1$d characters 
in selection."),
+                                         count.second);
                        else
-                               message = _("One word in document.");
+                               message = bformat(_("One word, %1$d characters 
in document."),
+                                         count.second);
                }
 
-               Alert::information(_("Count words"), message);
+               Alert::information(_("Count words and characters"), message);
        }
                break;
 
diff --git a/src/buffer_funcs.cpp b/src/buffer_funcs.cpp
index d38e3aa..26c5e87 100644
--- a/src/buffer_funcs.cpp
+++ b/src/buffer_funcs.cpp
@@ -160,9 +160,10 @@ Buffer * newUnnamedFile(string const & templatename, 
FileName const & path)
 }
 
 
-int countWords(DocIterator const & from, DocIterator const & to)
+std::pair<int, int> countWords(DocIterator const & from, DocIterator const & 
to)
 {
        int count = 0;
+       int chars = 0;
        bool inword = false;
        for (DocIterator dit = from ; dit != to ; dit.forwardPos()) {
                // Copied and adapted from isLetter() in ControlSpellChecker
@@ -170,6 +171,7 @@ int countWords(DocIterator const & from, DocIterator const 
& to)
                    && dit.pos() != dit.lastpos()
                    && dit.paragraph().isLetter(dit.pos())
                    && !dit.paragraph().isDeleted(dit.pos())) {
+                       ++chars;
                        if (!inword) {
                                ++count;
                                inword = true;
@@ -178,7 +180,7 @@ int countWords(DocIterator const & from, DocIterator const 
& to)
                        inword = false;
        }
 
-       return count;
+       return make_pair(count, chars);
 }
 
 
diff --git a/src/buffer_funcs.h b/src/buffer_funcs.h
index 061dc80..c1e3da9 100644
--- a/src/buffer_funcs.h
+++ b/src/buffer_funcs.h
@@ -41,8 +41,8 @@ Buffer * newFile(std::string const & filename, std::string 
const & templatename,
 Buffer * newUnnamedFile(std::string const & templatename,
        support::FileName const & path);
 
-/// Count the number of words in the text between these two iterators
-int countWords(DocIterator const & from, DocIterator const & to);
+/// Count the number of words and characters in the text between these two 
iterators
+std::pair<int, int> countWords(DocIterator const & from, DocIterator const & 
to);
 
 /// updates all counters
 void updateLabels(Buffer const &, bool childonly = false);
diff --git a/src/lfuns.h b/src/lfuns.h
index 698c607..7c716b8 100644
--- a/src/lfuns.h
+++ b/src/lfuns.h
@@ -552,6 +552,15 @@ enum kb_action {
        LFUN_INSET_REFRESH,
        LFUN_BUFFER_NEXT,
        LFUN_BUFFER_PREVIOUS,
+/**
+ * LFUN_WORDS_COUNT
+ * \li Action: Count the 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, Dec 27 2004
+ */
        LFUN_WORDS_COUNT,
        // 260
        LFUN_CHANGES_OUTPUT,             // jspitzm 20050121

Reply via email to