From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Manuel Giraud Newsgroups: gmane.emacs.devel Subject: Counting sentences in `count-words' Date: Fri, 20 May 2022 14:16:57 +0200 Message-ID: <87o7zsiekm.fsf@elite.giraud> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="12661"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/29.0.50 (berkeley-unix) To: emacs-devel Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Fri May 20 14:19:32 2022 Return-path: Envelope-to: ged-emacs-devel@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ns1bP-00033Q-R4 for ged-emacs-devel@m.gmane-mx.org; Fri, 20 May 2022 14:19:31 +0200 Original-Received: from localhost ([::1]:42496 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ns1bO-00085C-Mo for ged-emacs-devel@m.gmane-mx.org; Fri, 20 May 2022 08:19:30 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:56232) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ns1Z3-0006ZR-3K for emacs-devel@gnu.org; Fri, 20 May 2022 08:17:05 -0400 Original-Received: from ledu-giraud.fr ([51.159.28.247]:25371) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ns1Z0-0000Nr-Th for emacs-devel@gnu.org; Fri, 20 May 2022 08:17:04 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; s=rsa; bh=bGXvegodgbtsto09 AgOi5OrABcJTayYDUYqOcA1GC/w=; h=date:subject:to:from; d=ledu-giraud.fr; b=H0r39B/9+kPkrfW1sttIgFEVBwbAWL84XYvdeNnPt5Wq1maU8u SketiLwerrhaxu2xa+Cp/3b/uBRm08JF1nmEsF2go49m2P+b0zT92P2gFm6UucLXifMfPf /MdnjhXuMt0j//zw7Hxygv0IbLTLB2QxdXmbVdadkQWNstgggyI6/J/g9BHS9u019dVFbJ Qert6e93+/CahWlTvhKVE6LgcsBec9IgcyukqfHtwerBS7I9ozacG6JLrKfwfyd4604/M8 eZeVfz4PWOtpFMzVqxrVXCWosr86LmvLerSuMQzLanJLteF1MuPyjY+ILRz4d8RMaK0hxt cEcq2Gh0FNXg== Original-Received: from elite.giraud ( [10.1.1.1]) by ledu-giraud.fr (OpenSMTPD) with ESMTPSA id 698ee871 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO) for ; Fri, 20 May 2022 14:16:58 +0200 (CEST) Received-SPF: pass client-ip=51.159.28.247; envelope-from=manuel@ledu-giraud.fr; helo=ledu-giraud.fr X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.io gmane.emacs.devel:290000 Archived-At: --=-=-= Content-Type: text/plain 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. --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0001-Make-count-words-count-sentences.patch >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 --=-=-= Content-Type: text/plain -- Manuel Giraud --=-=-=--