diff options
Diffstat (limited to 'lisp/simple.el')
| -rw-r--r-- | lisp/simple.el | 50 |
1 files changed, 34 insertions, 16 deletions
diff --git a/lisp/simple.el b/lisp/simple.el index c81385680bf..af6d855d9c0 100644 --- a/lisp/simple.el +++ b/lisp/simple.el | |||
| @@ -945,28 +945,46 @@ rather than line counts." | |||
| 945 | (forward-line (1- line))))) | 945 | (forward-line (1- line))))) |
| 946 | 946 | ||
| 947 | (defun count-words-region (start end) | 947 | (defun count-words-region (start end) |
| 948 | "Count the number of words in the active region. | 948 | "Return the number of words between START and END. |
| 949 | If the region is not active, counts the number of words in the buffer." | 949 | If called interactively, print a message reporting the number of |
| 950 | (interactive (if (use-region-p) (list (region-beginning) (region-end)) | 950 | lines, words, and characters in the region." |
| 951 | (list (point-min) (point-max)))) | 951 | (interactive "r") |
| 952 | (let ((count 0)) | 952 | (let ((words 0)) |
| 953 | (save-excursion | 953 | (save-excursion |
| 954 | (save-restriction | 954 | (save-restriction |
| 955 | (narrow-to-region start end) | 955 | (narrow-to-region start end) |
| 956 | (goto-char (point-min)) | 956 | (goto-char (point-min)) |
| 957 | (while (forward-word 1) | 957 | (while (forward-word 1) |
| 958 | (setq count (1+ count))))) | 958 | (setq words (1+ words))))) |
| 959 | (when (called-interactively-p 'interactive) | 959 | (when (called-interactively-p 'interactive) |
| 960 | (message "%s has %d words" | 960 | (count-words--message "Region" |
| 961 | (if (use-region-p) "Region" "Buffer") | 961 | (count-lines start end) |
| 962 | count)) | 962 | words |
| 963 | count)) | 963 | (- end start))) |
| 964 | 964 | words)) | |
| 965 | (defun count-lines-region (start end) | 965 | |
| 966 | "Print number of lines and characters in the region." | 966 | (defun count-words () |
| 967 | (interactive "r") | 967 | "Display the number of lines, words, and characters in the buffer. |
| 968 | (message "Region has %d lines, %d characters" | 968 | In Transient Mark mode when the mark is active, display the |
| 969 | (count-lines start end) (- end start))) | 969 | number of lines, words, and characters in the region." |
| 970 | (interactive) | ||
| 971 | (if (use-region-p) | ||
| 972 | (call-interactively 'count-words-region) | ||
| 973 | (let* ((beg (point-min)) | ||
| 974 | (end (point-max)) | ||
| 975 | (lines (count-lines beg end)) | ||
| 976 | (words (count-words-region beg end)) | ||
| 977 | (chars (- end beg))) | ||
| 978 | (count-words--message "Buffer" lines words chars)))) | ||
| 979 | |||
| 980 | (defun count-words--message (str lines words chars) | ||
| 981 | (message "%s has %d line%s, %d word%s, and %d character%s." | ||
| 982 | str | ||
| 983 | lines (if (= lines 1) "" "s") | ||
| 984 | words (if (= words 1) "" "s") | ||
| 985 | chars (if (= chars 1) "" "s"))) | ||
| 986 | |||
| 987 | (defalias 'count-lines-region 'count-words-region) | ||
| 970 | 988 | ||
| 971 | (defun what-line () | 989 | (defun what-line () |
| 972 | "Print the current buffer line number and narrowed line number of point." | 990 | "Print the current buffer line number and narrowed line number of point." |