Subject: bug#54975: Allow editing diffs for conveniently fixing typos in the modified file right in the diff buffer

From: emacsq <[email-masked]>
Date: Sat, 16 Apr 2022 18:11:41 +0000

One of the main usage of diff is perusing changes before committing (commit commands usually pop up a diff buffer showing the changes) . When doing this one often finds typos, small things to fix in the diff, so one has to go back the file, fix it, do a diff/commit again to see the changes, etc.

To make this case more convenient, allow editing of diffs with the changes reflected in the original file.

The key 'e' is unused in diff mode, so like in occur it can turn on editing mode for the diff buffer. When the user edits the diff, the changes are reflected in the original file. C-c C-c ends the edit mode and also saves the the modified file, so the user doesn't have to manually switch to the file to save it, before committing, for example.

Here's a simple proof of concept code with advice which demonstrates the feature (going to to edit mode and saving are not included, only the reflection of the changes, so you have to toggle the diff buffer to writable first):

(advice-add 'diff-after-change-function :after 'my-diff-after-change-function)
;;(advice-remove 'diff-after-change-function 'my-diff-after-change-function)

(defun my-diff-after-change-function (start end len)
(ignore-errors
(if (eq len 0) ;; insertion
(if (eq ?- (char-after (line-beginning-position)))
(message "changes in deleted parts are not reflected")

(let ((text (buffer-substring-no-properties start end)))
(save-selected-window
(diff-goto-source)
(forward-char (- (length text)))
(insert text))))

;; deletion
(save-selected-window
(diff-goto-source)
(delete-char len)))))