]> git.eshelyaron.com Git - emacs.git/commitdiff
Add new function replace-buffer-contents
authorTassilo Horn <tsdh@gnu.org>
Fri, 8 Feb 2019 19:36:00 +0000 (20:36 +0100)
committerTassilo Horn <tsdh@gnu.org>
Fri, 8 Feb 2019 19:36:00 +0000 (20:36 +0100)
* 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.

lisp/subr.el
src/editfns.c

index 122a0d8da4cd6d596ad12aad734280dd1bdae062..44a1c6089497abdd5b0e48ac75464896e8104cb7 100644 (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
index a9ac263dafa7c539196135672b009b57af4289c2..7a600bacf180c189e05df02f0e6b2334beee930e 100644 (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);