Commit | Line | Data |
---|---|---|
b532d575 | 1 | ;;; diff-mode.el --- a mode for viewing/editing context diffs |
610a6418 | 2 | |
4f815065 SM |
3 | ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004 |
4 | ;; Free Software Foundation, Inc. | |
610a6418 SM |
5 | |
6 | ;; Author: Stefan Monnier <monnier@cs.yale.edu> | |
b532d575 | 7 | ;; Keywords: convenience patch diff |
610a6418 SM |
8 | |
9 | ;; This file is part of GNU Emacs. | |
10 | ||
11 | ;; GNU Emacs is free software; you can redistribute it and/or modify | |
12 | ;; it under the terms of the GNU General Public License as published by | |
13 | ;; the Free Software Foundation; either version 2, or (at your option) | |
14 | ;; any later version. | |
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 | |
22 | ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
23 | ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
24 | ;; Boston, MA 02111-1307, USA. | |
25 | ||
26 | ;;; Commentary: | |
27 | ||
cdbb79c1 | 28 | ;; Provides support for font-lock, outline, navigation |
610a6418 SM |
29 | ;; commands, editing and various conversions as well as jumping |
30 | ;; to the corresponding source file. | |
31 | ||
3ccdb14e | 32 | ;; Inspired by Pavel Machek's patch-mode.el (<pavel@@atrey.karlin.mff.cuni.cz>) |
b532d575 | 33 | ;; Some efforts were spent to have it somewhat compatible with XEmacs' |
610a6418 SM |
34 | ;; diff-mode as well as with compilation-minor-mode |
35 | ||
610a6418 SM |
36 | ;; Bugs: |
37 | ||
027ac3f8 | 38 | ;; - Reverse doesn't work with normal diffs. |
610a6418 SM |
39 | |
40 | ;; Todo: | |
41 | ||
22cd1973 SM |
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. | |
44 | ;; | |
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 | ;; | |
50 | ;; - Refine hunk on a word-by-word basis. | |
51 | ;; | |
027ac3f8 | 52 | ;; - Handle `diff -b' output in context->unified. |
610a6418 SM |
53 | |
54 | ;;; Code: | |
55 | ||
56 | (eval-when-compile (require 'cl)) | |
57 | ||
58 | ||
59 | (defgroup diff-mode () | |
3cec9c57 | 60 | "Major mode for viewing/editing diffs" |
ccce6558 | 61 | :version "21.1" |
610a6418 SM |
62 | :group 'tools |
63 | :group 'diff) | |
64 | ||
22cd1973 | 65 | (defcustom diff-default-read-only nil |
769dd0f1 SM |
66 | "If non-nil, `diff-mode' buffers default to being read-only." |
67 | :type 'boolean | |
68 | :group 'diff-mode) | |
69 | ||
e8a1ed31 | 70 | (defcustom diff-jump-to-old-file nil |
610a6418 SM |
71 | "*Non-nil means `diff-goto-source' jumps to the old file. |
72 | Else, it jumps to the new file." | |
610a6418 SM |
73 | :type '(boolean)) |
74 | ||
e8a1ed31 | 75 | (defcustom diff-update-on-the-fly t |
610a6418 SM |
76 | "*Non-nil means hunk headers are kept up-to-date on-the-fly. |
77 | When editing a diff file, the line numbers in the hunk headers | |
78 | need to be kept consistent with the actual diff. This can | |
79 | either be done on the fly (but this sometimes interacts poorly with the | |
80 | undo mechanism) or whenever the file is written (can be slow | |
81 | when editing big diffs)." | |
610a6418 SM |
82 | :type '(boolean)) |
83 | ||
00df919e MB |
84 | (defcustom diff-advance-after-apply-hunk t |
85 | "*Non-nil means `diff-apply-hunk' will move to the next hunk after applying." | |
00df919e MB |
86 | :type 'boolean) |
87 | ||
88 | ||
22cd1973 SM |
89 | (defcustom diff-mode-hook nil |
90 | "Run after setting up the `diff-mode' major mode." | |
91 | :type 'hook | |
92 | :options '(diff-delete-empty-files diff-make-unified)) | |
610a6418 SM |
93 | |
94 | (defvar diff-outline-regexp | |
95 | "\\([*+][*+][*+] [^0-9]\\|@@ ...\\|\\*\\*\\* [0-9].\\|--- [0-9]..\\)") | |
96 | ||
cd632e57 | 97 | ;;;; |
610a6418 | 98 | ;;;; keymap, menu, ... |
cd632e57 | 99 | ;;;; |
610a6418 | 100 | |
d87e5627 | 101 | (easy-mmode-defmap diff-mode-shared-map |
027ac3f8 | 102 | '(;; From Pavel Machek's patch-mode. |
d87e5627 SM |
103 | ("n" . diff-hunk-next) |
104 | ("N" . diff-file-next) | |
105 | ("p" . diff-hunk-prev) | |
106 | ("P" . diff-file-prev) | |
107 | ("k" . diff-hunk-kill) | |
108 | ("K" . diff-file-kill) | |
027ac3f8 | 109 | ;; From compilation-minor-mode. |
d87e5627 SM |
110 | ("}" . diff-file-next) |
111 | ("{" . diff-file-prev) | |
610a6418 | 112 | ("\C-m" . diff-goto-source) |
a1e2d7f7 | 113 | ([mouse-2] . diff-goto-source) |
027ac3f8 | 114 | ;; From XEmacs' diff-mode. |
610a6418 SM |
115 | ("W" . widen) |
116 | ;;("." . diff-goto-source) ;display-buffer | |
117 | ;;("f" . diff-goto-source) ;find-file | |
118 | ("o" . diff-goto-source) ;other-window | |
119 | ;;("w" . diff-goto-source) ;other-frame | |
120 | ;;("N" . diff-narrow) | |
121 | ;;("h" . diff-show-header) | |
122 | ;;("j" . diff-show-difference) ;jump to Nth diff | |
123 | ;;("q" . diff-quit) | |
124 | (" " . scroll-up) | |
125 | ("\177" . scroll-down) | |
027ac3f8 | 126 | ;; Our very own bindings. |
610a6418 SM |
127 | ("A" . diff-ediff-patch) |
128 | ("r" . diff-restrict-view) | |
129 | ("R" . diff-reverse-direction) | |
130 | ("U" . diff-context->unified) | |
c010ecfa BR |
131 | ("C" . diff-unified->context) |
132 | ("q" . quit-window)) | |
0b82e382 | 133 | "Basic keymap for `diff-mode', bound to various prefix keys.") |
610a6418 | 134 | |
d87e5627 | 135 | (easy-mmode-defmap diff-mode-map |
610a6418 | 136 | `(("\e" . ,diff-mode-shared-map) |
027ac3f8 SM |
137 | ;; From compilation-minor-mode. |
138 | ("\C-c\C-c" . diff-goto-source) | |
139 | ;; Misc operations. | |
22cd1973 | 140 | ("\C-c\C-r" . diff-refine-hunk) |
cd632e57 | 141 | ("\C-c\C-s" . diff-split-hunk) |
3cec9c57 | 142 | ("\C-c\C-a" . diff-apply-hunk) |
b711788a | 143 | ("\C-c\C-t" . diff-test-hunk) |
677c0382 | 144 | ("\C-c\C-f" . next-error-follow-minor-mode)) |
610a6418 SM |
145 | "Keymap for `diff-mode'. See also `diff-mode-shared-map'.") |
146 | ||
147 | (easy-menu-define diff-mode-menu diff-mode-map | |
148 | "Menu for `diff-mode'." | |
149 | '("Diff" | |
150 | ["Jump to Source" diff-goto-source t] | |
cd632e57 SM |
151 | ["Apply hunk" diff-apply-hunk t] |
152 | ["Apply diff with Ediff" diff-ediff-patch t] | |
610a6418 SM |
153 | ["-----" nil nil] |
154 | ["Reverse direction" diff-reverse-direction t] | |
155 | ["Context -> Unified" diff-context->unified t] | |
156 | ["Unified -> Context" diff-unified->context t] | |
157 | ;;["Fixup Headers" diff-fixup-modifs (not buffer-read-only)] | |
158 | )) | |
159 | ||
3cec9c57 | 160 | (defcustom diff-minor-mode-prefix "\C-c=" |
0b82e382 | 161 | "Prefix key for `diff-minor-mode' commands." |
3cec9c57 | 162 | :type '(choice (string "\e") (string "C-c=") string)) |
0b82e382 | 163 | |
d87e5627 SM |
164 | (easy-mmode-defmap diff-minor-mode-map |
165 | `((,diff-minor-mode-prefix . ,diff-mode-shared-map)) | |
0b82e382 SM |
166 | "Keymap for `diff-minor-mode'. See also `diff-mode-shared-map'.") |
167 | ||
610a6418 | 168 | |
cd632e57 | 169 | ;;;; |
610a6418 | 170 | ;;;; font-lock support |
cd632e57 | 171 | ;;;; |
610a6418 | 172 | |
c0aa75f7 | 173 | (defface diff-header-face |
55f2eb7e | 174 | '((((class color) (min-colors 88) (background light)) |
4f815065 | 175 | :background "grey85") |
55f2eb7e | 176 | (((class color) (min-colors 88) (background dark)) |
4f815065 | 177 | :background "grey45") |
55f2eb7e | 178 | (((class color) (background light)) |
4f815065 | 179 | :foreground "blue1" :weight bold) |
55f2eb7e | 180 | (((class color) (background dark)) |
4f815065 SM |
181 | :foreground "green" :weight bold) |
182 | (t :weight bold)) | |
1eabc5e6 | 183 | "`diff-mode' face inherited by hunk and index header faces.") |
c0aa75f7 SM |
184 | (defvar diff-header-face 'diff-header-face) |
185 | ||
610a6418 | 186 | (defface diff-file-header-face |
55f2eb7e | 187 | '((((class color) (min-colors 88) (background light)) |
4f815065 | 188 | :background "grey70" :weight bold) |
55f2eb7e | 189 | (((class color) (min-colors 88) (background dark)) |
4f815065 | 190 | :background "grey60" :weight bold) |
55f2eb7e | 191 | (((class color) (background light)) |
4f815065 | 192 | :foreground "yellow" :weight bold) |
55f2eb7e | 193 | (((class color) (background dark)) |
4f815065 SM |
194 | :foreground "cyan" :weight bold) |
195 | (t :weight bold)) ; :height 1.3 | |
1eabc5e6 | 196 | "`diff-mode' face used to highlight file header lines.") |
610a6418 SM |
197 | (defvar diff-file-header-face 'diff-file-header-face) |
198 | ||
199 | (defface diff-index-face | |
4f815065 | 200 | '((t :inherit diff-file-header-face)) |
1eabc5e6 | 201 | "`diff-mode' face used to highlight index header lines.") |
610a6418 SM |
202 | (defvar diff-index-face 'diff-index-face) |
203 | ||
204 | (defface diff-hunk-header-face | |
4f815065 | 205 | '((t :inherit diff-header-face)) |
1eabc5e6 | 206 | "`diff-mode' face used to highlight hunk header lines.") |
610a6418 SM |
207 | (defvar diff-hunk-header-face 'diff-hunk-header-face) |
208 | ||
209 | (defface diff-removed-face | |
4f815065 | 210 | '((t :inherit diff-changed-face)) |
1eabc5e6 | 211 | "`diff-mode' face used to highlight removed lines.") |
610a6418 SM |
212 | (defvar diff-removed-face 'diff-removed-face) |
213 | ||
214 | (defface diff-added-face | |
4f815065 | 215 | '((t :inherit diff-changed-face)) |
1eabc5e6 | 216 | "`diff-mode' face used to highlight added lines.") |
610a6418 SM |
217 | (defvar diff-added-face 'diff-added-face) |
218 | ||
219 | (defface diff-changed-face | |
86a27a3a | 220 | '((((type tty pc) (class color) (background light)) |
4f815065 | 221 | :foreground "magenta" :weight bold :slant italic) |
86a27a3a | 222 | (((type tty pc) (class color) (background dark)) |
4f815065 | 223 | :foreground "yellow" :weight bold :slant italic)) |
1eabc5e6 | 224 | "`diff-mode' face used to highlight changed lines.") |
610a6418 SM |
225 | (defvar diff-changed-face 'diff-changed-face) |
226 | ||
34460354 | 227 | (defface diff-function-face |
4f815065 | 228 | '((t :inherit diff-context-face)) |
1eabc5e6 | 229 | "`diff-mode' face used to highlight function names produced by \"diff -p\".") |
34460354 EZ |
230 | (defvar diff-function-face 'diff-function-face) |
231 | ||
9244f2c7 SM |
232 | (defface diff-context-face |
233 | '((((class color) (background light)) | |
4f815065 | 234 | :foreground "grey50") |
9244f2c7 | 235 | (((class color) (background dark)) |
4f815065 | 236 | :foreground "grey70")) |
1eabc5e6 | 237 | "`diff-mode' face used to highlight context and other side-information.") |
9244f2c7 | 238 | (defvar diff-context-face 'diff-context-face) |
c0aa75f7 | 239 | |
7dfb000f | 240 | (defface diff-nonexistent-face |
4f815065 | 241 | '((t :inherit diff-file-header-face)) |
1eabc5e6 | 242 | "`diff-mode' face used to highlight nonexistent files in recursive diffs.") |
7dfb000f | 243 | (defvar diff-nonexistent-face 'diff-nonexistent-face) |
469fc0a2 | 244 | |
22cd1973 SM |
245 | (defconst diff-yank-handler '(diff-yank-function)) |
246 | (defun diff-yank-function (text) | |
1ec7bd14 SM |
247 | ;; FIXME: the yank-handler is now called separately on each piece of text |
248 | ;; with a yank-handler property, so the next-single-property-change call | |
249 | ;; below will always return nil :-( --stef | |
22cd1973 SM |
250 | (let ((mixed (next-single-property-change 0 'yank-handler text)) |
251 | (start (point))) | |
252 | ;; First insert the text. | |
253 | (insert text) | |
254 | ;; If the text does not include any diff markers and if we're not | |
255 | ;; yanking back into a diff-mode buffer, get rid of the prefixes. | |
256 | (unless (or mixed (derived-mode-p 'diff-mode)) | |
257 | (undo-boundary) ; Just in case the user wanted the prefixes. | |
258 | (let ((re (save-excursion | |
259 | (if (re-search-backward "^[><!][ \t]" start t) | |
260 | (if (eq (char-after) ?!) | |
261 | "^[!+- ][ \t]" "^[<>][ \t]") | |
262 | "^[ <>!+-]")))) | |
263 | (save-excursion | |
264 | (while (re-search-backward re start t) | |
265 | (replace-match "" t t))))))) | |
266 | ||
267 | ||
610a6418 | 268 | (defvar diff-font-lock-keywords |
22cd1973 | 269 | `(("^\\(@@ -[0-9,]+ \\+[0-9,]+ @@\\)\\(.*\\)$" ;unified |
19e713d8 | 270 | (1 diff-hunk-header-face) |
34460354 | 271 | (2 diff-function-face)) |
469fc0a2 | 272 | ("^--- .+ ----$" . diff-hunk-header-face) ;context |
a62d56ab | 273 | ("^\\(\\*\\{15\\}\\)\\(.*\\)$" ;context |
19e713d8 | 274 | (1 diff-hunk-header-face) |
34460354 | 275 | (2 diff-function-face)) |
370d860c | 276 | ("^\\*\\*\\* .+ \\*\\*\\*\\*". diff-hunk-header-face) ;context |
a6373340 | 277 | ("^\\(---\\|\\+\\+\\+\\|\\*\\*\\*\\) \\(\\S-+\\)\\(.*[^*-]\\)?\n" |
c0aa75f7 | 278 | (0 diff-header-face) (2 diff-file-header-face prepend)) |
610a6418 | 279 | ("^[0-9,]+[acd][0-9,]+$" . diff-hunk-header-face) |
1ec7bd14 SM |
280 | ("^!.*\n" (0 diff-changed-face)) |
281 | ("^[+>].*\n" (0 diff-added-face)) | |
282 | ("^[-<].*\n" (0 diff-removed-face)) | |
c0aa75f7 | 283 | ("^Index: \\(.+\\).*\n" (0 diff-header-face) (1 diff-index-face prepend)) |
7dfb000f | 284 | ("^Only in .*\n" . diff-nonexistent-face) |
0e104800 | 285 | ("^#.*" . font-lock-string-face) |
1ec7bd14 | 286 | ("^[^-=+*!<>].*\n" (0 diff-context-face)))) |
610a6418 SM |
287 | |
288 | (defconst diff-font-lock-defaults | |
0e104800 | 289 | '(diff-font-lock-keywords t nil nil nil (font-lock-multiline . nil))) |
610a6418 | 290 | |
ccce6558 DL |
291 | (defvar diff-imenu-generic-expression |
292 | ;; Prefer second name as first is most likely to be a backup or | |
824693e7 DL |
293 | ;; version-control name. The [\t\n] at the end of the unidiff pattern |
294 | ;; catches Debian source diff files (which lack the trailing date). | |
295 | '((nil "\\+\\+\\+\\ \\([^\t\n]+\\)[\t\n]" 1) ; unidiffs | |
ccce6558 DL |
296 | (nil "^--- \\([^\t\n]+\\)\t.*\n\\*" 1))) ; context diffs |
297 | ||
cd632e57 | 298 | ;;;; |
610a6418 | 299 | ;;;; Movement |
cd632e57 | 300 | ;;;; |
610a6418 | 301 | |
c0078a04 | 302 | (defconst diff-hunk-header-re "^\\(@@ -[0-9,]+ \\+[0-9,]+ @@.*\\|\\*\\{15\\}.*\n\\*\\*\\* .+ \\*\\*\\*\\*\\|[0-9]+\\(,[0-9]+\\)?[acd][0-9]+\\(,[0-9]+\\)?\\)$") |
bd9d7d76 | 303 | (defconst diff-file-header-re (concat "^\\(--- .+\n\\+\\+\\+ \\|\\*\\*\\* .+\n--- \\|[^-+!<>0-9@* ]\\).+\n" (substring diff-hunk-header-re 1))) |
610a6418 SM |
304 | (defvar diff-narrowed-to nil) |
305 | ||
306 | (defun diff-end-of-hunk (&optional style) | |
996884b2 SM |
307 | (when (looking-at diff-hunk-header-re) |
308 | (unless style | |
309 | ;; Especially important for unified (because headers are ambiguous). | |
310 | (setq style (cdr (assq (char-after) '((?@ . unified) (?* . context)))))) | |
311 | (goto-char (match-end 0))) | |
0e104800 | 312 | (let ((end (and (re-search-forward (case style |
1eabc5e6 SM |
313 | ;; A `unified' header is ambiguous. |
314 | (unified (concat "^[^-+# \\]\\|" | |
315 | diff-file-header-re)) | |
0e104800 SM |
316 | (context "^[^-+#! \\]") |
317 | (normal "^[^<>#\\]") | |
318 | (t "^[^-+#!<> \\]")) | |
319 | nil t) | |
320 | (match-beginning 0)))) | |
321 | ;; The return value is used by easy-mmode-define-navigation. | |
322 | (goto-char (or end (point-max))))) | |
610a6418 SM |
323 | |
324 | (defun diff-beginning-of-hunk () | |
325 | (beginning-of-line) | |
326 | (unless (looking-at diff-hunk-header-re) | |
327 | (forward-line 1) | |
328 | (condition-case () | |
329 | (re-search-backward diff-hunk-header-re) | |
330 | (error (error "Can't find the beginning of the hunk"))))) | |
331 | ||
332 | (defun diff-beginning-of-file () | |
333 | (beginning-of-line) | |
334 | (unless (looking-at diff-file-header-re) | |
335 | (forward-line 2) | |
336 | (condition-case () | |
337 | (re-search-backward diff-file-header-re) | |
338 | (error (error "Can't find the beginning of the file"))))) | |
339 | ||
340 | (defun diff-end-of-file () | |
0e104800 | 341 | (re-search-forward "^[-+#!<>0-9@* \\]" nil t) |
22cd1973 SM |
342 | (re-search-forward (concat "^[^-+#!<>0-9@* \\]\\|" diff-file-header-re) |
343 | nil 'move) | |
344 | (if (match-beginning 1) | |
345 | (goto-char (match-beginning 1)) | |
346 | (beginning-of-line))) | |
610a6418 | 347 | |
d87e5627 SM |
348 | ;; Define diff-{hunk,file}-{prev,next} |
349 | (easy-mmode-define-navigation | |
067b4094 | 350 | diff-hunk diff-hunk-header-re "hunk" diff-end-of-hunk diff-restrict-view) |
d87e5627 SM |
351 | (easy-mmode-define-navigation |
352 | diff-file diff-file-header-re "file" diff-end-of-hunk) | |
610a6418 SM |
353 | |
354 | (defun diff-restrict-view (&optional arg) | |
355 | "Restrict the view to the current hunk. | |
356 | If the prefix ARG is given, restrict the view to the current file instead." | |
357 | (interactive "P") | |
358 | (save-excursion | |
359 | (if arg (diff-beginning-of-file) (diff-beginning-of-hunk)) | |
360 | (narrow-to-region (point) | |
361 | (progn (if arg (diff-end-of-file) (diff-end-of-hunk)) | |
362 | (point))) | |
363 | (set (make-local-variable 'diff-narrowed-to) (if arg 'file 'hunk)))) | |
364 | ||
365 | ||
d87e5627 | 366 | (defun diff-hunk-kill () |
610a6418 SM |
367 | "Kill current hunk." |
368 | (interactive) | |
369 | (diff-beginning-of-hunk) | |
1eabc5e6 | 370 | (let* ((start (point)) |
22cd1973 SM |
371 | (nexthunk (when (re-search-forward diff-hunk-header-re nil t) |
372 | (match-beginning 0))) | |
1eabc5e6 SM |
373 | (firsthunk (ignore-errors |
374 | (goto-char start) | |
375 | (diff-beginning-of-file) (diff-hunk-next) (point))) | |
376 | (nextfile (ignore-errors (diff-file-next) (point)))) | |
377 | (goto-char start) | |
610a6418 SM |
378 | (if (and firsthunk (= firsthunk start) |
379 | (or (null nexthunk) | |
380 | (and nextfile (> nexthunk nextfile)))) | |
1eabc5e6 | 381 | ;; It's the only hunk for this file, so kill the file. |
d87e5627 | 382 | (diff-file-kill) |
610a6418 SM |
383 | (diff-end-of-hunk) |
384 | (kill-region start (point))))) | |
385 | ||
d87e5627 | 386 | (defun diff-file-kill () |
610a6418 SM |
387 | "Kill current file's hunks." |
388 | (interactive) | |
389 | (diff-beginning-of-file) | |
390 | (let* ((start (point)) | |
391 | (prevhunk (save-excursion | |
392 | (ignore-errors | |
d87e5627 | 393 | (diff-hunk-prev) (point)))) |
610a6418 SM |
394 | (index (save-excursion |
395 | (re-search-backward "^Index: " prevhunk t)))) | |
396 | (when index (setq start index)) | |
397 | (diff-end-of-file) | |
22cd1973 | 398 | (if (looking-at "^\n") (forward-char 1)) ;`tla' generates such diffs. |
610a6418 SM |
399 | (kill-region start (point)))) |
400 | ||
fef8c55b SM |
401 | (defun diff-kill-junk () |
402 | "Kill spurious empty diffs." | |
403 | (interactive) | |
404 | (save-excursion | |
405 | (let ((inhibit-read-only t)) | |
406 | (goto-char (point-min)) | |
407 | (while (re-search-forward (concat "^\\(Index: .*\n\\)" | |
408 | "\\([^-+!* <>].*\n\\)*?" | |
409 | "\\(\\(Index:\\) \\|" | |
410 | diff-file-header-re "\\)") | |
411 | nil t) | |
412 | (delete-region (if (match-end 4) (match-beginning 0) (match-end 1)) | |
413 | (match-beginning 3)) | |
414 | (beginning-of-line))))) | |
415 | ||
cd632e57 SM |
416 | (defun diff-count-matches (re start end) |
417 | (save-excursion | |
418 | (let ((n 0)) | |
419 | (goto-char start) | |
420 | (while (re-search-forward re end t) (incf n)) | |
421 | n))) | |
422 | ||
423 | (defun diff-split-hunk () | |
424 | "Split the current (unified diff) hunk at point into two hunks." | |
425 | (interactive) | |
426 | (beginning-of-line) | |
427 | (let ((pos (point)) | |
428 | (start (progn (diff-beginning-of-hunk) (point)))) | |
429 | (unless (looking-at "@@ -\\([0-9]+\\),[0-9]+ \\+\\([0-9]+\\),[0-9]+ @@") | |
430 | (error "diff-split-hunk only works on unified context diffs")) | |
431 | (forward-line 1) | |
432 | (let* ((start1 (string-to-number (match-string 1))) | |
433 | (start2 (string-to-number (match-string 2))) | |
434 | (newstart1 (+ start1 (diff-count-matches "^[- \t]" (point) pos))) | |
435 | (newstart2 (+ start2 (diff-count-matches "^[+ \t]" (point) pos)))) | |
436 | (goto-char pos) | |
437 | ;; Hopefully the after-change-function will not screw us over. | |
438 | (insert "@@ -" (number-to-string newstart1) ",1 +" | |
439 | (number-to-string newstart2) ",1 @@\n") | |
440 | ;; Fix the original hunk-header. | |
441 | (diff-fixup-modifs start pos)))) | |
71296446 | 442 | |
cd632e57 | 443 | |
610a6418 SM |
444 | ;;;; |
445 | ;;;; jump to other buffers | |
446 | ;;;; | |
447 | ||
0b82e382 SM |
448 | (defvar diff-remembered-files-alist nil) |
449 | ||
610a6418 SM |
450 | (defun diff-filename-drop-dir (file) |
451 | (when (string-match "/" file) (substring file (match-end 0)))) | |
452 | ||
0b82e382 SM |
453 | (defun diff-merge-strings (ancestor from to) |
454 | "Merge the diff between ANCESTOR and FROM into TO. | |
455 | Returns the merged string if successful or nil otherwise. | |
d87e5627 | 456 | The strings are assumed not to contain any \"\\n\" (i.e. end of line). |
0b82e382 SM |
457 | If ANCESTOR = FROM, returns TO. |
458 | If ANCESTOR = TO, returns FROM. | |
459 | The heuristic is simplistic and only really works for cases | |
460 | like \(diff-merge-strings \"b/foo\" \"b/bar\" \"/a/c/foo\")." | |
461 | ;; Ideally, we want: | |
462 | ;; AMB ANB CMD -> CND | |
463 | ;; but that's ambiguous if `foo' or `bar' is empty: | |
464 | ;; a/foo a/foo1 b/foo.c -> b/foo1.c but not 1b/foo.c or b/foo.c1 | |
d87e5627 | 465 | (let ((str (concat ancestor "\n" from "\n" to))) |
0b82e382 | 466 | (when (and (string-match (concat |
d87e5627 SM |
467 | "\\`\\(.*?\\)\\(.*\\)\\(.*\\)\n" |
468 | "\\1\\(.*\\)\\3\n" | |
0b82e382 SM |
469 | "\\(.*\\(\\2\\).*\\)\\'") str) |
470 | (equal to (match-string 5 str))) | |
471 | (concat (substring str (match-beginning 5) (match-beginning 6)) | |
472 | (match-string 4 str) | |
473 | (substring str (match-end 6) (match-end 5)))))) | |
474 | ||
22cd1973 SM |
475 | (defun diff-tell-file-name (old name) |
476 | "Tell Emacs where the find the source file of the current hunk. | |
477 | If the OLD prefix arg is passed, tell the file NAME of the old file." | |
478 | (interactive | |
479 | (let* ((old current-prefix-arg) | |
480 | (fs (diff-hunk-file-names current-prefix-arg))) | |
481 | (unless fs (error "No file name to look for")) | |
482 | (list old (read-file-name (format "File for %s: " (car fs)) | |
483 | nil (diff-find-file-name old) t)))) | |
484 | (let ((fs (diff-hunk-file-names old))) | |
485 | (unless fs (error "No file name to look for")) | |
486 | (push (cons fs name) diff-remembered-files-alist))) | |
487 | ||
488 | (defun diff-hunk-file-names (&optional old) | |
489 | "Give the list of file names textually mentioned for the current hunk." | |
610a6418 SM |
490 | (save-excursion |
491 | (unless (looking-at diff-file-header-re) | |
492 | (or (ignore-errors (diff-beginning-of-file)) | |
493 | (re-search-forward diff-file-header-re nil t))) | |
22cd1973 | 494 | (let ((limit (save-excursion |
610a6418 | 495 | (condition-case () |
d87e5627 | 496 | (progn (diff-hunk-prev) (point)) |
610a6418 | 497 | (error (point-min))))) |
22cd1973 SM |
498 | (header-files |
499 | (if (looking-at "[-*][-*][-*] \\(\\S-+\\)\\(\\s-.*\\)?\n[-+][-+][-+] \\(\\S-+\\)") | |
500 | (list (if old (match-string 1) (match-string 3)) | |
501 | (if old (match-string 3) (match-string 1))) | |
502 | (forward-line 1) nil))) | |
503 | (delq nil | |
504 | (append | |
505 | (when (and (not old) | |
506 | (save-excursion | |
507 | (re-search-backward "^Index: \\(.+\\)" limit t))) | |
508 | (list (match-string 1))) | |
509 | header-files | |
510 | (when (re-search-backward | |
511 | "^diff \\(-\\S-+ +\\)*\\(\\S-+\\)\\( +\\(\\S-+\\)\\)?" | |
512 | nil t) | |
513 | (list (if old (match-string 2) (match-string 4)) | |
514 | (if old (match-string 4) (match-string 2))))))))) | |
515 | ||
516 | (defun diff-find-file-name (&optional old) | |
517 | "Return the file corresponding to the current patch. | |
518 | Non-nil OLD means that we want the old file." | |
519 | (save-excursion | |
520 | (unless (looking-at diff-file-header-re) | |
521 | (or (ignore-errors (diff-beginning-of-file)) | |
522 | (re-search-forward diff-file-header-re nil t))) | |
523 | (let ((fs (diff-hunk-file-names old))) | |
610a6418 | 524 | (or |
0b82e382 SM |
525 | ;; use any previously used preference |
526 | (cdr (assoc fs diff-remembered-files-alist)) | |
527 | ;; try to be clever and use previous choices as an inspiration | |
528 | (dolist (rf diff-remembered-files-alist) | |
529 | (let ((newfile (diff-merge-strings (caar rf) (car fs) (cdr rf)))) | |
530 | (if (and newfile (file-exists-p newfile)) (return newfile)))) | |
531 | ;; look for each file in turn. If none found, try again but | |
532 | ;; ignoring the first level of directory, ... | |
533 | (do* ((files fs (delq nil (mapcar 'diff-filename-drop-dir files))) | |
534 | (file nil nil)) | |
535 | ((or (null files) | |
536 | (setq file (do* ((files files (cdr files)) | |
537 | (file (car files) (car files))) | |
538 | ((or (null file) (file-exists-p file)) | |
539 | file)))) | |
540 | file)) | |
541 | ;; <foo>.rej patches implicitly apply to <foo> | |
610a6418 SM |
542 | (and (string-match "\\.rej\\'" (or buffer-file-name "")) |
543 | (let ((file (substring buffer-file-name 0 (match-beginning 0)))) | |
544 | (when (file-exists-p file) file))) | |
0b82e382 SM |
545 | ;; if all else fails, ask the user |
546 | (let ((file (read-file-name (format "Use file %s: " (or (first fs) "")) | |
547 | nil (first fs) t (first fs)))) | |
548 | (set (make-local-variable 'diff-remembered-files-alist) | |
549 | (cons (cons fs file) diff-remembered-files-alist)) | |
610a6418 SM |
550 | file))))) |
551 | ||
1b1b5dae | 552 | |
610a6418 SM |
553 | (defun diff-ediff-patch () |
554 | "Call `ediff-patch-file' on the current buffer." | |
555 | (interactive) | |
556 | (condition-case err | |
0e104800 | 557 | (ediff-patch-file nil (current-buffer)) |
610a6418 SM |
558 | (wrong-number-of-arguments (ediff-patch-file)))) |
559 | ||
71296446 | 560 | ;;;; |
610a6418 | 561 | ;;;; Conversion functions |
71296446 | 562 | ;;;; |
610a6418 SM |
563 | |
564 | ;;(defvar diff-inhibit-after-change nil | |
565 | ;; "Non-nil means inhibit `diff-mode's after-change functions.") | |
566 | ||
567 | (defun diff-unified->context (start end) | |
568 | "Convert unified diffs to context diffs. | |
569 | START and END are either taken from the region (if a prefix arg is given) or | |
570 | else cover the whole bufer." | |
571 | (interactive (if current-prefix-arg | |
572 | (list (mark) (point)) | |
573 | (list (point-min) (point-max)))) | |
574 | (unless (markerp end) (setq end (copy-marker end))) | |
575 | (let (;;(diff-inhibit-after-change t) | |
576 | (inhibit-read-only t)) | |
577 | (save-excursion | |
578 | (goto-char start) | |
c0078a04 | 579 | (while (and (re-search-forward "^\\(\\(---\\) .+\n\\(\\+\\+\\+\\) .+\\|@@ -\\([0-9]+\\),\\([0-9]+\\) \\+\\([0-9]+\\),\\([0-9]+\\) @@.*\\)$" nil t) |
610a6418 SM |
580 | (< (point) end)) |
581 | (combine-after-change-calls | |
582 | (if (match-beginning 2) | |
0e104800 | 583 | ;; we matched a file header |
610a6418 SM |
584 | (progn |
585 | ;; use reverse order to make sure the indices are kept valid | |
586 | (replace-match "---" t t nil 3) | |
587 | (replace-match "***" t t nil 2)) | |
588 | ;; we matched a hunk header | |
589 | (let ((line1 (match-string 4)) | |
590 | (lines1 (match-string 5)) | |
591 | (line2 (match-string 6)) | |
592 | (lines2 (match-string 7))) | |
593 | (replace-match | |
594 | (concat "***************\n*** " line1 "," | |
595 | (number-to-string (+ (string-to-number line1) | |
596 | (string-to-number lines1) | |
597 | -1)) " ****")) | |
598 | (forward-line 1) | |
599 | (save-restriction | |
600 | (narrow-to-region (point) | |
601 | (progn (diff-end-of-hunk 'unified) (point))) | |
602 | (let ((hunk (buffer-string))) | |
603 | (goto-char (point-min)) | |
604 | (if (not (save-excursion (re-search-forward "^-" nil t))) | |
605 | (delete-region (point) (point-max)) | |
606 | (goto-char (point-max)) | |
607 | (let ((modif nil) last-pt) | |
608 | (while (progn (setq last-pt (point)) | |
609 | (= (forward-line -1) 0)) | |
610 | (case (char-after) | |
611 | (? (insert " ") (setq modif nil) (backward-char 1)) | |
612 | (?+ (delete-region (point) last-pt) (setq modif t)) | |
613 | (?- (if (not modif) | |
614 | (progn (forward-char 1) | |
615 | (insert " ")) | |
616 | (delete-char 1) | |
617 | (insert "! ")) | |
618 | (backward-char 2)) | |
619 | (?\\ (when (save-excursion (forward-line -1) | |
620 | (= (char-after) ?+)) | |
621 | (delete-region (point) last-pt) (setq modif t))) | |
622 | (t (setq modif nil)))))) | |
623 | (goto-char (point-max)) | |
624 | (save-excursion | |
625 | (insert "--- " line2 "," | |
626 | (number-to-string (+ (string-to-number line2) | |
627 | (string-to-number lines2) | |
628 | -1)) " ----\n" hunk)) | |
629 | ;;(goto-char (point-min)) | |
630 | (forward-line 1) | |
631 | (if (not (save-excursion (re-search-forward "^+" nil t))) | |
632 | (delete-region (point) (point-max)) | |
633 | (let ((modif nil) (delete nil)) | |
634 | (while (not (eobp)) | |
635 | (case (char-after) | |
636 | (? (insert " ") (setq modif nil) (backward-char 1)) | |
637 | (?- (setq delete t) (setq modif t)) | |
638 | (?+ (if (not modif) | |
639 | (progn (forward-char 1) | |
640 | (insert " ")) | |
641 | (delete-char 1) | |
642 | (insert "! ")) | |
643 | (backward-char 2)) | |
644 | (?\\ (when (save-excursion (forward-line 1) | |
645 | (not (eobp))) | |
646 | (setq delete t) (setq modif t))) | |
647 | (t (setq modif nil))) | |
648 | (let ((last-pt (point))) | |
649 | (forward-line 1) | |
650 | (when delete | |
651 | (delete-region last-pt (point)) | |
652 | (setq delete nil))))))))))))))) | |
653 | ||
654 | (defun diff-context->unified (start end) | |
655 | "Convert context diffs to unified diffs. | |
656 | START and END are either taken from the region (if a prefix arg is given) or | |
657 | else cover the whole bufer." | |
658 | (interactive (if current-prefix-arg | |
659 | (list (mark) (point)) | |
660 | (list (point-min) (point-max)))) | |
661 | (unless (markerp end) (setq end (copy-marker end))) | |
662 | (let (;;(diff-inhibit-after-change t) | |
663 | (inhibit-read-only t)) | |
664 | (save-excursion | |
665 | (goto-char start) | |
c0078a04 | 666 | (while (and (re-search-forward "^\\(\\(\\*\\*\\*\\) .+\n\\(---\\) .+\\|\\*\\{15\\}.*\n\\*\\*\\* \\([0-9]+\\),\\(-?[0-9]+\\) \\*\\*\\*\\*\\)$" nil t) |
610a6418 SM |
667 | (< (point) end)) |
668 | (combine-after-change-calls | |
669 | (if (match-beginning 2) | |
670 | ;; we matched a file header | |
671 | (progn | |
672 | ;; use reverse order to make sure the indices are kept valid | |
673 | (replace-match "+++" t t nil 3) | |
674 | (replace-match "---" t t nil 2)) | |
675 | ;; we matched a hunk header | |
676 | (let ((line1s (match-string 4)) | |
677 | (line1e (match-string 5)) | |
678 | (pt1 (match-beginning 0))) | |
679 | (replace-match "") | |
680 | (unless (re-search-forward | |
681 | "^--- \\([0-9]+\\),\\(-?[0-9]+\\) ----$" nil t) | |
682 | (error "Can't find matching `--- n1,n2 ----' line")) | |
683 | (let ((line2s (match-string 1)) | |
684 | (line2e (match-string 2)) | |
685 | (pt2 (progn | |
686 | (delete-region (progn (beginning-of-line) (point)) | |
687 | (progn (forward-line 1) (point))) | |
688 | (point-marker)))) | |
689 | (goto-char pt1) | |
690 | (forward-line 1) | |
691 | (while (< (point) pt2) | |
692 | (case (char-after) | |
693 | ((?! ?-) (delete-char 2) (insert "-") (forward-line 1)) | |
694 | (?\ ;merge with the other half of the chunk | |
695 | (let* ((endline2 | |
696 | (save-excursion | |
697 | (goto-char pt2) (forward-line 1) (point))) | |
698 | (c (char-after pt2))) | |
699 | (case c | |
700 | ((?! ?+) | |
701 | (insert "+" | |
702 | (prog1 (buffer-substring (+ pt2 2) endline2) | |
703 | (delete-region pt2 endline2)))) | |
704 | (?\ ;FIXME: check consistency | |
705 | (delete-region pt2 endline2) | |
706 | (delete-char 1) | |
707 | (forward-line 1)) | |
708 | (?\\ (forward-line 1)) | |
709 | (t (delete-char 1) (forward-line 1))))) | |
710 | (t (forward-line 1)))) | |
711 | (while (looking-at "[+! ] ") | |
712 | (if (/= (char-after) ?!) (forward-char 1) | |
713 | (delete-char 1) (insert "+")) | |
714 | (delete-char 1) (forward-line 1)) | |
715 | (save-excursion | |
716 | (goto-char pt1) | |
717 | (insert "@@ -" line1s "," | |
718 | (number-to-string (- (string-to-number line1e) | |
719 | (string-to-number line1s) | |
720 | -1)) | |
721 | " +" line2s "," | |
722 | (number-to-string (- (string-to-number line2e) | |
723 | (string-to-number line2s) | |
724 | -1)) " @@")))))))))) | |
725 | ||
726 | (defun diff-reverse-direction (start end) | |
727 | "Reverse the direction of the diffs. | |
728 | START and END are either taken from the region (if a prefix arg is given) or | |
729 | else cover the whole bufer." | |
730 | (interactive (if current-prefix-arg | |
731 | (list (mark) (point)) | |
732 | (list (point-min) (point-max)))) | |
733 | (unless (markerp end) (setq end (copy-marker end))) | |
734 | (let (;;(diff-inhibit-after-change t) | |
735 | (inhibit-read-only t)) | |
736 | (save-excursion | |
737 | (goto-char start) | |
c0078a04 | 738 | (while (and (re-search-forward "^\\(\\([-*][-*][-*] \\)\\(.+\\)\n\\([-+][-+][-+] \\)\\(.+\\)\\|\\*\\{15\\}.*\n\\*\\*\\* \\(.+\\) \\*\\*\\*\\*\\|@@ -\\([0-9,]+\\) \\+\\([0-9,]+\\) @@.*\\)$" nil t) |
610a6418 SM |
739 | (< (point) end)) |
740 | (combine-after-change-calls | |
741 | (cond | |
742 | ;; a file header | |
743 | ((match-beginning 2) (replace-match "\\2\\5\n\\4\\3" nil)) | |
744 | ;; a context-diff hunk header | |
745 | ((match-beginning 6) | |
746 | (let ((pt-lines1 (match-beginning 6)) | |
747 | (lines1 (match-string 6))) | |
748 | (replace-match "" nil nil nil 6) | |
749 | (forward-line 1) | |
750 | (let ((half1s (point))) | |
0e104800 | 751 | (while (looking-at "[-! \\][ \t]\\|#") |
610a6418 SM |
752 | (when (= (char-after) ?-) (delete-char 1) (insert "+")) |
753 | (forward-line 1)) | |
fef8c55b | 754 | (let ((half1 (delete-and-extract-region half1s (point)))) |
610a6418 SM |
755 | (unless (looking-at "^--- \\([0-9]+,-?[0-9]+\\) ----$") |
756 | (insert half1) | |
757 | (error "Can't find matching `--- n1,n2 ----' line")) | |
758 | (let ((str1 (match-string 1))) | |
759 | (replace-match lines1 nil nil nil 1) | |
760 | (forward-line 1) | |
761 | (let ((half2s (point))) | |
0e104800 | 762 | (while (looking-at "[!+ \\][ \t]\\|#") |
610a6418 SM |
763 | (when (= (char-after) ?+) (delete-char 1) (insert "-")) |
764 | (forward-line 1)) | |
fef8c55b | 765 | (let ((half2 (delete-and-extract-region half2s (point)))) |
0e104800 | 766 | (insert (or half1 "")) |
610a6418 | 767 | (goto-char half1s) |
0e104800 | 768 | (insert (or half2 "")))) |
610a6418 SM |
769 | (goto-char pt-lines1) |
770 | (insert str1)))))) | |
771 | ;; a unified-diff hunk header | |
772 | ((match-beginning 7) | |
773 | (replace-match "@@ -\\8 +\\7 @@" nil) | |
774 | (forward-line 1) | |
775 | (let ((c (char-after)) first last) | |
776 | (while (case (setq c (char-after)) | |
777 | (?- (setq first (or first (point))) | |
778 | (delete-char 1) (insert "+") t) | |
779 | (?+ (setq last (or last (point))) | |
780 | (delete-char 1) (insert "-") t) | |
0e104800 | 781 | ((?\\ ?#) t) |
610a6418 | 782 | (t (when (and first last (< first last)) |
cb4e8176 | 783 | (insert (delete-and-extract-region first last))) |
610a6418 SM |
784 | (setq first nil last nil) |
785 | (equal ?\ c))) | |
786 | (forward-line 1)))))))))) | |
787 | ||
788 | (defun diff-fixup-modifs (start end) | |
789 | "Fixup the hunk headers (in case the buffer was modified). | |
790 | START and END are either taken from the region (if a prefix arg is given) or | |
791 | else cover the whole bufer." | |
792 | (interactive (if current-prefix-arg | |
793 | (list (mark) (point)) | |
794 | (list (point-min) (point-max)))) | |
795 | (let ((inhibit-read-only t)) | |
796 | (save-excursion | |
797 | (goto-char end) (diff-end-of-hunk) | |
798 | (let ((plus 0) (minus 0) (space 0) (bang 0)) | |
799 | (while (and (= (forward-line -1) 0) (<= start (point))) | |
bd9d7d76 SM |
800 | (if (not (looking-at |
801 | (concat "@@ -[0-9,]+ \\+[0-9,]+ @@" | |
802 | "\\|[-*][-*][-*] [0-9,]+ [-*][-*][-*][-*]$" | |
803 | "\\|--- .+\n\\+\\+\\+ "))) | |
610a6418 | 804 | (case (char-after) |
bd9d7d76 | 805 | (?\s (incf space)) |
610a6418 SM |
806 | (?+ (incf plus)) |
807 | (?- (incf minus)) | |
808 | (?! (incf bang)) | |
0e104800 | 809 | ((?\\ ?#) nil) |
610a6418 SM |
810 | (t (setq space 0 plus 0 minus 0 bang 0))) |
811 | (cond | |
c0078a04 | 812 | ((looking-at "@@ -[0-9]+,\\([0-9]*\\) \\+[0-9]+,\\([0-9]*\\) @@.*$") |
610a6418 SM |
813 | (let* ((old1 (match-string 1)) |
814 | (old2 (match-string 2)) | |
815 | (new1 (number-to-string (+ space minus))) | |
816 | (new2 (number-to-string (+ space plus)))) | |
817 | (unless (string= new2 old2) (replace-match new2 t t nil 2)) | |
818 | (unless (string= new1 old1) (replace-match new1 t t nil 1)))) | |
819 | ((looking-at "--- \\([0-9]+\\),\\([0-9]*\\) ----$") | |
820 | (when (> (+ space bang plus) 0) | |
821 | (let* ((old1 (match-string 1)) | |
822 | (old2 (match-string 2)) | |
823 | (new (number-to-string | |
824 | (+ space bang plus -1 (string-to-number old1))))) | |
825 | (unless (string= new old2) (replace-match new t t nil 2))))) | |
826 | ((looking-at "\\*\\*\\* \\([0-9]+\\),\\(-?[0-9]*\\) \\*\\*\\*\\*$") | |
827 | (when (> (+ space bang minus) 0) | |
828 | (let* ((old (match-string 1)) | |
829 | (new (format | |
830 | (concat "%0" (number-to-string (length old)) "d") | |
831 | (+ space bang minus -1 (string-to-number old))))) | |
832 | (unless (string= new old) (replace-match new t t nil 2)))))) | |
833 | (setq space 0 plus 0 minus 0 bang 0))))))) | |
834 | ||
71296446 | 835 | ;;;; |
610a6418 | 836 | ;;;; Hooks |
71296446 | 837 | ;;;; |
610a6418 SM |
838 | |
839 | (defun diff-write-contents-hooks () | |
840 | "Fixup hunk headers if necessary." | |
841 | (if (buffer-modified-p) (diff-fixup-modifs (point-min) (point-max))) | |
842 | nil) | |
843 | ||
610a6418 SM |
844 | ;; It turns out that making changes in the buffer from within an |
845 | ;; *-change-function is asking for trouble, whereas making them | |
846 | ;; from a post-command-hook doesn't pose much problems | |
847 | (defvar diff-unhandled-changes nil) | |
848 | (defun diff-after-change-function (beg end len) | |
849 | "Remember to fixup the hunk header. | |
850 | See `after-change-functions' for the meaning of BEG, END and LEN." | |
c0078a04 SM |
851 | ;; Ignoring changes when inhibit-read-only is set is strictly speaking |
852 | ;; incorrect, but it turns out that inhibit-read-only is normally not set | |
853 | ;; inside editing commands, while it tends to be set when the buffer gets | |
854 | ;; updated by an async process or by a conversion function, both of which | |
855 | ;; would rather not be uselessly slowed down by this hook. | |
610a6418 SM |
856 | (when (and (not undo-in-progress) (not inhibit-read-only)) |
857 | (if diff-unhandled-changes | |
858 | (setq diff-unhandled-changes | |
859 | (cons (min beg (car diff-unhandled-changes)) | |
cb4e8176 | 860 | (max end (cdr diff-unhandled-changes)))) |
610a6418 SM |
861 | (setq diff-unhandled-changes (cons beg end))))) |
862 | ||
863 | (defun diff-post-command-hook () | |
864 | "Fixup hunk headers if necessary." | |
865 | (when (consp diff-unhandled-changes) | |
866 | (ignore-errors | |
867 | (save-excursion | |
fef8c55b | 868 | (goto-char (car diff-unhandled-changes)) |
cb4e8176 SM |
869 | ;; Maybe we've cut the end of the hunk before point. |
870 | (if (and (bolp) (not (bobp))) (backward-char 1)) | |
1eabc5e6 SM |
871 | ;; We used to fixup modifs on all the changes, but it turns out |
872 | ;; that it's safer not to do it on big changes, for example | |
873 | ;; when yanking a big diff, since we might then screw up perfectly | |
874 | ;; correct values. -stef | |
875 | ;; (unless (ignore-errors | |
876 | ;; (diff-beginning-of-hunk) | |
877 | ;; (save-excursion | |
878 | ;; (diff-end-of-hunk) | |
879 | ;; (> (point) (car diff-unhandled-changes)))) | |
880 | ;; (goto-char (car diff-unhandled-changes)) | |
881 | ;; (re-search-forward diff-hunk-header-re (cdr diff-unhandled-changes)) | |
882 | ;; (diff-beginning-of-hunk)) | |
883 | ;; (diff-fixup-modifs (point) (cdr diff-unhandled-changes)) | |
884 | (diff-beginning-of-hunk) | |
885 | (when (save-excursion | |
886 | (diff-end-of-hunk) | |
cb4e8176 | 887 | (>= (point) (cdr diff-unhandled-changes))) |
1eabc5e6 | 888 | (diff-fixup-modifs (point) (cdr diff-unhandled-changes))))) |
610a6418 SM |
889 | (setq diff-unhandled-changes nil))) |
890 | ||
6fc049f6 SM |
891 | (defun diff-next-error (arg reset) |
892 | ;; Select a window that displays the current buffer so that point | |
893 | ;; movements are reflected in that window. Otherwise, the user might | |
894 | ;; never see the hunk corresponding to the source she's jumping to. | |
895 | (pop-to-buffer (current-buffer)) | |
896 | (if reset (goto-char (point-min))) | |
897 | (diff-hunk-next arg) | |
898 | (diff-goto-source)) | |
610a6418 | 899 | |
610a6418 | 900 | ;;;###autoload |
d87e5627 | 901 | (define-derived-mode diff-mode fundamental-mode "Diff" |
0b82e382 | 902 | "Major mode for viewing/editing context diffs. |
769dd0f1 SM |
903 | Supports unified and context diffs as well as (to a lesser extent) |
904 | normal diffs. | |
df52e0e7 SM |
905 | When the buffer is read-only, the ESC prefix is not necessary. |
906 | IF you edit the buffer manually, diff-mode will try to update the hunk | |
907 | headers for you on-the-fly. | |
908 | ||
909 | You can also switch between context diff and unified diff with \\[diff-context->unified], | |
910 | or vice versa with \\[diff-unified->context] and you can also revert the direction of | |
911 | a diff with \\[diff-reverse-direction]." | |
610a6418 SM |
912 | (set (make-local-variable 'font-lock-defaults) diff-font-lock-defaults) |
913 | (set (make-local-variable 'outline-regexp) diff-outline-regexp) | |
ccce6558 DL |
914 | (set (make-local-variable 'imenu-generic-expression) |
915 | diff-imenu-generic-expression) | |
19e713d8 DL |
916 | ;; These are not perfect. They would be better done separately for |
917 | ;; context diffs and unidiffs. | |
918 | ;; (set (make-local-variable 'paragraph-start) | |
919 | ;; (concat "@@ " ; unidiff hunk | |
920 | ;; "\\|\\*\\*\\* " ; context diff hunk or file start | |
921 | ;; "\\|--- [^\t]+\t")) ; context or unidiff file | |
922 | ;; ; start (first or second line) | |
923 | ;; (set (make-local-variable 'paragraph-separate) paragraph-start) | |
924 | ;; (set (make-local-variable 'page-delimiter) "--- [^\t]+\t") | |
925 | ;; compile support | |
6fc049f6 | 926 | (set (make-local-variable 'next-error-function) 'diff-next-error) |
cdbb79c1 | 927 | |
769dd0f1 | 928 | (when (and (> (point-max) (point-min)) diff-default-read-only) |
2b960ac0 | 929 | (toggle-read-only t)) |
769dd0f1 | 930 | ;; setup change hooks |
e8a1ed31 | 931 | (if (not diff-update-on-the-fly) |
3ccdb14e | 932 | (add-hook 'write-contents-functions 'diff-write-contents-hooks nil t) |
610a6418 | 933 | (make-local-variable 'diff-unhandled-changes) |
3cec9c57 SM |
934 | (add-hook 'after-change-functions 'diff-after-change-function nil t) |
935 | (add-hook 'post-command-hook 'diff-post-command-hook nil t)) | |
610a6418 | 936 | ;; Neat trick from Dave Love to add more bindings in read-only mode: |
22cd1973 SM |
937 | (let ((ro-bind (cons 'buffer-read-only diff-mode-shared-map))) |
938 | (add-to-list 'minor-mode-overriding-map-alist ro-bind) | |
939 | ;; Turn off this little trick in case the buffer is put in view-mode. | |
940 | (add-hook 'view-mode-hook | |
941 | `(lambda () | |
942 | (setq minor-mode-overriding-map-alist | |
5c84686c | 943 | (delq ',ro-bind minor-mode-overriding-map-alist))) |
22cd1973 | 944 | nil t)) |
281096ed SM |
945 | ;; add-log support |
946 | (set (make-local-variable 'add-log-current-defun-function) | |
947 | 'diff-current-defun) | |
1b1b5dae SM |
948 | (set (make-local-variable 'add-log-buffer-file-name-function) |
949 | 'diff-find-file-name)) | |
610a6418 SM |
950 | |
951 | ;;;###autoload | |
0b82e382 SM |
952 | (define-minor-mode diff-minor-mode |
953 | "Minor mode for viewing/editing context diffs. | |
954 | \\{diff-minor-mode-map}" | |
955 | nil " Diff" nil | |
956 | ;; FIXME: setup font-lock | |
fef8c55b | 957 | ;; setup change hooks |
e8a1ed31 | 958 | (if (not diff-update-on-the-fly) |
3ccdb14e | 959 | (add-hook 'write-contents-functions 'diff-write-contents-hooks nil t) |
fef8c55b | 960 | (make-local-variable 'diff-unhandled-changes) |
3cec9c57 SM |
961 | (add-hook 'after-change-functions 'diff-after-change-function nil t) |
962 | (add-hook 'post-command-hook 'diff-post-command-hook nil t))) | |
0b82e382 | 963 | |
22cd1973 SM |
964 | ;;; Handy hook functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
965 | ||
966 | (defun diff-delete-if-empty () | |
967 | ;; An empty diff file means there's no more diffs to integrate, so we | |
968 | ;; can just remove the file altogether. Very handy for .rej files if we | |
969 | ;; remove hunks as we apply them. | |
970 | (when (and buffer-file-name | |
971 | (eq 0 (nth 7 (file-attributes buffer-file-name)))) | |
972 | (delete-file buffer-file-name))) | |
973 | ||
974 | (defun diff-delete-empty-files () | |
975 | "Arrange for empty diff files to be removed." | |
976 | (add-hook 'after-save-hook 'diff-delete-if-empty nil t)) | |
977 | ||
978 | (defun diff-make-unified () | |
979 | "Turn context diffs into unified diffs if applicable." | |
980 | (if (save-excursion | |
981 | (goto-char (point-min)) | |
4c174fb4 | 982 | (and (looking-at diff-hunk-header-re) (eq (char-after) ?*))) |
22cd1973 SM |
983 | (let ((mod (buffer-modified-p))) |
984 | (unwind-protect | |
985 | (diff-context->unified (point-min) (point-max)) | |
986 | (restore-buffer-modified-p mod))))) | |
610a6418 | 987 | |
027ac3f8 SM |
988 | ;;; |
989 | ;;; Misc operations that have proved useful at some point. | |
990 | ;;; | |
991 | ||
992 | (defun diff-next-complex-hunk () | |
993 | "Jump to the next \"complex\" hunk. | |
994 | \"Complex\" is approximated by \"the hunk changes the number of lines\". | |
995 | Only works for unified diffs." | |
996 | (interactive) | |
997 | (while | |
998 | (and (re-search-forward "^@@ [-0-9]+,\\([0-9]+\\) [+0-9]+,\\([0-9]+\\) @@" | |
999 | nil t) | |
1000 | (equal (match-string 1) (match-string 2))))) | |
1001 | ||
1eabc5e6 SM |
1002 | (defun diff-hunk-text (hunk destp char-offset) |
1003 | "Return the literal source text from HUNK as (TEXT . OFFSET). | |
1004 | if DESTP is nil TEXT is the source, otherwise the destination text. | |
1005 | CHAR-OFFSET is a char-offset in HUNK, and OFFSET is the corresponding | |
1006 | char-offset in TEXT." | |
3017133f | 1007 | (with-temp-buffer |
6e4e8a3b SM |
1008 | (insert hunk) |
1009 | (goto-char (point-min)) | |
1010 | (let ((src-pos nil) | |
1011 | (dst-pos nil) | |
1012 | (divider-pos nil) | |
1013 | (num-pfx-chars 2)) | |
1014 | ;; Set the following variables: | |
1015 | ;; SRC-POS buffer pos of the source part of the hunk or nil if none | |
1016 | ;; DST-POS buffer pos of the destination part of the hunk or nil | |
1017 | ;; DIVIDER-POS buffer pos of any divider line separating the src & dst | |
1018 | ;; NUM-PFX-CHARS number of line-prefix characters used by this format" | |
1019 | (cond ((looking-at "^@@") | |
1020 | ;; unified diff | |
1021 | (setq num-pfx-chars 1) | |
1022 | (forward-line 1) | |
1023 | (setq src-pos (point) dst-pos (point))) | |
1024 | ((looking-at "^\\*\\*") | |
1025 | ;; context diff | |
1026 | (forward-line 2) | |
1027 | (setq src-pos (point)) | |
1028 | (re-search-forward "^--- " nil t) | |
1029 | (forward-line 0) | |
1030 | (setq divider-pos (point)) | |
1031 | (forward-line 1) | |
1032 | (setq dst-pos (point))) | |
1033 | ((looking-at "^[0-9]+a[0-9,]+$") | |
1034 | ;; normal diff, insert | |
1035 | (forward-line 1) | |
1036 | (setq dst-pos (point))) | |
1037 | ((looking-at "^[0-9,]+d[0-9]+$") | |
1038 | ;; normal diff, delete | |
1039 | (forward-line 1) | |
1040 | (setq src-pos (point))) | |
1041 | ((looking-at "^[0-9,]+c[0-9,]+$") | |
1042 | ;; normal diff, change | |
1043 | (forward-line 1) | |
1044 | (setq src-pos (point)) | |
1045 | (re-search-forward "^---$" nil t) | |
1046 | (forward-line 0) | |
1047 | (setq divider-pos (point)) | |
1048 | (forward-line 1) | |
1049 | (setq dst-pos (point))) | |
1050 | (t | |
1051 | (error "Unknown diff hunk type"))) | |
1052 | ||
1053 | (if (if destp (null dst-pos) (null src-pos)) | |
1054 | ;; Implied empty text | |
1055 | (if char-offset '("" . 0) "") | |
1056 | ||
1057 | ;; For context diffs, either side can be empty, (if there's only | |
1058 | ;; added or only removed text). We should then use the other side. | |
1059 | (cond ((equal src-pos divider-pos) (setq src-pos dst-pos)) | |
1060 | ((equal dst-pos (point-max)) (setq dst-pos src-pos))) | |
1061 | ||
1062 | (when char-offset (goto-char (+ (point-min) char-offset))) | |
1063 | ||
1064 | ;; Get rid of anything except the desired text. | |
1065 | (save-excursion | |
1066 | ;; Delete unused text region | |
1067 | (let ((keep (if destp dst-pos src-pos))) | |
1068 | (when (and divider-pos (> divider-pos keep)) | |
1069 | (delete-region divider-pos (point-max))) | |
1070 | (delete-region (point-min) keep)) | |
1071 | ;; Remove line-prefix characters, and unneeded lines (unified diffs). | |
1072 | (let ((kill-char (if destp ?- ?+))) | |
1073 | (goto-char (point-min)) | |
1074 | (while (not (eobp)) | |
1075 | (if (eq (char-after) kill-char) | |
1076 | (delete-region (point) (progn (forward-line 1) (point))) | |
1077 | (delete-char num-pfx-chars) | |
1078 | (forward-line 1))))) | |
1079 | ||
1080 | (let ((text (buffer-substring-no-properties (point-min) (point-max)))) | |
1081 | (if char-offset (cons text (- (point) (point-min))) text)))))) | |
370d860c | 1082 | |
7530b6da | 1083 | |
c0aa75f7 | 1084 | (defun diff-find-text (text) |
1eabc5e6 | 1085 | "Return the buffer position (BEG . END) of the nearest occurrence of TEXT. |
7530b6da | 1086 | If TEXT isn't found, nil is returned." |
7530b6da MB |
1087 | (let* ((orig (point)) |
1088 | (forw (and (search-forward text nil t) | |
1eabc5e6 | 1089 | (cons (match-beginning 0) (match-end 0)))) |
7530b6da MB |
1090 | (back (and (goto-char (+ orig (length text))) |
1091 | (search-backward text nil t) | |
1eabc5e6 SM |
1092 | (cons (match-beginning 0) (match-end 0))))) |
1093 | ;; Choose the closest match. | |
1094 | (if (and forw back) | |
1095 | (if (> (- (car forw) orig) (- orig (car back))) back forw) | |
1096 | (or back forw)))) | |
1097 | ||
1098 | (defun diff-find-approx-text (text) | |
1099 | "Return the buffer position (BEG . END) of the nearest occurrence of TEXT. | |
1100 | Whitespace differences are ignored." | |
1101 | (let* ((orig (point)) | |
1102 | (re (concat "^[ \t\n\f]*" | |
1103 | (mapconcat 'regexp-quote (split-string text) "[ \t\n\f]+") | |
1104 | "[ \t\n\f]*\n")) | |
1105 | (forw (and (re-search-forward re nil t) | |
1106 | (cons (match-beginning 0) (match-end 0)))) | |
1107 | (back (and (goto-char (+ orig (length text))) | |
1108 | (re-search-backward re nil t) | |
1109 | (cons (match-beginning 0) (match-end 0))))) | |
1110 | ;; Choose the closest match. | |
7530b6da | 1111 | (if (and forw back) |
1eabc5e6 | 1112 | (if (> (- (car forw) orig) (- orig (car back))) back forw) |
7530b6da MB |
1113 | (or back forw)))) |
1114 | ||
370d860c SM |
1115 | (defsubst diff-xor (a b) (if a (not b) b)) |
1116 | ||
d868b3bd | 1117 | (defun diff-find-source-location (&optional other-file reverse) |
1eabc5e6 SM |
1118 | "Find out (BUF LINE-OFFSET POS SRC DST SWITCHED). |
1119 | BUF is the buffer corresponding to the source file. | |
1120 | LINE-OFFSET is the offset between the expected and actual positions | |
1121 | of the text of the hunk or nil if the text was not found. | |
1122 | POS is a pair (BEG . END) indicating the position of the text in the buffer. | |
1123 | SRC and DST are the two variants of text as returned by `diff-hunk-text'. | |
1124 | SRC is the variant that was found in the buffer. | |
1125 | SWITCHED is non-nil if the patch is already applied." | |
7b91e0f2 | 1126 | (save-excursion |
e8a1ed31 | 1127 | (let* ((other (diff-xor other-file diff-jump-to-old-file)) |
370d860c SM |
1128 | (char-offset (- (point) (progn (diff-beginning-of-hunk) (point)))) |
1129 | (hunk (buffer-substring (point) | |
1130 | (save-excursion (diff-end-of-hunk) (point)))) | |
1131 | (old (diff-hunk-text hunk reverse char-offset)) | |
1132 | (new (diff-hunk-text hunk (not reverse) char-offset)) | |
7b91e0f2 | 1133 | ;; Find the location specification. |
d868b3bd | 1134 | (line (if (not (looking-at "\\(?:\\*\\{15\\}.*\n\\)?[-@* ]*\\([0-9,]+\\)\\([ acd+]+\\([0-9,]+\\)\\)?")) |
370d860c SM |
1135 | (error "Can't find the hunk header") |
1136 | (if other (match-string 1) | |
1137 | (if (match-end 3) (match-string 3) | |
1138 | (unless (re-search-forward "^--- \\([0-9,]+\\)" nil t) | |
1139 | (error "Can't find the hunk separator")) | |
1140 | (match-string 1))))) | |
1141 | (file (or (diff-find-file-name other) (error "Can't find the file"))) | |
1142 | (buf (find-file-noselect file))) | |
7b91e0f2 SM |
1143 | ;; Update the user preference if he so wished. |
1144 | (when (> (prefix-numeric-value other-file) 8) | |
e8a1ed31 | 1145 | (setq diff-jump-to-old-file other)) |
d868b3bd SM |
1146 | (with-current-buffer buf |
1147 | (goto-line (string-to-number line)) | |
1148 | (let* ((orig-pos (point)) | |
1eabc5e6 | 1149 | (switched nil) |
22cd1973 | 1150 | ;; FIXME: Check for case where both OLD and NEW are found. |
1eabc5e6 SM |
1151 | (pos (or (diff-find-text (car old)) |
1152 | (progn (setq switched t) (diff-find-text (car new))) | |
1153 | (progn (setq switched nil) | |
22cd1973 SM |
1154 | (condition-case nil |
1155 | (diff-find-approx-text (car old)) | |
1156 | (invalid-regexp nil))) ;Regex too big. | |
1eabc5e6 | 1157 | (progn (setq switched t) |
22cd1973 SM |
1158 | (condition-case nil |
1159 | (diff-find-approx-text (car new)) | |
1160 | (invalid-regexp nil))) ;Regex too big. | |
1eabc5e6 | 1161 | (progn (setq switched nil) nil)))) |
370d860c SM |
1162 | (nconc |
1163 | (list buf) | |
1eabc5e6 SM |
1164 | (if pos |
1165 | (list (count-lines orig-pos (car pos)) pos) | |
1166 | (list nil (cons orig-pos (+ orig-pos (length (car old)))))) | |
370d860c SM |
1167 | (if switched (list new old t) (list old new)))))))) |
1168 | ||
7b91e0f2 | 1169 | |
370d860c SM |
1170 | (defun diff-hunk-status-msg (line-offset reversed dry-run) |
1171 | (let ((msg (if dry-run | |
1172 | (if reversed "already applied" "not yet applied") | |
1173 | (if reversed "undone" "applied")))) | |
1174 | (message (cond ((null line-offset) "Hunk text not found") | |
1175 | ((= line-offset 0) "Hunk %s") | |
1176 | ((= line-offset 1) "Hunk %s at offset %d line") | |
1177 | (t "Hunk %s at offset %d lines")) | |
1178 | msg line-offset))) | |
1179 | ||
1180 | ||
1181 | (defun diff-apply-hunk (&optional reverse) | |
6e4e8a3b | 1182 | "Apply the current hunk to the source file and go to the next. |
7530b6da | 1183 | By default, the new source file is patched, but if the variable |
e8a1ed31 | 1184 | `diff-jump-to-old-file' is non-nil, then the old source file is |
7530b6da MB |
1185 | patched instead (some commands, such as `diff-goto-source' can change |
1186 | the value of this variable when given an appropriate prefix argument). | |
1187 | ||
4eaa6852 | 1188 | With a prefix argument, REVERSE the hunk." |
370d860c | 1189 | (interactive "P") |
281096ed | 1190 | (destructuring-bind (buf line-offset pos old new &optional switched) |
22cd1973 SM |
1191 | ;; If REVERSE go to the new file, otherwise go to the old. |
1192 | (diff-find-source-location (not reverse) reverse) | |
370d860c | 1193 | (cond |
4eaa6852 MB |
1194 | ((null line-offset) |
1195 | (error "Can't find the text to patch")) | |
370d860c | 1196 | ((and switched |
cdbb79c1 | 1197 | ;; A reversed patch was detected, perhaps apply it in reverse. |
370d860c SM |
1198 | (not (save-window-excursion |
1199 | (pop-to-buffer buf) | |
1eabc5e6 | 1200 | (goto-char (+ (car pos) (cdr old))) |
370d860c SM |
1201 | (y-or-n-p |
1202 | (if reverse | |
1203 | "Hunk hasn't been applied yet; apply it now? " | |
1204 | "Hunk has already been applied; undo it? "))))) | |
4eaa6852 | 1205 | (message "(Nothing done)")) |
370d860c | 1206 | (t |
4eaa6852 MB |
1207 | ;; Apply the hunk |
1208 | (with-current-buffer buf | |
1eabc5e6 SM |
1209 | (goto-char (car pos)) |
1210 | (delete-region (car pos) (cdr pos)) | |
4eaa6852 MB |
1211 | (insert (car new))) |
1212 | ;; Display BUF in a window | |
1eabc5e6 | 1213 | (set-window-point (display-buffer buf) (+ (car pos) (cdr new))) |
4eaa6852 MB |
1214 | (diff-hunk-status-msg line-offset (diff-xor switched reverse) nil) |
1215 | (when diff-advance-after-apply-hunk | |
1216 | (diff-hunk-next)))))) | |
370d860c | 1217 | |
7530b6da | 1218 | |
7530b6da MB |
1219 | (defun diff-test-hunk (&optional reverse) |
1220 | "See whether it's possible to apply the current hunk. | |
4eaa6852 | 1221 | With a prefix argument, try to REVERSE the hunk." |
7530b6da | 1222 | (interactive "P") |
370d860c | 1223 | (destructuring-bind (buf line-offset pos src dst &optional switched) |
22cd1973 SM |
1224 | ;; If REVERSE go to the new file, otherwise go to the old. |
1225 | (diff-find-source-location (not reverse) reverse) | |
1eabc5e6 | 1226 | (set-window-point (display-buffer buf) (+ (car pos) (cdr src))) |
4eaa6852 | 1227 | (diff-hunk-status-msg line-offset (diff-xor reverse switched) t))) |
370d860c | 1228 | |
7530b6da | 1229 | |
a1e2d7f7 SM |
1230 | (defalias 'diff-mouse-goto-source 'diff-goto-source) |
1231 | ||
1232 | (defun diff-goto-source (&optional other-file event) | |
7530b6da | 1233 | "Jump to the corresponding source line. |
e8a1ed31 | 1234 | `diff-jump-to-old-file' (or its opposite if the OTHER-FILE prefix arg |
281096ed | 1235 | is given) determines whether to jump to the old or the new file. |
7530b6da | 1236 | If the prefix arg is bigger than 8 (for example with \\[universal-argument] \\[universal-argument]) |
e8a1ed31 | 1237 | then `diff-jump-to-old-file' is also set, for the next invocations." |
a1e2d7f7 | 1238 | (interactive (list current-prefix-arg last-input-event)) |
a6373340 SM |
1239 | ;; When pointing at a removal line, we probably want to jump to |
1240 | ;; the old location, and else to the new (i.e. as if reverting). | |
1241 | ;; This is a convenient detail when using smerge-diff. | |
a1e2d7f7 | 1242 | (if event (posn-set-point (event-end event))) |
a6373340 SM |
1243 | (let ((rev (not (save-excursion (beginning-of-line) (looking-at "[-<]"))))) |
1244 | (destructuring-bind (buf line-offset pos src dst &optional switched) | |
1245 | (diff-find-source-location other-file rev) | |
1246 | (pop-to-buffer buf) | |
1eabc5e6 | 1247 | (goto-char (+ (car pos) (cdr src))) |
55d5d717 | 1248 | (diff-hunk-status-msg line-offset (diff-xor rev switched) t)))) |
370d860c | 1249 | |
7530b6da | 1250 | |
281096ed | 1251 | (defun diff-current-defun () |
370d860c SM |
1252 | "Find the name of function at point. |
1253 | For use in `add-log-current-defun-function'." | |
2f9edc8b KG |
1254 | (save-excursion |
1255 | (when (looking-at diff-hunk-header-re) | |
1256 | (forward-line 1) | |
4f815065 | 1257 | (re-search-forward "^[^ ]" nil t)) |
2f9edc8b KG |
1258 | (destructuring-bind (buf line-offset pos src dst &optional switched) |
1259 | (diff-find-source-location) | |
281096ed SM |
1260 | (beginning-of-line) |
1261 | (or (when (memq (char-after) '(?< ?-)) | |
370d860c SM |
1262 | ;; Cursor is pointing at removed text. This could be a removed |
1263 | ;; function, in which case, going to the source buffer will | |
1264 | ;; not help since the function is now removed. Instead, | |
1265 | ;; try to figure out the function name just from the code-fragment. | |
281096ed SM |
1266 | (let ((old (if switched dst src))) |
1267 | (with-temp-buffer | |
1268 | (insert (car old)) | |
281096ed | 1269 | (funcall (with-current-buffer buf major-mode)) |
cb4e8176 | 1270 | (goto-char (+ (point-min) (cdr old))) |
281096ed SM |
1271 | (add-log-current-defun)))) |
1272 | (with-current-buffer buf | |
1eabc5e6 | 1273 | (goto-char (+ (car pos) (cdr src))) |
281096ed | 1274 | (add-log-current-defun)))))) |
027ac3f8 | 1275 | |
22cd1973 SM |
1276 | (defun diff-refine-hunk () |
1277 | "Refine the current hunk by ignoring space differences." | |
1278 | (interactive) | |
1279 | (let* ((char-offset (- (point) (progn (diff-beginning-of-hunk) (point)))) | |
1280 | (opts (case (char-after) (?@ "-bu") (?* "-bc") (t "-b"))) | |
1281 | (line-nb (and (or (looking-at "[^0-9]+\\([0-9]+\\)") | |
1282 | (error "Can't find line number")) | |
1283 | (string-to-number (match-string 1)))) | |
1284 | (hunk (delete-and-extract-region | |
1285 | (point) (save-excursion (diff-end-of-hunk) (point)))) | |
1286 | (lead (make-string (1- line-nb) ?\n)) ;Line nums start at 1. | |
1287 | (file1 (make-temp-file "diff1")) | |
1288 | (file2 (make-temp-file "diff2")) | |
1289 | (coding-system-for-read buffer-file-coding-system) | |
1290 | old new) | |
1291 | (unwind-protect | |
1292 | (save-excursion | |
1293 | (setq old (diff-hunk-text hunk nil char-offset)) | |
1294 | (setq new (diff-hunk-text hunk t char-offset)) | |
1295 | (write-region (concat lead (car old)) nil file1 nil 'nomessage) | |
1296 | (write-region (concat lead (car new)) nil file2 nil 'nomessage) | |
1297 | (with-temp-buffer | |
1298 | (let ((status | |
1299 | (call-process diff-command nil t nil | |
1300 | opts file1 file2))) | |
1301 | (case status | |
1302 | (0 nil) ;Nothing to reformat. | |
1303 | (1 (goto-char (point-min)) | |
1304 | ;; Remove the file-header. | |
1305 | (when (re-search-forward diff-hunk-header-re nil t) | |
1306 | (delete-region (point-min) (match-beginning 0)))) | |
1307 | (t (goto-char (point-max)) | |
1308 | (unless (bolp) (insert "\n")) | |
1309 | (insert hunk))) | |
1310 | (setq hunk (buffer-string)) | |
1311 | (unless (memq status '(0 1)) | |
1312 | (error "Diff returned: %s" status))))) | |
1313 | ;; Whatever happens, put back some equivalent text: either the new | |
1314 | ;; one or the original one in case some error happened. | |
1315 | (insert hunk) | |
1316 | (delete-file file1) | |
1317 | (delete-file file2)))) | |
1318 | ||
610a6418 SM |
1319 | ;; provide the package |
1320 | (provide 'diff-mode) | |
1321 | ||
027ac3f8 | 1322 | ;;; Old Change Log from when diff-mode wasn't part of Emacs: |
610a6418 SM |
1323 | ;; Revision 1.11 1999/10/09 23:38:29 monnier |
1324 | ;; (diff-mode-load-hook): dropped. | |
1325 | ;; (auto-mode-alist): also catch *.diffs. | |
1326 | ;; (diff-find-file-name, diff-mode): add smarts to find the right file | |
1327 | ;; for *.rej files (that lack any file name indication). | |
1328 | ;; | |
1329 | ;; Revision 1.10 1999/09/30 15:32:11 monnier | |
1330 | ;; added support for "\ No newline at end of file". | |
1331 | ;; | |
1332 | ;; Revision 1.9 1999/09/15 00:01:13 monnier | |
1333 | ;; - added basic `compile' support. | |
1334 | ;; - have diff-kill-hunk call diff-kill-file if it's the only hunk. | |
1335 | ;; - diff-kill-file now tries to kill the leading garbage as well. | |
1336 | ;; | |
1337 | ;; Revision 1.8 1999/09/13 21:10:09 monnier | |
1338 | ;; - don't use CL in the autoloaded code | |
1339 | ;; - accept diffs using -T | |
1340 | ;; | |
1341 | ;; Revision 1.7 1999/09/05 20:53:03 monnier | |
1342 | ;; interface to ediff-patch | |
1343 | ;; | |
1344 | ;; Revision 1.6 1999/09/01 20:55:13 monnier | |
1345 | ;; (ediff=patch-file): add bindings to call ediff-patch. | |
1346 | ;; (diff-find-file-name): taken out of diff-goto-source. | |
1347 | ;; (diff-unified->context, diff-context->unified, diff-reverse-direction, | |
1348 | ;; diff-fixup-modifs): only use the region if a prefix arg is given. | |
1349 | ;; | |
1350 | ;; Revision 1.5 1999/08/31 19:18:52 monnier | |
1351 | ;; (diff-beginning-of-file, diff-prev-file): fixed wrong parenthesis. | |
1352 | ;; | |
1353 | ;; Revision 1.4 1999/08/31 13:01:44 monnier | |
1354 | ;; use `combine-after-change-calls' to minimize the slowdown of font-lock. | |
1355 | ;; | |
1356 | ||
4f815065 | 1357 | ;; arch-tag: 2571d7ff-bc28-4cf9-8585-42e21890be66 |
610a6418 | 1358 | ;;; diff-mode.el ends here |