implement Word counting (bug 728)

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@9402 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
Jean-Marc Lasgouttes 2004-12-27 16:30:27 +00:00
parent f1e9f57553
commit a839021d22
9 changed files with 81 additions and 1 deletions

View 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> 2004-12-17 Jürgen Spitzmüller <j.spitzmueller@gmx.de>
* Makefile.am: honor the textrm_*.xpm renaming. * Makefile.am: honor the textrm_*.xpm renaming.

View File

@ -402,6 +402,7 @@ Menuset
Menu "tools" Menu "tools"
Item "Spellchecker...|S" "dialog-show spellchecker" Item "Spellchecker...|S" "dialog-show spellchecker"
OptItem "Thesaurus...|T" "thesaurus-entry" OptItem "Thesaurus...|T" "thesaurus-entry"
Item "Count Words|W" "words-count"
OptItem "Check TeX|h" "buffer-chktex" OptItem "Check TeX|h" "buffer-chktex"
Item "TeX Information...|I" "dialog-show texinfo" Item "TeX Information...|I" "dialog-show texinfo"
Separator Separator

View File

@ -958,6 +958,7 @@ FuncStatus BufferView::Pimpl::getStatus(FuncRequest const & cmd)
case LFUN_MARK_ON: case LFUN_MARK_ON:
case LFUN_SETMARK: case LFUN_SETMARK:
case LFUN_CENTER: case LFUN_CENTER:
case LFUN_WORDS_COUNT:
flag.enabled(true); flag.enabled(true);
break; break;
@ -1130,6 +1131,35 @@ bool BufferView::Pimpl::dispatch(FuncRequest const & cmd)
bv_->center(); bv_->center();
break; 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: default:
return false; return false;
} }

View 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> 2004-12-19 Angus Leeming <leeming@lyx.org>
* buffer.C (save): s/slashify_path/internal_path/. * buffer.C (save): s/slashify_path/internal_path/.

View File

@ -339,6 +339,7 @@ void LyXAction::init()
{ LFUN_INSET_REFRESH, "", Noop }, { LFUN_INSET_REFRESH, "", Noop },
{ LFUN_NEXTBUFFER, "buffer-next", ReadOnly }, { LFUN_NEXTBUFFER, "buffer-next", ReadOnly },
{ LFUN_PREVIOUSBUFFER, "buffer-previous", ReadOnly }, { LFUN_PREVIOUSBUFFER, "buffer-previous", ReadOnly },
{ LFUN_WORDS_COUNT, "words-count", ReadOnly },
{ LFUN_NOACTION, "", Noop } { LFUN_NOACTION, "", Noop }
}; };

View File

@ -17,6 +17,7 @@
#include "buffer.h" #include "buffer.h"
#include "bufferlist.h" #include "bufferlist.h"
#include "bufferparams.h" #include "bufferparams.h"
#include "dociterator.h"
#include "errorlist.h" #include "errorlist.h"
#include "gettext.h" #include "gettext.h"
#include "LaTeX.h" #include "LaTeX.h"
@ -228,3 +229,26 @@ string const BufferFormat(Buffer const & buffer)
else else
return "latex"; 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;
}

View File

@ -16,8 +16,9 @@
class Buffer; class Buffer;
class TeXErrors; class DocIterator;
class ErrorList; class ErrorList;
class TeXErrors;
/** /**
* Loads a LyX file \c filename into \c Buffer * 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 &); 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 #endif // BUFFER_FUNCS_H

View File

@ -352,6 +352,8 @@ enum kb_action {
LFUN_INSET_REFRESH, LFUN_INSET_REFRESH,
LFUN_NEXTBUFFER, LFUN_NEXTBUFFER,
LFUN_PREVIOUSBUFFER, LFUN_PREVIOUSBUFFER,
// 270
LFUN_WORDS_COUNT,
LFUN_LASTACTION // end of the table LFUN_LASTACTION // end of the table
}; };

2
status
View File

@ -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 (xforms): replace the Combox code with a native xforms widget, for eventual
inclusion in the xforms library itself. inclusion in the xforms library itself.
Add a basic Word Count feature.