me, thinking out loud…

A couple of counting functions

I had a strict character limit of 180 chars for something I was writing. I just wrote a simple function to count characters in a region or a buffer. Another function to count the words.

(defun count-chars ()
  "Count the number of chars in a buffer or region."
  (interactive)
  (let* ((beg (if (region-active-p) (region-beginning) (point-min)))
         (end (if (region-active-p) (region-end) (point-max))))
         (message (number-to-string (- end beg)))))
(defun count-words ()
  "Count the number of words in a buffer or region."
  (interactive)
  (let* ((beg (if (region-active-p) (region-beginning) (point-min)))
         (end (if (region-active-p) (region-end) (point-max)))
         (count 0))
    (save-excursion
      (goto-char beg)
      (while (< (point) end)
        (forward-word)
        (setq count (1+ count))))
    (message (int-to-string count))))

Comments on: "A couple of counting functions" (1)

  1. You might also be interested in my wc-mode for emacs at http://github.com/bnbeckwith/wc-mode.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Tag Cloud

%d bloggers like this: