emacs-elpa-diffs
[Top][All Lists]
Advanced

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

[elpa] externals/compat c8bc47b671 18/27: Add replace-string-in-region


From: ELPA Syncer
Subject: [elpa] externals/compat c8bc47b671 18/27: Add replace-string-in-region
Date: Sat, 5 Mar 2022 04:57:28 -0500 (EST)

branch: externals/compat
commit c8bc47b671fd1852c2cc916e1867c65b7fb4eaae
Author: Philip Kaludercic <philipk@posteo.net>
Commit: Philip Kaludercic <philipk@posteo.net>

    Add replace-string-in-region
---
 MANUAL       |  1 +
 compat-28.el | 27 +++++++++++++++++++++++++++
 2 files changed, 28 insertions(+)

diff --git a/MANUAL b/MANUAL
index 51009a7926..56d6967cdd 100644
--- a/MANUAL
+++ b/MANUAL
@@ -331,6 +331,7 @@ provided by compat by default:
 - Function ~always~ :: [[info:elisp#Calling Functions][(elisp) Calling 
Functions]].
 - Function ~insert-into-buffer~ :: See [[info:elisp#Insertion][(elisp) 
Insertion]].
 - Function ~replace-regexp-in-region~ :: See [[info:elisp#Search and 
Replace][(elisp) Search and Replace]].
+- Function ~replace-string-in-region~ :: See [[info:elisp#Search and 
Replace][(elisp) Search and Replace]].
 - Function ~buffer-local-boundp~ :: See [[info:elisp#Creating 
Buffer-Local][(elisp) Creating Buffer-Local]].
 - Function ~with-existing-directory~ :: See [[info:elisp#Testing 
Accessibility][(elisp) Testing Accessibility]].
 - Macro ~dlet~ :: See [[info:elisp#Local Variables][(elisp) Local Variables]].
diff --git a/compat-28.el b/compat-28.el
index d60ef5c2f6..37d12f9e1c 100644
--- a/compat-28.el
+++ b/compat-28.el
@@ -332,6 +332,33 @@ Point in BUFFER will be placed after the inserted text."
     (with-current-buffer buffer
       (insert-buffer-substring current start end))))
 
+(compat-defun replace-string-in-region (string replacement &optional start end)
+  "Replace STRING with REPLACEMENT in the region from START to END.
+The number of replaced occurrences are returned, or nil if STRING
+doesn't exist in the region.
+
+If START is nil, use the current point.  If END is nil, use `point-max'.
+
+Comparisons and replacements are done with fixed case."
+  (if start
+      (when (< start (point-min))
+        (error "Start before start of buffer"))
+    (setq start (point)))
+  (if end
+      (when (> end (point-max))
+        (error "End after end of buffer"))
+    (setq end (point-max)))
+  (save-excursion
+    (let ((matches 0)
+          (case-fold-search nil))
+      (goto-char start)
+      (while (search-forward string end t)
+        (delete-region (match-beginning 0) (match-end 0))
+        (insert replacement)
+        (setq matches (1+ matches)))
+      (and (not (zerop matches))
+           matches))))
+
 (compat-defun replace-regexp-in-region (regexp replacement &optional start end)
   "Replace REGEXP with REPLACEMENT in the region from START to END.
 The number of replaced occurrences are returned, or nil if REGEXP



reply via email to

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