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
master
David Lawrence Ramsey 2005-07-25 03:47:08 +00:00
parent 7ea09e540f
commit 72936854c4
3 changed files with 28 additions and 9 deletions

View File

@ -117,6 +117,8 @@ CVS code -
- global.c: - global.c:
shortcut_init() shortcut_init()
- Simplify wording of nano_gotoline_msg. (Jordi and Ken Tyler) - 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: - move.c:
do_first_line(), do_last_line() do_first_line(), do_last_line()
- Simplify by only using edit_redraw(), and also make them call - Simplify by only using edit_redraw(), and also make them call
@ -204,6 +206,10 @@ CVS code -
- Add comments and minor cleanups. (DLR) - Add comments and minor cleanups. (DLR)
find_history(), get_history_completion() find_history(), get_history_completion()
- Make parameters const where possible. (DLR) - 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: - winio.c:
edit_redraw(), edit_refresh() edit_redraw(), edit_refresh()
- Clean up and simplify. (DLR) - Clean up and simplify. (DLR)

View File

@ -292,7 +292,7 @@ void shortcut_init(bool unjustify)
const char *nano_nextword_msg = N_("Move forward one word"); const char *nano_nextword_msg = N_("Move forward one word");
const char *nano_prevword_msg = N_("Move backward one word"); const char *nano_prevword_msg = N_("Move backward one word");
const char *nano_wordcount_msg = const char *nano_wordcount_msg =
N_("Count the number of words in the file"); N_("Count the number of words, lines, and characters");
#endif #endif
#ifndef DISABLE_JUSTIFY #ifndef DISABLE_JUSTIFY
const char *nano_parabegin_msg = const char *nano_parabegin_msg =

View File

@ -2049,7 +2049,8 @@ void do_spell(void)
#ifndef NANO_SMALL #ifndef NANO_SMALL
void do_word_count(void) 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; size_t pww_save = openfile->placewewant;
filestruct *current_save = openfile->current; filestruct *current_save = openfile->current;
bool old_mark_set = openfile->mark_set; bool old_mark_set = openfile->mark_set;
@ -2077,26 +2078,34 @@ void do_word_count(void)
openfile->placewewant = 0; openfile->placewewant = 0;
/* Keep moving to the next word (counting punctuation characters as /* Keep moving to the next word (counting punctuation characters as
* part of a word so that we match the output of "wc -w"), without * part of a word, as "wc -w" does), without updating the screen,
* updating the screen, until we reach the end of the file, * until we reach the end of the file, incrementing the total word
* incrementing the total word count whenever we're on a word just * count whenever we're on a word just before moving. */
* before moving. */
while (openfile->current != openfile->filebot || while (openfile->current != openfile->filebot ||
openfile->current_x != 0) { openfile->current_x != 0) {
if (do_next_word(TRUE, FALSE)) if (do_next_word(TRUE, FALSE))
words++; 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 (old_mark_set) {
/* If the mark was on and we added a magicline, remove it /* If the mark was on and we added a magicline, remove it
* now. */ * now. */
if (added_magicline) if (added_magicline)
remove_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 /* Unpartition the filestruct so that it contains all the text
* again, and turn the mark back on. */ * again, and turn the mark back on. */
unpartition_filestruct(&filepart); unpartition_filestruct(&filepart);
openfile->mark_set = TRUE; openfile->mark_set = TRUE;
} else {
lines = openfile->totlines;
chars = openfile->totsize;
} }
/* Restore where we were. */ /* Restore where we were. */
@ -2104,8 +2113,12 @@ void do_word_count(void)
openfile->current_x = current_x_save; openfile->current_x = current_x_save;
openfile->placewewant = pww_save; openfile->placewewant = pww_save;
/* Display the total word count on the statusbar. */ /* Display the total word, line, and character counts on the
statusbar("%s: %lu", old_mark_set ? _("Word Count in Selection") : * statusbar. */
_("Word Count"), (unsigned long)words); 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 */ #endif /* !NANO_SMALL */