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