]> git.eshelyaron.com Git - emacs.git/commitdiff
Use `replace-region-contents` to replace insert+delete
authorStefan Monnier <monnier@iro.umontreal.ca>
Fri, 28 Mar 2025 05:01:17 +0000 (01:01 -0400)
committerEshel Yaron <me@eshelyaron.com>
Mon, 31 Mar 2025 08:51:11 +0000 (10:51 +0200)
* lisp/minibuffer.el (completion--replace):
* lisp/emacs-lisp/cl-lib.el (cl--set-buffer-substring):
* lisp/subr.el (replace-string-in-region):
Use `replace-region-contents` instead of insert+delete.

* lisp/help-fns.el (help-fns--signature):
Use `replace-region-contents` instead of `cl--set-buffer-substring`.

* lisp/language/japan-util.el (japanese-replace-region):
Rewrite using `replace-region-contents` and mark obsolete.
(japanese-katakana-region, japanese-hankaku-region):
Use `replace-region-contents` instead.

* lisp/progmodes/flymake-proc.el (flymake-proc--replace-region):
Rewrite using `replace-region-contents` and mark obsolete.
(flymake-proc--check-patch-master-file-buffer):
Use `replace-region-contents` instead.

(cherry picked from commit 1d07a6d7e34677be1653b1d4d464ff00cabfa102)

lisp/emacs-lisp/cl-lib.el
lisp/emacs-lisp/gv.el
lisp/help-fns.el
lisp/language/japan-util.el
lisp/minibuffer.el
lisp/subr.el

index b4cb1200c9a47a2320f379e004990e550787ea8b..381fbee63d31a490248a6e9629d1539145a17d1a 100644 (file)
@@ -154,12 +154,10 @@ to an element already in the list stored in PLACE.
        `(setq ,place (cl-adjoin ,x ,place ,@keys)))
     `(cl-callf2 cl-adjoin ,x ,place ,@keys)))
 
