Commit | Line | Data |
---|---|---|
60370d40 | 1 | ;;; diff.el --- run `diff' in compilation-mode |
c0274f38 | 2 | |
0d30b337 | 3 | ;; Copyright (C) 1992, 1994, 1996, 2001, 2002, 2003, 2004, |
409cc4a3 | 4 | ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc. |
3a801d0c | 5 | |
6228c05b | 6 | ;; Maintainer: FSF |
e9571d2a | 7 | ;; Keywords: unix, tools |
e5167999 | 8 | |
b10a4379 JB |
9 | ;; This file is part of GNU Emacs. |
10 | ||
eb3fa2cf | 11 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
b10a4379 | 12 | ;; it under the terms of the GNU General Public License as published by |
eb3fa2cf GM |
13 | ;; the Free Software Foundation, either version 3 of the License, or |
14 | ;; (at your option) any later version. | |
b10a4379 JB |
15 | |
16 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
17 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 | ;; GNU General Public License for more details. | |
20 | ||
21 | ;; You should have received a copy of the GNU General Public License | |
eb3fa2cf | 22 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
b10a4379 | 23 | |
e41b2db1 ER |
24 | ;;; Commentary: |
25 | ||
26 | ;; This package helps you explore differences between files, using the | |
27 | ;; UNIX command diff(1). The commands are `diff' and `diff-backup'. | |
28 | ;; You can specify options with `diff-switches'. | |
29 | ||
e5167999 ER |
30 | ;;; Code: |
31 | ||
85b6275f RS |
32 | (defgroup diff nil |
33 | "Comparing files with `diff'." | |
34 | :group 'tools) | |
35 | ||
36 | ;;;###autoload | |
37 | (defcustom diff-switches "-c" | |
9201cc28 | 38 | "A string or list of strings specifying switches to be passed to diff." |
85b6275f RS |
39 | :type '(choice string (repeat string)) |
40 | :group 'diff) | |
41 | ||
42 | ;;;###autoload | |
43 | (defcustom diff-command "diff" | |
9201cc28 | 44 | "The command to use to run diff." |
660d4800 | 45 | :type 'string |
85b6275f | 46 | :group 'diff) |
d009603c | 47 | |
5d68c2c2 RS |
48 | (defvar diff-old-temp-file nil |
49 | "This is the name of a temp file to be deleted after diff finishes.") | |
50 | (defvar diff-new-temp-file nil | |
51 | "This is the name of a temp file to be deleted after diff finishes.") | |
52 | ||
4c11f6a8 SM |
53 | ;; prompt if prefix arg present |
54 | (defun diff-switches () | |
55 | (if current-prefix-arg | |
56 | (read-string "Diff switches: " | |
57 | (if (stringp diff-switches) | |
58 | diff-switches | |
59 | (mapconcat 'identity diff-switches " "))))) | |
60 | ||
540d0666 SM |
61 | (defun diff-sentinel (code) |
62 | "Code run when the diff process exits. | |
4837b516 GM |
63 | CODE is the exit code of the process. It should be 0 only if no diffs |
64 | were found." | |
540d0666 SM |
65 | (if diff-old-temp-file (delete-file diff-old-temp-file)) |
66 | (if diff-new-temp-file (delete-file diff-new-temp-file)) | |
67 | (save-excursion | |
68 | (goto-char (point-max)) | |
53dd481c CY |
69 | (let ((inhibit-read-only t)) |
70 | (insert (format "\nDiff finished%s. %s\n" | |
d02b0e30 CY |
71 | (cond ((equal 0 code) " (no differences)") |
72 | ((equal 2 code) " (diff error)") | |
73 | (t "")) | |
53dd481c | 74 | (current-time-string)))))) |
540d0666 | 75 | |
3fe5c37a DN |
76 | (defvar diff-old-file nil) |
77 | (defvar diff-new-file nil) | |
78 | (defvar diff-extra-args nil) | |
79 | ||
b10a4379 | 80 | ;;;###autoload |
0f7b0f88 | 81 | (defun diff (old new &optional switches no-async) |
b10a4379 | 82 | "Find and display the differences between OLD and NEW files. |
4b00edc8 | 83 | Interactively the current buffer's file name is the default for NEW |
646bd331 | 84 | and a backup file for NEW is the default for OLD. |
4c11f6a8 SM |
85 | If NO-ASYNC is non-nil, call diff synchronously. |
86 | With prefix arg, prompt for diff switches." | |
b10a4379 | 87 | (interactive |
4c11f6a8 SM |
88 | (let (oldf newf) |
89 | (setq newf (buffer-file-name) | |
90 | newf (if (and newf (file-exists-p newf)) | |
91 | (read-file-name | |
5b76833f RF |
92 | (concat "Diff new file (default " |
93 | (file-name-nondirectory newf) "): ") | |
4c11f6a8 SM |
94 | nil newf t) |
95 | (read-file-name "Diff new file: " nil nil t))) | |
96 | (setq oldf (file-newest-backup newf) | |
97 | oldf (if (and oldf (file-exists-p oldf)) | |
98 | (read-file-name | |
5b76833f RF |
99 | (concat "Diff original file (default " |
100 | (file-name-nondirectory oldf) "): ") | |
4c11f6a8 SM |
101 | (file-name-directory oldf) oldf t) |
102 | (read-file-name "Diff original file: " | |
103 | (file-name-directory newf) nil t))) | |
104 | (list oldf newf (diff-switches)))) | |
b10a4379 JB |
105 | (setq new (expand-file-name new) |
106 | old (expand-file-name old)) | |
17c91d79 SM |
107 | (or switches (setq switches diff-switches)) ; If not specified, use default. |
108 | (let* ((old-alt (file-local-copy old)) | |
5d68c2c2 | 109 | (new-alt (file-local-copy new)) |
540d0666 SM |
110 | (command |
111 | (mapconcat 'identity | |
112 | `(,diff-command | |
113 | ;; Use explicitly specified switches | |
114 | ,@(if (listp switches) switches (list switches)) | |
115 | ,@(if (or old-alt new-alt) | |
116 | (list "-L" old "-L" new)) | |
117 | ,(shell-quote-argument (or old-alt old)) | |
118 | ,(shell-quote-argument (or new-alt new))) | |
119 | " ")) | |
120 | (buf (get-buffer-create "*Diff*")) | |
de9dc5e5 | 121 | (thisdir default-directory) |
540d0666 | 122 | proc) |
9f748a35 | 123 | (save-excursion |
540d0666 SM |
124 | (display-buffer buf) |
125 | (set-buffer buf) | |
126 | (setq buffer-read-only nil) | |
127 | (buffer-disable-undo (current-buffer)) | |
53dd481c CY |
128 | (let ((inhibit-read-only t)) |
129 | (erase-buffer)) | |
540d0666 SM |
130 | (buffer-enable-undo (current-buffer)) |
131 | (diff-mode) | |
d4871b4f SM |
132 | ;; Use below 2 vars for backward-compatibility. |
133 | (set (make-local-variable 'diff-old-file) old) | |
134 | (set (make-local-variable 'diff-new-file) new) | |
135 | (set (make-local-variable 'diff-extra-args) (list switches no-async)) | |
540d0666 | 136 | (set (make-local-variable 'revert-buffer-function) |
d4871b4f SM |
137 | (lambda (ignore-auto noconfirm) |
138 | (apply 'diff diff-old-file diff-new-file diff-extra-args))) | |
540d0666 SM |
139 | (set (make-local-variable 'diff-old-temp-file) old-alt) |
140 | (set (make-local-variable 'diff-new-temp-file) new-alt) | |
de9dc5e5 | 141 | (setq default-directory thisdir) |
53dd481c CY |
142 | (let ((inhibit-read-only t)) |
143 | (insert command "\n")) | |
540d0666 SM |
144 | (if (and (not no-async) (fboundp 'start-process)) |
145 | (progn | |
146 | (setq proc (start-process "Diff" buf shell-file-name | |
147 | shell-command-switch command)) | |
53dd481c | 148 | (set-process-filter proc 'diff-process-filter) |
540d0666 SM |
149 | (set-process-sentinel |
150 | proc (lambda (proc msg) | |
151 | (with-current-buffer (process-buffer proc) | |
152 | (diff-sentinel (process-exit-status proc)))))) | |
153 | ;; Async processes aren't available. | |
53dd481c CY |
154 | (let ((inhibit-read-only t)) |
155 | (diff-sentinel | |
156 | (call-process shell-file-name nil buf nil | |
157 | shell-command-switch command))))) | |
540d0666 | 158 | buf)) |
aa228418 | 159 | |
53dd481c CY |
160 | (defun diff-process-filter (proc string) |
161 | (with-current-buffer (process-buffer proc) | |
162 | (let ((moving (= (point) (process-mark proc)))) | |
163 | (save-excursion | |
164 | ;; Insert the text, advancing the process marker. | |
165 | (goto-char (process-mark proc)) | |
166 | (let ((inhibit-read-only t)) | |
167 | (insert string)) | |
168 | (set-marker (process-mark proc) (point))) | |
169 | (if moving (goto-char (process-mark proc)))))) | |
170 | ||
646bd331 RM |
171 | ;;;###autoload |
172 | (defun diff-backup (file &optional switches) | |
37c51f00 RS |
173 | "Diff this file with its backup file or vice versa. |
174 | Uses the latest backup, if there are several numerical backups. | |
175 | If this file is a backup, diff it with its original. | |
4c11f6a8 SM |
176 | The backup file is the first file given to `diff'. |
177 | With prefix arg, prompt for diff switches." | |
646bd331 | 178 | (interactive (list (read-file-name "Diff (file with backup): ") |
4c11f6a8 | 179 | (diff-switches))) |
37c51f00 RS |
180 | (let (bak ori) |
181 | (if (backup-file-name-p file) | |
182 | (setq bak file | |
183 | ori (file-name-sans-versions file)) | |
184 | (setq bak (or (diff-latest-backup-file file) | |
185 | (error "No backup found for %s" file)) | |
186 | ori file)) | |
646bd331 | 187 | (diff bak ori switches))) |
37c51f00 RS |
188 | |
189 | (defun diff-latest-backup-file (fn) ; actually belongs into files.el | |
190 | "Return the latest existing backup of FILE, or nil." | |
a617e913 | 191 | (let ((handler (find-file-name-handler fn 'diff-latest-backup-file))) |
34c46d87 | 192 | (if handler |
6d0d0e8d | 193 | (funcall handler 'diff-latest-backup-file fn) |
a8090e38 | 194 | (file-newest-backup fn)))) |
aa228418 | 195 | |
a4ef6584 ER |
196 | (provide 'diff) |
197 | ||
d4871b4f | 198 | ;; arch-tag: 7de2c29b-7ea5-4b85-9b9d-72dd860de2bd |
c0274f38 | 199 | ;;; diff.el ends here |