Subject: Re: bug#52769: 29.0.50; [FEATURE REQUEST] repunctuate-sentences in region

From: Juri Linkov <[email-masked]>
Date: Tue, 28 Dec 2021 21:28:21 +0200

> When I tried 'repunctuate-sentences', it stunned by its inefficiency:
> it requires a confirmation even when there are already two spaces
> at the end of the sentence!  Why does it do this?

If no one has a better idea for a simpler implementation,
then this patch fixes the problem by skipping the sentences
that already have two spaces at the end:

diff --git a/lisp/textmodes/paragraphs.el b/lisp/textmodes/paragraphs.el
index 98362b8579..0b09895339 100644
--- a/lisp/textmodes/paragraphs.el
+++ b/lisp/textmodes/paragraphs.el
@@ -494,7 +494,14 @@ repunctuate-sentences
     (if no-query
         (while (re-search-forward regexp nil t)
           (replace-match to-string))
-      (query-replace-regexp regexp to-string nil start end))))
+      (let ((regexp "\\([]\"')]?\\)\\([.?!]\\)\\([]\"')]?\\)\\( +\\)")
+            (space-filter (lambda (_start _end)
+                            (not (length= (match-string 4) 2)))))
+        (unwind-protect
+            (progn
+              (add-function :after-while isearch-filter-predicate space-filter)
+              (query-replace-regexp regexp to-string nil start end))
+          (remove-function isearch-filter-predicate space-filter))))))

 (defun backward-sentence (&optional arg)