annotate lisp/diff.el @ 731:5c6db33a9ef6

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