* 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
* Re: Counting sentences in `count-words'
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
0 siblings, 1 reply; 16+ messages in thread
From: Eli Zaretskii @ 2022-05-21 9:26 UTC (permalink / raw)
To: Manuel Giraud; +Cc: emacs-devel
> From: Manuel Giraud <manuel@ledu-giraud.fr>
> Date: Fri, 20 May 2022 14:16:57 +0200
>
> Why not have emacs to count sentences besides lines, words and
> characters? So far, here is the patch that I end up with.
Thanks, looks like a useful addition.
> - 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.
Why not use ignore-errors where you call forward-sentence?
> --- 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)))))
And why this change?
Finally, I think this addition warrants a NEWS entry.
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: Counting sentences in `count-words'
2022-05-21 9:26 ` Eli Zaretskii
@ 2022-05-21 17:07 ` Manuel Giraud
2022-05-21 17:32 ` Eli Zaretskii
0 siblings, 1 reply; 16+ messages in thread
From: Manuel Giraud @ 2022-05-21 17:07 UTC (permalink / raw)
To: Eli Zaretskii; +Cc: emacs-devel
Eli Zaretskii <eliz@gnu.org> writes:
>> - 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.
>
> Why not use ignore-errors where you call forward-sentence?
Yes. I did that and it works as intended. And `forward-sentence' still
prints an error at the end of buffer. Thanks!
>> --- 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)))))
>
> And why this change?
I've made this last change because otherwise a call to
`forward-sentence' does not return t if it has move and nil if it has
not (like `forward-word' does) so I could not count in a while loop.
In fact, I've just discovered that my counting is not correct (if ! is
the point and @ is the mark):
!I do. Have.
Three sentences.@ -- count--words --> 3 sentences
but:
!I do. Have.
Three sentences. -- count--words --> 4 sentences (and I think it
@ still should be 3)
> Finally, I think this addition warrants a NEWS entry.
Ok. I don't know how to write them but I could try to mimic what was
done in another patch. I've also modified the info manual and the
docstring.
--
Manuel Giraud
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: Counting sentences in `count-words'
2022-05-21 17:07 ` Manuel Giraud
@ 2022-05-21 17:32 ` Eli Zaretskii
2022-05-22 12:42 ` Manuel Giraud
0 siblings, 1 reply; 16+ messages in thread
From: Eli Zaretskii @ 2022-05-21 17:32 UTC (permalink / raw)
To: Manuel Giraud; +Cc: emacs-devel
> From: Manuel Giraud <manuel@ledu-giraud.fr>
> Cc: emacs-devel@gnu.org
> Date: Sat, 21 May 2022 19:07:26 +0200
>
> In fact, I've just discovered that my counting is not correct (if ! is
> the point and @ is the mark):
>
> !I do. Have.
> Three sentences.@ -- count--words --> 3 sentences
>
> but:
>
> !I do. Have.
> Three sentences. -- count--words --> 4 sentences (and I think it
> @ still should be 3)
Sounds like you need to make sure each sentence is not a string whose
contents is whitespace and nothing else.
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: Counting sentences in `count-words'
2022-05-21 17:32 ` Eli Zaretskii
@ 2022-05-22 12:42 ` Manuel Giraud
2022-05-22 16:52 ` Visuwesh
0 siblings, 1 reply; 16+ messages in thread
From: Manuel Giraud @ 2022-05-22 12:42 UTC (permalink / raw)
To: Eli Zaretskii; +Cc: emacs-devel
[-- Attachment #1: Type: text/plain, Size: 200 bytes --]
Eli Zaretskii <eliz@gnu.org> 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.
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Make-count-words-count-sentences.patch --]
[-- Type: text/x-patch, Size: 4824 bytes --]
From fc7faf6319f3da55f2a63b39279fcd415b5ceb6b 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 | 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
[-- Attachment #3: Type: text/plain, Size: 18 bytes --]
--
Manuel Giraud
^ permalink raw reply related [flat|nested] 16+ messages in thread
* Re: Counting sentences in `count-words'
2022-05-22 12:42 ` Manuel Giraud
@ 2022-05-22 16:52 ` Visuwesh
2022-05-23 8:16 ` Manuel Giraud
0 siblings, 1 reply; 16+ messages in thread
From: Visuwesh @ 2022-05-22 16:52 UTC (permalink / raw)
To: Manuel Giraud; +Cc: Eli Zaretskii, emacs-devel
[ஞாயிறு மே 22, 2022] Manuel Giraud wrote:
> Eli Zaretskii <eliz@gnu.org> 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.
Does it make sense to count sentences in prog-mode buffers? I think it
will needlessly increase the noise when the buffer isn't derived from
text-mode.
With that said, I don't think I will mind it either way since I rarely
use `count-words'.
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: Counting sentences in `count-words'
2022-05-22 16:52 ` Visuwesh
@ 2022-05-23 8:16 ` Manuel Giraud
2022-05-23 11:06 ` Eli Zaretskii
0 siblings, 1 reply; 16+ messages in thread
From: Manuel Giraud @ 2022-05-23 8:16 UTC (permalink / raw)
To: Eli Zaretskii; +Cc: emacs-devel
Visuwesh <visuweshm@gmail.com> writes:
> Does it make sense to count sentences in prog-mode buffers? I think it
> will needlessly increase the noise when the buffer isn't derived from
> text-mode.
You're right I could test on the derived mode. But then maybe someone
wants to count sentences in a comment or a docstring. I don't know.
--
Manuel Giraud
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: Counting sentences in `count-words'
2022-05-23 8:16 ` Manuel Giraud
@ 2022-05-23 11:06 ` Eli Zaretskii
2022-05-23 11:09 ` Lars Ingebrigtsen
0 siblings, 1 reply; 16+ messages in thread
From: Eli Zaretskii @ 2022-05-23 11:06 UTC (permalink / raw)
To: Manuel Giraud, Lars Ingebrigtsen; +Cc: emacs-devel
> From: Manuel Giraud <manuel@ledu-giraud.fr>
> Cc: emacs-devel@gnu.org
> Date: Mon, 23 May 2022 10:16:06 +0200
>
> Visuwesh <visuweshm@gmail.com> writes:
>
> > Does it make sense to count sentences in prog-mode buffers? I think it
> > will needlessly increase the noise when the buffer isn't derived from
> > text-mode.
>
> You're right I could test on the derived mode. But then maybe someone
> wants to count sentences in a comment or a docstring. I don't know.
I'm not sure we should be bothered by that. After all, "words"
doesn't make much more sense when the major mode is for a programming
language, either. If we start distinguishing between major modes,
we'd need to provide a defcustom for that, etc. etc. I say let's keep
it simple.
Lars, WDYT?
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: Counting sentences in `count-words'
2022-05-23 11:06 ` Eli Zaretskii
@ 2022-05-23 11:09 ` Lars Ingebrigtsen
2022-05-23 12:23 ` Manuel Giraud
0 siblings, 1 reply; 16+ messages in thread
From: Lars Ingebrigtsen @ 2022-05-23 11:09 UTC (permalink / raw)
To: Eli Zaretskii; +Cc: Manuel Giraud, emacs-devel
Eli Zaretskii <eliz@gnu.org> writes:
> I'm not sure we should be bothered by that. After all, "words"
> doesn't make much more sense when the major mode is for a programming
> language, either. If we start distinguishing between major modes,
> we'd need to provide a defcustom for that, etc. etc. I say let's keep
> it simple.
>
> Lars, WDYT?
Keeping it simple sounds good to me.
--
(domestic pets only, the antidote for overdose, milk.)
bloggy blog: http://lars.ingebrigtsen.no
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: Counting sentences in `count-words'
2022-05-23 11:09 ` Lars Ingebrigtsen
@ 2022-05-23 12:23 ` Manuel Giraud
2022-05-23 12:39 ` Eli Zaretskii
0 siblings, 1 reply; 16+ messages in thread
From: Manuel Giraud @ 2022-05-23 12:23 UTC (permalink / raw)
To: Lars Ingebrigtsen; +Cc: Eli Zaretskii, emacs-devel
Lars Ingebrigtsen <larsi@gnus.org> writes:
> Eli Zaretskii <eliz@gnu.org> writes:
[...]
>> Lars, WDYT?
>
> Keeping it simple sounds good to me.
Ok. Then my last patch is the best I have now… maybe the only thing left
is the `count-sentences' function defined in "simple.el" using
`forward-sentence' from "paragraphs.el". What do you think?
--
Manuel Giraud
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: Counting sentences in `count-words'
2022-05-23 12:23 ` Manuel Giraud
@ 2022-05-23 12:39 ` Eli Zaretskii
2022-05-23 13:18 ` Manuel Giraud
0 siblings, 1 reply; 16+ messages in thread
From: Eli Zaretskii @ 2022-05-23 12:39 UTC (permalink / raw)
To: Manuel Giraud; +Cc: larsi, emacs-devel
> From: Manuel Giraud <manuel@ledu-giraud.fr>
> Cc: Eli Zaretskii <eliz@gnu.org>, emacs-devel@gnu.org
> Date: Mon, 23 May 2022 14:23:55 +0200
>
> >> Lars, WDYT?
> >
> > Keeping it simple sounds good to me.
>
> Ok. Then my last patch is the best I have now… maybe the only thing left
> is the `count-sentences' function defined in "simple.el" using
> `forward-sentence' from "paragraphs.el". What do you think?
It may be better to move count-sentences to paragraphs.el.
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: Counting sentences in `count-words'
2022-05-23 12:39 ` Eli Zaretskii
@ 2022-05-23 13:18 ` Manuel Giraud
2022-05-24 15:57 ` Filipp Gunbin
2022-05-29 8:02 ` Eli Zaretskii
0 siblings, 2 replies; 16+ messages in thread
From: Manuel Giraud @ 2022-05-23 13:18 UTC (permalink / raw)
To: Eli Zaretskii; +Cc: larsi, emacs-devel
[-- 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
^ permalink raw reply related [flat|nested] 16+ messages in thread
* Re: Counting sentences in `count-words'
2022-05-23 13:18 ` Manuel Giraud
@ 2022-05-24 15:57 ` Filipp Gunbin
2022-05-25 8:17 ` Manuel Giraud
2022-05-29 8:02 ` Eli Zaretskii
1 sibling, 1 reply; 16+ messages in thread
From: Filipp Gunbin @ 2022-05-24 15:57 UTC (permalink / raw)
To: Manuel Giraud; +Cc: Eli Zaretskii, larsi, emacs-devel
On 23/05/2022 15:18 +0200, Manuel Giraud wrote:
> 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.
>
With just a single newline in file, (count-sentences (point-min)
(point-max)) loops indefinitely.
I guess there're two options for safe movement:
- Either rely on what movement function returns - in this case
forward-sentence doesn't specify return value, so no luck
- Or track the point yourself, and check whether you've moved. I think
you should do this here.
Filipp
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: Counting sentences in `count-words'
2022-05-24 15:57 ` Filipp Gunbin
@ 2022-05-25 8:17 ` Manuel Giraud
2022-05-25 10:13 ` Filipp Gunbin
0 siblings, 1 reply; 16+ messages in thread
From: Manuel Giraud @ 2022-05-25 8:17 UTC (permalink / raw)
To: Eli Zaretskii; +Cc: larsi, emacs-devel
Filipp Gunbin <fgunbin@fastmail.fm> writes:
> With just a single newline in file, (count-sentences (point-min)
> (point-max)) loops indefinitely.
Hi Filipp,
I'm not able to reproduce. For me™, it returns 0. I've tried with both
DOS and Unix line terminator.
> I guess there're two options for safe movement:
>
> - Either rely on what movement function returns - in this case
> forward-sentence doesn't specify return value, so no luck
>
> - Or track the point yourself, and check whether you've moved. I think
> you should do this here.
Maybe you're right, I can then avoid modifying 'forward-sentence'.
--
Manuel Giraud
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: Counting sentences in `count-words'
2022-05-25 8:17 ` Manuel Giraud
@ 2022-05-25 10:13 ` Filipp Gunbin
0 siblings, 0 replies; 16+ messages in thread
From: Filipp Gunbin @ 2022-05-25 10:13 UTC (permalink / raw)
To: Manuel Giraud; +Cc: Eli Zaretskii, larsi, emacs-devel
On 25/05/2022 10:17 +0200, Manuel Giraud wrote:
> Filipp Gunbin <fgunbin@fastmail.fm> writes:
>
>> With just a single newline in file, (count-sentences (point-min)
>> (point-max)) loops indefinitely.
>
> Hi Filipp,
>
> I'm not able to reproduce. For me™, it returns 0. I've tried with both
> DOS and Unix line terminator.
>
>> I guess there're two options for safe movement:
>>
>> - Either rely on what movement function returns - in this case
>> forward-sentence doesn't specify return value, so no luck
>>
>> - Or track the point yourself, and check whether you've moved. I think
>> you should do this here.
>
> Maybe you're right, I can then avoid modifying 'forward-sentence'.
Ah, sorry, I tried just the count-sentences, without modification of
forward-sentence.
Filipp
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: Counting sentences in `count-words'
2022-05-23 13:18 ` Manuel Giraud
2022-05-24 15:57 ` Filipp Gunbin
@ 2022-05-29 8:02 ` Eli Zaretskii
1 sibling, 0 replies; 16+ messages in thread
From: Eli Zaretskii @ 2022-05-29 8:02 UTC (permalink / raw)
To: Manuel Giraud; +Cc: larsi, emacs-devel
> From: Manuel Giraud <manuel@ledu-giraud.fr>
> Cc: larsi@gnus.org, emacs-devel@gnu.org
> Date: Mon, 23 May 2022 15:18:45 +0200
>
> > It may be better to move count-sentences to paragraphs.el.
>
> Ok. Here is a new version of the patch.
Thanks, I installed this.
Please in the future include in the patches a ChangeLog-style commit
log message, as described in CONTRIBUTE. (I wrote the log message for
you this time.)
^ permalink raw reply [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.