Reindent `diff-current-defun'.
[emacs.git] / lisp / diff.el
blobc985b66036e605b9ea7e57a07520909543e27d97
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 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 (thisdir default-directory)
115 proc)
116 (save-excursion
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)
129 (setq default-directory thisdir)
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))
145 ;;;###autoload
146 (defun diff-backup (file &optional switches)
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.
150 The backup file is the first file given to `diff'.
151 With prefix arg, prompt for diff switches."
152 (interactive (list (read-file-name "Diff (file with backup): ")
153 (diff-switches)))
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))
161 (diff bak ori switches)))
163 (defun diff-latest-backup-file (fn) ; actually belongs into files.el
164 "Return the latest existing backup of FILE, or nil."
165 (let ((handler (find-file-name-handler fn 'diff-latest-backup-file)))
166 (if handler
167 (funcall handler 'diff-latest-backup-file fn)
168 (file-newest-backup fn))))
170 (provide 'diff)
172 ;;; arch-tag: 7de2c29b-7ea5-4b85-9b9d-72dd860de2bd
173 ;;; diff.el ends here