1
0
mirror of https://git.savannah.gnu.org/git/emacs.git synced 2025-01-22 18:35:09 +00:00

Rework count-words-region. New command count-words.

See discussion at http://lists.gnu.org/archive/html/emacs-devel/2011-10/msg00193.html

* lisp/simple.el (count-words-region): Always count in the region.
Report the number of lines and characters too.
(count-words): New command, which counts in the buffer if the
region is inactive, as count-words-region used to.
(count-words--message): New function.  Handle plurals.
(count-lines-region): Make it an alias for count-words-region.

* lisp/bindings.el (esc-map): Replace count-lines-region with
count-words-region.

* doc/emacs/basic.texi (Position Info): Omit page commands.  Document
count-words-region and count-words.

* doc/emacs/text.texi (Pages): Move what-page documentation here.
This commit is contained in:
Chong Yidong 2011-10-08 12:37:46 -04:00
parent 27a1646245
commit b2b0776e50
8 changed files with 81 additions and 40 deletions

View File

@ -1,3 +1,10 @@
2011-10-08 Chong Yidong <cyd@stupidchicken.com>
* basic.texi (Position Info): Omit page commands. Document
count-words-region and count-words.
* text.texi (Pages): Move what-page documentation here.
2011-10-08 Chong Yidong <cyd@stupidchicken.com>
* mini.texi (Minibuffer File): Minor copyedits. Use xref to

View File

@ -23,7 +23,7 @@ suggest you first run the Emacs learn-by-doing tutorial, by typing
* Help: Basic Help. Asking what a character does.
* Blank Lines:: Making and deleting blank lines.
* Continuation Lines:: How Emacs displays lines too wide for the screen.
* Position Info:: What page, line, row, or column is point on?
* Position Info:: What line, row, or column is point on?
* Arguments:: Numeric arguments for repeating a command N times.
* Repeating:: Repeating the previous command quickly.
@end menu
@ -569,28 +569,26 @@ logical lines. @xref{Visual Line Mode}.
@section Cursor Position Information
Here are commands to get information about the size and position of
parts of the buffer, and to count lines.
parts of the buffer, and to count words and lines.
@table @kbd
@item M-x what-page
Display the page number of point, and the line number within that page.
@item M-x what-line
Display the line number of point in the whole buffer.
Display the line number of point.
@item M-x line-number-mode
@itemx M-x column-number-mode
Toggle automatic display of the current line number or column number.
@xref{Optional Mode Line}.
@item M-=
Display the number of lines in the region (@code{count-lines-region}).
@xref{Mark}, for information about the region.
Display the number of lines, words, and characters that are present in
the region (@code{count-words-region}). @xref{Mark}, for information
about the region.
@item C-x l
Display the number of lines in the current page
(@code{count-lines-page}). @xref{Pages}.
@item M-x count-words
Display the number of lines, words, and characters that are present in
the buffer. If the region is active (@pxref{Mark}), display the
numbers for the region instead.
@item M-x count-words-region
Display the number of words in the region.
@item C-x =
Display the character code of character after point, character position of
point, and column of point (@code{what-cursor-position}).
@ -602,7 +600,6 @@ Toggle automatic display of the size of the buffer.
@xref{Optional Mode Line}.
@end table
@findex what-page
@findex what-line
@cindex line number commands
@cindex location of point
@ -616,16 +613,14 @@ the accessible portion (@pxref{Narrowing}). By contrast,
@code{what-line} displays both the line number relative to the
narrowed region and the line number relative to the whole buffer.
@kbd{M-x what-page} counts pages from the beginning of the file, and
counts lines within the page, showing both numbers in the echo area.
@xref{Pages}.
@kindex M-=
@findex count-lines-region
@kbd{M-=} (@code{count-lines-region}) displays the number of lines
in the region (@pxref{Mark}), while @kbd{C-x l}
(@code{count-lines-page}) counts the lines in the current page
(@pxref{Pages}).
@findex count-words-region
@findex count-words
@kbd{M-=} (@code{count-words-region}) displays a message reporting
the number of lines, words, and characters in the region. @kbd{M-x
count-words} displays a similar message for the entire buffer, or for
the region if the region is @dfn{active}. @xref{Mark}, for an
explanation of the region.
@kindex C-x =
@findex what-cursor-position

