(diff-default-read-only): Change default.
[emacs.git] / lisp / diff.el
blob231130db21224750b4cbef68efd98f9eae5d743d
1 ;;; diff.el --- run `diff' in compilation-mode
3 ;; Copyright (C) 1992, 1994, 1996, 2001, 2004 Free Software Foundation, Inc.
5 ;; Maintainer: FSF
6 ;; Keywords: unix, tools
8 ;; This file is part of GNU Emacs.
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
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
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.
20 ;; You should have received a copy of the GNU General Public License
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.
25 ;;; Commentary:
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'.
31 ;;; Code:
33 (defgroup diff nil
34 "Comparing files with `diff'."
35 :group 'tools)
37 ;;;###autoload
38 (defcustom diff-switches "-c"
39 "*A string or list of strings specifying switches to be be passed to diff."
40 :type '(choice string (repeat string))
41 :group 'diff)
43 ;;;###autoload
44 (defcustom diff-command "diff"
45 "*The command to use to run diff."
46 :type 'string
47 :group 'diff)
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.")
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 " ")))))
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)))))
73 ;;;###autoload
74 (defun diff (old new &optional switches no-async)
75 "Find and display the differences between OLD and NEW files.
76 Interactively the current buffer's file name is the default for NEW
77 and a backup file for NEW is the default for OLD.
78 If NO-ASYNC is non-nil, call diff synchronously.
79 With prefix arg, prompt for diff switches."
80 (interactive
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))))
98 (setq new (expand-file-name new)
99 old (expand-file-name old))
100 (or switches (setq switches diff-switches)) ; If not specified, use default.
101 (let* ((old-alt (file-local-copy old))
102 (new-alt (file-local-copy new))
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*"))
114 proc)
115 (save-excursion
116 (display-buffer buf)
117 (set-buffer buf)
118 (setq buffer-read-only nil)
119 (buffer-disable-undo (current-buffer))
120 (erase-buffer)
121 (buffer-enable-undo (current-buffer))
122 (diff-mode)
123 (set (make-local-variable 'revert-buffer-function)
124 `(lambda (ignore-auto noconfirm)
125 (diff ',old ',new ',switches ',no-async)))
126 (set (make-local-variable 'diff-old-temp-file) old-alt)
127 (set (make-local-variable 'diff-new-temp-file) new-alt)
128 (insert command "\n")
129 (if (and (not no-async) (fboundp 'start-process))
130 (progn
131 (setq proc (start-process "Diff" buf shell-file-name
132 shell-command-switch command))
133 (set-process-sentinel
134 proc (lambda (proc msg)
135 (with-current-buffer (process-buffer proc)
136 (diff-sentinel (process-exit-status proc))))))
137 ;; Async processes aren't available.
138 (diff-sentinel
139 (call-process shell-file-name nil buf nil
140 shell-command-switch command))))
141 buf))
143 ;;;###autoload
144 (defun diff-backup (file &optional switches)
145 "Diff this file with its backup file or vice versa.
146 Uses the latest backup, if there are several numerical backups.
147 If this file is a backup, diff it with its original.
148 The backup file is the first file given to `diff'.
149 With prefix arg, prompt for diff switches."
150 (interactive (list (read-file-name "Diff (file with backup): ")
151 (diff-switches)))
152 (let (bak ori)
153 (if (backup-file-name-p file)
154 (setq bak file
155 ori (file-name-sans-versions file))
156 (setq bak (or (diff-latest-backup-file file)
157 (error "No backup found for %s" file))
158 ori file))
159 (diff bak ori switches)))
161 (defun diff-latest-backup-file (fn) ; actually belongs into files.el
162 "Return the latest existing backup of FILE, or nil."
163 (let ((handler (find-file-name-handler fn 'diff-latest-backup-file)))
164 (if handler
165 (funcall handler 'diff-latest-backup-file fn)
166 (file-newest-backup fn))))
168 (provide 'diff)
170 ;;; arch-tag: 7de2c29b-7ea5-4b85-9b9d-72dd860de2bd
171 ;;; diff.el ends here