-(defun cl--set-buffer-substring (start end val)
+(defun cl--set-buffer-substring (start end val &optional inherit)
   "Delete region from START to END and insert VAL."
-  (save-excursion (delete-region start end)
-                 (goto-char start)
-                 (insert val)
-                 val))
+  (replace-region-contents start end val 0 nil inherit)
+  val)
 
 (defun cl--set-substring (str start end val)
   (if end (if (< end 0) (incf end (length str)))
index d133dd0e42688079162cd388898291f79d9aeb38..0579bf6a265c03d9fb94c4d58ecf46f6afcf7f24 100644 (file)
@@ -684,6 +684,8 @@ REF must have been previously obtained with `gv-ref'."
   `(insert (prog1 ,store (erase-buffer))))
 (make-obsolete-generalized-variable 'buffer-string nil "29.1")
 
+;; FIXME: Can't use `replace-region-contents' because it's not
+;; expected to be costly, so we need to pass MAX-SECS==0.
 (gv-define-simple-setter buffer-substring cl--set-buffer-substring)
 (make-obsolete-generalized-variable 'buffer-substring nil "29.1")
 
index 2a2dd1f72628aa96c6611f2d48f002cb00f2bf38..dedafb74073055adcc226806b9f68b457db3d281 100644 (file)
@@ -795,7 +795,7 @@ the C sources, too."
                      (save-excursion
                        (forward-char -1)
                        (<= (current-column) (- fill-column 12)))
-                     (cl--set-buffer-substring (- beg 3) beg " ")))))
+                     (replace-region-contents (- beg 3) beg " " 0)))))
           high-doc)))))
 
 (defun help-fns--parent-mode (function)
index 718c469d5622cff96dd9e40c8954a34bca193b4e..6fbb52b627eea4c341776555ac062db967d8855c 100644 (file)
@@ -217,9 +217,9 @@ The argument object is not altered--the value is a copy."
 
 (defun japanese-replace-region (from to string)
   "Replace the region specified by FROM and TO to STRING."
-  (goto-char from)
-  (insert string)
-  (delete-char (- to from)))
+  (declare (obsolete replace-region-contents "31.1"))
+  (goto-char to)
+  (replace-region-contents from to string 0))
 
 ;;;###autoload
 (defun japanese-katakana-region (from to &optional hankaku)
@@ -238,13 +238,15 @@ of which charset is `japanese-jisx0201-kana'."
                     (get-char-code-property kana 'kana-composition)))
               slot) ;; next
          (if (and composition (setq slot (assq (following-char) composition)))
-             (japanese-replace-region (match-beginning 0) (1+ (point))
-                                      (cdr slot))
+             (progn
+               (goto-char (1+ (point)))
+               (replace-region-contents (match-beginning 0) (point)
+                                        (cdr slot) 0))
            (let ((kata (get-char-code-property
                         kana (if hankaku 'jisx0201 'katakana))))
              (if kata
-                 (japanese-replace-region (match-beginning 0) (point)
-                                          kata)))))))))
+                 (replace-region-contents (match-beginning 0) (point)
+                                          kata 0)))))))))
 
 
 ;;;###autoload
@@ -260,13 +262,16 @@ of which charset is `japanese-jisx0201-kana'."
               (composition (get-char-code-property kata 'kana-composition))
               slot) ;; next
          (if (and composition (setq slot (assq (following-char) composition)))
-             (japanese-replace-region (match-beginning 0) (1+ (point))
-                                      (get-char-code-property
-                                       (cdr slot) 'hiragana))
+             (progn
+               (goto-char (1+ (point)))
+               (replace-region-contents (match-beginning 0) (point)
+                                        (get-char-code-property
+                                         (cdr slot) 'hiragana)
+                                        0))
            (let ((hira (get-char-code-property kata 'hiragana)))
              (if hira
-                 (japanese-replace-region (match-beginning 0) (point)
-                                          hira)))))))))
+                 (replace-region-contents (match-beginning 0) (point)
+                                          hira 0)))))))))
 
 ;;;###autoload
 (defun japanese-hankaku-region (from to &optional ascii-only)
@@ -285,8 +290,8 @@ Optional argument ASCII-ONLY non-nil means to convert only to ASCII char."
                                 (get-char-code-property zenkaku 'jisx0201))
                            (get-char-code-property zenkaku 'ascii))))
          (if hankaku
-             (japanese-replace-region (match-beginning 0) (match-end 0)
-                                      hankaku)))))))
+             (replace-region-contents (match-beginning 0) (match-end 0)
+                                      hankaku 0)))))))
 
 ;;;###autoload
 (defun japanese-zenkaku-region (from to &optional katakana-only)
@@ -307,12 +312,14 @@ Optional argument KATAKANA-ONLY non-nil means to convert only KATAKANA char."
               (composition (get-char-code-property hankaku 'kana-composition))
               slot) ;; next
          (if (and composition (setq slot (assq (following-char) composition)))
-             (japanese-replace-region (match-beginning 0) (1+ (point))
-                                      (cdr slot))
+             (progn
+               (goto-char (1+ (point)))
+               (replace-region-contents (match-beginning 0) (point)
+                                        (cdr slot) 0))
            (let ((zenkaku (japanese-zenkaku hankaku)))
              (if zenkaku
-                 (japanese-replace-region (match-beginning 0) (match-end 0)
-                                          zenkaku)))))))))
+                 (replace-region-contents (match-beginning 0) (match-end 0)
+                                          zenkaku 0)))))))))
 
 ;;;###autoload
 (defun read-hiragana-string (prompt &optional initial-input)
index 424c6ea16084cea6fd554e167a229f8cc6787c72..6d64bbea72eb2ad2dc6de6d6309a5f6c7b93481f 100644 (file)
@@ -1455,35 +1455,8 @@ Moves point to the end of the new text."
   (remove-text-properties 0 (length newtext)
                           '(face nil display nil completion--unquoted nil)
                           newtext)
-  ;; Maybe this should be in subr.el.
-  ;; You'd think this is trivial to do, but details matter if you want
-  ;; to keep markers "at the right place" and be robust in the face of
-  ;; after-change-functions that may themselves modify the buffer.
-  (let ((prefix-len 0))
-    ;; Don't touch markers in the shared prefix (if any).
-    (while (and (< prefix-len (length newtext))
-                (< (+ beg prefix-len) end)
-                (eq (char-after (+ beg prefix-len))
-                    (aref newtext prefix-len)))
-      (setq prefix-len (1+ prefix-len)))
-    (unless (zerop prefix-len)
-      (setq beg (+ beg prefix-len))
-      (setq newtext (substring newtext prefix-len))))
-  (let ((suffix-len 0))
-    ;; Don't touch markers in the shared suffix (if any).
-    (while (and (< suffix-len (length newtext))
-                (< beg (- end suffix-len))
-                (eq (char-before (- end suffix-len))
-                    (aref newtext (- (length newtext) suffix-len 1))))
-      (setq suffix-len (1+ suffix-len)))
-    (unless (zerop suffix-len)
-      (setq end (- end suffix-len))
-      (setq newtext (substring newtext 0 (- suffix-len))))
-    (goto-char beg)
-    (let ((length (- end beg)))         ;Read `end' before we insert the text.
-      (insert-and-inherit newtext)
-      (delete-region (point) (+ (point) length)))
-    (forward-char suffix-len)))
+  (replace-region-contents beg end newtext 0.1 nil 'inherit)
+  (goto-char (+ beg (length newtext))))
 
 (defvar completion-in-region-mode nil
   "This variable is obsolete and no longer used.")
@@ -3338,7 +3311,7 @@ This calls the function that `completion-in-region-function' specifies
 \(passing the same four arguments that it received) to do the work,
 and returns whatever it does.  The return value should be nil
 if there was no valid completion, else t."
-  (cl-assert (<= start (point)) (<= (point) end))
+  (cl-assert (<= start (point) end) t)
   (funcall completion-in-region-function start end collection predicate))
 
 (defcustom read-file-name-completion-ignore-case
index 844b75e60d290fcd41d9a02a3546679a129e399d..f02685460b1992b13858728134b781ee103f4371 100644 (file)
@@ -4388,8 +4388,8 @@ Comparisons and replacements are done with fixed case."
       (let ((matches 0)
             (case-fold-search nil))
         (while (search-forward string nil t)
-          (delete-region (match-beginning 0) (match-end 0))
-          (insert replacement)
+          (replace-region-contents (match-beginning 0) (match-end 0)
+                                   replacement 0)
           (setq matches (1+ matches)))
         (and (not (zerop matches))
              matches)))))