From e010edd1f86a23646bc86e7ecfec8eb551ddb65b Mon Sep 17 00:00:00 2001
From: David Lawrence Ramsey <pooka109@gmail.com>
Date: Sun, 12 Jun 2005 22:31:03 +0000
Subject: [PATCH] implement word count via Meta-D at the main window

git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@2633 35c25a1d-7b9e-4130-9fde-d3aeb78583b8
---
 ChangeLog    |  4 ++++
 src/global.c |  8 +++++++-
 src/nano.c   | 55 ++++++++++++++++++++++++++++++++++++++++++++++++----
 src/nano.h   |  1 +
 src/proto.h  |  4 +++-
 5 files changed, 66 insertions(+), 6 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 1c4b96ea..f726e9c0 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -61,6 +61,10 @@ CVS code -
 	  is_blank_mbchar(), is_blank_wchar() (renamed niswblank()),
 	  is_cntrl_wchar(), control_rep(), control_mbrep(), etc.;
 	  removal of is_alnum_char() and is_alnum_wchar(). (DLR)
+	- Implement word count via Meta-D at the main window.  Note that
+	  this is disabled when NANO_SMALL is defined.  New functions
+	  do_word_count() and do_next_word_void(); changes to
+	  shortcut_init() and do_next_word(). (DLR)
 - chars.c:
   make_mbstring()
 	- Change erroneous ENABLE_EXTRA #ifdef to NANO_EXTRA to fix a
diff --git a/src/global.c b/src/global.c
index f432e37c..4512e10f 100644
--- a/src/global.c
+++ b/src/global.c
@@ -321,6 +321,8 @@ void shortcut_init(bool unjustify)
 #ifndef NANO_SMALL
     const char *nano_nextword_msg = N_("Move forward one word");
     const char *nano_prevword_msg = N_("Move backward one word");
+    const char *nano_wordcount_msg =
+	N_("Count the number of words in the file");
 #endif
 #ifndef DISABLE_JUSTIFY
     const char *nano_parabegin_msg =
@@ -564,11 +566,15 @@ void shortcut_init(bool unjustify)
 #ifndef NANO_SMALL
     sc_init_one(&main_list, NANO_NEXTWORD_KEY, N_("Next Word"),
 	IFHELP(nano_nextword_msg, NANO_NO_KEY), NANO_NO_KEY,
-	NANO_NO_KEY, VIEW, do_next_word);
+	NANO_NO_KEY, VIEW, do_next_word_void);
 
     sc_init_one(&main_list, NANO_NO_KEY, N_("Prev Word"),
 	IFHELP(nano_prevword_msg, NANO_PREVWORD_KEY), NANO_NO_KEY,
 	NANO_NO_KEY, VIEW, do_prev_word);
+
+    sc_init_one(&main_list, NANO_NO_KEY, N_("Word Count"),
+	IFHELP(nano_wordcount_msg, NANO_WORDCOUNT_KEY), NANO_NO_KEY,
+	NANO_NO_KEY, VIEW, do_word_count);
 #endif
 
 #ifndef DISABLE_JUSTIFY
diff --git a/src/nano.c b/src/nano.c
index 4df9dda9..535a2c15 100644
--- a/src/nano.c
+++ b/src/nano.c
@@ -1445,13 +1445,16 @@ void do_enter(void)
 }
 
 #ifndef NANO_SMALL
-/* Move to the next word. */
-void do_next_word(void)
+/* Move to the next word.  If allow_update is FALSE, don't update the
+ * screen afterward.  Return TRUE if we started on a word, and FALSE
+ * otherwise. */
+bool do_next_word(bool allow_update)
 {
     size_t pww_save = placewewant;
     const filestruct *current_save = current;
     char *char_mb;
     int char_mb_len;
+    bool started_on_word = FALSE;
 
     assert(current != NULL && current->data != NULL);
 
@@ -1467,6 +1470,10 @@ void do_next_word(void)
 	 * line. */
 	if (!is_alnum_mbchar(char_mb))
 	    break;
+	/* If we haven't found it, then we've started on a word, so set
+	 * started_on_word to TRUE. */
+	else
+	    started_on_word = TRUE;
 
 	current_x += char_mb_len;
     }
@@ -1505,8 +1512,17 @@ void do_next_word(void)
 
     placewewant = xplustabs();
 
-    /* Update the screen. */
-    edit_redraw(current_save, pww_save);
+    /* If allow_update is TRUE, update the screen. */
+    if (allow_update)
+	edit_redraw(current_save, pww_save);
+
+    /* Return whether we started on a word. */
+    return started_on_word;
+}
+
+void do_next_word_void(void)
+{
+    do_next_word(TRUE);
 }
 
 /* Move to the previous word. */
@@ -1615,6 +1631,37 @@ void do_prev_word(void)
     edit_redraw(current_save, pww_save);
 }
 
+void do_word_count(void)
+{
+    size_t words = 0;
+    size_t current_x_save = current_x, pww_save = placewewant;
+    filestruct *current_save = current;
+
+    /* Start at the beginning of the file. */
+    current = fileage;
+    current_x = 0;
+    placewewant = 0;
+
+    /* Keep moving to the next word until we reach the end of the file,
+     * incrementing the total word count whenever we're on a word just
+     * before moving. */
+    while (current != filebot || current_x != 0) {
+	if (do_next_word(FALSE))
+	    words++;
+    }
+
+    /* Go back to where we were before. */
+    current = current_save;
+    current_x = current_x_save;
+    placewewant = pww_save;
+
+    /* Update the screen. */
+    edit_refresh();
+
+    /* Display the total word count on the statusbar. */
+    statusbar(_("Word count: %lu"), (unsigned long)words);
+}
+
 void do_mark(void)
 {
     TOGGLE(MARK_ISSET);
diff --git a/src/nano.h b/src/nano.h
index 4bcbb8e6..0be8df4d 100644
--- a/src/nano.h
+++ b/src/nano.h
@@ -455,6 +455,7 @@ typedef struct syntaxtype {
 #define NANO_BRACKET_KEY	NANO_ALT_RBRACKET
 #define NANO_NEXTWORD_KEY	NANO_CONTROL_SPACE
 #define NANO_PREVWORD_KEY	NANO_ALT_SPACE
+#define NANO_WORDCOUNT_KEY	NANO_ALT_D
 #define NANO_CUTTILLEND_KEY	NANO_CONTROL_X
 #define NANO_CUTTILLEND_ALTKEY	NANO_ALT_T
 #define NANO_PARABEGIN_KEY	NANO_CONTROL_W
diff --git a/src/proto.h b/src/proto.h
index 6ceeab90..f180a8c1 100644
--- a/src/proto.h
+++ b/src/proto.h
@@ -401,8 +401,10 @@ void do_delete(void);
 void do_tab(void);
 void do_enter(void);
 #ifndef NANO_SMALL
-void do_next_word(void);
+bool do_next_word(bool allow_update);
+void do_next_word_void(void);
 void do_prev_word(void);
+void do_word_count(void);
 void do_mark(void);
 #endif
 #ifndef DISABLE_WRAPPING
-- 
GitLab