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.
This commit is contained in:
Tassilo Horn 2019-02-08 20:36:00 +01:00
parent ac1e5a5e2e
commit 61748cd78f
2 changed files with 35 additions and 1 deletions

View file

@ -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

View file

@ -1910,6 +1910,11 @@ determines whether case is significant or ignored. */)
#undef ELEMENT
#undef EQUAL
#define USE_HEURISTIC
#ifdef USE_HEURISTIC
#define DIFFSEQ_HEURISTIC
#endif
/* Counter used to rarely_quit in replace-buffer-contents. */
static unsigned short rbc_quitcounter;
@ -2017,8 +2022,11 @@ differences between the two buffers. */)
.insertions = SAFE_ALLOCA (ins_bytes),
.fdiag = buffer + size_b + 1,
.bdiag = buffer + diags + size_b + 1,
#ifdef DIFFSEQ_HEURISTIC
.heuristic = true,
#endif
/* FIXME: Find a good number for .too_expensive. */
.too_expensive = 1000000,
.too_expensive = 64,
};
memclear (ctx.deletions, del_bytes);
memclear (ctx.insertions, ins_bytes);