From: Stefan Monnier Date: Fri, 28 Mar 2025 05:01:17 +0000 (-0400) Subject: Use `replace-region-contents` to replace insert+delete X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=d49c8a0eb2521261099ecb2936009dbd26a99209;p=emacs.git Use `replace-region-contents` to replace insert+delete * 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) --- diff --git a/lisp/emacs-lisp/cl-lib.el b/lisp/emacs-lisp/cl-lib.el index b4cb1200c9a..381fbee63d3 100644 --- a/lisp/emacs-lisp/cl-lib.el +++ b/lisp/emacs-lisp/cl-lib.el @@ -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))) diff --git a/lisp/emacs-lisp/gv.el b/lisp/emacs-lisp/gv.el index d133dd0e426..0579bf6a265 100644 --- a/lisp/emacs-lisp/gv.el +++ b/lisp/emacs-lisp/gv.el @@ -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") diff --git a/lisp/help-fns.el b/lisp/help-fns.el index 2a2dd1f7262..dedafb74073 100644 --- a/lisp/help-fns.el +++ b/lisp/help-fns.el @@ -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) diff --git a/lisp/language/japan-util.el b/lisp/language/japan-util.el index 718c469d562..6fbb52b627e 100644 --- a/lisp/language/japan-util.el +++ b/lisp/language/japan-util.el @@ -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) diff --git a/lisp/minibuffer.el b/lisp/minibuffer.el index 424c6ea1608..6d64bbea72e 100644 --- a/lisp/minibuffer.el +++ b/lisp/minibuffer.el @@ -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 diff --git a/lisp/subr.el b/lisp/subr.el index 844b75e60d2..f02685460b1 100644 --- a/lisp/subr.el +++ b/lisp/subr.el @@ -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)))))