From 11e679f73ecca718c61307973c3d67739354e1c7 Mon Sep 17 00:00:00 2001 From: brokenclock Date: Tue, 14 Oct 2014 21:55:04 +0200 Subject: [PATCH] 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. --- lib/doc/LFUNs.lyx | 51 +++++++++++++++++++++++++++++++++++++++++++++-- src/FuncCode.h | 1 + src/LyXAction.cpp | 20 ++++++++++++++++++- src/Text3.cpp | 28 ++++++++++++++++++++++++++ 4 files changed, 97 insertions(+), 3 deletions(-) diff --git a/lib/doc/LFUNs.lyx b/lib/doc/LFUNs.lyx index b0ac9f469f..0d9bb938dd 100644 --- a/lib/doc/LFUNs.lyx +++ b/lib/doc/LFUNs.lyx @@ -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 [] +\end_layout +\begin_layout Description +Params : 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 [] +\end_layout +\begin_layout Description +Params : 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. diff --git a/src/FuncCode.h b/src/FuncCode.h index 2ae820a171..3bd0cd0eef 100644 --- a/src/FuncCode.h +++ b/src/FuncCode.h @@ -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 }; diff --git a/src/LyXAction.cpp b/src/LyXAction.cpp index 175f68ee80..0efbb3c8e1 100644 --- a/src/LyXAction.cpp +++ b/src/LyXAction.cpp @@ -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 [] + * \li Params: : 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. diff --git a/src/Text3.cpp b/src/Text3.cpp index 79711f4373..6bf3bc601f 100644 --- a/src/Text3.cpp +++ b/src/Text3.cpp @@ -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(cur.buffer()->wordCount())); + } else if (arg0 == "chars") { + cur.message(convert(cur.buffer()->charCount(false))); + } else if (arg0 == "chars-space") { + cur.message(convert(cur.buffer()->charCount(true))); + } else { + cur.message(convert(cur.buffer()->wordCount()) + " " + + convert(cur.buffer()->charCount(false)) + " " + + convert(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;