(forward-line (1- line)))))
(defun count-words-region (start end)
- "Print the number of words in the region.
-When called interactively, the word count is printed in echo area."
- (interactive "r")
+ "Count the number of words in the active region.
+If the region is not active, counts the number of words in the buffer."
+ (interactive (if (use-region-p) (list (region-beginning) (region-end))
+ (list (point-min) (point-max))))
(let ((count 0))
(save-excursion
(save-restriction
(goto-char (point-min))
(while (forward-word 1)
(setq count (1+ count)))))
- (if (called-interactively-p 'interactive)
- (message "Region has %d words" count))
+ (when (called-interactively-p 'interactive)
+ (message "%s has %d words"
+ (if (use-region-p) "Region" "Buffer")
+ count))
count))
(defun count-lines-region (start end)
(if (eq selective-display t)
(save-match-data
(let ((done 0))
- (while (re-search-forward "[\n\C-m]" nil t 40)
- (setq done (+ 40 done)))
- (while (re-search-forward "[\n\C-m]" nil t 1)
- (setq done (+ 1 done)))
- (goto-char (point-max))
- (if (and (/= start end)
+ (while (re-search-forward "[\n\C-m]" nil t 40)
+ (setq done (+ 40 done)))
+ (while (re-search-forward "[\n\C-m]" nil t 1)
+ (setq done (+ 1 done)))
+ (goto-char (point-max))
+ (if (and (/= start end)
(not (bolp)))
(1+ done)
done)))