[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Emacs-diffs] master 61748cd 1/2: Add new function replace-buffer-conten
From: |
Tassilo Horn |
Subject: |
[Emacs-diffs] master 61748cd 1/2: Add new function replace-buffer-contents |
Date: |
Fri, 8 Feb 2019 14:42:58 -0500 (EST) |
branch: master
commit 61748cd78fa3e93a54b60b7568b7e319d9ea09e0
Author: Tassilo Horn <address@hidden>
Commit: Tassilo Horn <address@hidden>
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.
---
lisp/subr.el | 26 ++++++++++++++++++++++++++
src/editfns.c | 10 +++++++++-
2 files changed, 35 insertions(+), 1 deletion(-)
diff --git a/lisp/subr.el b/lisp/subr.el
index 122a0d8..44a1c60 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 a9ac263..7a600ba 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);