(font-lock-keywords): Fix doc for multiline matches.
[emacs.git] / lisp / diff-mode.el
blob2c0553efcc2074d11bea480b6c1a5c5dad5bc4de
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$
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'.
53 ;;; Code:
55 (eval-when-compile (require 'cl))
58 (defgroup diff-mode ()
59 "Major-mode for viewing/editing diffs"
60 :group 'tools
61 :group 'diff)
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."
66 :group 'diff-mode
67 :type '(boolean))
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)."
76 :group 'diff-mode
77 :type '(boolean))
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]..\\)")
85 ;;;;
86 ;;;; keymap, menu, ...
87 ;;;;
89 (defmacro diff-defmap (var bindings doc)
90 `(defvar ,var
91 (let ((m (make-keymap)))
92 (dolist (b ,bindings)
93 (define-key m (car b) (cdr b)))
95 ,doc))
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
110 ("W" . widen)
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
119 ;;("q" . diff-quit)
120 (" " . scroll-up)
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'."
139 '("Diff"
140 ["Jump to Source" diff-goto-source t]
141 ["Apply with Ediff" diff-ediff-patch t]
142 ["-----" nil nil]
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."
151 :group 'diff-mode
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'.")
159 ;;;;
160 ;;;; font-lock support
161 ;;;;
163 (defface diff-file-header-face
164 '((((class color) (background light))
165 (:background "grey70" :bold t))
166 (t (:bold t)))
167 "diff-mode face used to highlight file header lines."
168 :group 'diff-mode)
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))
174 (t (:bold t)))
175 "diff-mode face used to highlight index header lines."
176 :group 'diff-mode)
177 (defvar diff-index-face 'diff-index-face)
179 (defface diff-hunk-header-face
180 '((((class color) (background light))
181 (:background "grey85"))
182 (t (:bold t)))
183 "diff-mode face used to highlight hunk header lines."
184 :group 'diff-mode)
185 (defvar diff-hunk-header-face 'diff-hunk-header-face)
187 (defface diff-removed-face
188 '((t ()))
189 "diff-mode face used to highlight removed lines."
190 :group 'diff-mode)
191 (defvar diff-removed-face 'diff-removed-face)
193 (defface diff-added-face
194 '((t ()))
195 "diff-mode face used to highlight added lines."
196 :group 'diff-mode)
197 (defvar diff-added-face 'diff-added-face)
199 (defface diff-changed-face
200 '((t ()))
201 "diff-mode face used to highlight changed lines."
202 :group 'diff-mode)
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))
220 ;;;;
221 ;;;; Compile support
222 ;;;;
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)))
232 ;;;;
233 ;;;; Movement
234 ;;;;
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]\\|---\\)")
246 (t "^[^-+!<> \\]"))
247 nil 'move)
248 (beginning-of-line))
250 (defun diff-beginning-of-hunk ()
251 (beginning-of-line)
252 (unless (looking-at diff-hunk-header-re)
253 (forward-line 1)
254 (condition-case ()
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 ()
259 (beginning-of-line)
260 (unless (looking-at diff-file-header-re)
261 (forward-line 2)
262 (condition-case ()
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)
269 (beginning-of-line))
271 (defun diff-recenter ()
272 "Scroll if necessary to display the current hunk."
273 (interactive)
274 (when (eq (current-buffer) (window-buffer (selected-window)))
275 (let ((endpt (save-excursion (diff-end-of-hunk) (point))))
276 (unless (<= endpt (window-end))
277 (recenter)
278 ;;(unless (<= endpt (window-end nil t))
279 ;; (set-window-start (selected-window) (point)))
280 ))))
282 (defun diff-next-hunk (&optional count)
283 "Move to next (COUNT'th) hunk."
284 (interactive "p")
285 (unless count (setq count 1))
286 (if (< count 0) (diff-prev-hunk (- count))
287 (when (looking-at diff-hunk-header-re) (incf count))
288 (condition-case ()
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))
292 (diff-recenter)))
294 (defun diff-prev-hunk (&optional count)
295 "Move to previous (COUNT'th) hunk."
296 (interactive "p")
297 (unless count (setq count 1))
298 (if (< count 0) (diff-next-hunk (- count))
299 (condition-case ()
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."
305 (interactive "p")
306 (unless count (setq count 1))
307 (if (< count 0) (diff-prev-file (- count))
308 (when (looking-at diff-file-header-re) (incf count))
309 (condition-case ()
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))
313 (diff-recenter)))
315 (defun diff-prev-file (&optional count)
316 "Move to (COUNT'th) previous file header."
317 (interactive "p")
318 (unless count (setq count 1))
319 (if (< count 0) (diff-next-file (- count))
320 (condition-case ()
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."
327 (interactive "P")
328 (save-excursion
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))
332 (point)))
333 (set (make-local-variable 'diff-narrowed-to) (if arg 'file 'hunk))))
336 (defun diff-kill-hunk ()
337 "Kill current hunk."
338 (interactive)
339 (diff-beginning-of-hunk)
340 (let ((start (point))
341 (firsthunk (save-excursion
342 (ignore-errors
343 (diff-beginning-of-file) (diff-next-hunk) (point))))
344 (nexthunk (save-excursion
345 (ignore-errors
346 (diff-next-hunk) (point))))
347 (nextfile (save-excursion
348 (ignore-errors
349 (diff-next-file) (point)))))
350 (if (and firsthunk (= firsthunk start)
351 (or (null nexthunk)
352 (and nextfile (> nexthunk nextfile))))
353 ;; we're the only hunk for this file, so kill the file
354 (diff-kill-file)
355 (diff-end-of-hunk)
356 (kill-region start (point)))))
358 (defun diff-kill-file ()
359 "Kill current file's hunks."
360 (interactive)
361 (diff-beginning-of-file)
362 (let* ((start (point))
363 (prevhunk (save-excursion
364 (ignore-errors
365 (diff-prev-hunk) (point))))
366 (index (save-excursion
367 (re-search-backward "^Index: " prevhunk t))))
368 (when index (setq start index))
369 (diff-end-of-file)
370 (kill-region start (point))))
372 ;;;;
373 ;;;; jump to other buffers
374 ;;;;
376 (defvar diff-remembered-files-alist nil)
378 (defun diff-filename-drop-dir (file)
379 (when (string-match "/" file) (substring file (match-end 0))))
381 (defun diff-merge-strings (ancestor from to)
382 "Merge the diff between ANCESTOR and FROM into TO.
383 Returns the merged string if successful or nil otherwise.
384 If ANCESTOR = FROM, returns TO.
385 If ANCESTOR = TO, returns FROM.
386 The heuristic is simplistic and only really works for cases
387 like \(diff-merge-strings \"b/foo\" \"b/bar\" \"/a/c/foo\")."
388 ;; Ideally, we want:
389 ;; AMB ANB CMD -> CND
390 ;; but that's ambiguous if `foo' or `bar' is empty:
391 ;; a/foo a/foo1 b/foo.c -> b/foo1.c but not 1b/foo.c or b/foo.c1
392 (let ((str (concat ancestor " /|/ " from " /|/ " to)))
393 (when (and (string-match (concat
394 "\\`\\(.*?\\)\\(.*\\)\\(.*\\) /|/ "
395 "\\1\\(.*\\)\\3 /|/ "
396 "\\(.*\\(\\2\\).*\\)\\'") str)
397 (equal to (match-string 5 str)))
398 (concat (substring str (match-beginning 5) (match-beginning 6))
399 (match-string 4 str)
400 (substring str (match-end 6) (match-end 5))))))
404 (defun diff-find-file-name (&optional old)
405 "Return the file corresponding to the current patch.
406 Non-nil OLD means that we want the old file."
407 (save-excursion
408 (unless (looking-at diff-file-header-re)
409 (or (ignore-errors (diff-beginning-of-file))
410 (re-search-forward diff-file-header-re nil t)))
411 (let* ((limit (save-excursion
412 (condition-case ()
413 (progn (diff-prev-hunk) (point))
414 (error (point-min)))))
415 (header-files
416 (if (looking-at "[-*][-*][-*] \\(\\S-+\\)\\s-.*\n[-+][-+][-+] \\(\\S-+\\)\\s-.*$")
417 (list (if old (match-string 1) (match-string 2))
418 (if old (match-string 2) (match-string 1)))
419 (forward-line 1) nil))
420 (fs (append
421 (when (save-excursion
422 (re-search-backward "^Index: \\(.+\\)" limit t))
423 (list (match-string 1)))
424 header-files
425 (when (re-search-backward "^diff \\(-\\S-+ +\\)*\\(\\S-+\\)\\( +\\(\\S-+\\)\\)?" nil t)
426 (list (if old (match-string 2) (match-string 4))
427 (if old (match-string 4) (match-string 2))))))
428 (fs (delq nil fs)))
430 ;; use any previously used preference
431 (cdr (assoc fs diff-remembered-files-alist))
432 ;; try to be clever and use previous choices as an inspiration
433 (dolist (rf diff-remembered-files-alist)
434 (let ((newfile (diff-merge-strings (caar rf) (car fs) (cdr rf))))
435 (if (and newfile (file-exists-p newfile)) (return newfile))))
436 ;; look for each file in turn. If none found, try again but
437 ;; ignoring the first level of directory, ...
438 (do* ((files fs (delq nil (mapcar 'diff-filename-drop-dir files)))
439 (file nil nil))
440 ((or (null files)
441 (setq file (do* ((files files (cdr files))
442 (file (car files) (car files)))
443 ((or (null file) (file-exists-p file))
444 file))))
445 file))
446 ;; <foo>.rej patches implicitly apply to <foo>
447 (and (string-match "\\.rej\\'" (or buffer-file-name ""))
448 (let ((file (substring buffer-file-name 0 (match-beginning 0))))
449 (when (file-exists-p file) file)))
450 ;; if all else fails, ask the user
451 (let ((file (read-file-name (format "Use file %s: " (or (first fs) ""))
452 nil (first fs) t (first fs))))
453 (set (make-local-variable 'diff-remembered-files-alist)
454 (cons (cons fs file) diff-remembered-files-alist))
455 file)))))
457 (defun diff-goto-source (&optional other-file)
458 "Jump to the corresponding source line.
459 `diff-jump-to-old-file-flag' (or its opposite if the OTHER-FILE prefix arg
460 is give) determines whether to jump to the old or the new file.
461 If the prefix arg is bigger than 8 (for example with \\[universal-argument] \\[universal-argument])
462 then `diff-jump-to-old-file-flag' is also set, for the next invokations."
463 (interactive "P")
464 (save-excursion
465 (let ((old (if (not other-file) diff-jump-to-old-file-flag
466 (not diff-jump-to-old-file-flag))))
467 (when (> (prefix-numeric-value other-file) 8)
468 (setq diff-jump-to-old-file-flag old))
469 (diff-beginning-of-hunk)
470 (let* ((loc (if (not (looking-at "[-@*\n ]*\\([0-9,]+\\)\\([ acd+]+\\([0-9,]+\\)\\)?"))
471 (error "Can't find the hunk header")
472 (if old (match-string 1)
473 (if (match-end 3) (match-string 3)
474 (unless (re-search-forward "^--- \\([0-9,]+\\)" nil t)
475 (error "Can't find the hunk separator"))
476 (match-string 1)))))
477 (lines (if (string-match "^\\([0-9]*\\),\\([0-9]*\\)" loc)
478 (cons (string-to-number (match-string 1 loc))
479 (string-to-number (match-string 2 loc)))
480 (cons (string-to-number loc) nil)))
481 (file (diff-find-file-name old)))
482 (unless file (error "Can't find the file"))
483 (pop-to-buffer (find-file-noselect file))
484 (let* ((line (car lines))
485 (span (if (or (null (cdr lines)) (< (cdr lines) 0)) 0
486 (if (< (cdr lines) line) (cdr lines)
487 (- (cdr lines) line)))))
488 (ignore-errors
489 (goto-line line)
490 (forward-line span)
491 (push-mark (point) t t)
492 (goto-line line)))))))
495 (defun diff-ediff-patch ()
496 "Call `ediff-patch-file' on the current buffer."
497 (interactive)
498 (condition-case err
499 (ediff-patch-file (current-buffer))
500 (wrong-number-of-arguments (ediff-patch-file))))
502 ;;;;
503 ;;;; Conversion functions
504 ;;;;
506 ;;(defvar diff-inhibit-after-change nil
507 ;; "Non-nil means inhibit `diff-mode's after-change functions.")
509 (defun diff-unified->context (start end)
510 "Convert unified diffs to context diffs.
511 START and END are either taken from the region (if a prefix arg is given) or
512 else cover the whole bufer."
513 (interactive (if current-prefix-arg
514 (list (mark) (point))
515 (list (point-min) (point-max))))
516 (unless (markerp end) (setq end (copy-marker end)))
517 (let (;;(diff-inhibit-after-change t)
518 (inhibit-read-only t))
519 (save-excursion
520 (goto-char start)
521 (while (and (re-search-forward "^\\(\\(---\\) .+\n\\(\\+\\+\\+\\) .+\\|@@ -\\([0-9]+\\),\\([0-9]+\\) \\+\\([0-9]+\\),\\([0-9]+\\) @@\\)$" nil t)
522 (< (point) end))
523 (combine-after-change-calls
524 (if (match-beginning 2)
525 ;;we matched a file header
526 (progn
527 ;; use reverse order to make sure the indices are kept valid
528 (replace-match "---" t t nil 3)
529 (replace-match "***" t t nil 2))
530 ;; we matched a hunk header
531 (let ((line1 (match-string 4))
532 (lines1 (match-string 5))
533 (line2 (match-string 6))
534 (lines2 (match-string 7)))
535 (replace-match
536 (concat "***************\n*** " line1 ","
537 (number-to-string (+ (string-to-number line1)
538 (string-to-number lines1)
539 -1)) " ****"))
540 (forward-line 1)
541 (save-restriction
542 (narrow-to-region (point)
543 (progn (diff-end-of-hunk 'unified) (point)))
544 (let ((hunk (buffer-string)))
545 (goto-char (point-min))
546 (if (not (save-excursion (re-search-forward "^-" nil t)))
547 (delete-region (point) (point-max))
548 (goto-char (point-max))
549 (let ((modif nil) last-pt)
550 (while (progn (setq last-pt (point))
551 (= (forward-line -1) 0))
552 (case (char-after)
553 (? (insert " ") (setq modif nil) (backward-char 1))
554 (?+ (delete-region (point) last-pt) (setq modif t))
555 (?- (if (not modif)
556 (progn (forward-char 1)
557 (insert " "))
558 (delete-char 1)
559 (insert "! "))
560 (backward-char 2))
561 (?\\ (when (save-excursion (forward-line -1)
562 (= (char-after) ?+))
563 (delete-region (point) last-pt) (setq modif t)))
564 (t (setq modif nil))))))
565 (goto-char (point-max))
566 (save-excursion
567 (insert "--- " line2 ","
568 (number-to-string (+ (string-to-number line2)
569 (string-to-number lines2)
570 -1)) " ----\n" hunk))
571 ;;(goto-char (point-min))
572 (forward-line 1)
573 (if (not (save-excursion (re-search-forward "^+" nil t)))
574 (delete-region (point) (point-max))
575 (let ((modif nil) (delete nil))
576 (while (not (eobp))
577 (case (char-after)
578 (? (insert " ") (setq modif nil) (backward-char 1))
579 (?- (setq delete t) (setq modif t))
580 (?+ (if (not modif)
581 (progn (forward-char 1)
582 (insert " "))
583 (delete-char 1)
584 (insert "! "))
585 (backward-char 2))
586 (?\\ (when (save-excursion (forward-line 1)
587 (not (eobp)))
588 (setq delete t) (setq modif t)))
589 (t (setq modif nil)))
590 (let ((last-pt (point)))
591 (forward-line 1)
592 (when delete
593 (delete-region last-pt (point))
594 (setq delete nil)))))))))))))))
596 (defun diff-context->unified (start end)
597 "Convert context diffs to unified diffs.
598 START and END are either taken from the region (if a prefix arg is given) or
599 else cover the whole bufer."
600 (interactive (if current-prefix-arg
601 (list (mark) (point))
602 (list (point-min) (point-max))))
603 (unless (markerp end) (setq end (copy-marker end)))
604 (let (;;(diff-inhibit-after-change t)
605 (inhibit-read-only t))
606 (save-excursion
607 (goto-char start)
608 (while (and (re-search-forward "^\\(\\(\\*\\*\\*\\) .+\n\\(---\\) .+\\|\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\n\\*\\*\\* \\([0-9]+\\),\\(-?[0-9]+\\) \\*\\*\\*\\*\\)$" nil t)
609 (< (point) end))
610 (combine-after-change-calls
611 (if (match-beginning 2)
612 ;; we matched a file header
613 (progn
614 ;; use reverse order to make sure the indices are kept valid
615 (replace-match "+++" t t nil 3)
616 (replace-match "---" t t nil 2))
617 ;; we matched a hunk header
618 (let ((line1s (match-string 4))
619 (line1e (match-string 5))
620 (pt1 (match-beginning 0)))
621 (replace-match "")
622 (unless (re-search-forward
623 "^--- \\([0-9]+\\),\\(-?[0-9]+\\) ----$" nil t)
624 (error "Can't find matching `--- n1,n2 ----' line"))
625 (let ((line2s (match-string 1))
626 (line2e (match-string 2))
627 (pt2 (progn
628 (delete-region (progn (beginning-of-line) (point))
629 (progn (forward-line 1) (point)))
630 (point-marker))))
631 (goto-char pt1)
632 (forward-line 1)
633 (while (< (point) pt2)
634 (case (char-after)
635 ((?! ?-) (delete-char 2) (insert "-") (forward-line 1))
636 (?\ ;merge with the other half of the chunk
637 (let* ((endline2
638 (save-excursion
639 (goto-char pt2) (forward-line 1) (point)))
640 (c (char-after pt2)))
641 (case c
642 ((?! ?+)
643 (insert "+"
644 (prog1 (buffer-substring (+ pt2 2) endline2)
645 (delete-region pt2 endline2))))
646 (?\ ;FIXME: check consistency
647 (delete-region pt2 endline2)
648 (delete-char 1)
649 (forward-line 1))
650 (?\\ (forward-line 1))
651 (t (delete-char 1) (forward-line 1)))))
652 (t (forward-line 1))))
653 (while (looking-at "[+! ] ")
654 (if (/= (char-after) ?!) (forward-char 1)
655 (delete-char 1) (insert "+"))
656 (delete-char 1) (forward-line 1))
657 (save-excursion
658 (goto-char pt1)
659 (insert "@@ -" line1s ","
660 (number-to-string (- (string-to-number line1e)
661 (string-to-number line1s)
662 -1))
663 " +" line2s ","
664 (number-to-string (- (string-to-number line2e)
665 (string-to-number line2s)
666 -1)) " @@"))))))))))
668 (defun diff-reverse-direction (start end)
669 "Reverse the direction of the diffs.
670 START and END are either taken from the region (if a prefix arg is given) or
671 else cover the whole bufer."
672 (interactive (if current-prefix-arg
673 (list (mark) (point))
674 (list (point-min) (point-max))))
675 (unless (markerp end) (setq end (copy-marker end)))
676 (let (;;(diff-inhibit-after-change t)
677 (inhibit-read-only t))
678 (save-excursion
679 (goto-char start)
680 (while (and (re-search-forward "^\\(\\([-*][-*][-*] \\)\\(.+\\)\n\\([-+][-+][-+] \\)\\(.+\\)\\|\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\n\\*\\*\\* \\(.+\\) \\*\\*\\*\\*\\|@@ -\\(.+\\) \\+\\(.+\\) @@\\)$" nil t)
681 (< (point) end))
682 (combine-after-change-calls
683 (cond
684 ;; a file header
685 ((match-beginning 2) (replace-match "\\2\\5\n\\4\\3" nil))
686 ;; a context-diff hunk header
687 ((match-beginning 6)
688 (let ((pt-lines1 (match-beginning 6))
689 (lines1 (match-string 6)))
690 (replace-match "" nil nil nil 6)
691 (forward-line 1)
692 (let ((half1s (point)))
693 (while (looking-at "[-! \\][ \t]")
694 (when (= (char-after) ?-) (delete-char 1) (insert "+"))
695 (forward-line 1))
696 (let ((half1 (buffer-substring half1s (point))))
697 (delete-region half1s (point))
698 (unless (looking-at "^--- \\([0-9]+,-?[0-9]+\\) ----$")
699 (insert half1)
700 (error "Can't find matching `--- n1,n2 ----' line"))
701 (let ((str1 (match-string 1)))
702 (replace-match lines1 nil nil nil 1)
703 (forward-line 1)
704 (let ((half2s (point)))
705 (while (looking-at "[!+ \\][ \t]")
706 (when (= (char-after) ?+) (delete-char 1) (insert "-"))
707 (forward-line 1))
708 (let ((half2 (buffer-substring half2s (point))))
709 (delete-region half2s (point))
710 (insert half1)
711 (goto-char half1s)
712 (insert half2)))
713 (goto-char pt-lines1)
714 (insert str1))))))
715 ;; a unified-diff hunk header
716 ((match-beginning 7)
717 (replace-match "@@ -\\8 +\\7 @@" nil)
718 (forward-line 1)
719 (let ((c (char-after)) first last)
720 (while (case (setq c (char-after))
721 (?- (setq first (or first (point)))
722 (delete-char 1) (insert "+") t)
723 (?+ (setq last (or last (point)))
724 (delete-char 1) (insert "-") t)
725 (?\\ t)
726 (t (when (and first last (< first last))
727 (let ((str (buffer-substring first last)))
728 (save-excursion (delete-region first last))
729 (insert str)))
730 (setq first nil last nil)
731 (equal ?\ c)))
732 (forward-line 1))))))))))
734 (defun diff-fixup-modifs (start end)
735 "Fixup the hunk headers (in case the buffer was modified).
736 START and END are either taken from the region (if a prefix arg is given) or
737 else cover the whole bufer."
738 (interactive (if current-prefix-arg
739 (list (mark) (point))
740 (list (point-min) (point-max))))
741 (let ((inhibit-read-only t))
742 (save-excursion
743 (goto-char end) (diff-end-of-hunk)
744 (let ((plus 0) (minus 0) (space 0) (bang 0))
745 (while (and (= (forward-line -1) 0) (<= start (point)))
746 (if (not (looking-at "\\(@@ .+ @@\\|[-*][-*][-*] .+ [-*][-*][-*][-*]\\)$"))
747 (case (char-after)
748 (?\ (incf space))
749 (?+ (incf plus))
750 (?- (incf minus))
751 (?! (incf bang))
752 (?\\ nil)
753 (t (setq space 0 plus 0 minus 0 bang 0)))
754 (cond
755 ((looking-at "@@ -[0-9]+,\\([0-9]*\\) \\+[0-9]+,\\([0-9]*\\) @@$")
756 (let* ((old1 (match-string 1))
757 (old2 (match-string 2))
758 (new1 (number-to-string (+ space minus)))
759 (new2 (number-to-string (+ space plus))))
760 (unless (string= new2 old2) (replace-match new2 t t nil 2))
761 (unless (string= new1 old1) (replace-match new1 t t nil 1))))
762 ((looking-at "--- \\([0-9]+\\),\\([0-9]*\\) ----$")
763 (when (> (+ space bang plus) 0)
764 (let* ((old1 (match-string 1))
765 (old2 (match-string 2))
766 (new (number-to-string
767 (+ space bang plus -1 (string-to-number old1)))))
768 (unless (string= new old2) (replace-match new t t nil 2)))))
769 ((looking-at "\\*\\*\\* \\([0-9]+\\),\\(-?[0-9]*\\) \\*\\*\\*\\*$")
770 (when (> (+ space bang minus) 0)
771 (let* ((old (match-string 1))
772 (new (format
773 (concat "%0" (number-to-string (length old)) "d")
774 (+ space bang minus -1 (string-to-number old)))))
775 (unless (string= new old) (replace-match new t t nil 2))))))
776 (setq space 0 plus 0 minus 0 bang 0)))))))
778 ;;;;
779 ;;;; Hooks
780 ;;;;
782 (defun diff-write-contents-hooks ()
783 "Fixup hunk headers if necessary."
784 (if (buffer-modified-p) (diff-fixup-modifs (point-min) (point-max)))
785 nil)
787 ;; XEmacs doesn't seem to have this feature
788 (defvar undo-in-progress nil)
790 ;; It turns out that making changes in the buffer from within an
791 ;; *-change-function is asking for trouble, whereas making them
792 ;; from a post-command-hook doesn't pose much problems
793 (defvar diff-unhandled-changes nil)
794 (defun diff-after-change-function (beg end len)
795 "Remember to fixup the hunk header.
796 See `after-change-functions' for the meaning of BEG, END and LEN."
797 (when (and (not undo-in-progress) (not inhibit-read-only))
798 (if diff-unhandled-changes
799 (setq diff-unhandled-changes
800 (cons (min beg (car diff-unhandled-changes))
801 (max beg (cdr diff-unhandled-changes))))
802 (setq diff-unhandled-changes (cons beg end)))))
804 (defun diff-post-command-hook ()
805 "Fixup hunk headers if necessary."
806 (when (consp diff-unhandled-changes)
807 (ignore-errors
808 (save-excursion
809 (goto-char (car diff-unhandled-changes)) (diff-beginning-of-hunk)
810 (diff-fixup-modifs (point) (cdr diff-unhandled-changes))))
811 (setq diff-unhandled-changes nil)))
813 ;;;;
814 ;;;; The main function
815 ;;;;
817 ;;;###autoload
818 (defun diff-mode ()
819 "Major mode for viewing/editing context diffs.
820 Supports unified and context diffs as well as (to a lesser extent) normal diffs.
821 When the buffer is read-only, the ESC prefix is not necessary.
822 This mode runs `diff-mode-hook'.
823 \\{diff-mode-map}"
824 (interactive)
825 (kill-all-local-variables)
826 (setq major-mode 'diff-mode)
827 (setq mode-name "Diff")
828 (use-local-map diff-mode-map)
829 (set (make-local-variable 'font-lock-defaults) diff-font-lock-defaults)
830 (set (make-local-variable 'outline-regexp) diff-outline-regexp)
831 ;; compile support
832 (set (make-local-variable 'compilation-file-regexp-alist)
833 diff-file-regexp-alist)
834 (set (make-local-variable 'compilation-error-regexp-alist)
835 diff-error-regexp-alist)
836 (when (string-match "\\.rej\\'" (or buffer-file-name ""))
837 (set (make-local-variable 'compilation-current-file)
838 (substring buffer-file-name 0 (match-beginning 0))))
839 (compilation-shell-minor-mode 1)
841 (setq buffer-read-only t)
842 (if (not diff-update-on-the-fly-flag)
843 (add-hook 'write-contents-hooks 'diff-write-contents-hooks)
844 (make-local-variable 'diff-unhandled-changes)
845 (add-hook (make-local-hook 'after-change-functions)
846 'diff-after-change-function nil t)
847 (add-hook (make-local-hook 'post-command-hook)
848 'diff-post-command-hook nil t))
849 ;; Neat trick from Dave Love to add more bindings in read-only mode:
850 (add-to-list (make-local-variable 'minor-mode-map-alist)
851 (cons 'buffer-read-only diff-mode-shared-map))
853 (run-hooks 'diff-mode-hook))
855 ;;;###autoload
856 (define-minor-mode diff-minor-mode
857 "Minor mode for viewing/editing context diffs.
858 \\{diff-minor-mode-map}"
859 nil " Diff" nil
860 ;; FIXME: setup font-lock
861 ;; FIXME: setup change hooks
865 ;; provide the package
866 (provide 'diff-mode)
868 ;;; Change Log:
869 ;; $Log: diff-mode.el,v $
870 ;; Revision 1.11 1999/10/09 23:38:29 monnier
871 ;; (diff-mode-load-hook): dropped.
872 ;; (auto-mode-alist): also catch *.diffs.
873 ;; (diff-find-file-name, diff-mode): add smarts to find the right file
874 ;; for *.rej files (that lack any file name indication).
876 ;; Revision 1.10 1999/09/30 15:32:11 monnier
877 ;; added support for "\ No newline at end of file".
879 ;; Revision 1.9 1999/09/15 00:01:13 monnier
880 ;; - added basic `compile' support.
881 ;; - have diff-kill-hunk call diff-kill-file if it's the only hunk.
882 ;; - diff-kill-file now tries to kill the leading garbage as well.
884 ;; Revision 1.8 1999/09/13 21:10:09 monnier
885 ;; - don't use CL in the autoloaded code
886 ;; - accept diffs using -T
888 ;; Revision 1.7 1999/09/05 20:53:03 monnier
889 ;; interface to ediff-patch
891 ;; Revision 1.6 1999/09/01 20:55:13 monnier
892 ;; (ediff=patch-file): add bindings to call ediff-patch.
893 ;; (diff-find-file-name): taken out of diff-goto-source.
894 ;; (diff-unified->context, diff-context->unified, diff-reverse-direction,
895 ;; diff-fixup-modifs): only use the region if a prefix arg is given.
897 ;; Revision 1.5 1999/08/31 19:18:52 monnier
898 ;; (diff-beginning-of-file, diff-prev-file): fixed wrong parenthesis.
900 ;; Revision 1.4 1999/08/31 13:01:44 monnier
901 ;; use `combine-after-change-calls' to minimize the slowdown of font-lock.
904 ;;; diff-mode.el ends here