View File

@ -264,7 +264,7 @@ Basic Editing Commands
* Basic Help:: Asking what a character does.
* Blank Lines:: Making and deleting blank lines.
* Continuation Lines:: How Emacs displays lines too wide for the screen.
* Position Info:: What page, line, row, or column is point on?
* Position Info:: What line, row, or column is point on?
* Arguments:: Numeric arguments for repeating a command N times.
* Repeating:: Repeating the previous command quickly.

View File

@ -332,6 +332,8 @@ pages are often meaningful divisions of the file, Emacs provides
commands to move over them and operate on them.
@table @kbd
@item M-x what-page
Display the page number of point, and the line number within that page.
@item C-x [
Move point to previous page boundary (@code{backward-page}).
@item C-x ]
@ -342,6 +344,10 @@ Put point and mark around this page (or another page) (@code{mark-page}).
Count the lines in this page (@code{count-lines-page}).
@end table
@findex what-page
@kbd{M-x what-page} counts pages from the beginning of the file, and
counts lines within the page, showing both numbers in the echo area.
@kindex C-x [
@kindex C-x ]
@findex forward-page

View File

@ -455,7 +455,10 @@ isearch-yank-kill.
*** M-s C-e in Isearch is now bound to isearch-yank-line.
+++
** New command `count-words-region'. This does what you expect.
** 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.
** The default value of `backup-by-copying-when-mismatch' is now t.

View File

@ -1,3 +1,15 @@
2011-10-08 Chong Yidong <cyd@stupidchicken.com>
* simple.el (count-words-region): Always count in the region.
Report the number of lines and characters too.
(count-words): New command, which counts in the buffer if the
region is inactive, as count-words-region used to.
(count-words--message): New function. Handle plurals.
(count-lines-region): Make it an alias for count-words-region.
* bindings.el (esc-map): Replace count-lines-region with
count-words-region.
2011-10-08 Martin Rudalics <rudalics@gmx.at>
* window.el (window--delete): Delete dedicated frame

View File

@ -773,7 +773,7 @@ if `inhibit-field-text-motion' is non-nil."
(define-key ctl-x-map "\C-o" 'delete-blank-lines)
(define-key esc-map " " 'just-one-space)
(define-key esc-map "z" 'zap-to-char)
(define-key esc-map "=" 'count-lines-region)
(define-key esc-map "=" 'count-words-region)
(define-key ctl-x-map "=" 'what-cursor-position)
(define-key esc-map ":" 'eval-expression)
;; Define ESC ESC : like ESC : for people who type ESC ESC out of habit.

View File

@ -945,28 +945,46 @@ rather than line counts."
(forward-line (1- line)))))
(defun count-words-region (start end)
"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))
"Return the number of words between START and END.
If called interactively, print a message reporting the number of
lines, words, and characters in the region."
(interactive "r")
(let ((words 0))
(save-excursion
(save-restriction
(narrow-to-region start end)
(goto-char (point-min))
(while (forward-word 1)
(setq count (1+ count)))))
(setq words (1+ words)))))
(when (called-interactively-p 'interactive)
(message "%s has %d words"
(if (use-region-p) "Region" "Buffer")
count))
count))
(count-words--message "Region"
(count-lines start end)
words
(- end start)))
words))
(defun count-lines-region (start end)
"Print number of lines and characters in the region."
(interactive "r")
(message "Region has %d lines, %d characters"
(count-lines start end) (- end start)))
(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)
(defun what-line ()
"Print the current buffer line number and narrowed line number of point."