From: Tassilo Horn Date: Fri, 8 Feb 2019 19:36:00 +0000 (+0100) Subject: Add new function replace-buffer-contents X-Git-Tag: emacs-27.0.90~3644 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=61748cd78fa3e93a54b60b7568b7e319d9ea09e0;p=emacs.git 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. --- 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 diff --git a/src/editfns.c b/src/editfns.c index a9ac263dafa..7a600bacf18 100644 --- a/src/editfns.c +++ b/src/editfns.c @@ -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);