diff options
author | Tassilo Horn <tsdh@gnu.org> | 2019-02-08 20:36:00 +0100 |
---|---|---|
committer | Tassilo Horn <tsdh@gnu.org> | 2019-02-08 20:36:00 +0100 |
commit | 61748cd78fa3e93a54b60b7568b7e319d9ea09e0 (patch) | |
tree | 55461b979a5be7c1e61ad0c8996cd8d20f7f8ec7 /lisp | |
parent | ac1e5a5e2ed7c6cf5bec50e5ebf7fab6792230bd (diff) | |
download | emacs-61748cd78fa3e93a54b60b7568b7e319d9ea09e0.tar.gz |
Add new function replace-buffer-contents
* src/editfns.c (Freplace_buffer_contents): Use lower value of
too_expensive and enable heuristic.
* lisp/subr.el (replace-region-contents): New convenient wrapper
function around replace-buffer-contents.
Diffstat (limited to 'lisp')
-rw-r--r-- | lisp/subr.el | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/lisp/subr.el b/lisp/subr.el index 122a0d8da4c..44a1c608949 100644 --- a/lisp/subr.el +++ b/lisp/subr.el @@ -5476,4 +5476,30 @@ returned list are in the same order as in TREE. ;; for discoverability: (defalias 'flatten-list 'flatten-tree) +(defun replace-region-contents (beg end replace-fn) + "Replace the region between BEG and END using REPLACE-FN. +REPLACE-FN runs on the current buffer narrowed to the region. It +should return either a string or a buffer replacing the region. + +The replacement is performed using `replace-buffer-contents'. + +Note: If the replacement is a string, it'll be placed in a +temporary buffer so that `replace-buffer-contents' can operate on +it. Therefore, if you already have the replacement in a buffer, +it makes no sense to convert it to a string using +`buffer-substring' or similar." + (save-excursion + (save-restriction + (narrow-to-region beg end) + (goto-char (point-min)) + (let ((repl (funcall replace-fn))) + (if (bufferp repl) + (replace-buffer-contents repl) + (let ((source-buffer (current-buffer))) + (with-temp-buffer + (insert repl) + (let ((tmp-buffer (current-buffer))) + (set-buffer source-buffer) + (replace-buffer-contents tmp-buffer))))))))) + ;;; subr.el ends here |