annotate lisp/packages/diff.el @ 2:ac2d302a0011 r19-15b2

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