From 3b23b0865e6d285869adccf53f631692c820d081 Mon Sep 17 00:00:00 2001 From: Manuel Giraud 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