]> git.eshelyaron.com Git - emacs.git/commitdiff
Make `count-words' count sentences.
authorManuel Giraud <manuel@ledu-giraud.fr>
Fri, 20 May 2022 11:52:28 +0000 (13:52 +0200)
committerEli Zaretskii <eliz@gnu.org>
Sun, 29 May 2022 07:58:13 +0000 (10:58 +0300)
* lisp/textmodes/paragraphs.el (count-sentences): New function.
* lisp/simple.el (count-words--format): Update format for showing
sentences.
(count-words): Also count sentences.

* lisp/simple.el (count-words):
* etc/NEWS:
* doc/emacs/basic.texi (Position Info): Update documentation for
sentence counting.

doc/emacs/basic.texi
etc/NEWS
lisp/simple.el
lisp/textmodes/paragraphs.el

index 196a28be5a14d2c1cced86f2ae178962c482a11b..b93a6d5de6e7eb80bd41191422c3755c24ec6442 100644 (file)
@@ -653,14 +653,14 @@ Toggle automatic display of the current line number or column number.
 displayed before each line, see @ref{Display Custom}.
 
 @item M-=
-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.
+Display the number of lines, sentences, words, and characters that are
+present in the region (@code{count-words-region}).  @xref{Mark}, for
+information about the region.
 
 @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.
+Display the number of lines, sentences, words, and characters that are
+present in the buffer.  If the region is active (@pxref{Mark}),
+display the numbers for the region instead.
 
 @item C-x =
 Display the character code of character after point, character position of
@@ -689,7 +689,7 @@ narrowed region and the line number relative to the whole buffer.
 @kindex M-=
 @findex count-words-region
   @kbd{M-=} (@code{count-words-region}) displays a message reporting
-the number of lines, words, and characters in the region
+the number of lines, sentences, words, and characters in the region
 (@pxref{Mark}, for an explanation of the region).  With a prefix
 argument, @kbd{C-u M-=}, the command displays a count for the entire
 buffer.
index 85a0ee44b9eb0795dfa89d8140e11b61302f3f33..1165ac37b24926cad3fe331d9e42a3f241627f96 100644 (file)
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -330,6 +330,9 @@ a convenient method of making commands disabled in this way.
 ---
 ** 'count-lines' will now report buffer totals if given a prefix.
 
++++
+** 'count-words' will now report sentences count when used interactively.
+
 ---
 ** New user option 'find-library-include-other-files'.
 If set to nil, commands like 'find-library' will only include library
index db52d83cea4ac232a826ccb1ab8ceb46fabbb128..a254ee225144e2436e84e7999ac0862d255dc14a 100644 (file)
@@ -1667,8 +1667,9 @@ 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.  With prefix argument, also
-include the data for the entire (un-narrowed) buffer.
+number of lines, sentences, words, and chars.  With prefix
+argument, also include the data for the entire (un-narrowed)
+buffer.
 
 If called from Lisp, return the number of words between START and
 END, without printing any message.  TOTALS is ignored when called
@@ -1708,11 +1709,13 @@ from Lisp."
 
 (defun count-words--format (str start end)
   (let ((lines (count-lines start end))
+       (sentences (count-sentences start end))
        (words (count-words start end))
        (chars (- end start)))
-    (format "%s has %d line%s, %d word%s, and %d character%s"
+    (format "%s has %d line%s, %d sentence%s, %d word%s, and %d character%s"
             str
             lines (if (= lines 1) "" "s")
+            sentences (if (= sentences 1) "" "s")
             words (if (= words 1) "" "s")
             chars (if (= chars 1) "" "s"))))
 
index 7daf71e990e8d9931c38e48c49cd424ff3ac8ceb..98eb494823d4c7797c25c97200c858902d1a496c 100644 (file)
@@ -477,7 +477,23 @@ sentences.  Also, every paragraph boundary terminates sentences as well."
            (skip-chars-backward " \t\n")
          (goto-char par-end)))
       (setq arg (1- arg)))
-    (constrain-to-field nil opoint t)))
+    (let ((npoint (constrain-to-field nil opoint t)))
+      (not (= npoint opoint)))))
+
+(defun count-sentences (start end)
+  "Count sentences in current buffer from START to END."
+  (let ((sentences 0)
+        (inhibit-field-text-motion t))
+    (save-excursion
+      (save-restriction
+        (narrow-to-region start end)
+        (goto-char (point-min))
+       (while (ignore-errors (forward-sentence))
+         (setq sentences (1+ sentences)))
+        ;; Remove last possibly empty sentence
+        (when (/= (skip-chars-backward " \t\n") 0)
+          (setq sentences (1- sentences)))
+       sentences))))
 
 (defun repunctuate-sentences-filter (_start _end)
   "Search filter used by `repunctuate-sentences' to skip unneeded spaces.