all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Manuel Giraud <manuel@ledu-giraud.fr>
To: Eli Zaretskii <eliz@gnu.org>
Cc: larsi@gnus.org,  emacs-devel@gnu.org
Subject: Re: Counting sentences in `count-words'
Date: Mon, 23 May 2022 15:18:45 +0200	[thread overview]
Message-ID: <871qwk9ykq.fsf@elite.giraud> (raw)
In-Reply-To: <83zgj85spj.fsf@gnu.org> (Eli Zaretskii's message of "Mon, 23 May 2022 15:39:04 +0300")

[-- Attachment #1: Type: text/plain, Size: 140 bytes --]

Eli Zaretskii <eliz@gnu.org> writes:

> It may be better to move count-sentences to paragraphs.el.

Ok. Here is a new version of the patch.

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Make-count-words-count-sentences.patch --]
[-- Type: text/x-patch, Size: 4780 bytes --]

From 2a6328872e61db1a4dffb0d092b707c32ef1f379 Mon Sep 17 00:00:00 2001
From: Manuel Giraud <manuel@ledu-giraud.fr>
Date: Fri, 20 May 2022 13:52:28 +0200
Subject: [PATCH] Make `count-words' count sentences.

---
 doc/emacs/basic.texi         | 14 +++++++-------
 etc/NEWS                     |  3 +++
 lisp/simple.el               |  9 ++++++---
 lisp/textmodes/paragraphs.el | 18 +++++++++++++++++-
 4 files changed, 33 insertions(+), 11 deletions(-)

diff --git a/doc/emacs/basic.texi b/doc/emacs/basic.texi
index 196a28be5a..b93a6d5de6 100644
--- a/doc/emacs/basic.texi
+++ b/doc/emacs/basic.texi
@@ -653,14 +653,14 @@ Position Info
 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 @@ Position Info
 @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.
diff --git a/etc/NEWS b/etc/NEWS
index 7089e3a271..36df7f1861 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -315,6 +315,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
diff --git a/lisp/simple.el b/lisp/simple.el
index cd7a82b7ac..f3a941432e 100644
--- a/lisp/simple.el
+++ b/lisp/simple.el
@@ -1649,8 +1649,9 @@ count-words
 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
@@ -1690,11 +1691,13 @@ count-words--buffer-format
 
 (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"))))
 
diff --git a/lisp/textmodes/paragraphs.el b/lisp/textmodes/paragraphs.el
index 7daf71e990..98eb494823 100644
--- a/lisp/textmodes/paragraphs.el
+++ b/lisp/textmodes/paragraphs.el
@@ -477,7 +477,23 @@ forward-sentence
 	    (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.
-- 
2.36.0


[-- Attachment #3: Type: text/plain, Size: 18 bytes --]

-- 
Manuel Giraud

  reply	other threads:[~2022-05-23 13:18 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-20 12:16 Counting sentences in `count-words' Manuel Giraud
2022-05-21  9:26 ` Eli Zaretskii
2022-05-21 17:07   ` Manuel Giraud
2022-05-21 17:32     ` Eli Zaretskii
2022-05-22 12:42       ` Manuel Giraud
2022-05-22 16:52         ` Visuwesh
2022-05-23  8:16           ` Manuel Giraud
2022-05-23 11:06             ` Eli Zaretskii
2022-05-23 11:09               ` Lars Ingebrigtsen
2022-05-23 12:23                 ` Manuel Giraud
2022-05-23 12:39                   ` Eli Zaretskii
2022-05-23 13:18                     ` Manuel Giraud [this message]
2022-05-24 15:57                       ` Filipp Gunbin
2022-05-25  8:17                         ` Manuel Giraud
2022-05-25 10:13                           ` Filipp Gunbin
2022-05-29  8:02                       ` Eli Zaretskii

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=871qwk9ykq.fsf@elite.giraud \
    --to=manuel@ledu-giraud.fr \
    --cc=eliz@gnu.org \
    --cc=emacs-devel@gnu.org \
    --cc=larsi@gnus.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.