help-gnu-emacs
[Top][All Lists]
Advanced

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

Re: Counting words


From: Masashi Ito
Subject: Re: Counting words
Date: Sun, 30 Nov 2003 15:55:02 -0500
User-agent: Mutt/1.4i

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))))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;




reply via email to

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