[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

master fb4f2aa038: * lisp/textmodes/paragraphs.el (repunctuate-sentences

From: Juri Linkov
Subject: master fb4f2aa038: * lisp/textmodes/paragraphs.el (repunctuate-sentences-filter): New function.
Date: Wed, 29 Dec 2021 14:10:28 -0500 (EST)

branch: master
commit fb4f2aa0389d7d10dfcc1b6d8601c2d5e80730f0
Author: Juri Linkov <juri@linkov.net>
Commit: Juri Linkov <juri@linkov.net>

    * lisp/textmodes/paragraphs.el (repunctuate-sentences-filter): New function.
    (repunctuate-sentences): Use it on isearch-filter-predicate
    to skip unnecessary matches.  (bug#52769)
 lisp/textmodes/paragraphs.el | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)

diff --git a/lisp/textmodes/paragraphs.el b/lisp/textmodes/paragraphs.el
index acb26fd1c1..788230141e 100644
--- a/lisp/textmodes/paragraphs.el
+++ b/lisp/textmodes/paragraphs.el
@@ -479,6 +479,14 @@ sentences.  Also, every paragraph boundary terminates 
sentences as well."
       (setq arg (1- arg)))
     (constrain-to-field nil opoint t)))
+(defun repunctuate-sentences-filter (_start _end)
+  "Search filter used by `repunctuate-sentences' to skip unneeded spaces.
+By default, it skips occurrences that already have two spaces.
+It is advised to put `advice-add' on this function to add more filters,
+for example, `(looking-back (rx (or \"e.g.\" \"i.e.\") \" \") 5)'
+with a set of predefined abbreviations to skip from adding two spaces."
+  (not (length= (match-string 4) 2)))
 (defun repunctuate-sentences (&optional no-query start end)
   "Put two spaces at the end of sentences from point to the end of buffer.
 It works using `query-replace-regexp'.  In Transient Mark mode,
@@ -489,14 +497,20 @@ asking for confirmation."
   (interactive (list nil
                      (if (use-region-p) (region-beginning))
                      (if (use-region-p) (region-end))))
-  (let ((regexp "\\([]\"')]?\\)\\([.?!]\\)\\([]\"')]?\\) +")
+  (let ((regexp "\\([]\"')]?\\)\\([.?!]\\)\\([]\"')]?\\)\\( +\\)")
         (to-string "\\1\\2\\3  "))
     (if no-query
           (when start (goto-char start))
           (while (re-search-forward regexp end t)
             (replace-match to-string)))
-      (query-replace-regexp regexp to-string nil start end))))
+      (unwind-protect
+          (progn
+            (add-function :after-while isearch-filter-predicate
+                          #'repunctuate-sentences-filter)
+            (query-replace-regexp regexp to-string nil start end))
+        (remove-function isearch-filter-predicate
+                         #'repunctuate-sentences-filter)))))
 (defun backward-sentence (&optional arg)

reply via email to

[Prev in Thread] Current Thread [Next in Thread]