aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPhilipp Stephani2017-05-07 21:01:53 +0200
committerPhilipp Stephani2017-06-17 15:40:58 +0200
commitd682f0daa3c0bfdd5ee8ce0e9226353d505e85a9 (patch)
treeef0a6de2163d2f3c3c9bcb5012875e0018ef496f
parent46279c1ea117bab75bdeccfd04703033c9e7d26d (diff)
downloademacs-d682f0daa3c0bfdd5ee8ce0e9226353d505e85a9.tar.gz
emacs-d682f0daa3c0bfdd5ee8ce0e9226353d505e85a9.zip
Add command to replace buffer contents
Add a new command 'replace-buffer-contents' that uses the Myers diff algorithm to non-destructively replace the accessible portion of the current buffer. The Myers algorithm is implemented in Gnulib. * src/editfns.c (Freplace_buffer_contents): New command. (set_bit, bit_is_set, buffer_chars_equal): New helper functions. (syms_of_editfns): Define new command. * test/src/editfns-tests.el (replace-buffer-contents-1) (replace-buffer-contents-2): New unit tests. * src/buffer.h (BUF_FETCH_CHAR_AS_MULTIBYTE): New helper macro. * admin/merge-gnulib (GNULIB_MODULES): Add diffseq.h and minmax.h.
-rwxr-xr-xadmin/merge-gnulib4
-rw-r--r--etc/NEWS5
-rw-r--r--lib/diffseq.h525
-rw-r--r--lib/gnulib.mk.in18
-rw-r--r--lib/minmax.h60
-rw-r--r--m4/gnulib-comp.m46
-rw-r--r--m4/minmax.m444
-rw-r--r--src/buffer.h9
-rw-r--r--src/editfns.c201
-rw-r--r--test/src/editfns-tests.el31
10 files changed, 900 insertions, 3 deletions
diff --git a/admin/merge-gnulib b/admin/merge-gnulib
index e5fb0f59fb3..d4bbf17cb3d 100755
--- a/admin/merge-gnulib
+++ b/admin/merge-gnulib
@@ -30,12 +30,12 @@ GNULIB_MODULES='
30 careadlinkat close-stream 30 careadlinkat close-stream
31 count-leading-zeros count-one-bits count-trailing-zeros 31 count-leading-zeros count-one-bits count-trailing-zeros
32 crypto/md5 crypto/sha1 crypto/sha256 crypto/sha512 32 crypto/md5 crypto/sha1 crypto/sha256 crypto/sha512
33 dtoastr dtotimespec dup2 environ execinfo faccessat 33 diffseq dtoastr dtotimespec dup2 environ execinfo faccessat
34 fcntl fcntl-h fdatasync fdopendir 34 fcntl fcntl-h fdatasync fdopendir
35 filemode filevercmp flexmember fstatat fsync 35 filemode filevercmp flexmember fstatat fsync
36 getloadavg getopt-gnu gettime gettimeofday gitlog-to-changelog 36 getloadavg getopt-gnu gettime gettimeofday gitlog-to-changelog
37 ignore-value intprops largefile lstat 37 ignore-value intprops largefile lstat
38 manywarnings memrchr mkostemp mktime 38 manywarnings memrchr minmax mkostemp mktime
39 pipe2 pselect pthread_sigmask putenv qcopy-acl readlink readlinkat 39 pipe2 pselect pthread_sigmask putenv qcopy-acl readlink readlinkat
40 sig2str socklen stat-time std-gnu11 stdalign stddef stdio 40 sig2str socklen stat-time std-gnu11 stdalign stddef stdio
41 stpcpy strftime strtoimax symlink sys_stat 41 stpcpy strftime strtoimax symlink sys_stat
diff --git a/etc/NEWS b/etc/NEWS
index 2fb8daab101..ab600eb2786 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -462,6 +462,11 @@ Negative prefix arg flips the direction of selection. Also,
462defun are selected unless they are separated from the defun by a blank 462defun are selected unless they are separated from the defun by a blank
463line. 463line.
464 464
465** New command 'replace-buffer-contents'. This command replaces the
466contents of theaccessible portion of the current buffer with the
467contents of the accessible portion of a different buffer while keeping
468point, mark, markers, and text properties as intact as possible.
469
465 470
466* Changes in Specialized Modes and Packages in Emacs 26.1 471* Changes in Specialized Modes and Packages in Emacs 26.1
467 472
diff --git a/lib/diffseq.h b/lib/diffseq.h
new file mode 100644
index 00000000000..d7a374357c7
--- /dev/null
+++ b/lib/diffseq.h
@@ -0,0 +1,525 @@
1/* Analyze differences between two vectors.
2
3 Copyright (C) 1988-1989, 1992-1995, 2001-2004, 2006-2017 Free Software
4 Foundation, Inc.
5
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18
19
20/* The basic idea is to consider two vectors as similar if, when
21 transforming the first vector into the second vector through a
22 sequence of edits (inserts and deletes of one element each),
23 this sequence is short - or equivalently, if the ordered list
24 of elements that are untouched by these edits is long. For a
25 good introduction to the subject, read about the "Levenshtein
26 distance" in Wikipedia.
27
28 The basic algorithm is described in:
29 "An O(ND) Difference Algorithm and its Variations", Eugene W. Myers,
30 Algorithmica Vol. 1, 1986, pp. 251-266,
31 <http://dx.doi.org/10.1007/BF01840446>.
32 See especially section 4.2, which describes the variation used below.
33
34 The basic algorithm was independently discovered as described in:
35 "Algorithms for Approximate String Matching", Esko Ukkonen,
36 Information and Control Vol. 64, 1985, pp. 100-118,
37 <http://dx.doi.org/10.1016/S0019-9958(85)80046-2>.
38
39 Unless the 'find_minimal' flag is set, this code uses the TOO_EXPENSIVE
40 heuristic, by Paul Eggert, to limit the cost to O(N**1.5 log N)
41 at the price of producing suboptimal output for large inputs with
42 many differences. */
43
44/* Before including this file, you need to define:
45 ELEMENT The element type of the vectors being compared.
46 EQUAL A two-argument macro that tests two elements for
47 equality.
48 OFFSET A signed integer type sufficient to hold the
49 difference between two indices. Usually
50 something like ptrdiff_t.
51 EXTRA_CONTEXT_FIELDS Declarations of fields for 'struct context'.
52 NOTE_DELETE(ctxt, xoff) Record the removal of the object xvec[xoff].
53 NOTE_INSERT(ctxt, yoff) Record the insertion of the object yvec[yoff].
54 EARLY_ABORT(ctxt) (Optional) A boolean expression that triggers an
55 early abort of the computation.
56 USE_HEURISTIC (Optional) Define if you want to support the
57 heuristic for large vectors.
58 It is also possible to use this file with abstract arrays. In this case,
59 xvec and yvec are not represented in memory. They only exist conceptually.
60 In this case, the list of defines above is amended as follows:
61 ELEMENT Undefined.
62 EQUAL Undefined.
63 XVECREF_YVECREF_EQUAL(ctxt, xoff, yoff)
64 A three-argument macro: References xvec[xoff] and
65 yvec[yoff] and tests these elements for equality.
66 Before including this file, you also need to include:
67 #include <limits.h>
68 #include <stdbool.h>
69 #include "minmax.h"
70 */
71
72/* Maximum value of type OFFSET. */
73#define OFFSET_MAX \
74 ((((OFFSET)1 << (sizeof (OFFSET) * CHAR_BIT - 2)) - 1) * 2 + 1)
75
76/* Default to no early abort. */
77#ifndef EARLY_ABORT
78# define EARLY_ABORT(ctxt) false
79#endif
80
81/* Use this to suppress gcc's "...may be used before initialized" warnings.
82 Beware: The Code argument must not contain commas. */
83#ifndef IF_LINT
84# if defined GCC_LINT || defined lint
85# define IF_LINT(Code) Code
86# else
87# define IF_LINT(Code) /* empty */
88# endif
89#endif
90
91/* As above, but when Code must contain one comma. */
92#ifndef IF_LINT2
93# if defined GCC_LINT || defined lint
94# define IF_LINT2(Code1, Code2) Code1, Code2
95# else
96# define IF_LINT2(Code1, Code2) /* empty */
97# endif
98#endif
99
100/*
101 * Context of comparison operation.
102 */
103struct context
104{
105 #ifdef ELEMENT
106 /* Vectors being compared. */
107 ELEMENT const *xvec;
108 ELEMENT const *yvec;
109 #endif
110
111 /* Extra fields. */
112 EXTRA_CONTEXT_FIELDS
113
114 /* Vector, indexed by diagonal, containing 1 + the X coordinate of the point
115 furthest along the given diagonal in the forward search of the edit
116 matrix. */
117 OFFSET *fdiag;
118
119 /* Vector, indexed by diagonal, containing the X coordinate of the point
120 furthest along the given diagonal in the backward search of the edit
121 matrix. */
122 OFFSET *bdiag;
123
124 #ifdef USE_HEURISTIC
125 /* This corresponds to the diff --speed-large-files flag. With this
126 heuristic, for vectors with a constant small density of changes,
127 the algorithm is linear in the vector size. */
128 bool heuristic;
129 #endif
130
131 /* Edit scripts longer than this are too expensive to compute. */
132 OFFSET too_expensive;
133
134 /* Snakes bigger than this are considered "big". */
135 #define SNAKE_LIMIT 20
136};
137
138struct partition
139{
140 /* Midpoints of this partition. */
141 OFFSET xmid;
142 OFFSET ymid;
143
144 /* True if low half will be analyzed minimally. */
145 bool lo_minimal;
146
147 /* Likewise for high half. */
148 bool hi_minimal;
149};
150
151
152/* Find the midpoint of the shortest edit script for a specified portion
153 of the two vectors.
154
155 Scan from the beginnings of the vectors, and simultaneously from the ends,
156 doing a breadth-first search through the space of edit-sequence.
157 When the two searches meet, we have found the midpoint of the shortest
158 edit sequence.
159
160 If FIND_MINIMAL is true, find the minimal edit script regardless of
161 expense. Otherwise, if the search is too expensive, use heuristics to
162 stop the search and report a suboptimal answer.
163
164 Set PART->(xmid,ymid) to the midpoint (XMID,YMID). The diagonal number
165 XMID - YMID equals the number of inserted elements minus the number
166 of deleted elements (counting only elements before the midpoint).
167
168 Set PART->lo_minimal to true iff the minimal edit script for the
169 left half of the partition is known; similarly for PART->hi_minimal.
170
171 This function assumes that the first elements of the specified portions
172 of the two vectors do not match, and likewise that the last elements do not
173 match. The caller must trim matching elements from the beginning and end
174 of the portions it is going to specify.
175
176 If we return the "wrong" partitions, the worst this can do is cause
177 suboptimal diff output. It cannot cause incorrect diff output. */
178
179static void
180diag (OFFSET xoff, OFFSET xlim, OFFSET yoff, OFFSET ylim, bool find_minimal,
181 struct partition *part, struct context *ctxt)
182{
183 OFFSET *const fd = ctxt->fdiag; /* Give the compiler a chance. */
184 OFFSET *const bd = ctxt->bdiag; /* Additional help for the compiler. */
185#ifdef ELEMENT
186 ELEMENT const *const xv = ctxt->xvec; /* Still more help for the compiler. */
187 ELEMENT const *const yv = ctxt->yvec; /* And more and more . . . */
188 #define XREF_YREF_EQUAL(x,y) EQUAL (xv[x], yv[y])
189#else
190 #define XREF_YREF_EQUAL(x,y) XVECREF_YVECREF_EQUAL (ctxt, x, y)
191#endif
192 const OFFSET dmin = xoff - ylim; /* Minimum valid diagonal. */
193 const OFFSET dmax = xlim - yoff; /* Maximum valid diagonal. */
194 const OFFSET fmid = xoff - yoff; /* Center diagonal of top-down search. */
195 const OFFSET bmid = xlim - ylim; /* Center diagonal of bottom-up search. */
196 OFFSET fmin = fmid;
197 OFFSET fmax = fmid; /* Limits of top-down search. */
198 OFFSET bmin = bmid;
199 OFFSET bmax = bmid; /* Limits of bottom-up search. */
200 OFFSET c; /* Cost. */
201 bool odd = (fmid - bmid) & 1; /* True if southeast corner is on an odd
202 diagonal with respect to the northwest. */
203
204 fd[fmid] = xoff;
205 bd[bmid] = xlim;
206
207 for (c = 1;; ++c)
208 {
209 OFFSET d; /* Active diagonal. */
210 bool big_snake = false;
211
212 /* Extend the top-down search by an edit step in each diagonal. */
213 if (fmin > dmin)
214 fd[--fmin - 1] = -1;
215 else
216 ++fmin;
217 if (fmax < dmax)
218 fd[++fmax + 1] = -1;
219 else
220 --fmax;
221 for (d = fmax; d >= fmin; d -= 2)
222 {
223 OFFSET x;
224 OFFSET y;
225 OFFSET tlo = fd[d - 1];
226 OFFSET thi = fd[d + 1];
227 OFFSET x0 = tlo < thi ? thi : tlo + 1;
228
229 for (x = x0, y = x0 - d;
230 x < xlim && y < ylim && XREF_YREF_EQUAL (x, y);
231 x++, y++)
232 continue;
233 if (x - x0 > SNAKE_LIMIT)
234 big_snake = true;
235 fd[d] = x;
236 if (odd && bmin <= d && d <= bmax && bd[d] <= x)
237 {
238 part->xmid = x;
239 part->ymid = y;
240 part->lo_minimal = part->hi_minimal = true;
241 return;
242 }
243 }
244
245 /* Similarly extend the bottom-up search. */
246 if (bmin > dmin)
247 bd[--bmin - 1] = OFFSET_MAX;
248 else
249 ++bmin;
250 if (bmax < dmax)
251 bd[++bmax + 1] = OFFSET_MAX;
252 else
253 --bmax;
254 for (d = bmax; d >= bmin; d -= 2)
255 {
256 OFFSET x;
257 OFFSET y;
258 OFFSET tlo = bd[d - 1];
259 OFFSET thi = bd[d + 1];
260 OFFSET x0 = tlo < thi ? tlo : thi - 1;
261
262 for (x = x0, y = x0 - d;
263 xoff < x && yoff < y && XREF_YREF_EQUAL (x - 1, y - 1);
264 x--, y--)
265 continue;
266 if (x0 - x > SNAKE_LIMIT)
267 big_snake = true;
268 bd[d] = x;
269 if (!odd && fmin <= d && d <= fmax && x <= fd[d])
270 {
271 part->xmid = x;
272 part->ymid = y;
273 part->lo_minimal = part->hi_minimal = true;
274 return;
275 }
276 }
277
278 if (find_minimal)
279 continue;
280
281#ifdef USE_HEURISTIC
282 /* Heuristic: check occasionally for a diagonal that has made lots
283 of progress compared with the edit distance. If we have any
284 such, find the one that has made the most progress and return it
285 as if it had succeeded.
286
287 With this heuristic, for vectors with a constant small density
288 of changes, the algorithm is linear in the vector size. */
289
290 if (200 < c && big_snake && ctxt->heuristic)
291 {
292 {
293 OFFSET best = 0;
294
295 for (d = fmax; d >= fmin; d -= 2)
296 {
297 OFFSET dd = d - fmid;
298 OFFSET x = fd[d];
299 OFFSET y = x - d;
300 OFFSET v = (x - xoff) * 2 - dd;
301
302 if (v > 12 * (c + (dd < 0 ? -dd : dd)))
303 {
304 if (v > best
305 && xoff + SNAKE_LIMIT <= x && x < xlim
306 && yoff + SNAKE_LIMIT <= y && y < ylim)
307 {
308 /* We have a good enough best diagonal; now insist
309 that it end with a significant snake. */
310 int k;
311
312 for (k = 1; XREF_YREF_EQUAL (x - k, y - k); k++)
313 if (k == SNAKE_LIMIT)
314 {
315 best = v;
316 part->xmid = x;
317 part->ymid = y;
318 break;
319 }
320 }
321 }
322 }
323 if (best > 0)
324 {
325 part->lo_minimal = true;
326 part->hi_minimal = false;
327 return;
328 }
329 }
330
331 {
332 OFFSET best = 0;
333
334 for (d = bmax; d >= bmin; d -= 2)
335 {
336 OFFSET dd = d - bmid;
337 OFFSET x = bd[d];
338 OFFSET y = x - d;
339 OFFSET v = (xlim - x) * 2 + dd;
340
341 if (v > 12 * (c + (dd < 0 ? -dd : dd)))
342 {
343 if (v > best
344 && xoff < x && x <= xlim - SNAKE_LIMIT
345 && yoff < y && y <= ylim - SNAKE_LIMIT)
346 {
347 /* We have a good enough best diagonal; now insist
348 that it end with a significant snake. */
349 int k;
350
351 for (k = 0; XREF_YREF_EQUAL (x + k, y + k); k++)
352 if (k == SNAKE_LIMIT - 1)
353 {
354 best = v;
355 part->xmid = x;
356 part->ymid = y;
357 break;
358 }
359 }
360 }
361 }
362 if (best > 0)
363 {
364 part->lo_minimal = false;
365 part->hi_minimal = true;
366 return;
367 }
368 }
369 }
370#endif /* USE_HEURISTIC */
371
372 /* Heuristic: if we've gone well beyond the call of duty, give up
373 and report halfway between our best results so far. */
374 if (c >= ctxt->too_expensive)
375 {
376 OFFSET fxybest;
377 OFFSET fxbest IF_LINT (= 0);
378 OFFSET bxybest;
379 OFFSET bxbest IF_LINT (= 0);
380
381 /* Find forward diagonal that maximizes X + Y. */
382 fxybest = -1;
383 for (d = fmax; d >= fmin; d -= 2)
384 {
385 OFFSET x = MIN (fd[d], xlim);
386 OFFSET y = x - d;
387 if (ylim < y)
388 {
389 x = ylim + d;
390 y = ylim;
391 }
392 if (fxybest < x + y)
393 {
394 fxybest = x + y;
395 fxbest = x;
396 }
397 }
398
399 /* Find backward diagonal that minimizes X + Y. */
400 bxybest = OFFSET_MAX;
401 for (d = bmax; d >= bmin; d -= 2)
402 {
403 OFFSET x = MAX (xoff, bd[d]);
404 OFFSET y = x - d;
405 if (y < yoff)
406 {
407 x = yoff + d;
408 y = yoff;
409 }
410 if (x + y < bxybest)
411 {
412 bxybest = x + y;
413 bxbest = x;
414 }
415 }
416
417 /* Use the better of the two diagonals. */
418 if ((xlim + ylim) - bxybest < fxybest - (xoff + yoff))
419 {
420 part->xmid = fxbest;
421 part->ymid = fxybest - fxbest;
422 part->lo_minimal = true;
423 part->hi_minimal = false;
424 }
425 else
426 {
427 part->xmid = bxbest;
428 part->ymid = bxybest - bxbest;
429 part->lo_minimal = false;
430 part->hi_minimal = true;
431 }
432 return;
433 }
434 }
435 #undef XREF_YREF_EQUAL
436}
437
438
439/* Compare in detail contiguous subsequences of the two vectors
440 which are known, as a whole, to match each other.
441
442 The subsequence of vector 0 is [XOFF, XLIM) and likewise for vector 1.
443
444 Note that XLIM, YLIM are exclusive bounds. All indices into the vectors
445 are origin-0.
446
447 If FIND_MINIMAL, find a minimal difference no matter how
448 expensive it is.
449
450 The results are recorded by invoking NOTE_DELETE and NOTE_INSERT.
451
452 Return false if terminated normally, or true if terminated through early
453 abort. */
454
455static bool
456compareseq (OFFSET xoff, OFFSET xlim, OFFSET yoff, OFFSET ylim,
457 bool find_minimal, struct context *ctxt)
458{
459#ifdef ELEMENT
460 ELEMENT const *xv = ctxt->xvec; /* Help the compiler. */
461 ELEMENT const *yv = ctxt->yvec;
462 #define XREF_YREF_EQUAL(x,y) EQUAL (xv[x], yv[y])
463#else
464 #define XREF_YREF_EQUAL(x,y) XVECREF_YVECREF_EQUAL (ctxt, x, y)
465#endif
466
467 /* Slide down the bottom initial diagonal. */
468 while (xoff < xlim && yoff < ylim && XREF_YREF_EQUAL (xoff, yoff))
469 {
470 xoff++;
471 yoff++;
472 }
473
474 /* Slide up the top initial diagonal. */
475 while (xoff < xlim && yoff < ylim && XREF_YREF_EQUAL (xlim - 1, ylim - 1))
476 {
477 xlim--;
478 ylim--;
479 }
480
481 /* Handle simple cases. */
482 if (xoff == xlim)
483 while (yoff < ylim)
484 {
485 NOTE_INSERT (ctxt, yoff);
486 if (EARLY_ABORT (ctxt))
487 return true;
488 yoff++;
489 }
490 else if (yoff == ylim)
491 while (xoff < xlim)
492 {
493 NOTE_DELETE (ctxt, xoff);
494 if (EARLY_ABORT (ctxt))
495 return true;
496 xoff++;
497 }
498 else
499 {
500 struct partition part IF_LINT2 (= { .xmid = 0, .ymid = 0 });
501
502 /* Find a point of correspondence in the middle of the vectors. */
503 diag (xoff, xlim, yoff, ylim, find_minimal, &part, ctxt);
504
505 /* Use the partitions to split this problem into subproblems. */
506 if (compareseq (xoff, part.xmid, yoff, part.ymid, part.lo_minimal, ctxt))
507 return true;
508 if (compareseq (part.xmid, xlim, part.ymid, ylim, part.hi_minimal, ctxt))
509 return true;
510 }
511
512 return false;
513 #undef XREF_YREF_EQUAL
514}
515
516#undef ELEMENT
517#undef EQUAL
518#undef OFFSET
519#undef EXTRA_CONTEXT_FIELDS
520#undef NOTE_DELETE
521#undef NOTE_INSERT
522#undef EARLY_ABORT
523#undef USE_HEURISTIC
524#undef XVECREF_YVECREF_EQUAL
525#undef OFFSET_MAX
diff --git a/lib/gnulib.mk.in b/lib/gnulib.mk.in
index 73d304307d4..509089e6391 100644
--- a/lib/gnulib.mk.in
+++ b/lib/gnulib.mk.in
@@ -21,7 +21,7 @@
21# the same distribution terms as the rest of that program. 21# the same distribution terms as the rest of that program.
22# 22#
23# Generated by gnulib-tool. 23# Generated by gnulib-tool.
24# Reproduce by: gnulib-tool --import --lib=libgnu --source-base=lib --m4-base=m4 --doc-base=doc --tests-base=tests --aux-dir=build-aux --avoid=close --avoid=dup --avoid=fchdir --avoid=fstat --avoid=malloc-posix --avoid=msvc-inval --avoid=msvc-nothrow --avoid=open --avoid=openat-die --avoid=opendir --avoid=raise --avoid=save-cwd --avoid=select --avoid=setenv --avoid=sigprocmask --avoid=stat --avoid=stdarg --avoid=stdbool --avoid=threadlib --avoid=tzset --avoid=unsetenv --avoid=utime --avoid=utime-h --gnu-make --makefile-name=gnulib.mk.in --conditional-dependencies --no-libtool --macro-prefix=gl --no-vc-files alloca-opt binary-io byteswap c-ctype c-strcase careadlinkat close-stream count-leading-zeros count-one-bits count-trailing-zeros crypto/md5 crypto/sha1 crypto/sha256 crypto/sha512 dtoastr dtotimespec dup2 environ execinfo faccessat fcntl fcntl-h fdatasync fdopendir filemode filevercmp flexmember fstatat fsync getloadavg getopt-gnu gettime gettimeofday gitlog-to-changelog ignore-value intprops largefile lstat manywarnings memrchr mkostemp mktime pipe2 pselect pthread_sigmask putenv qcopy-acl readlink readlinkat sig2str socklen stat-time std-gnu11 stdalign stddef stdio stpcpy strftime strtoimax symlink sys_stat sys_time time time_r time_rz timegm timer-time timespec-add timespec-sub update-copyright utimens vla warnings 24# Reproduce by: gnulib-tool --import --lib=libgnu --source-base=lib --m4-base=m4 --doc-base=doc --tests-base=tests --aux-dir=build-aux --avoid=close --avoid=dup --avoid=fchdir --avoid=fstat --avoid=malloc-posix --avoid=msvc-inval --avoid=msvc-nothrow --avoid=open --avoid=openat-die --avoid=opendir --avoid=raise --avoid=save-cwd --avoid=select --avoid=setenv --avoid=sigprocmask --avoid=stat --avoid=stdarg --avoid=stdbool --avoid=threadlib --avoid=tzset --avoid=unsetenv --avoid=utime --avoid=utime-h --gnu-make --makefile-name=gnulib.mk.in --conditional-dependencies --no-libtool --macro-prefix=gl --no-vc-files alloca-opt binary-io byteswap c-ctype c-strcase careadlinkat close-stream count-leading-zeros count-one-bits count-trailing-zeros crypto/md5 crypto/sha1 crypto/sha256 crypto/sha512 diffseq dtoastr dtotimespec dup2 environ execinfo faccessat fcntl fcntl-h fdatasync fdopendir filemode filevercmp flexmember fstatat fsync getloadavg getopt-gnu gettime gettimeofday gitlog-to-changelog ignore-value intprops largefile lstat manywarnings memrchr minmax mkostemp mktime pipe2 pselect pthread_sigmask putenv qcopy-acl readlink readlinkat sig2str socklen stat-time std-gnu11 stdalign stddef stdio stpcpy strftime strtoimax symlink sys_stat sys_time time time_r time_rz timegm timer-time timespec-add timespec-sub update-copyright utimens vla warnings
25 25
26 26
27MOSTLYCLEANFILES += core *.stackdump 27MOSTLYCLEANFILES += core *.stackdump
@@ -1167,6 +1167,14 @@ EXTRA_DIST += gl_openssl.h sha512.h
1167endif 1167endif
1168## end gnulib module crypto/sha512 1168## end gnulib module crypto/sha512
1169 1169
1170## begin gnulib module diffseq
1171ifeq (,$(OMIT_GNULIB_MODULE_diffseq))
1172
1173libgnu_a_SOURCES += diffseq.h
1174
1175endif
1176## end gnulib module diffseq
1177
1170## begin gnulib module dirent 1178## begin gnulib module dirent
1171ifeq (,$(OMIT_GNULIB_MODULE_dirent)) 1179ifeq (,$(OMIT_GNULIB_MODULE_dirent))
1172 1180
@@ -1747,6 +1755,14 @@ EXTRA_libgnu_a_SOURCES += memrchr.c
1747endif 1755endif
1748## end gnulib module memrchr 1756## end gnulib module memrchr
1749 1757
1758## begin gnulib module minmax
1759ifeq (,$(OMIT_GNULIB_MODULE_minmax))
1760
1761libgnu_a_SOURCES += minmax.h
1762
1763endif
1764## end gnulib module minmax
1765
1750## begin gnulib module mkostemp 1766## begin gnulib module mkostemp
1751ifeq (,$(OMIT_GNULIB_MODULE_mkostemp)) 1767ifeq (,$(OMIT_GNULIB_MODULE_mkostemp))
1752 1768
diff --git a/lib/minmax.h b/lib/minmax.h
new file mode 100644
index 00000000000..6b602a94fdb
--- /dev/null
+++ b/lib/minmax.h
@@ -0,0 +1,60 @@
1/* MIN, MAX macros.
2 Copyright (C) 1995, 1998, 2001, 2003, 2005, 2009-2017 Free Software
3 Foundation, Inc.
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3, or (at your option)
8 any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, see <http://www.gnu.org/licenses/>. */
17
18#ifndef _MINMAX_H
19#define _MINMAX_H
20
21/* Note: MIN, MAX are also defined in <sys/param.h> on some systems
22 (glibc, IRIX, HP-UX, OSF/1). Therefore you might get warnings about
23 MIN, MAX macro redefinitions on some systems; the workaround is to
24 #include this file as the last one among the #include list. */
25
26/* Before we define the following symbols we get the <limits.h> file
27 since otherwise we get redefinitions on some systems if <limits.h> is
28 included after this file. Likewise for <sys/param.h>.
29 If more than one of these system headers define MIN and MAX, pick just
30 one of the headers (because the definitions most likely are the same). */
31#if HAVE_MINMAX_IN_LIMITS_H
32# include <limits.h>
33#elif HAVE_MINMAX_IN_SYS_PARAM_H
34# include <sys/param.h>
35#endif
36
37/* Note: MIN and MAX should be used with two arguments of the
38 same type. They might not return the minimum and maximum of their two
39 arguments, if the arguments have different types or have unusual
40 floating-point values. For example, on a typical host with 32-bit 'int',
41 64-bit 'long long', and 64-bit IEEE 754 'double' types:
42
43 MAX (-1, 2147483648) returns 4294967295.
44 MAX (9007199254740992.0, 9007199254740993) returns 9007199254740992.0.
45 MAX (NaN, 0.0) returns 0.0.
46 MAX (+0.0, -0.0) returns -0.0.
47
48 and in each case the answer is in some sense bogus. */
49
50/* MAX(a,b) returns the maximum of A and B. */
51#ifndef MAX
52# define MAX(a,b) ((a) > (b) ? (a) : (b))
53#endif
54
55/* MIN(a,b) returns the minimum of A and B. */
56#ifndef MIN
57# define MIN(a,b) ((a) < (b) ? (a) : (b))
58#endif
59
60#endif /* _MINMAX_H */
diff --git a/m4/gnulib-comp.m4 b/m4/gnulib-comp.m4
index 8f53a990e34..1ac58e871cc 100644
--- a/m4/gnulib-comp.m4
+++ b/m4/gnulib-comp.m4
@@ -61,6 +61,7 @@ AC_DEFUN([gl_EARLY],
61 # Code from module crypto/sha1: 61 # Code from module crypto/sha1:
62 # Code from module crypto/sha256: 62 # Code from module crypto/sha256:
63 # Code from module crypto/sha512: 63 # Code from module crypto/sha512:
64 # Code from module diffseq:
64 # Code from module dirent: 65 # Code from module dirent:
65 # Code from module dirfd: 66 # Code from module dirfd:
66 # Code from module dosname: 67 # Code from module dosname:
@@ -105,6 +106,7 @@ AC_DEFUN([gl_EARLY],
105 # Code from module lstat: 106 # Code from module lstat:
106 # Code from module manywarnings: 107 # Code from module manywarnings:
107 # Code from module memrchr: 108 # Code from module memrchr:
109 # Code from module minmax:
108 # Code from module mkostemp: 110 # Code from module mkostemp:
109 # Code from module mktime: 111 # Code from module mktime:
110 # Code from module mktime-internal: 112 # Code from module mktime-internal:
@@ -289,6 +291,7 @@ AC_DEFUN([gl_INIT],
289 gl_PREREQ_MEMRCHR 291 gl_PREREQ_MEMRCHR
290 fi 292 fi
291 gl_STRING_MODULE_INDICATOR([memrchr]) 293 gl_STRING_MODULE_INDICATOR([memrchr])
294 gl_MINMAX
292 gl_FUNC_MKOSTEMP 295 gl_FUNC_MKOSTEMP
293 if test $HAVE_MKOSTEMP = 0; then 296 if test $HAVE_MKOSTEMP = 0; then
294 AC_LIBOBJ([mkostemp]) 297 AC_LIBOBJ([mkostemp])
@@ -821,6 +824,7 @@ AC_DEFUN([gl_FILE_LIST], [
821 lib/count-one-bits.h 824 lib/count-one-bits.h
822 lib/count-trailing-zeros.c 825 lib/count-trailing-zeros.c
823 lib/count-trailing-zeros.h 826 lib/count-trailing-zeros.h
827 lib/diffseq.h
824 lib/dirent.in.h 828 lib/dirent.in.h
825 lib/dirfd.c 829 lib/dirfd.c
826 lib/dosname.h 830 lib/dosname.h
@@ -875,6 +879,7 @@ AC_DEFUN([gl_FILE_LIST], [
875 lib/md5.c 879 lib/md5.c
876 lib/md5.h 880 lib/md5.h
877 lib/memrchr.c 881 lib/memrchr.c
882 lib/minmax.h
878 lib/mkostemp.c 883 lib/mkostemp.c
879 lib/mktime-internal.h 884 lib/mktime-internal.h
880 lib/mktime.c 885 lib/mktime.c
@@ -991,6 +996,7 @@ AC_DEFUN([gl_FILE_LIST], [
991 m4/manywarnings.m4 996 m4/manywarnings.m4
992 m4/md5.m4 997 m4/md5.m4
993 m4/memrchr.m4 998 m4/memrchr.m4
999 m4/minmax.m4
994 m4/mkostemp.m4 1000 m4/mkostemp.m4
995 m4/mktime.m4 1001 m4/mktime.m4
996 m4/multiarch.m4 1002 m4/multiarch.m4
diff --git a/m4/minmax.m4 b/m4/minmax.m4
new file mode 100644
index 00000000000..6845fce89c4
--- /dev/null
+++ b/m4/minmax.m4
@@ -0,0 +1,44 @@
1# minmax.m4 serial 4
2dnl Copyright (C) 2005, 2009-2017 Free Software Foundation, Inc.
3dnl This file is free software; the Free Software Foundation
4dnl gives unlimited permission to copy and/or distribute it,
5dnl with or without modifications, as long as this notice is preserved.
6
7AC_PREREQ([2.53])
8
9AC_DEFUN([gl_MINMAX],
10[
11 AC_REQUIRE([gl_PREREQ_MINMAX])
12])
13
14# Prerequisites of lib/minmax.h.
15AC_DEFUN([gl_PREREQ_MINMAX],
16[
17 gl_MINMAX_IN_HEADER([limits.h])
18 gl_MINMAX_IN_HEADER([sys/param.h])
19])
20
21dnl gl_MINMAX_IN_HEADER(HEADER)
22dnl The parameter has to be a literal header name; it cannot be macro,
23dnl nor a shell variable. (Because autoheader collects only AC_DEFINE
24dnl invocations with a literal macro name.)
25AC_DEFUN([gl_MINMAX_IN_HEADER],
26[
27 m4_pushdef([header], AS_TR_SH([$1]))
28 m4_pushdef([HEADER], AS_TR_CPP([$1]))
29 AC_CACHE_CHECK([whether <$1> defines MIN and MAX],
30 [gl_cv_minmax_in_]header,
31 [AC_COMPILE_IFELSE(
32 [AC_LANG_PROGRAM(
33 [[#include <$1>
34 int x = MIN (42, 17);]],
35 [[]])],
36 [gl_cv_minmax_in_]header[=yes],
37 [gl_cv_minmax_in_]header[=no])])
38 if test $gl_cv_minmax_in_[]header = yes; then
39 AC_DEFINE([HAVE_MINMAX_IN_]HEADER, 1,
40 [Define to 1 if <$1> defines the MIN and MAX macros.])
41 fi
42 m4_popdef([HEADER])
43 m4_popdef([header])
44])
diff --git a/src/buffer.h b/src/buffer.h
index a2bdc4e7294..be270fe4823 100644
--- a/src/buffer.h
+++ b/src/buffer.h
@@ -412,6 +412,15 @@ extern void enlarge_buffer_text (struct buffer *, ptrdiff_t);
412 ? BUF_FETCH_MULTIBYTE_CHAR ((buf), (pos)) \ 412 ? BUF_FETCH_MULTIBYTE_CHAR ((buf), (pos)) \
413 : BUF_FETCH_BYTE ((buf), (pos))) 413 : BUF_FETCH_BYTE ((buf), (pos)))
414 414
415/* Return character at byte position POS in buffer BUF. If BUF is
416 unibyte and the character is not ASCII, make the returning
417 character multibyte. */
418
419#define BUF_FETCH_CHAR_AS_MULTIBYTE(buf, pos) \
420 (! NILP (BVAR ((buf), enable_multibyte_characters)) \
421 ? BUF_FETCH_MULTIBYTE_CHAR ((buf), (pos)) \
422 : UNIBYTE_TO_CHAR (BUF_FETCH_BYTE ((buf), (pos))))
423
415/* Return the byte at byte position N in buffer BUF. */ 424/* Return the byte at byte position N in buffer BUF. */
416 425
417#define BUF_FETCH_BYTE(buf, n) \ 426#define BUF_FETCH_BYTE(buf, n) \
diff --git a/src/editfns.c b/src/editfns.c
index 43b17f9f116..76b4aaf81bc 100644
--- a/src/editfns.c
+++ b/src/editfns.c
@@ -3105,6 +3105,206 @@ determines whether case is significant or ignored. */)
3105 /* Same length too => they are equal. */ 3105 /* Same length too => they are equal. */
3106 return make_number (0); 3106 return make_number (0);
3107} 3107}
3108
3109
3110/* Set up necessary definitions for diffseq.h; see comments in
3111 diffseq.h for explanation. */
3112
3113#undef ELEMENT
3114#undef EQUAL
3115
3116#define XVECREF_YVECREF_EQUAL(ctx, xoff, yoff) \
3117 buffer_chars_equal ((ctx), (xoff), (yoff))
3118
3119#define OFFSET ptrdiff_t
3120
3121#define EXTRA_CONTEXT_FIELDS \
3122 /* Buffers to compare. */ \
3123 struct buffer *buffer_a; \
3124 struct buffer *buffer_b; \
3125 /* Bit vectors recording for each character whether it was deleted
3126 or inserted. */ \
3127 unsigned char *deletions; \
3128 unsigned char *insertions;
3129
3130#define NOTE_DELETE(ctx, xoff) set_bit ((ctx)->deletions, (xoff))
3131#define NOTE_INSERT(ctx, yoff) set_bit ((ctx)->insertions, (yoff))
3132
3133struct context;
3134static void set_bit (unsigned char *, OFFSET);
3135static bool bit_is_set (const unsigned char *, OFFSET);
3136static bool buffer_chars_equal (struct context *, OFFSET, OFFSET);
3137
3138#include "minmax.h"
3139#include "diffseq.h"
3140
3141DEFUN ("replace-buffer-contents", Freplace_buffer_contents,
3142 Sreplace_buffer_contents, 1, 1, "bSource buffer: ",
3143 doc: /* Replace accessible portion of the current buffer with accessible portion of SOURCE.
3144As far as possible the replacement is non-destructive, i.e. existing
3145buffer contents, markers, properties, and overlays in the current
3146buffer stay intact. */)
3147 (Lisp_Object source)
3148{
3149 struct buffer *a = current_buffer;
3150 Lisp_Object source_buffer = Fget_buffer (source);
3151 if (NILP (source_buffer))
3152 nsberror (source);
3153 struct buffer *b = XBUFFER (source_buffer);
3154 if (! BUFFER_LIVE_P (b))
3155 error ("Selecting deleted buffer");
3156 if (a == b)
3157 error ("Cannot replace a buffer with itself");
3158
3159 ptrdiff_t min_a = BEGV;
3160 ptrdiff_t min_b = BUF_BEGV (b);
3161 ptrdiff_t size_a = ZV - min_a;
3162 ptrdiff_t size_b = BUF_ZV (b) - min_b;
3163 eassume (size_a >= 0);
3164 eassume (size_b >= 0);
3165 bool a_empty = size_a == 0;
3166 bool b_empty = size_b == 0;
3167
3168 /* Handle trivial cases where at least one accessible portion is
3169 empty. */
3170
3171 if (a_empty && b_empty)
3172 return Qnil;
3173
3174 if (a_empty)
3175 return Finsert_buffer_substring (source, Qnil, Qnil);
3176
3177 if (b_empty)
3178 {
3179 del_range_both (BEGV, BEGV_BYTE, ZV, ZV_BYTE, true);
3180 return Qnil;
3181 }
3182
3183 /* FIXME: It is not documented how to initialize the contents of the
3184 context structure. This code cargo-cults from the existing
3185 caller in src/analyze.c of GNU Diffutils, which appears to
3186 work. */
3187
3188 ptrdiff_t diags = size_a + size_b + 3;
3189 ptrdiff_t *buffer;
3190 USE_SAFE_ALLOCA;
3191 SAFE_NALLOCA (buffer, 2, diags);
3192 /* Micro-optimization: Casting to size_t generates much better
3193 code. */
3194 ptrdiff_t del_bytes = (size_t) size_a / CHAR_BIT + 1;
3195 ptrdiff_t ins_bytes = (size_t) size_b / CHAR_BIT + 1;
3196 struct context ctx = {
3197 .buffer_a = a,
3198 .buffer_b = b,
3199 .deletions = SAFE_ALLOCA (del_bytes),
3200 .insertions = SAFE_ALLOCA (ins_bytes),
3201 .fdiag = buffer + size_b + 1,
3202 .bdiag = buffer + diags + size_b + 1,
3203 /* FIXME: Find a good number for .too_expensive. */
3204 .too_expensive = 1000000,
3205 };
3206 memclear (ctx.deletions, del_bytes);
3207 memclear (ctx.insertions, ins_bytes);
3208 /* compareseq requires indices to be zero-based. We add BEGV back
3209 later. */
3210 bool early_abort = compareseq (0, size_a, 0, size_b, false, &ctx);
3211 /* Since we didn’t define EARLY_ABORT, we should never abort
3212 early. */
3213 eassert (! early_abort);
3214 SAFE_FREE ();
3215
3216 Fundo_boundary ();
3217 ptrdiff_t count = SPECPDL_INDEX ();
3218 record_unwind_protect (save_excursion_restore, save_excursion_save ());
3219
3220 SET_PT_BOTH (BEGV, BEGV_BYTE);
3221 ptrdiff_t i = size_a;
3222 ptrdiff_t j = size_b;
3223 /* Walk backwards through the lists of changes. This was also
3224 cargo-culted from src/analyze.c in GNU Diffutils. Because we
3225 walk backwards, we don’t have to keep the positions in sync. */
3226 while (i >= 0 || j >= 0)
3227 {
3228 /* Check whether there is a change (insertion or deletion)
3229 before the current position. */
3230 if ((i > 0 && bit_is_set (ctx.deletions, i - 1)) ||
3231 (j > 0 && bit_is_set (ctx.insertions, j - 1)))
3232 {
3233 ptrdiff_t end_a = min_a + i;
3234 ptrdiff_t end_b = min_b + j;
3235 /* Find the beginning of the current change run. */
3236 while (i > 0 && bit_is_set (ctx.deletions, i - 1))
3237 --i;
3238 while (j > 0 && bit_is_set (ctx.insertions, j - 1))
3239 --j;
3240 ptrdiff_t beg_a = min_a + i;
3241 ptrdiff_t beg_b = min_b + j;
3242 eassert (beg_a >= BEGV);
3243 eassert (beg_b >= BUF_BEGV (b));
3244 eassert (beg_a <= end_a);
3245 eassert (beg_b <= end_b);
3246 eassert (end_a <= ZV);
3247 eassert (end_b <= BUF_ZV (b));
3248 eassert (beg_a < end_a || beg_b < end_b);
3249 if (beg_a < end_a)
3250 del_range (beg_a, end_a);
3251 if (beg_b < end_b)
3252 {
3253 SET_PT (beg_a);
3254 Finsert_buffer_substring (source, make_natnum (beg_b),
3255 make_natnum (end_b));
3256 }
3257 }
3258 --i;
3259 --j;
3260 }
3261
3262 return unbind_to (count, Qnil);
3263}
3264
3265static void
3266set_bit (unsigned char *a, ptrdiff_t i)
3267{
3268 eassert (i >= 0);
3269 /* Micro-optimization: Casting to size_t generates much better
3270 code. */
3271 size_t j = i;
3272 a[j / CHAR_BIT] |= (1 << (j % CHAR_BIT));
3273}
3274
3275static bool
3276bit_is_set (const unsigned char *a, ptrdiff_t i)
3277{
3278 eassert (i >= 0);
3279 /* Micro-optimization: Casting to size_t generates much better
3280 code. */
3281 size_t j = i;
3282 return a[j / CHAR_BIT] & (1 << (j % CHAR_BIT));
3283}
3284
3285/* Return true if the characters at position POS_A of buffer
3286 CTX->buffer_a and at position POS_B of buffer CTX->buffer_b are
3287 equal. POS_A and POS_B are zero-based. Text properties are
3288 ignored. */
3289
3290static bool
3291buffer_chars_equal (struct context *ctx,
3292 ptrdiff_t pos_a, ptrdiff_t pos_b)
3293{
3294 eassert (pos_a >= 0);
3295 pos_a += BUF_BEGV (ctx->buffer_a);
3296 eassert (pos_a >= BUF_BEGV (ctx->buffer_a));
3297 eassert (pos_a < BUF_ZV (ctx->buffer_a));
3298
3299 eassert (pos_b >= 0);
3300 pos_b += BUF_BEGV (ctx->buffer_b);
3301 eassert (pos_b >= BUF_BEGV (ctx->buffer_b));
3302 eassert (pos_b < BUF_ZV (ctx->buffer_b));
3303
3304 return BUF_FETCH_CHAR_AS_MULTIBYTE (ctx->buffer_a, pos_a)
3305 == BUF_FETCH_CHAR_AS_MULTIBYTE (ctx->buffer_b, pos_b);
3306}
3307
3108 3308
3109static void 3309static void
3110subst_char_in_region_unwind (Lisp_Object arg) 3310subst_char_in_region_unwind (Lisp_Object arg)
@@ -5315,6 +5515,7 @@ functions if all the text being accessed has this property. */);
5315 5515
5316 defsubr (&Sinsert_buffer_substring); 5516 defsubr (&Sinsert_buffer_substring);
5317 defsubr (&Scompare_buffer_substrings); 5517 defsubr (&Scompare_buffer_substrings);
5518 defsubr (&Sreplace_buffer_contents);
5318 defsubr (&Ssubst_char_in_region); 5519 defsubr (&Ssubst_char_in_region);
5319 defsubr (&Stranslate_region_internal); 5520 defsubr (&Stranslate_region_internal);
5320 defsubr (&Sdelete_region); 5521 defsubr (&Sdelete_region);
diff --git a/test/src/editfns-tests.el b/test/src/editfns-tests.el
index 3073e371933..a3ea8ab60b5 100644
--- a/test/src/editfns-tests.el
+++ b/test/src/editfns-tests.el
@@ -208,4 +208,35 @@
208 '(error "Invalid format operation %$"))) 208 '(error "Invalid format operation %$")))
209 (should (equal (format "%1$c %1$s" ?±) "± 177"))) 209 (should (equal (format "%1$c %1$s" ?±) "± 177")))
210 210
211(ert-deftest replace-buffer-contents-1 ()
212 (with-temp-buffer
213 (insert #("source" 2 4 (prop 7)))
214 (let ((source (current-buffer)))
215 (with-temp-buffer
216 (insert "before dest after")
217 (let ((marker (set-marker (make-marker) 14)))
218 (save-restriction
219 (narrow-to-region 8 12)
220 (replace-buffer-contents source))
221 (should (equal (marker-buffer marker) (current-buffer)))
222 (should (equal (marker-position marker) 16)))
223 (should (equal-including-properties
224 (buffer-string)
225 #("before source after" 9 11 (prop 7))))
226 (should (equal (point) 9))))
227 (should (equal-including-properties
228 (buffer-string)
229 #("source" 2 4 (prop 7))))))
230
231(ert-deftest replace-buffer-contents-2 ()
232 (with-temp-buffer
233 (insert "foo bar baz qux")
234 (let ((source (current-buffer)))
235 (with-temp-buffer
236 (insert "foo BAR baz qux")
237 (replace-buffer-contents source)
238 (should (equal-including-properties
239 (buffer-string)
240 "foo bar baz qux"))))))
241
211;;; editfns-tests.el ends here 242;;; editfns-tests.el ends here