Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Word count in Emacs

15 views
Skip to first unread message

B. T. Raven

unread,
Sep 14, 2012, 1:55:44 PM9/14/12
to
Can anyone give me a clue why the following doesn't print a number in
the mini-buffer or to the *Messages* buffer?:

(defun count-words (start end) ;; alias wce
"Print number of words in the region."
(interactive "r")
(save-excursion
(save-restriction
(narrow-to-region start end)
(goto-char (point-min))
(count-matches "\\sw+"))))

an alias is assigned so that I can invoke with

M-x wce (word count emacs)

Thanks,

Ed

Jambunathan K

unread,
Sep 14, 2012, 2:14:38 PM9/14/12
to B. T. Raven, help-gn...@gnu.org

M-x count- TAB

or

M-= (does exactly what you are trying to do here, I suppose)
--

Alp Aker

unread,
Sep 14, 2012, 3:28:55 PM9/14/12
to B. T. Raven, help-gn...@gnu.org
> Can anyone give me a clue why the following doesn't print a number in
> the mini-buffer or to the *Messages* buffer?:
>
> (defun count-words (start end) ;; alias wce
> "Print number of words in the region."
> (interactive "r")
> (save-excursion
> (save-restriction
> (narrow-to-region start end)
> (goto-char (point-min))
> (count-matches "\\sw+"))))

As you've defined it, the function returns a number, but it's not the
case that the return value of an interactive command is automatically
echoed in the mini-buffer. Compare:

(defun count-words (start end) ;; alias wce
"Print number of words in the region."
(interactive "r")
(save-excursion
(save-restriction
(narrow-to-region start end)
(goto-char (point-min))
(message (number-to-string (count-matches "\\sw+"))))))

B. T. Raven

unread,
Sep 15, 2012, 7:11:11 PM9/15/12
to
Die Fri Sep 14 2012 14:28:55 GMT-0500 (Central Daylight Time) Alp Aker
<alptek...@gmail.com> scripsit:
Thanks, Alp. That did it.

Ed

B. T. Raven

unread,
Sep 15, 2012, 7:23:25 PM9/15/12
to

>
> M-x count- TAB

Using this I just find my own function. I think I looked at C-h f count
tab before defining this. Am I masking an original count-words function
in ver. 23.3?

>
> or
>
> M-= (does exactly what you are trying to do here, I suppose)

That is count-lines-regions, returning no. of lines and chars, not words
unless it's been enhanced in ver. 24

Thanks, anyway

Ed
0 new messages