From mboxrd@z Thu Jan 1 00:00:00 1970 Path: main.gmane.org!not-for-mail From: Masashi Ito Newsgroups: gmane.emacs.help Subject: Re: Counting words Date: Sun, 30 Nov 2003 15:55:02 -0500 Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Message-ID: <20031130205502.GE398@dyn-wireless-245-198.dyn.columbia.edu> References: NNTP-Posting-Host: deer.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: sea.gmane.org 1070225966 11937 80.91.224.253 (30 Nov 2003 20:59:26 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Sun, 30 Nov 2003 20:59:26 +0000 (UTC) Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Sun Nov 30 21:59:24 2003 Return-path: Original-Received: from monty-python.gnu.org ([199.232.76.173]) by deer.gmane.org with esmtp (Exim 3.35 #1 (Debian)) id 1AQYf5-0004h6-00 for ; Sun, 30 Nov 2003 21:59:24 +0100 Original-Received: from localhost ([127.0.0.1] helo=monty-python.gnu.org) by monty-python.gnu.org with esmtp (Exim 4.24) id 1AQZbW-0001ij-Co for geh-help-gnu-emacs@m.gmane.org; Sun, 30 Nov 2003 16:59:46 -0500 Original-Received: from list by monty-python.gnu.org with tmda-scanned (Exim 4.24) id 1AQZaz-0001iT-62 for help-gnu-emacs@gnu.org; Sun, 30 Nov 2003 16:59:13 -0500 Original-Received: from mail by monty-python.gnu.org with spam-scanned (Exim 4.24) id 1AQZaQ-0001Vb-TZ for help-gnu-emacs@gnu.org; Sun, 30 Nov 2003 16:59:10 -0500 Original-Received: from [160.39.245.198] (helo=dyn-wireless-245-198.dyn.columbia.edu) by monty-python.gnu.org with esmtp (Exim 4.24) id 1AQZaQ-0001Si-Jh for help-gnu-emacs@gnu.org; Sun, 30 Nov 2003 16:58:38 -0500 Original-Received: from dyn-wireless-245-198.dyn.columbia.edu (localhost [127.0.0.1]) by dyn-wireless-245-198.dyn.columbia.edu (8.12.7/8.12.2) with ESMTP id hAUKt3P6000492 for ; Sun, 30 Nov 2003 15:55:03 -0500 (EST) Original-Received: (from masashii@localhost) by dyn-wireless-245-198.dyn.columbia.edu (8.12.7/8.12.2/Submit) id hAUKt2rt000491 for help-gnu-emacs@gnu.org; Sun, 30 Nov 2003 15:55:02 -0500 (EST) Original-To: help-gnu-emacs@gnu.org Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.4i X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.2 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: main.gmane.org gmane.emacs.help:14765 X-Report-Spam: http://spam.gmane.org/gmane.emacs.help:14765 Hi, In case you would like to count words in the specified region rather than the whole buffer, the following lisp works. I am using it, putting it in my .emacs file, and binding the function to C-c c. I found the lisp definition at: http://olympus.het.brown.edu/cgi-bin/info2www?(emacs-lisp-intro)Counting+Words Also, to have, say, "two-story" counted as one word rather than two words, see: http://olympus.het.brown.edu/cgi-bin/info2www?(emacs-lisp-intro)Syntax Best, Masashi ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;; Final version: while' (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) ;;; original ;; (re-search-forward "\\w+\\W*" end t)) ;;; but, to count words joined by a hyphen (or hyphens) as one word (re-search-forward "\\(\\w\\|\\s_\\)+[^ \t\n]*[ \t\n]*" 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)))))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;