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: Re: Counting sentences in `count-words' Date: Sun, 22 May 2022 14:42:00 +0200 Message-ID: <87y1ytd9if.fsf@elite.giraud> References: <87o7zsiekm.fsf@elite.giraud> <83y1yv8cdp.fsf@gnu.org> <878rquizld.fsf@elite.giraud> <83czg694ft.fsf@gnu.org> 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="1002"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/29.0.50 (berkeley-unix) Cc: emacs-devel@gnu.org To: Eli Zaretskii Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Sun May 22 14:43:15 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 1nskvS-00006f-4m for ged-emacs-devel@m.gmane-mx.org; Sun, 22 May 2022 14:43:14 +0200 Original-Received: from localhost ([::1]:43160 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nskvQ-0007m5-Q4 for ged-emacs-devel@m.gmane-mx.org; Sun, 22 May 2022 08:43:13 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:57920) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nskuR-00070r-S5 for emacs-devel@gnu.org; Sun, 22 May 2022 08:42:12 -0400 Original-Received: from ledu-giraud.fr ([51.159.28.247]:3259) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nskuP-0005ZQ-K7; Sun, 22 May 2022 08:42:11 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; s=rsa; bh=4aNonvwfUT2ZLfoU AU3BlZjY+fDqWXzX+vawhFp+iwU=; h=in-reply-to:date:references:subject: cc:to:from; d=ledu-giraud.fr; b=LQg/eS5vPVPfulc4WEuSI4T/6oCfSSjTawAqpy IznjO2hM9HxV1SO5jLidB8ROF8DdFvz5+JCBXCFvh09K4HB+QFEofg/KUe69VhsQAVwIR2 WqFxADFi9Wdefk7GeYUsTo2XTsFnz9bpPT5/GmwQ27fJe37ghb1iA5zEJgJVe12vDFAXQL bAKsJm+j5WGYbZ3yIXpF2uyDH+y4tqMr0+NR4VvH3jZq/eVTisJF/ga+KgSTPWp6ewoZ1T vYJuAwSU6VfL8E+DXJXpWFrR8Fe4d1+WymC8lFVq/0f7NnE0yE1VlMtnvtMrxYqPAbLAiL yAhUe0dDogbagjChrkE55Idw== Original-Received: from elite.giraud ( [10.1.1.1]) by ledu-giraud.fr (OpenSMTPD) with ESMTPSA id 9b631eac (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Sun, 22 May 2022 14:42:04 +0200 (CEST) In-Reply-To: <83czg694ft.fsf@gnu.org> (Eli Zaretskii's message of "Sat, 21 May 2022 20:32:54 +0300") 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:290091 Archived-At: --=-=-= Content-Type: text/plain Eli Zaretskii writes: > Sounds like you need to make sure each sentence is not a string whose > contents is whitespace and nothing else. Hi, Here is an updated version of the patch. --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0001-Make-count-words-count-sentences.patch >From fc7faf6319f3da55f2a63b39279fcd415b5ceb6b 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. --- doc/emacs/basic.texi | 14 +++++++------- etc/NEWS | 3 +++ lisp/simple.el | 23 ++++++++++++++++++++--- lisp/textmodes/paragraphs.el | 3 ++- 4 files changed, 32 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..bee7e90f72 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,14 +1691,30 @@ 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 (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)))) + (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..7eb80d9af1 100644 --- a/lisp/textmodes/paragraphs.el +++ b/lisp/textmodes/paragraphs.el @@ -477,7 +477,8 @@ 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 repunctuate-sentences-filter (_start _end) "Search filter used by `repunctuate-sentences' to skip unneeded spaces. -- 2.36.0 --=-=-= Content-Type: text/plain -- Manuel Giraud --=-=-=--