Various menu changes.
[emacs.git] / lisp / diff-mode.el
blobd8301bc37d07b563231afe320b3c50d7a6d943d3
1 ;;; diff-mode.el --- A mode for viewing/editing context diffs
3 ;; Copyright (C) 1998-1999 Free Software Foundation, Inc.
5 ;; Author: Stefan Monnier <monnier@cs.yale.edu>
6 ;; Keywords: patch diff
7 ;; Revision: $Id: diff-mode.el,v 1.5 2000/02/07 02:01:07 monnier Exp $
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
26 ;;; Commentary:
28 ;; Provides support for font-lock patterns, outline-regexps, navigation
29 ;; commands, editing and various conversions as well as jumping
30 ;; to the corresponding source file.
32 ;; inspired by Pavel Machek's patch-mode.el (<pavel@atrey.karlin.mff.cuni.cz>)
33 ;; some efforts were spent to have it somewhat compatible with XEmacs'
34 ;; diff-mode as well as with compilation-minor-mode
36 ;; to use it, simply add to your .emacs the following lines:
37 ;;
38 ;; (autoload 'diff-mode "diff-mode" "Diff major mode" t)
39 ;; (add-to-list 'auto-mode-alist '("\\.\\(diffs?\\|patch\\|rej\\)\\'" . diff-mode))
41 ;; Bugs:
43 ;; - reverse doesn't work with normal diffs.
44 ;; - (nitpick) the mark is not always quite right in diff-goto-source.
46 ;; Todo:
48 ;; - spice up the minor-mode with editing and font-lock support.
49 ;; - improve narrowed-view support.
50 ;; - improve the `compile' support (?).
51 ;; - recognize pcl-cvs' special string for `cvs-execute-single'.
52 ;; - add support for # comments in diffs
54 ;;; Code:
56 (eval-when-compile (require 'cl))
59 (defgroup diff-mode ()
60 "Major-mode for viewing/editing diffs"
61 :group 'tools
62 :group 'diff)
64 (defcustom diff-jump-to-old-file-flag nil
65 "*Non-nil means `diff-goto-source' jumps to the old file.
66 Else, it jumps to the new file."
67 :group 'diff-mode
68 :type '(boolean))
70 (defcustom diff-update-on-the-fly-flag t
71 "*Non-nil means hunk headers are kept up-to-date on-the-fly.
72 When editing a diff file, the line numbers in the hunk headers
73 need to be kept consistent with the actual diff. This can
74 either be done on the fly (but this sometimes interacts poorly with the
75 undo mechanism) or whenever the file is written (can be slow
76 when editing big diffs)."
77 :group 'diff-mode
78 :type '(boolean))
80 (defvar diff-mode-hook nil
81 "Run after setting up the `diff-mode' major mode.")
83 (defvar diff-outline-regexp
84 "\\([*+][*+][*+] [^0-9]\\|@@ ...\\|\\*\\*\\* [0-9].\\|--- [0-9]..\\)")
86 ;;;;
87 ;;;; keymap, menu, ...
88 ;;;;
90 (easy-mmode-defmap diff-mode-shared-map
91 '(;; from Pavel Machek's patch-mode
92 ("n" . diff-hunk-next)
93 ("N" . diff-file-next)
94 ("p" . diff-hunk-prev)
95 ("P" . diff-file-prev)
96 ("k" . diff-hunk-kill)
97 ("K" . diff-file-kill)
98 ;; from compilation-minor-mode
99 ("}" . diff-file-next)
100 ("{" . diff-file-prev)
101 ("\C-m" . diff-goto-source)
102 ;; from XEmacs' diff-mode
103 ("W" . widen)
104 ;;("." . diff-goto-source) ;display-buffer
105 ;;("f" . diff-goto-source) ;find-file
106 ("o" . diff-goto-source) ;other-window
107 ;;("w" . diff-goto-source) ;other-frame
108 ;;("N" . diff-narrow)
109 ;;("h" . diff-show-header)
110 ;;("j" . diff-show-difference) ;jump to Nth diff
111 ;;("q" . diff-quit)
112 (" " . scroll-up)
113 ("\177" . scroll-down)
114 ;; our very own bindings
115 ("A" . diff-ediff-patch)
116 ("r" . diff-restrict-view)
117 ("R" . diff-reverse-direction)
118 ("U" . diff-context->unified)
119 ("C" . diff-unified->context))
120 "Basic keymap for `diff-mode', bound to various prefix keys.")
122 (easy-mmode-defmap diff-mode-map
123 `(("\e" . ,diff-mode-shared-map)
124 ;; from compilation-minor-mode
125 ("\C-c\C-c" . diff-goto-source))
126 "Keymap for `diff-mode'. See also `diff-mode-shared-map'.")
128 (easy-menu-define diff-mode-menu diff-mode-map
129 "Menu for `diff-mode'."
130 '("Diff"
131 ["Jump to Source" diff-goto-source t]
132 ["Apply with Ediff" diff-ediff-patch t]
133 ["-----" nil nil]
134 ["Reverse direction" diff-reverse-direction t]
135 ["Context -> Unified" diff-context->unified t]
136 ["Unified -> Context" diff-unified->context t]
137 ;;["Fixup Headers" diff-fixup-modifs (not buffer-read-only)]
140 (defcustom diff-minor-mode-prefix "\C-cd"
141 "Prefix key for `diff-minor-mode' commands."
142 :group 'diff-mode
143 :type '(choice (string "\e") (string "C-cd") string))
145 (easy-mmode-defmap diff-minor-mode-map
146 `((,diff-minor-mode-prefix . ,diff-mode-shared-map))
147 "Keymap for `diff-minor-mode'. See also `diff-mode-shared-map'.")
150 ;;;;
151 ;;;; font-lock support
152 ;;;;
154 (defface diff-file-header-face
155 '((((class color) (background light))
156 (:background "grey70" :bold t))
157 (t (:bold t)))
158 "diff-mode face used to highlight file header lines."
159 :group 'diff-mode)
160 (defvar diff-file-header-face 'diff-file-header-face)
162 (defface diff-index-face
163 '((((class color) (background light))
164 (:background "grey70" :bold t))
165 (t (:bold t)))
166 "diff-mode face used to highlight index header lines."
167 :group 'diff-mode)
168 (defvar diff-index-face 'diff-index-face)
170 (defface diff-hunk-header-face
171 '((((class color) (background light))
172 (:background "grey85"))
173 (t (:bold t)))
174 "diff-mode face used to highlight hunk header lines."
175 :group 'diff-mode)
176 (defvar diff-hunk-header-face 'diff-hunk-header-face)
178 (defface diff-removed-face
179 '((t ()))
180 "diff-mode face used to highlight removed lines."
181 :group 'diff-mode)
182 (defvar diff-removed-face 'diff-removed-face)
184 (defface diff-added-face
185 '((t ()))
186 "diff-mode face used to highlight added lines."
187 :group 'diff-mode)
188 (defvar diff-added-face 'diff-added-face)
190 (defface diff-changed-face
191 '((t ()))
192 "diff-mode face used to highlight changed lines."
193 :group 'diff-mode)
194 (defvar diff-changed-face 'diff-changed-face)
196 (defvar diff-font-lock-keywords
197 '(("^@@ .+ @@$" . diff-hunk-header-face) ;unified
198 ("^--- .+ ----$" . diff-hunk-header-face) ;context
199 ("^\\*\\*\\*.+\\*\\*\\*\n" . diff-hunk-header-face) ;context
200 ("^\\(---\\|\\+\\+\\+\\|\\*\\*\\*\\) .*\n" . diff-file-header-face)
201 ("^[0-9,]+[acd][0-9,]+$" . diff-hunk-header-face)
202 ("^!.*\n" . diff-changed-face) ;context
203 ("^[+>].*\n" . diff-added-face)
204 ("^[-<].*\n" . diff-removed-face)
205 ("^Index: .*\n" . diff-index-face)
206 ("^[^-=+*!<>].*\n" . font-lock-comment-face)))
208 (defconst diff-font-lock-defaults
209 '(diff-font-lock-keywords t nil nil nil))
211 ;;;;
212 ;;;; Compile support
213 ;;;;
215 (defvar diff-file-regexp-alist
216 '(("Index: \\(.+\\)" 1)))
218 (defvar diff-error-regexp-alist
219 '(("@@ -\\([0-9]+\\),[0-9]+ \\+\\([0-9]+\\),[0-9]+ @@" nil 2)
220 ("--- \\([0-9]+\\),[0-9]+ ----" nil 1)
221 ("\\([0-9]+\\)\\(,[0-9]+\\)?[adc]\\([0-9]+\\)" nil 3)))
223 ;;;;
224 ;;;; Movement
225 ;;;;
227 (defconst diff-hunk-header-re "^\\(@@ .+ @@\\|\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\n\\*\\*\\* .+ \\*\\*\\*\\*\\|[0-9]+\\(,[0-9]+\\)?[acd][0-9]+\\(,[0-9]+\\)?\\)$")
228 (defconst diff-file-header-re (concat "^\\(--- .+\n\\+\\+\\+\\|\\*\\*\\* .+\n---\\|[^-+!<>0-9@* ]\\).+\n" (substring diff-hunk-header-re 1)))
229 (defvar diff-narrowed-to nil)
231 (defun diff-end-of-hunk (&optional style)
232 (if (looking-at diff-hunk-header-re) (goto-char (match-end 0)))
233 (re-search-forward (case style
234 (unified "^[^-+ \\]")
235 (context "^\\([^-+! \\][ \t]\\|--- .+ ----\\)")
236 (normal "^\\([<>\\][ \t]\\|---\\)")
237 (t "^[^-+!<> \\]"))
238 nil 'move)
239 (beginning-of-line)
240 (point))
242 (defun diff-beginning-of-hunk ()
243 (beginning-of-line)
244 (unless (looking-at diff-hunk-header-re)
245 (forward-line 1)
246 (condition-case ()
247 (re-search-backward diff-hunk-header-re)
248 (error (error "Can't find the beginning of the hunk")))))
250 (defun diff-beginning-of-file ()
251 (beginning-of-line)
252 (unless (looking-at diff-file-header-re)
253 (forward-line 2)
254 (condition-case ()
255 (re-search-backward diff-file-header-re)
256 (error (error "Can't find the beginning of the file")))))
258 (defun diff-end-of-file ()
259 (re-search-forward "^[-+!<>0-9@* \\]" nil t)
260 (re-search-forward "^[^-+!<>0-9@* \\]" nil 'move)
261 (beginning-of-line))
263 ;; Define diff-{hunk,file}-{prev,next}
264 (easy-mmode-define-navigation
265 diff-hunk diff-hunk-header-re "hunk" diff-end-of-hunk)
266 (easy-mmode-define-navigation
267 diff-file diff-file-header-re "file" diff-end-of-hunk)
269 (defun diff-restrict-view (&optional arg)
270 "Restrict the view to the current hunk.
271 If the prefix ARG is given, restrict the view to the current file instead."
272 (interactive "P")
273 (save-excursion
274 (if arg (diff-beginning-of-file) (diff-beginning-of-hunk))
275 (narrow-to-region (point)
276 (progn (if arg (diff-end-of-file) (diff-end-of-hunk))
277 (point)))
278 (set (make-local-variable 'diff-narrowed-to) (if arg 'file 'hunk))))
281 (defun diff-hunk-kill ()
282 "Kill current hunk."
283 (interactive)
284 (diff-beginning-of-hunk)
285 (let ((start (point))
286 (firsthunk (save-excursion
287 (ignore-errors
288 (diff-beginning-of-file) (diff-hunk-next) (point))))
289 (nexthunk (save-excursion
290 (ignore-errors
291 (diff-hunk-next) (point))))
292 (nextfile (save-excursion
293 (ignore-errors
294 (diff-file-next) (point)))))
295 (if (and firsthunk (= firsthunk start)
296 (or (null nexthunk)
297 (and nextfile (> nexthunk nextfile))))
298 ;; we're the only hunk for this file, so kill the file
299 (diff-file-kill)
300 (diff-end-of-hunk)
301 (kill-region start (point)))))
303 (defun diff-file-kill ()
304 "Kill current file's hunks."
305 (interactive)
306 (diff-beginning-of-file)
307 (let* ((start (point))
308 (prevhunk (save-excursion
309 (ignore-errors
310 (diff-hunk-prev) (point))))
311 (index (save-excursion
312 (re-search-backward "^Index: " prevhunk t))))
313 (when index (setq start index))
314 (diff-end-of-file)
315 (kill-region start (point))))
317 (defun diff-kill-junk ()
318 "Kill spurious empty diffs."
319 (interactive)
320 (save-excursion
321 (let ((inhibit-read-only t))
322 (goto-char (point-min))
323 (while (re-search-forward (concat "^\\(Index: .*\n\\)"
324 "\\([^-+!* <>].*\n\\)*?"
325 "\\(\\(Index:\\) \\|"
326 diff-file-header-re "\\)")
327 nil t)
328 (delete-region (if (match-end 4) (match-beginning 0) (match-end 1))
329 (match-beginning 3))
330 (beginning-of-line)))))
332 ;;;;
333 ;;;; jump to other buffers
334 ;;;;
336 (defvar diff-remembered-files-alist nil)
338 (defun diff-filename-drop-dir (file)
339 (when (string-match "/" file) (substring file (match-end 0))))
341 (defun diff-merge-strings (ancestor from to)
342 "Merge the diff between ANCESTOR and FROM into TO.
343 Returns the merged string if successful or nil otherwise.
344 The strings are assumed not to contain any \"\\n\" (i.e. end of line).
345 If ANCESTOR = FROM, returns TO.
346 If ANCESTOR = TO, returns FROM.
347 The heuristic is simplistic and only really works for cases
348 like \(diff-merge-strings \"b/foo\" \"b/bar\" \"/a/c/foo\")."
349 ;; Ideally, we want:
350 ;; AMB ANB CMD -> CND
351 ;; but that's ambiguous if `foo' or `bar' is empty:
352 ;; a/foo a/foo1 b/foo.c -> b/foo1.c but not 1b/foo.c or b/foo.c1
353 (let ((str (concat ancestor "\n" from "\n" to)))
354 (when (and (string-match (concat
355 "\\`\\(.*?\\)\\(.*\\)\\(.*\\)\n"
356 "\\1\\(.*\\)\\3\n"
357 "\\(.*\\(\\2\\).*\\)\\'") str)
358 (equal to (match-string 5 str)))
359 (concat (substring str (match-beginning 5) (match-beginning 6))
360 (match-string 4 str)
361 (substring str (match-end 6) (match-end 5))))))
365 (defun diff-find-file-name (&optional old)
366 "Return the file corresponding to the current patch.
367 Non-nil OLD means that we want the old file."
368 (save-excursion
369 (unless (looking-at diff-file-header-re)
370 (or (ignore-errors (diff-beginning-of-file))
371 (re-search-forward diff-file-header-re nil t)))
372 (let* ((limit (save-excursion
373 (condition-case ()
374 (progn (diff-hunk-prev) (point))
375 (error (point-min)))))
376 (header-files
377 (if (looking-at "[-*][-*][-*] \\(\\S-+\\)\\s-.*\n[-+][-+][-+] \\(\\S-+\\)\\s-.*$")
378 (list (if old (match-string 1) (match-string 2))
379 (if old (match-string 2) (match-string 1)))
380 (forward-line 1) nil))
381 (fs (append
382 (when (save-excursion
383 (re-search-backward "^Index: \\(.+\\)" limit t))
384 (list (match-string 1)))
385 header-files
386 (when (re-search-backward "^diff \\(-\\S-+ +\\)*\\(\\S-+\\)\\( +\\(\\S-+\\)\\)?" nil t)
387 (list (if old (match-string 2) (match-string 4))
388 (if old (match-string 4) (match-string 2))))))
389 (fs (delq nil fs)))
391 ;; use any previously used preference
392 (cdr (assoc fs diff-remembered-files-alist))
393 ;; try to be clever and use previous choices as an inspiration
394 (dolist (rf diff-remembered-files-alist)
395 (let ((newfile (diff-merge-strings (caar rf) (car fs) (cdr rf))))
396 (if (and newfile (file-exists-p newfile)) (return newfile))))
397 ;; look for each file in turn. If none found, try again but
398 ;; ignoring the first level of directory, ...
399 (do* ((files fs (delq nil (mapcar 'diff-filename-drop-dir files)))
400 (file nil nil))
401 ((or (null files)
402 (setq file (do* ((files files (cdr files))
403 (file (car files) (car files)))
404 ((or (null file) (file-exists-p file))
405 file))))
406 file))
407 ;; <foo>.rej patches implicitly apply to <foo>
408 (and (string-match "\\.rej\\'" (or buffer-file-name ""))
409 (let ((file (substring buffer-file-name 0 (match-beginning 0))))
410 (when (file-exists-p file) file)))
411 ;; if all else fails, ask the user
412 (let ((file (read-file-name (format "Use file %s: " (or (first fs) ""))
413 nil (first fs) t (first fs))))
414 (set (make-local-variable 'diff-remembered-files-alist)
415 (cons (cons fs file) diff-remembered-files-alist))
416 file)))))
418 (defun diff-goto-source (&optional other-file)
419 "Jump to the corresponding source line.
420 `diff-jump-to-old-file-flag' (or its opposite if the OTHER-FILE prefix arg
421 is give) determines whether to jump to the old or the new file.
422 If the prefix arg is bigger than 8 (for example with \\[universal-argument] \\[universal-argument])
423 then `diff-jump-to-old-file-flag' is also set, for the next invokations."
424 (interactive "P")
425 (save-excursion
426 (let ((old (if (not other-file) diff-jump-to-old-file-flag
427 (not diff-jump-to-old-file-flag))))
428 (when (> (prefix-numeric-value other-file) 8)
429 (setq diff-jump-to-old-file-flag old))
430 (diff-beginning-of-hunk)
431 (let* ((loc (if (not (looking-at "[-@*\n ]*\\([0-9,]+\\)\\([ acd+]+\\([0-9,]+\\)\\)?"))
432 (error "Can't find the hunk header")
433 (if old (match-string 1)
434 (if (match-end 3) (match-string 3)
435 (unless (re-search-forward "^--- \\([0-9,]+\\)" nil t)
436 (error "Can't find the hunk separator"))
437 (match-string 1)))))
438 (lines (if (string-match "^\\([0-9]*\\),\\([0-9]*\\)" loc)
439 (cons (string-to-number (match-string 1 loc))
440 (string-to-number (match-string 2 loc)))
441 (cons (string-to-number loc) nil)))
442 (file (diff-find-file-name old)))
443 (unless file (error "Can't find the file"))
444 (pop-to-buffer (find-file-noselect file))
445 (let* ((line (car lines))
446 (span (if (or (null (cdr lines)) (< (cdr lines) 0)) 0
447 (if (< (cdr lines) line) (cdr lines)
448 (- (cdr lines) line)))))
449 (ignore-errors
450 (goto-line line)
451 (forward-line span)
452 (push-mark (point) t t)
453 (goto-line line)))))))
456 (defun diff-ediff-patch ()
457 "Call `ediff-patch-file' on the current buffer."
458 (interactive)
459 (condition-case err
460 (ediff-patch-file (current-buffer))
461 (wrong-number-of-arguments (ediff-patch-file))))
463 ;;;;
464 ;;;; Conversion functions
465 ;;;;
467 ;;(defvar diff-inhibit-after-change nil
468 ;; "Non-nil means inhibit `diff-mode's after-change functions.")
470 (defun diff-unified->context (start end)
471 "Convert unified diffs to context diffs.
472 START and END are either taken from the region (if a prefix arg is given) or
473 else cover the whole bufer."
474 (interactive (if current-prefix-arg
475 (list (mark) (point))
476 (list (point-min) (point-max))))
477 (unless (markerp end) (setq end (copy-marker end)))
478 (let (;;(diff-inhibit-after-change t)
479 (inhibit-read-only t))
480 (save-excursion
481 (goto-char start)
482 (while (and (re-search-forward "^\\(\\(---\\) .+\n\\(\\+\\+\\+\\) .+\\|@@ -\\([0-9]+\\),\\([0-9]+\\) \\+\\([0-9]+\\),\\([0-9]+\\) @@\\)$" nil t)
483 (< (point) end))
484 (combine-after-change-calls
485 (if (match-beginning 2)
486 ;;we matched a file header
487 (progn
488 ;; use reverse order to make sure the indices are kept valid
489 (replace-match "---" t t nil 3)
490 (replace-match "***" t t nil 2))
491 ;; we matched a hunk header
492 (let ((line1 (match-string 4))
493 (lines1 (match-string 5))
494 (line2 (match-string 6))
495 (lines2 (match-string 7)))
496 (replace-match
497 (concat "***************\n*** " line1 ","
498 (number-to-string (+ (string-to-number line1)
499 (string-to-number lines1)
500 -1)) " ****"))
501 (forward-line 1)
502 (save-restriction
503 (narrow-to-region (point)
504 (progn (diff-end-of-hunk 'unified) (point)))
505 (let ((hunk (buffer-string)))
506 (goto-char (point-min))
507 (if (not (save-excursion (re-search-forward "^-" nil t)))
508 (delete-region (point) (point-max))
509 (goto-char (point-max))
510 (let ((modif nil) last-pt)
511 (while (progn (setq last-pt (point))
512 (= (forward-line -1) 0))
513 (case (char-after)
514 (? (insert " ") (setq modif nil) (backward-char 1))
515 (?+ (delete-region (point) last-pt) (setq modif t))
516 (?- (if (not modif)
517 (progn (forward-char 1)
518 (insert " "))
519 (delete-char 1)
520 (insert "! "))
521 (backward-char 2))
522 (?\\ (when (save-excursion (forward-line -1)
523 (= (char-after) ?+))
524 (delete-region (point) last-pt) (setq modif t)))
525 (t (setq modif nil))))))
526 (goto-char (point-max))
527 (save-excursion
528 (insert "--- " line2 ","
529 (number-to-string (+ (string-to-number line2)
530 (string-to-number lines2)
531 -1)) " ----\n" hunk))
532 ;;(goto-char (point-min))
533 (forward-line 1)
534 (if (not (save-excursion (re-search-forward "^+" nil t)))
535 (delete-region (point) (point-max))
536 (let ((modif nil) (delete nil))
537 (while (not (eobp))
538 (case (char-after)
539 (? (insert " ") (setq modif nil) (backward-char 1))
540 (?- (setq delete t) (setq modif t))
541 (?+ (if (not modif)
542 (progn (forward-char 1)
543 (insert " "))
544 (delete-char 1)
545 (insert "! "))
546 (backward-char 2))
547 (?\\ (when (save-excursion (forward-line 1)
548 (not (eobp)))
549 (setq delete t) (setq modif t)))
550 (t (setq modif nil)))
551 (let ((last-pt (point)))
552 (forward-line 1)
553 (when delete
554 (delete-region last-pt (point))
555 (setq delete nil)))))))))))))))
557 (defun diff-context->unified (start end)
558 "Convert context diffs to unified diffs.
559 START and END are either taken from the region (if a prefix arg is given) or
560 else cover the whole bufer."
561 (interactive (if current-prefix-arg
562 (list (mark) (point))
563 (list (point-min) (point-max))))
564 (unless (markerp end) (setq end (copy-marker end)))
565 (let (;;(diff-inhibit-after-change t)
566 (inhibit-read-only t))
567 (save-excursion
568 (goto-char start)
569 (while (and (re-search-forward "^\\(\\(\\*\\*\\*\\) .+\n\\(---\\) .+\\|\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\n\\*\\*\\* \\([0-9]+\\),\\(-?[0-9]+\\) \\*\\*\\*\\*\\)$" nil t)
570 (< (point) end))
571 (combine-after-change-calls
572 (if (match-beginning 2)
573 ;; we matched a file header
574 (progn
575 ;; use reverse order to make sure the indices are kept valid
576 (replace-match "+++" t t nil 3)
577 (replace-match "---" t t nil 2))
578 ;; we matched a hunk header
579 (let ((line1s (match-string 4))
580 (line1e (match-string 5))
581 (pt1 (match-beginning 0)))
582 (replace-match "")
583 (unless (re-search-forward
584 "^--- \\([0-9]+\\),\\(-?[0-9]+\\) ----$" nil t)
585 (error "Can't find matching `--- n1,n2 ----' line"))
586 (let ((line2s (match-string 1))
587 (line2e (match-string 2))
588 (pt2 (progn
589 (delete-region (progn (beginning-of-line) (point))
590 (progn (forward-line 1) (point)))
591 (point-marker))))
592 (goto-char pt1)
593 (forward-line 1)
594 (while (< (point) pt2)
595 (case (char-after)
596 ((?! ?-) (delete-char 2) (insert "-") (forward-line 1))
597 (?\ ;merge with the other half of the chunk
598 (let* ((endline2
599 (save-excursion
600 (goto-char pt2) (forward-line 1) (point)))
601 (c (char-after pt2)))
602 (case c
603 ((?! ?+)
604 (insert "+"
605 (prog1 (buffer-substring (+ pt2 2) endline2)
606 (delete-region pt2 endline2))))
607 (?\ ;FIXME: check consistency
608 (delete-region pt2 endline2)
609 (delete-char 1)
610 (forward-line 1))
611 (?\\ (forward-line 1))
612 (t (delete-char 1) (forward-line 1)))))
613 (t (forward-line 1))))
614 (while (looking-at "[+! ] ")
615 (if (/= (char-after) ?!) (forward-char 1)
616 (delete-char 1) (insert "+"))
617 (delete-char 1) (forward-line 1))
618 (save-excursion
619 (goto-char pt1)
620 (insert "@@ -" line1s ","
621 (number-to-string (- (string-to-number line1e)
622 (string-to-number line1s)
623 -1))
624 " +" line2s ","
625 (number-to-string (- (string-to-number line2e)
626 (string-to-number line2s)
627 -1)) " @@"))))))))))
629 (defun diff-reverse-direction (start end)
630 "Reverse the direction of the diffs.
631 START and END are either taken from the region (if a prefix arg is given) or
632 else cover the whole bufer."
633 (interactive (if current-prefix-arg
634 (list (mark) (point))
635 (list (point-min) (point-max))))
636 (unless (markerp end) (setq end (copy-marker end)))
637 (let (;;(diff-inhibit-after-change t)
638 (inhibit-read-only t))
639 (save-excursion
640 (goto-char start)
641 (while (and (re-search-forward "^\\(\\([-*][-*][-*] \\)\\(.+\\)\n\\([-+][-+][-+] \\)\\(.+\\)\\|\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\n\\*\\*\\* \\(.+\\) \\*\\*\\*\\*\\|@@ -\\(.+\\) \\+\\(.+\\) @@\\)$" nil t)
642 (< (point) end))
643 (combine-after-change-calls
644 (cond
645 ;; a file header
646 ((match-beginning 2) (replace-match "\\2\\5\n\\4\\3" nil))
647 ;; a context-diff hunk header
648 ((match-beginning 6)
649 (let ((pt-lines1 (match-beginning 6))
650 (lines1 (match-string 6)))
651 (replace-match "" nil nil nil 6)
652 (forward-line 1)
653 (let ((half1s (point)))
654 (while (looking-at "[-! \\][ \t]")
655 (when (= (char-after) ?-) (delete-char 1) (insert "+"))
656 (forward-line 1))
657 (let ((half1 (delete-and-extract-region half1s (point))))
658 (unless (looking-at "^--- \\([0-9]+,-?[0-9]+\\) ----$")
659 (insert half1)
660 (error "Can't find matching `--- n1,n2 ----' line"))
661 (let ((str1 (match-string 1)))
662 (replace-match lines1 nil nil nil 1)
663 (forward-line 1)
664 (let ((half2s (point)))
665 (while (looking-at "[!+ \\][ \t]")
666 (when (= (char-after) ?+) (delete-char 1) (insert "-"))
667 (forward-line 1))
668 (let ((half2 (delete-and-extract-region half2s (point))))
669 (insert half1)
670 (goto-char half1s)
671 (insert half2)))
672 (goto-char pt-lines1)
673 (insert str1))))))
674 ;; a unified-diff hunk header
675 ((match-beginning 7)
676 (replace-match "@@ -\\8 +\\7 @@" nil)
677 (forward-line 1)
678 (let ((c (char-after)) first last)
679 (while (case (setq c (char-after))
680 (?- (setq first (or first (point)))
681 (delete-char 1) (insert "+") t)
682 (?+ (setq last (or last (point)))
683 (delete-char 1) (insert "-") t)
684 (?\\ t)
685 (t (when (and first last (< first last))
686 (let ((str
687 (save-excursion
688 (delete-and-extract-region first last))))
689 (insert str)))
690 (setq first nil last nil)
691 (equal ?\ c)))
692 (forward-line 1))))))))))
694 (defun diff-fixup-modifs (start end)
695 "Fixup the hunk headers (in case the buffer was modified).
696 START and END are either taken from the region (if a prefix arg is given) or
697 else cover the whole bufer."
698 (interactive (if current-prefix-arg
699 (list (mark) (point))
700 (list (point-min) (point-max))))
701 (let ((inhibit-read-only t))
702 (save-excursion
703 (goto-char end) (diff-end-of-hunk)
704 (let ((plus 0) (minus 0) (space 0) (bang 0))
705 (while (and (= (forward-line -1) 0) (<= start (point)))
706 (if (not (looking-at "\\(@@ .+ @@\\|[-*][-*][-*] .+ [-*][-*][-*][-*]\\)$"))
707 (case (char-after)
708 (?\ (incf space))
709 (?+ (incf plus))
710 (?- (incf minus))
711 (?! (incf bang))
712 (?\\ nil)
713 (t (setq space 0 plus 0 minus 0 bang 0)))
714 (cond
715 ((looking-at "@@ -[0-9]+,\\([0-9]*\\) \\+[0-9]+,\\([0-9]*\\) @@$")
716 (let* ((old1 (match-string 1))
717 (old2 (match-string 2))
718 (new1 (number-to-string (+ space minus)))
719 (new2 (number-to-string (+ space plus))))
720 (unless (string= new2 old2) (replace-match new2 t t nil 2))
721 (unless (string= new1 old1) (replace-match new1 t t nil 1))))
722 ((looking-at "--- \\([0-9]+\\),\\([0-9]*\\) ----$")
723 (when (> (+ space bang plus) 0)
724 (let* ((old1 (match-string 1))
725 (old2 (match-string 2))
726 (new (number-to-string
727 (+ space bang plus -1 (string-to-number old1)))))
728 (unless (string= new old2) (replace-match new t t nil 2)))))
729 ((looking-at "\\*\\*\\* \\([0-9]+\\),\\(-?[0-9]*\\) \\*\\*\\*\\*$")
730 (when (> (+ space bang minus) 0)
731 (let* ((old (match-string 1))
732 (new (format
733 (concat "%0" (number-to-string (length old)) "d")
734 (+ space bang minus -1 (string-to-number old)))))
735 (unless (string= new old) (replace-match new t t nil 2))))))
736 (setq space 0 plus 0 minus 0 bang 0)))))))
738 ;;;;
739 ;;;; Hooks
740 ;;;;
742 (defun diff-write-contents-hooks ()
743 "Fixup hunk headers if necessary."
744 (if (buffer-modified-p) (diff-fixup-modifs (point-min) (point-max)))
745 nil)
747 ;; It turns out that making changes in the buffer from within an
748 ;; *-change-function is asking for trouble, whereas making them
749 ;; from a post-command-hook doesn't pose much problems
750 (defvar diff-unhandled-changes nil)
751 (defun diff-after-change-function (beg end len)
752 "Remember to fixup the hunk header.
753 See `after-change-functions' for the meaning of BEG, END and LEN."
754 (when (and (not undo-in-progress) (not inhibit-read-only))
755 (if diff-unhandled-changes
756 (setq diff-unhandled-changes
757 (cons (min beg (car diff-unhandled-changes))
758 (max beg (cdr diff-unhandled-changes))))
759 (setq diff-unhandled-changes (cons beg end)))))
761 (defun diff-post-command-hook ()
762 "Fixup hunk headers if necessary."
763 (when (consp diff-unhandled-changes)
764 (ignore-errors
765 (save-excursion
766 (goto-char (car diff-unhandled-changes))
767 (unless (ignore-errors
768 (diff-beginning-of-hunk)
769 (save-excursion
770 (diff-end-of-hunk)
771 (> (point) (car diff-unhandled-changes))))
772 (goto-char (car diff-unhandled-changes))
773 (re-search-forward diff-hunk-header-re (cdr diff-unhandled-changes))
774 (diff-beginning-of-hunk))
775 (diff-fixup-modifs (point) (cdr diff-unhandled-changes))))
776 (setq diff-unhandled-changes nil)))
778 ;;;;
779 ;;;; The main function
780 ;;;;
782 ;;;###autoload
783 (define-derived-mode diff-mode fundamental-mode "Diff"
784 "Major mode for viewing/editing context diffs.
785 Supports unified and context diffs as well as (to a lesser extent) normal diffs.
786 When the buffer is read-only, the ESC prefix is not necessary.
787 This mode runs `diff-mode-hook'.
788 \\{diff-mode-map}"
789 (set (make-local-variable 'font-lock-defaults) diff-font-lock-defaults)
790 (set (make-local-variable 'outline-regexp) diff-outline-regexp)
791 ;; compile support
792 (set (make-local-variable 'compilation-file-regexp-alist)
793 diff-file-regexp-alist)
794 (set (make-local-variable 'compilation-error-regexp-alist)
795 diff-error-regexp-alist)
796 (when (string-match "\\.rej\\'" (or buffer-file-name ""))
797 (set (make-local-variable 'compilation-current-file)
798 (substring buffer-file-name 0 (match-beginning 0))))
799 (compilation-shell-minor-mode 1)
800 ;; setup change hooks
801 (toggle-read-only t)
802 (if (not diff-update-on-the-fly-flag)
803 (add-hook 'write-contents-hooks 'diff-write-contents-hooks)
804 (make-local-variable 'diff-unhandled-changes)
805 (add-hook (make-local-hook 'after-change-functions)
806 'diff-after-change-function nil t)
807 (add-hook (make-local-hook 'post-command-hook)
808 'diff-post-command-hook nil t))
809 ;; Neat trick from Dave Love to add more bindings in read-only mode:
810 (add-to-list (make-local-variable 'minor-mode-overriding-map-alist)
811 (cons 'buffer-read-only diff-mode-shared-map)))
813 ;;;###autoload
814 (define-minor-mode diff-minor-mode
815 "Minor mode for viewing/editing context diffs.
816 \\{diff-minor-mode-map}"
817 nil " Diff" nil
818 ;; FIXME: setup font-lock
819 ;; setup change hooks
820 (if (not diff-update-on-the-fly-flag)
821 (add-hook 'write-contents-hooks 'diff-write-contents-hooks)
822 (make-local-variable 'diff-unhandled-changes)
823 (add-hook (make-local-hook 'after-change-functions)
824 'diff-after-change-function nil t)
825 (add-hook (make-local-hook 'post-command-hook)
826 'diff-post-command-hook nil t)))
829 ;; provide the package
830 (provide 'diff-mode)
832 ;;; Change Log:
833 ;; $Log: diff-mode.el,v $
834 ;; Revision 1.5 2000/02/07 02:01:07 monnier
835 ;; (diff-kill-junk): New interactive function.
836 ;; (diff-reverse-direction): Use delete-and-extract-region.
837 ;; (diff-post-command-hook): Restrict the area so that the hook also works
838 ;; outside of any diff hunk. This is necessary for the minor-mode.
839 ;; (diff-mode): Use toggle-read-only and minor-mode-overriding-map-alist.
840 ;; (diff-minor-mode): Setup the hooks for header-hunk rewriting.
842 ;; Revision 1.4 1999/12/07 07:04:03 monnier
843 ;; * diff-mode.el (diff-mode-shared-map): fset'd and doc change.
844 ;; (diff-minor-mode, diff-minor-mode-prefix, diff-minor-mode-map):
845 ;; New code to support the minor mode version.
846 ;; (diff-recenter): New function.
847 ;; (diff-next-hunk, diff-next-file): Use it.
848 ;; (diff-remembered-files-alist): New var.
849 ;; (diff-merge-strings): New function.
850 ;; (diff-find-file-name): Make it smarter and use the user's input more.
851 ;; (diff-mode): Cosmetic changes.
853 ;; Revision 1.11 1999/10/09 23:38:29 monnier
854 ;; (diff-mode-load-hook): dropped.
855 ;; (auto-mode-alist): also catch *.diffs.
856 ;; (diff-find-file-name, diff-mode): add smarts to find the right file
857 ;; for *.rej files (that lack any file name indication).
859 ;; Revision 1.10 1999/09/30 15:32:11 monnier
860 ;; added support for "\ No newline at end of file".
862 ;; Revision 1.9 1999/09/15 00:01:13 monnier
863 ;; - added basic `compile' support.
864 ;; - have diff-kill-hunk call diff-kill-file if it's the only hunk.
865 ;; - diff-kill-file now tries to kill the leading garbage as well.
867 ;; Revision 1.8 1999/09/13 21:10:09 monnier
868 ;; - don't use CL in the autoloaded code
869 ;; - accept diffs using -T
871 ;; Revision 1.7 1999/09/05 20:53:03 monnier
872 ;; interface to ediff-patch
874 ;; Revision 1.6 1999/09/01 20:55:13 monnier
875 ;; (ediff=patch-file): add bindings to call ediff-patch.
876 ;; (diff-find-file-name): taken out of diff-goto-source.
877 ;; (diff-unified->context, diff-context->unified, diff-reverse-direction,
878 ;; diff-fixup-modifs): only use the region if a prefix arg is given.
880 ;; Revision 1.5 1999/08/31 19:18:52 monnier
881 ;; (diff-beginning-of-file, diff-prev-file): fixed wrong parenthesis.
883 ;; Revision 1.4 1999/08/31 13:01:44 monnier
884 ;; use `combine-after-change-calls' to minimize the slowdown of font-lock.
887 ;;; diff-mode.el ends here