(match): Use yellow background on light-bg terminals.
[emacs.git] / lisp / diff.el
blob83de826bd03a8dfee1b518bb27457bf7f3e88e53
1 ;;; diff.el --- run `diff' in compilation-mode
3 ;; Copyright (C) 1992, 1994, 1996, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007 Free Software Foundation, Inc.
6 ;; Maintainer: FSF
7 ;; Keywords: unix, tools
9 ;; This file is part of GNU Emacs.
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
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
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.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
26 ;;; Commentary:
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'.
32 ;;; Code:
34 (defgroup diff nil
35 "Comparing files with `diff'."
36 :group 'tools)
38 ;;;###autoload
39 (defcustom diff-switches "-c"
40 "*A string or list of strings specifying switches to be passed to diff."
41 :type '(choice string (repeat string))
42 :group 'diff)
44 ;;;###autoload
45 (defcustom diff-command "diff"
46 "*The command to use to run diff."
47 :type 'string
48 :group 'diff)
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.")
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 " ")))))
63 (defun diff-sentinel (code)
64 "Code run when the diff process exits.
65 CODE is the exit code of the process. It should be 0 iff no diffs were found."
66 (if diff-old-temp-file (delete-file diff-old-temp-file))
67 (if diff-new-temp-file (delete-file diff-new-temp-file))
68 (save-excursion
69 (goto-char (point-max))
70 (let ((inhibit-read-only t))
71 (insert (format "\nDiff finished%s. %s\n"
72 (if (equal 0 code) " (no differences)" "")
73 (current-time-string))))))
75 ;;;###autoload
76 (defun diff (old new &optional switches no-async)
77 "Find and display the differences between OLD and NEW files.
78 Interactively the current buffer's file name is the default for NEW
79 and a backup file for NEW is the default for OLD.
80 If NO-ASYNC is non-nil, call diff synchronously.
81 With prefix arg, prompt for diff switches."
82 (interactive
83 (let (oldf newf)
84 (setq newf (buffer-file-name)
85 newf (if (and newf (file-exists-p newf))
86 (read-file-name
87 (concat "Diff new file (default "
88 (file-name-nondirectory newf) "): ")
89 nil newf t)
90 (read-file-name "Diff new file: " nil nil t)))
91 (setq oldf (file-newest-backup newf)
92 oldf (if (and oldf (file-exists-p oldf))
93 (read-file-name
94 (concat "Diff original file (default "
95 (file-name-nondirectory oldf) "): ")
96 (file-name-directory oldf) oldf t)
97 (read-file-name "Diff original file: "
98 (file-name-directory newf) nil t)))
99 (list oldf newf (diff-switches))))
100 (setq new (expand-file-name new)
101 old (expand-file-name old))
102 (or switches (setq switches diff-switches)) ; If not specified, use default.
103 (let* ((old-alt (file-local-copy old))
104 (new-alt (file-local-copy new))
105 (command
106 (mapconcat 'identity
107 `(,diff-command
108 ;; Use explicitly specified switches
109 ,@(if (listp switches) switches (list switches))
110 ,@(if (or old-alt new-alt)
111 (list "-L" old "-L" new))
112 ,(shell-quote-argument (or old-alt old))
113 ,(shell-quote-argument (or new-alt new)))
114 " "))
115 (buf (get-buffer-create "*Diff*"))
116 (thisdir default-directory)
117 proc)
118 (save-excursion
119 (display-buffer buf)
120 (set-buffer buf)
121 (setq buffer-read-only nil)
122 (buffer-disable-undo (current-buffer))
123 (let ((inhibit-read-only t))
124 (erase-buffer))
125 (buffer-enable-undo (current-buffer))
126 (diff-mode)
127 (set (make-local-variable 'revert-buffer-function)
128 `(lambda (ignore-auto noconfirm)
129 (diff ',old ',new ',switches ',no-async)))
130 (set (make-local-variable 'diff-old-temp-file) old-alt)
131 (set (make-local-variable 'diff-new-temp-file) new-alt)
132 (setq default-directory thisdir)
133 (let ((inhibit-read-only t))
134 (insert command "\n"))
135 (if (and (not no-async) (fboundp 'start-process))
136 (progn
137 (setq proc (start-process "Diff" buf shell-file-name
138 shell-command-switch command))
139 (set-process-filter proc 'diff-process-filter)
140 (set-process-sentinel
141 proc (lambda (proc msg)
142 (with-current-buffer (process-buffer proc)
143 (diff-sentinel (process-exit-status proc))))))
144 ;; Async processes aren't available.
145 (let ((inhibit-read-only t))
146 (diff-sentinel
147 (call-process shell-file-name nil buf nil
148 shell-command-switch command)))))
149 buf))
151 (defun diff-process-filter (proc string)
152 (with-current-buffer (process-buffer proc)
153 (let ((moving (= (point) (process-mark proc))))
154 (save-excursion
155 ;; Insert the text, advancing the process marker.
156 (goto-char (process-mark proc))
157 (let ((inhibit-read-only t))
158 (insert string))
159 (set-marker (process-mark proc) (point)))
160 (if moving (goto-char (process-mark proc))))))
162 ;;;###autoload
163 (defun diff-backup (file &optional switches)
164 "Diff this file with its backup file or vice versa.
165 Uses the latest backup, if there are several numerical backups.
166 If this file is a backup, diff it with its original.
167 The backup file is the first file given to `diff'.
168 With prefix arg, prompt for diff switches."
169 (interactive (list (read-file-name "Diff (file with backup): ")
170 (diff-switches)))
171 (let (bak ori)
172 (if (backup-file-name-p file)
173 (setq bak file
174 ori (file-name-sans-versions file))
175 (setq bak (or (diff-latest-backup-file file)
176 (error "No backup found for %s" file))
177 ori file))
178 (diff bak ori switches)))
180 (defun diff-latest-backup-file (fn) ; actually belongs into files.el
181 "Return the latest existing backup of FILE, or nil."
182 (let ((handler (find-file-name-handler fn 'diff-latest-backup-file)))
183 (if handler
184 (funcall handler 'diff-latest-backup-file fn)
185 (file-newest-backup fn))))
187 (provide 'diff)
189 ;;; arch-tag: 7de2c29b-7ea5-4b85-9b9d-72dd860de2bd
190 ;;; diff.el ends here