diff options
| author | Chong Yidong | 2012-03-05 14:10:11 +0800 |
|---|---|---|
| committer | Chong Yidong | 2012-03-05 14:10:11 +0800 |
| commit | 10607bea34d2cddc62761df6c88d674c2861e71d (patch) | |
| tree | d810f16718f7caeccaf97ec2a98bc22776aba713 /lisp | |
| parent | 50ab02c5cc9c84ecb6459bb3b1a041154365f8ac (diff) | |
| download | emacs-10607bea34d2cddc62761df6c88d674c2861e71d.tar.gz emacs-10607bea34d2cddc62761df6c88d674c2861e71d.zip | |
Tweaks to count-words and count-words-region behavior.
In particular, make count-words more analogous to the existing
count-lines function.
* lisp/simple.el (count-words): If called from Lisp, return the word
count, for symmetry with `count-lines'. Arglist changed.
(count-words--message): Args changed. Consolidate counting code
from count-words and count-words-region.
(count-words-region): Caller changed.
(count-lines-region): Make it an obsolete alias.
Diffstat (limited to 'lisp')
| -rw-r--r-- | lisp/ChangeLog | 9 | ||||
| -rw-r--r-- | lisp/simple.el | 81 |
2 files changed, 52 insertions, 38 deletions
diff --git a/lisp/ChangeLog b/lisp/ChangeLog index 26d2c1746a2..92d12fbc1ff 100644 --- a/lisp/ChangeLog +++ b/lisp/ChangeLog | |||
| @@ -1,3 +1,12 @@ | |||
| 1 | 2012-03-05 Chong Yidong <cyd@gnu.org> | ||
| 2 | |||
| 3 | * simple.el (count-words): If called from Lisp, return the word | ||
| 4 | count, for symmetry with `count-lines'. Arglist changed. | ||
| 5 | (count-words--message): Args changed. Consolidate counting code | ||
| 6 | from count-words and count-words-region. | ||
| 7 | (count-words-region): Caller changed. | ||
| 8 | (count-lines-region): Make it an obsolete alias. | ||
| 9 | |||
| 1 | 2012-03-04 Tassilo Horn <tassilo@member.fsf.org> | 10 | 2012-03-04 Tassilo Horn <tassilo@member.fsf.org> |
| 2 | 11 | ||
| 3 | * saveplace.el (save-place-to-alist) | 12 | * saveplace.el (save-place-to-alist) |
diff --git a/lisp/simple.el b/lisp/simple.el index c968ac01b0d..2b4651ba697 100644 --- a/lisp/simple.el +++ b/lisp/simple.el | |||
| @@ -949,46 +949,51 @@ rather than line counts." | |||
| 949 | (forward-line (1- line))))) | 949 | (forward-line (1- line))))) |
| 950 | 950 | ||
| 951 | (defun count-words-region (start end) | 951 | (defun count-words-region (start end) |
| 952 | "Return the number of words between START and END. | 952 | "Count the number of words in the region. |
| 953 | If called interactively, print a message reporting the number of | 953 | If called interactively, print a message reporting the number of |
| 954 | lines, words, and characters in the region." | 954 | lines, words, and chars in the region. |
| 955 | If called from Lisp, return the number of words between positions | ||
| 956 | START and END." | ||
| 955 | (interactive "r") | 957 | (interactive "r") |
| 956 | (let ((words 0)) | 958 | (if (called-interactively-p 'any) |
| 957 | (save-excursion | 959 | (count-words--message "Region" start end) |
| 958 | (save-restriction | 960 | (count-words start end))) |
| 959 | (narrow-to-region start end) | 961 | |
| 960 | (goto-char (point-min)) | 962 | (defun count-words (start end) |
| 961 | (while (forward-word 1) | 963 | "Count words between START and END. |
| 962 | (setq words (1+ words))))) | 964 | If called interactively, START and END are normally the start and |
| 963 | (when (called-interactively-p 'interactive) | 965 | end of the buffer; but if the region is active, START and END are |
| 964 | (count-words--message "Region" | 966 | the start and end of the region. Print a message reporting the |
| 965 | (count-lines start end) | 967 | number of lines, words, and chars. |
| 966 | words | 968 | |
| 967 | (- end start))) | 969 | If called from Lisp, return the number of words between START and |
| 968 | words)) | 970 | END, without printing any message." |
| 969 | 971 | (interactive (list nil nil)) | |
| 970 | (defun count-words () | 972 | (cond ((not (called-interactively-p 'any)) |
| 971 | "Display the number of lines, words, and characters in the buffer. | 973 | (let ((words 0)) |
| 972 | In Transient Mark mode when the mark is active, display the | 974 | (save-excursion |
| 973 | number of lines, words, and characters in the region." | 975 | (save-restriction |
| 974 | (interactive) | 976 | (narrow-to-region start end) |
| 975 | (if (use-region-p) | 977 | (goto-char (point-min)) |
| 976 | (call-interactively 'count-words-region) | 978 | (while (forward-word 1) |
| 977 | (let* ((beg (point-min)) | 979 | (setq words (1+ words))))) |
| 978 | (end (point-max)) | 980 | words)) |
| 979 | (lines (count-lines beg end)) | 981 | ((use-region-p) |
| 980 | (words (count-words-region beg end)) | 982 | (call-interactively 'count-words-region)) |
| 981 | (chars (- end beg))) | 983 | (t |
| 982 | (count-words--message "Buffer" lines words chars)))) | 984 | (count-words--message "Buffer" (point-min) (point-max))))) |
| 983 | 985 | ||
| 984 | (defun count-words--message (str lines words chars) | 986 | (defun count-words--message (str start end) |
| 985 | (message "%s has %d line%s, %d word%s, and %d character%s." | 987 | (let ((lines (count-lines start end)) |
| 986 | str | 988 | (words (count-words start end)) |
| 987 | lines (if (= lines 1) "" "s") | 989 | (chars (- end start))) |
| 988 | words (if (= words 1) "" "s") | 990 | (message "%s has %d line%s, %d word%s, and %d character%s." |
| 989 | chars (if (= chars 1) "" "s"))) | 991 | str |
| 990 | 992 | lines (if (= lines 1) "" "s") | |
| 991 | (defalias 'count-lines-region 'count-words-region) | 993 | words (if (= words 1) "" "s") |
| 994 | chars (if (= chars 1) "" "s")))) | ||
| 995 | |||
| 996 | (define-obsolete-function-alias 'count-lines-region 'count-words-region "24.1") | ||
| 992 | 997 | ||
| 993 | (defun what-line () | 998 | (defun what-line () |
| 994 | "Print the current buffer line number and narrowed line number of point." | 999 | "Print the current buffer line number and narrowed line number of point." |