Bonsoir,

Le 14/10/2014 16:23, Jean-Marc Lasgouttes a écrit :
Moyennant ces petits changements, je pense que ce patch est prêt.
Voilà, j'ai corrigé tout ça. J'espère que la documentation dans LyXAction.cpp te conviendra, c'est le seul point sur lequel j'avais encore un doute....

Cordialement,

Stéphane Mourey

--
Blog: Impossible Exil <http://impossible-exil.info>
>From 17b6ebf2820746bad71a3246d90be5b33a0fd527 Mon Sep 17 00:00:00 2001
From: brokenclock <[email protected]>
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, characters or characters+spaces count in the current document or selection.
---
 src/FuncCode.h    |  1 +
 src/LyXAction.cpp | 17 +++++++++++++++++
 src/Text3.cpp     | 28 ++++++++++++++++++++++++++++
 3 files changed, 46 insertions(+)

diff --git a/src/FuncCode.h b/src/FuncCode.h
index 2ae820a..3bd0cd0 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 175f68e..2df6ec9 100644
--- a/src/LyXAction.cpp
+++ b/src/LyXAction.cpp
@@ -3526,6 +3526,23 @@ void LyXAction::init()
  */
 		{ LFUN_STATISTICS, "statistics", ReadOnly, System },
 /*!
+ * \var lyx::FuncCode lyx::LFUN_SERVER_GET_STATISTICS
+ * \li Action: Count the statistics (number of words and characters)
+               in the document or in the selection if any.
+ * \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 needed count. If not
+                       specified, the words, chars and chars-space counts.
+                       space separated.
+                       words: count words
+                       chars: count characters
+                       chars-space: count characters and spaces
+ * \li Origin: brokenclock, Oct 10 2014
+ * \endvar
+ */
+		{ LFUN_SERVER_GET_STATISTICS, "server-get-statistics", ReadOnly | Argument, System },
+/*!
  * \var lyx::FuncCode lyx::LFUN_COMPLETION_INLINE
  * \li Action: Show the inline completion at the cursor position.
  * \li Syntax: completion-inline
diff --git a/src/Text3.cpp b/src/Text3.cpp
index 79711f4..6bf3bc6 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<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;
-- 
1.9.1

Répondre à