Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!wuarchive!rex!ukma!usenet.ins.cwru.edu!eagle!eagle.lerc.nasa.gov!mikef From: mikef@breanne.lerc.nasa.gov (Mike J. Fuller) Newsgroups: comp.emacs Subject: Re: count words region Message-ID: Date: 22 Mar 91 17:49:06 GMT References: Sender: news@eagle.lerc.nasa.gov Organization: NASA Lewis Research Center, Cleveland, OH Lines: 77 In-Reply-To: anthony@cs.UAlberta.CA's message of 14 Mar 91 17:07:50 GMT >>>>> On 13 Mar 91 21:56:38 GMT, pjnesser@mbunix.mitre.org (Nesser) said: > Has anyone written a count-words-region command? I'm sure I'm > not the only one who would find it very useful. Thanks, .. jj The first function returns the number of words in a region. The second redefines M-= to count the number of lines, words, and characters in a region instead of just the number of lines. The third makes a similar modification to C-x l. Counting the number of words in a large region can be slow so I don't suggest doing it on a region with more than 200,000 words unless you have a Sparcstation 2 or need to go get a cup of coffee. I didn't write these functions, but unfortunately I don't remember who did so I can't give the real author credit. I just saw them in some newsgroup one day, thought they were nice, and put them in my .emacs file. Actually, I did make a few minor changes, but it is always easier to modify someone else's code than to write it in the first place. (defun count-words (start end) "Return number of words between START and END." (let ((count 0)) (save-excursion (save-restriction (narrow-to-region start end) (goto-char (point-min)) (while (forward-word 1) (setq count (1+ count))))) count)) (define-key esc-map "=" 'count-region) (defun count-region (start end) "Count number of lines, words, and characters in region." (interactive "r") (let ((l (count-lines start end)) (w (count-words start end)) (c (- end start))) (message "Region has %d line%s, %d word%s, and %d character%s." l (if (= 1 l) "" "s") w (if (= 1 w) "" "s") c (if (= 1 c) "" "s")))) (define-key ctl-x-map "l" 'count-page) (defun count-page () "Count number of lines, words, and characters on current page, and how many are before or after point." (interactive) (save-excursion (let ((opoint (point)) start end total before after) (forward-page) (beginning-of-line) (or (looking-at page-delimiter) (end-of-line)) (setq end (point)) (backward-page) (setq start (point)) (setq ltotal (count-lines start end) lbefore (count-lines start opoint) lafter (count-lines opoint end) wtotal (count-words start end) wbefore (count-words start opoint) wafter (count-words opoint end) ctotal (- end start) cbefore (- opoint start) cafter (- end opoint)) (message "Page: %d line%s (%d+%d), %d word%s (%d+%d), and %d char%s (%d+%d)." ltotal (if (= 1 ltotal) "" "s") lbefore lafter wtotal (if (= 1 wtotal) "" "s") wbefore wafter ctotal (if (= 1 ctotal) "" "s") cbefore cafter)))) /-----------------------------------------------------------------------------\ | Mike J. Fuller | Internet: mikef@sarah.lerc.nasa.gov | "I hate | |----------------| mikef@zippysun.math.uakron.edu | quotations." | |/\/\/\/\/\/\/\/\| Bitnet: r3mjf1@akronvm | -- R.W. Emerson | \-----------------------------------------------------------------------------/