Say I have a command that operates on the entire buffer.
The simple solution is to:
- Run the command taking the entire buffer as input.
- Store the output.
- Clear the current buffer.
- Insert the new contents.
The problem with this is it's quite slow and it seems the undo system stores a lot of data for this operation.
In cases where only a few changes are made - is there a way to only apply changes - in a way doesn't require the heavy operation of replacing the entire contents?
Something like creating a diff and applying it, instead of replacing the entire buffer, however it need not use the diff format.
Or do I need to write my own code to detect differences and apply them as edits?
diff
anddiff-apply-hunk
to be a sensible approach here. I don't know that it would be a lighter approach as such, but I think it would tend to achieve the aim of not using so much undo data.(diff OLD NEW &optional SWITCHES NO-ASYNC)
,OLD
andNEW
can each be buffers (despite the docstring indicating they must be files). I discovered this courtesy of thediff-buffer-with-file
implementation, which passes a buffer object. Thediff
function usesdiff-file-local-copy
to create a local temp file as necessary.