diff options
| author | Jim Blandy | 1991-07-24 04:35:23 +0000 |
|---|---|---|
| committer | Jim Blandy | 1991-07-24 04:35:23 +0000 |
| commit | b10a43799500bb04ab70cca9e780262fda5812fe (patch) | |
| tree | ed5e5d0b6e06b1f7d03e3b0056768c7c8c320869 /lisp/diff.el | |
| parent | fb8e984708c1ae363cdf8d7d1df5fde03746f8da (diff) | |
| download | emacs-b10a43799500bb04ab70cca9e780262fda5812fe.tar.gz emacs-b10a43799500bb04ab70cca9e780262fda5812fe.zip | |
Initial revision
Diffstat (limited to 'lisp/diff.el')
| -rw-r--r-- | lisp/diff.el | 205 |
1 files changed, 205 insertions, 0 deletions
diff --git a/lisp/diff.el b/lisp/diff.el new file mode 100644 index 00000000000..4dd8b2e57ff --- /dev/null +++ b/lisp/diff.el | |||
| @@ -0,0 +1,205 @@ | |||
| 1 | ;; "DIFF" mode for handling output from unix diff utility. | ||
| 2 | ;; Copyright (C) 1990 Free Software Foundation, Inc. | ||
| 3 | ;; Written fpb@ittc.wec.com 1/27/89 | ||
| 4 | |||
| 5 | ;; This file is part of GNU Emacs. | ||
| 6 | |||
| 7 | ;; GNU Emacs is free software; you can redistribute it and/or modify | ||
| 8 | ;; it under the terms of the GNU General Public License as published by | ||
| 9 | ;; the Free Software Foundation; either version 1, or (at your option) | ||
| 10 | ;; any later version. | ||
| 11 | |||
| 12 | ;; GNU Emacs is distributed in the hope that it will be useful, | ||
| 13 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 14 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 15 | ;; GNU General Public License for more details. | ||
| 16 | |||
| 17 | ;; You should have received a copy of the GNU General Public License | ||
| 18 | ;; along with GNU Emacs; see the file COPYING. If not, write to | ||
| 19 | ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | ||
| 20 | |||
| 21 | ;; todo: diff-switches flexibility: | ||
| 22 | ;; (defconst diff-switches-function | ||
| 23 | ;; '(lambda (file) | ||
| 24 | ;; (if (string-match "\\.el$" file) | ||
| 25 | ;; "-c -F\"^(\"" | ||
| 26 | ;; "-p")) | ||
| 27 | ;; "Function to return switches to pass to the `diff' utility, in \\[diff]. | ||
| 28 | ;; This function is called with one arg, a file name, and returns a string | ||
| 29 | ;; containing 0 or more arguments which are passed on to `diff'. | ||
| 30 | ;; NOTE: This is not an ordinary hook; it may not be a list of functions.") | ||
| 31 | |||
| 32 | (defvar diff-switches nil | ||
| 33 | "*A list of switches to pass to the diff program.") | ||
| 34 | |||
| 35 | (defvar diff-search-pattern "^\\([0-9]\\|\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\)" | ||
| 36 | "Regular expression that delineates difference regions in diffs.") | ||
| 37 | |||
| 38 | ;; Initialize the keymap if it isn't already | ||
| 39 | (if (boundp 'diff-mode-map) | ||
| 40 | nil | ||
| 41 | (setq diff-mode-map (make-keymap)) | ||
| 42 | (suppress-keymap diff-mode-map) | ||
| 43 | (define-key diff-mode-map "?" 'describe-mode) | ||
| 44 | (define-key diff-mode-map "." 'diff-beginning-of-diff) | ||
| 45 | (define-key diff-mode-map " " 'scroll-up) | ||
| 46 | (define-key diff-mode-map "\177" 'scroll-down) | ||
| 47 | (define-key diff-mode-map "n" 'diff-next-difference) | ||
| 48 | (define-key diff-mode-map "p" 'diff-previous-difference) | ||
| 49 | (define-key diff-mode-map "j" 'diff-show-difference)) | ||
| 50 | |||
| 51 | ;;;###autoload | ||
| 52 | (defun diff (old new) | ||
| 53 | "Find and display the differences between OLD and NEW files. | ||
| 54 | Interactively you are prompted with the current buffer's file name for NEW | ||
| 55 | and what appears to be it's backup for OLD." | ||
| 56 | (interactive | ||
| 57 | (let (oldf newf) | ||
| 58 | (reverse | ||
| 59 | (list | ||
| 60 | (setq newf (buffer-file-name) | ||
| 61 | newf (if (and newf (file-exists-p newf)) | ||
| 62 | (read-file-name | ||
| 63 | (concat "Diff new file: (" | ||
| 64 | (file-name-nondirectory newf) ") ") | ||
| 65 | nil newf t) | ||
| 66 | (read-file-name "Diff new file: " nil nil t))) | ||
| 67 | (setq oldf (file-newest-backup newf) | ||
| 68 | oldf (if (and oldf (file-exists-p oldf)) | ||
| 69 | (read-file-name | ||
| 70 | (concat "Diff original file: (" | ||
| 71 | (file-name-nondirectory oldf) ") ") | ||
| 72 | (file-name-directory oldf) oldf t) | ||
| 73 | (read-file-name "Diff original file: " | ||
| 74 | (file-name-directory newf) nil t))))))) | ||
| 75 | (message "Comparing files %s %s..." new old) | ||
| 76 | (setq new (expand-file-name new) | ||
| 77 | old (expand-file-name old)) | ||
| 78 | (let ((buffer-read-only nil) | ||
| 79 | (sw diff-switches)) | ||
| 80 | (with-output-to-temp-buffer "*Diff Output*" | ||
| 81 | (buffer-disable-undo standard-output) | ||
| 82 | (save-excursion | ||
| 83 | (set-buffer standard-output) | ||
| 84 | (erase-buffer) | ||
| 85 | (apply 'call-process "diff" nil t nil | ||
| 86 | (append diff-switches (list old new))))) | ||
| 87 | (set-buffer "*Diff Output*") | ||
| 88 | (goto-char (point-min)) | ||
| 89 | (while sw | ||
| 90 | (if (string= (car sw) "-c") | ||
| 91 | ;; strip leading filenames from context diffs | ||
| 92 | (progn (forward-line 2) (delete-region (point-min) (point)))) | ||
| 93 | (setq sw (cdr sw)))) | ||
| 94 | (diff-mode) | ||
| 95 | (if (string= "0" diff-total-differences) | ||
| 96 | (let ((buffer-read-only nil)) | ||
| 97 | (insert (message "There are no differences."))) | ||
| 98 | (narrow-to-region (point) (progn | ||
| 99 | (forward-line 1) | ||
| 100 | (if (re-search-forward diff-search-pattern | ||
| 101 | nil t) | ||
| 102 | (goto-char (match-beginning 0)) | ||
| 103 | (goto-char (point-max))))) | ||
| 104 | (setq diff-current-difference "1"))) | ||
| 105 | |||
| 106 | ;; Take a buffer full of Unix diff output and go into a mode to easily | ||
| 107 | ;; see the next and previous difference | ||
| 108 | (defun diff-mode () | ||
| 109 | "Diff Mode is used by \\[diff] for perusing the output from the diff program. | ||
| 110 | All normal editing commands are turned off. Instead, these are available: | ||
| 111 | \\<diff-mode-map> | ||
| 112 | \\[diff-beginning-of-diff] Move point to start of this difference. | ||
| 113 | \\[scroll-up] Scroll to next screen of this difference. | ||
| 114 | \\[scroll-down] Scroll to previous screen of this difference. | ||
| 115 | \\[diff-next-difference] Move to Next Difference. | ||
| 116 | \\[diff-previous-difference] Move to Previous Difference. | ||
| 117 | \\[diff-show-difference] Jump to difference specified by numeric position. | ||
| 118 | " | ||
| 119 | (interactive) | ||
| 120 | (use-local-map diff-mode-map) | ||
| 121 | (setq buffer-read-only t | ||
| 122 | major-mode 'diff-mode | ||
| 123 | mode-name "Diff" | ||
| 124 | mode-line-modified "--- " | ||
| 125 | mode-line-process | ||
| 126 | '(" " diff-current-difference "/" diff-total-differences)) | ||
| 127 | (make-local-variable 'diff-current-difference) | ||
| 128 | (set (make-local-variable 'diff-total-differences) | ||
| 129 | (int-to-string (diff-count-differences)))) | ||
| 130 | |||
| 131 | (defun diff-next-difference (n) | ||
| 132 | "In diff mode, go to the beginning of the next difference as delimited | ||
| 133 | by `diff-search-pattern'." | ||
| 134 | (interactive "p") | ||
| 135 | (if (< n 0) (diff-previous-difference (- n)) | ||
| 136 | (if (zerop n) () | ||
| 137 | (goto-char (point-min)) | ||
| 138 | (forward-line 1) ; to get past the match for the start of this diff | ||
| 139 | (widen) | ||
| 140 | (if (re-search-forward diff-search-pattern nil 'move n) | ||
| 141 | (let ((start (goto-char (match-beginning 0)))) | ||
| 142 | (forward-line 1) | ||
| 143 | (if (re-search-forward diff-search-pattern nil 'move) | ||
| 144 | (goto-char (match-beginning 0))) | ||
| 145 | (narrow-to-region start (point)) | ||
| 146 | (setq diff-current-difference | ||
| 147 | (int-to-string (+ n (string-to-int | ||
| 148 | diff-current-difference))))) | ||
| 149 | (re-search-backward diff-search-pattern nil) | ||
| 150 | (narrow-to-region (point) (point-max)) | ||
| 151 | (message "No following differences.") | ||
| 152 | (setq diff-current-difference diff-total-differences)) | ||
| 153 | (goto-char (point-min))))) | ||
| 154 | |||
| 155 | (defun diff-previous-difference (n) | ||
| 156 | "In diff mode, go the the beginning of the previous difference as delimited | ||
| 157 | by `diff-search-pattern'." | ||
| 158 | (interactive "p") | ||
| 159 | (if (< n 0) (diff-next-difference (- n)) | ||
| 160 | (if (zerop n) () | ||
| 161 | (goto-char (point-min)) | ||
| 162 | (widen) | ||
| 163 | (if (re-search-backward diff-search-pattern nil 'move n) | ||
| 164 | (setq diff-current-difference | ||
| 165 | (int-to-string (- (string-to-int diff-current-difference) n))) | ||
| 166 | (message "No previous differences.") | ||
| 167 | (setq diff-current-difference "1")) | ||
| 168 | (narrow-to-region (point) (progn | ||
| 169 | (forward-line 1) | ||
| 170 | (re-search-forward diff-search-pattern nil) | ||
| 171 | (goto-char (match-beginning 0)))) | ||
| 172 | (goto-char (point-min))))) | ||
| 173 | |||
| 174 | (defun diff-show-difference (n) | ||
| 175 | "Show difference number N (prefix arg)." | ||
| 176 | (interactive "p") | ||
| 177 | (let ((cur (string-to-int diff-current-difference))) | ||
| 178 | (cond ((or (= n cur) | ||
| 179 | (zerop n) | ||
| 180 | (not (natnump n))) ; should signal an error perhaps. | ||
| 181 | ;; just redisplay. | ||
| 182 | (goto-char (point-min))) | ||
| 183 | ((< n cur) | ||
| 184 | (diff-previous-difference (- cur n))) | ||
| 185 | ((> n cur) | ||
| 186 | (diff-next-difference (- n cur)))))) | ||
| 187 | |||
| 188 | (defun diff-beginning-of-diff () | ||
| 189 | "Go to beginning of current difference." | ||
| 190 | (interactive) | ||
| 191 | (goto-char (point-min))) | ||
| 192 | |||
| 193 | ;; This function counts up the number of differences in the buffer. | ||
| 194 | (defun diff-count-differences () | ||
| 195 | "Count number of differences in the current buffer." | ||
| 196 | (message "Counting differences...") | ||
| 197 | (save-excursion | ||
| 198 | (save-restriction | ||
| 199 | (widen) | ||
| 200 | (goto-char (point-min)) | ||
| 201 | (let ((cnt 0)) | ||
| 202 | (while (re-search-forward diff-search-pattern nil t) | ||
| 203 | (setq cnt (1+ cnt))) | ||
| 204 | (message "Counting differences...done (%d)" cnt) | ||
| 205 | cnt)))) | ||