From: Chong Yidong Date: Mon, 5 Mar 2012 06:10:11 +0000 (+0800) Subject: Tweaks to count-words and count-words-region behavior. X-Git-Tag: emacs-pretest-24.0.05~165 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=10607bea34d2cddc62761df6c88d674c2861e71d;p=emacs.git 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. --- diff --git a/etc/NEWS b/etc/NEWS index b611d726eea..fd4f7afa863 100644 --- 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. diff --git a/lisp/ChangeLog b/lisp/ChangeLog index 26d2c1746a2..92d12fbc1ff 100644 --- a/lisp/ChangeLog +++ b/lisp/ChangeLog @@ -1,3 +1,12 @@ +2012-03-05 Chong Yidong + + * 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 * 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." (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."