- (setq count (1+ count)))))
- (if (called-interactively-p 'interactive)
- (message "Region has %d words" count))
- count))
-
-(defun count-lines-region (start end)
- "Print number of lines and characters in the region."
- (interactive "r")
- (message "Region has %d lines, %d characters"
- (count-lines start end) (- end start)))
+ (setq words (1+ words)))))
+ (when (called-interactively-p 'interactive)
+ (count-words--message "Region"
+ (count-lines start end)
+ words
+ (- end start)))
+ words))
+
+(defun count-words ()
+ "Display the number of lines, words, and characters in the buffer.
+In Transient Mark mode when the mark is active, display the
+number of lines, words, and characters in the region."
+ (interactive)
+ (if (use-region-p)
+ (call-interactively 'count-words-region)
+ (let* ((beg (point-min))
+ (end (point-max))
+ (lines (count-lines beg end))
+ (words (count-words-region beg end))
+ (chars (- end beg)))
+ (count-words--message "Buffer" lines words chars))))
+
+(defun count-words--message (str lines words chars)
+ (message "%s has %d line%s, %d word%s, and %d character%s."
+ str
+ lines (if (= lines 1) "" "s")
+ words (if (= words 1) "" "s")
+ chars (if (= chars 1) "" "s")))
+
+(defalias 'count-lines-region 'count-words-region)