(sh-get-kw): Remove '()' from the list of
[bpt/emacs.git] / lisp / diff-mode.el
CommitLineData
b532d575 1;;; diff-mode.el --- a mode for viewing/editing context diffs
610a6418 2
0d30b337 3;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004,
1c58b439 4;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
610a6418 5
cc1eecfd 6;; Author: Stefan Monnier <monnier@iro.umontreal.ca>
b532d575 7;; Keywords: convenience patch diff
610a6418
SM
8
9;; This file is part of GNU Emacs.
10
eb3fa2cf 11;; GNU Emacs is free software: you can redistribute it and/or modify
610a6418 12;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
13;; the Free Software Foundation, either version 3 of the License, or
14;; (at your option) any later version.
610a6418
SM
15
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.
20
21;; You should have received a copy of the GNU General Public License
eb3fa2cf 22;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
610a6418
SM
23
24;;; Commentary:
25
cdbb79c1 26;; Provides support for font-lock, outline, navigation
610a6418
SM
27;; commands, editing and various conversions as well as jumping
28;; to the corresponding source file.
29
3ccdb14e 30;; Inspired by Pavel Machek's patch-mode.el (<pavel@@atrey.karlin.mff.cuni.cz>)
b532d575 31;; Some efforts were spent to have it somewhat compatible with XEmacs'
610a6418
SM
32;; diff-mode as well as with compilation-minor-mode
33
610a6418
SM
34;; Bugs:
35
027ac3f8 36;; - Reverse doesn't work with normal diffs.
610a6418
SM
37
38;; Todo:
39
8330c175
SM
40;; - Improve `diff-add-change-log-entries-other-window',
41;; it is very simplistic now.
8a72c7f8 42;;
22cd1973
SM
43;; - Add a `delete-after-apply' so C-c C-a automatically deletes hunks.
44;; Also allow C-c C-a to delete already-applied hunks.
45;;
46;; - Try `diff <file> <hunk>' to try and fuzzily discover the source location
47;; of a hunk. Show then the changes between <file> and <hunk> and make it
48;; possible to apply them to <file>, <hunk-src>, or <hunk-dst>.
49;; Or maybe just make it into a ".rej to diff3-markers converter".
e78cf8e5 50;; Maybe just use `wiggle' (by Neil Brown) to do it for us.
22cd1973 51;;
e78cf8e5
SM
52;; - in diff-apply-hunk, strip context in replace-match to better
53;; preserve markers and spacing.
027ac3f8 54;; - Handle `diff -b' output in context->unified.
610a6418
SM
55
56;;; Code:
610a6418
SM
57(eval-when-compile (require 'cl))
58
6e0f362c
JB
59(defvar add-log-buffer-file-name-function)
60
610a6418
SM
61
62(defgroup diff-mode ()
aa753196 63 "Major mode for viewing/editing diffs."
ccce6558 64 :version "21.1"
610a6418
SM
65 :group 'tools
66 :group 'diff)
67
22cd1973 68(defcustom diff-default-read-only nil
769dd0f1
SM
69 "If non-nil, `diff-mode' buffers default to being read-only."
70 :type 'boolean
71 :group 'diff-mode)
72
e8a1ed31 73(defcustom diff-jump-to-old-file nil
44a07c5a 74 "Non-nil means `diff-goto-source' jumps to the old file.
610a6418 75Else, it jumps to the new file."
f5307782
JB
76 :type 'boolean
77 :group 'diff-mode)
610a6418 78
e8a1ed31 79(defcustom diff-update-on-the-fly t
610a6418
SM
80 "*Non-nil means hunk headers are kept up-to-date on-the-fly.
81When editing a diff file, the line numbers in the hunk headers
82need to be kept consistent with the actual diff. This can
83either be done on the fly (but this sometimes interacts poorly with the
84undo mechanism) or whenever the file is written (can be slow
85when editing big diffs)."
f5307782
JB
86 :type 'boolean
87 :group 'diff-mode)
610a6418 88
00df919e
MB
89(defcustom diff-advance-after-apply-hunk t
90 "*Non-nil means `diff-apply-hunk' will move to the next hunk after applying."
f5307782
JB
91 :type 'boolean
92 :group 'diff-mode)
00df919e 93
22cd1973
SM
94(defcustom diff-mode-hook nil
95 "Run after setting up the `diff-mode' major mode."
96 :type 'hook
f5307782
JB
97 :options '(diff-delete-empty-files diff-make-unified)
98 :group 'diff-mode)
610a6418
SM
99
100(defvar diff-outline-regexp
101 "\\([*+][*+][*+] [^0-9]\\|@@ ...\\|\\*\\*\\* [0-9].\\|--- [0-9]..\\)")
102
cd632e57 103;;;;
610a6418 104;;;; keymap, menu, ...
cd632e57 105;;;;
610a6418 106
d87e5627 107(easy-mmode-defmap diff-mode-shared-map
027ac3f8 108 '(;; From Pavel Machek's patch-mode.
d87e5627
SM
109 ("n" . diff-hunk-next)
110 ("N" . diff-file-next)
111 ("p" . diff-hunk-prev)
112 ("P" . diff-file-prev)
25edda53
DN
113 ("\t" . diff-hunk-next)
114 ([backtab] . diff-hunk-prev)
d87e5627
SM
115 ("k" . diff-hunk-kill)
116 ("K" . diff-file-kill)
027ac3f8 117 ;; From compilation-minor-mode.
d87e5627
SM
118 ("}" . diff-file-next)
119 ("{" . diff-file-prev)
610a6418 120 ("\C-m" . diff-goto-source)
a1e2d7f7 121 ([mouse-2] . diff-goto-source)
027ac3f8 122 ;; From XEmacs' diff-mode.
fe39af99
SM
123 ;; Standard M-w is useful, so don't change M-W.
124 ;;("W" . widen)
610a6418
SM
125 ;;("." . diff-goto-source) ;display-buffer
126 ;;("f" . diff-goto-source) ;find-file
127 ("o" . diff-goto-source) ;other-window
128 ;;("w" . diff-goto-source) ;other-frame
129 ;;("N" . diff-narrow)
130 ;;("h" . diff-show-header)
131 ;;("j" . diff-show-difference) ;jump to Nth diff
132 ;;("q" . diff-quit)
e78cf8e5
SM
133 ;; Not useful if you have to metafy them.
134 ;;(" " . scroll-up)
135 ;;("\177" . scroll-down)
136 ;; Standard M-a is useful, so don't change M-A.
137 ;;("A" . diff-ediff-patch)
138 ;; Standard M-r is useful, so don't change M-r or M-R.
139 ;;("r" . diff-restrict-view)
140 ;;("R" . diff-reverse-direction)
c010ecfa 141 ("q" . quit-window))
0b82e382 142 "Basic keymap for `diff-mode', bound to various prefix keys.")
610a6418 143
d87e5627 144(easy-mmode-defmap diff-mode-map
610a6418 145 `(("\e" . ,diff-mode-shared-map)
027ac3f8
SM
146 ;; From compilation-minor-mode.
147 ("\C-c\C-c" . diff-goto-source)
8330c175
SM
148 ;; By analogy with the global C-x 4 a binding.
149 ("\C-x4A" . diff-add-change-log-entries-other-window)
027ac3f8 150 ;; Misc operations.
3cec9c57 151 ("\C-c\C-a" . diff-apply-hunk)
28408bfd
RS
152 ("\C-c\C-e" . diff-ediff-patch)
153 ("\C-c\C-n" . diff-restrict-view)
28408bfd 154 ("\C-c\C-s" . diff-split-hunk)
b711788a 155 ("\C-c\C-t" . diff-test-hunk)
2659df68 156 ("\C-c\C-r" . diff-reverse-direction)
28408bfd 157 ("\C-c\C-u" . diff-context->unified)
fe39af99
SM
158 ;; `d' because it duplicates the context :-( --Stef
159 ("\C-c\C-d" . diff-unified->context)
2659df68
SM
160 ("\C-c\C-w" . diff-ignore-whitespace-hunk)
161 ("\C-c\C-b" . diff-refine-hunk) ;No reason for `b' :-(
677c0382 162 ("\C-c\C-f" . next-error-follow-minor-mode))
610a6418
SM
163 "Keymap for `diff-mode'. See also `diff-mode-shared-map'.")
164
165(easy-menu-define diff-mode-menu diff-mode-map
166 "Menu for `diff-mode'."
167 '("Diff"
fb4dfdd2
DN
168 ["Jump to Source" diff-goto-source
169 :help "Jump to the corresponding source line"]
170 ["Apply hunk" diff-apply-hunk
171 :help "Apply the current hunk to the source file and go to the next"]
172 ["Test applying hunk" diff-test-hunk
173 :help "See whether it's possible to apply the current hunk"]
174 ["Apply diff with Ediff" diff-ediff-patch
175 :help "Call `ediff-patch-file' on the current buffer"]
8330c175 176 ["Create Change Log entries" diff-add-change-log-entries-other-window
8a72c7f8 177 :help "Create ChangeLog entries for the changes in the diff buffer"]
8f2d38de 178 "-----"
fb4dfdd2
DN
179 ["Reverse direction" diff-reverse-direction
180 :help "Reverse the direction of the diffs"]
181 ["Context -> Unified" diff-context->unified
182 :help "Convert context diffs to unified diffs"]
183 ["Unified -> Context" diff-unified->context
184 :help "Convert unified diffs to context diffs"]
610a6418 185 ;;["Fixup Headers" diff-fixup-modifs (not buffer-read-only)]
8f2d38de 186 "-----"
fb4dfdd2
DN
187 ["Split hunk" diff-split-hunk
188 :active (diff-splittable-p)
189 :help "Split the current (unified diff) hunk at point into two hunks"]
190 ["Ignore whitespace changes" diff-ignore-whitespace-hunk
191 :help "Re-diff the current hunk, ignoring whitespace differences"]
192 ["Highlight fine changes" diff-refine-hunk
193 :help "Highlight changes of hunk at point at a finer granularity"]
194 ["Kill current hunk" diff-hunk-kill
195 :help "Kill current hunk"]
196 ["Kill current file's hunks" diff-file-kill
197 :help "Kill all current file's hunks"]
8f2d38de 198 "-----"
fb4dfdd2
DN
199 ["Previous Hunk" diff-hunk-prev
200 :help "Go to the previous count'th hunk"]
201 ["Next Hunk" diff-hunk-next
202 :help "Go to the next count'th hunk"]
203 ["Previous File" diff-file-prev
204 :help "Go to the previous count'th file"]
205 ["Next File" diff-file-next
206 :help "Go to the next count'th file"]
610a6418
SM
207 ))
208
3cec9c57 209(defcustom diff-minor-mode-prefix "\C-c="
0b82e382 210 "Prefix key for `diff-minor-mode' commands."
f5307782
JB
211 :type '(choice (string "\e") (string "C-c=") string)
212 :group 'diff-mode)
0b82e382 213
d87e5627
SM
214(easy-mmode-defmap diff-minor-mode-map
215 `((,diff-minor-mode-prefix . ,diff-mode-shared-map))
0b82e382
SM
216 "Keymap for `diff-minor-mode'. See also `diff-mode-shared-map'.")
217
7381be9d
TTN
218(define-minor-mode diff-auto-refine-mode
219 "Automatically highlight changes in detail as the user visits hunks.
220When transitioning from disabled to enabled,
221try to refine the current hunk, as well."
4542adfb 222 :group 'diff-mode :init-value t :lighter nil ;; " Auto-Refine"
7381be9d
TTN
223 (when diff-auto-refine-mode
224 (condition-case-no-debug nil (diff-refine-hunk) (error nil))))
610a6418 225
cd632e57 226;;;;
610a6418 227;;;; font-lock support
cd632e57 228;;;;
610a6418 229
221711eb 230(defface diff-header
55f2eb7e 231 '((((class color) (min-colors 88) (background light))
9f4e4f5b 232 :background "grey80")
55f2eb7e 233 (((class color) (min-colors 88) (background dark))
4f815065 234 :background "grey45")
55f2eb7e 235 (((class color) (background light))
4f815065 236 :foreground "blue1" :weight bold)
55f2eb7e 237 (((class color) (background dark))
4f815065
SM
238 :foreground "green" :weight bold)
239 (t :weight bold))
d676819e
LK
240 "`diff-mode' face inherited by hunk and index header faces."
241 :group 'diff-mode)
221711eb
MB
242;; backward-compatibility alias
243(put 'diff-header-face 'face-alias 'diff-header)
244(defvar diff-header-face 'diff-header)
c0aa75f7 245
221711eb 246(defface diff-file-header
55f2eb7e 247 '((((class color) (min-colors 88) (background light))
4f815065 248 :background "grey70" :weight bold)
55f2eb7e 249 (((class color) (min-colors 88) (background dark))
4f815065 250 :background "grey60" :weight bold)
55f2eb7e 251 (((class color) (background light))
f3abba99 252 :foreground "green" :weight bold)
55f2eb7e 253 (((class color) (background dark))
4f815065
SM
254 :foreground "cyan" :weight bold)
255 (t :weight bold)) ; :height 1.3
d676819e
LK
256 "`diff-mode' face used to highlight file header lines."
257 :group 'diff-mode)
221711eb
MB
258;; backward-compatibility alias
259(put 'diff-file-header-face 'face-alias 'diff-file-header)
260(defvar diff-file-header-face 'diff-file-header)
610a6418 261
221711eb
MB
262(defface diff-index
263 '((t :inherit diff-file-header))
d676819e
LK
264 "`diff-mode' face used to highlight index header lines."
265 :group 'diff-mode)
221711eb
MB
266;; backward-compatibility alias
267(put 'diff-index-face 'face-alias 'diff-index)
268(defvar diff-index-face 'diff-index)
610a6418 269
221711eb
MB
270(defface diff-hunk-header
271 '((t :inherit diff-header))
d676819e
LK
272 "`diff-mode' face used to highlight hunk header lines."
273 :group 'diff-mode)
221711eb
MB
274;; backward-compatibility alias
275(put 'diff-hunk-header-face 'face-alias 'diff-hunk-header)
276(defvar diff-hunk-header-face 'diff-hunk-header)
610a6418 277
221711eb
MB
278(defface diff-removed
279 '((t :inherit diff-changed))
d676819e
LK
280 "`diff-mode' face used to highlight removed lines."
281 :group 'diff-mode)
221711eb
MB
282;; backward-compatibility alias
283(put 'diff-removed-face 'face-alias 'diff-removed)
284(defvar diff-removed-face 'diff-removed)
610a6418 285
221711eb
MB
286(defface diff-added
287 '((t :inherit diff-changed))
d676819e
LK
288 "`diff-mode' face used to highlight added lines."
289 :group 'diff-mode)
221711eb
MB
290;; backward-compatibility alias
291(put 'diff-added-face 'face-alias 'diff-added)
292(defvar diff-added-face 'diff-added)
610a6418 293
221711eb 294(defface diff-changed
86a27a3a 295 '((((type tty pc) (class color) (background light))
4f815065 296 :foreground "magenta" :weight bold :slant italic)
86a27a3a 297 (((type tty pc) (class color) (background dark))
4f815065 298 :foreground "yellow" :weight bold :slant italic))
d676819e
LK
299 "`diff-mode' face used to highlight changed lines."
300 :group 'diff-mode)
221711eb
MB
301;; backward-compatibility alias
302(put 'diff-changed-face 'face-alias 'diff-changed)
303(defvar diff-changed-face 'diff-changed)
610a6418 304
f3abba99
JL
305(defface diff-indicator-removed
306 '((t :inherit diff-removed))
307 "`diff-mode' face used to highlight indicator of removed lines (-, <)."
308 :group 'diff-mode
309 :version "22.1")
310(defvar diff-indicator-removed-face 'diff-indicator-removed)
311
312(defface diff-indicator-added
313 '((t :inherit diff-added))
314 "`diff-mode' face used to highlight indicator of added lines (+, >)."
315 :group 'diff-mode
316 :version "22.1")
317(defvar diff-indicator-added-face 'diff-indicator-added)
318
319(defface diff-indicator-changed
320 '((t :inherit diff-changed))
321 "`diff-mode' face used to highlight indicator of changed lines."
322 :group 'diff-mode
323 :version "22.1")
324(defvar diff-indicator-changed-face 'diff-indicator-changed)
325
221711eb 326(defface diff-function
ea5d66db 327 '((t :inherit diff-header))
d676819e
LK
328 "`diff-mode' face used to highlight function names produced by \"diff -p\"."
329 :group 'diff-mode)
221711eb
MB
330;; backward-compatibility alias
331(put 'diff-function-face 'face-alias 'diff-function)
332(defvar diff-function-face 'diff-function)
34460354 333
221711eb 334(defface diff-context
f3abba99 335 '((((class color grayscale) (min-colors 88)) :inherit shadow))
d676819e
LK
336 "`diff-mode' face used to highlight context and other side-information."
337 :group 'diff-mode)
221711eb
MB
338;; backward-compatibility alias
339(put 'diff-context-face 'face-alias 'diff-context)
340(defvar diff-context-face 'diff-context)
c0aa75f7 341
221711eb
MB
342(defface diff-nonexistent
343 '((t :inherit diff-file-header))
d676819e
LK
344 "`diff-mode' face used to highlight nonexistent files in recursive diffs."
345 :group 'diff-mode)
221711eb
MB
346;; backward-compatibility alias
347(put 'diff-nonexistent-face 'face-alias 'diff-nonexistent)
348(defvar diff-nonexistent-face 'diff-nonexistent)
469fc0a2 349
22cd1973
SM
350(defconst diff-yank-handler '(diff-yank-function))
351(defun diff-yank-function (text)
1ec7bd14
SM
352 ;; FIXME: the yank-handler is now called separately on each piece of text
353 ;; with a yank-handler property, so the next-single-property-change call
354 ;; below will always return nil :-( --stef
22cd1973
SM
355 (let ((mixed (next-single-property-change 0 'yank-handler text))
356 (start (point)))
357 ;; First insert the text.
358 (insert text)
359 ;; If the text does not include any diff markers and if we're not
360 ;; yanking back into a diff-mode buffer, get rid of the prefixes.
361 (unless (or mixed (derived-mode-p 'diff-mode))
362 (undo-boundary) ; Just in case the user wanted the prefixes.
363 (let ((re (save-excursion
364 (if (re-search-backward "^[><!][ \t]" start t)
365 (if (eq (char-after) ?!)
366 "^[!+- ][ \t]" "^[<>][ \t]")
367 "^[ <>!+-]"))))
368 (save-excursion
369 (while (re-search-backward re start t)
370 (replace-match "" t t)))))))
cfc80227 371
f52d2f9c 372(defconst diff-hunk-header-re-unified
43392d12 373 "^@@ -\\([0-9]+\\)\\(?:,\\([0-9]+\\)\\)? \\+\\([0-9]+\\)\\(?:,\\([0-9]+\\)\\)? @@")
bf6970a5
SM
374(defconst diff-context-mid-hunk-header-re
375 "--- \\([0-9]+\\)\\(?:,\\([0-9]+\\)\\)? ----$")
22cd1973 376
610a6418 377(defvar diff-font-lock-keywords
f52d2f9c
SM
378 `((,(concat "\\(" diff-hunk-header-re-unified "\\)\\(.*\\)$")
379 (1 diff-hunk-header-face) (6 diff-function-face))
f3abba99
JL
380 ("^\\(\\*\\{15\\}\\)\\(.*\\)$" ;context
381 (1 diff-hunk-header-face) (2 diff-function-face))
370d860c 382 ("^\\*\\*\\* .+ \\*\\*\\*\\*". diff-hunk-header-face) ;context
bf6970a5 383 (,diff-context-mid-hunk-header-re . diff-hunk-header-face) ;context
f3abba99
JL
384 ("^[0-9,]+[acd][0-9,]+$" . diff-hunk-header-face) ;normal
385 ("^---$" . diff-hunk-header-face) ;normal
4c969f97
SM
386 ;; For file headers, accept files with spaces, but be careful to rule
387 ;; out false-positives when matching hunk headers.
388 ("^\\(---\\|\\+\\+\\+\\|\\*\\*\\*\\) \\([^\t\n]+?\\)\\(?:\t.*\\| \\(\\*\\*\\*\\*\\|----\\)\\)?\n"
389 (0 diff-header-face)
390 (2 (if (not (match-end 3)) diff-file-header-face) prepend))
f3abba99
JL
391 ("^\\([-<]\\)\\(.*\n\\)"
392 (1 diff-indicator-removed-face) (2 diff-removed-face))
393 ("^\\([+>]\\)\\(.*\n\\)"
394 (1 diff-indicator-added-face) (2 diff-added-face))
395 ("^\\(!\\)\\(.*\n\\)"
396 (1 diff-indicator-changed-face) (2 diff-changed-face))
397 ("^Index: \\(.+\\).*\n"
398 (0 diff-header-face) (1 diff-index-face prepend))
7dfb000f 399 ("^Only in .*\n" . diff-nonexistent-face)
f3abba99 400 ("^\\(#\\)\\(.*\\)"
050dcc13
JL
401 (1 font-lock-comment-delimiter-face)
402 (2 font-lock-comment-face))
f3abba99 403 ("^[^-=+*!<>#].*\n" (0 diff-context-face))))
610a6418
SM
404
405(defconst diff-font-lock-defaults
0e104800 406 '(diff-font-lock-keywords t nil nil nil (font-lock-multiline . nil)))
610a6418 407
ccce6558
DL
408(defvar diff-imenu-generic-expression
409 ;; Prefer second name as first is most likely to be a backup or
824693e7
DL
410 ;; version-control name. The [\t\n] at the end of the unidiff pattern
411 ;; catches Debian source diff files (which lack the trailing date).
412 '((nil "\\+\\+\\+\\ \\([^\t\n]+\\)[\t\n]" 1) ; unidiffs
ccce6558
DL
413 (nil "^--- \\([^\t\n]+\\)\t.*\n\\*" 1))) ; context diffs
414
cd632e57 415;;;;
610a6418 416;;;; Movement
cd632e57 417;;;;
610a6418 418
f52d2f9c
SM
419(defvar diff-valid-unified-empty-line t
420 "If non-nil, empty lines are valid in unified diffs.
421Some versions of diff replace all-blank context lines in unified format with
422empty lines. This makes the format less robust, but is tolerated.
423See http://lists.gnu.org/archive/html/emacs-devel/2007-11/msg01990.html")
424
425(defconst diff-hunk-header-re
426 (concat "^\\(?:" diff-hunk-header-re-unified ".*\\|\\*\\{15\\}.*\n\\*\\*\\* .+ \\*\\*\\*\\*\\|[0-9]+\\(,[0-9]+\\)?[acd][0-9]+\\(,[0-9]+\\)?\\)$"))
365bdf63 427(defconst diff-file-header-re (concat "^\\(--- .+\n\\+\\+\\+ \\|\\*\\*\\* .+\n--- \\|[^-+!<>0-9@* \n]\\).+\n" (substring diff-hunk-header-re 1)))
610a6418
SM
428(defvar diff-narrowed-to nil)
429
be36f934 430(defun diff-hunk-style (&optional style)
996884b2 431 (when (looking-at diff-hunk-header-re)
be36f934 432 (setq style (cdr (assq (char-after) '((?@ . unified) (?* . context)))))
996884b2 433 (goto-char (match-end 0)))
be36f934
SM
434 style)
435
f52d2f9c
SM
436(defun diff-end-of-hunk (&optional style donttrustheader)
437 (let (end)
438 (when (looking-at diff-hunk-header-re)
439 ;; Especially important for unified (because headers are ambiguous).
440 (setq style (diff-hunk-style style))
441 (goto-char (match-end 0))
442 (when (and (not donttrustheader) (match-end 2))
43392d12
SM
443 (let* ((nold (string-to-number (or (match-string 2) "1")))
444 (nnew (string-to-number (or (match-string 4) "1")))
aec1ef07 445 (endold
f52d2f9c
SM
446 (save-excursion
447 (re-search-forward (if diff-valid-unified-empty-line
448 "^[- \n]" "^[- ]")
aec1ef07
SM
449 nil t nold)
450 (line-beginning-position 2)))
451 (endnew
452 ;; The hunk may end with a bunch of "+" lines, so the `end' is
453 ;; then further than computed above.
454 (save-excursion
455 (re-search-forward (if diff-valid-unified-empty-line
456 "^[+ \n]" "^[+ ]")
457 nil t nnew)
458 (line-beginning-position 2))))
459 (setq end (max endold endnew)))))
f52d2f9c
SM
460 ;; We may have a first evaluation of `end' thanks to the hunk header.
461 (unless end
462 (setq end (and (re-search-forward
463 (case style
464 (unified (concat (if diff-valid-unified-empty-line
465 "^[^-+# \\\n]\\|" "^[^-+# \\]\\|")
466 ;; A `unified' header is ambiguous.
467 diff-file-header-re))
468 (context "^[^-+#! \\]")
469 (normal "^[^<>#\\]")
470 (t "^[^-+#!<> \\]"))
471 nil t)
472 (match-beginning 0)))
473 (when diff-valid-unified-empty-line
474 ;; While empty lines may be valid inside hunks, they are also likely
475 ;; to be unrelated to the hunk.
476 (goto-char (or end (point-max)))
477 (while (eq ?\n (char-before (1- (point))))
478 (forward-char -1)
479 (setq end (point)))))
0e104800
SM
480 ;; The return value is used by easy-mmode-define-navigation.
481 (goto-char (or end (point-max)))))
610a6418 482
fd691799
SM
483(defun diff-beginning-of-hunk (&optional try-harder)
484 "Move back to beginning of hunk.
485If TRY-HARDER is non-nil, try to cater to the case where we're not in a hunk
486but in the file header instead, in which case move forward to the first hunk."
610a6418
SM
487 (beginning-of-line)
488 (unless (looking-at diff-hunk-header-re)
489 (forward-line 1)
490 (condition-case ()
491 (re-search-backward diff-hunk-header-re)
fd691799
SM
492 (error
493 (if (not try-harder)
494 (error "Can't find the beginning of the hunk")
495 (diff-beginning-of-file-and-junk)
496 (diff-hunk-next))))))
610a6418 497
f151b310
SM
498(defun diff-unified-hunk-p ()
499 (save-excursion
500 (ignore-errors
501 (diff-beginning-of-hunk)
502 (looking-at "^@@"))))
503
610a6418
SM
504(defun diff-beginning-of-file ()
505 (beginning-of-line)
506 (unless (looking-at diff-file-header-re)
4c969f97
SM
507 (let ((start (point))
508 res)
509 ;; diff-file-header-re may need to match up to 4 lines, so in case
510 ;; we're inside the header, we need to move up to 3 lines forward.
511 (forward-line 3)
512 (if (and (setq res (re-search-backward diff-file-header-re nil t))
513 ;; Maybe the 3 lines forward were too much and we matched
514 ;; a file header after our starting point :-(
515 (or (<= (point) start)
516 (setq res (re-search-backward diff-file-header-re nil t))))
517 res
518 (goto-char start)
519 (error "Can't find the beginning of the file")))))
520
610a6418
SM
521
522(defun diff-end-of-file ()
0e104800 523 (re-search-forward "^[-+#!<>0-9@* \\]" nil t)
22cd1973
SM
524 (re-search-forward (concat "^[^-+#!<>0-9@* \\]\\|" diff-file-header-re)
525 nil 'move)
526 (if (match-beginning 1)
527 (goto-char (match-beginning 1))
528 (beginning-of-line)))
610a6418 529
d87e5627
SM
530;; Define diff-{hunk,file}-{prev,next}
531(easy-mmode-define-navigation
2659df68 532 diff-hunk diff-hunk-header-re "hunk" diff-end-of-hunk diff-restrict-view
7381be9d 533 (if diff-auto-refine-mode
2659df68
SM
534 (condition-case-no-debug nil (diff-refine-hunk) (error nil))))
535
d87e5627
SM
536(easy-mmode-define-navigation
537 diff-file diff-file-header-re "file" diff-end-of-hunk)
610a6418
SM
538
539(defun diff-restrict-view (&optional arg)
540 "Restrict the view to the current hunk.
541If the prefix ARG is given, restrict the view to the current file instead."
542 (interactive "P")
543 (save-excursion
fd691799 544 (if arg (diff-beginning-of-file) (diff-beginning-of-hunk 'try-harder))
610a6418
SM
545 (narrow-to-region (point)
546 (progn (if arg (diff-end-of-file) (diff-end-of-hunk))
547 (point)))
548 (set (make-local-variable 'diff-narrowed-to) (if arg 'file 'hunk))))
549
550
d87e5627 551(defun diff-hunk-kill ()
610a6418
SM
552 "Kill current hunk."
553 (interactive)
554 (diff-beginning-of-hunk)
1eabc5e6 555 (let* ((start (point))
22cd1973
SM
556 (nexthunk (when (re-search-forward diff-hunk-header-re nil t)
557 (match-beginning 0)))
1eabc5e6
SM
558 (firsthunk (ignore-errors
559 (goto-char start)
560 (diff-beginning-of-file) (diff-hunk-next) (point)))
dd24cb37
CY
561 (nextfile (ignore-errors (diff-file-next) (point)))
562 (inhibit-read-only t))
1eabc5e6 563 (goto-char start)
610a6418
SM
564 (if (and firsthunk (= firsthunk start)
565 (or (null nexthunk)
566 (and nextfile (> nexthunk nextfile))))
1eabc5e6 567 ;; It's the only hunk for this file, so kill the file.
d87e5627 568 (diff-file-kill)
610a6418
SM
569 (diff-end-of-hunk)
570 (kill-region start (point)))))
571
8d73b84e
DN
572;; "index ", "old mode", "new mode", "new file mode" and
573;; "deleted file mode" are output by git-diff.
2f745f7f 574(defconst diff-file-junk-re
8d73b84e 575 "diff \\|index \\|\\(?:deleted file\\|new\\(?: file\\)?\\|old\\) mode")
f52d2f9c 576
fd691799
SM
577(defun diff-beginning-of-file-and-junk ()
578 "Go to the beginning of file-related diff-info.
579This is like `diff-beginning-of-file' except it tries to skip back over leading
580data such as \"Index: ...\" and such."
f52d2f9c
SM
581 (let* ((orig (point))
582 ;; Skip forward over what might be "leading junk" so as to get
583 ;; closer to the actual diff.
584 (_ (progn (beginning-of-line)
585 (while (looking-at diff-file-junk-re)
586 (forward-line 1))))
587 (start (point))
4c969f97
SM
588 (prevfile (condition-case err
589 (save-excursion (diff-beginning-of-file) (point))
590 (error err)))
591 (err (if (consp prevfile) prevfile))
592 (nextfile (ignore-errors
593 (save-excursion
594 (goto-char start) (diff-file-next) (point))))
595 ;; prevhunk is one of the limits.
596 (prevhunk (save-excursion
597 (ignore-errors
598 (if (numberp prevfile) (goto-char prevfile))
599 (diff-hunk-prev) (point))))
600 (previndex (save-excursion
f52d2f9c 601 (forward-line 1) ;In case we're looking at "Index:".
4c969f97
SM
602 (re-search-backward "^Index: " prevhunk t))))
603 ;; If we're in the junk, we should use nextfile instead of prevfile.
604 (if (and (numberp nextfile)
605 (or (not (numberp prevfile))
606 (and previndex (> previndex prevfile))))
607 (setq prevfile nextfile))
608 (if (and previndex (numberp prevfile) (< previndex prevfile))
609 (setq prevfile previndex))
610 (if (and (numberp prevfile) (<= prevfile start))
f52d2f9c
SM
611 (progn
612 (goto-char prevfile)
613 ;; Now skip backward over the leading junk we may have before the
614 ;; diff itself.
615 (while (save-excursion
616 (and (zerop (forward-line -1))
617 (looking-at diff-file-junk-re)))
618 (forward-line -1)))
4c969f97
SM
619 ;; File starts *after* the starting point: we really weren't in
620 ;; a file diff but elsewhere.
f52d2f9c 621 (goto-char orig)
4c969f97 622 (signal (car err) (cdr err)))))
fd691799 623
d87e5627 624(defun diff-file-kill ()
610a6418
SM
625 "Kill current file's hunks."
626 (interactive)
f52d2f9c
SM
627 (let ((orig (point))
628 (start (progn (diff-beginning-of-file-and-junk) (point)))
dd24cb37 629 (inhibit-read-only t))
610a6418 630 (diff-end-of-file)
22cd1973 631 (if (looking-at "^\n") (forward-char 1)) ;`tla' generates such diffs.
f52d2f9c 632 (if (> orig (point)) (error "Not inside a file diff"))
610a6418
SM
633 (kill-region start (point))))
634
fef8c55b
SM
635(defun diff-kill-junk ()
636 "Kill spurious empty diffs."
637 (interactive)
638 (save-excursion
639 (let ((inhibit-read-only t))
640 (goto-char (point-min))
641 (while (re-search-forward (concat "^\\(Index: .*\n\\)"
642 "\\([^-+!* <>].*\n\\)*?"
643 "\\(\\(Index:\\) \\|"
644 diff-file-header-re "\\)")
645 nil t)
646 (delete-region (if (match-end 4) (match-beginning 0) (match-end 1))
647 (match-beginning 3))
648 (beginning-of-line)))))
649
cd632e57
SM
650(defun diff-count-matches (re start end)
651 (save-excursion
652 (let ((n 0))
653 (goto-char start)
654 (while (re-search-forward re end t) (incf n))
655 n)))
656
f151b310
SM
657(defun diff-splittable-p ()
658 (save-excursion
659 (beginning-of-line)
660 (and (looking-at "^[-+ ]")
661 (progn (forward-line -1) (looking-at "^[-+ ]"))
662 (diff-unified-hunk-p))))
663
cd632e57
SM
664(defun diff-split-hunk ()
665 "Split the current (unified diff) hunk at point into two hunks."
666 (interactive)
667 (beginning-of-line)
668 (let ((pos (point))
669 (start (progn (diff-beginning-of-hunk) (point))))
f52d2f9c 670 (unless (looking-at diff-hunk-header-re-unified)
cd632e57
SM
671 (error "diff-split-hunk only works on unified context diffs"))
672 (forward-line 1)
673 (let* ((start1 (string-to-number (match-string 1)))
f52d2f9c 674 (start2 (string-to-number (match-string 3)))
cd632e57 675 (newstart1 (+ start1 (diff-count-matches "^[- \t]" (point) pos)))
dd24cb37
CY
676 (newstart2 (+ start2 (diff-count-matches "^[+ \t]" (point) pos)))
677 (inhibit-read-only t))
cd632e57
SM
678 (goto-char pos)
679 ;; Hopefully the after-change-function will not screw us over.
680 (insert "@@ -" (number-to-string newstart1) ",1 +"
681 (number-to-string newstart2) ",1 @@\n")
682 ;; Fix the original hunk-header.
683 (diff-fixup-modifs start pos))))
71296446 684
cd632e57 685
610a6418
SM
686;;;;
687;;;; jump to other buffers
688;;;;
689
0b82e382 690(defvar diff-remembered-files-alist nil)
adf4cc7e 691(defvar diff-remembered-defdir nil)
0b82e382 692
610a6418
SM
693(defun diff-filename-drop-dir (file)
694 (when (string-match "/" file) (substring file (match-end 0))))
695
0b82e382
SM
696(defun diff-merge-strings (ancestor from to)
697 "Merge the diff between ANCESTOR and FROM into TO.
698Returns the merged string if successful or nil otherwise.
d87e5627 699The strings are assumed not to contain any \"\\n\" (i.e. end of line).
0b82e382
SM
700If ANCESTOR = FROM, returns TO.
701If ANCESTOR = TO, returns FROM.
702The heuristic is simplistic and only really works for cases
703like \(diff-merge-strings \"b/foo\" \"b/bar\" \"/a/c/foo\")."
704 ;; Ideally, we want:
705 ;; AMB ANB CMD -> CND
706 ;; but that's ambiguous if `foo' or `bar' is empty:
707 ;; a/foo a/foo1 b/foo.c -> b/foo1.c but not 1b/foo.c or b/foo.c1
d87e5627 708 (let ((str (concat ancestor "\n" from "\n" to)))
0b82e382 709 (when (and (string-match (concat
d87e5627
SM
710 "\\`\\(.*?\\)\\(.*\\)\\(.*\\)\n"
711 "\\1\\(.*\\)\\3\n"
0b82e382
SM
712 "\\(.*\\(\\2\\).*\\)\\'") str)
713 (equal to (match-string 5 str)))
714 (concat (substring str (match-beginning 5) (match-beginning 6))
715 (match-string 4 str)
716 (substring str (match-end 6) (match-end 5))))))
717
22cd1973
SM
718(defun diff-tell-file-name (old name)
719 "Tell Emacs where the find the source file of the current hunk.
720If the OLD prefix arg is passed, tell the file NAME of the old file."
721 (interactive
722 (let* ((old current-prefix-arg)
723 (fs (diff-hunk-file-names current-prefix-arg)))
724 (unless fs (error "No file name to look for"))
725 (list old (read-file-name (format "File for %s: " (car fs))
726 nil (diff-find-file-name old) t))))
727 (let ((fs (diff-hunk-file-names old)))
728 (unless fs (error "No file name to look for"))
729 (push (cons fs name) diff-remembered-files-alist)))
cfc80227 730
22cd1973
SM
731(defun diff-hunk-file-names (&optional old)
732 "Give the list of file names textually mentioned for the current hunk."
610a6418
SM
733 (save-excursion
734 (unless (looking-at diff-file-header-re)
735 (or (ignore-errors (diff-beginning-of-file))
736 (re-search-forward diff-file-header-re nil t)))
22cd1973 737 (let ((limit (save-excursion
610a6418 738 (condition-case ()
d87e5627 739 (progn (diff-hunk-prev) (point))
610a6418 740 (error (point-min)))))
22cd1973
SM
741 (header-files
742 (if (looking-at "[-*][-*][-*] \\(\\S-+\\)\\(\\s-.*\\)?\n[-+][-+][-+] \\(\\S-+\\)")
743 (list (if old (match-string 1) (match-string 3))
744 (if old (match-string 3) (match-string 1)))
745 (forward-line 1) nil)))
746 (delq nil
747 (append
748 (when (and (not old)
749 (save-excursion
750 (re-search-backward "^Index: \\(.+\\)" limit t)))
751 (list (match-string 1)))
752 header-files
753 (when (re-search-backward
754 "^diff \\(-\\S-+ +\\)*\\(\\S-+\\)\\( +\\(\\S-+\\)\\)?"
755 nil t)
756 (list (if old (match-string 2) (match-string 4))
757 (if old (match-string 4) (match-string 2)))))))))
758
e4456fdf 759(defun diff-find-file-name (&optional old batch prefix)
22cd1973 760 "Return the file corresponding to the current patch.
e78cf8e5 761Non-nil OLD means that we want the old file.
e4456fdf
SM
762Non-nil BATCH means to prefer returning an incorrect answer than to prompt
763the user.
e78cf8e5 764PREFIX is only used internally: don't use it."
adf4cc7e
SM
765 (unless (equal diff-remembered-defdir default-directory)
766 ;; Flush diff-remembered-files-alist if the default-directory is changed.
767 (set (make-local-variable 'diff-remembered-defdir) default-directory)
768 (set (make-local-variable 'diff-remembered-files-alist) nil))
22cd1973
SM
769 (save-excursion
770 (unless (looking-at diff-file-header-re)
771 (or (ignore-errors (diff-beginning-of-file))
772 (re-search-forward diff-file-header-re nil t)))
773 (let ((fs (diff-hunk-file-names old)))
e78cf8e5 774 (if prefix (setq fs (mapcar (lambda (f) (concat prefix f)) fs)))
610a6418 775 (or
0b82e382
SM
776 ;; use any previously used preference
777 (cdr (assoc fs diff-remembered-files-alist))
778 ;; try to be clever and use previous choices as an inspiration
779 (dolist (rf diff-remembered-files-alist)
780 (let ((newfile (diff-merge-strings (caar rf) (car fs) (cdr rf))))
781 (if (and newfile (file-exists-p newfile)) (return newfile))))
782 ;; look for each file in turn. If none found, try again but
783 ;; ignoring the first level of directory, ...
784 (do* ((files fs (delq nil (mapcar 'diff-filename-drop-dir files)))
785 (file nil nil))
786 ((or (null files)
787 (setq file (do* ((files files (cdr files))
788 (file (car files) (car files)))
f9274544
GM
789 ;; Use file-regular-p to avoid
790 ;; /dev/null, directories, etc.
791 ((or (null file) (file-regular-p file))
0b82e382
SM
792 file))))
793 file))
794 ;; <foo>.rej patches implicitly apply to <foo>
610a6418
SM
795 (and (string-match "\\.rej\\'" (or buffer-file-name ""))
796 (let ((file (substring buffer-file-name 0 (match-beginning 0))))
797 (when (file-exists-p file) file)))
e78cf8e5
SM
798 ;; If we haven't found the file, maybe it's because we haven't paid
799 ;; attention to the PCL-CVS hint.
800 (and (not prefix)
801 (boundp 'cvs-pcl-cvs-dirchange-re)
802 (save-excursion
803 (re-search-backward cvs-pcl-cvs-dirchange-re nil t))
e4456fdf
SM
804 (diff-find-file-name old batch (match-string 1)))
805 ;; Invent something, if necessary.
806 (when batch
807 (or (car fs) default-directory))
0b82e382
SM
808 ;; if all else fails, ask the user
809 (let ((file (read-file-name (format "Use file %s: " (or (first fs) ""))
810 nil (first fs) t (first fs))))
811 (set (make-local-variable 'diff-remembered-files-alist)
812 (cons (cons fs file) diff-remembered-files-alist))
610a6418
SM
813 file)))))
814
1b1b5dae 815
610a6418
SM
816(defun diff-ediff-patch ()
817 "Call `ediff-patch-file' on the current buffer."
818 (interactive)
819 (condition-case err
0e104800 820 (ediff-patch-file nil (current-buffer))
610a6418
SM
821 (wrong-number-of-arguments (ediff-patch-file))))
822
71296446 823;;;;
610a6418 824;;;; Conversion functions
71296446 825;;;;
610a6418
SM
826
827;;(defvar diff-inhibit-after-change nil
828;; "Non-nil means inhibit `diff-mode's after-change functions.")
829
830(defun diff-unified->context (start end)
831 "Convert unified diffs to context diffs.
832START and END are either taken from the region (if a prefix arg is given) or
dfbd373d 833else cover the whole buffer."
a70b0ff8
JL
834 (interactive (if (or current-prefix-arg (and transient-mark-mode mark-active))
835 (list (region-beginning) (region-end))
610a6418 836 (list (point-min) (point-max))))
e78cf8e5 837 (unless (markerp end) (setq end (copy-marker end t)))
610a6418
SM
838 (let (;;(diff-inhibit-after-change t)
839 (inhibit-read-only t))
840 (save-excursion
841 (goto-char start)
f52d2f9c
SM
842 (while (and (re-search-forward
843 (concat "^\\(\\(---\\) .+\n\\(\\+\\+\\+\\) .+\\|"
844 diff-hunk-header-re-unified ".*\\)$")
845 nil t)
610a6418
SM
846 (< (point) end))
847 (combine-after-change-calls
848 (if (match-beginning 2)
0e104800 849 ;; we matched a file header
610a6418
SM
850 (progn
851 ;; use reverse order to make sure the indices are kept valid
852 (replace-match "---" t t nil 3)
853 (replace-match "***" t t nil 2))
854 ;; we matched a hunk header
855 (let ((line1 (match-string 4))
43392d12 856 (lines1 (or (match-string 5) "1"))
610a6418 857 (line2 (match-string 6))
341dd15a 858 (lines2 (or (match-string 7) "1"))
3dc04e83
SM
859 ;; Variables to use the special undo function.
860 (old-undo buffer-undo-list)
861 (old-end (marker-position end))
862 (start (match-beginning 0))
863 (reversible t))
610a6418
SM
864 (replace-match
865 (concat "***************\n*** " line1 ","
866 (number-to-string (+ (string-to-number line1)
867 (string-to-number lines1)
341dd15a
MB
868 -1))
869 " ****"))
610a6418 870 (save-restriction
f52d2f9c
SM
871 (narrow-to-region (line-beginning-position 2)
872 ;; Call diff-end-of-hunk from just before
873 ;; the hunk header so it can use the hunk
874 ;; header info.
610a6418
SM
875 (progn (diff-end-of-hunk 'unified) (point)))
876 (let ((hunk (buffer-string)))
877 (goto-char (point-min))
878 (if (not (save-excursion (re-search-forward "^-" nil t)))
879 (delete-region (point) (point-max))
880 (goto-char (point-max))
881 (let ((modif nil) last-pt)
882 (while (progn (setq last-pt (point))
883 (= (forward-line -1) 0))
884 (case (char-after)
bdad2e31 885 (?\s (insert " ") (setq modif nil) (backward-char 1))
610a6418
SM
886 (?+ (delete-region (point) last-pt) (setq modif t))
887 (?- (if (not modif)
888 (progn (forward-char 1)
889 (insert " "))
890 (delete-char 1)
891 (insert "! "))
892 (backward-char 2))
893 (?\\ (when (save-excursion (forward-line -1)
894 (= (char-after) ?+))
895 (delete-region (point) last-pt) (setq modif t)))
f52d2f9c
SM
896 ;; diff-valid-unified-empty-line.
897 (?\n (insert " ") (setq modif nil) (backward-char 2))
610a6418
SM
898 (t (setq modif nil))))))
899 (goto-char (point-max))
900 (save-excursion
901 (insert "--- " line2 ","
902 (number-to-string (+ (string-to-number line2)
903 (string-to-number lines2)
341dd15a 904 -1))
f52d2f9c 905 " ----\n" hunk))
610a6418
SM
906 ;;(goto-char (point-min))
907 (forward-line 1)
908 (if (not (save-excursion (re-search-forward "^+" nil t)))
909 (delete-region (point) (point-max))
910 (let ((modif nil) (delete nil))
3dc04e83
SM
911 (if (save-excursion (re-search-forward "^\\+.*\n-" nil t))
912 ;; Normally, lines in a substitution come with
913 ;; first the removals and then the additions, and
914 ;; the context->unified function follows this
915 ;; convention, of course. Yet, other alternatives
916 ;; are valid as well, but they preclude the use of
917 ;; context->unified as an undo command.
918 (setq reversible nil))
610a6418
SM
919 (while (not (eobp))
920 (case (char-after)
bdad2e31 921 (?\s (insert " ") (setq modif nil) (backward-char 1))
610a6418
SM
922 (?- (setq delete t) (setq modif t))
923 (?+ (if (not modif)
924 (progn (forward-char 1)
925 (insert " "))
926 (delete-char 1)
927 (insert "! "))
928 (backward-char 2))
929 (?\\ (when (save-excursion (forward-line 1)
930 (not (eobp)))
931 (setq delete t) (setq modif t)))
f52d2f9c
SM
932 ;; diff-valid-unified-empty-line.
933 (?\n (insert " ") (setq modif nil) (backward-char 2)
934 (setq reversible nil))
610a6418
SM
935 (t (setq modif nil)))
936 (let ((last-pt (point)))
937 (forward-line 1)
938 (when delete
939 (delete-region last-pt (point))
3dc04e83
SM
940 (setq delete nil)))))))
941 (unless (or (not reversible) (eq buffer-undo-list t))
942 ;; Drop the many undo entries and replace them with
943 ;; a single entry that uses diff-context->unified to do
944 ;; the work.
945 (setq buffer-undo-list
946 (cons (list 'apply (- old-end end) start (point-max)
947 'diff-context->unified start (point-max))
948 old-undo)))))))))))
610a6418 949
28408bfd 950(defun diff-context->unified (start end &optional to-context)
610a6418 951 "Convert context diffs to unified diffs.
28408bfd
RS
952START and END are either taken from the region
953\(when it is highlighted) or else cover the whole buffer.
954With a prefix argument, convert unified format to context format."
955 (interactive (if (and transient-mark-mode mark-active)
a70b0ff8 956 (list (region-beginning) (region-end) current-prefix-arg)
28408bfd
RS
957 (list (point-min) (point-max) current-prefix-arg)))
958 (if to-context
959 (diff-unified->context start end)
e78cf8e5 960 (unless (markerp end) (setq end (copy-marker end t)))
28408bfd 961 (let ( ;;(diff-inhibit-after-change t)
be36f934 962 (inhibit-read-only t))
28408bfd 963 (save-excursion
be36f934
SM
964 (goto-char start)
965 (while (and (re-search-forward "^\\(\\(\\*\\*\\*\\) .+\n\\(---\\) .+\\|\\*\\{15\\}.*\n\\*\\*\\* \\([0-9]+\\),\\(-?[0-9]+\\) \\*\\*\\*\\*\\)$" nil t)
966 (< (point) end))
967 (combine-after-change-calls
968 (if (match-beginning 2)
969 ;; we matched a file header
970 (progn
971 ;; use reverse order to make sure the indices are kept valid
972 (replace-match "+++" t t nil 3)
973 (replace-match "---" t t nil 2))
974 ;; we matched a hunk header
975 (let ((line1s (match-string 4))
976 (line1e (match-string 5))
977 (pt1 (match-beginning 0))
978 ;; Variables to use the special undo function.
979 (old-undo buffer-undo-list)
980 (old-end (marker-position end))
981 (reversible t))
982 (replace-match "")
983 (unless (re-search-forward
bf6970a5 984 diff-context-mid-hunk-header-re nil t)
be36f934
SM
985 (error "Can't find matching `--- n1,n2 ----' line"))
986 (let ((line2s (match-string 1))
987 (line2e (match-string 2))
988 (pt2 (progn
989 (delete-region (progn (beginning-of-line) (point))
990 (progn (forward-line 1) (point)))
991 (point-marker))))
992 (goto-char pt1)
993 (forward-line 1)
994 (while (< (point) pt2)
995 (case (char-after)
996 (?! (delete-char 2) (insert "-") (forward-line 1))
997 (?- (forward-char 1) (delete-char 1) (forward-line 1))
998 (?\s ;merge with the other half of the chunk
999 (let* ((endline2
1000 (save-excursion
1001 (goto-char pt2) (forward-line 1) (point))))
1002 (case (char-after pt2)
1003 ((?! ?+)
1004 (insert "+"
1005 (prog1 (buffer-substring (+ pt2 2) endline2)
1006 (delete-region pt2 endline2))))
1007 (?\s
1008 (unless (= (- endline2 pt2)
1009 (- (line-beginning-position 2) (point)))
1010 ;; If the two lines we're merging don't have the
1011 ;; same length (can happen with "diff -b"), then
1012 ;; diff-unified->context will not properly undo
1013 ;; this operation.
1014 (setq reversible nil))
1015 (delete-region pt2 endline2)
1016 (delete-char 1)
1017 (forward-line 1))
1018 (?\\ (forward-line 1))
1019 (t (setq reversible nil)
1020 (delete-char 1) (forward-line 1)))))
1021 (t (setq reversible nil) (forward-line 1))))
1022 (while (looking-at "[+! ] ")
1023 (if (/= (char-after) ?!) (forward-char 1)
1024 (delete-char 1) (insert "+"))
1025 (delete-char 1) (forward-line 1))
1026 (save-excursion
1027 (goto-char pt1)
1028 (insert "@@ -" line1s ","
1029 (number-to-string (- (string-to-number line1e)
1030 (string-to-number line1s)
1031 -1))
1032 " +" line2s ","
1033 (number-to-string (- (string-to-number line2e)
1034 (string-to-number line2s)
1035 -1)) " @@"))
1036 (set-marker pt2 nil)
1037 ;; The whole procedure succeeded, let's replace the myriad
1038 ;; of undo elements with just a single special one.
1039 (unless (or (not reversible) (eq buffer-undo-list t))
1040 (setq buffer-undo-list
1041 (cons (list 'apply (- old-end end) pt1 (point)
1042 'diff-unified->context pt1 (point))
1043 old-undo)))
1044 )))))))))
610a6418
SM
1045
1046(defun diff-reverse-direction (start end)
1047 "Reverse the direction of the diffs.
1048START and END are either taken from the region (if a prefix arg is given) or
dfbd373d 1049else cover the whole buffer."
a70b0ff8
JL
1050 (interactive (if (or current-prefix-arg (and transient-mark-mode mark-active))
1051 (list (region-beginning) (region-end))
610a6418 1052 (list (point-min) (point-max))))
e78cf8e5 1053 (unless (markerp end) (setq end (copy-marker end t)))
610a6418
SM
1054 (let (;;(diff-inhibit-after-change t)
1055 (inhibit-read-only t))
1056 (save-excursion
1057 (goto-char start)
c0078a04 1058 (while (and (re-search-forward "^\\(\\([-*][-*][-*] \\)\\(.+\\)\n\\([-+][-+][-+] \\)\\(.+\\)\\|\\*\\{15\\}.*\n\\*\\*\\* \\(.+\\) \\*\\*\\*\\*\\|@@ -\\([0-9,]+\\) \\+\\([0-9,]+\\) @@.*\\)$" nil t)
610a6418
SM
1059 (< (point) end))
1060 (combine-after-change-calls
1061 (cond
1062 ;; a file header
1063 ((match-beginning 2) (replace-match "\\2\\5\n\\4\\3" nil))
1064 ;; a context-diff hunk header
1065 ((match-beginning 6)
1066 (let ((pt-lines1 (match-beginning 6))
1067 (lines1 (match-string 6)))
1068 (replace-match "" nil nil nil 6)
1069 (forward-line 1)
1070 (let ((half1s (point)))
0e104800 1071 (while (looking-at "[-! \\][ \t]\\|#")
610a6418
SM
1072 (when (= (char-after) ?-) (delete-char 1) (insert "+"))
1073 (forward-line 1))
fef8c55b 1074 (let ((half1 (delete-and-extract-region half1s (point))))
bf6970a5 1075 (unless (looking-at diff-context-mid-hunk-header-re)
610a6418
SM
1076 (insert half1)
1077 (error "Can't find matching `--- n1,n2 ----' line"))
bf6970a5
SM
1078 (let* ((str1end (or (match-end 2) (match-end 1)))
1079 (str1 (buffer-substring (match-beginning 1) str1end)))
1080 (goto-char str1end)
1081 (insert lines1)
1082 (delete-region (match-beginning 1) str1end)
610a6418
SM
1083 (forward-line 1)
1084 (let ((half2s (point)))
0e104800 1085 (while (looking-at "[!+ \\][ \t]\\|#")
610a6418
SM
1086 (when (= (char-after) ?+) (delete-char 1) (insert "-"))
1087 (forward-line 1))
fef8c55b 1088 (let ((half2 (delete-and-extract-region half2s (point))))
0e104800 1089 (insert (or half1 ""))
610a6418 1090 (goto-char half1s)
0e104800 1091 (insert (or half2 ""))))
610a6418
SM
1092 (goto-char pt-lines1)
1093 (insert str1))))))
1094 ;; a unified-diff hunk header
1095 ((match-beginning 7)
1096 (replace-match "@@ -\\8 +\\7 @@" nil)
1097 (forward-line 1)
1098 (let ((c (char-after)) first last)
1099 (while (case (setq c (char-after))
1100 (?- (setq first (or first (point)))
1101 (delete-char 1) (insert "+") t)
1102 (?+ (setq last (or last (point)))
1103 (delete-char 1) (insert "-") t)
0e104800 1104 ((?\\ ?#) t)
610a6418 1105 (t (when (and first last (< first last))
cb4e8176 1106 (insert (delete-and-extract-region first last)))
610a6418 1107 (setq first nil last nil)
f52d2f9c
SM
1108 (memq c (if diff-valid-unified-empty-line
1109 '(?\s ?\n) '(?\s)))))
610a6418
SM
1110 (forward-line 1))))))))))
1111
1112(defun diff-fixup-modifs (start end)
1113 "Fixup the hunk headers (in case the buffer was modified).
1114START and END are either taken from the region (if a prefix arg is given) or
dfbd373d 1115else cover the whole buffer."
a70b0ff8
JL
1116 (interactive (if (or current-prefix-arg (and transient-mark-mode mark-active))
1117 (list (region-beginning) (region-end))
610a6418
SM
1118 (list (point-min) (point-max))))
1119 (let ((inhibit-read-only t))
1120 (save-excursion
f52d2f9c 1121 (goto-char end) (diff-end-of-hunk nil 'donttrustheader)
610a6418
SM
1122 (let ((plus 0) (minus 0) (space 0) (bang 0))
1123 (while (and (= (forward-line -1) 0) (<= start (point)))
bd9d7d76 1124 (if (not (looking-at
f52d2f9c 1125 (concat diff-hunk-header-re-unified
bd9d7d76
SM
1126 "\\|[-*][-*][-*] [0-9,]+ [-*][-*][-*][-*]$"
1127 "\\|--- .+\n\\+\\+\\+ ")))
610a6418 1128 (case (char-after)
bd9d7d76 1129 (?\s (incf space))
610a6418
SM
1130 (?+ (incf plus))
1131 (?- (incf minus))
1132 (?! (incf bang))
0e104800 1133 ((?\\ ?#) nil)
610a6418
SM
1134 (t (setq space 0 plus 0 minus 0 bang 0)))
1135 (cond
f52d2f9c
SM
1136 ((looking-at diff-hunk-header-re-unified)
1137 (let* ((old1 (match-string 2))
1138 (old2 (match-string 4))
610a6418
SM
1139 (new1 (number-to-string (+ space minus)))
1140 (new2 (number-to-string (+ space plus))))
f52d2f9c
SM
1141 (if old2
1142 (unless (string= new2 old2) (replace-match new2 t t nil 4))
1143 (goto-char (match-end 4)) (insert "," new2))
1144 (if old1
1145 (unless (string= new1 old1) (replace-match new1 t t nil 2))
1146 (goto-char (match-end 2)) (insert "," new1))))
bf6970a5 1147 ((looking-at diff-context-mid-hunk-header-re)
610a6418
SM
1148 (when (> (+ space bang plus) 0)
1149 (let* ((old1 (match-string 1))
1150 (old2 (match-string 2))
1151 (new (number-to-string
1152 (+ space bang plus -1 (string-to-number old1)))))
1153 (unless (string= new old2) (replace-match new t t nil 2)))))
1154 ((looking-at "\\*\\*\\* \\([0-9]+\\),\\(-?[0-9]*\\) \\*\\*\\*\\*$")
1155 (when (> (+ space bang minus) 0)
1156 (let* ((old (match-string 1))
1157 (new (format
1158 (concat "%0" (number-to-string (length old)) "d")
1159 (+ space bang minus -1 (string-to-number old)))))
1160 (unless (string= new old) (replace-match new t t nil 2))))))
1161 (setq space 0 plus 0 minus 0 bang 0)))))))
1162
71296446 1163;;;;
610a6418 1164;;;; Hooks
71296446 1165;;;;
610a6418
SM
1166
1167(defun diff-write-contents-hooks ()
1168 "Fixup hunk headers if necessary."
1169 (if (buffer-modified-p) (diff-fixup-modifs (point-min) (point-max)))
1170 nil)
1171
610a6418
SM
1172;; It turns out that making changes in the buffer from within an
1173;; *-change-function is asking for trouble, whereas making them
1174;; from a post-command-hook doesn't pose much problems
1175(defvar diff-unhandled-changes nil)
1176(defun diff-after-change-function (beg end len)
1177 "Remember to fixup the hunk header.
1178See `after-change-functions' for the meaning of BEG, END and LEN."
c0078a04
SM
1179 ;; Ignoring changes when inhibit-read-only is set is strictly speaking
1180 ;; incorrect, but it turns out that inhibit-read-only is normally not set
1181 ;; inside editing commands, while it tends to be set when the buffer gets
1182 ;; updated by an async process or by a conversion function, both of which
1183 ;; would rather not be uselessly slowed down by this hook.
610a6418
SM
1184 (when (and (not undo-in-progress) (not inhibit-read-only))
1185 (if diff-unhandled-changes
1186 (setq diff-unhandled-changes
1187 (cons (min beg (car diff-unhandled-changes))
cb4e8176 1188 (max end (cdr diff-unhandled-changes))))
610a6418
SM
1189 (setq diff-unhandled-changes (cons beg end)))))
1190
1191(defun diff-post-command-hook ()
1192 "Fixup hunk headers if necessary."
1193 (when (consp diff-unhandled-changes)
1194 (ignore-errors
1195 (save-excursion
fef8c55b 1196 (goto-char (car diff-unhandled-changes))
cb4e8176
SM
1197 ;; Maybe we've cut the end of the hunk before point.
1198 (if (and (bolp) (not (bobp))) (backward-char 1))
bf6970a5
SM
1199 ;; We used to fixup modifs on all the changes, but it turns out that
1200 ;; it's safer not to do it on big changes, e.g. when yanking a big
1201 ;; diff, or when the user edits the header, since we might then
1202 ;; screw up perfectly correct values. --Stef
1eabc5e6 1203 (diff-beginning-of-hunk)
bf6970a5
SM
1204 (let* ((style (if (looking-at "\\*\\*\\*") 'context))
1205 (start (line-beginning-position (if (eq style 'context) 3 2)))
1206 (mid (if (eq style 'context)
1207 (save-excursion
1208 (re-search-forward diff-context-mid-hunk-header-re
1209 nil t)))))
1210 (when (and ;; Don't try to fixup changes in the hunk header.
1211 (> (car diff-unhandled-changes) start)
1212 ;; Don't try to fixup changes in the mid-hunk header either.
1213 (or (not mid)
1214 (< (cdr diff-unhandled-changes) (match-beginning 0))
1215 (> (car diff-unhandled-changes) (match-end 0)))
1216 (save-excursion
f52d2f9c 1217 (diff-end-of-hunk nil 'donttrustheader)
bf6970a5
SM
1218 ;; Don't try to fixup changes past the end of the hunk.
1219 (>= (point) (cdr diff-unhandled-changes))))
1eabc5e6 1220 (diff-fixup-modifs (point) (cdr diff-unhandled-changes)))))
bf6970a5 1221 (setq diff-unhandled-changes nil))))
610a6418 1222
6fc049f6
SM
1223(defun diff-next-error (arg reset)
1224 ;; Select a window that displays the current buffer so that point
1225 ;; movements are reflected in that window. Otherwise, the user might
1226 ;; never see the hunk corresponding to the source she's jumping to.
1227 (pop-to-buffer (current-buffer))
1228 (if reset (goto-char (point-min)))
1229 (diff-hunk-next arg)
1230 (diff-goto-source))
610a6418 1231
610a6418 1232;;;###autoload
d87e5627 1233(define-derived-mode diff-mode fundamental-mode "Diff"
0b82e382 1234 "Major mode for viewing/editing context diffs.
769dd0f1 1235Supports unified and context diffs as well as (to a lesser extent)
6035b97e 1236normal diffs.
b64d66b5 1237
df52e0e7 1238When the buffer is read-only, the ESC prefix is not necessary.
3b696504 1239If you edit the buffer manually, diff-mode will try to update the hunk
df52e0e7
SM
1240headers for you on-the-fly.
1241
1242You can also switch between context diff and unified diff with \\[diff-context->unified],
72147c53 1243or vice versa with \\[diff-unified->context] and you can also reverse the direction of
e78cf8e5 1244a diff with \\[diff-reverse-direction].
b64d66b5 1245
b64d66b5
RS
1246 \\{diff-mode-map}"
1247
610a6418
SM
1248 (set (make-local-variable 'font-lock-defaults) diff-font-lock-defaults)
1249 (set (make-local-variable 'outline-regexp) diff-outline-regexp)
ccce6558
DL
1250 (set (make-local-variable 'imenu-generic-expression)
1251 diff-imenu-generic-expression)
19e713d8
DL
1252 ;; These are not perfect. They would be better done separately for
1253 ;; context diffs and unidiffs.
1254 ;; (set (make-local-variable 'paragraph-start)
1255 ;; (concat "@@ " ; unidiff hunk
1256 ;; "\\|\\*\\*\\* " ; context diff hunk or file start
1257 ;; "\\|--- [^\t]+\t")) ; context or unidiff file
1258 ;; ; start (first or second line)
1259 ;; (set (make-local-variable 'paragraph-separate) paragraph-start)
1260 ;; (set (make-local-variable 'page-delimiter) "--- [^\t]+\t")
1261 ;; compile support
6fc049f6 1262 (set (make-local-variable 'next-error-function) 'diff-next-error)
cdbb79c1 1263
19446c41
DN
1264 (set (make-local-variable 'beginning-of-defun-function)
1265 'diff-beginning-of-file-and-junk)
1266 (set (make-local-variable 'end-of-defun-function)
1267 'diff-end-of-file)
1268
53dd481c 1269 (setq buffer-read-only diff-default-read-only)
769dd0f1 1270 ;; setup change hooks
e8a1ed31 1271 (if (not diff-update-on-the-fly)
3ccdb14e 1272 (add-hook 'write-contents-functions 'diff-write-contents-hooks nil t)
610a6418 1273 (make-local-variable 'diff-unhandled-changes)
3cec9c57
SM
1274 (add-hook 'after-change-functions 'diff-after-change-function nil t)
1275 (add-hook 'post-command-hook 'diff-post-command-hook nil t))
610a6418 1276 ;; Neat trick from Dave Love to add more bindings in read-only mode:
e78cf8e5 1277 (lexical-let ((ro-bind (cons 'buffer-read-only diff-mode-shared-map)))
22cd1973
SM
1278 (add-to-list 'minor-mode-overriding-map-alist ro-bind)
1279 ;; Turn off this little trick in case the buffer is put in view-mode.
1280 (add-hook 'view-mode-hook
e78cf8e5
SM
1281 (lambda ()
1282 (setq minor-mode-overriding-map-alist
1283 (delq ro-bind minor-mode-overriding-map-alist)))
22cd1973 1284 nil t))
281096ed
SM
1285 ;; add-log support
1286 (set (make-local-variable 'add-log-current-defun-function)
1287 'diff-current-defun)
1b1b5dae
SM
1288 (set (make-local-variable 'add-log-buffer-file-name-function)
1289 'diff-find-file-name))
610a6418
SM
1290
1291;;;###autoload
0b82e382
SM
1292(define-minor-mode diff-minor-mode
1293 "Minor mode for viewing/editing context diffs.
1294\\{diff-minor-mode-map}"
cfc80227 1295 :group 'diff-mode :lighter " Diff"
0b82e382 1296 ;; FIXME: setup font-lock
fef8c55b 1297 ;; setup change hooks
e8a1ed31 1298 (if (not diff-update-on-the-fly)
3ccdb14e 1299 (add-hook 'write-contents-functions 'diff-write-contents-hooks nil t)
fef8c55b 1300 (make-local-variable 'diff-unhandled-changes)
3cec9c57
SM
1301 (add-hook 'after-change-functions 'diff-after-change-function nil t)
1302 (add-hook 'post-command-hook 'diff-post-command-hook nil t)))
0b82e382 1303
e78cf8e5 1304;;; Handy hook functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
22cd1973
SM
1305
1306(defun diff-delete-if-empty ()
1307 ;; An empty diff file means there's no more diffs to integrate, so we
1308 ;; can just remove the file altogether. Very handy for .rej files if we
1309 ;; remove hunks as we apply them.
1310 (when (and buffer-file-name
1311 (eq 0 (nth 7 (file-attributes buffer-file-name))))
1312 (delete-file buffer-file-name)))
1313
1314(defun diff-delete-empty-files ()
1315 "Arrange for empty diff files to be removed."
1316 (add-hook 'after-save-hook 'diff-delete-if-empty nil t))
1317
1318(defun diff-make-unified ()
1319 "Turn context diffs into unified diffs if applicable."
1320 (if (save-excursion
1321 (goto-char (point-min))
4c174fb4 1322 (and (looking-at diff-hunk-header-re) (eq (char-after) ?*)))
22cd1973
SM
1323 (let ((mod (buffer-modified-p)))
1324 (unwind-protect
1325 (diff-context->unified (point-min) (point-max))
1326 (restore-buffer-modified-p mod)))))
610a6418 1327
027ac3f8
SM
1328;;;
1329;;; Misc operations that have proved useful at some point.
1330;;;
1331
1332(defun diff-next-complex-hunk ()
1333 "Jump to the next \"complex\" hunk.
1334\"Complex\" is approximated by \"the hunk changes the number of lines\".
1335Only works for unified diffs."
1336 (interactive)
1337 (while
f52d2f9c
SM
1338 (and (re-search-forward diff-hunk-header-re-unified nil t)
1339 (equal (match-string 2) (match-string 4)))))
027ac3f8 1340
3a349573
SM
1341(defun diff-sanity-check-context-hunk-half (lines)
1342 (let ((count lines))
1343 (while
1344 (cond
1345 ((and (memq (char-after) '(?\s ?! ?+ ?-))
1346 (memq (char-after (1+ (point))) '(?\s ?\t)))
1347 (decf count) t)
1348 ((or (zerop count) (= count lines)) nil)
1349 ((memq (char-after) '(?! ?+ ?-))
1350 (if (not (and (eq (char-after (1+ (point))) ?\n)
1351 (y-or-n-p "Try to auto-fix whitespace loss damage? ")))
1352 (error "End of hunk ambiguously marked")
1353 (forward-char 1) (insert " ") (forward-line -1) t))
1354 ((< lines 0)
1355 (error "End of hunk ambiguously marked"))
1356 ((not (y-or-n-p "Try to auto-fix whitespace loss and word-wrap damage? "))
1357 (error "Abort!"))
1358 ((eolp) (insert " ") (forward-line -1) t)
1359 (t (insert " ") (delete-region (- (point) 2) (- (point) 1)) t))
1360 (forward-line))))
1361
1362(defun diff-sanity-check-hunk ()
1363 (let (;; Every modification is protected by a y-or-n-p, so it's probably
1364 ;; OK to override a read-only setting.
1365 (inhibit-read-only t))
1366 (save-excursion
1367 (cond
1368 ((not (looking-at diff-hunk-header-re))
1369 (error "Not recognizable hunk header"))
1370
1371 ;; A context diff.
1372 ((eq (char-after) ?*)
7fb6ce6e 1373 (if (not (looking-at "\\*\\{15\\}\\(?: .*\\)?\n\\*\\*\\* \\([0-9]+\\)\\(?:,\\([0-9]+\\)\\)? \\*\\*\\*\\*"))
3a349573
SM
1374 (error "Unrecognized context diff first hunk header format")
1375 (forward-line 2)
1376 (diff-sanity-check-context-hunk-half
f52d2f9c 1377 (if (match-end 2)
7fb6ce6e
TTN
1378 (1+ (- (string-to-number (match-string 2))
1379 (string-to-number (match-string 1))))
1380 1))
bf6970a5 1381 (if (not (looking-at diff-context-mid-hunk-header-re))
3a349573
SM
1382 (error "Unrecognized context diff second hunk header format")
1383 (forward-line)
1384 (diff-sanity-check-context-hunk-half
f52d2f9c 1385 (if (match-end 2)
7fb6ce6e
TTN
1386 (1+ (- (string-to-number (match-string 2))
1387 (string-to-number (match-string 1))))
1388 1)))))
3a349573
SM
1389
1390 ;; A unified diff.
1391 ((eq (char-after) ?@)
f52d2f9c 1392 (if (not (looking-at diff-hunk-header-re-unified))
3a349573 1393 (error "Unrecognized unified diff hunk header format")
43392d12
SM
1394 (let ((before (string-to-number (or (match-string 2) "1")))
1395 (after (string-to-number (or (match-string 4) "1"))))
3a349573
SM
1396 (forward-line)
1397 (while
1398 (case (char-after)
1399 (?\s (decf before) (decf after) t)
80a01d97
SM
1400 (?-
1401 (if (and (looking-at diff-file-header-re)
1402 (zerop before) (zerop after))
1403 ;; No need to query: this is a case where two patches
1404 ;; are concatenated and only counting the lines will
1405 ;; give the right result. Let's just add an empty
1406 ;; line so that our code which doesn't count lines
1407 ;; will not get confused.
1408 (progn (save-excursion (insert "\n")) nil)
1409 (decf before) t))
3a349573
SM
1410 (?+ (decf after) t)
1411 (t
1412 (cond
f52d2f9c
SM
1413 ((and diff-valid-unified-empty-line
1414 ;; Not just (eolp) so we don't infloop at eob.
79fd6168
SM
1415 (eq (char-after) ?\n)
1416 (> before 0) (> after 0))
f52d2f9c 1417 (decf before) (decf after) t)
3a349573
SM
1418 ((and (zerop before) (zerop after)) nil)
1419 ((or (< before 0) (< after 0))
1420 (error (if (or (zerop before) (zerop after))
1421 "End of hunk ambiguously marked"
1422 "Hunk seriously messed up")))
f52d2f9c 1423 ((not (y-or-n-p (concat "Try to auto-fix " (if (eolp) "whitespace loss" "word-wrap damage") "? ")))
3a349573
SM
1424 (error "Abort!"))
1425 ((eolp) (insert " ") (forward-line -1) t)
1426 (t (insert " ")
1427 (delete-region (- (point) 2) (- (point) 1)) t))))
1428 (forward-line)))))
1429
1430 ;; A plain diff.
1431 (t
1432 ;; TODO.
1433 )))))
1434
1eabc5e6
SM
1435(defun diff-hunk-text (hunk destp char-offset)
1436 "Return the literal source text from HUNK as (TEXT . OFFSET).
a70b0ff8 1437If DESTP is nil, TEXT is the source, otherwise the destination text.
1eabc5e6
SM
1438CHAR-OFFSET is a char-offset in HUNK, and OFFSET is the corresponding
1439char-offset in TEXT."
3017133f 1440 (with-temp-buffer
6e4e8a3b
SM
1441 (insert hunk)
1442 (goto-char (point-min))
1443 (let ((src-pos nil)
1444 (dst-pos nil)
1445 (divider-pos nil)
1446 (num-pfx-chars 2))
1447 ;; Set the following variables:
1448 ;; SRC-POS buffer pos of the source part of the hunk or nil if none
1449 ;; DST-POS buffer pos of the destination part of the hunk or nil
1450 ;; DIVIDER-POS buffer pos of any divider line separating the src & dst
1451 ;; NUM-PFX-CHARS number of line-prefix characters used by this format"
1452 (cond ((looking-at "^@@")
1453 ;; unified diff
1454 (setq num-pfx-chars 1)
1455 (forward-line 1)
1456 (setq src-pos (point) dst-pos (point)))
1457 ((looking-at "^\\*\\*")
1458 ;; context diff
1459 (forward-line 2)
1460 (setq src-pos (point))
bf6970a5 1461 (re-search-forward diff-context-mid-hunk-header-re nil t)
6e4e8a3b
SM
1462 (forward-line 0)
1463 (setq divider-pos (point))
1464 (forward-line 1)
1465 (setq dst-pos (point)))
1466 ((looking-at "^[0-9]+a[0-9,]+$")
1467 ;; normal diff, insert
1468 (forward-line 1)
1469 (setq dst-pos (point)))
1470 ((looking-at "^[0-9,]+d[0-9]+$")
1471 ;; normal diff, delete
1472 (forward-line 1)
1473 (setq src-pos (point)))
1474 ((looking-at "^[0-9,]+c[0-9,]+$")
1475 ;; normal diff, change
1476 (forward-line 1)
1477 (setq src-pos (point))
1478 (re-search-forward "^---$" nil t)
1479 (forward-line 0)
1480 (setq divider-pos (point))
1481 (forward-line 1)
1482 (setq dst-pos (point)))
1483 (t
1484 (error "Unknown diff hunk type")))
1485
1486 (if (if destp (null dst-pos) (null src-pos))
1487 ;; Implied empty text
1488 (if char-offset '("" . 0) "")
1489
1490 ;; For context diffs, either side can be empty, (if there's only
1491 ;; added or only removed text). We should then use the other side.
1492 (cond ((equal src-pos divider-pos) (setq src-pos dst-pos))
1493 ((equal dst-pos (point-max)) (setq dst-pos src-pos)))
1494
1495 (when char-offset (goto-char (+ (point-min) char-offset)))
1496
1497 ;; Get rid of anything except the desired text.
1498 (save-excursion
1499 ;; Delete unused text region
1500 (let ((keep (if destp dst-pos src-pos)))
1501 (when (and divider-pos (> divider-pos keep))
1502 (delete-region divider-pos (point-max)))
1503 (delete-region (point-min) keep))
1504 ;; Remove line-prefix characters, and unneeded lines (unified diffs).
1505 (let ((kill-char (if destp ?- ?+)))
1506 (goto-char (point-min))
1507 (while (not (eobp))
1508 (if (eq (char-after) kill-char)
1509 (delete-region (point) (progn (forward-line 1) (point)))
1510 (delete-char num-pfx-chars)
1511 (forward-line 1)))))
1512
1513 (let ((text (buffer-substring-no-properties (point-min) (point-max))))
1514 (if char-offset (cons text (- (point) (point-min))) text))))))
370d860c 1515
7530b6da 1516
c0aa75f7 1517(defun diff-find-text (text)
1eabc5e6 1518 "Return the buffer position (BEG . END) of the nearest occurrence of TEXT.
7530b6da 1519If TEXT isn't found, nil is returned."
7530b6da
MB
1520 (let* ((orig (point))
1521 (forw (and (search-forward text nil t)
1eabc5e6 1522 (cons (match-beginning 0) (match-end 0))))
7530b6da
MB
1523 (back (and (goto-char (+ orig (length text)))
1524 (search-backward text nil t)
1eabc5e6
SM
1525 (cons (match-beginning 0) (match-end 0)))))
1526 ;; Choose the closest match.
1527 (if (and forw back)
1528 (if (> (- (car forw) orig) (- orig (car back))) back forw)
1529 (or back forw))))
1530
1531(defun diff-find-approx-text (text)
1532 "Return the buffer position (BEG . END) of the nearest occurrence of TEXT.
1533Whitespace differences are ignored."
1534 (let* ((orig (point))
1535 (re (concat "^[ \t\n\f]*"
1536 (mapconcat 'regexp-quote (split-string text) "[ \t\n\f]+")
1537 "[ \t\n\f]*\n"))
1538 (forw (and (re-search-forward re nil t)
1539 (cons (match-beginning 0) (match-end 0))))
1540 (back (and (goto-char (+ orig (length text)))
1541 (re-search-backward re nil t)
1542 (cons (match-beginning 0) (match-end 0)))))
1543 ;; Choose the closest match.
7530b6da 1544 (if (and forw back)
1eabc5e6 1545 (if (> (- (car forw) orig) (- orig (car back))) back forw)
7530b6da
MB
1546 (or back forw))))
1547
44a07c5a 1548(defsubst diff-xor (a b) (if a (if (not b) a) b))
370d860c 1549
d868b3bd 1550(defun diff-find-source-location (&optional other-file reverse)
1eabc5e6
SM
1551 "Find out (BUF LINE-OFFSET POS SRC DST SWITCHED).
1552BUF is the buffer corresponding to the source file.
1553LINE-OFFSET is the offset between the expected and actual positions
1554 of the text of the hunk or nil if the text was not found.
1555POS is a pair (BEG . END) indicating the position of the text in the buffer.
1556SRC and DST are the two variants of text as returned by `diff-hunk-text'.
1557 SRC is the variant that was found in the buffer.
1558SWITCHED is non-nil if the patch is already applied."
7b91e0f2 1559 (save-excursion
e8a1ed31 1560 (let* ((other (diff-xor other-file diff-jump-to-old-file))
fd691799
SM
1561 (char-offset (- (point) (progn (diff-beginning-of-hunk 'try-harder)
1562 (point))))
3a349573
SM
1563 ;; Check that the hunk is well-formed. Otherwise diff-mode and
1564 ;; the user may disagree on what constitutes the hunk
1565 ;; (e.g. because an empty line truncates the hunk mid-course),
1566 ;; leading to potentially nasty surprises for the user.
1567 (_ (diff-sanity-check-hunk))
370d860c
SM
1568 (hunk (buffer-substring (point)
1569 (save-excursion (diff-end-of-hunk) (point))))
1570 (old (diff-hunk-text hunk reverse char-offset))
1571 (new (diff-hunk-text hunk (not reverse) char-offset))
7b91e0f2 1572 ;; Find the location specification.
d868b3bd 1573 (line (if (not (looking-at "\\(?:\\*\\{15\\}.*\n\\)?[-@* ]*\\([0-9,]+\\)\\([ acd+]+\\([0-9,]+\\)\\)?"))
370d860c
SM
1574 (error "Can't find the hunk header")
1575 (if other (match-string 1)
1576 (if (match-end 3) (match-string 3)
bf6970a5
SM
1577 (unless (re-search-forward
1578 diff-context-mid-hunk-header-re nil t)
370d860c
SM
1579 (error "Can't find the hunk separator"))
1580 (match-string 1)))))
1581 (file (or (diff-find-file-name other) (error "Can't find the file")))
1582 (buf (find-file-noselect file)))
7b91e0f2
SM
1583 ;; Update the user preference if he so wished.
1584 (when (> (prefix-numeric-value other-file) 8)
e8a1ed31 1585 (setq diff-jump-to-old-file other))
d868b3bd
SM
1586 (with-current-buffer buf
1587 (goto-line (string-to-number line))
1588 (let* ((orig-pos (point))
1eabc5e6 1589 (switched nil)
22cd1973 1590 ;; FIXME: Check for case where both OLD and NEW are found.
1eabc5e6
SM
1591 (pos (or (diff-find-text (car old))
1592 (progn (setq switched t) (diff-find-text (car new)))
1593 (progn (setq switched nil)
22cd1973
SM
1594 (condition-case nil
1595 (diff-find-approx-text (car old))
1596 (invalid-regexp nil))) ;Regex too big.
1eabc5e6 1597 (progn (setq switched t)
22cd1973
SM
1598 (condition-case nil
1599 (diff-find-approx-text (car new))
1600 (invalid-regexp nil))) ;Regex too big.
1eabc5e6 1601 (progn (setq switched nil) nil))))
370d860c
SM
1602 (nconc
1603 (list buf)
1eabc5e6
SM
1604 (if pos
1605 (list (count-lines orig-pos (car pos)) pos)
1606 (list nil (cons orig-pos (+ orig-pos (length (car old))))))
370d860c
SM
1607 (if switched (list new old t) (list old new))))))))
1608
7b91e0f2 1609
370d860c
SM
1610(defun diff-hunk-status-msg (line-offset reversed dry-run)
1611 (let ((msg (if dry-run
1612 (if reversed "already applied" "not yet applied")
1613 (if reversed "undone" "applied"))))
1614 (message (cond ((null line-offset) "Hunk text not found")
1615 ((= line-offset 0) "Hunk %s")
1616 ((= line-offset 1) "Hunk %s at offset %d line")
1617 (t "Hunk %s at offset %d lines"))
1618 msg line-offset)))
1619
cb9a30c8 1620(defvar diff-apply-hunk-to-backup-file nil)
370d860c
SM
1621
1622(defun diff-apply-hunk (&optional reverse)
6e4e8a3b 1623 "Apply the current hunk to the source file and go to the next.
7530b6da 1624By default, the new source file is patched, but if the variable
e8a1ed31 1625`diff-jump-to-old-file' is non-nil, then the old source file is
7530b6da
MB
1626patched instead (some commands, such as `diff-goto-source' can change
1627the value of this variable when given an appropriate prefix argument).
1628
4eaa6852 1629With a prefix argument, REVERSE the hunk."
370d860c 1630 (interactive "P")
281096ed 1631 (destructuring-bind (buf line-offset pos old new &optional switched)
44a07c5a
SM
1632 ;; Sometimes we'd like to have the following behavior: if REVERSE go
1633 ;; to the new file, otherwise go to the old. But that means that by
1634 ;; default we use the old file, which is the opposite of the default
1635 ;; for diff-goto-source, and is thus confusing. Also when you don't
1636 ;; know about it it's pretty surprising.
1637 ;; TODO: make it possible to ask explicitly for this behavior.
1638 ;;
1639 ;; This is duplicated in diff-test-hunk.
1640 (diff-find-source-location nil reverse)
370d860c 1641 (cond
4eaa6852
MB
1642 ((null line-offset)
1643 (error "Can't find the text to patch"))
cb9a30c8
SM
1644 ((with-current-buffer buf
1645 (and buffer-file-name
1646 (backup-file-name-p buffer-file-name)
1647 (not diff-apply-hunk-to-backup-file)
1648 (not (set (make-local-variable 'diff-apply-hunk-to-backup-file)
1649 (yes-or-no-p (format "Really apply this hunk to %s? "
1650 (file-name-nondirectory
1651 buffer-file-name)))))))
593b4fa8
AS
1652 (error "%s"
1653 (substitute-command-keys
cb9a30c8
SM
1654 (format "Use %s\\[diff-apply-hunk] to apply it to the other file"
1655 (if (not reverse) "\\[universal-argument] ")))))
370d860c 1656 ((and switched
cdbb79c1 1657 ;; A reversed patch was detected, perhaps apply it in reverse.
370d860c
SM
1658 (not (save-window-excursion
1659 (pop-to-buffer buf)
1eabc5e6 1660 (goto-char (+ (car pos) (cdr old)))
370d860c
SM
1661 (y-or-n-p
1662 (if reverse
1663 "Hunk hasn't been applied yet; apply it now? "
1664 "Hunk has already been applied; undo it? ")))))
4eaa6852 1665 (message "(Nothing done)"))
370d860c 1666 (t
4eaa6852
MB
1667 ;; Apply the hunk
1668 (with-current-buffer buf
1eabc5e6
SM
1669 (goto-char (car pos))
1670 (delete-region (car pos) (cdr pos))
4eaa6852
MB
1671 (insert (car new)))
1672 ;; Display BUF in a window
1eabc5e6 1673 (set-window-point (display-buffer buf) (+ (car pos) (cdr new)))
4eaa6852
MB
1674 (diff-hunk-status-msg line-offset (diff-xor switched reverse) nil)
1675 (when diff-advance-after-apply-hunk
1676 (diff-hunk-next))))))
370d860c 1677
7530b6da 1678
7530b6da
MB
1679(defun diff-test-hunk (&optional reverse)
1680 "See whether it's possible to apply the current hunk.
4eaa6852 1681With a prefix argument, try to REVERSE the hunk."
7530b6da 1682 (interactive "P")
370d860c 1683 (destructuring-bind (buf line-offset pos src dst &optional switched)
44a07c5a 1684 (diff-find-source-location nil reverse)
1eabc5e6 1685 (set-window-point (display-buffer buf) (+ (car pos) (cdr src)))
4eaa6852 1686 (diff-hunk-status-msg line-offset (diff-xor reverse switched) t)))
370d860c 1687
7530b6da 1688
a1e2d7f7
SM
1689(defalias 'diff-mouse-goto-source 'diff-goto-source)
1690
1691(defun diff-goto-source (&optional other-file event)
7530b6da 1692 "Jump to the corresponding source line.
e8a1ed31 1693`diff-jump-to-old-file' (or its opposite if the OTHER-FILE prefix arg
281096ed 1694is given) determines whether to jump to the old or the new file.
7530b6da 1695If the prefix arg is bigger than 8 (for example with \\[universal-argument] \\[universal-argument])
a70b0ff8 1696then `diff-jump-to-old-file' is also set, for the next invocations."
a1e2d7f7 1697 (interactive (list current-prefix-arg last-input-event))
a6373340
SM
1698 ;; When pointing at a removal line, we probably want to jump to
1699 ;; the old location, and else to the new (i.e. as if reverting).
1700 ;; This is a convenient detail when using smerge-diff.
a1e2d7f7 1701 (if event (posn-set-point (event-end event)))
a6373340
SM
1702 (let ((rev (not (save-excursion (beginning-of-line) (looking-at "[-<]")))))
1703 (destructuring-bind (buf line-offset pos src dst &optional switched)
1704 (diff-find-source-location other-file rev)
1705 (pop-to-buffer buf)
1eabc5e6 1706 (goto-char (+ (car pos) (cdr src)))
55d5d717 1707 (diff-hunk-status-msg line-offset (diff-xor rev switched) t))))
370d860c 1708
7530b6da 1709
281096ed 1710(defun diff-current-defun ()
370d860c
SM
1711 "Find the name of function at point.
1712For use in `add-log-current-defun-function'."
ef63ea1c
SM
1713 ;; Kill change-log-default-name so it gets recomputed each time, since
1714 ;; each hunk may belong to another file which may belong to another
1715 ;; directory and hence have a different ChangeLog file.
1716 (kill-local-variable 'change-log-default-name)
2f9edc8b
KG
1717 (save-excursion
1718 (when (looking-at diff-hunk-header-re)
1719 (forward-line 1)
4f815065 1720 (re-search-forward "^[^ ]" nil t))
2f9edc8b
KG
1721 (destructuring-bind (buf line-offset pos src dst &optional switched)
1722 (diff-find-source-location)
281096ed
SM
1723 (beginning-of-line)
1724 (or (when (memq (char-after) '(?< ?-))
370d860c
SM
1725 ;; Cursor is pointing at removed text. This could be a removed
1726 ;; function, in which case, going to the source buffer will
1727 ;; not help since the function is now removed. Instead,
1728 ;; try to figure out the function name just from the code-fragment.
281096ed
SM
1729 (let ((old (if switched dst src)))
1730 (with-temp-buffer
1731 (insert (car old))
337f393e 1732 (funcall (buffer-local-value 'major-mode buf))
cb4e8176 1733 (goto-char (+ (point-min) (cdr old)))
281096ed
SM
1734 (add-log-current-defun))))
1735 (with-current-buffer buf
1eabc5e6 1736 (goto-char (+ (car pos) (cdr src)))
281096ed 1737 (add-log-current-defun))))))
027ac3f8 1738
2659df68
SM
1739(defun diff-ignore-whitespace-hunk ()
1740 "Re-diff the current hunk, ignoring whitespace differences."
22cd1973 1741 (interactive)
fd691799
SM
1742 (let* ((char-offset (- (point) (progn (diff-beginning-of-hunk 'try-harder)
1743 (point))))
22cd1973
SM
1744 (opts (case (char-after) (?@ "-bu") (?* "-bc") (t "-b")))
1745 (line-nb (and (or (looking-at "[^0-9]+\\([0-9]+\\)")
1746 (error "Can't find line number"))
1747 (string-to-number (match-string 1))))
14cf0430 1748 (inhibit-read-only t)
22cd1973
SM
1749 (hunk (delete-and-extract-region
1750 (point) (save-excursion (diff-end-of-hunk) (point))))
1751 (lead (make-string (1- line-nb) ?\n)) ;Line nums start at 1.
1752 (file1 (make-temp-file "diff1"))
1753 (file2 (make-temp-file "diff2"))
1754 (coding-system-for-read buffer-file-coding-system)
1755 old new)
1756 (unwind-protect
1757 (save-excursion
1758 (setq old (diff-hunk-text hunk nil char-offset))
1759 (setq new (diff-hunk-text hunk t char-offset))
1760 (write-region (concat lead (car old)) nil file1 nil 'nomessage)
1761 (write-region (concat lead (car new)) nil file2 nil 'nomessage)
1762 (with-temp-buffer
1763 (let ((status
1764 (call-process diff-command nil t nil
1765 opts file1 file2)))
1766 (case status
1767 (0 nil) ;Nothing to reformat.
1768 (1 (goto-char (point-min))
1769 ;; Remove the file-header.
1770 (when (re-search-forward diff-hunk-header-re nil t)
1771 (delete-region (point-min) (match-beginning 0))))
1772 (t (goto-char (point-max))
1773 (unless (bolp) (insert "\n"))
1774 (insert hunk)))
1775 (setq hunk (buffer-string))
1776 (unless (memq status '(0 1))
1777 (error "Diff returned: %s" status)))))
1778 ;; Whatever happens, put back some equivalent text: either the new
1779 ;; one or the original one in case some error happened.
1780 (insert hunk)
1781 (delete-file file1)
1782 (delete-file file2))))
1783
be36f934
SM
1784;;; Fine change highlighting.
1785
2659df68
SM
1786(defface diff-refine-change
1787 '((((class color) (min-colors 88) (background light))
9f4e4f5b 1788 :background "grey85")
2659df68 1789 (((class color) (min-colors 88) (background dark))
5e15554d 1790 :background "grey60")
2659df68
SM
1791 (((class color) (background light))
1792 :background "yellow")
1793 (((class color) (background dark))
1794 :background "green")
1795 (t :weight bold))
1796 "Face used for char-based changes shown by `diff-refine-hunk'."
5eee3c95 1797 :group 'diff-mode)
be36f934 1798
2659df68 1799(defun diff-refine-preproc ()
8872469d
SM
1800 (while (re-search-forward "^[+>]" nil t)
1801 ;; Remove spurious changes due to the fact that one side of the hunk is
1802 ;; marked with leading + or > and the other with leading - or <.
1803 ;; We used to replace all the prefix chars with " " but this only worked
1804 ;; when we did char-based refinement (or when using
1805 ;; smerge-refine-weight-hack) since otherwise, the `forward' motion done
1806 ;; in chopup do not necessarily do the same as the ones in highlight
1807 ;; since the "_" is not treated the same as " ".
1808 (replace-match (cdr (assq (char-before) '((?+ . "-") (?> . "<"))))))
1809 )
be36f934 1810
2659df68 1811(defun diff-refine-hunk ()
be36f934
SM
1812 "Highlight changes of hunk at point at a finer granularity."
1813 (interactive)
2f42c75f 1814 (eval-and-compile (require 'smerge-mode))
8872469d
SM
1815 (save-excursion
1816 (diff-beginning-of-hunk 'try-harder)
1817 (let* ((style (diff-hunk-style)) ;Skips the hunk header as well.
1818 (beg (point))
2659df68 1819 (props '((diff-mode . fine) (face diff-refine-change)))
8872469d
SM
1820 (end (progn (diff-end-of-hunk) (point))))
1821
1822 (remove-overlays beg end 'diff-mode 'fine)
1823
1824 (goto-char beg)
1825 (case style
1826 (unified
1827 (while (re-search-forward "^\\(?:-.*\n\\)+\\(\\)\\(?:\\+.*\n\\)+"
1828 end t)
1829 (smerge-refine-subst (match-beginning 0) (match-end 1)
1830 (match-end 1) (match-end 0)
2659df68 1831 props 'diff-refine-preproc)))
8872469d
SM
1832 (context
1833 (let* ((middle (save-excursion (re-search-forward "^---")))
1834 (other middle))
1835 (while (re-search-forward "^\\(?:!.*\n\\)+" middle t)
1836 (smerge-refine-subst (match-beginning 0) (match-end 0)
1837 (save-excursion
1838 (goto-char other)
1839 (re-search-forward "^\\(?:!.*\n\\)+" end)
1840 (setq other (match-end 0))
1841 (match-beginning 0))
1842 other
2659df68 1843 props 'diff-refine-preproc))))
8872469d
SM
1844 (t ;; Normal diffs.
1845 (let ((beg1 (1+ (point))))
1846 (when (re-search-forward "^---.*\n" end t)
1847 ;; It's a combined add&remove, so there's something to do.
1848 (smerge-refine-subst beg1 (match-beginning 0)
1849 (match-end 0) end
2659df68 1850 props 'diff-refine-preproc))))))))
be36f934
SM
1851
1852
8330c175
SM
1853(defun diff-add-change-log-entries-other-window ()
1854 "Iterate through the current diff and create ChangeLog entries.
1855I.e. like `add-change-log-entry-other-window' but applied to all hunks."
8a72c7f8
DN
1856 (interactive)
1857 ;; XXX: Currently add-change-log-entry-other-window is only called
1858 ;; once per hunk. Some hunks have multiple changes, it would be
1859 ;; good to call it for each change.
1860 (save-excursion
1861 (goto-char (point-min))
1862 (let ((orig-buffer (current-buffer)))
1863 (condition-case nil
1864 ;; Call add-change-log-entry-other-window for each hunk in
1865 ;; the diff buffer.
8330c175
SM
1866 (while (progn
1867 (diff-hunk-next)
1868 ;; Move to where the changes are,
1869 ;; `add-change-log-entry-other-window' works better in
1870 ;; that case.
02e3336d
SM
1871 (re-search-forward
1872 (concat "\n[!+-<>]"
1873 ;; If the hunk is a context hunk with an empty first
1874 ;; half, recognize the "--- NNN,MMM ----" line
1875 "\\(-- [0-9]+\\(,[0-9]+\\)? ----\n"
1876 ;; and skip to the next non-context line.
1877 "\\( .*\n\\)*[+]\\)?")
1878 nil t))
8330c175 1879 (save-excursion
2a520399 1880 (add-change-log-entry nil nil t nil t)))
8330c175 1881 ;; When there's no more hunks, diff-hunk-next signals an error.
8a72c7f8
DN
1882 (error nil)))))
1883
5b8441bf
VJL
1884(defun diff-show-trailing-whitespaces ()
1885 "Show trailing whitespaces in modified lines for diff-mode."
355b422f
VJL
1886 (interactive)
1887 (let ((whitespace-style '(trailing))
1888 (whitespace-trailing-regexp "^[-\+!<>].*?\\([\t ]+\\)$"))
d3cecb40 1889 (whitespace-mode 1))) ; display trailing whitespace in diff buffer
355b422f 1890
610a6418
SM
1891;; provide the package
1892(provide 'diff-mode)
1893
027ac3f8 1894;;; Old Change Log from when diff-mode wasn't part of Emacs:
610a6418
SM
1895;; Revision 1.11 1999/10/09 23:38:29 monnier
1896;; (diff-mode-load-hook): dropped.
1897;; (auto-mode-alist): also catch *.diffs.
1898;; (diff-find-file-name, diff-mode): add smarts to find the right file
1899;; for *.rej files (that lack any file name indication).
1900;;
1901;; Revision 1.10 1999/09/30 15:32:11 monnier
1902;; added support for "\ No newline at end of file".
1903;;
1904;; Revision 1.9 1999/09/15 00:01:13 monnier
1905;; - added basic `compile' support.
1906;; - have diff-kill-hunk call diff-kill-file if it's the only hunk.
1907;; - diff-kill-file now tries to kill the leading garbage as well.
1908;;
1909;; Revision 1.8 1999/09/13 21:10:09 monnier
1910;; - don't use CL in the autoloaded code
1911;; - accept diffs using -T
1912;;
1913;; Revision 1.7 1999/09/05 20:53:03 monnier
1914;; interface to ediff-patch
1915;;
1916;; Revision 1.6 1999/09/01 20:55:13 monnier
1917;; (ediff=patch-file): add bindings to call ediff-patch.
1918;; (diff-find-file-name): taken out of diff-goto-source.
1919;; (diff-unified->context, diff-context->unified, diff-reverse-direction,
1920;; diff-fixup-modifs): only use the region if a prefix arg is given.
1921;;
1922;; Revision 1.5 1999/08/31 19:18:52 monnier
1923;; (diff-beginning-of-file, diff-prev-file): fixed wrong parenthesis.
1924;;
1925;; Revision 1.4 1999/08/31 13:01:44 monnier
1926;; use `combine-after-change-calls' to minimize the slowdown of font-lock.
1927;;
1928
4f815065 1929;; arch-tag: 2571d7ff-bc28-4cf9-8585-42e21890be66
610a6418 1930;;; diff-mode.el ends here