* count-words-region and count-words-buffer
@ 2006-09-26 14:00 Hrvoje Niksic
2006-09-26 15:34 ` Robert J. Chassell
2006-09-27 8:08 ` Hrvoje Niksic
0 siblings, 2 replies; 3+ messages in thread
From: Hrvoje Niksic @ 2006-09-26 14:00 UTC (permalink / raw)
The XEmacs functions `count-words-region' and `count-words-buffer'
have proven extremely useful over the years. This implementation is
mine, but the behavior should be the same.
(defun count-words-region (b e)
"Print the number of words in the region.
When called interactively, the word count is printed in echo area."
(interactive "r")
(let ((cnt 0))
(save-excursion
(save-restriction
(narrow-to-region b e)
(goto-char (point-min))
(while (forward-word 1)
(setq cnt (1+ cnt)))))
(if (interactive-p)
(message "Region has %d words" cnt))
cnt))
(defun count-words-buffer ()
"Print the number of words in the buffer.
When called interactively, the word count is printed in echo area."
(interactive)
(let ((cnt (count-words-region (point-min) (point-max))))
(if (interactive-p)
(message "Buffer has %d words" cnt))
cnt))
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: count-words-region and count-words-buffer
2006-09-26 14:00 count-words-region and count-words-buffer Hrvoje Niksic
@ 2006-09-26 15:34 ` Robert J. Chassell
2006-09-27 8:08 ` Hrvoje Niksic
1 sibling, 0 replies; 3+ messages in thread
From: Robert J. Chassell @ 2006-09-26 15:34 UTC (permalink / raw)
Hrvoje Niksic <hniksic@xemacs.org> wrote
The XEmacs functions `count-words-region' and `count-words-buffer'
have proven extremely useful over the years.
Yes, true. For whatever reason, perhaps because the function is easy
to write or perhaps because hackers do not count words but necessity,
a word count function does not appear as a default command. My
`Introduction to Emacs Lisp', which is part of the distribution, has
two versions of the `count-words-region' command, one using a while
loop and one recursive.
Here is a while loop version of `count-words-region' that I have kept
in my .emacs file for the past seventeen years:
;;; Word Count Command
;;; 26 August 1989
(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)
(re-search-forward "\\w+\\W*" 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))))))
(global-set-key "\C-c=" 'count-words-region)
--
Robert J. Chassell GnuPG Key ID: 004B4AC8
bob@rattlesnake.com bob@gnu.org
http://www.rattlesnake.com http://www.teak.cc
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: count-words-region and count-words-buffer
2006-09-26 14:00 count-words-region and count-words-buffer Hrvoje Niksic
2006-09-26 15:34 ` Robert J. Chassell
@ 2006-09-27 8:08 ` Hrvoje Niksic
1 sibling, 0 replies; 3+ messages in thread
From: Hrvoje Niksic @ 2006-09-27 8:08 UTC (permalink / raw)
Robert, please note that the functions in the elisp intro are written
for teaching purposes -- to demonstrate concepts such as recursions
and regexp search and how to debug them. They were meant to be used
by end-users.
That word-counting is easy to implement is true to an extent, but it's
not sufficient argument for omitting such a basic functionality from
Emacs. Also, while it may be easy to implement word-counting in some
form, it's harder to get it right, and harder yet to get it fast for
large buffers. For example, the elisp intro code you cited uses a
regexp search to count words, which is slower than forward-word. The
recursive version would also blow the specpdl-depth limit on large
buffers.
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2006-09-27 8:08 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-09-26 14:00 count-words-region and count-words-buffer Hrvoje Niksic
2006-09-26 15:34 ` Robert J. Chassell
2006-09-27 8:08 ` Hrvoje Niksic
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.