emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

count-words-region and count-words-buffer


From: Hrvoje Niksic
Subject: count-words-region and count-words-buffer
Date: Tue, 26 Sep 2006 16:00:05 +0200
User-agent: Gnus/5.1006 (Gnus v5.10.6) Emacs/21.4 (gnu/linux)

The XEmacs functions `count-words-region' and `count-words-buffer'
have proven extremely useful over the years.  This implementation is
mine, but the behavior should be the same.

(defun count-words-region (b e)
  "Print the number of words in the region.
When called interactively, the word count is printed in echo area."
  (interactive "r")
  (let ((cnt 0))
    (save-excursion
      (save-restriction
        (narrow-to-region b e)
        (goto-char (point-min))
        (while (forward-word 1)
          (setq cnt (1+ cnt)))))
    (if (interactive-p)
        (message "Region has %d words" cnt))
    cnt))

(defun count-words-buffer ()
  "Print the number of words in the buffer.
When called interactively, the word count is printed in echo area."
  (interactive)
  (let ((cnt (count-words-region (point-min) (point-max))))
    (if (interactive-p)
        (message "Buffer has %d words" cnt))
    cnt))




reply via email to

[Prev in Thread] Current Thread [Next in Thread]