[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: count-words-region and count-words-buffer
From: |
Robert J. Chassell |
Subject: |
Re: count-words-region and count-words-buffer |
Date: |
Tue, 26 Sep 2006 15:34:01 +0000 (UTC) |
Hrvoje Niksic <address@hidden> wrote
The XEmacs functions `count-words-region' and `count-words-buffer'
have proven extremely useful over the years.
Yes, true. For whatever reason, perhaps because the function is easy
to write or perhaps because hackers do not count words but necessity,
a word count function does not appear as a default command. My
`Introduction to Emacs Lisp', which is part of the distribution, has
two versions of the `count-words-region' command, one using a while
loop and one recursive.
Here is a while loop version of `count-words-region' that I have kept
in my .emacs file for the past seventeen years:
;;; Word Count Command
;;; 26 August 1989
(defun count-words-region (beginning end)
"Print number of words in the region."
(interactive "r")
(message "Counting words in region ... ")
;;; 1. Set up appropriate conditions.
(save-excursion
(let ((count 0))
(goto-char beginning)
;;; 2. Run the while loop.
(while (and (< (point) end)
(re-search-forward "\\w+\\W*" end t))
(setq count (1+ count)))
;;; 3. Send a message to the user.
(cond ((zerop count)
(message "The region does NOT have any words."))
((= 1 count) (message "The region has 1 word."))
(t (message "The region has %d words." count))))))
(global-set-key "\C-c=" 'count-words-region)
--
Robert J. Chassell GnuPG Key ID: 004B4AC8
address@hidden address@hidden
http://www.rattlesnake.com http://www.teak.cc