Eli and Martin, thanks for your answers.

Eli Zaretskii <eliz@gnu.org> writes:

> Please take a look at replace-buffer-contents, which is new with Emacs
> 26.  It might allow you to implement this functionality in a much
> simpler way, as it already contains an internal implementation of a
> Diff-like comparison algorithm, and doesn't require the Diff program
> to be installed.

I didn't know of replace-buffer-contents, so I took a look at it.  Nice
that its documentation even mentions the problem when a marker is
inside a hunk, because of the delete + insert thing (just like Martin
mentions).  IMO, it does most of the things required, but here is one
problem I notice with respect to the expected functionality of
revert-buffer-by-hunks (as I've understood it):

It only calls Fundo_boundary before starting the whole set of
modifications, and thus after replacing the contents (in my tests, the
whole buffer), a single C-/ brings all the changes back, much like
revert-buffer.  And since it binds inhibit-modification-hooks to t, I
think I can't bind locally after-change-functions to an expression that
calls undo-boundary, to do the trick.

Perhaps an optional call to Fundo_boundary in the while loop could be
enough, but I'm not sure how much it will impact on the speed of
replace-buffer-contents.  Or more, if it is justified to add that call.
Please, point out to me if I'm not seeing this right.

Other than that, it looks like a perfect candidate to use (at least to me)
to get the functionality wanted.

> One caveat: replace-buffer-contents can be very slow when the buffer
> is large and reverting it requires a large number of small changes.
> It will fall back to a simpler algorithm for large numbers of changes,
> and could give up entirely if making the changes takes too much time,
> see its doc string.  Perhaps in those cases we should fall back to a
> different code, like the one you wrote.

> Did you time your code?  How long does it take to revert buffers of
> different sizes with different amounts of changes?

I haven't timed it yet.  I didn't know if it would be considered good
enough, to time it.  For a week, I've been testing it manually with some
of the changes in the Emacs sources, and the experience has been
satisfactory.  Are there, by any chance, such tests for
replace-buffer-contents?  I could use them, for comparison purposes.
I will try in the following days to define some parameters (such as
buffer size), and time revert-buffer-by-hunks, to provide some numbers.

Provided it is fast enough, I think something like replacing by hunks a
region would be a good fallback to replace-buffer-contents.  I sure hope
so.

>> a) What variables would you think should be customizable?
>
> The name of the Diff command should be customizable.  Or maybe just
> use diff-command already provided by diff.el.  Same with Diff
> switches.

I agree.  If using diff.el, it makes total sense to use those
variables.  Of course, that means the patch-buffer function should
be modified to work on the different diff output formats (I think --context
and --unified should be enough).  For the record, I don't propose to use
diff-apply-hunk and other diff-mode.el functions, because when I used
that, I ended up with markers at (point-min), I don't know why.  But if
it is desired to reuse those functions instead of repeating code, I
think I will need time (and help, perhaps), to understand why that
happened.


martin rudalics <rudalics@gmx.at> writes:

> What we probably need is an extra step to scan the buffer for markers
> and save their textual context before reverting and a step to restore
> them according to their textual context after reverting.  But if your

When I bumped into the problem of the marker being sent to the
beginning of the hunk, I started looking for something to get the
markers of the buffer, but didn't found anything at the Lisp level.

> method allows to easily determine which hunks remain unchanged, we
> could avoid such textual search for markers in unchanged hunks and,
> depending on the approach used for replacing text, simply restore
> these markers from their offsets from the beginning of the hunk they
> belong to.

Yes, I believe that by getting the diff output and with the line-offset
handling in the patch-buffer function, it would be easy to determine the
unchanged regions.


Thanks again to both of you.

Best regards,
Mauro.