annotate lisp/diff.el @ 807:4f28bd14272c

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