Function: count-words
count-words is an interactive and byte-compiled function defined in
simple.el.gz.
Signature
(count-words START END &optional TOTALS)
Documentation
Count words between START and END.
If called interactively, START and END are normally the start and end of the buffer; but if the region is active, START and END are the start and end of the region. Print a message reporting the number of lines, sentences, words, and chars. With prefix argument, also include the data for the entire (un-narrowed) buffer.
If called from Lisp, return the number of words between START and END, without printing any message. TOTALS is ignored when called from Lisp.
Probably introduced at or before Emacs version 24.1.
Key Bindings
Source Code
;; Defined in /usr/src/emacs/lisp/simple.el.gz
(defun count-words (start end &optional totals)
"Count words between START and END.
If called interactively, START and END are normally the start and
end of the buffer; but if the region is active, START and END are
the start and end of the region. Print a message reporting the
number of lines, sentences, words, and chars. With prefix
argument, also include the data for the entire (un-narrowed)
buffer.
If called from Lisp, return the number of words between START and
END, without printing any message. TOTALS is ignored when called
from Lisp."
(interactive (list nil nil current-prefix-arg))
;; When called from Lisp, return the data.
(if (not (called-interactively-p 'any))
(let ((words 0)
;; Count across field boundaries. (Bug#41761)
(inhibit-field-text-motion t))
(save-excursion
(save-restriction
(narrow-to-region start end)
(goto-char (point-min))
(while (forward-word-strictly 1)
(setq words (1+ words)))))
words)
;; When called interactively, message the data.
(let ((totals (if (and totals
(or (use-region-p)
(buffer-narrowed-p)))
(save-restriction
(widen)
(count-words--format "; buffer in total"
(point-min) (point-max)))
"")))
(if (use-region-p)
(message "%s%s" (count-words--format
"Region" (region-beginning) (region-end))
totals)
(message "%s%s" (count-words--buffer-format) totals)))))