annotate lisp/packages/diff.el @ 0:376386a54a3c r19-14

Import from CVS: tag r19-14
author cvs
date Mon, 13 Aug 2007 08:45:50 +0200
parents
children ac2d302a0011
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1 ;;; diff.el --- Run `diff' in compilation-mode.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3 ;; Copyright (C) 1992, 1994 Free Software Foundation, Inc.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
4
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
5 ;; Keywords: unix, tools
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
6
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
7 ;; This file is part of XEmacs.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
8
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
9 ;; XEmacs is free software; you can redistribute it and/or modify it
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
10 ;; under the terms of the GNU General Public License as published by
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
11 ;; the Free Software Foundation; either version 2, or (at your option)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
12 ;; any later version.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
13
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
14 ;; XEmacs is distributed in the hope that it will be useful, but
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
15 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
17 ;; General Public License for more details.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
18
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
19 ;; You should have received a copy of the GNU General Public License
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
20 ;; along with XEmacs; see the file COPYING. If not, write to the Free
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
21 ;; Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
22
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
23 ;;; Synched up with: FSF 19.30.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
24
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
25 ;;; Commentary:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
26
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
27 ;; This package helps you explore differences between files, using the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
28 ;; UNIX command diff(1). The commands are `diff' and `diff-backup'.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
29 ;; You can specify options with `diff-switches'.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
30
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
31 ;;; Code:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
32
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
33 (require 'compile)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
34
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
35 ;;; This is duplicated in vc.el.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
36 ;;;###autoload
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
37 (defvar diff-switches (purecopy "-c")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
38 "*A string or list of strings specifying switches to be be passed to diff.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
39
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
40 (defvar diff-command "diff"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
41 "*The command to use to run diff.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
42
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
43 (defvar diff-regexp-alist
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
44 '(
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
45 ;; -u format: @@ -OLDSTART,OLDEND +NEWSTART,NEWEND @@
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
46 ("^@@ -\\([0-9]+\\),[0-9]+ \\+\\([0-9]+\\),[0-9]+ @@$" 1 2)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
47
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
48 ;; -c format: *** OLDSTART,OLDEND ****
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
49 ("^\\*\\*\\* \\([0-9]+\\),[0-9]+ \\*\\*\\*\\*$" 1 nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
50 ;; --- NEWSTART,NEWEND ----
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
51 ("^--- \\([0-9]+\\),[0-9]+ ----$" nil 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
52
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
53 ;; plain diff format: OLDSTART[,OLDEND]{a,d,c}NEWSTART[,NEWEND]
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
54 ("^\\([0-9]+\\)\\(,[0-9]+\\)?[adc]\\([0-9]+\\)\\(,[0-9]+\\)?$" 1 3)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
55
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
56 ;; -e (ed) format: OLDSTART[,OLDEND]{a,d,c}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
57 ("^\\([0-9]+\\)\\(,[0-9]+\\)?[adc]$" 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
58
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
59 ;; -f format: {a,d,c}OLDSTART[ OLDEND]
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
60 ;; -n format: {a,d,c}OLDSTART LINES-CHANGED
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
61 ("^[adc]\\([0-9]+\\)\\( [0-9]+\\)?$" 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
62 )
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
63 "Alist (REGEXP OLD-IDX NEW-IDX) of regular expressions to match difference
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
64 sections in \\[diff] output. If REGEXP matches, the OLD-IDX'th
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
65 subexpression gives the line number in the old file, and NEW-IDX'th
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
66 subexpression gives the line number in the new file. If OLD-IDX or NEW-IDX
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
67 is nil, REGEXP matches only half a section.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
68
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
69 (defvar diff-old-file nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
70 "This is the old file name in the comparison in this buffer.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
71 (defvar diff-new-file nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
72 "This is the new file name in the comparison in this buffer.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
73 (defvar diff-old-temp-file nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
74 "This is the name of a temp file to be deleted after diff finishes.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
75 (defvar diff-new-temp-file nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
76 "This is the name of a temp file to be deleted after diff finishes.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
77
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
78 ;; See compilation-parse-errors-function (compile.el).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
79 (defun diff-parse-differences (limit-search find-at-least)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
80 (setq compilation-error-list nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
81 (message "Parsing differences...")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
82
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
83 ;; Don't reparse diffs already seen at last parse.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
84 (if compilation-parsing-end (goto-char compilation-parsing-end))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
85
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
86 ;; Construct in REGEXP a regexp composed of all those in dired-regexp-alist.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
87 (let ((regexp (mapconcat #'(lambda (elt)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
88 (concat "\\(" (car elt) "\\)"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
89 diff-regexp-alist
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
90 "\\|"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
91 ;; (GROUP-IDX OLD-IDX NEW-IDX)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
92 (groups (let ((subexpr 1))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
93 (mapcar #'(lambda (elt)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
94 (prog1
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
95 (cons subexpr
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
96 (mapcar #'(lambda (n)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
97 (and n
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
98 (+ subexpr n)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
99 (cdr elt)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
100 (setq subexpr (+ subexpr 1
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
101 ;;#### undefined??
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
102 (count-regexp-groupings
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
103 (car elt))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
104 diff-regexp-alist)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
105
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
106 (new-error
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
107 (function (lambda (file subexpr)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
108 (setq compilation-error-list
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
109 (cons
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
110 (cons (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
111 ;; Report location of message
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
112 ;; at beginning of line.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
113 (goto-char
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
114 (match-beginning subexpr))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
115 (beginning-of-line)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
116 (point-marker))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
117 ;; Report location of corresponding text.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
118 (let ((line (string-to-int
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
119 (buffer-substring
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
120 (match-beginning subexpr)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
121 (match-end subexpr)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
122 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
123 (save-match-data
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
124 (set-buffer (find-file-noselect file)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
125 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
126 (goto-line line)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
127 (point-marker)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
128 compilation-error-list)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
129
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
130 (found-desired nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
131 (num-loci-found 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
132 g)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
133
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
134 (while (and (not found-desired)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
135 ;; We don't just pass LIMIT-SEARCH to re-search-forward
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
136 ;; because we want to find matches containing LIMIT-SEARCH
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
137 ;; but which extend past it.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
138 (re-search-forward regexp nil t))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
139
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
140 ;; Find which individual regexp matched.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
141 (setq g groups)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
142 (while (and g (null (match-beginning (car (car g)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
143 (setq g (cdr g)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
144 (setq g (car g))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
145
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
146 (if (nth 1 g) ;OLD-IDX
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
147 (funcall new-error diff-old-file (nth 1 g)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
148 (if (nth 2 g) ;NEW-IDX
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
149 (funcall new-error diff-new-file (nth 2 g)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
150
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
151 (setq num-loci-found (1+ num-loci-found))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
152 (if (or (and find-at-least
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
153 (>= num-loci-found find-at-least))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
154 (and limit-search (>= (point) limit-search)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
155 ;; We have found as many new loci as the user wants,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
156 ;; or the user wanted a specific diff, and we're past it.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
157 (setq found-desired t)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
158 (if found-desired
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
159 (setq compilation-parsing-end (point))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
160 ;; Set to point-max, not point, so we don't perpetually
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
161 ;; parse the last bit of text when it isn't a diff header.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
162 (setq compilation-parsing-end (point-max)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
163 (message "Parsing differences...done"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
164 (setq compilation-error-list (nreverse compilation-error-list)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
165
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
166 ;;;###autoload
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
167 (defun diff (old new &optional switches)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
168 "Find and display the differences between OLD and NEW files.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
169 Interactively the current buffer's file name is the default for NEW
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
170 and a backup file for NEW is the default for OLD.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
171 With prefix arg, prompt for diff switches."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
172 (interactive
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
173 (nconc
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
174 (let (oldf newf)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
175 (nreverse
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
176 (list
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
177 (setq newf (buffer-file-name)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
178 newf (if (and newf (file-exists-p newf))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
179 (read-file-name
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
180 (concat "Diff new file: ("
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
181 (file-name-nondirectory newf) ") ")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
182 nil newf t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
183 (read-file-name "Diff new file: " nil nil t)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
184 (setq oldf (file-newest-backup newf)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
185 oldf (if (and oldf (file-exists-p oldf))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
186 (read-file-name
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
187 (concat "Diff original file: ("
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
188 (file-name-nondirectory oldf) ") ")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
189 (file-name-directory oldf) oldf t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
190 (read-file-name "Diff original file: "
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
191 (file-name-directory newf) nil t))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
192 (if current-prefix-arg
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
193 (list (read-string "Diff switches: "
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
194 (if (stringp diff-switches)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
195 diff-switches
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
196 (mapconcat 'identity diff-switches " "))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
197 nil)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
198 (setq new (expand-file-name new)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
199 old (expand-file-name old))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
200 ;; XEmacs addition -- allow (diff "../old/" "new-file.el") to work
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
201 (cond ((file-directory-p old)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
202 (or (file-directory-p new)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
203 (setq old (expand-file-name (file-name-nondirectory new)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
204 (file-name-as-directory old)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
205 ((file-directory-p new)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
206 (setq new (expand-file-name (file-name-nondirectory old)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
207 (file-name-as-directory new)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
208 (let ((old-alt (file-local-copy old))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
209 (new-alt (file-local-copy new))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
210 buf)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
211 (unwind-protect
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
212 (let ((command
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
213 (mapconcat 'identity
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
214 (append (list diff-command)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
215 ;; Use explicitly specified switches
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
216 (if switches
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
217 (if (consp switches)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
218 switches (list switches))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
219 ;; If not specified, use default.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
220 (if (consp diff-switches)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
221 diff-switches
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
222 (list diff-switches)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
223 (if (or old-alt new-alt)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
224 (list "-L" old "-L" new))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
225 (list
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
226 (shell-quote-argument (or old-alt old)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
227 (list
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
228 (shell-quote-argument (or new-alt new))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
229 " ")))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
230 (setq buf
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
231 (compile-internal command
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
232 "No more differences" "Diff"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
233 'diff-parse-differences))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
234 (pop-to-buffer buf)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
235 (set (make-local-variable 'diff-old-file) old)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
236 (set (make-local-variable 'diff-new-file) new)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
237 (set (make-local-variable 'diff-old-temp-file) old-alt)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
238 (set (make-local-variable 'diff-new-temp-file) new-alt)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
239 (set (make-local-variable 'compilation-finish-function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
240 (function (lambda (buff msg)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
241 (if diff-old-temp-file
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
242 (delete-file diff-old-temp-file))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
243 (if diff-new-temp-file
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
244 (delete-file diff-new-temp-file)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
245 buf))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
246
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
247 ;;;###autoload
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
248 (defun diff-backup (file &optional switches)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
249 "Diff this file with its backup file or vice versa.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
250 Uses the latest backup, if there are several numerical backups.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
251 If this file is a backup, diff it with its original.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
252 The backup file is the first file given to `diff'."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
253 (interactive (list (read-file-name "Diff (file with backup): ")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
254 (if current-prefix-arg
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
255 (read-string "Diff switches: "
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
256 (if (stringp diff-switches)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
257 diff-switches
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
258 (mapconcat 'identity
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
259 diff-switches " ")))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
260 nil)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
261 (let (bak ori)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
262 (if (backup-file-name-p file)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
263 (setq bak file
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
264 ori (file-name-sans-versions file))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
265 (setq bak (or (diff-latest-backup-file file)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
266 (error "No backup found for %s" file))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
267 ori file))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
268 (diff bak ori switches)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
269
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
270 (defun diff-latest-backup-file (fn) ; actually belongs into files.el
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
271 "Return the latest existing backup of FILE, or nil."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
272 (let ((handler (find-file-name-handler fn 'diff-latest-backup-file)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
273 (if handler
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
274 (funcall handler 'diff-latest-backup-file fn)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
275 ;; First try simple backup, then the highest numbered of the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
276 ;; numbered backups.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
277 ;; Ignore the value of version-control because we look for existing
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
278 ;; backups, which maybe were made earlier or by another user with
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
279 ;; a different value of version-control.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
280 (setq fn (file-chase-links (expand-file-name fn)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
281 (or
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
282 (let ((bak (make-backup-file-name fn)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
283 (if (file-exists-p bak) bak))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
284 ;; We use BACKUPNAME to cope with backups stored in a different dir.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
285 (let* ((backupname (car (find-backup-file-name fn)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
286 (dir (file-name-directory backupname))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
287 (base-versions (concat (file-name-sans-versions
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
288 (file-name-nondirectory backupname))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
289 ".~"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
290 (bv-length (length base-versions)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
291 (concat dir
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
292 (car (sort
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
293 (file-name-all-completions base-versions dir)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
294 ;; bv-length is a fluid var for backup-extract-version:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
295 (function
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
296 (lambda (fn1 fn2)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
297 (> (backup-extract-version fn1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
298 (backup-extract-version fn2))))))))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
299
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
300 (provide 'diff)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
301
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
302 ;;; diff.el ends here