all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Counting sentences in `count-words'
@ 2022-05-20 12:16 Manuel Giraud
  2022-05-21  9:26 ` Eli Zaretskii
  0 siblings, 1 reply; 16+ messages in thread
From: Manuel Giraud @ 2022-05-20 12:16 UTC (permalink / raw)
  To: emacs-devel

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

Hi,

Why not have emacs to count sentences besides lines, words and
characters? So far, here is the patch that I end up with.

Comments:

    - the `count-sentences' function seems a bit out of place in
      "simple.el" but since I'm using it here I left it there;
          
    - I had to ignore-errors on (end-of-paragraph-text) in
      `forward-sentence' otherwise `count-words' won't work on a buffer
      without a terminal new line. But now `forward-sentence' won't
      error out at the end of buffer. Maybe there is a way around it
      that I don't know.
      

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

From 3b23b0865e6d285869adccf53f631692c820d081 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.

---
 lisp/simple.el               | 16 +++++++++++++++-
 lisp/textmodes/paragraphs.el |  5 +++--
 2 files changed, 18 insertions(+), 3 deletions(-)

diff --git a/lisp/simple.el b/lisp/simple.el
index cd7a82b7ac..feebb333a3 100644
--- a/lisp/simple.el
+++ b/lisp/simple.el
@@ -1690,14 +1690,28 @@ 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"))))
 
+(defun count-sentences (start end)
+  (let ((sentences 0)
+        (inhibit-field-text-motion t))
+    (save-excursion
+      (save-restriction
+        (narrow-to-region start end)
+        (goto-char (point-min))
+	(while (forward-sentence)
+	  (setq sentences (+ 1 sentences)))
+	sentences))))
+
 (define-obsolete-function-alias 'count-lines-region 'count-words-region "24.1")
 
 (defun what-line ()
diff --git a/lisp/textmodes/paragraphs.el b/lisp/textmodes/paragraphs.el
index 7daf71e990..e8aa16bafa 100644
--- a/lisp/textmodes/paragraphs.el
+++ b/lisp/textmodes/paragraphs.el
@@ -472,12 +472,13 @@ forward-sentence
 	  (goto-char par-text-beg)))
       (setq arg (1+ arg)))
     (while (> arg 0)
-      (let ((par-end (save-excursion (end-of-paragraph-text) (point))))
+      (let ((par-end (save-excursion (ignore-errors (end-of-paragraph-text)) (point))))
 	(if (re-search-forward sentence-end par-end t)
 	    (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 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

^ permalink raw reply related	[flat|nested] 16+ messages in thread

end of thread, other threads:[~2022-05-29  8:02 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
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

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.