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