From 72936854c452386ebfb1b2d9a6f5ae9561446662 Mon Sep 17 00:00:00 2001
From: David Lawrence Ramsey <pooka109@gmail.com>
Date: Mon, 25 Jul 2005 03:47:08 +0000
Subject: [PATCH] expand do_word_count() to also count the number of lines and
 characters in the file or selection, as wc does

git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@2924 35c25a1d-7b9e-4130-9fde-d3aeb78583b8
---
 ChangeLog    |  6 ++++++
 src/global.c |  2 +-
 src/text.c   | 29 +++++++++++++++++++++--------
 3 files changed, 28 insertions(+), 9 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 89042846..ae412ec1 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -117,6 +117,8 @@ CVS code -
 - global.c:
   shortcut_init()
 	- Simplify wording of nano_gotoline_msg. (Jordi and Ken Tyler)
+	- Clarify wording of nano_wordcount_msg, as it won't go through
+	  the entire file if the mark is on. (DLR)
 - move.c:
   do_first_line(), do_last_line()
 	- Simplify by only using edit_redraw(), and also make them call
@@ -204,6 +206,10 @@ CVS code -
 	- Add comments and minor cleanups. (DLR)
   find_history(), get_history_completion()
 	- Make parameters const where possible. (DLR)
+- text.c:
+  do_word_count()
+	- Expand to also count the number of lines and characters in the
+	  file or selection, as wc does. (DLR)
 - winio.c:
   edit_redraw(), edit_refresh()
 	- Clean up and simplify. (DLR)
diff --git a/src/global.c b/src/global.c
index 1e7a1ec6..8917b063 100644
--- a/src/global.c
+++ b/src/global.c
@@ -292,7 +292,7 @@ void shortcut_init(bool unjustify)
     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");
+	N_("Count the number of words, lines, and characters");
 #endif
 #ifndef DISABLE_JUSTIFY
     const char *nano_parabegin_msg =
diff --git a/src/text.c b/src/text.c
index 460225c7..b7fec6f9 100644
--- a/src/text.c
+++ b/src/text.c
@@ -2049,7 +2049,8 @@ void do_spell(void)
 #ifndef NANO_SMALL
 void do_word_count(void)
 {
-    size_t words = 0, current_x_save = openfile->current_x;
+    size_t words = 0, lines = 0, chars = 0;
+    size_t current_x_save = openfile->current_x;
     size_t pww_save = openfile->placewewant;
     filestruct *current_save = openfile->current;
     bool old_mark_set = openfile->mark_set;
@@ -2077,26 +2078,34 @@ void do_word_count(void)
     openfile->placewewant = 0;
 
     /* Keep moving to the next word (counting punctuation characters as
-     * part of a word so that we match the output of "wc -w"), without
-     * updating the screen, until we reach the end of the file,
-     * incrementing the total word count whenever we're on a word just
-     * before moving. */
+     * part of a word, as "wc -w" does), without updating the screen,
+     * until we reach the end of the file, incrementing the total word
+     * count whenever we're on a word just before moving. */
     while (openfile->current != openfile->filebot ||
 	openfile->current_x != 0) {
 	if (do_next_word(TRUE, FALSE))
 	    words++;
     }
 
+    /* Get the total line and character counts, as "wc -l"  and "wc -c"
+     * do, but get the latter in multibyte characters. */
     if (old_mark_set) {
 	/* If the mark was on and we added a magicline, remove it
 	 * now. */
 	if (added_magicline)
 	    remove_magicline();
 
+	lines = openfile->filebot->lineno - openfile->fileage->lineno +
+		1;
+	chars = get_totsize(openfile->fileage, openfile->filebot);
+
 	/* Unpartition the filestruct so that it contains all the text
 	 * again, and turn the mark back on. */
 	unpartition_filestruct(&filepart);
 	openfile->mark_set = TRUE;
+    } else {
+	lines = openfile->totlines;
+	chars = openfile->totsize;
     }
 
     /* Restore where we were. */
@@ -2104,8 +2113,12 @@ void do_word_count(void)
     openfile->current_x = current_x_save;
     openfile->placewewant = pww_save;
 
-    /* Display the total word count on the statusbar. */
-    statusbar("%s: %lu", old_mark_set ? _("Word Count in Selection") :
-	_("Word Count"), (unsigned long)words);
+    /* Display the total word, line, and character counts on the
+     * statusbar. */
+    statusbar("%s: %lu %s, %lu %s, %lu %s", old_mark_set ?
+	_("In selection") : _("In file"), (unsigned long)words,
+	P_("word", "words", (unsigned long)words), (unsigned long)lines,
+	P_("line", "lines", (unsigned long)lines), (unsigned long)chars,
+	P_("char", "chars", (unsigned long)chars));
 }
 #endif /* !NANO_SMALL */
-- 
GitLab