Add info-lookup help for gdb-script-mode
[emacs.git] / lisp / vc / diff-mode.el
blob9dfcd944bbdd410c7cc9408f65fa9052bb4227d8
1 ;;; diff-mode.el --- a mode for viewing/editing context diffs -*- lexical-binding: t -*-
3 ;; Copyright (C) 1998-2017 Free Software Foundation, Inc.
5 ;; Author: Stefan Monnier <monnier@iro.umontreal.ca>
6 ;; Keywords: convenience patch diff vc
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;; Commentary:
25 ;; Provides support for font-lock, outline, navigation
26 ;; commands, editing and various conversions as well as jumping
27 ;; to the corresponding source file.
29 ;; Inspired by Pavel Machek's patch-mode.el (<pavel@@atrey.karlin.mff.cuni.cz>)
30 ;; Some efforts were spent to have it somewhat compatible with XEmacs's
31 ;; diff-mode as well as with compilation-minor-mode
33 ;; Bugs:
35 ;; - Reverse doesn't work with normal diffs.
37 ;; Todo:
39 ;; - Improve `diff-add-change-log-entries-other-window',
40 ;; it is very simplistic now.
42 ;; - Add a `delete-after-apply' so C-c C-a automatically deletes hunks.
43 ;; Also allow C-c C-a to delete already-applied hunks.
45 ;; - Try `diff <file> <hunk>' to try and fuzzily discover the source location
46 ;; of a hunk. Show then the changes between <file> and <hunk> and make it
47 ;; possible to apply them to <file>, <hunk-src>, or <hunk-dst>.
48 ;; Or maybe just make it into a ".rej to diff3-markers converter".
49 ;; Maybe just use `wiggle' (by Neil Brown) to do it for us.
51 ;; - in diff-apply-hunk, strip context in replace-match to better
52 ;; preserve markers and spacing.
53 ;; - Handle `diff -b' output in context->unified.
55 ;;; Code:
56 (eval-when-compile (require 'cl-lib))
58 (defvar add-log-buffer-file-name-function)
61 (defgroup diff-mode ()
62 "Major mode for viewing/editing diffs."
63 :version "21.1"
64 :group 'tools
65 :group 'diff)
67 (defcustom diff-default-read-only nil
68 "If non-nil, `diff-mode' buffers default to being read-only."
69 :type 'boolean
70 :group 'diff-mode)
72 (defcustom diff-jump-to-old-file nil
73 "Non-nil means `diff-goto-source' jumps to the old file.
74 Else, it jumps to the new file."
75 :type 'boolean
76 :group 'diff-mode)
78 (defcustom diff-update-on-the-fly t
79 "Non-nil means hunk headers are kept up-to-date on-the-fly.
80 When editing a diff file, the line numbers in the hunk headers
81 need to be kept consistent with the actual diff. This can
82 either be done on the fly (but this sometimes interacts poorly with the
83 undo mechanism) or whenever the file is written (can be slow
84 when editing big diffs)."
85 :type 'boolean
86 :group 'diff-mode)
88 (defcustom diff-advance-after-apply-hunk t
89 "Non-nil means `diff-apply-hunk' will move to the next hunk after applying."
90 :type 'boolean
91 :group 'diff-mode)
93 (defcustom diff-mode-hook nil
94 "Run after setting up the `diff-mode' major mode."
95 :type 'hook
96 :options '(diff-delete-empty-files diff-make-unified)
97 :group 'diff-mode)
99 (defvar diff-vc-backend nil
100 "The VC backend that created the current Diff buffer, if any.")
102 (defvar diff-outline-regexp
103 "\\([*+][*+][*+] [^0-9]\\|@@ ...\\|\\*\\*\\* [0-9].\\|--- [0-9]..\\)")
105 ;;;;
106 ;;;; keymap, menu, ...
107 ;;;;
109 (easy-mmode-defmap diff-mode-shared-map
110 '(("n" . diff-hunk-next)
111 ("N" . diff-file-next)
112 ("p" . diff-hunk-prev)
113 ("P" . diff-file-prev)
114 ("\t" . diff-hunk-next)
115 ([backtab] . diff-hunk-prev)
116 ("k" . diff-hunk-kill)
117 ("K" . diff-file-kill)
118 ("}" . diff-file-next) ; From compilation-minor-mode.
119 ("{" . diff-file-prev)
120 ("\C-m" . diff-goto-source)
121 ([mouse-2] . diff-goto-source)
122 ("W" . widen)
123 ("o" . diff-goto-source) ; other-window
124 ("A" . diff-ediff-patch)
125 ("r" . diff-restrict-view)
126 ("R" . diff-reverse-direction)
127 ([remap undo] . diff-undo))
128 "Basic keymap for `diff-mode', bound to various prefix keys."
129 :inherit special-mode-map)
131 (easy-mmode-defmap diff-mode-map
132 `(("\e" . ,(let ((map (make-sparse-keymap)))
133 ;; We want to inherit most bindings from diff-mode-shared-map,
134 ;; but not all since they may hide useful M-<foo> global
135 ;; bindings when editing.
136 (set-keymap-parent map diff-mode-shared-map)
137 (dolist (key '("A" "r" "R" "g" "q" "W" "z"))
138 (define-key map key nil))
139 map))
140 ;; From compilation-minor-mode.
141 ("\C-c\C-c" . diff-goto-source)
142 ;; By analogy with the global C-x 4 a binding.
143 ("\C-x4A" . diff-add-change-log-entries-other-window)
144 ;; Misc operations.
145 ("\C-c\C-a" . diff-apply-hunk)
146 ("\C-c\C-e" . diff-ediff-patch)
147 ("\C-c\C-n" . diff-restrict-view)
148 ("\C-c\C-s" . diff-split-hunk)
149 ("\C-c\C-t" . diff-test-hunk)
150 ("\C-c\C-r" . diff-reverse-direction)
151 ("\C-c\C-u" . diff-context->unified)
152 ;; `d' because it duplicates the context :-( --Stef
153 ("\C-c\C-d" . diff-unified->context)
154 ("\C-c\C-w" . diff-ignore-whitespace-hunk)
155 ("\C-c\C-b" . diff-refine-hunk) ;No reason for `b' :-(
156 ("\C-c\C-f" . next-error-follow-minor-mode))
157 "Keymap for `diff-mode'. See also `diff-mode-shared-map'.")
159 (easy-menu-define diff-mode-menu diff-mode-map
160 "Menu for `diff-mode'."
161 '("Diff"
162 ["Jump to Source" diff-goto-source
163 :help "Jump to the corresponding source line"]
164 ["Apply hunk" diff-apply-hunk
165 :help "Apply the current hunk to the source file and go to the next"]
166 ["Test applying hunk" diff-test-hunk
167 :help "See whether it's possible to apply the current hunk"]
168 ["Apply diff with Ediff" diff-ediff-patch
169 :help "Call `ediff-patch-file' on the current buffer"]
170 ["Create Change Log entries" diff-add-change-log-entries-other-window
171 :help "Create ChangeLog entries for the changes in the diff buffer"]
172 "-----"
173 ["Reverse direction" diff-reverse-direction
174 :help "Reverse the direction of the diffs"]
175 ["Context -> Unified" diff-context->unified
176 :help "Convert context diffs to unified diffs"]
177 ["Unified -> Context" diff-unified->context
178 :help "Convert unified diffs to context diffs"]
179 ;;["Fixup Headers" diff-fixup-modifs (not buffer-read-only)]
180 ["Remove trailing whitespace" diff-delete-trailing-whitespace
181 :help "Remove trailing whitespace problems introduced by the diff"]
182 ["Show trailing whitespace" whitespace-mode
183 :style toggle :selected (bound-and-true-p whitespace-mode)
184 :help "Show trailing whitespace in modified lines"]
185 "-----"
186 ["Split hunk" diff-split-hunk
187 :active (diff-splittable-p)
188 :help "Split the current (unified diff) hunk at point into two hunks"]
189 ["Ignore whitespace changes" diff-ignore-whitespace-hunk
190 :help "Re-diff the current hunk, ignoring whitespace differences"]
191 ["Highlight fine changes" diff-refine-hunk
192 :help "Highlight changes of hunk at point at a finer granularity"]
193 ["Kill current hunk" diff-hunk-kill
194 :help "Kill current hunk"]
195 ["Kill current file's hunks" diff-file-kill
196 :help "Kill all current file's hunks"]
197 "-----"
198 ["Previous Hunk" diff-hunk-prev
199 :help "Go to the previous count'th hunk"]
200 ["Next Hunk" diff-hunk-next
201 :help "Go to the next count'th hunk"]
202 ["Previous File" diff-file-prev
203 :help "Go to the previous count'th file"]
204 ["Next File" diff-file-next
205 :help "Go to the next count'th file"]
208 (defcustom diff-minor-mode-prefix "\C-c="
209 "Prefix key for `diff-minor-mode' commands."
210 :type '(choice (string "\e") (string "C-c=") string)
211 :group 'diff-mode)
213 (easy-mmode-defmap diff-minor-mode-map
214 `((,diff-minor-mode-prefix . ,diff-mode-shared-map))
215 "Keymap for `diff-minor-mode'. See also `diff-mode-shared-map'.")
217 (define-minor-mode diff-auto-refine-mode
218 "Toggle automatic diff hunk highlighting (Diff Auto Refine mode).
219 With a prefix argument ARG, enable Diff Auto Refine mode if ARG
220 is positive, and disable it otherwise. If called from Lisp,
221 enable the mode if ARG is omitted or nil.
223 Diff Auto Refine mode is a buffer-local minor mode used with
224 `diff-mode'. When enabled, Emacs automatically highlights
225 changes in detail as the user visits hunks. When transitioning
226 from disabled to enabled, it tries to refine the current hunk, as
227 well."
228 :group 'diff-mode :init-value t :lighter nil ;; " Auto-Refine"
229 (when diff-auto-refine-mode
230 (condition-case-unless-debug nil (diff-refine-hunk) (error nil))))
232 ;;;;
233 ;;;; font-lock support
234 ;;;;
236 (defface diff-header
237 '((((class color) (min-colors 88) (background light))
238 :background "grey80")
239 (((class color) (min-colors 88) (background dark))
240 :background "grey45")
241 (((class color))
242 :foreground "blue1" :weight bold)
243 (t :weight bold))
244 "`diff-mode' face inherited by hunk and index header faces."
245 :group 'diff-mode)
247 (defface diff-file-header
248 '((((class color) (min-colors 88) (background light))
249 :background "grey70" :weight bold)
250 (((class color) (min-colors 88) (background dark))
251 :background "grey60" :weight bold)
252 (((class color))
253 :foreground "cyan" :weight bold)
254 (t :weight bold)) ; :height 1.3
255 "`diff-mode' face used to highlight file header lines."
256 :group 'diff-mode)
258 (defface diff-index
259 '((t :inherit diff-file-header))
260 "`diff-mode' face used to highlight index header lines."
261 :group 'diff-mode)
263 (defface diff-hunk-header
264 '((t :inherit diff-header))
265 "`diff-mode' face used to highlight hunk header lines."
266 :group 'diff-mode)
268 (defface diff-removed
269 '((default
270 :inherit diff-changed)
271 (((class color) (min-colors 88) (background light))
272 :background "#ffdddd")
273 (((class color) (min-colors 88) (background dark))
274 :background "#553333")
275 (((class color))
276 :foreground "red"))
277 "`diff-mode' face used to highlight removed lines."
278 :group 'diff-mode)
280 (defface diff-added
281 '((default
282 :inherit diff-changed)
283 (((class color) (min-colors 88) (background light))
284 :background "#ddffdd")
285 (((class color) (min-colors 88) (background dark))
286 :background "#335533")
287 (((class color))
288 :foreground "green"))
289 "`diff-mode' face used to highlight added lines."
290 :group 'diff-mode)
292 (defface diff-changed
293 '((t nil))
294 "`diff-mode' face used to highlight changed lines."
295 :version "25.1"
296 :group 'diff-mode)
298 (defface diff-indicator-removed
299 '((t :inherit diff-removed))
300 "`diff-mode' face used to highlight indicator of removed lines (-, <)."
301 :group 'diff-mode
302 :version "22.1")
303 (defvar diff-indicator-removed-face 'diff-indicator-removed)
305 (defface diff-indicator-added
306 '((t :inherit diff-added))
307 "`diff-mode' face used to highlight indicator of added lines (+, >)."
308 :group 'diff-mode
309 :version "22.1")
310 (defvar diff-indicator-added-face 'diff-indicator-added)
312 (defface diff-indicator-changed
313 '((t :inherit diff-changed))
314 "`diff-mode' face used to highlight indicator of changed lines."
315 :group 'diff-mode
316 :version "22.1")
317 (defvar diff-indicator-changed-face 'diff-indicator-changed)
319 (defface diff-function
320 '((t :inherit diff-header))
321 "`diff-mode' face used to highlight function names produced by \"diff -p\"."
322 :group 'diff-mode)
324 (defface diff-context
325 '((((class color grayscale) (min-colors 88) (background light))
326 :foreground "#333333")
327 (((class color grayscale) (min-colors 88) (background dark))
328 :foreground "#dddddd"))
329 "`diff-mode' face used to highlight context and other side-information."
330 :version "25.1"
331 :group 'diff-mode)
333 (defface diff-nonexistent
334 '((t :inherit diff-file-header))
335 "`diff-mode' face used to highlight nonexistent files in recursive diffs."
336 :group 'diff-mode)
338 (defconst diff-yank-handler '(diff-yank-function))
339 (defun diff-yank-function (text)
340 ;; FIXME: the yank-handler is now called separately on each piece of text
341 ;; with a yank-handler property, so the next-single-property-change call
342 ;; below will always return nil :-( --stef
343 (let ((mixed (next-single-property-change 0 'yank-handler text))
344 (start (point)))
345 ;; First insert the text.
346 (insert text)
347 ;; If the text does not include any diff markers and if we're not
348 ;; yanking back into a diff-mode buffer, get rid of the prefixes.
349 (unless (or mixed (derived-mode-p 'diff-mode))
350 (undo-boundary) ; Just in case the user wanted the prefixes.
351 (let ((re (save-excursion
352 (if (re-search-backward "^[><!][ \t]" start t)
353 (if (eq (char-after) ?!)
354 "^[!+- ][ \t]" "^[<>][ \t]")
355 "^[ <>!+-]"))))
356 (save-excursion
357 (while (re-search-backward re start t)
358 (replace-match "" t t)))))))
360 (defconst diff-hunk-header-re-unified
361 "^@@ -\\([0-9]+\\)\\(?:,\\([0-9]+\\)\\)? \\+\\([0-9]+\\)\\(?:,\\([0-9]+\\)\\)? @@")
362 (defconst diff-context-mid-hunk-header-re
363 "--- \\([0-9]+\\)\\(?:,\\([0-9]+\\)\\)? ----$")
365 (defvar diff-use-changed-face (and (face-differs-from-default-p 'diff-changed)
366 (not (face-equal 'diff-changed 'diff-added))
367 (not (face-equal 'diff-changed 'diff-removed)))
368 "If non-nil, use the face `diff-changed' for changed lines in context diffs.
369 Otherwise, use the face `diff-removed' for removed lines,
370 and the face `diff-added' for added lines.")
372 (defvar diff-font-lock-keywords
373 `((,(concat "\\(" diff-hunk-header-re-unified "\\)\\(.*\\)$")
374 (1 'diff-hunk-header) (6 'diff-function))
375 ("^\\(\\*\\{15\\}\\)\\(.*\\)$" ;context
376 (1 'diff-hunk-header) (2 'diff-function))
377 ("^\\*\\*\\* .+ \\*\\*\\*\\*". 'diff-hunk-header) ;context
378 (,diff-context-mid-hunk-header-re . 'diff-hunk-header) ;context
379 ("^[0-9,]+[acd][0-9,]+$" . 'diff-hunk-header) ;normal
380 ("^---$" . 'diff-hunk-header) ;normal
381 ;; For file headers, accept files with spaces, but be careful to rule
382 ;; out false-positives when matching hunk headers.
383 ("^\\(---\\|\\+\\+\\+\\|\\*\\*\\*\\) \\([^\t\n]+?\\)\\(?:\t.*\\| \\(\\*\\*\\*\\*\\|----\\)\\)?\n"
384 (0 'diff-header)
385 (2 (if (not (match-end 3)) 'diff-file-header) prepend))
386 ("^\\([-<]\\)\\(.*\n\\)"
387 (1 diff-indicator-removed-face) (2 'diff-removed))
388 ("^\\([+>]\\)\\(.*\n\\)"
389 (1 diff-indicator-added-face) (2 'diff-added))
390 ("^\\(!\\)\\(.*\n\\)"
391 (1 (if diff-use-changed-face
392 diff-indicator-changed-face
393 ;; Otherwise, search for `diff-context-mid-hunk-header-re' and
394 ;; if the line of context diff is above, use `diff-removed';
395 ;; if below, use `diff-added'.
396 (save-match-data
397 (let ((limit (save-excursion (diff-beginning-of-hunk))))
398 (if (save-excursion (re-search-backward diff-context-mid-hunk-header-re limit t))
399 diff-indicator-added-face
400 diff-indicator-removed-face)))))
401 (2 (if diff-use-changed-face
402 'diff-changed
403 ;; Otherwise, use the same method as above.
404 (save-match-data
405 (let ((limit (save-excursion (diff-beginning-of-hunk))))
406 (if (save-excursion (re-search-backward diff-context-mid-hunk-header-re limit t))
407 'diff-added
408 'diff-removed))))))
409 ("^\\(?:Index\\|revno\\): \\(.+\\).*\n"
410 (0 'diff-header) (1 'diff-index prepend))
411 ("^Only in .*\n" . 'diff-nonexistent)
412 ("^\\(#\\)\\(.*\\)"
413 (1 font-lock-comment-delimiter-face)
414 (2 font-lock-comment-face))
415 ("^[^-=+*!<>#].*\n" (0 'diff-context))))
417 (defconst diff-font-lock-defaults
418 '(diff-font-lock-keywords t nil nil nil (font-lock-multiline . nil)))
420 (defvar diff-imenu-generic-expression
421 ;; Prefer second name as first is most likely to be a backup or
422 ;; version-control name. The [\t\n] at the end of the unidiff pattern
423 ;; catches Debian source diff files (which lack the trailing date).
424 '((nil "\\+\\+\\+\\ \\([^\t\n]+\\)[\t\n]" 1) ; unidiffs
425 (nil "^--- \\([^\t\n]+\\)\t.*\n\\*" 1))) ; context diffs
427 ;;;;
428 ;;;; Movement
429 ;;;;
431 (defvar diff-valid-unified-empty-line t
432 "If non-nil, empty lines are valid in unified diffs.
433 Some versions of diff replace all-blank context lines in unified format with
434 empty lines. This makes the format less robust, but is tolerated.
435 See http://lists.gnu.org/archive/html/emacs-devel/2007-11/msg01990.html")
437 (defconst diff-hunk-header-re
438 (concat "^\\(?:" diff-hunk-header-re-unified ".*\\|\\*\\{15\\}.*\n\\*\\*\\* .+ \\*\\*\\*\\*\\|[0-9]+\\(,[0-9]+\\)?[acd][0-9]+\\(,[0-9]+\\)?\\)$"))
439 (defconst diff-file-header-re (concat "^\\(--- .+\n\\+\\+\\+ \\|\\*\\*\\* .+\n--- \\|[^-+!<>0-9@* \n]\\).+\n" (substring diff-hunk-header-re 1)))
440 (defvar diff-narrowed-to nil)
442 (defun diff-hunk-style (&optional style)
443 (when (looking-at diff-hunk-header-re)
444 (setq style (cdr (assq (char-after) '((?@ . unified) (?* . context)))))
445 (goto-char (match-end 0)))
446 style)
448 (defun diff-end-of-hunk (&optional style donttrustheader)
449 "Advance to the end of the current hunk, and return its position."
450 (let (end)
451 (when (looking-at diff-hunk-header-re)
452 ;; Especially important for unified (because headers are ambiguous).
453 (setq style (diff-hunk-style style))
454 (goto-char (match-end 0))
455 (when (and (not donttrustheader) (match-end 2))
456 (let* ((nold (string-to-number (or (match-string 2) "1")))
457 (nnew (string-to-number (or (match-string 4) "1")))
458 (endold
459 (save-excursion
460 (re-search-forward (if diff-valid-unified-empty-line
461 "^[- \n]" "^[- ]")
462 nil t nold)
463 (line-beginning-position
464 ;; Skip potential "\ No newline at end of file".
465 (if (looking-at ".*\n\\\\") 3 2))))
466 (endnew
467 ;; The hunk may end with a bunch of "+" lines, so the `end' is
468 ;; then further than computed above.
469 (save-excursion
470 (re-search-forward (if diff-valid-unified-empty-line
471 "^[+ \n]" "^[+ ]")
472 nil t nnew)
473 (line-beginning-position
474 ;; Skip potential "\ No newline at end of file".
475 (if (looking-at ".*\n\\\\") 3 2)))))
476 (setq end (max endold endnew)))))
477 ;; We may have a first evaluation of `end' thanks to the hunk header.
478 (unless end
479 (setq end (and (re-search-forward
480 (pcase style
481 (`unified
482 (concat (if diff-valid-unified-empty-line
483 "^[^-+# \\\n]\\|" "^[^-+# \\]\\|")
484 ;; A `unified' header is ambiguous.
485 diff-file-header-re))
486 (`context "^[^-+#! \\]")
487 (`normal "^[^<>#\\]")
488 (_ "^[^-+#!<> \\]"))
489 nil t)
490 (match-beginning 0)))
491 (when diff-valid-unified-empty-line
492 ;; While empty lines may be valid inside hunks, they are also likely
493 ;; to be unrelated to the hunk.
494 (goto-char (or end (point-max)))
495 (while (eq ?\n (char-before (1- (point))))
496 (forward-char -1)
497 (setq end (point)))))
498 ;; The return value is used by easy-mmode-define-navigation.
499 (goto-char (or end (point-max)))))
501 (defun diff-beginning-of-hunk (&optional try-harder)
502 "Move back to the previous hunk beginning, and return its position.
503 If point is in a file header rather than a hunk, advance to the
504 next hunk if TRY-HARDER is non-nil; otherwise signal an error."
505 (beginning-of-line)
506 (if (looking-at diff-hunk-header-re)
507 (point)
508 (forward-line 1)
509 (condition-case ()
510 (re-search-backward diff-hunk-header-re)
511 (error
512 (unless try-harder
513 (error "Can't find the beginning of the hunk"))
514 (diff-beginning-of-file-and-junk)
515 (diff-hunk-next)
516 (point)))))
518 (defun diff-unified-hunk-p ()
519 (save-excursion
520 (ignore-errors
521 (diff-beginning-of-hunk)
522 (looking-at "^@@"))))
524 (defun diff-beginning-of-file ()
525 (beginning-of-line)
526 (unless (looking-at diff-file-header-re)
527 (let ((start (point))
528 res)
529 ;; diff-file-header-re may need to match up to 4 lines, so in case
530 ;; we're inside the header, we need to move up to 3 lines forward.
531 (forward-line 3)
532 (if (and (setq res (re-search-backward diff-file-header-re nil t))
533 ;; Maybe the 3 lines forward were too much and we matched
534 ;; a file header after our starting point :-(
535 (or (<= (point) start)
536 (setq res (re-search-backward diff-file-header-re nil t))))
538 (goto-char start)
539 (error "Can't find the beginning of the file")))))
542 (defun diff-end-of-file ()
543 (re-search-forward "^[-+#!<>0-9@* \\]" nil t)
544 (re-search-forward (concat "^[^-+#!<>0-9@* \\]\\|" diff-file-header-re)
545 nil 'move)
546 (if (match-beginning 1)
547 (goto-char (match-beginning 1))
548 (beginning-of-line)))
550 (defvar diff--auto-refine-data nil)
552 ;; Define diff-{hunk,file}-{prev,next}
553 (easy-mmode-define-navigation
554 diff--internal-hunk diff-hunk-header-re "hunk" diff-end-of-hunk diff-restrict-view)
556 (easy-mmode-define-navigation
557 diff--internal-file diff-file-header-re "file" diff-end-of-file)
559 (defun diff--wrap-navigation (skip-hunk-start
560 what orig
561 header-re goto-start-func count)
562 "Wrap diff-{hunk,file}-{next,prev} for more intuitive behavior.
563 Override the default diff-{hunk,file}-{next,prev} implementation
564 by skipping any lines that are associated with this hunk/file but
565 precede the hunk-start marker. For instance, a diff file could
566 contain
568 diff --git a/lisp/vc/diff-mode.el b/lisp/vc/diff-mode.el
569 index 923de9a..6b1c24f 100644
570 --- a/lisp/vc/diff-mode.el
571 +++ b/lisp/vc/diff-mode.el
572 @@ -590,6 +590,22 @@
573 .......
575 If a point is on 'index', then the point is considered to be in
576 this first hunk. Move the point to the @@... marker before
577 executing the default diff-hunk-next/prev implementation to move
578 to the NEXT marker."
579 (if (not skip-hunk-start)
580 (funcall orig count)
582 (let ((start (point)))
583 (funcall goto-start-func)
585 ;; Trap the error.
586 (condition-case nil
587 (funcall orig count)
588 (error nil))
590 (when (not (looking-at header-re))
591 (goto-char start)
592 (user-error (format "No %s" what)))
594 ;; We successfully moved to the next/prev hunk/file. Apply the
595 ;; auto-refinement if needed
596 (when diff-auto-refine-mode
597 (unless (prog1 diff--auto-refine-data
598 (setq diff--auto-refine-data
599 (cons (current-buffer) (point-marker))))
600 (run-at-time 0.0 nil
601 (lambda ()
602 (when diff--auto-refine-data
603 (let ((buffer (car diff--auto-refine-data))
604 (point (cdr diff--auto-refine-data)))
605 (setq diff--auto-refine-data nil)
606 (with-local-quit
607 (when (buffer-live-p buffer)
608 (with-current-buffer buffer
609 (save-excursion
610 (goto-char point)
611 (diff-refine-hunk))))))))))))))
613 ;; These functions all take a skip-hunk-start argument which controls
614 ;; whether we skip pre-hunk-start text or not. In interactive uses we
615 ;; always want to do this, but the simple behavior is still necessary
616 ;; to, for example, avoid an infinite loop:
618 ;; diff-hunk-next calls
619 ;; diff--wrap-navigation calls
620 ;; diff-bounds-of-hunk calls
621 ;; diff-beginning-of-hunk calls
622 ;; diff-hunk-next
624 ;; Here the outer diff-hunk-next has skip-hunk-start set to t, but the
625 ;; inner one does not, which breaks the loop.
626 (defun diff-hunk-prev (&optional count skip-hunk-start)
627 "Go to the previous COUNT'th hunk."
628 (interactive (list (prefix-numeric-value current-prefix-arg) t))
629 (diff--wrap-navigation
630 skip-hunk-start
631 "prev hunk"
632 'diff--internal-hunk-prev
633 diff-hunk-header-re
634 (lambda () (goto-char (car (diff-bounds-of-hunk))))
635 count))
637 (defun diff-hunk-next (&optional count skip-hunk-start)
638 "Go to the next COUNT'th hunk."
639 (interactive (list (prefix-numeric-value current-prefix-arg) t))
640 (diff--wrap-navigation
641 skip-hunk-start
642 "next hunk"
643 'diff--internal-hunk-next
644 diff-hunk-header-re
645 (lambda () (goto-char (car (diff-bounds-of-hunk))))
646 count))
648 (defun diff-file-prev (&optional count skip-hunk-start)
649 "Go to the previous COUNT'th file."
650 (interactive (list (prefix-numeric-value current-prefix-arg) t))
651 (diff--wrap-navigation
652 skip-hunk-start
653 "prev file"
654 'diff--internal-file-prev
655 diff-file-header-re
656 (lambda () (goto-char (car (diff-bounds-of-file))) (diff--internal-hunk-next))
657 count))
659 (defun diff-file-next (&optional count skip-hunk-start)
660 "Go to the next COUNT'th file."
661 (interactive (list (prefix-numeric-value current-prefix-arg) t))
662 (diff--wrap-navigation
663 skip-hunk-start
664 "next file"
665 'diff--internal-file-next
666 diff-file-header-re
667 (lambda () (goto-char (car (diff-bounds-of-file))) (diff--internal-hunk-next))
668 count))
673 (defun diff-bounds-of-hunk ()
674 "Return the bounds of the diff hunk at point.
675 The return value is a list (BEG END), which are the hunk's start
676 and end positions. Signal an error if no hunk is found. If
677 point is in a file header, return the bounds of the next hunk."
678 (save-excursion
679 (let ((pos (point))
680 (beg (diff-beginning-of-hunk t))
681 (end (diff-end-of-hunk)))
682 (cond ((> end pos)
683 (list beg end))
684 ;; If this hunk ends above POS, consider the next hunk.
685 ((re-search-forward diff-hunk-header-re nil t)
686 (list (match-beginning 0) (diff-end-of-hunk)))
687 ;; There's no next hunk, so just take the one we have.
688 (t (list beg end))))))
690 (defun diff-bounds-of-file ()
691 "Return the bounds of the file segment at point.
692 The return value is a list (BEG END), which are the segment's
693 start and end positions."
694 (save-excursion
695 (let ((pos (point))
696 (beg (progn (diff-beginning-of-file-and-junk)
697 (point))))
698 (diff-end-of-file)
699 ;; bzr puts a newline after the last hunk.
700 (while (looking-at "^\n")
701 (forward-char 1))
702 (if (> pos (point))
703 (error "Not inside a file diff"))
704 (list beg (point)))))
706 (defun diff-restrict-view (&optional arg)
707 "Restrict the view to the current hunk.
708 If the prefix ARG is given, restrict the view to the current file instead."
709 (interactive "P")
710 (apply 'narrow-to-region
711 (if arg (diff-bounds-of-file) (diff-bounds-of-hunk)))
712 (set (make-local-variable 'diff-narrowed-to) (if arg 'file 'hunk)))
714 (defun diff-hunk-kill ()
715 "Kill the hunk at point."
716 (interactive)
717 (let* ((hunk-bounds (diff-bounds-of-hunk))
718 (file-bounds (ignore-errors (diff-bounds-of-file)))
719 ;; If the current hunk is the only one for its file, kill the
720 ;; file header too.
721 (bounds (if (and file-bounds
722 (progn (goto-char (car file-bounds))
723 (= (progn (diff-hunk-next) (point))
724 (car hunk-bounds)))
725 (progn (goto-char (cadr hunk-bounds))
726 ;; bzr puts a newline after the last hunk.
727 (while (looking-at "^\n")
728 (forward-char 1))
729 (= (point) (cadr file-bounds))))
730 file-bounds
731 hunk-bounds))
732 (inhibit-read-only t))
733 (apply 'kill-region bounds)
734 (goto-char (car bounds))))
736 ;; "index ", "old mode", "new mode", "new file mode" and
737 ;; "deleted file mode" are output by git-diff.
738 (defconst diff-file-junk-re
739 "diff \\|index \\|\\(?:deleted file\\|new\\(?: file\\)?\\|old\\) mode\\|=== modified file")
741 (defun diff-beginning-of-file-and-junk ()
742 "Go to the beginning of file-related diff-info.
743 This is like `diff-beginning-of-file' except it tries to skip back over leading
744 data such as \"Index: ...\" and such."
745 (let* ((orig (point))
746 ;; Skip forward over what might be "leading junk" so as to get
747 ;; closer to the actual diff.
748 (_ (progn (beginning-of-line)
749 (while (looking-at diff-file-junk-re)
750 (forward-line 1))))
751 (start (point))
752 (prevfile (condition-case err
753 (save-excursion (diff-beginning-of-file) (point))
754 (error err)))
755 (err (if (consp prevfile) prevfile))
756 (nextfile (ignore-errors
757 (save-excursion
758 (goto-char start) (diff-file-next) (point))))
759 ;; prevhunk is one of the limits.
760 (prevhunk (save-excursion
761 (ignore-errors
762 (if (numberp prevfile) (goto-char prevfile))
763 (diff-hunk-prev) (point))))
764 (previndex (save-excursion
765 (forward-line 1) ;In case we're looking at "Index:".
766 (re-search-backward "^Index: " prevhunk t))))
767 ;; If we're in the junk, we should use nextfile instead of prevfile.
768 (if (and (numberp nextfile)
769 (or (not (numberp prevfile))
770 (and previndex (> previndex prevfile))))
771 (setq prevfile nextfile))
772 (if (and previndex (numberp prevfile) (< previndex prevfile))
773 (setq prevfile previndex))
774 (if (numberp prevfile)
775 (progn
776 (goto-char prevfile)
777 ;; Now skip backward over the leading junk we may have before the
778 ;; diff itself.
779 (while (save-excursion
780 (and (zerop (forward-line -1))
781 (looking-at diff-file-junk-re)))
782 (forward-line -1)))
783 ;; File starts *after* the starting point: we really weren't in
784 ;; a file diff but elsewhere.
785 (goto-char orig)
786 (signal (car err) (cdr err)))))
788 (defun diff-file-kill ()
789 "Kill current file's hunks."
790 (interactive)
791 (let ((inhibit-read-only t))
792 (apply 'kill-region (diff-bounds-of-file))))
794 (defun diff-kill-junk ()
795 "Kill spurious empty diffs."
796 (interactive)
797 (save-excursion
798 (let ((inhibit-read-only t))
799 (goto-char (point-min))
800 (while (re-search-forward (concat "^\\(Index: .*\n\\)"
801 "\\([^-+!* <>].*\n\\)*?"
802 "\\(\\(Index:\\) \\|"
803 diff-file-header-re "\\)")
804 nil t)
805 (delete-region (if (match-end 4) (match-beginning 0) (match-end 1))
806 (match-beginning 3))
807 (beginning-of-line)))))
809 (defun diff-count-matches (re start end)
810 (save-excursion
811 (let ((n 0))
812 (goto-char start)
813 (while (re-search-forward re end t) (cl-incf n))
814 n)))
816 (defun diff-splittable-p ()
817 (save-excursion
818 (beginning-of-line)
819 (and (looking-at "^[-+ ]")
820 (progn (forward-line -1) (looking-at "^[-+ ]"))
821 (diff-unified-hunk-p))))
823 (defun diff-split-hunk ()
824 "Split the current (unified diff) hunk at point into two hunks."
825 (interactive)
826 (beginning-of-line)
827 (let ((pos (point))
828 (start (diff-beginning-of-hunk)))
829 (unless (looking-at diff-hunk-header-re-unified)
830 (error "diff-split-hunk only works on unified context diffs"))
831 (forward-line 1)
832 (let* ((start1 (string-to-number (match-string 1)))
833 (start2 (string-to-number (match-string 3)))
834 (newstart1 (+ start1 (diff-count-matches "^[- \t]" (point) pos)))
835 (newstart2 (+ start2 (diff-count-matches "^[+ \t]" (point) pos)))
836 (inhibit-read-only t))
837 (goto-char pos)
838 ;; Hopefully the after-change-function will not screw us over.
839 (insert "@@ -" (number-to-string newstart1) ",1 +"
840 (number-to-string newstart2) ",1 @@\n")
841 ;; Fix the original hunk-header.
842 (diff-fixup-modifs start pos))))
845 ;;;;
846 ;;;; jump to other buffers
847 ;;;;
849 (defvar diff-remembered-files-alist nil)
850 (defvar diff-remembered-defdir nil)
852 (defun diff-filename-drop-dir (file)
853 (when (string-match "/" file) (substring file (match-end 0))))
855 (defun diff-merge-strings (ancestor from to)
856 "Merge the diff between ANCESTOR and FROM into TO.
857 Returns the merged string if successful or nil otherwise.
858 The strings are assumed not to contain any \"\\n\" (i.e. end of line).
859 If ANCESTOR = FROM, returns TO.
860 If ANCESTOR = TO, returns FROM.
861 The heuristic is simplistic and only really works for cases
862 like \(diff-merge-strings \"b/foo\" \"b/bar\" \"/a/c/foo\")."
863 ;; Ideally, we want:
864 ;; AMB ANB CMD -> CND
865 ;; but that's ambiguous if `foo' or `bar' is empty:
866 ;; a/foo a/foo1 b/foo.c -> b/foo1.c but not 1b/foo.c or b/foo.c1
867 (let ((str (concat ancestor "\n" from "\n" to)))
868 (when (and (string-match (concat
869 "\\`\\(.*?\\)\\(.*\\)\\(.*\\)\n"
870 "\\1\\(.*\\)\\3\n"
871 "\\(.*\\(\\2\\).*\\)\\'") str)
872 (equal to (match-string 5 str)))
873 (concat (substring str (match-beginning 5) (match-beginning 6))
874 (match-string 4 str)
875 (substring str (match-end 6) (match-end 5))))))
877 (defun diff-tell-file-name (old name)
878 "Tell Emacs where the find the source file of the current hunk.
879 If the OLD prefix arg is passed, tell the file NAME of the old file."
880 (interactive
881 (let* ((old current-prefix-arg)
882 (fs (diff-hunk-file-names current-prefix-arg)))
883 (unless fs (error "No file name to look for"))
884 (list old (read-file-name (format "File for %s: " (car fs))
885 nil (diff-find-file-name old 'noprompt) t))))
886 (let ((fs (diff-hunk-file-names old)))
887 (unless fs (error "No file name to look for"))
888 (push (cons fs name) diff-remembered-files-alist)))
890 (defun diff-hunk-file-names (&optional old)
891 "Give the list of file names textually mentioned for the current hunk."
892 (save-excursion
893 (unless (looking-at diff-file-header-re)
894 (or (ignore-errors (diff-beginning-of-file))
895 (re-search-forward diff-file-header-re nil t)))
896 (let ((limit (save-excursion
897 (condition-case ()
898 (progn (diff-hunk-prev) (point))
899 (error (point-min)))))
900 (header-files
901 ;; handle filenames with spaces;
902 ;; cf. diff-font-lock-keywords / diff-file-header
903 (if (looking-at "[-*][-*][-*] \\([^\t\n]+\\).*\n[-+][-+][-+] \\([^\t\n]+\\)")
904 (list (if old (match-string 1) (match-string 2))
905 (if old (match-string 2) (match-string 1)))
906 (forward-line 1) nil)))
907 (delq nil
908 (append
909 (when (and (not old)
910 (save-excursion
911 (re-search-backward "^Index: \\(.+\\)" limit t)))
912 (list (match-string 1)))
913 header-files
914 ;; this assumes that there are no spaces in filenames
915 (when (re-search-backward
916 "^diff \\(-\\S-+ +\\)*\\(\\S-+\\)\\( +\\(\\S-+\\)\\)?"
917 nil t)
918 (list (if old (match-string 2) (match-string 4))
919 (if old (match-string 4) (match-string 2)))))))))
921 (defun diff-find-file-name (&optional old noprompt prefix)
922 "Return the file corresponding to the current patch.
923 Non-nil OLD means that we want the old file.
924 Non-nil NOPROMPT means to prefer returning nil than to prompt the user.
925 PREFIX is only used internally: don't use it."
926 (unless (equal diff-remembered-defdir default-directory)
927 ;; Flush diff-remembered-files-alist if the default-directory is changed.
928 (set (make-local-variable 'diff-remembered-defdir) default-directory)
929 (set (make-local-variable 'diff-remembered-files-alist) nil))
930 (save-excursion
931 (unless (looking-at diff-file-header-re)
932 (or (ignore-errors (diff-beginning-of-file))
933 (re-search-forward diff-file-header-re nil t)))
934 (let ((fs (diff-hunk-file-names old)))
935 (if prefix (setq fs (mapcar (lambda (f) (concat prefix f)) fs)))
937 ;; use any previously used preference
938 (cdr (assoc fs diff-remembered-files-alist))
939 ;; try to be clever and use previous choices as an inspiration
940 (cl-dolist (rf diff-remembered-files-alist)
941 (let ((newfile (diff-merge-strings (caar rf) (car fs) (cdr rf))))
942 (if (and newfile (file-exists-p newfile)) (cl-return newfile))))
943 ;; look for each file in turn. If none found, try again but
944 ;; ignoring the first level of directory, ...
945 (cl-do* ((files fs (delq nil (mapcar 'diff-filename-drop-dir files)))
946 (file nil nil))
947 ((or (null files)
948 (setq file (cl-do* ((files files (cdr files))
949 (file (car files) (car files)))
950 ;; Use file-regular-p to avoid
951 ;; /dev/null, directories, etc.
952 ((or (null file) (file-regular-p file))
953 file))))
954 file))
955 ;; <foo>.rej patches implicitly apply to <foo>
956 (and (string-match "\\.rej\\'" (or buffer-file-name ""))
957 (let ((file (substring buffer-file-name 0 (match-beginning 0))))
958 (when (file-exists-p file) file)))
959 ;; If we haven't found the file, maybe it's because we haven't paid
960 ;; attention to the PCL-CVS hint.
961 (and (not prefix)
962 (boundp 'cvs-pcl-cvs-dirchange-re)
963 (save-excursion
964 (re-search-backward cvs-pcl-cvs-dirchange-re nil t))
965 (diff-find-file-name old noprompt (match-string 1)))
966 ;; if all else fails, ask the user
967 (unless noprompt
968 (let ((file (expand-file-name (or (car fs) ""))))
969 (setq file
970 (read-file-name (format "Use file %s: " file)
971 (file-name-directory file) file t
972 (file-name-nondirectory file)))
973 (set (make-local-variable 'diff-remembered-files-alist)
974 (cons (cons fs file) diff-remembered-files-alist))
975 file))))))
978 (defun diff-ediff-patch ()
979 "Call `ediff-patch-file' on the current buffer."
980 (interactive)
981 (condition-case nil
982 (ediff-patch-file nil (current-buffer))
983 (wrong-number-of-arguments (ediff-patch-file))))
985 ;;;;
986 ;;;; Conversion functions
987 ;;;;
989 ;;(defvar diff-inhibit-after-change nil
990 ;; "Non-nil means inhibit `diff-mode's after-change functions.")
992 (defun diff-unified->context (start end)
993 "Convert unified diffs to context diffs.
994 START and END are either taken from the region (if a prefix arg is given) or
995 else cover the whole buffer."
996 (interactive (if (or current-prefix-arg (use-region-p))
997 (list (region-beginning) (region-end))
998 (list (point-min) (point-max))))
999 (unless (markerp end) (setq end (copy-marker end t)))
1000 (let (;;(diff-inhibit-after-change t)
1001 (inhibit-read-only t))
1002 (save-excursion
1003 (goto-char start)
1004 (while (and (re-search-forward
1005 (concat "^\\(\\(---\\) .+\n\\(\\+\\+\\+\\) .+\\|"
1006 diff-hunk-header-re-unified ".*\\)$")
1007 nil t)
1008 (< (point) end))
1009 (combine-after-change-calls
1010 (if (match-beginning 2)
1011 ;; we matched a file header
1012 (progn
1013 ;; use reverse order to make sure the indices are kept valid
1014 (replace-match "---" t t nil 3)
1015 (replace-match "***" t t nil 2))
1016 ;; we matched a hunk header
1017 (let ((line1 (match-string 4))
1018 (lines1 (or (match-string 5) "1"))
1019 (line2 (match-string 6))
1020 (lines2 (or (match-string 7) "1"))
1021 ;; Variables to use the special undo function.
1022 (old-undo buffer-undo-list)
1023 (old-end (marker-position end))
1024 (start (match-beginning 0))
1025 (reversible t))
1026 (replace-match
1027 (concat "***************\n*** " line1 ","
1028 (number-to-string (+ (string-to-number line1)
1029 (string-to-number lines1)
1030 -1))
1031 " ****"))
1032 (save-restriction
1033 (narrow-to-region (line-beginning-position 2)
1034 ;; Call diff-end-of-hunk from just before
1035 ;; the hunk header so it can use the hunk
1036 ;; header info.
1037 (progn (diff-end-of-hunk 'unified) (point)))
1038 (let ((hunk (buffer-string)))
1039 (goto-char (point-min))
1040 (if (not (save-excursion (re-search-forward "^-" nil t)))
1041 (delete-region (point) (point-max))
1042 (goto-char (point-max))
1043 (let ((modif nil) last-pt)
1044 (while (progn (setq last-pt (point))
1045 (= (forward-line -1) 0))
1046 (pcase (char-after)
1047 (?\s (insert " ") (setq modif nil) (backward-char 1))
1048 (?+ (delete-region (point) last-pt) (setq modif t))
1049 (?- (if (not modif)
1050 (progn (forward-char 1)
1051 (insert " "))
1052 (delete-char 1)
1053 (insert "! "))
1054 (backward-char 2))
1055 (?\\ (when (save-excursion (forward-line -1)
1056 (= (char-after) ?+))
1057 (delete-region (point) last-pt)
1058 (setq modif t)))
1059 ;; diff-valid-unified-empty-line.
1060 (?\n (insert " ") (setq modif nil)
1061 (backward-char 2))
1062 (_ (setq modif nil))))))
1063 (goto-char (point-max))
1064 (save-excursion
1065 (insert "--- " line2 ","
1066 (number-to-string (+ (string-to-number line2)
1067 (string-to-number lines2)
1068 -1))
1069 " ----\n" hunk))
1070 ;;(goto-char (point-min))
1071 (forward-line 1)
1072 (if (not (save-excursion (re-search-forward "^+" nil t)))
1073 (delete-region (point) (point-max))
1074 (let ((modif nil) (delete nil))
1075 (if (save-excursion (re-search-forward "^\\+.*\n-"
1076 nil t))
1077 ;; Normally, lines in a substitution come with
1078 ;; first the removals and then the additions, and
1079 ;; the context->unified function follows this
1080 ;; convention, of course. Yet, other alternatives
1081 ;; are valid as well, but they preclude the use of
1082 ;; context->unified as an undo command.
1083 (setq reversible nil))
1084 (while (not (eobp))
1085 (pcase (char-after)
1086 (?\s (insert " ") (setq modif nil) (backward-char 1))
1087 (?- (setq delete t) (setq modif t))
1088 (?+ (if (not modif)
1089 (progn (forward-char 1)
1090 (insert " "))
1091 (delete-char 1)
1092 (insert "! "))
1093 (backward-char 2))
1094 (?\\ (when (save-excursion (forward-line 1)
1095 (not (eobp)))
1096 (setq delete t) (setq modif t)))
1097 ;; diff-valid-unified-empty-line.
1098 (?\n (insert " ") (setq modif nil) (backward-char 2)
1099 (setq reversible nil))
1100 (_ (setq modif nil)))
1101 (let ((last-pt (point)))
1102 (forward-line 1)
1103 (when delete
1104 (delete-region last-pt (point))
1105 (setq delete nil)))))))
1106 (unless (or (not reversible) (eq buffer-undo-list t))
1107 ;; Drop the many undo entries and replace them with
1108 ;; a single entry that uses diff-context->unified to do
1109 ;; the work.
1110 (setq buffer-undo-list
1111 (cons (list 'apply (- old-end end) start (point-max)
1112 'diff-context->unified start (point-max))
1113 old-undo)))))))))))
1115 (defun diff-context->unified (start end &optional to-context)
1116 "Convert context diffs to unified diffs.
1117 START and END are either taken from the region
1118 \(when it is highlighted) or else cover the whole buffer.
1119 With a prefix argument, convert unified format to context format."
1120 (interactive (if (use-region-p)
1121 (list (region-beginning) (region-end) current-prefix-arg)
1122 (list (point-min) (point-max) current-prefix-arg)))
1123 (if to-context
1124 (diff-unified->context start end)
1125 (unless (markerp end) (setq end (copy-marker end t)))
1126 (let ( ;;(diff-inhibit-after-change t)
1127 (inhibit-read-only t))
1128 (save-excursion
1129 (goto-char start)
1130 (while (and (re-search-forward "^\\(\\(\\*\\*\\*\\) .+\n\\(---\\) .+\\|\\*\\{15\\}.*\n\\*\\*\\* \\([0-9]+\\),\\(-?[0-9]+\\) \\*\\*\\*\\*\\)\\(?: \\(.*\\)\\|$\\)" nil t)
1131 (< (point) end))
1132 (combine-after-change-calls
1133 (if (match-beginning 2)
1134 ;; we matched a file header
1135 (progn
1136 ;; use reverse order to make sure the indices are kept valid
1137 (replace-match "+++" t t nil 3)
1138 (replace-match "---" t t nil 2))
1139 ;; we matched a hunk header
1140 (let ((line1s (match-string 4))
1141 (line1e (match-string 5))
1142 (pt1 (match-beginning 0))
1143 ;; Variables to use the special undo function.
1144 (old-undo buffer-undo-list)
1145 (old-end (marker-position end))
1146 ;; We currently throw away the comment that can follow
1147 ;; the hunk header. FIXME: Preserve it instead!
1148 (reversible (not (match-end 6))))
1149 (replace-match "")
1150 (unless (re-search-forward
1151 diff-context-mid-hunk-header-re nil t)
1152 (error "Can't find matching `--- n1,n2 ----' line"))
1153 (let ((line2s (match-string 1))
1154 (line2e (match-string 2))
1155 (pt2 (progn
1156 (delete-region (progn (beginning-of-line) (point))
1157 (progn (forward-line 1) (point)))
1158 (point-marker))))
1159 (goto-char pt1)
1160 (forward-line 1)
1161 (while (< (point) pt2)
1162 (pcase (char-after)
1163 (?! (delete-char 2) (insert "-") (forward-line 1))
1164 (?- (forward-char 1) (delete-char 1) (forward-line 1))
1165 (?\s ;merge with the other half of the chunk
1166 (let* ((endline2
1167 (save-excursion
1168 (goto-char pt2) (forward-line 1) (point))))
1169 (pcase (char-after pt2)
1170 ((or ?! ?+)
1171 (insert "+"
1172 (prog1
1173 (buffer-substring (+ pt2 2) endline2)
1174 (delete-region pt2 endline2))))
1175 (?\s
1176 (unless (= (- endline2 pt2)
1177 (- (line-beginning-position 2) (point)))
1178 ;; If the two lines we're merging don't have the
1179 ;; same length (can happen with "diff -b"), then
1180 ;; diff-unified->context will not properly undo
1181 ;; this operation.
1182 (setq reversible nil))
1183 (delete-region pt2 endline2)
1184 (delete-char 1)
1185 (forward-line 1))
1186 (?\\ (forward-line 1))
1187 (_ (setq reversible nil)
1188 (delete-char 1) (forward-line 1)))))
1189 (_ (setq reversible nil) (forward-line 1))))
1190 (while (looking-at "[+! ] ")
1191 (if (/= (char-after) ?!) (forward-char 1)
1192 (delete-char 1) (insert "+"))
1193 (delete-char 1) (forward-line 1))
1194 (save-excursion
1195 (goto-char pt1)
1196 (insert "@@ -" line1s ","
1197 (number-to-string (- (string-to-number line1e)
1198 (string-to-number line1s)
1199 -1))
1200 " +" line2s ","
1201 (number-to-string (- (string-to-number line2e)
1202 (string-to-number line2s)
1203 -1)) " @@"))
1204 (set-marker pt2 nil)
1205 ;; The whole procedure succeeded, let's replace the myriad
1206 ;; of undo elements with just a single special one.
1207 (unless (or (not reversible) (eq buffer-undo-list t))
1208 (setq buffer-undo-list
1209 (cons (list 'apply (- old-end end) pt1 (point)
1210 'diff-unified->context pt1 (point))
1211 old-undo)))
1212 )))))))))
1214 (defun diff-reverse-direction (start end)
1215 "Reverse the direction of the diffs.
1216 START and END are either taken from the region (if a prefix arg is given) or
1217 else cover the whole buffer."
1218 (interactive (if (or current-prefix-arg (use-region-p))
1219 (list (region-beginning) (region-end))
1220 (list (point-min) (point-max))))
1221 (unless (markerp end) (setq end (copy-marker end t)))
1222 (let (;;(diff-inhibit-after-change t)
1223 (inhibit-read-only t))
1224 (save-excursion
1225 (goto-char start)
1226 (while (and (re-search-forward "^\\(\\([-*][-*][-*] \\)\\(.+\\)\n\\([-+][-+][-+] \\)\\(.+\\)\\|\\*\\{15\\}.*\n\\*\\*\\* \\(.+\\) \\*\\*\\*\\*\\|@@ -\\([0-9,]+\\) \\+\\([0-9,]+\\) @@.*\\)$" nil t)
1227 (< (point) end))
1228 (combine-after-change-calls
1229 (cond
1230 ;; a file header
1231 ((match-beginning 2) (replace-match "\\2\\5\n\\4\\3" nil))
1232 ;; a context-diff hunk header
1233 ((match-beginning 6)
1234 (let ((pt-lines1 (match-beginning 6))
1235 (lines1 (match-string 6)))
1236 (replace-match "" nil nil nil 6)
1237 (forward-line 1)
1238 (let ((half1s (point)))
1239 (while (looking-at "[-! \\][ \t]\\|#")
1240 (when (= (char-after) ?-) (delete-char 1) (insert "+"))
1241 (forward-line 1))
1242 (let ((half1 (delete-and-extract-region half1s (point))))
1243 (unless (looking-at diff-context-mid-hunk-header-re)
1244 (insert half1)
1245 (error "Can't find matching `--- n1,n2 ----' line"))
1246 (let* ((str1end (or (match-end 2) (match-end 1)))
1247 (str1 (buffer-substring (match-beginning 1) str1end)))
1248 (goto-char str1end)
1249 (insert lines1)
1250 (delete-region (match-beginning 1) str1end)
1251 (forward-line 1)
1252 (let ((half2s (point)))
1253 (while (looking-at "[!+ \\][ \t]\\|#")
1254 (when (= (char-after) ?+) (delete-char 1) (insert "-"))
1255 (forward-line 1))
1256 (let ((half2 (delete-and-extract-region half2s (point))))
1257 (insert (or half1 ""))
1258 (goto-char half1s)
1259 (insert (or half2 ""))))
1260 (goto-char pt-lines1)
1261 (insert str1))))))
1262 ;; a unified-diff hunk header
1263 ((match-beginning 7)
1264 (replace-match "@@ -\\8 +\\7 @@" nil)
1265 (forward-line 1)
1266 (let ((c (char-after)) first last)
1267 (while (pcase (setq c (char-after))
1268 (?- (setq first (or first (point)))
1269 (delete-char 1) (insert "+") t)
1270 (?+ (setq last (or last (point)))
1271 (delete-char 1) (insert "-") t)
1272 ((or ?\\ ?#) t)
1273 (_ (when (and first last (< first last))
1274 (insert (delete-and-extract-region first last)))
1275 (setq first nil last nil)
1276 (memq c (if diff-valid-unified-empty-line
1277 '(?\s ?\n) '(?\s)))))
1278 (forward-line 1))))))))))
1280 (defun diff-fixup-modifs (start end)
1281 "Fixup the hunk headers (in case the buffer was modified).
1282 START and END are either taken from the region (if a prefix arg is given) or
1283 else cover the whole buffer."
1284 (interactive (if (or current-prefix-arg (use-region-p))
1285 (list (region-beginning) (region-end))
1286 (list (point-min) (point-max))))
1287 (let ((inhibit-read-only t))
1288 (save-excursion
1289 (goto-char end) (diff-end-of-hunk nil 'donttrustheader)
1290 (let ((plus 0) (minus 0) (space 0) (bang 0))
1291 (while (and (= (forward-line -1) 0) (<= start (point)))
1292 (if (not (looking-at
1293 (concat diff-hunk-header-re-unified
1294 "\\|[-*][-*][-*] [0-9,]+ [-*][-*][-*][-*]$"
1295 "\\|--- .+\n\\+\\+\\+ ")))
1296 (pcase (char-after)
1297 (?\s (cl-incf space))
1298 (?+ (cl-incf plus))
1299 (?- (cl-incf minus))
1300 (?! (cl-incf bang))
1301 ((or ?\\ ?#) nil)
1302 (?\n (if diff-valid-unified-empty-line
1303 (cl-incf space)
1304 (setq space 0 plus 0 minus 0 bang 0)))
1305 (_ (setq space 0 plus 0 minus 0 bang 0)))
1306 (cond
1307 ((looking-at diff-hunk-header-re-unified)
1308 (let* ((old1 (match-string 2))
1309 (old2 (match-string 4))
1310 (new1 (number-to-string (+ space minus)))
1311 (new2 (number-to-string (+ space plus))))
1312 (if old2
1313 (unless (string= new2 old2) (replace-match new2 t t nil 4))
1314 (goto-char (match-end 3))
1315 (insert "," new2))
1316 (if old1
1317 (unless (string= new1 old1) (replace-match new1 t t nil 2))
1318 (goto-char (match-end 1))
1319 (insert "," new1))))
1320 ((looking-at diff-context-mid-hunk-header-re)
1321 (when (> (+ space bang plus) 0)
1322 (let* ((old1 (match-string 1))
1323 (old2 (match-string 2))
1324 (new (number-to-string
1325 (+ space bang plus -1 (string-to-number old1)))))
1326 (unless (string= new old2) (replace-match new t t nil 2)))))
1327 ((looking-at "\\*\\*\\* \\([0-9]+\\),\\(-?[0-9]*\\) \\*\\*\\*\\*$")
1328 (when (> (+ space bang minus) 0)
1329 (let* ((old (match-string 1))
1330 (new (format
1331 (concat "%0" (number-to-string (length old)) "d")
1332 (+ space bang minus -1 (string-to-number old)))))
1333 (unless (string= new old) (replace-match new t t nil 2))))))
1334 (setq space 0 plus 0 minus 0 bang 0)))))))
1336 ;;;;
1337 ;;;; Hooks
1338 ;;;;
1340 (defun diff-write-contents-hooks ()
1341 "Fixup hunk headers if necessary."
1342 (if (buffer-modified-p) (diff-fixup-modifs (point-min) (point-max)))
1343 nil)
1345 ;; It turns out that making changes in the buffer from within an
1346 ;; *-change-function is asking for trouble, whereas making them
1347 ;; from a post-command-hook doesn't pose much problems
1348 (defvar diff-unhandled-changes nil)
1349 (defun diff-after-change-function (beg end _len)
1350 "Remember to fixup the hunk header.
1351 See `after-change-functions' for the meaning of BEG, END and LEN."
1352 ;; Ignoring changes when inhibit-read-only is set is strictly speaking
1353 ;; incorrect, but it turns out that inhibit-read-only is normally not set
1354 ;; inside editing commands, while it tends to be set when the buffer gets
1355 ;; updated by an async process or by a conversion function, both of which
1356 ;; would rather not be uselessly slowed down by this hook.
1357 (when (and (not undo-in-progress) (not inhibit-read-only))
1358 (if diff-unhandled-changes
1359 (setq diff-unhandled-changes
1360 (cons (min beg (car diff-unhandled-changes))
1361 (max end (cdr diff-unhandled-changes))))
1362 (setq diff-unhandled-changes (cons beg end)))))
1364 (defun diff-post-command-hook ()
1365 "Fixup hunk headers if necessary."
1366 (when (consp diff-unhandled-changes)
1367 (ignore-errors
1368 (save-excursion
1369 (goto-char (car diff-unhandled-changes))
1370 ;; Maybe we've cut the end of the hunk before point.
1371 (if (and (bolp) (not (bobp))) (backward-char 1))
1372 ;; We used to fixup modifs on all the changes, but it turns out that
1373 ;; it's safer not to do it on big changes, e.g. when yanking a big
1374 ;; diff, or when the user edits the header, since we might then
1375 ;; screw up perfectly correct values. --Stef
1376 (diff-beginning-of-hunk)
1377 (let* ((style (if (looking-at "\\*\\*\\*") 'context))
1378 (start (line-beginning-position (if (eq style 'context) 3 2)))
1379 (mid (if (eq style 'context)
1380 (save-excursion
1381 (re-search-forward diff-context-mid-hunk-header-re
1382 nil t)))))
1383 (when (and ;; Don't try to fixup changes in the hunk header.
1384 (>= (car diff-unhandled-changes) start)
1385 ;; Don't try to fixup changes in the mid-hunk header either.
1386 (or (not mid)
1387 (< (cdr diff-unhandled-changes) (match-beginning 0))
1388 (> (car diff-unhandled-changes) (match-end 0)))
1389 (save-excursion
1390 (diff-end-of-hunk nil 'donttrustheader)
1391 ;; Don't try to fixup changes past the end of the hunk.
1392 (>= (point) (cdr diff-unhandled-changes))))
1393 (diff-fixup-modifs (point) (cdr diff-unhandled-changes)))))
1394 (setq diff-unhandled-changes nil))))
1396 (defun diff-next-error (arg reset)
1397 ;; Select a window that displays the current buffer so that point
1398 ;; movements are reflected in that window. Otherwise, the user might
1399 ;; never see the hunk corresponding to the source she's jumping to.
1400 (pop-to-buffer (current-buffer))
1401 (if reset (goto-char (point-min)))
1402 (diff-hunk-next arg)
1403 (diff-goto-source))
1405 (defvar whitespace-style)
1406 (defvar whitespace-trailing-regexp)
1408 ;;;###autoload
1409 (define-derived-mode diff-mode fundamental-mode "Diff"
1410 "Major mode for viewing/editing context diffs.
1411 Supports unified and context diffs as well as (to a lesser extent)
1412 normal diffs.
1414 When the buffer is read-only, the ESC prefix is not necessary.
1415 If you edit the buffer manually, diff-mode will try to update the hunk
1416 headers for you on-the-fly.
1418 You can also switch between context diff and unified diff with \\[diff-context->unified],
1419 or vice versa with \\[diff-unified->context] and you can also reverse the direction of
1420 a diff with \\[diff-reverse-direction].
1422 \\{diff-mode-map}"
1424 (set (make-local-variable 'font-lock-defaults) diff-font-lock-defaults)
1425 (add-hook 'font-lock-mode-hook
1426 (lambda () (remove-overlays nil nil 'diff-mode 'fine))
1427 nil 'local)
1428 (set (make-local-variable 'outline-regexp) diff-outline-regexp)
1429 (set (make-local-variable 'imenu-generic-expression)
1430 diff-imenu-generic-expression)
1431 ;; These are not perfect. They would be better done separately for
1432 ;; context diffs and unidiffs.
1433 ;; (set (make-local-variable 'paragraph-start)
1434 ;; (concat "@@ " ; unidiff hunk
1435 ;; "\\|\\*\\*\\* " ; context diff hunk or file start
1436 ;; "\\|--- [^\t]+\t")) ; context or unidiff file
1437 ;; ; start (first or second line)
1438 ;; (set (make-local-variable 'paragraph-separate) paragraph-start)
1439 ;; (set (make-local-variable 'page-delimiter) "--- [^\t]+\t")
1440 ;; compile support
1441 (set (make-local-variable 'next-error-function) 'diff-next-error)
1443 (set (make-local-variable 'beginning-of-defun-function)
1444 'diff-beginning-of-file-and-junk)
1445 (set (make-local-variable 'end-of-defun-function)
1446 'diff-end-of-file)
1448 (diff-setup-whitespace)
1450 (if diff-default-read-only
1451 (setq buffer-read-only t))
1452 ;; setup change hooks
1453 (if (not diff-update-on-the-fly)
1454 (add-hook 'write-contents-functions 'diff-write-contents-hooks nil t)
1455 (make-local-variable 'diff-unhandled-changes)
1456 (add-hook 'after-change-functions 'diff-after-change-function nil t)
1457 (add-hook 'post-command-hook 'diff-post-command-hook nil t))
1458 ;; Neat trick from Dave Love to add more bindings in read-only mode:
1459 (let ((ro-bind (cons 'buffer-read-only diff-mode-shared-map)))
1460 (add-to-list 'minor-mode-overriding-map-alist ro-bind)
1461 ;; Turn off this little trick in case the buffer is put in view-mode.
1462 (add-hook 'view-mode-hook
1463 (lambda ()
1464 (setq minor-mode-overriding-map-alist
1465 (delq ro-bind minor-mode-overriding-map-alist)))
1466 nil t))
1467 ;; add-log support
1468 (set (make-local-variable 'add-log-current-defun-function)
1469 'diff-current-defun)
1470 (set (make-local-variable 'add-log-buffer-file-name-function)
1471 (lambda () (diff-find-file-name nil 'noprompt)))
1472 (unless (buffer-file-name)
1473 (hack-dir-local-variables-non-file-buffer)))
1475 ;;;###autoload
1476 (define-minor-mode diff-minor-mode
1477 "Toggle Diff minor mode.
1478 With a prefix argument ARG, enable Diff minor mode if ARG is
1479 positive, and disable it otherwise. If called from Lisp, enable
1480 the mode if ARG is omitted or nil.
1482 \\{diff-minor-mode-map}"
1483 :group 'diff-mode :lighter " Diff"
1484 ;; FIXME: setup font-lock
1485 ;; setup change hooks
1486 (if (not diff-update-on-the-fly)
1487 (add-hook 'write-contents-functions 'diff-write-contents-hooks nil t)
1488 (make-local-variable 'diff-unhandled-changes)
1489 (add-hook 'after-change-functions 'diff-after-change-function nil t)
1490 (add-hook 'post-command-hook 'diff-post-command-hook nil t)))
1492 ;;; Handy hook functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1494 (defun diff-setup-whitespace ()
1495 "Set up Whitespace mode variables for the current Diff mode buffer.
1496 This sets `whitespace-style' and `whitespace-trailing-regexp' so
1497 that Whitespace mode shows trailing whitespace problems on the
1498 modified lines of the diff."
1499 (set (make-local-variable 'whitespace-style) '(face trailing))
1500 (let ((style (save-excursion
1501 (goto-char (point-min))
1502 ;; FIXME: For buffers filled from async processes, this search
1503 ;; will simply fail because the buffer is still empty :-(
1504 (when (re-search-forward diff-hunk-header-re nil t)
1505 (goto-char (match-beginning 0))
1506 (diff-hunk-style)))))
1507 (set (make-local-variable 'whitespace-trailing-regexp)
1508 (if (eq style 'context)
1509 "^[-+!] .*?\\([\t ]+\\)$"
1510 "^[-+!<>].*?\\([\t ]+\\)$"))))
1512 (defun diff-delete-if-empty ()
1513 ;; An empty diff file means there's no more diffs to integrate, so we
1514 ;; can just remove the file altogether. Very handy for .rej files if we
1515 ;; remove hunks as we apply them.
1516 (when (and buffer-file-name
1517 (eq 0 (nth 7 (file-attributes buffer-file-name))))
1518 (delete-file buffer-file-name)))
1520 (defun diff-delete-empty-files ()
1521 "Arrange for empty diff files to be removed."
1522 (add-hook 'after-save-hook 'diff-delete-if-empty nil t))
1524 (defun diff-make-unified ()
1525 "Turn context diffs into unified diffs if applicable."
1526 (if (save-excursion
1527 (goto-char (point-min))
1528 (and (looking-at diff-hunk-header-re) (eq (char-after) ?*)))
1529 (let ((mod (buffer-modified-p)))
1530 (unwind-protect
1531 (diff-context->unified (point-min) (point-max))
1532 (restore-buffer-modified-p mod)))))
1535 ;;; Misc operations that have proved useful at some point.
1538 (defun diff-next-complex-hunk ()
1539 "Jump to the next \"complex\" hunk.
1540 \"Complex\" is approximated by \"the hunk changes the number of lines\".
1541 Only works for unified diffs."
1542 (interactive)
1543 (while
1544 (and (re-search-forward diff-hunk-header-re-unified nil t)
1545 (equal (match-string 2) (match-string 4)))))
1547 (defun diff-sanity-check-context-hunk-half (lines)
1548 (let ((count lines))
1549 (while
1550 (cond
1551 ((and (memq (char-after) '(?\s ?! ?+ ?-))
1552 (memq (char-after (1+ (point))) '(?\s ?\t)))
1553 (cl-decf count) t)
1554 ((or (zerop count) (= count lines)) nil)
1555 ((memq (char-after) '(?! ?+ ?-))
1556 (if (not (and (eq (char-after (1+ (point))) ?\n)
1557 (y-or-n-p "Try to auto-fix whitespace loss damage? ")))
1558 (error "End of hunk ambiguously marked")
1559 (forward-char 1) (insert " ") (forward-line -1) t))
1560 ((< lines 0)
1561 (error "End of hunk ambiguously marked"))
1562 ((not (y-or-n-p "Try to auto-fix whitespace loss and word-wrap damage? "))
1563 (error "Abort!"))
1564 ((eolp) (insert " ") (forward-line -1) t)
1565 (t (insert " ") (delete-region (- (point) 2) (- (point) 1)) t))
1566 (forward-line))))
1568 (defun diff-sanity-check-hunk ()
1569 (let (;; Every modification is protected by a y-or-n-p, so it's probably
1570 ;; OK to override a read-only setting.
1571 (inhibit-read-only t))
1572 (save-excursion
1573 (cond
1574 ((not (looking-at diff-hunk-header-re))
1575 (error "Not recognizable hunk header"))
1577 ;; A context diff.
1578 ((eq (char-after) ?*)
1579 (if (not (looking-at "\\*\\{15\\}\\(?: .*\\)?\n\\*\\*\\* \\([0-9]+\\)\\(?:,\\([0-9]+\\)\\)? \\*\\*\\*\\*"))
1580 (error "Unrecognized context diff first hunk header format")
1581 (forward-line 2)
1582 (diff-sanity-check-context-hunk-half
1583 (if (match-end 2)
1584 (1+ (- (string-to-number (match-string 2))
1585 (string-to-number (match-string 1))))
1587 (if (not (looking-at diff-context-mid-hunk-header-re))
1588 (error "Unrecognized context diff second hunk header format")
1589 (forward-line)
1590 (diff-sanity-check-context-hunk-half
1591 (if (match-end 2)
1592 (1+ (- (string-to-number (match-string 2))
1593 (string-to-number (match-string 1))))
1594 1)))))
1596 ;; A unified diff.
1597 ((eq (char-after) ?@)
1598 (if (not (looking-at diff-hunk-header-re-unified))
1599 (error "Unrecognized unified diff hunk header format")
1600 (let ((before (string-to-number (or (match-string 2) "1")))
1601 (after (string-to-number (or (match-string 4) "1"))))
1602 (forward-line)
1603 (while
1604 (pcase (char-after)
1605 (?\s (cl-decf before) (cl-decf after) t)
1607 (if (and (looking-at diff-file-header-re)
1608 (zerop before) (zerop after))
1609 ;; No need to query: this is a case where two patches
1610 ;; are concatenated and only counting the lines will
1611 ;; give the right result. Let's just add an empty
1612 ;; line so that our code which doesn't count lines
1613 ;; will not get confused.
1614 (progn (save-excursion (insert "\n")) nil)
1615 (cl-decf before) t))
1616 (?+ (cl-decf after) t)
1618 (cond
1619 ((and diff-valid-unified-empty-line
1620 ;; Not just (eolp) so we don't infloop at eob.
1621 (eq (char-after) ?\n)
1622 (> before 0) (> after 0))
1623 (cl-decf before) (cl-decf after) t)
1624 ((and (zerop before) (zerop after)) nil)
1625 ((or (< before 0) (< after 0))
1626 (error (if (or (zerop before) (zerop after))
1627 "End of hunk ambiguously marked"
1628 "Hunk seriously messed up")))
1629 ((not (y-or-n-p (concat "Try to auto-fix " (if (eolp) "whitespace loss" "word-wrap damage") "? ")))
1630 (error "Abort!"))
1631 ((eolp) (insert " ") (forward-line -1) t)
1632 (t (insert " ")
1633 (delete-region (- (point) 2) (- (point) 1)) t))))
1634 (forward-line)))))
1636 ;; A plain diff.
1638 ;; TODO.
1639 )))))
1641 (defun diff-hunk-text (hunk destp char-offset)
1642 "Return the literal source text from HUNK as (TEXT . OFFSET).
1643 If DESTP is nil, TEXT is the source, otherwise the destination text.
1644 CHAR-OFFSET is a char-offset in HUNK, and OFFSET is the corresponding
1645 char-offset in TEXT."
1646 (with-temp-buffer
1647 (insert hunk)
1648 (goto-char (point-min))
1649 (let ((src-pos nil)
1650 (dst-pos nil)
1651 (divider-pos nil)
1652 (num-pfx-chars 2))
1653 ;; Set the following variables:
1654 ;; SRC-POS buffer pos of the source part of the hunk or nil if none
1655 ;; DST-POS buffer pos of the destination part of the hunk or nil
1656 ;; DIVIDER-POS buffer pos of any divider line separating the src & dst
1657 ;; NUM-PFX-CHARS number of line-prefix characters used by this format"
1658 (cond ((looking-at "^@@")
1659 ;; unified diff
1660 (setq num-pfx-chars 1)
1661 (forward-line 1)
1662 (setq src-pos (point) dst-pos (point)))
1663 ((looking-at "^\\*\\*")
1664 ;; context diff
1665 (forward-line 2)
1666 (setq src-pos (point))
1667 (re-search-forward diff-context-mid-hunk-header-re nil t)
1668 (forward-line 0)
1669 (setq divider-pos (point))
1670 (forward-line 1)
1671 (setq dst-pos (point)))
1672 ((looking-at "^[0-9]+a[0-9,]+$")
1673 ;; normal diff, insert
1674 (forward-line 1)
1675 (setq dst-pos (point)))
1676 ((looking-at "^[0-9,]+d[0-9]+$")
1677 ;; normal diff, delete
1678 (forward-line 1)
1679 (setq src-pos (point)))
1680 ((looking-at "^[0-9,]+c[0-9,]+$")
1681 ;; normal diff, change
1682 (forward-line 1)
1683 (setq src-pos (point))
1684 (re-search-forward "^---$" nil t)
1685 (forward-line 0)
1686 (setq divider-pos (point))
1687 (forward-line 1)
1688 (setq dst-pos (point)))
1690 (error "Unknown diff hunk type")))
1692 (if (if destp (null dst-pos) (null src-pos))
1693 ;; Implied empty text
1694 (if char-offset '("" . 0) "")
1696 ;; For context diffs, either side can be empty, (if there's only
1697 ;; added or only removed text). We should then use the other side.
1698 (cond ((equal src-pos divider-pos) (setq src-pos dst-pos))
1699 ((equal dst-pos (point-max)) (setq dst-pos src-pos)))
1701 (when char-offset (goto-char (+ (point-min) char-offset)))
1703 ;; Get rid of anything except the desired text.
1704 (save-excursion
1705 ;; Delete unused text region
1706 (let ((keep (if destp dst-pos src-pos)))
1707 (when (and divider-pos (> divider-pos keep))
1708 (delete-region divider-pos (point-max)))
1709 (delete-region (point-min) keep))
1710 ;; Remove line-prefix characters, and unneeded lines (unified diffs).
1711 (let ((kill-char (if destp ?- ?+)))
1712 (goto-char (point-min))
1713 (while (not (eobp))
1714 (if (eq (char-after) kill-char)
1715 (delete-region (point) (progn (forward-line 1) (point)))
1716 (delete-char num-pfx-chars)
1717 (forward-line 1)))))
1719 (let ((text (buffer-substring-no-properties (point-min) (point-max))))
1720 (if char-offset (cons text (- (point) (point-min))) text))))))
1723 (defun diff-find-text (text)
1724 "Return the buffer position (BEG . END) of the nearest occurrence of TEXT.
1725 If TEXT isn't found, nil is returned."
1726 (let* ((orig (point))
1727 (forw (and (search-forward text nil t)
1728 (cons (match-beginning 0) (match-end 0))))
1729 (back (and (goto-char (+ orig (length text)))
1730 (search-backward text nil t)
1731 (cons (match-beginning 0) (match-end 0)))))
1732 ;; Choose the closest match.
1733 (if (and forw back)
1734 (if (> (- (car forw) orig) (- orig (car back))) back forw)
1735 (or back forw))))
1737 (defun diff-find-approx-text (text)
1738 "Return the buffer position (BEG . END) of the nearest occurrence of TEXT.
1739 Whitespace differences are ignored."
1740 (let* ((orig (point))
1741 (re (concat "^[ \t\n\f]*"
1742 (mapconcat 'regexp-quote (split-string text) "[ \t\n\f]+")
1743 "[ \t\n\f]*\n"))
1744 (forw (and (re-search-forward re nil t)
1745 (cons (match-beginning 0) (match-end 0))))
1746 (back (and (goto-char (+ orig (length text)))
1747 (re-search-backward re nil t)
1748 (cons (match-beginning 0) (match-end 0)))))
1749 ;; Choose the closest match.
1750 (if (and forw back)
1751 (if (> (- (car forw) orig) (- orig (car back))) back forw)
1752 (or back forw))))
1754 (defsubst diff-xor (a b) (if a (if (not b) a) b))
1756 (defun diff-find-source-location (&optional other-file reverse noprompt)
1757 "Find out (BUF LINE-OFFSET POS SRC DST SWITCHED).
1758 BUF is the buffer corresponding to the source file.
1759 LINE-OFFSET is the offset between the expected and actual positions
1760 of the text of the hunk or nil if the text was not found.
1761 POS is a pair (BEG . END) indicating the position of the text in the buffer.
1762 SRC and DST are the two variants of text as returned by `diff-hunk-text'.
1763 SRC is the variant that was found in the buffer.
1764 SWITCHED is non-nil if the patch is already applied.
1765 NOPROMPT, if non-nil, means not to prompt the user."
1766 (save-excursion
1767 (let* ((hunk-bounds (diff-bounds-of-hunk))
1768 (other (diff-xor other-file diff-jump-to-old-file))
1769 (char-offset (- (point) (goto-char (car hunk-bounds))))
1770 ;; Check that the hunk is well-formed. Otherwise diff-mode and
1771 ;; the user may disagree on what constitutes the hunk
1772 ;; (e.g. because an empty line truncates the hunk mid-course),
1773 ;; leading to potentially nasty surprises for the user.
1775 ;; Suppress check when NOPROMPT is non-nil (Bug#3033).
1776 (_ (unless noprompt (diff-sanity-check-hunk)))
1777 (hunk (buffer-substring
1778 (point) (cadr hunk-bounds)))
1779 (old (diff-hunk-text hunk reverse char-offset))
1780 (new (diff-hunk-text hunk (not reverse) char-offset))
1781 ;; Find the location specification.
1782 (line (if (not (looking-at "\\(?:\\*\\{15\\}.*\n\\)?[-@* ]*\\([0-9,]+\\)\\([ acd+]+\\([0-9,]+\\)\\)?"))
1783 (error "Can't find the hunk header")
1784 (if other (match-string 1)
1785 (if (match-end 3) (match-string 3)
1786 (unless (re-search-forward
1787 diff-context-mid-hunk-header-re nil t)
1788 (error "Can't find the hunk separator"))
1789 (match-string 1)))))
1790 (file (or (diff-find-file-name other noprompt)
1791 (error "Can't find the file")))
1792 (buf (find-file-noselect file)))
1793 ;; Update the user preference if he so wished.
1794 (when (> (prefix-numeric-value other-file) 8)
1795 (setq diff-jump-to-old-file other))
1796 (with-current-buffer buf
1797 (goto-char (point-min)) (forward-line (1- (string-to-number line)))
1798 (let* ((orig-pos (point))
1799 (switched nil)
1800 ;; FIXME: Check for case where both OLD and NEW are found.
1801 (pos (or (diff-find-text (car old))
1802 (progn (setq switched t) (diff-find-text (car new)))
1803 (progn (setq switched nil)
1804 (condition-case nil
1805 (diff-find-approx-text (car old))
1806 (invalid-regexp nil))) ;Regex too big.
1807 (progn (setq switched t)
1808 (condition-case nil
1809 (diff-find-approx-text (car new))
1810 (invalid-regexp nil))) ;Regex too big.
1811 (progn (setq switched nil) nil))))
1812 (nconc
1813 (list buf)
1814 (if pos
1815 (list (count-lines orig-pos (car pos)) pos)
1816 (list nil (cons orig-pos (+ orig-pos (length (car old))))))
1817 (if switched (list new old t) (list old new))))))))
1820 (defun diff-hunk-status-msg (line-offset reversed dry-run)
1821 (let ((msg (if dry-run
1822 (if reversed "already applied" "not yet applied")
1823 (if reversed "undone" "applied"))))
1824 (message (cond ((null line-offset) "Hunk text not found")
1825 ((= line-offset 0) "Hunk %s")
1826 ((= line-offset 1) "Hunk %s at offset %d line")
1827 (t "Hunk %s at offset %d lines"))
1828 msg line-offset)))
1830 (defvar diff-apply-hunk-to-backup-file nil)
1832 (defun diff-apply-hunk (&optional reverse)
1833 "Apply the current hunk to the source file and go to the next.
1834 By default, the new source file is patched, but if the variable
1835 `diff-jump-to-old-file' is non-nil, then the old source file is
1836 patched instead (some commands, such as `diff-goto-source' can change
1837 the value of this variable when given an appropriate prefix argument).
1839 With a prefix argument, REVERSE the hunk."
1840 (interactive "P")
1841 (pcase-let ((`(,buf ,line-offset ,pos ,old ,new ,switched)
1842 ;; Sometimes we'd like to have the following behavior: if
1843 ;; REVERSE go to the new file, otherwise go to the old.
1844 ;; But that means that by default we use the old file, which is
1845 ;; the opposite of the default for diff-goto-source, and is thus
1846 ;; confusing. Also when you don't know about it it's
1847 ;; pretty surprising.
1848 ;; TODO: make it possible to ask explicitly for this behavior.
1850 ;; This is duplicated in diff-test-hunk.
1851 (diff-find-source-location nil reverse)))
1852 (cond
1853 ((null line-offset)
1854 (error "Can't find the text to patch"))
1855 ((with-current-buffer buf
1856 (and buffer-file-name
1857 (backup-file-name-p buffer-file-name)
1858 (not diff-apply-hunk-to-backup-file)
1859 (not (set (make-local-variable 'diff-apply-hunk-to-backup-file)
1860 (yes-or-no-p (format "Really apply this hunk to %s? "
1861 (file-name-nondirectory
1862 buffer-file-name)))))))
1863 (error "%s"
1864 (substitute-command-keys
1865 (format "Use %s\\[diff-apply-hunk] to apply it to the other file"
1866 (if (not reverse) "\\[universal-argument] ")))))
1867 ((and switched
1868 ;; A reversed patch was detected, perhaps apply it in reverse.
1869 (not (save-window-excursion
1870 (pop-to-buffer buf)
1871 (goto-char (+ (car pos) (cdr old)))
1872 (y-or-n-p
1873 (if reverse
1874 "Hunk hasn't been applied yet; apply it now? "
1875 "Hunk has already been applied; undo it? ")))))
1876 (message "(Nothing done)"))
1878 ;; Apply the hunk
1879 (with-current-buffer buf
1880 (goto-char (car pos))
1881 (delete-region (car pos) (cdr pos))
1882 (insert (car new)))
1883 ;; Display BUF in a window
1884 (set-window-point (display-buffer buf) (+ (car pos) (cdr new)))
1885 (diff-hunk-status-msg line-offset (diff-xor switched reverse) nil)
1887 ;; Advance to the next hunk with skip-hunk-start set to t
1888 ;; because we want the behavior of moving to the next logical
1889 ;; hunk, not the original behavior where were would sometimes
1890 ;; stay on the current hunk. This is the behavior we get when
1891 ;; navigating through hunks interactively, and we want it when
1892 ;; applying hunks too (see http://debbugs.gnu.org/17544).
1893 (when diff-advance-after-apply-hunk
1894 (diff-hunk-next nil t))))))
1897 (defun diff-test-hunk (&optional reverse)
1898 "See whether it's possible to apply the current hunk.
1899 With a prefix argument, try to REVERSE the hunk."
1900 (interactive "P")
1901 (pcase-let ((`(,buf ,line-offset ,pos ,src ,_dst ,switched)
1902 (diff-find-source-location nil reverse)))
1903 (set-window-point (display-buffer buf) (+ (car pos) (cdr src)))
1904 (diff-hunk-status-msg line-offset (diff-xor reverse switched) t)))
1907 (defun diff-kill-applied-hunks ()
1908 "Kill all hunks that have already been applied starting at point."
1909 (interactive)
1910 (while (not (eobp))
1911 (pcase-let ((`(,_buf ,line-offset ,_pos ,_src ,_dst ,switched)
1912 (diff-find-source-location nil nil)))
1913 (if (and line-offset switched)
1914 (diff-hunk-kill)
1915 (diff-hunk-next)))))
1917 (defalias 'diff-mouse-goto-source 'diff-goto-source)
1919 (defun diff-goto-source (&optional other-file event)
1920 "Jump to the corresponding source line.
1921 `diff-jump-to-old-file' (or its opposite if the OTHER-FILE prefix arg
1922 is given) determines whether to jump to the old or the new file.
1923 If the prefix arg is bigger than 8 (for example with \\[universal-argument] \\[universal-argument])
1924 then `diff-jump-to-old-file' is also set, for the next invocations."
1925 (interactive (list current-prefix-arg last-input-event))
1926 ;; When pointing at a removal line, we probably want to jump to
1927 ;; the old location, and else to the new (i.e. as if reverting).
1928 ;; This is a convenient detail when using smerge-diff.
1929 (if event (posn-set-point (event-end event)))
1930 (let ((rev (not (save-excursion (beginning-of-line) (looking-at "[-<]")))))
1931 (pcase-let ((`(,buf ,line-offset ,pos ,src ,_dst ,switched)
1932 (diff-find-source-location other-file rev)))
1933 (pop-to-buffer buf)
1934 (goto-char (+ (car pos) (cdr src)))
1935 (diff-hunk-status-msg line-offset (diff-xor rev switched) t))))
1938 (defun diff-current-defun ()
1939 "Find the name of function at point.
1940 For use in `add-log-current-defun-function'."
1941 ;; Kill change-log-default-name so it gets recomputed each time, since
1942 ;; each hunk may belong to another file which may belong to another
1943 ;; directory and hence have a different ChangeLog file.
1944 (kill-local-variable 'change-log-default-name)
1945 (save-excursion
1946 (when (looking-at diff-hunk-header-re)
1947 (forward-line 1)
1948 (re-search-forward "^[^ ]" nil t))
1949 (pcase-let ((`(,buf ,_line-offset ,pos ,src ,dst ,switched)
1950 (ignore-errors ;Signals errors in place of prompting.
1951 ;; Use `noprompt' since this is used in which-func-mode
1952 ;; and such.
1953 (diff-find-source-location nil nil 'noprompt))))
1954 (when buf
1955 (beginning-of-line)
1956 (or (when (memq (char-after) '(?< ?-))
1957 ;; Cursor is pointing at removed text. This could be a removed
1958 ;; function, in which case, going to the source buffer will
1959 ;; not help since the function is now removed. Instead,
1960 ;; try to figure out the function name just from the
1961 ;; code-fragment.
1962 (let ((old (if switched dst src)))
1963 (with-temp-buffer
1964 (insert (car old))
1965 (funcall (buffer-local-value 'major-mode buf))
1966 (goto-char (+ (point-min) (cdr old)))
1967 (add-log-current-defun))))
1968 (with-current-buffer buf
1969 (goto-char (+ (car pos) (cdr src)))
1970 (add-log-current-defun)))))))
1972 (defun diff-ignore-whitespace-hunk ()
1973 "Re-diff the current hunk, ignoring whitespace differences."
1974 (interactive)
1975 (let* ((hunk-bounds (diff-bounds-of-hunk))
1976 (char-offset (- (point) (goto-char (car hunk-bounds))))
1977 (opts (pcase (char-after) (?@ "-bu") (?* "-bc") (_ "-b")))
1978 (line-nb (and (or (looking-at "[^0-9]+\\([0-9]+\\)")
1979 (error "Can't find line number"))
1980 (string-to-number (match-string 1))))
1981 (inhibit-read-only t)
1982 (hunk (delete-and-extract-region
1983 (point) (cadr hunk-bounds)))
1984 (lead (make-string (1- line-nb) ?\n)) ;Line nums start at 1.
1985 (file1 (make-temp-file "diff1"))
1986 (file2 (make-temp-file "diff2"))
1987 (coding-system-for-read buffer-file-coding-system)
1988 old new)
1989 (unwind-protect
1990 (save-excursion
1991 (setq old (diff-hunk-text hunk nil char-offset))
1992 (setq new (diff-hunk-text hunk t char-offset))
1993 (write-region (concat lead (car old)) nil file1 nil 'nomessage)
1994 (write-region (concat lead (car new)) nil file2 nil 'nomessage)
1995 (with-temp-buffer
1996 (let ((status
1997 (call-process diff-command nil t nil
1998 opts file1 file2)))
1999 (pcase status
2000 (0 nil) ;Nothing to reformat.
2001 (1 (goto-char (point-min))
2002 ;; Remove the file-header.
2003 (when (re-search-forward diff-hunk-header-re nil t)
2004 (delete-region (point-min) (match-beginning 0))))
2005 (_ (goto-char (point-max))
2006 (unless (bolp) (insert "\n"))
2007 (insert hunk)))
2008 (setq hunk (buffer-string))
2009 (unless (memq status '(0 1))
2010 (error "Diff returned: %s" status)))))
2011 ;; Whatever happens, put back some equivalent text: either the new
2012 ;; one or the original one in case some error happened.
2013 (insert hunk)
2014 (delete-file file1)
2015 (delete-file file2))))
2017 ;;; Fine change highlighting.
2019 (defface diff-refine-changed
2020 '((((class color) (min-colors 88) (background light))
2021 :background "#ffff55")
2022 (((class color) (min-colors 88) (background dark))
2023 :background "#aaaa22")
2024 (t :inverse-video t))
2025 "Face used for char-based changes shown by `diff-refine-hunk'."
2026 :group 'diff-mode)
2028 (defface diff-refine-removed
2029 '((default
2030 :inherit diff-refine-changed)
2031 (((class color) (min-colors 88) (background light))
2032 :background "#ffbbbb")
2033 (((class color) (min-colors 88) (background dark))
2034 :background "#aa2222"))
2035 "Face used for removed characters shown by `diff-refine-hunk'."
2036 :group 'diff-mode
2037 :version "24.3")
2039 (defface diff-refine-added
2040 '((default
2041 :inherit diff-refine-changed)
2042 (((class color) (min-colors 88) (background light))
2043 :background "#aaffaa")
2044 (((class color) (min-colors 88) (background dark))
2045 :background "#22aa22"))
2046 "Face used for added characters shown by `diff-refine-hunk'."
2047 :group 'diff-mode
2048 :version "24.3")
2050 (defun diff-refine-preproc ()
2051 (while (re-search-forward "^[+>]" nil t)
2052 ;; Remove spurious changes due to the fact that one side of the hunk is
2053 ;; marked with leading + or > and the other with leading - or <.
2054 ;; We used to replace all the prefix chars with " " but this only worked
2055 ;; when we did char-based refinement (or when using
2056 ;; smerge-refine-weight-hack) since otherwise, the `forward' motion done
2057 ;; in chopup do not necessarily do the same as the ones in highlight
2058 ;; since the "_" is not treated the same as " ".
2059 (replace-match (cdr (assq (char-before) '((?+ . "-") (?> . "<"))))))
2062 (declare-function smerge-refine-subst "smerge-mode"
2063 (beg1 end1 beg2 end2 props-c &optional preproc props-r props-a))
2065 (defun diff-refine-hunk ()
2066 "Highlight changes of hunk at point at a finer granularity."
2067 (interactive)
2068 (require 'smerge-mode)
2069 (save-excursion
2070 (let* ((hunk-bounds (diff-bounds-of-hunk))
2071 (style (progn (goto-char (car hunk-bounds))
2072 (diff-hunk-style))) ;Skips the hunk header as well.
2073 (beg (point))
2074 (end (cadr hunk-bounds))
2075 (props-c '((diff-mode . fine) (face diff-refine-changed)))
2076 (props-r '((diff-mode . fine) (face diff-refine-removed)))
2077 (props-a '((diff-mode . fine) (face diff-refine-added))))
2079 (remove-overlays beg end 'diff-mode 'fine)
2081 (goto-char beg)
2082 (pcase style
2083 (`unified
2084 (while (re-search-forward
2085 (eval-when-compile
2086 (let ((no-LF-at-eol-re "\\(?:\\\\.*\n\\)?"))
2087 (concat "^\\(?:-.*\n\\)+" no-LF-at-eol-re
2088 "\\(\\)"
2089 "\\(?:\\+.*\n\\)+" no-LF-at-eol-re)))
2090 end t)
2091 (smerge-refine-subst (match-beginning 0) (match-end 1)
2092 (match-end 1) (match-end 0)
2093 nil 'diff-refine-preproc props-r props-a)))
2094 (`context
2095 (let* ((middle (save-excursion (re-search-forward "^---")))
2096 (other middle))
2097 (while (re-search-forward "^\\(?:!.*\n\\)+" middle t)
2098 (smerge-refine-subst (match-beginning 0) (match-end 0)
2099 (save-excursion
2100 (goto-char other)
2101 (re-search-forward "^\\(?:!.*\n\\)+" end)
2102 (setq other (match-end 0))
2103 (match-beginning 0))
2104 other
2105 (if diff-use-changed-face props-c)
2106 'diff-refine-preproc
2107 (unless diff-use-changed-face props-r)
2108 (unless diff-use-changed-face props-a)))))
2109 (_ ;; Normal diffs.
2110 (let ((beg1 (1+ (point))))
2111 (when (re-search-forward "^---.*\n" end t)
2112 ;; It's a combined add&remove, so there's something to do.
2113 (smerge-refine-subst beg1 (match-beginning 0)
2114 (match-end 0) end
2115 nil 'diff-refine-preproc props-r props-a))))))))
2117 (defun diff-undo (&optional arg)
2118 "Perform `undo', ignoring the buffer's read-only status."
2119 (interactive "P")
2120 (let ((inhibit-read-only t))
2121 (undo arg)))
2123 (defun diff-add-change-log-entries-other-window ()
2124 "Iterate through the current diff and create ChangeLog entries.
2125 I.e. like `add-change-log-entry-other-window' but applied to all hunks."
2126 (interactive)
2127 ;; XXX: Currently add-change-log-entry-other-window is only called
2128 ;; once per hunk. Some hunks have multiple changes, it would be
2129 ;; good to call it for each change.
2130 (save-excursion
2131 (goto-char (point-min))
2132 (condition-case nil
2133 ;; Call add-change-log-entry-other-window for each hunk in
2134 ;; the diff buffer.
2135 (while (progn
2136 (diff-hunk-next)
2137 ;; Move to where the changes are,
2138 ;; `add-change-log-entry-other-window' works better in
2139 ;; that case.
2140 (re-search-forward
2141 (concat "\n[!+-<>]"
2142 ;; If the hunk is a context hunk with an empty first
2143 ;; half, recognize the "--- NNN,MMM ----" line
2144 "\\(-- [0-9]+\\(,[0-9]+\\)? ----\n"
2145 ;; and skip to the next non-context line.
2146 "\\( .*\n\\)*[+]\\)?")
2147 nil t))
2148 (save-excursion
2149 ;; FIXME: this pops up windows of all the buffers.
2150 (add-change-log-entry nil nil t nil t)))
2151 ;; When there's no more hunks, diff-hunk-next signals an error.
2152 (error nil))))
2154 (defun diff-delete-trailing-whitespace (&optional other-file)
2155 "Remove trailing whitespace from lines modified in this diff.
2156 This edits both the current Diff mode buffer and the patched
2157 source file(s). If `diff-jump-to-old-file' is non-nil, edit the
2158 original (unpatched) source file instead. With a prefix argument
2159 OTHER-FILE, flip the choice of which source file to edit.
2161 If a file referenced in the diff has no buffer and needs to be
2162 fixed, visit it in a buffer."
2163 (interactive "P")
2164 (save-excursion
2165 (goto-char (point-min))
2166 (let* ((other (diff-xor other-file diff-jump-to-old-file))
2167 (modified-buffers nil)
2168 (style (save-excursion
2169 (when (re-search-forward diff-hunk-header-re nil t)
2170 (goto-char (match-beginning 0))
2171 (diff-hunk-style))))
2172 (regexp (concat "^[" (if other "-<" "+>") "!]"
2173 (if (eq style 'context) " " "")
2174 ".*?\\([ \t]+\\)$"))
2175 (inhibit-read-only t)
2176 (end-marker (make-marker))
2177 hunk-end)
2178 ;; Move to the first hunk.
2179 (re-search-forward diff-hunk-header-re nil 1)
2180 (while (progn (save-excursion
2181 (re-search-forward diff-hunk-header-re nil 1)
2182 (setq hunk-end (point)))
2183 (< (point) hunk-end))
2184 ;; For context diffs, search only in the appropriate half of
2185 ;; the hunk. For other diffs, search within the entire hunk.
2186 (if (not (eq style 'context))
2187 (set-marker end-marker hunk-end)
2188 (let ((mid-hunk
2189 (save-excursion
2190 (re-search-forward diff-context-mid-hunk-header-re hunk-end)
2191 (point))))
2192 (if other
2193 (set-marker end-marker mid-hunk)
2194 (goto-char mid-hunk)
2195 (set-marker end-marker hunk-end))))
2196 (while (re-search-forward regexp end-marker t)
2197 (let ((match-data (match-data)))
2198 (pcase-let ((`(,buf ,line-offset ,pos ,src ,_dst ,_switched)
2199 (diff-find-source-location other-file)))
2200 (when line-offset
2201 ;; Remove the whitespace in the Diff mode buffer.
2202 (set-match-data match-data)
2203 (replace-match "" t t nil 1)
2204 ;; Remove the whitespace in the source buffer.
2205 (with-current-buffer buf
2206 (save-excursion
2207 (goto-char (+ (car pos) (cdr src)))
2208 (beginning-of-line)
2209 (when (re-search-forward "\\([ \t]+\\)$" (line-end-position) t)
2210 (unless (memq buf modified-buffers)
2211 (push buf modified-buffers))
2212 (replace-match ""))))))))
2213 (goto-char hunk-end))
2214 (if modified-buffers
2215 (message "Deleted trailing whitespace from %s."
2216 (mapconcat (lambda (buf) (format-message
2217 "`%s'" (buffer-name buf)))
2218 modified-buffers ", "))
2219 (message "No trailing whitespace to delete.")))))
2221 ;; provide the package
2222 (provide 'diff-mode)
2224 ;;; Old Change Log from when diff-mode wasn't part of Emacs:
2225 ;; Revision 1.11 1999/10/09 23:38:29 monnier
2226 ;; (diff-mode-load-hook): dropped.
2227 ;; (auto-mode-alist): also catch *.diffs.
2228 ;; (diff-find-file-name, diff-mode): add smarts to find the right file
2229 ;; for *.rej files (that lack any file name indication).
2231 ;; Revision 1.10 1999/09/30 15:32:11 monnier
2232 ;; added support for "\ No newline at end of file".
2234 ;; Revision 1.9 1999/09/15 00:01:13 monnier
2235 ;; - added basic `compile' support.
2236 ;; - have diff-kill-hunk call diff-kill-file if it's the only hunk.
2237 ;; - diff-kill-file now tries to kill the leading garbage as well.
2239 ;; Revision 1.8 1999/09/13 21:10:09 monnier
2240 ;; - don't use CL in the autoloaded code
2241 ;; - accept diffs using -T
2243 ;; Revision 1.7 1999/09/05 20:53:03 monnier
2244 ;; interface to ediff-patch
2246 ;; Revision 1.6 1999/09/01 20:55:13 monnier
2247 ;; (ediff=patch-file): add bindings to call ediff-patch.
2248 ;; (diff-find-file-name): taken out of diff-goto-source.
2249 ;; (diff-unified->context, diff-context->unified, diff-reverse-direction,
2250 ;; diff-fixup-modifs): only use the region if a prefix arg is given.
2252 ;; Revision 1.5 1999/08/31 19:18:52 monnier
2253 ;; (diff-beginning-of-file, diff-prev-file): fixed wrong parenthesis.
2255 ;; Revision 1.4 1999/08/31 13:01:44 monnier
2256 ;; use `combine-after-change-calls' to minimize the slowdown of font-lock.
2259 ;;; diff-mode.el ends here