]> git.eshelyaron.com Git - emacs.git/commitdiff
* lisp/simple.el (count-words-region): Use buffer if there's no region.
authorReuben Thomas <rrt@sc3d.org>
Sat, 10 Sep 2011 03:02:06 +0000 (23:02 -0400)
committerStefan Monnier <monnier@iro.umontreal.ca>
Sat, 10 Sep 2011 03:02:06 +0000 (23:02 -0400)
lisp/ChangeLog
lisp/simple.el

index 7b0b4890724f69a77122c0a3feba2c53146ec807..89b43bab43d215e7d4973b1f0759773df17303c0 100644 (file)
@@ -1,3 +1,7 @@
+2011-09-10  Reuben Thomas  <rrt@sc3d.org>
+
+       * simple.el (count-words-region): Use buffer if there's no region.
+
 2011-09-09  Juri Linkov  <juri@jurta.org>
 
        * wdired.el (wdired-change-to-wdired-mode): Set buffer-local
index 5b639f774cb86950b41db28a34f27bde578ed924..74343496c72336f19c5cba40a2b56da9c42689ff 100644 (file)
@@ -938,9 +938,10 @@ rather than line counts."
       (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
@@ -948,8 +949,10 @@ When called interactively, the word count is printed in echo area."
         (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)
@@ -983,12 +986,12 @@ and the greater of them is not at the start of a line."
       (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)))