1 ;; "DIFF" mode for handling output from unix diff utility.
2 ;; Copyright (C) 1990 Free Software Foundation, Inc.
3 ;; Written fpb@ittc.wec.com 1/27/89
5 ;; This file is part of GNU Emacs.
7 ;; GNU Emacs is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation; either version 1, or (at your option)
12 ;; GNU Emacs is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs; see the file COPYING. If not, write to
19 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
21 ;; todo: diff-switches flexibility:
22 ;; (defconst diff-switches-function
24 ;; (if (string-match "\\.el$" file)
27 ;; "Function to return switches to pass to the `diff' utility, in \\[diff].
28 ;; This function is called with one arg, a file name, and returns a string
29 ;; containing 0 or more arguments which are passed on to `diff'.
30 ;; NOTE: This is not an ordinary hook; it may not be a list of functions.")
32 (defvar diff-switches nil
33 "*A list of switches to pass to the diff program.")
35 (defvar diff-search-pattern
"^\\([0-9]\\|\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\)"
36 "Regular expression that delineates difference regions in diffs.")
38 ;; Initialize the keymap if it isn't already
39 (if (boundp 'diff-mode-map
)
41 (setq diff-mode-map
(make-keymap))
42 (suppress-keymap diff-mode-map
)
43 (define-key diff-mode-map
"?" 'describe-mode
)
44 (define-key diff-mode-map
"." 'diff-beginning-of-diff
)
45 (define-key diff-mode-map
" " 'scroll-up
)
46 (define-key diff-mode-map
"\177" 'scroll-down
)
47 (define-key diff-mode-map
"n" 'diff-next-difference
)
48 (define-key diff-mode-map
"p" 'diff-previous-difference
)
49 (define-key diff-mode-map
"j" 'diff-show-difference
))
53 "Find and display the differences between OLD and NEW files.
54 Interactively you are prompted with the current buffer's file name for NEW
55 and what appears to be it's backup for OLD."
60 (setq newf
(buffer-file-name)
61 newf
(if (and newf
(file-exists-p newf
))
63 (concat "Diff new file: ("
64 (file-name-nondirectory newf
) ") ")
66 (read-file-name "Diff new file: " nil nil t
)))
67 (setq oldf
(file-newest-backup newf
)
68 oldf
(if (and oldf
(file-exists-p oldf
))
70 (concat "Diff original file: ("
71 (file-name-nondirectory oldf
) ") ")
72 (file-name-directory oldf
) oldf t
)
73 (read-file-name "Diff original file: "
74 (file-name-directory newf
) nil t
)))))))
75 (message "Comparing files %s %s..." new old
)
76 (setq new
(expand-file-name new
)
77 old
(expand-file-name old
))
78 (let ((buffer-read-only nil
)
80 (with-output-to-temp-buffer "*Diff Output*"
81 (buffer-disable-undo standard-output
)
83 (set-buffer standard-output
)
85 (apply 'call-process
"diff" nil t nil
86 (append diff-switches
(list old new
)))))
87 (set-buffer "*Diff Output*")
88 (goto-char (point-min))
90 (if (string= (car sw
) "-c")
91 ;; strip leading filenames from context diffs
92 (progn (forward-line 2) (delete-region (point-min) (point))))
95 (if (string= "0" diff-total-differences
)
96 (let ((buffer-read-only nil
))
97 (insert (message "There are no differences.")))
98 (narrow-to-region (point) (progn
100 (if (re-search-forward diff-search-pattern
102 (goto-char (match-beginning 0))
103 (goto-char (point-max)))))
104 (setq diff-current-difference
"1")))
106 ;; Take a buffer full of Unix diff output and go into a mode to easily
107 ;; see the next and previous difference
109 "Diff Mode is used by \\[diff] for perusing the output from the diff program.
110 All normal editing commands are turned off. Instead, these are available:
112 \\[diff-beginning-of-diff] Move point to start of this difference.
113 \\[scroll-up] Scroll to next screen of this difference.
114 \\[scroll-down] Scroll to previous screen of this difference.
115 \\[diff-next-difference] Move to Next Difference.
116 \\[diff-previous-difference] Move to Previous Difference.
117 \\[diff-show-difference] Jump to difference specified by numeric position.
120 (use-local-map diff-mode-map
)
121 (setq buffer-read-only t
122 major-mode
'diff-mode
124 mode-line-modified
"--- "
126 '(" " diff-current-difference
"/" diff-total-differences
))
127 (make-local-variable 'diff-current-difference
)
128 (set (make-local-variable 'diff-total-differences
)
129 (int-to-string (diff-count-differences))))
131 (defun diff-next-difference (n)
132 "In diff mode, go to the beginning of the next difference as delimited
133 by `diff-search-pattern'."
135 (if (< n
0) (diff-previous-difference (- n
))
137 (goto-char (point-min))
138 (forward-line 1) ; to get past the match for the start of this diff
140 (if (re-search-forward diff-search-pattern nil
'move n
)
141 (let ((start (goto-char (match-beginning 0))))
143 (if (re-search-forward diff-search-pattern nil
'move
)
144 (goto-char (match-beginning 0)))
145 (narrow-to-region start
(point))
146 (setq diff-current-difference
147 (int-to-string (+ n
(string-to-int
148 diff-current-difference
)))))
149 (re-search-backward diff-search-pattern nil
)
150 (narrow-to-region (point) (point-max))
151 (message "No following differences.")
152 (setq diff-current-difference diff-total-differences
))
153 (goto-char (point-min)))))
155 (defun diff-previous-difference (n)
156 "In diff mode, go the the beginning of the previous difference as delimited
157 by `diff-search-pattern'."
159 (if (< n
0) (diff-next-difference (- n
))
161 (goto-char (point-min))
163 (if (re-search-backward diff-search-pattern nil
'move n
)
164 (setq diff-current-difference
165 (int-to-string (- (string-to-int diff-current-difference
) n
)))
166 (message "No previous differences.")
167 (setq diff-current-difference
"1"))
168 (narrow-to-region (point) (progn
170 (re-search-forward diff-search-pattern nil
)
171 (goto-char (match-beginning 0))))
172 (goto-char (point-min)))))
174 (defun diff-show-difference (n)
175 "Show difference number N (prefix arg)."
177 (let ((cur (string-to-int diff-current-difference
)))
180 (not (natnump n
))) ; should signal an error perhaps.
182 (goto-char (point-min)))
184 (diff-previous-difference (- cur n
)))
186 (diff-next-difference (- n cur
))))))
188 (defun diff-beginning-of-diff ()
189 "Go to beginning of current difference."
191 (goto-char (point-min)))
193 ;; This function counts up the number of differences in the buffer.
194 (defun diff-count-differences ()
195 "Count number of differences in the current buffer."
196 (message "Counting differences...")
200 (goto-char (point-min))
202 (while (re-search-forward diff-search-pattern nil t
)
204 (message "Counting differences...done (%d)" cnt
)