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.4 1999/12/07 07:04:03 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)
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.
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:
38 ;; (autoload 'diff-mode "diff-mode" "Diff major mode" t)
39 ;; (add-to-list 'auto-mode-alist '("\\.\\(diffs?\\|patch\\|rej\\)\\'" . diff-mode))
43 ;; - reverse doesn't work with normal diffs.
44 ;; - (nitpick) the mark is not always quite right in diff-goto-source.
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'.
55 (eval-when-compile (require 'cl
))
58 (defgroup diff-mode
()
59 "Major-mode for viewing/editing diffs"
63 (defcustom diff-jump-to-old-file-flag nil
64 "*Non-nil means `diff-goto-source' jumps to the old file.
65 Else, it jumps to the new file."
69 (defcustom diff-update-on-the-fly-flag t
70 "*Non-nil means hunk headers are kept up-to-date on-the-fly.
71 When editing a diff file, the line numbers in the hunk headers
72 need to be kept consistent with the actual diff. This can
73 either be done on the fly (but this sometimes interacts poorly with the
74 undo mechanism) or whenever the file is written (can be slow
75 when editing big diffs)."
79 (defvar diff-mode-hook nil
80 "Run after setting up the `diff-mode' major mode.")
82 (defvar diff-outline-regexp
83 "\\([*+][*+][*+] [^0-9]\\|@@ ...\\|\\*\\*\\* [0-9].\\|--- [0-9]..\\)")
86 ;;;; keymap, menu, ...
89 (defmacro diff-defmap
(var bindings doc
)
91 (let ((m (make-keymap)))
93 (define-key m
(car b
) (cdr b
)))
97 (diff-defmap diff-mode-shared-map
98 '(;; from Pavel Machek's patch-mode
99 ("n" . diff-next-hunk
)
100 ("N" . diff-next-file
)
101 ("p" . diff-prev-hunk
)
102 ("P" . diff-prev-file
)
103 ("k" . diff-kill-hunk
)
104 ("K" . diff-kill-file
)
105 ;; from compilation-minor-mode
106 ("}" . diff-next-file
)
107 ("{" . diff-prev-file
)
108 ("\C-m" . diff-goto-source
)
109 ;; from XEmacs' diff-mode
111 ;;("\C-l" . diff-recenter)
112 ;;("." . diff-goto-source) ;display-buffer
113 ;;("f" . diff-goto-source) ;find-file
114 ("o" . diff-goto-source
) ;other-window
115 ;;("w" . diff-goto-source) ;other-frame
116 ;;("N" . diff-narrow)
117 ;;("h" . diff-show-header)
118 ;;("j" . diff-show-difference) ;jump to Nth diff
121 ("\177" . scroll-down
)
122 ;; our very own bindings
123 ("A" . diff-ediff-patch
)
124 ("r" . diff-restrict-view
)
125 ("R" . diff-reverse-direction
)
126 ("U" . diff-context-
>unified
)
127 ("C" . diff-unified-
>context
))
128 "Basic keymap for `diff-mode', bound to various prefix keys.")
129 (fset 'diff-mode-shared-map diff-mode-shared-map
)
131 (diff-defmap diff-mode-map
132 `(("\e" .
,diff-mode-shared-map
)
133 ;; from compilation-minor-mode
134 ("\C-c\C-c" . diff-goto-source
))
135 "Keymap for `diff-mode'. See also `diff-mode-shared-map'.")
137 (easy-menu-define diff-mode-menu diff-mode-map
138 "Menu for `diff-mode'."
140 ["Jump to Source" diff-goto-source t
]
141 ["Apply with Ediff" diff-ediff-patch t
]
143 ["Reverse direction" diff-reverse-direction t
]
144 ["Context -> Unified" diff-context-
>unified t
]
145 ["Unified -> Context" diff-unified-
>context t
]
146 ;;["Fixup Headers" diff-fixup-modifs (not buffer-read-only)]
149 (defcustom diff-minor-mode-prefix
"\C-cd"
150 "Prefix key for `diff-minor-mode' commands."
152 :type
'(choice (string "\e") (string "C-cd") string
))
154 (diff-defmap diff-minor-mode-map
155 `((,diff-minor-mode-prefix . diff-mode-shared-map
))
156 "Keymap for `diff-minor-mode'. See also `diff-mode-shared-map'.")
160 ;;;; font-lock support
163 (defface diff-file-header-face
164 '((((class color
) (background light
))
165 (:background
"grey70" :bold t
))
167 "diff-mode face used to highlight file header lines."
169 (defvar diff-file-header-face
'diff-file-header-face
)
171 (defface diff-index-face
172 '((((class color
) (background light
))
173 (:background
"grey70" :bold t
))
175 "diff-mode face used to highlight index header lines."
177 (defvar diff-index-face
'diff-index-face
)
179 (defface diff-hunk-header-face
180 '((((class color
) (background light
))
181 (:background
"grey85"))
183 "diff-mode face used to highlight hunk header lines."
185 (defvar diff-hunk-header-face
'diff-hunk-header-face
)
187 (defface diff-removed-face
189 "diff-mode face used to highlight removed lines."
191 (defvar diff-removed-face
'diff-removed-face
)
193 (defface diff-added-face
195 "diff-mode face used to highlight added lines."
197 (defvar diff-added-face
'diff-added-face
)
199 (defface diff-changed-face
201 "diff-mode face used to highlight changed lines."
203 (defvar diff-changed-face
'diff-changed-face
)
205 (defvar diff-font-lock-keywords
206 '(("^@@ .+ @@$" . diff-hunk-header-face
) ;unified
207 ("^--- .+ ----$" . diff-hunk-header-face
) ;context
208 ("^\\*\\*\\*.+\\*\\*\\*\n" . diff-hunk-header-face
) ;context
209 ("^\\(---\\|\\+\\+\\+\\|\\*\\*\\*\\) .*\n" . diff-file-header-face
)
210 ("^[0-9,]+[acd][0-9,]+$" . diff-hunk-header-face
)
211 ("^!.*\n" . diff-changed-face
) ;context
212 ("^[+>].*\n" . diff-added-face
)
213 ("^[-<].*\n" . diff-removed-face
)
214 ("^Index: .*\n" . diff-index-face
)
215 ("^[^-=+*!<>].*\n" . font-lock-comment-face
)))
217 (defconst diff-font-lock-defaults
218 '(diff-font-lock-keywords t nil nil nil
))
224 (defvar diff-file-regexp-alist
225 '(("Index: \\(.+\\)" 1)))
227 (defvar diff-error-regexp-alist
228 '(("@@ -\\([0-9]+\\),[0-9]+ \\+\\([0-9]+\\),[0-9]+ @@" nil
2)
229 ("--- \\([0-9]+\\),[0-9]+ ----" nil
1)
230 ("\\([0-9]+\\)\\(,[0-9]+\\)?[adc]\\([0-9]+\\)" nil
3)))
236 (defconst diff-hunk-header-re
"^\\(@@ .+ @@\\|\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\n\\*\\*\\* .+ \\*\\*\\*\\*\\|[0-9]+\\(,[0-9]+\\)?[acd][0-9]+\\(,[0-9]+\\)?\\)$")
237 (defconst diff-file-header-re
(concat "^\\(--- .+\n\\+\\+\\+\\|\\*\\*\\* .+\n---\\|[^-+!<>0-9@* ]\\).+\n" (substring diff-hunk-header-re
1)))
238 (defvar diff-narrowed-to nil
)
240 (defun diff-end-of-hunk (&optional style
)
241 (if (looking-at diff-hunk-header-re
) (goto-char (match-end 0)))
242 (re-search-forward (case style
243 (unified "^[^-+ \\]")
244 (context "^\\([^-+! \\][ \t]\\|--- .+ ----\\)")
245 (normal "^\\([<>\\][ \t]\\|---\\)")
250 (defun diff-beginning-of-hunk ()
252 (unless (looking-at diff-hunk-header-re
)
255 (re-search-backward diff-hunk-header-re
)
256 (error (error "Can't find the beginning of the hunk")))))
258 (defun diff-beginning-of-file ()
260 (unless (looking-at diff-file-header-re
)
263 (re-search-backward diff-file-header-re
)
264 (error (error "Can't find the beginning of the file")))))
266 (defun diff-end-of-file ()
267 (re-search-forward "^[-+!<>0-9@* \\]" nil t
)
268 (re-search-forward "^[^-+!<>0-9@* \\]" nil
'move
)
271 (defun diff-recenter ()
272 "Scroll if necessary to display the current hunk."
274 (when (eq (current-buffer) (window-buffer (selected-window)))
275 (let ((endpt (save-excursion (diff-end-of-hunk) (point))))
276 (unless (<= endpt
(window-end))
278 ;;(unless (<= endpt (window-end nil t))
279 ;; (set-window-start (selected-window) (point)))
282 (defun diff-next-hunk (&optional count
)
283 "Move to next (COUNT'th) hunk."
285 (unless count
(setq count
1))
286 (if (< count
0) (diff-prev-hunk (- count
))
287 (when (looking-at diff-hunk-header-re
) (incf count
))
289 (re-search-forward diff-hunk-header-re nil nil count
)
290 (error (error "Can't find next hunk")))
291 (goto-char (match-beginning 0))
294 (defun diff-prev-hunk (&optional count
)
295 "Move to previous (COUNT'th) hunk."
297 (unless count
(setq count
1))
298 (if (< count
0) (diff-next-hunk (- count
))
300 (re-search-backward diff-hunk-header-re nil nil count
)
301 (error (error "Can't find previous hunk")))))
303 (defun diff-next-file (&optional count
)
304 "Move to next (COUNT'th) file header."
306 (unless count
(setq count
1))
307 (if (< count
0) (diff-prev-file (- count
))
308 (when (looking-at diff-file-header-re
) (incf count
))
310 (re-search-forward diff-file-header-re nil nil count
)
311 (error (error "Can't find next file")))
312 (goto-char (match-beginning 0))
315 (defun diff-prev-file (&optional count
)
316 "Move to (COUNT'th) previous file header."
318 (unless count
(setq count
1))
319 (if (< count
0) (diff-next-file (- count
))
321 (re-search-backward diff-file-header-re nil nil count
)
322 (error (error "Can't find previous file")))))
324 (defun diff-restrict-view (&optional arg
)
325 "Restrict the view to the current hunk.
326 If the prefix ARG is given, restrict the view to the current file instead."
329 (if arg
(diff-beginning-of-file) (diff-beginning-of-hunk))
330 (narrow-to-region (point)
331 (progn (if arg
(diff-end-of-file) (diff-end-of-hunk))
333 (set (make-local-variable 'diff-narrowed-to
) (if arg
'file
'hunk
))))
336 (defun diff-kill-hunk ()
339 (diff-beginning-of-hunk)
340 (let ((start (point))
341 (firsthunk (save-excursion
343 (diff-beginning-of-file) (diff-next-hunk) (point))))
344 (nexthunk (save-excursion
346 (diff-next-hunk) (point))))
347 (nextfile (save-excursion
349 (diff-next-file) (point)))))
350 (if (and firsthunk
(= firsthunk start
)
352 (and nextfile
(> nexthunk nextfile
))))
353 ;; we're the only hunk for this file, so kill the file
356 (kill-region start
(point)))))
358 (defun diff-kill-file ()
359 "Kill current file's hunks."
361 (diff-beginning-of-file)
362 (let* ((start (point))
363 (prevhunk (save-excursion
365 (diff-prev-hunk) (point))))
366 (index (save-excursion
367 (re-search-backward "^Index: " prevhunk t
))))
368 (when index
(setq start index
))
370 (kill-region start
(point))))
372 (defun diff-kill-junk ()
373 "Kill spurious empty diffs."
376 (let ((inhibit-read-only t
))
377 (goto-char (point-min))
378 (while (re-search-forward (concat "^\\(Index: .*\n\\)"
379 "\\([^-+!* <>].*\n\\)*?"
380 "\\(\\(Index:\\) \\|"
381 diff-file-header-re
"\\)")
383 (delete-region (if (match-end 4) (match-beginning 0) (match-end 1))
385 (beginning-of-line)))))
388 ;;;; jump to other buffers
391 (defvar diff-remembered-files-alist nil
)
393 (defun diff-filename-drop-dir (file)
394 (when (string-match "/" file
) (substring file
(match-end 0))))
396 (defun diff-merge-strings (ancestor from to
)
397 "Merge the diff between ANCESTOR and FROM into TO.
398 Returns the merged string if successful or nil otherwise.
399 If ANCESTOR = FROM, returns TO.
400 If ANCESTOR = TO, returns FROM.
401 The heuristic is simplistic and only really works for cases
402 like \(diff-merge-strings \"b/foo\" \"b/bar\" \"/a/c/foo\")."
404 ;; AMB ANB CMD -> CND
405 ;; but that's ambiguous if `foo' or `bar' is empty:
406 ;; a/foo a/foo1 b/foo.c -> b/foo1.c but not 1b/foo.c or b/foo.c1
407 (let ((str (concat ancestor
" /|/ " from
" /|/ " to
)))
408 (when (and (string-match (concat
409 "\\`\\(.*?\\)\\(.*\\)\\(.*\\) /|/ "
410 "\\1\\(.*\\)\\3 /|/ "
411 "\\(.*\\(\\2\\).*\\)\\'") str
)
412 (equal to
(match-string 5 str
)))
413 (concat (substring str
(match-beginning 5) (match-beginning 6))
415 (substring str
(match-end 6) (match-end 5))))))
419 (defun diff-find-file-name (&optional old
)
420 "Return the file corresponding to the current patch.
421 Non-nil OLD means that we want the old file."
423 (unless (looking-at diff-file-header-re
)
424 (or (ignore-errors (diff-beginning-of-file))
425 (re-search-forward diff-file-header-re nil t
)))
426 (let* ((limit (save-excursion
428 (progn (diff-prev-hunk) (point))
429 (error (point-min)))))
431 (if (looking-at "[-*][-*][-*] \\(\\S-+\\)\\s-.*\n[-+][-+][-+] \\(\\S-+\\)\\s-.*$")
432 (list (if old
(match-string 1) (match-string 2))
433 (if old
(match-string 2) (match-string 1)))
434 (forward-line 1) nil
))
436 (when (save-excursion
437 (re-search-backward "^Index: \\(.+\\)" limit t
))
438 (list (match-string 1)))
440 (when (re-search-backward "^diff \\(-\\S-+ +\\)*\\(\\S-+\\)\\( +\\(\\S-+\\)\\)?" nil t
)
441 (list (if old
(match-string 2) (match-string 4))
442 (if old
(match-string 4) (match-string 2))))))
445 ;; use any previously used preference
446 (cdr (assoc fs diff-remembered-files-alist
))
447 ;; try to be clever and use previous choices as an inspiration
448 (dolist (rf diff-remembered-files-alist
)
449 (let ((newfile (diff-merge-strings (caar rf
) (car fs
) (cdr rf
))))
450 (if (and newfile
(file-exists-p newfile
)) (return newfile
))))
451 ;; look for each file in turn. If none found, try again but
452 ;; ignoring the first level of directory, ...
453 (do* ((files fs
(delq nil
(mapcar 'diff-filename-drop-dir files
)))
456 (setq file
(do* ((files files
(cdr files
))
457 (file (car files
) (car files
)))
458 ((or (null file
) (file-exists-p file
))
461 ;; <foo>.rej patches implicitly apply to <foo>
462 (and (string-match "\\.rej\\'" (or buffer-file-name
""))
463 (let ((file (substring buffer-file-name
0 (match-beginning 0))))
464 (when (file-exists-p file
) file
)))
465 ;; if all else fails, ask the user
466 (let ((file (read-file-name (format "Use file %s: " (or (first fs
) ""))
467 nil
(first fs
) t
(first fs
))))
468 (set (make-local-variable 'diff-remembered-files-alist
)
469 (cons (cons fs file
) diff-remembered-files-alist
))
472 (defun diff-goto-source (&optional other-file
)
473 "Jump to the corresponding source line.
474 `diff-jump-to-old-file-flag' (or its opposite if the OTHER-FILE prefix arg
475 is give) determines whether to jump to the old or the new file.
476 If the prefix arg is bigger than 8 (for example with \\[universal-argument] \\[universal-argument])
477 then `diff-jump-to-old-file-flag' is also set, for the next invokations."
480 (let ((old (if (not other-file
) diff-jump-to-old-file-flag
481 (not diff-jump-to-old-file-flag
))))
482 (when (> (prefix-numeric-value other-file
) 8)
483 (setq diff-jump-to-old-file-flag old
))
484 (diff-beginning-of-hunk)
485 (let* ((loc (if (not (looking-at "[-@*\n ]*\\([0-9,]+\\)\\([ acd+]+\\([0-9,]+\\)\\)?"))
486 (error "Can't find the hunk header")
487 (if old
(match-string 1)
488 (if (match-end 3) (match-string 3)
489 (unless (re-search-forward "^--- \\([0-9,]+\\)" nil t
)
490 (error "Can't find the hunk separator"))
492 (lines (if (string-match "^\\([0-9]*\\),\\([0-9]*\\)" loc
)
493 (cons (string-to-number (match-string 1 loc
))
494 (string-to-number (match-string 2 loc
)))
495 (cons (string-to-number loc
) nil
)))
496 (file (diff-find-file-name old
)))
497 (unless file
(error "Can't find the file"))
498 (pop-to-buffer (find-file-noselect file
))
499 (let* ((line (car lines
))
500 (span (if (or (null (cdr lines
)) (< (cdr lines
) 0)) 0
501 (if (< (cdr lines
) line
) (cdr lines
)
502 (- (cdr lines
) line
)))))
506 (push-mark (point) t t
)
507 (goto-line line
)))))))
510 (defun diff-ediff-patch ()
511 "Call `ediff-patch-file' on the current buffer."
514 (ediff-patch-file (current-buffer))
515 (wrong-number-of-arguments (ediff-patch-file))))
518 ;;;; Conversion functions
521 ;;(defvar diff-inhibit-after-change nil
522 ;; "Non-nil means inhibit `diff-mode's after-change functions.")
524 (defun diff-unified->context
(start end
)
525 "Convert unified diffs to context diffs.
526 START and END are either taken from the region (if a prefix arg is given) or
527 else cover the whole bufer."
528 (interactive (if current-prefix-arg
529 (list (mark) (point))
530 (list (point-min) (point-max))))
531 (unless (markerp end
) (setq end
(copy-marker end
)))
532 (let (;;(diff-inhibit-after-change t)
533 (inhibit-read-only t
))
536 (while (and (re-search-forward "^\\(\\(---\\) .+\n\\(\\+\\+\\+\\) .+\\|@@ -\\([0-9]+\\),\\([0-9]+\\) \\+\\([0-9]+\\),\\([0-9]+\\) @@\\)$" nil t
)
538 (combine-after-change-calls
539 (if (match-beginning 2)
540 ;;we matched a file header
542 ;; use reverse order to make sure the indices are kept valid
543 (replace-match "---" t t nil
3)
544 (replace-match "***" t t nil
2))
545 ;; we matched a hunk header
546 (let ((line1 (match-string 4))
547 (lines1 (match-string 5))
548 (line2 (match-string 6))
549 (lines2 (match-string 7)))
551 (concat "***************\n*** " line1
","
552 (number-to-string (+ (string-to-number line1
)
553 (string-to-number lines1
)
557 (narrow-to-region (point)
558 (progn (diff-end-of-hunk 'unified
) (point)))
559 (let ((hunk (buffer-string)))
560 (goto-char (point-min))
561 (if (not (save-excursion (re-search-forward "^-" nil t
)))
562 (delete-region (point) (point-max))
563 (goto-char (point-max))
564 (let ((modif nil
) last-pt
)
565 (while (progn (setq last-pt
(point))
566 (= (forward-line -
1) 0))
568 (?
(insert " ") (setq modif nil
) (backward-char 1))
569 (?
+ (delete-region (point) last-pt
) (setq modif t
))
571 (progn (forward-char 1)
576 (?
\\ (when (save-excursion (forward-line -
1)
578 (delete-region (point) last-pt
) (setq modif t
)))
579 (t (setq modif nil
))))))
580 (goto-char (point-max))
582 (insert "--- " line2
","
583 (number-to-string (+ (string-to-number line2
)
584 (string-to-number lines2
)
585 -
1)) " ----\n" hunk
))
586 ;;(goto-char (point-min))
588 (if (not (save-excursion (re-search-forward "^+" nil t
)))
589 (delete-region (point) (point-max))
590 (let ((modif nil
) (delete nil
))
593 (?
(insert " ") (setq modif nil
) (backward-char 1))
594 (?-
(setq delete t
) (setq modif t
))
596 (progn (forward-char 1)
601 (?
\\ (when (save-excursion (forward-line 1)
603 (setq delete t
) (setq modif t
)))
604 (t (setq modif nil
)))
605 (let ((last-pt (point)))
608 (delete-region last-pt
(point))
609 (setq delete nil
)))))))))))))))
611 (defun diff-context->unified
(start end
)
612 "Convert context diffs to unified diffs.
613 START and END are either taken from the region (if a prefix arg is given) or
614 else cover the whole bufer."
615 (interactive (if current-prefix-arg
616 (list (mark) (point))
617 (list (point-min) (point-max))))
618 (unless (markerp end
) (setq end
(copy-marker end
)))
619 (let (;;(diff-inhibit-after-change t)
620 (inhibit-read-only t
))
623 (while (and (re-search-forward "^\\(\\(\\*\\*\\*\\) .+\n\\(---\\) .+\\|\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\n\\*\\*\\* \\([0-9]+\\),\\(-?[0-9]+\\) \\*\\*\\*\\*\\)$" nil t
)
625 (combine-after-change-calls
626 (if (match-beginning 2)
627 ;; we matched a file header
629 ;; use reverse order to make sure the indices are kept valid
630 (replace-match "+++" t t nil
3)
631 (replace-match "---" t t nil
2))
632 ;; we matched a hunk header
633 (let ((line1s (match-string 4))
634 (line1e (match-string 5))
635 (pt1 (match-beginning 0)))
637 (unless (re-search-forward
638 "^--- \\([0-9]+\\),\\(-?[0-9]+\\) ----$" nil t
)
639 (error "Can't find matching `--- n1,n2 ----' line"))
640 (let ((line2s (match-string 1))
641 (line2e (match-string 2))
643 (delete-region (progn (beginning-of-line) (point))
644 (progn (forward-line 1) (point)))
648 (while (< (point) pt2
)
650 ((?
! ?-
) (delete-char 2) (insert "-") (forward-line 1))
651 (?\
;merge with the other half of the chunk
654 (goto-char pt2
) (forward-line 1) (point)))
655 (c (char-after pt2
)))
659 (prog1 (buffer-substring (+ pt2
2) endline2
)
660 (delete-region pt2 endline2
))))
661 (?\
;FIXME: check consistency
662 (delete-region pt2 endline2
)
665 (?
\\ (forward-line 1))
666 (t (delete-char 1) (forward-line 1)))))
667 (t (forward-line 1))))
668 (while (looking-at "[+! ] ")
669 (if (/= (char-after) ?
!) (forward-char 1)
670 (delete-char 1) (insert "+"))
671 (delete-char 1) (forward-line 1))
674 (insert "@@ -" line1s
","
675 (number-to-string (- (string-to-number line1e
)
676 (string-to-number line1s
)
679 (number-to-string (- (string-to-number line2e
)
680 (string-to-number line2s
)
683 (defun diff-reverse-direction (start end
)
684 "Reverse the direction of the diffs.
685 START and END are either taken from the region (if a prefix arg is given) or
686 else cover the whole bufer."
687 (interactive (if current-prefix-arg
688 (list (mark) (point))
689 (list (point-min) (point-max))))
690 (unless (markerp end
) (setq end
(copy-marker end
)))
691 (let (;;(diff-inhibit-after-change t)
692 (inhibit-read-only t
))
695 (while (and (re-search-forward "^\\(\\([-*][-*][-*] \\)\\(.+\\)\n\\([-+][-+][-+] \\)\\(.+\\)\\|\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\n\\*\\*\\* \\(.+\\) \\*\\*\\*\\*\\|@@ -\\(.+\\) \\+\\(.+\\) @@\\)$" nil t
)
697 (combine-after-change-calls
700 ((match-beginning 2) (replace-match "\\2\\5\n\\4\\3" nil
))
701 ;; a context-diff hunk header
703 (let ((pt-lines1 (match-beginning 6))
704 (lines1 (match-string 6)))
705 (replace-match "" nil nil nil
6)
707 (let ((half1s (point)))
708 (while (looking-at "[-! \\][ \t]")
709 (when (= (char-after) ?-
) (delete-char 1) (insert "+"))
711 (let ((half1 (delete-and-extract-region half1s
(point))))
712 (unless (looking-at "^--- \\([0-9]+,-?[0-9]+\\) ----$")
714 (error "Can't find matching `--- n1,n2 ----' line"))
715 (let ((str1 (match-string 1)))
716 (replace-match lines1 nil nil nil
1)
718 (let ((half2s (point)))
719 (while (looking-at "[!+ \\][ \t]")
720 (when (= (char-after) ?
+) (delete-char 1) (insert "-"))
722 (let ((half2 (delete-and-extract-region half2s
(point))))
726 (goto-char pt-lines1
)
728 ;; a unified-diff hunk header
730 (replace-match "@@ -\\8 +\\7 @@" nil
)
732 (let ((c (char-after)) first last
)
733 (while (case (setq c
(char-after))
734 (?-
(setq first
(or first
(point)))
735 (delete-char 1) (insert "+") t
)
736 (?
+ (setq last
(or last
(point)))
737 (delete-char 1) (insert "-") t
)
739 (t (when (and first last
(< first last
))
742 (delete-and-extract-region first last
))))
744 (setq first nil last nil
)
746 (forward-line 1))))))))))
748 (defun diff-fixup-modifs (start end
)
749 "Fixup the hunk headers (in case the buffer was modified).
750 START and END are either taken from the region (if a prefix arg is given) or
751 else cover the whole bufer."
752 (interactive (if current-prefix-arg
753 (list (mark) (point))
754 (list (point-min) (point-max))))
755 (let ((inhibit-read-only t
))
757 (goto-char end
) (diff-end-of-hunk)
758 (let ((plus 0) (minus 0) (space 0) (bang 0))
759 (while (and (= (forward-line -
1) 0) (<= start
(point)))
760 (if (not (looking-at "\\(@@ .+ @@\\|[-*][-*][-*] .+ [-*][-*][-*][-*]\\)$"))
767 (t (setq space
0 plus
0 minus
0 bang
0)))
769 ((looking-at "@@ -[0-9]+,\\([0-9]*\\) \\+[0-9]+,\\([0-9]*\\) @@$")
770 (let* ((old1 (match-string 1))
771 (old2 (match-string 2))
772 (new1 (number-to-string (+ space minus
)))
773 (new2 (number-to-string (+ space plus
))))
774 (unless (string= new2 old2
) (replace-match new2 t t nil
2))
775 (unless (string= new1 old1
) (replace-match new1 t t nil
1))))
776 ((looking-at "--- \\([0-9]+\\),\\([0-9]*\\) ----$")
777 (when (> (+ space bang plus
) 0)
778 (let* ((old1 (match-string 1))
779 (old2 (match-string 2))
780 (new (number-to-string
781 (+ space bang plus -
1 (string-to-number old1
)))))
782 (unless (string= new old2
) (replace-match new t t nil
2)))))
783 ((looking-at "\\*\\*\\* \\([0-9]+\\),\\(-?[0-9]*\\) \\*\\*\\*\\*$")
784 (when (> (+ space bang minus
) 0)
785 (let* ((old (match-string 1))
787 (concat "%0" (number-to-string (length old
)) "d")
788 (+ space bang minus -
1 (string-to-number old
)))))
789 (unless (string= new old
) (replace-match new t t nil
2))))))
790 (setq space
0 plus
0 minus
0 bang
0)))))))
796 (defun diff-write-contents-hooks ()
797 "Fixup hunk headers if necessary."
798 (if (buffer-modified-p) (diff-fixup-modifs (point-min) (point-max)))
801 ;; It turns out that making changes in the buffer from within an
802 ;; *-change-function is asking for trouble, whereas making them
803 ;; from a post-command-hook doesn't pose much problems
804 (defvar diff-unhandled-changes nil
)
805 (defun diff-after-change-function (beg end len
)
806 "Remember to fixup the hunk header.
807 See `after-change-functions' for the meaning of BEG, END and LEN."
808 (when (and (not undo-in-progress
) (not inhibit-read-only
))
809 (if diff-unhandled-changes
810 (setq diff-unhandled-changes
811 (cons (min beg
(car diff-unhandled-changes
))
812 (max beg
(cdr diff-unhandled-changes
))))
813 (setq diff-unhandled-changes
(cons beg end
)))))
815 (defun diff-post-command-hook ()
816 "Fixup hunk headers if necessary."
817 (when (consp diff-unhandled-changes
)
820 (goto-char (car diff-unhandled-changes
))
821 (unless (ignore-errors
822 (diff-beginning-of-hunk)
825 (> (point) (car diff-unhandled-changes
))))
826 (goto-char (car diff-unhandled-changes
))
827 (re-search-forward diff-hunk-header-re
(cdr diff-unhandled-changes
))
828 (diff-beginning-of-hunk))
829 (diff-fixup-modifs (point) (cdr diff-unhandled-changes
))))
830 (setq diff-unhandled-changes nil
)))
833 ;;;; The main function
838 "Major mode for viewing/editing context diffs.
839 Supports unified and context diffs as well as (to a lesser extent) normal diffs.
840 When the buffer is read-only, the ESC prefix is not necessary.
841 This mode runs `diff-mode-hook'.
844 (kill-all-local-variables)
845 (setq major-mode
'diff-mode
)
846 (setq mode-name
"Diff")
847 (use-local-map diff-mode-map
)
848 (set (make-local-variable 'font-lock-defaults
) diff-font-lock-defaults
)
849 (set (make-local-variable 'outline-regexp
) diff-outline-regexp
)
851 (set (make-local-variable 'compilation-file-regexp-alist
)
852 diff-file-regexp-alist
)
853 (set (make-local-variable 'compilation-error-regexp-alist
)
854 diff-error-regexp-alist
)
855 (when (string-match "\\.rej\\'" (or buffer-file-name
""))
856 (set (make-local-variable 'compilation-current-file
)
857 (substring buffer-file-name
0 (match-beginning 0))))
858 (compilation-shell-minor-mode 1)
859 ;; setup change hooks
861 (if (not diff-update-on-the-fly-flag
)
862 (add-hook 'write-contents-hooks
'diff-write-contents-hooks
)
863 (make-local-variable 'diff-unhandled-changes
)
864 (add-hook (make-local-hook 'after-change-functions
)
865 'diff-after-change-function nil t
)
866 (add-hook (make-local-hook 'post-command-hook
)
867 'diff-post-command-hook nil t
))
868 ;; Neat trick from Dave Love to add more bindings in read-only mode:
869 (add-to-list (make-local-variable 'minor-mode-overriding-map-alist
)
870 (cons 'buffer-read-only diff-mode-shared-map
))
871 (run-hooks 'diff-mode-hook
))
874 (define-minor-mode diff-minor-mode
875 "Minor mode for viewing/editing context diffs.
876 \\{diff-minor-mode-map}"
878 ;; FIXME: setup font-lock
879 ;; setup change hooks
880 (if (not diff-update-on-the-fly-flag
)
881 (add-hook 'write-contents-hooks
'diff-write-contents-hooks
)
882 (make-local-variable 'diff-unhandled-changes
)
883 (add-hook (make-local-hook 'after-change-functions
)
884 'diff-after-change-function nil t
)
885 (add-hook (make-local-hook 'post-command-hook
)
886 'diff-post-command-hook nil t
)))
889 ;; provide the package
893 ;; $Log: diff-mode.el,v $
894 ;; Revision 1.4 1999/12/07 07:04:03 monnier
895 ;; * diff-mode.el (diff-mode-shared-map): fset'd and doc change.
896 ;; (diff-minor-mode, diff-minor-mode-prefix, diff-minor-mode-map):
897 ;; New code to support the minor mode version.
898 ;; (diff-recenter): New function.
899 ;; (diff-next-hunk, diff-next-file): Use it.
900 ;; (diff-remembered-files-alist): New var.
901 ;; (diff-merge-strings): New function.
902 ;; (diff-find-file-name): Make it smarter and use the user's input more.
903 ;; (diff-mode): Cosmetic changes.
905 ;; Revision 1.11 1999/10/09 23:38:29 monnier
906 ;; (diff-mode-load-hook): dropped.
907 ;; (auto-mode-alist): also catch *.diffs.
908 ;; (diff-find-file-name, diff-mode): add smarts to find the right file
909 ;; for *.rej files (that lack any file name indication).
911 ;; Revision 1.10 1999/09/30 15:32:11 monnier
912 ;; added support for "\ No newline at end of file".
914 ;; Revision 1.9 1999/09/15 00:01:13 monnier
915 ;; - added basic `compile' support.
916 ;; - have diff-kill-hunk call diff-kill-file if it's the only hunk.
917 ;; - diff-kill-file now tries to kill the leading garbage as well.
919 ;; Revision 1.8 1999/09/13 21:10:09 monnier
920 ;; - don't use CL in the autoloaded code
921 ;; - accept diffs using -T
923 ;; Revision 1.7 1999/09/05 20:53:03 monnier
924 ;; interface to ediff-patch
926 ;; Revision 1.6 1999/09/01 20:55:13 monnier
927 ;; (ediff=patch-file): add bindings to call ediff-patch.
928 ;; (diff-find-file-name): taken out of diff-goto-source.
929 ;; (diff-unified->context, diff-context->unified, diff-reverse-direction,
930 ;; diff-fixup-modifs): only use the region if a prefix arg is given.
932 ;; Revision 1.5 1999/08/31 19:18:52 monnier
933 ;; (diff-beginning-of-file, diff-prev-file): fixed wrong parenthesis.
935 ;; Revision 1.4 1999/08/31 13:01:44 monnier
936 ;; use `combine-after-change-calls' to minimize the slowdown of font-lock.
939 ;;; diff-mode.el ends here