all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Masashi Ito <mi61@columbia.edu>
Subject: Re: Counting words
Date: Sun, 30 Nov 2003 15:55:02 -0500	[thread overview]
Message-ID: <20031130205502.GE398@dyn-wireless-245-198.dyn.columbia.edu> (raw)
In-Reply-To: <E1AQYTg-0008GI-SU@monty-python.gnu.org>

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	other threads:[~2003-11-30 20:55 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <E1AQYTg-0008GI-SU@monty-python.gnu.org>
2003-11-30 20:55 ` Masashi Ito [this message]
     [not found] <mailman.1903.1176202249.7795.help-gnu-emacs@gnu.org>
2007-04-10 11:05 ` Counting words Robert D. Crawford
2007-04-10 11:46 ` "Wilfred Zegwaard (privé)"
2007-04-10 15:13   ` Peter Dyballa
2007-04-10 17:37   ` "Wilfred Zegwaard (privé)"
     [not found] ` <mailman.1904.1176205848.7795.help-gnu-emacs@gnu.org>
2007-04-11 19:42   ` Colin S. Miller
2007-04-12 13:14     ` thorne
2007-04-10 10:46 "Wilfred Zegwaard (privé)"
  -- strict thread matches above, loose matches on Subject: below --
2003-11-30 18:54 David Sumbler
2003-11-30 19:03 ` Jesper Harder
2003-11-30 20:03   ` David Sumbler
2003-11-30 23:51     ` Matthias Mees
2003-12-01  1:55   ` Roodwriter

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20031130205502.GE398@dyn-wireless-245-198.dyn.columbia.edu \
    --to=mi61@columbia.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.