[Top][All Lists]

[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


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

Also, to have, say, "two-story" counted as one word rather than two words,



;;; 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.
    (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)
              "The region does NOT have any words."))
            ((= 1 count)
              "The region has 1 word."))
              "The region has %d words." count))))))

reply via email to

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