annotate lisp/diff.el @ 659:505130d1ddf8

*** empty log message ***
author Eric S. Raymond <esr@snark.thyrsus.com>
date Sat, 30 May 1992 22:12:04 +0000
parents c3bbd755b7da
children 8a533acedb77
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
349
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1 ;; "DIFF" mode for handling output from unix diff utility.
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
2 ;; Copyright (C) 1990 Free Software Foundation, Inc.
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
3 ;; Written fpb@ittc.wec.com 1/27/89
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
4
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
5 ;; This file is part of GNU Emacs.
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
6
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
7 ;; GNU Emacs is free software; you can redistribute it and/or modify
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
8 ;; it under the terms of the GNU General Public License as published by
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
9 ;; the Free Software Foundation; either version 1, or (at your option)
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
10 ;; any later version.
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
11
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
12 ;; GNU Emacs is distributed in the hope that it will be useful,
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
15 ;; GNU General Public License for more details.
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
16
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
17 ;; You should have received a copy of the GNU General Public License
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
18 ;; along with GNU Emacs; see the file COPYING. If not, write to
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
19 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
20
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
21 ;; todo: diff-switches flexibility:
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
22 ;; (defconst diff-switches-function
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
23 ;; '(lambda (file)
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
24 ;; (if (string-match "\\.el$" file)
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
25 ;; "-c -F\"^(\""
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
26 ;; "-p"))
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
27 ;; "Function to return switches to pass to the `diff' utility, in \\[diff].
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
28 ;; This function is called with one arg, a file name, and returns a string
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
29 ;; containing 0 or more arguments which are passed on to `diff'.
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
30 ;; NOTE: This is not an ordinary hook; it may not be a list of functions.")
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
31
474
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 349
diff changeset
32 ;; - fpb@ittc.wec.com - Sep 25, 1990
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 349
diff changeset
33 ;; Added code to support sccs diffing.
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 349
diff changeset
34 ;; also fixed one minor glitch in the
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 349
diff changeset
35 ;; search for the pattern. If you only 1 addition you won't find the end
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 349
diff changeset
36 ;; of the pattern (minor)
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 349
diff changeset
37
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 349
diff changeset
38 ;;
349
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
39 (defvar diff-switches nil
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
40 "*A list of switches to pass to the diff program.")
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
41
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
42 (defvar diff-search-pattern "^\\([0-9]\\|\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\)"
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
43 "Regular expression that delineates difference regions in diffs.")
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
44
474
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 349
diff changeset
45 (defvar diff-rcs-extension ",v"
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 349
diff changeset
46 "*Extension to find RCS file, some systems do not use ,v")
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 349
diff changeset
47
349
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
48 ;; Initialize the keymap if it isn't already
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
49 (if (boundp 'diff-mode-map)
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
50 nil
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
51 (setq diff-mode-map (make-keymap))
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
52 (suppress-keymap diff-mode-map)
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
53 (define-key diff-mode-map "?" 'describe-mode)
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
54 (define-key diff-mode-map "." 'diff-beginning-of-diff)
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
55 (define-key diff-mode-map " " 'scroll-up)
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
56 (define-key diff-mode-map "\177" 'scroll-down)
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
57 (define-key diff-mode-map "n" 'diff-next-difference)
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
58 (define-key diff-mode-map "p" 'diff-previous-difference)
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
59 (define-key diff-mode-map "j" 'diff-show-difference))
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
60
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
61 ;;;###autoload
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
62 (defun diff (old new)
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
63 "Find and display the differences between OLD and NEW files.
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
64 Interactively you are prompted with the current buffer's file name for NEW
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
65 and what appears to be it's backup for OLD."
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
66 (interactive
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
67 (let (oldf newf)
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
68 (reverse
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
69 (list
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
70 (setq newf (buffer-file-name)
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
71 newf (if (and newf (file-exists-p newf))
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
72 (read-file-name
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
73 (concat "Diff new file: ("
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
74 (file-name-nondirectory newf) ") ")
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
75 nil newf t)
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
76 (read-file-name "Diff new file: " nil nil t)))
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
77 (setq oldf (file-newest-backup newf)
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
78 oldf (if (and oldf (file-exists-p oldf))
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
79 (read-file-name
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
80 (concat "Diff original file: ("
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
81 (file-name-nondirectory oldf) ") ")
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
82 (file-name-directory oldf) oldf t)
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
83 (read-file-name "Diff original file: "
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
84 (file-name-directory newf) nil t)))))))
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
85 (message "Comparing files %s %s..." new old)
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
86 (setq new (expand-file-name new)
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
87 old (expand-file-name old))
474
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 349
diff changeset
88 (diff-internal-diff "diff" (append diff-switches (list new old)) nil))
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 349
diff changeset
89
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 349
diff changeset
90 (defun diff-sccs (new)
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 349
diff changeset
91 "Find and display the differences between OLD and SCCS files."
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 349
diff changeset
92 (interactive
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 349
diff changeset
93 (let (newf)
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 349
diff changeset
94 (list
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 349
diff changeset
95 (setq newf (buffer-file-name)
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 349
diff changeset
96 newf (if (and newf (file-exists-p newf))
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 349
diff changeset
97 (read-file-name
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 349
diff changeset
98 (concat "Diff new file: ("
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 349
diff changeset
99 (file-name-nondirectory newf) ") ")
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 349
diff changeset
100 nil newf t)
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 349
diff changeset
101 (read-file-name "Diff new file: " nil nil t))))))
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 349
diff changeset
102
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 349
diff changeset
103 (message "Comparing SCCS file %s..." new)
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 349
diff changeset
104 (setq new (expand-file-name new))
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 349
diff changeset
105 (if (file-exists-p (concat
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 349
diff changeset
106 (file-name-directory new)
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 349
diff changeset
107 "SCCS/s."
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 349
diff changeset
108 (file-name-nondirectory new)))
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 349
diff changeset
109 (diff-internal-diff "sccs"
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 349
diff changeset
110 (append '("diffs") diff-switches (list new))
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 349
diff changeset
111 2)
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 349
diff changeset
112 (error "%s does not exist"
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 349
diff changeset
113 (concat (file-name-directory new) "SCCS/s."
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 349
diff changeset
114 (file-name-nondirectory new)))))
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 349
diff changeset
115
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 349
diff changeset
116 (defun diff-rcs (new)
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 349
diff changeset
117 "Find and display the differences between OLD and RCS files."
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 349
diff changeset
118 (interactive
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 349
diff changeset
119 (let (newf)
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 349
diff changeset
120 (list
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 349
diff changeset
121 (setq newf (buffer-file-name)
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 349
diff changeset
122 newf (if (and newf (file-exists-p newf))
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 349
diff changeset
123 (read-file-name
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 349
diff changeset
124 (concat "Diff new file: ("
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 349
diff changeset
125 (file-name-nondirectory newf) ") ")
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 349
diff changeset
126 nil newf t)
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 349
diff changeset
127 (read-file-name "Diff new file: " nil nil t))))))
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 349
diff changeset
128
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 349
diff changeset
129 (message "Comparing RCS file %s..." new)
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 349
diff changeset
130 (let* ((fullname (expand-file-name new))
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 349
diff changeset
131 (rcsfile (concat (file-name-directory fullname)
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 349
diff changeset
132 "RCS/"
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 349
diff changeset
133 (file-name-nondirectory fullname)
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 349
diff changeset
134 diff-rcs-extension)))
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 349
diff changeset
135 (if (file-exists-p rcsfile)
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 349
diff changeset
136 (diff-internal-diff "rcsdiff" (append diff-switches (list fullname)) 4)
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 349
diff changeset
137 (error "%s does not exist" rcsfile))))
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 349
diff changeset
138
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 349
diff changeset
139 (defun diff-internal-diff (diff-command sw strip)
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 349
diff changeset
140 (let ((buffer-read-only nil))
349
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
141 (with-output-to-temp-buffer "*Diff Output*"
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
142 (buffer-disable-undo standard-output)
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
143 (save-excursion
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
144 (set-buffer standard-output)
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
145 (erase-buffer)
474
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 349
diff changeset
146 (apply 'call-process diff-command nil t nil sw)))
349
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
147 (set-buffer "*Diff Output*")
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
148 (goto-char (point-min))
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
149 (while sw
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
150 (if (string= (car sw) "-c")
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
151 ;; strip leading filenames from context diffs
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
152 (progn (forward-line 2) (delete-region (point-min) (point))))
474
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 349
diff changeset
153 (if (and (string= (car sw) "-C") (string= "sccs" diff-command))
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 349
diff changeset
154 ;; strip stuff from SCCS context diffs
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 349
diff changeset
155 (progn (forward-line 2) (delete-region (point-min) (point))))
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 349
diff changeset
156 (setq sw (cdr sw)))
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 349
diff changeset
157 (if strip
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 349
diff changeset
158 ;; strip stuff from SCCS context diffs
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 349
diff changeset
159 (progn (forward-line strip) (delete-region (point-min) (point)))))
349
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
160 (diff-mode)
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
161 (if (string= "0" diff-total-differences)
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
162 (let ((buffer-read-only nil))
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
163 (insert (message "There are no differences.")))
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
164 (narrow-to-region (point) (progn
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
165 (forward-line 1)
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
166 (if (re-search-forward diff-search-pattern
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
167 nil t)
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
168 (goto-char (match-beginning 0))
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
169 (goto-char (point-max)))))
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
170 (setq diff-current-difference "1")))
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
171
474
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 349
diff changeset
172 ;; Take a buffer full of Unix diff output and go into a mode to easily
349
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
173 ;; see the next and previous difference
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
174 (defun diff-mode ()
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
175 "Diff Mode is used by \\[diff] for perusing the output from the diff program.
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
176 All normal editing commands are turned off. Instead, these are available:
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
177 \\<diff-mode-map>
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
178 \\[diff-beginning-of-diff] Move point to start of this difference.
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
179 \\[scroll-up] Scroll to next screen of this difference.
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
180 \\[scroll-down] Scroll to previous screen of this difference.
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
181 \\[diff-next-difference] Move to Next Difference.
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
182 \\[diff-previous-difference] Move to Previous Difference.
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
183 \\[diff-show-difference] Jump to difference specified by numeric position.
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
184 "
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
185 (interactive)
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
186 (use-local-map diff-mode-map)
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
187 (setq buffer-read-only t
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
188 major-mode 'diff-mode
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
189 mode-name "Diff"
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
190 mode-line-modified "--- "
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
191 mode-line-process
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
192 '(" " diff-current-difference "/" diff-total-differences))
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
193 (make-local-variable 'diff-current-difference)
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
194 (set (make-local-variable 'diff-total-differences)
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
195 (int-to-string (diff-count-differences))))
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
196
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
197 (defun diff-next-difference (n)
474
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 349
diff changeset
198 "Go to the beginning of the next difference.
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 349
diff changeset
199 Differences are delimited by `diff-search-pattern'."
349
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
200 (interactive "p")
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
201 (if (< n 0) (diff-previous-difference (- n))
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
202 (if (zerop n) ()
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
203 (goto-char (point-min))
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
204 (forward-line 1) ; to get past the match for the start of this diff
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
205 (widen)
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
206 (if (re-search-forward diff-search-pattern nil 'move n)
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
207 (let ((start (goto-char (match-beginning 0))))
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
208 (forward-line 1)
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
209 (if (re-search-forward diff-search-pattern nil 'move)
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
210 (goto-char (match-beginning 0)))
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
211 (narrow-to-region start (point))
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
212 (setq diff-current-difference
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
213 (int-to-string (+ n (string-to-int
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
214 diff-current-difference)))))
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
215 (re-search-backward diff-search-pattern nil)
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
216 (narrow-to-region (point) (point-max))
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
217 (message "No following differences.")
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
218 (setq diff-current-difference diff-total-differences))
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
219 (goto-char (point-min)))))
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
220
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
221 (defun diff-previous-difference (n)
474
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 349
diff changeset
222 "Go the the beginning of the previous difference.
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 349
diff changeset
223 Differences are delimited by `diff-search-pattern'."
349
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
224 (interactive "p")
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
225 (if (< n 0) (diff-next-difference (- n))
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
226 (if (zerop n) ()
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
227 (goto-char (point-min))
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
228 (widen)
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
229 (if (re-search-backward diff-search-pattern nil 'move n)
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
230 (setq diff-current-difference
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
231 (int-to-string (- (string-to-int diff-current-difference) n)))
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
232 (message "No previous differences.")
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
233 (setq diff-current-difference "1"))
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
234 (narrow-to-region (point) (progn
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
235 (forward-line 1)
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
236 (re-search-forward diff-search-pattern nil)
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
237 (goto-char (match-beginning 0))))
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
238 (goto-char (point-min)))))
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
239
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
240 (defun diff-show-difference (n)
474
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 349
diff changeset
241 "Show difference number N (prefix argument)."
349
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
242 (interactive "p")
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
243 (let ((cur (string-to-int diff-current-difference)))
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
244 (cond ((or (= n cur)
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
245 (zerop n)
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
246 (not (natnump n))) ; should signal an error perhaps.
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
247 ;; just redisplay.
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
248 (goto-char (point-min)))
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
249 ((< n cur)
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
250 (diff-previous-difference (- cur n)))
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
251 ((> n cur)
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
252 (diff-next-difference (- n cur))))))
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
253
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
254 (defun diff-beginning-of-diff ()
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
255 "Go to beginning of current difference."
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
256 (interactive)
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
257 (goto-char (point-min)))
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
258
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
259 ;; This function counts up the number of differences in the buffer.
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
260 (defun diff-count-differences ()
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
261 "Count number of differences in the current buffer."
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
262 (message "Counting differences...")
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
263 (save-excursion
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
264 (save-restriction
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
265 (widen)
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
266 (goto-char (point-min))
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
267 (let ((cnt 0))
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
268 (while (re-search-forward diff-search-pattern nil t)
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
269 (setq cnt (1+ cnt)))
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
270 (message "Counting differences...done (%d)" cnt)
feacf757c5b2 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
271 cnt))))