diff options
| author | Craig Jennings <c@cjennings.net> | 2025-11-01 12:56:12 -0500 |
|---|---|---|
| committer | Craig Jennings <c@cjennings.net> | 2025-11-01 12:56:12 -0500 |
| commit | 0a87ce119751a64fcd113b5f1fbcd7946d192145 (patch) | |
| tree | e20b122941cb6c7e0ce9784c00cf545c2796b4cc | |
| parent | 6944fc4ef6cc3d2383324ac0b1527e132c8e019e (diff) | |
feat:buffer: Add diffing buffer contents with saved file
Introduce a new function `cj/diff-buffer-with-file` to compare
current buffer with saved file version using unified diff format.
Bind it to "C-; b D" for easy access. This enhances buffer
management capabilities by allowing users to view changes before
saving.
| -rw-r--r-- | modules/custom-buffer-file.el | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/modules/custom-buffer-file.el b/modules/custom-buffer-file.el index 2fe8847f..2d2fa919 100644 --- a/modules/custom-buffer-file.el +++ b/modules/custom-buffer-file.el @@ -7,6 +7,7 @@ ;; Functions include: ;; - printing buffers or regions as PostScript to the default printer (with color support) ;; - moving/renaming/deleting buffer files +;; - diffing buffer contents with saved file version ;; - copying file paths and file:// links to the kill ring ;; - copying entire buffer contents ;; - clearing buffer contents from point to top or bottom. @@ -219,6 +220,25 @@ Do not save the deleted text in the kill ring." (kill-new (buffer-name)) (message "Copied: %s" (buffer-name))) +(defun cj/diff-buffer-with-file () + "Compare the current modified buffer with the saved version. +Uses unified diff format (-u) for better readability. +Signal an error if the buffer is not visiting a file. + +TODO: Future integration with difftastic for structural diffs (Method 3)." + (interactive) + (let ((file-path (buffer-file-name))) + (cond + ((not file-path) + (user-error "Current buffer is not visiting a file")) + ((not (file-exists-p file-path)) + (user-error "File %s does not exist on disk" file-path)) + ((not (buffer-modified-p)) + (message "Buffer has no unsaved changes")) + (t + (let ((diff-switches "-u")) ; unified diff format + (diff-buffer-with-file (current-buffer))))))) + ;; --------------------------- Buffer And File Keymap -------------------------- ;; Buffer & file operations prefix and keymap @@ -228,6 +248,7 @@ Do not save the deleted text in the kill ring." "r" #'cj/rename-buffer-and-file "p" #'cj/print-buffer-ps "d" #'cj/delete-buffer-and-file + "D" #'cj/diff-buffer-with-file "c" #'cj/copy-whole-buffer "n" #'cj/copy-buffer-name "t" #'cj/clear-to-top-of-buffer @@ -248,6 +269,7 @@ Do not save the deleted text in the kill ring." "C-; b r" "rename file" "C-; b p" "print to PS" "C-; b d" "delete file" + "C-; b D" "diff buffer with file" "C-; b c" "copy buffer" "C-; b n" "copy buffer name" "C-; b t" "clear to top" |
