Tweaks to count-words and count-words-region behavior.
authorChong Yidong <cyd@gnu.org>
Mon, 5 Mar 2012 06:10:11 +0000 (14:10 +0800)
committerChong Yidong <cyd@gnu.org>
Mon, 5 Mar 2012 06:10:11 +0000 (14:10 +0800)
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.

etc/NEWS
lisp/ChangeLog
lisp/simple.el

index b611d72..fd4f7af 100644 (file)
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -483,9 +483,12 @@ isearch-yank-kill.
 
 +++
 ** New commands `count-words-region' and `count-words'.
-
-*** `count-lines-region' is now an alias for `count-words-region',
-bound to M-=, which shows the number of lines, words, and characters.
++++
+*** M-= is bound to `count-words-region', not `count-lines-region'.
+The `count-words-region' command, when called interactively, reports
+the number of lines, words, and characters in the region.  It is a
+superset of the old `count-lines-region', which is now an obsolete
+alias for it.
 
 +++
 ** The default value of `backup-by-copying-when-mismatch' is now t.
index 26d2c17..92d12fb 100644 (file)
@@ -1,3 +1,12 @@
+2012-03-05  Chong Yidong  <cyd@gnu.org>
+
+       * 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.
+
 2012-03-04  Tassilo Horn  <tassilo@member.fsf.org>
 
        * saveplace.el (save-place-to-alist)
index c968ac0..2b4651b 100644 (file)
@@ -949,46 +949,51 @@ rather than line counts."
       (forward-line (1- line)))))
 
 (defun count-words-region (start end)
-  "Return the number of words between START and END.
+  "Count the number of words in the region.
 If called interactively, print a message reporting the number of
-lines, words, and characters in the region."
+lines, words, and chars in the region.
+If called from Lisp, return the number of words between positions
+START and END."
   (interactive "r")
-  (let ((words 0))
-    (save-excursion
-      (save-restriction
-        (narrow-to-region start end)
-        (goto-char (point-min))
-        (while (forward-word 1)
-          (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)
+  (if (called-interactively-p 'any)
+      (count-words--message "Region" start end)
+    (count-words start end)))
+
+(defun count-words (start end)
+  "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, words, and chars.
+
+If called from Lisp, return the number of words between START and
+END, without printing any message."
+  (interactive (list nil nil))
+  (cond ((not (called-interactively-p 'any))
+        (let ((words 0))
+          (save-excursion
+            (save-restriction
+              (narrow-to-region start end)
+              (goto-char (point-min))
+              (while (forward-word 1)
+                (setq words (1+ words)))))
+          words))
+       ((use-region-p)
+        (call-interactively 'count-words-region))
+       (t
+        (count-words--message "Buffer" (point-min) (point-max)))))
+
+(defun count-words--message (str start end)
+  (let ((lines (count-lines start end))
+       (words (count-words start end))
+       (chars (- end start)))
+    (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"))))
+
+(define-obsolete-function-alias 'count-lines-region 'count-words-region "24.1")
 
 (defun what-line ()
   "Print the current buffer line number and narrowed line number of point."