diff options
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 @@ +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) 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." |