* count-words-region
@ 2010-11-13 13:02 Stephen Berman
0 siblings, 0 replies; only message in thread
From: Stephen Berman @ 2010-11-13 13:02 UTC (permalink / raw)
To: emacs-devel
The newly installed count-words-region differs from the shell command wc
in certain cases, for example, in this sentence: "How many words does
count-words-region say this sentence has?" I think wc delimits words by
white space. This may sometimes be better than word syntax, but
sometimes worse (e.g. if a comment string like ";;" is counted). It
could be useful to allow a command for counting words to be flexible
with respect to what delimits words. Here's an implementation that does
this:
(defun count-words (&optional syntax)
"Count words in region if it exists, else in visible buffer.
What counts as a word depends on the syntax class, solicited by
prefix argument SYNTAX; the default is to use white space to
delimit words, like the shell command wc."
(interactive "P")
(let ((syn (if syntax (read-from-minibuffer "Syntax code: ") " "))
(beg (if (mark t) (region-beginning) (point-min)))
(end (if (mark t) (region-end) (point-max)))
(count 0))
(save-excursion
(goto-char beg)
;; don't count initial white space
(and (string= syn " ") (skip-syntax-forward syn))
(while (< (point) end)
(skip-syntax-forward (concat "^" syn))
(unless (>= (point) end) (skip-syntax-forward syn))
(setq count (1+ count))))
(message "%d words in %s"
count (if (> (buffer-size) (- end beg)) "region" "buffer"))))
Steve Berman
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2010-11-13 13:02 UTC | newest]
Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-11-13 13:02 count-words-region Stephen Berman
Code repositories for project(s) associated with this public inbox
https://git.savannah.gnu.org/cgit/emacs.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).