Add LFUN_SERVER_GET_STATISTICS command

This function can be used with the LyX server to obtain the word, character or character+space count in the current document or selection.
This commit is contained in:
brokenclock 2014-10-14 21:55:04 +02:00 committed by Jean-Marc Lasgouttes
parent 3706d90037
commit 11e679f73e
4 changed files with 97 additions and 3 deletions

View File

@ -98,7 +98,7 @@ The LyX Team
\end_layout
\begin_layout Date
2014-02-13
2014-10-23
\end_layout
\begin_layout Section*
@ -3075,6 +3075,22 @@ Syntax selection-paste
Origin lasgouttes, 14 Jan 2009
\end_layout
\begin_layout Subsection*
separator-insert
\end_layout
\begin_layout Description
Action Inserts an environment separator or paragraph break.
\end_layout
\begin_layout Description
Syntax separator-insert [<ARG>]
\end_layout
\begin_layout Description
Params <ARG>: <plain|parbreak> default: plain
\end_layout
\begin_layout Description
Origin ef, 2 May 2014
\end_layout
\begin_layout Subsection*
set-graphics-group
\end_layout
@ -5320,6 +5336,37 @@ Action Returns the current layout (that is environment) name on the cursor posit
Syntax server-get-layout
\end_layout
\begin_layout Subsection*
server-get-statistics
\end_layout
\begin_layout Description
Action Returns the statistics (number of words and characters) in the document or in the given selection.
\end_layout
\begin_layout Description
Notion Note that this function gives the number of words/chars written, not the number of characters which will be typeset.
\end_layout
\begin_layout Description
Syntax server-get-statistics [<TYPE>]
\end_layout
\begin_layout Description
Params <TYPE>: <words|chars|chars-space> The requested count; if not specified, the three values are returned, separated by a space.
\begin_inset Newline newline
\end_inset
words: count words.
\begin_inset Newline newline
\end_inset
chars: count characters.
\begin_inset Newline newline
\end_inset
chars-space: count characters and spaces.
\end_layout
\begin_layout Description
Origin brokenclock, Oct 10 2014
\end_layout
\begin_layout Subsection*
server-get-xy
\end_layout
@ -5411,7 +5458,7 @@ Origin SLior, 11 Jun 2000
statistics
\end_layout
\begin_layout Description
Action Count the statistics (number of words and characters) in the document or in the given selection.
Action Count the statistics (number of words and characters) in the document or in the given selection and display it in a dialog box.
\end_layout
\begin_layout Description
Notion Note that this function gives the number of words/chars written, not the number of characters which will be typeset.

View File

@ -458,6 +458,7 @@ enum FuncCode
// 355
LFUN_SPELLING_CONTINUOUSLY, // vfr, 20130324
LFUN_SEPARATOR_INSERT, // ef 20140502
LFUN_SERVER_GET_STATISTICS, // brokenclock 20141010
LFUN_LASTACTION // end of the table
};

View File

@ -3517,7 +3517,8 @@ void LyXAction::init()
/*!
* \var lyx::FuncCode lyx::LFUN_STATISTICS
* \li Action: Count the statistics (number of words and characters)
in the document or in the given selection.
in the document or in the given selection and display it
in a dialog box.
* \li Notion: Note that this function gives the number of words/chars written,
not the number of characters which will be typeset.
* \li Syntax: statistics
@ -3525,6 +3526,23 @@ void LyXAction::init()
* \endvar
*/
{ LFUN_STATISTICS, "statistics", ReadOnly, System },
/*!
* \var lyx::FuncCode lyx::LFUN_SERVER_GET_STATISTICS
* \li Action: Returns 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 typeset.
* \li Syntax: server-get-statistics [<TYPE>]
* \li Params: <TYPE>: <words|chars|chars-space> The requested count; if not
specified, the three values are returned, separated
by a space.\n
words: count words.\n
chars: count characters.\n
chars-space: count characters and spaces.
* \li Origin: brokenclock, Oct 10 2014
* \endvar
*/
{ LFUN_SERVER_GET_STATISTICS, "server-get-statistics", ReadOnly, System },
/*!
* \var lyx::FuncCode lyx::LFUN_COMPLETION_INLINE
* \li Action: Show the inline completion at the cursor position.

View File

@ -2391,6 +2391,33 @@ void Text::dispatch(Cursor & cur, FuncRequest & cmd)
needsUpdate = true;
break;
case LFUN_SERVER_GET_STATISTICS:
{
DocIterator from, to;
if (cur.selection()) {
from = cur.selectionBegin();
to = cur.selectionEnd();
} else {
from = doc_iterator_begin(cur.buffer());
to = doc_iterator_end(cur.buffer());
}
cur.buffer()->updateStatistics(from, to);
string const arg0 = cmd.getArg(0);
if (arg0 == "words") {
cur.message(convert<docstring>(cur.buffer()->wordCount()));
} else if (arg0 == "chars") {
cur.message(convert<docstring>(cur.buffer()->charCount(false)));
} else if (arg0 == "chars-space") {
cur.message(convert<docstring>(cur.buffer()->charCount(true)));
} else {
cur.message(convert<docstring>(cur.buffer()->wordCount()) + " "
+ convert<docstring>(cur.buffer()->charCount(false)) + " "
+ convert<docstring>(cur.buffer()->charCount(true)));
}
}
break;
default:
LYXERR(Debug::ACTION, "Command " << cmd << " not DISPATCHED by Text");
cur.undispatched();
@ -3109,6 +3136,7 @@ bool Text::getStatus(Cursor & cur, FuncRequest const & cmd,
case LFUN_UNICODE_INSERT:
case LFUN_THESAURUS_ENTRY:
case LFUN_ESCAPE:
case LFUN_SERVER_GET_STATISTICS:
// these are handled in our dispatch()
enable = true;
break;