| 1 | ;;; smerge-mode.el --- Minor mode to resolve diff3 conflicts |
| 2 | |
| 3 | ;; Copyright (C) 1999, 2000, 01, 03, 2004 Free Software Foundation, Inc. |
| 4 | |
| 5 | ;; Author: Stefan Monnier <monnier@cs.yale.edu> |
| 6 | ;; Keywords: revision-control merge diff3 cvs conflict |
| 7 | |
| 8 | ;; This file is part of GNU Emacs. |
| 9 | |
| 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 2, or (at your option) |
| 13 | ;; any later version. |
| 14 | |
| 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. |
| 19 | |
| 20 | ;; You should have received a copy of the GNU General Public License |
| 21 | ;; along with GNU Emacs; see the file COPYING. If not, write to the |
| 22 | ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 23 | ;; Boston, MA 02111-1307, USA. |
| 24 | |
| 25 | ;;; Commentary: |
| 26 | |
| 27 | ;; Provides a lightweight alternative to emerge/ediff. |
| 28 | ;; To use it, simply add to your .emacs the following lines: |
| 29 | ;; |
| 30 | ;; (autoload 'smerge-mode "smerge-mode" nil t) |
| 31 | ;; |
| 32 | ;; you can even have it turned on automatically with the following |
| 33 | ;; piece of code in your .emacs: |
| 34 | ;; |
| 35 | ;; (defun sm-try-smerge () |
| 36 | ;; (save-excursion |
| 37 | ;; (goto-char (point-min)) |
| 38 | ;; (when (re-search-forward "^<<<<<<< " nil t) |
| 39 | ;; (smerge-mode 1)))) |
| 40 | ;; (add-hook 'find-file-hook 'sm-try-smerge t) |
| 41 | |
| 42 | ;;; Todo: |
| 43 | |
| 44 | ;; - if requested, ask the user whether he wants to call ediff right away |
| 45 | |
| 46 | ;;; Code: |
| 47 | |
| 48 | (eval-when-compile (require 'cl)) |
| 49 | |
| 50 | |
| 51 | (defgroup smerge () |
| 52 | "Minor mode to resolve diff3 conflicts." |
| 53 | :group 'tools |
| 54 | :prefix "smerge-") |
| 55 | |
| 56 | (defcustom smerge-diff-buffer-name "*vc-diff*" |
| 57 | "Buffer name to use for displaying diffs." |
| 58 | :group 'smerge |
| 59 | :type '(choice |
| 60 | (const "*vc-diff*") |
| 61 | (const "*cvs-diff*") |
| 62 | (const "*smerge-diff*") |
| 63 | string)) |
| 64 | |
| 65 | (defcustom smerge-diff-switches |
| 66 | (append '("-d" "-b") |
| 67 | (if (listp diff-switches) diff-switches (list diff-switches))) |
| 68 | "*A list of strings specifying switches to be passed to diff. |
| 69 | Used in `smerge-diff-base-mine' and related functions." |
| 70 | :group 'smerge |
| 71 | :type '(repeat string)) |
| 72 | |
| 73 | (defcustom smerge-auto-leave t |
| 74 | "*Non-nil means to leave `smerge-mode' when the last conflict is resolved." |
| 75 | :group 'smerge |
| 76 | :type 'boolean) |
| 77 | |
| 78 | (defface smerge-mine-face |
| 79 | '((((background light)) |
| 80 | (:foreground "blue")) |
| 81 | (((background dark)) |
| 82 | (:foreground "cyan"))) |
| 83 | "Face for your code." |
| 84 | :group 'smerge) |
| 85 | (defvar smerge-mine-face 'smerge-mine-face) |
| 86 | |
| 87 | (defface smerge-other-face |
| 88 | '((((background light)) |
| 89 | (:foreground "darkgreen")) |
| 90 | (((background dark)) |
| 91 | (:foreground "lightgreen"))) |
| 92 | "Face for the other code." |
| 93 | :group 'smerge) |
| 94 | (defvar smerge-other-face 'smerge-other-face) |
| 95 | |
| 96 | (defface smerge-base-face |
| 97 | '((((background light)) |
| 98 | (:foreground "red")) |
| 99 | (((background dark)) |
| 100 | (:foreground "orange"))) |
| 101 | "Face for the base code." |
| 102 | :group 'smerge) |
| 103 | (defvar smerge-base-face 'smerge-base-face) |
| 104 | |
| 105 | (defface smerge-markers-face |
| 106 | '((((background light)) |
| 107 | (:background "grey85")) |
| 108 | (((background dark)) |
| 109 | (:background "grey30"))) |
| 110 | "Face for the conflict markers." |
| 111 | :group 'smerge) |
| 112 | (defvar smerge-markers-face 'smerge-markers-face) |
| 113 | |
| 114 | (easy-mmode-defmap smerge-basic-map |
| 115 | `(("n" . smerge-next) |
| 116 | ("p" . smerge-prev) |
| 117 | ("r" . smerge-resolve) |
| 118 | ("a" . smerge-keep-all) |
| 119 | ("b" . smerge-keep-base) |
| 120 | ("o" . smerge-keep-other) |
| 121 | ("m" . smerge-keep-mine) |
| 122 | ("E" . smerge-ediff) |
| 123 | ("\C-m" . smerge-keep-current) |
| 124 | ("=" . ,(make-sparse-keymap "Diff")) |
| 125 | ("=<" "base-mine" . smerge-diff-base-mine) |
| 126 | ("=>" "base-other" . smerge-diff-base-other) |
| 127 | ("==" "mine-other" . smerge-diff-mine-other)) |
| 128 | "The base keymap for `smerge-mode'.") |
| 129 | |
| 130 | (defcustom smerge-command-prefix "\C-c^" |
| 131 | "Prefix for `smerge-mode' commands." |
| 132 | :group 'smerge |
| 133 | :type '(choice (string "\e") (string "\C-c^") (string "") string)) |
| 134 | |
| 135 | (easy-mmode-defmap smerge-mode-map |
| 136 | `((,smerge-command-prefix . ,smerge-basic-map)) |
| 137 | "Keymap for `smerge-mode'.") |
| 138 | |
| 139 | (defvar smerge-check-cache nil) |
| 140 | (make-variable-buffer-local 'smerge-check-cache) |
| 141 | (defun smerge-check (n) |
| 142 | (condition-case nil |
| 143 | (let ((state (cons (point) (buffer-modified-tick)))) |
| 144 | (unless (equal (cdr smerge-check-cache) state) |
| 145 | (smerge-match-conflict) |
| 146 | (setq smerge-check-cache (cons (match-data) state))) |
| 147 | (nth (* 2 n) (car smerge-check-cache))) |
| 148 | (error nil))) |
| 149 | |
| 150 | (easy-menu-define smerge-mode-menu smerge-mode-map |
| 151 | "Menu for `smerge-mode'." |
| 152 | '("SMerge" |
| 153 | ["Next" smerge-next :help "Go to next conflict"] |
| 154 | ["Previous" smerge-prev :help "Go to previous conflict"] |
| 155 | "--" |
| 156 | ["Keep All" smerge-keep-all :help "Keep all three versions" |
| 157 | :active (smerge-check 1)] |
| 158 | ["Keep Current" smerge-keep-current :help "Use current (at point) version" |
| 159 | :active (and (smerge-check 1) (> (smerge-get-current) 0))] |
| 160 | "--" |
| 161 | ["Revert to Base" smerge-keep-base :help "Revert to base version" |
| 162 | :active (smerge-check 2)] |
| 163 | ["Keep Other" smerge-keep-other :help "Keep `other' version" |
| 164 | :active (smerge-check 3)] |
| 165 | ["Keep Yours" smerge-keep-mine :help "Keep your version" |
| 166 | :active (smerge-check 1)] |
| 167 | "--" |
| 168 | ["Diff Base/Mine" smerge-diff-base-mine |
| 169 | :help "Diff `base' and `mine' for current conflict" |
| 170 | :active (smerge-check 2)] |
| 171 | ["Diff Base/Other" smerge-diff-base-other |
| 172 | :help "Diff `base' and `other' for current conflict" |
| 173 | :active (smerge-check 2)] |
| 174 | ["Diff Mine/Other" smerge-diff-mine-other |
| 175 | :help "Diff `mine' and `other' for current conflict" |
| 176 | :active (smerge-check 1)] |
| 177 | "--" |
| 178 | ["Invoke Ediff" smerge-ediff |
| 179 | :help "Use Ediff to resolve the conflicts" |
| 180 | :active (smerge-check 1)] |
| 181 | ["Auto Resolve" smerge-resolve |
| 182 | :help "Try auto-resolution heuristics" |
| 183 | :active (smerge-check 1)] |
| 184 | ["Combine" smerge-combine-with-next |
| 185 | :help "Combine current conflict with next" |
| 186 | :active (smerge-check 1)] |
| 187 | )) |
| 188 | |
| 189 | (easy-menu-define smerge-context-menu nil |
| 190 | "Context menu for mine area in `smerge-mode'." |
| 191 | '(nil |
| 192 | ["Keep Current" smerge-keep-current :help "Use current (at point) version"] |
| 193 | ["Kill Current" smerge-kill-current :help "Remove current (at point) version"] |
| 194 | ["Keep All" smerge-keep-all :help "Keep all three versions"] |
| 195 | "---" |
| 196 | ["More..." (popup-menu smerge-mode-menu) :help "Show full SMerge mode menu"] |
| 197 | )) |
| 198 | |
| 199 | (defconst smerge-font-lock-keywords |
| 200 | '((smerge-find-conflict |
| 201 | (1 smerge-mine-face prepend t) |
| 202 | (2 smerge-base-face prepend t) |
| 203 | (3 smerge-other-face prepend t) |
| 204 | ;; FIXME: `keep' doesn't work right with syntactic fontification. |
| 205 | (0 smerge-markers-face keep) |
| 206 | (4 nil t t) |
| 207 | (5 nil t t))) |
| 208 | "Font lock patterns for `smerge-mode'.") |
| 209 | |
| 210 | (defconst smerge-begin-re "^<<<<<<< \\(.*\\)\n") |
| 211 | (defconst smerge-end-re "^>>>>>>> .*\n") |
| 212 | (defconst smerge-base-re "^||||||| .*\n") |
| 213 | (defconst smerge-other-re "^=======\n") |
| 214 | |
| 215 | (defvar smerge-conflict-style nil |
| 216 | "Keep track of which style of conflict is in use. |
| 217 | Can be nil if the style is undecided, or else: |
| 218 | - `diff3-E' |
| 219 | - `diff3-A'") |
| 220 | |
| 221 | ;; Compiler pacifiers |
| 222 | (defvar font-lock-mode) |
| 223 | (defvar font-lock-keywords) |
| 224 | |
| 225 | ;;;; |
| 226 | ;;;; Actual code |
| 227 | ;;;; |
| 228 | |
| 229 | ;; Define smerge-next and smerge-prev |
| 230 | (easy-mmode-define-navigation smerge smerge-begin-re "conflict") |
| 231 | |
| 232 | (defconst smerge-match-names ["conflict" "mine" "base" "other"]) |
| 233 | |
| 234 | (defun smerge-ensure-match (n) |
| 235 | (unless (match-end n) |
| 236 | (error (format "No `%s'" (aref smerge-match-names n))))) |
| 237 | |
| 238 | (defun smerge-auto-leave () |
| 239 | (when (and smerge-auto-leave |
| 240 | (save-excursion (goto-char (point-min)) |
| 241 | (not (re-search-forward smerge-begin-re nil t)))) |
| 242 | (smerge-mode -1))) |
| 243 | |
| 244 | |
| 245 | (defun smerge-keep-all () |
| 246 | "Concatenate all versions." |
| 247 | (interactive) |
| 248 | (smerge-match-conflict) |
| 249 | (let ((mb2 (or (match-beginning 2) (point-max))) |
| 250 | (me2 (or (match-end 2) (point-min)))) |
| 251 | (delete-region (match-end 3) (match-end 0)) |
| 252 | (delete-region (max me2 (match-end 1)) (match-beginning 3)) |
| 253 | (if (and (match-end 2) (/= (match-end 1) (match-end 3))) |
| 254 | (delete-region (match-end 1) (match-beginning 2))) |
| 255 | (delete-region (match-beginning 0) (min (match-beginning 1) mb2)) |
| 256 | (smerge-auto-leave))) |
| 257 | |
| 258 | (defun smerge-keep-n (n) |
| 259 | ;; We used to use replace-match, but that did not preserve markers so well. |
| 260 | (delete-region (match-end n) (match-end 0)) |
| 261 | (delete-region (match-beginning 0) (match-beginning n))) |
| 262 | |
| 263 | (defun smerge-combine-with-next () |
| 264 | "Combine the current conflict with the next one." |
| 265 | (interactive) |
| 266 | (smerge-match-conflict) |
| 267 | (let ((ends nil)) |
| 268 | (dolist (i '(3 2 1 0)) |
| 269 | (push (if (match-end i) (copy-marker (match-end i) t)) ends)) |
| 270 | (setq ends (apply 'vector ends)) |
| 271 | (goto-char (aref ends 0)) |
| 272 | (if (not (re-search-forward smerge-begin-re nil t)) |
| 273 | (error "No next conflict") |
| 274 | (smerge-match-conflict) |
| 275 | (let ((match-data (mapcar (lambda (m) (if m (copy-marker m))) |
| 276 | (match-data)))) |
| 277 | ;; First copy the in-between text in each alternative. |
| 278 | (dolist (i '(1 2 3)) |
| 279 | (when (aref ends i) |
| 280 | (goto-char (aref ends i)) |
| 281 | (insert-buffer-substring (current-buffer) |
| 282 | (aref ends 0) (car match-data)))) |
| 283 | (delete-region (aref ends 0) (car match-data)) |
| 284 | ;; Then move the second conflict's alternatives into the first. |
| 285 | (dolist (i '(1 2 3)) |
| 286 | (set-match-data match-data) |
| 287 | (when (and (aref ends i) (match-end i)) |
| 288 | (goto-char (aref ends i)) |
| 289 | (insert-buffer-substring (current-buffer) |
| 290 | (match-beginning i) (match-end i)))) |
| 291 | (delete-region (car match-data) (cadr match-data)) |
| 292 | ;; Free the markers. |
| 293 | (dolist (m match-data) (if m (move-marker m nil))) |
| 294 | (mapc (lambda (m) (if m (move-marker m nil))) ends))))) |
| 295 | |
| 296 | (defvar smerge-resolve-function |
| 297 | (lambda () (error "Don't know how to resolve")) |
| 298 | "Mode-specific merge function. |
| 299 | The function is called with no argument and with the match data set |
| 300 | according to `smerge-match-conflict'.") |
| 301 | |
| 302 | (defvar smerge-text-properties |
| 303 | `(help-echo "merge conflict: mouse-3 shows a menu" |
| 304 | ;; mouse-face highlight |
| 305 | keymap (keymap (down-mouse-3 . smerge-popup-context-menu)))) |
| 306 | |
| 307 | (defun smerge-remove-props (&optional beg end) |
| 308 | (remove-text-properties |
| 309 | (or beg (match-beginning 0)) |
| 310 | (or end (match-end 0)) |
| 311 | smerge-text-properties)) |
| 312 | |
| 313 | (defun smerge-popup-context-menu (event) |
| 314 | "Pop up the Smerge mode context menu under mouse." |
| 315 | (interactive "e") |
| 316 | (if (and smerge-mode |
| 317 | (save-excursion (posn-set-point (event-end event)) (smerge-check 1))) |
| 318 | (progn |
| 319 | (posn-set-point (event-end event)) |
| 320 | (smerge-match-conflict) |
| 321 | (let ((i (smerge-get-current)) |
| 322 | o) |
| 323 | (if (<= i 0) |
| 324 | ;; Out of range |
| 325 | (popup-menu smerge-mode-menu) |
| 326 | ;; Install overlay. |
| 327 | (setq o (make-overlay (match-beginning i) (match-end i))) |
| 328 | (unwind-protect |
| 329 | (progn |
| 330 | (overlay-put o 'face 'highlight) |
| 331 | (sit-for 0) ;Display the new highlighting. |
| 332 | (popup-menu smerge-context-menu)) |
| 333 | ;; Delete overlay. |
| 334 | (delete-overlay o))))) |
| 335 | ;; There's no conflict at point, the text-props are just obsolete. |
| 336 | (save-excursion |
| 337 | (let ((beg (re-search-backward smerge-end-re nil t)) |
| 338 | (end (re-search-forward smerge-begin-re nil t))) |
| 339 | (smerge-remove-props (or beg (point-min)) (or end (point-max))) |
| 340 | (push event unread-command-events))))) |
| 341 | |
| 342 | (defun smerge-resolve () |
| 343 | "Resolve the conflict at point intelligently. |
| 344 | This relies on mode-specific knowledge and thus only works in |
| 345 | some major modes. Uses `smerge-resolve-function' to do the actual work." |
| 346 | (interactive) |
| 347 | (smerge-match-conflict) |
| 348 | (smerge-remove-props) |
| 349 | (cond |
| 350 | ;; Trivial diff3 -A non-conflicts. |
| 351 | ((and (eq (match-end 1) (match-end 3)) |
| 352 | (eq (match-beginning 1) (match-beginning 3))) |
| 353 | ;; FIXME: Add "if [ diff -b MINE OTHER ]; then select OTHER; fi" |
| 354 | (smerge-keep-n 3)) |
| 355 | ((and (match-end 2) |
| 356 | ;; FIXME: Add "diff -b BASE MINE | patch OTHER". |
| 357 | ;; FIXME: Add "diff -b BASE OTHER | patch MINE". |
| 358 | nil) |
| 359 | ) |
| 360 | ((and (not (match-end 2)) |
| 361 | ;; FIXME: Add "diff -b"-based refinement. |
| 362 | nil) |
| 363 | ) |
| 364 | (t |
| 365 | ;; Mode-specific conflict resolution. |
| 366 | (funcall smerge-resolve-function))) |
| 367 | (smerge-auto-leave)) |
| 368 | |
| 369 | (defun smerge-keep-base () |
| 370 | "Revert to the base version." |
| 371 | (interactive) |
| 372 | (smerge-match-conflict) |
| 373 | (smerge-ensure-match 2) |
| 374 | (smerge-remove-props) |
| 375 | (smerge-keep-n 2) |
| 376 | (smerge-auto-leave)) |
| 377 | |
| 378 | (defun smerge-keep-other () |
| 379 | "Use \"other\" version." |
| 380 | (interactive) |
| 381 | (smerge-match-conflict) |
| 382 | ;;(smerge-ensure-match 3) |
| 383 | (smerge-remove-props) |
| 384 | (smerge-keep-n 3) |
| 385 | (smerge-auto-leave)) |
| 386 | |
| 387 | (defun smerge-keep-mine () |
| 388 | "Keep your version." |
| 389 | (interactive) |
| 390 | (smerge-match-conflict) |
| 391 | ;;(smerge-ensure-match 1) |
| 392 | (smerge-remove-props) |
| 393 | (smerge-keep-n 1) |
| 394 | (smerge-auto-leave)) |
| 395 | |
| 396 | (defun smerge-get-current () |
| 397 | (let ((i 3)) |
| 398 | (while (or (not (match-end i)) |
| 399 | (< (point) (match-beginning i)) |
| 400 | (>= (point) (match-end i))) |
| 401 | (decf i)) |
| 402 | i)) |
| 403 | |
| 404 | (defun smerge-keep-current () |
| 405 | "Use the current (under the cursor) version." |
| 406 | (interactive) |
| 407 | (smerge-match-conflict) |
| 408 | (let ((i (smerge-get-current))) |
| 409 | (if (<= i 0) (error "Not inside a version") |
| 410 | (smerge-remove-props) |
| 411 | (smerge-keep-n i) |
| 412 | (smerge-auto-leave)))) |
| 413 | |
| 414 | (defun smerge-kill-current () |
| 415 | "Remove the current (under the cursor) version." |
| 416 | (interactive) |
| 417 | (smerge-match-conflict) |
| 418 | (let ((i (smerge-get-current))) |
| 419 | (if (<= i 0) (error "Not inside a version") |
| 420 | (smerge-remove-props) |
| 421 | (let ((left nil)) |
| 422 | (dolist (n '(3 2 1)) |
| 423 | (if (and (match-end n) (/= (match-end n) (match-end i))) |
| 424 | (push n left))) |
| 425 | (if (and (cdr left) |
| 426 | (/= (match-end (car left)) (match-end (cadr left)))) |
| 427 | (ding) ;We don't know how to do that. |
| 428 | (smerge-keep-n (car left)) |
| 429 | (smerge-auto-leave)))))) |
| 430 | |
| 431 | (defun smerge-diff-base-mine () |
| 432 | "Diff 'base' and 'mine' version in current conflict region." |
| 433 | (interactive) |
| 434 | (smerge-diff 2 1)) |
| 435 | |
| 436 | (defun smerge-diff-base-other () |
| 437 | "Diff 'base' and 'other' version in current conflict region." |
| 438 | (interactive) |
| 439 | (smerge-diff 2 3)) |
| 440 | |
| 441 | (defun smerge-diff-mine-other () |
| 442 | "Diff 'mine' and 'other' version in current conflict region." |
| 443 | (interactive) |
| 444 | (smerge-diff 1 3)) |
| 445 | |
| 446 | (defun smerge-match-conflict () |
| 447 | "Get info about the conflict. Puts the info in the `match-data'. |
| 448 | The submatches contain: |
| 449 | 0: the whole conflict. |
| 450 | 1: your code. |
| 451 | 2: the base code. |
| 452 | 3: other code. |
| 453 | An error is raised if not inside a conflict." |
| 454 | (save-excursion |
| 455 | (condition-case nil |
| 456 | (let* ((orig-point (point)) |
| 457 | |
| 458 | (_ (forward-line 1)) |
| 459 | (_ (re-search-backward smerge-begin-re)) |
| 460 | |
| 461 | (start (match-beginning 0)) |
| 462 | (mine-start (match-end 0)) |
| 463 | (filename (or (match-string 1) "")) |
| 464 | |
| 465 | (_ (re-search-forward smerge-end-re)) |
| 466 | (_ (assert (< orig-point (match-end 0)))) |
| 467 | |
| 468 | (other-end (match-beginning 0)) |
| 469 | (end (match-end 0)) |
| 470 | |
| 471 | (_ (re-search-backward smerge-other-re start)) |
| 472 | |
| 473 | (mine-end (match-beginning 0)) |
| 474 | (other-start (match-end 0)) |
| 475 | |
| 476 | base-start base-end) |
| 477 | |
| 478 | ;; handle the various conflict styles |
| 479 | (cond |
| 480 | ((save-excursion |
| 481 | (goto-char mine-start) |
| 482 | (re-search-forward smerge-begin-re end t)) |
| 483 | ;; There's a nested conflict and we're after the the beginning |
| 484 | ;; of the outer one but before the beginning of the inner one. |
| 485 | (error "There is a nested conflict")) |
| 486 | |
| 487 | ((re-search-backward smerge-base-re start t) |
| 488 | ;; a 3-parts conflict |
| 489 | (set (make-local-variable 'smerge-conflict-style) 'diff3-A) |
| 490 | (setq base-end mine-end) |
| 491 | (setq mine-end (match-beginning 0)) |
| 492 | (setq base-start (match-end 0))) |
| 493 | |
| 494 | ((string= filename (file-name-nondirectory |
| 495 | (or buffer-file-name ""))) |
| 496 | ;; a 2-parts conflict |
| 497 | (set (make-local-variable 'smerge-conflict-style) 'diff3-E)) |
| 498 | |
| 499 | ((and (not base-start) |
| 500 | (or (eq smerge-conflict-style 'diff3-A) |
| 501 | (equal filename "ANCESTOR") |
| 502 | (string-match "\\`[.0-9]+\\'" filename))) |
| 503 | ;; a same-diff conflict |
| 504 | (setq base-start mine-start) |
| 505 | (setq base-end mine-end) |
| 506 | (setq mine-start other-start) |
| 507 | (setq mine-end other-end))) |
| 508 | |
| 509 | (let ((inhibit-read-only t) |
| 510 | (inhibit-modification-hooks t) |
| 511 | (m (buffer-modified-p))) |
| 512 | (unwind-protect |
| 513 | (add-text-properties start end smerge-text-properties) |
| 514 | (restore-buffer-modified-p m))) |
| 515 | |
| 516 | (store-match-data (list start end |
| 517 | mine-start mine-end |
| 518 | base-start base-end |
| 519 | other-start other-end |
| 520 | (when base-start (1- base-start)) base-start |
| 521 | (1- other-start) other-start)) |
| 522 | t) |
| 523 | (search-failed (error "Point not in conflict region"))))) |
| 524 | |
| 525 | (defun smerge-find-conflict (&optional limit) |
| 526 | "Find and match a conflict region. Intended as a font-lock MATCHER. |
| 527 | The submatches are the same as in `smerge-match-conflict'. |
| 528 | Returns non-nil if a match is found between the point and LIMIT. |
| 529 | The point is moved to the end of the conflict." |
| 530 | (when (re-search-forward smerge-begin-re limit t) |
| 531 | (condition-case err |
| 532 | (progn |
| 533 | (smerge-match-conflict) |
| 534 | (goto-char (match-end 0))) |
| 535 | (error (smerge-find-conflict limit))))) |
| 536 | |
| 537 | (defun smerge-diff (n1 n2) |
| 538 | (smerge-match-conflict) |
| 539 | (smerge-ensure-match n1) |
| 540 | (smerge-ensure-match n2) |
| 541 | (let ((name1 (aref smerge-match-names n1)) |
| 542 | (name2 (aref smerge-match-names n2)) |
| 543 | ;; Read them before the match-data gets clobbered. |
| 544 | (beg1 (match-beginning n1)) |
| 545 | (end1 (match-end n1)) |
| 546 | (beg2 (match-beginning n2)) |
| 547 | (end2 (match-end n2)) |
| 548 | (file1 (make-temp-file "smerge1")) |
| 549 | (file2 (make-temp-file "smerge2")) |
| 550 | (dir default-directory) |
| 551 | (file (file-relative-name buffer-file-name)) |
| 552 | (coding-system-for-read buffer-file-coding-system)) |
| 553 | (write-region beg1 end1 file1 nil 'nomessage) |
| 554 | (write-region beg2 end2 file2 nil 'nomessage) |
| 555 | (unwind-protect |
| 556 | (with-current-buffer (get-buffer-create smerge-diff-buffer-name) |
| 557 | (setq default-directory dir) |
| 558 | (let ((inhibit-read-only t)) |
| 559 | (erase-buffer) |
| 560 | (let ((status |
| 561 | (apply 'call-process diff-command nil t nil |
| 562 | (append smerge-diff-switches |
| 563 | (list "-L" (concat name1 "/" file) |
| 564 | "-L" (concat name2 "/" file) |
| 565 | file1 file2))))) |
| 566 | (if (eq status 0) (insert "No differences found.\n")))) |
| 567 | (goto-char (point-min)) |
| 568 | (diff-mode) |
| 569 | (display-buffer (current-buffer) t)) |
| 570 | (delete-file file1) |
| 571 | (delete-file file2)))) |
| 572 | |
| 573 | ;; compiler pacifiers |
| 574 | (defvar smerge-ediff-windows) |
| 575 | (defvar smerge-ediff-buf) |
| 576 | (defvar ediff-buffer-A) |
| 577 | (defvar ediff-buffer-B) |
| 578 | (defvar ediff-buffer-C) |
| 579 | |
| 580 | ;;;###autoload |
| 581 | (defun smerge-ediff (&optional name-mine name-other name-base) |
| 582 | "Invoke ediff to resolve the conflicts. |
| 583 | NAME-MINE, NAME-OTHER, and NAME-BASE, if non-nil, are used for the |
| 584 | buffer names." |
| 585 | (interactive) |
| 586 | (let* ((buf (current-buffer)) |
| 587 | (mode major-mode) |
| 588 | ;;(ediff-default-variant 'default-B) |
| 589 | (config (current-window-configuration)) |
| 590 | (filename (file-name-nondirectory buffer-file-name)) |
| 591 | (mine (generate-new-buffer |
| 592 | (or name-mine (concat "*" filename " MINE*")))) |
| 593 | (other (generate-new-buffer |
| 594 | (or name-other (concat "*" filename " OTHER*")))) |
| 595 | base) |
| 596 | (with-current-buffer mine |
| 597 | (buffer-disable-undo) |
| 598 | (insert-buffer-substring buf) |
| 599 | (goto-char (point-min)) |
| 600 | (while (smerge-find-conflict) |
| 601 | (when (match-beginning 2) (setq base t)) |
| 602 | (smerge-keep-n 1)) |
| 603 | (buffer-enable-undo) |
| 604 | (set-buffer-modified-p nil) |
| 605 | (funcall mode)) |
| 606 | |
| 607 | (with-current-buffer other |
| 608 | (buffer-disable-undo) |
| 609 | (insert-buffer-substring buf) |
| 610 | (goto-char (point-min)) |
| 611 | (while (smerge-find-conflict) |
| 612 | (smerge-keep-n 3)) |
| 613 | (buffer-enable-undo) |
| 614 | (set-buffer-modified-p nil) |
| 615 | (funcall mode)) |
| 616 | |
| 617 | (when base |
| 618 | (setq base (generate-new-buffer |
| 619 | (or name-base (concat "*" filename " BASE*")))) |
| 620 | (with-current-buffer base |
| 621 | (buffer-disable-undo) |
| 622 | (insert-buffer-substring buf) |
| 623 | (goto-char (point-min)) |
| 624 | (while (smerge-find-conflict) |
| 625 | (if (match-end 2) |
| 626 | (smerge-keep-n 2) |
| 627 | (delete-region (match-beginning 0) (match-end 0)))) |
| 628 | (buffer-enable-undo) |
| 629 | (set-buffer-modified-p nil) |
| 630 | (funcall mode))) |
| 631 | |
| 632 | ;; the rest of the code is inspired from vc.el |
| 633 | ;; Fire up ediff. |
| 634 | (set-buffer |
| 635 | (if base |
| 636 | (ediff-merge-buffers-with-ancestor mine other base) |
| 637 | ;; nil 'ediff-merge-revisions-with-ancestor buffer-file-name) |
| 638 | (ediff-merge-buffers mine other))) |
| 639 | ;; nil 'ediff-merge-revisions buffer-file-name))) |
| 640 | |
| 641 | ;; Ediff is now set up, and we are in the control buffer. |
| 642 | ;; Do a few further adjustments and take precautions for exit. |
| 643 | (set (make-local-variable 'smerge-ediff-windows) config) |
| 644 | (set (make-local-variable 'smerge-ediff-buf) buf) |
| 645 | (set (make-local-variable 'ediff-quit-hook) |
| 646 | (lambda () |
| 647 | (let ((buffer-A ediff-buffer-A) |
| 648 | (buffer-B ediff-buffer-B) |
| 649 | (buffer-C ediff-buffer-C) |
| 650 | (buffer-Ancestor ediff-ancestor-buffer) |
| 651 | (buf smerge-ediff-buf) |
| 652 | (windows smerge-ediff-windows)) |
| 653 | (ediff-cleanup-mess) |
| 654 | (with-current-buffer buf |
| 655 | (erase-buffer) |
| 656 | (insert-buffer buffer-C) |
| 657 | (kill-buffer buffer-A) |
| 658 | (kill-buffer buffer-B) |
| 659 | (kill-buffer buffer-C) |
| 660 | (when (bufferp buffer-Ancestor) (kill-buffer buffer-Ancestor)) |
| 661 | (set-window-configuration windows) |
| 662 | (message "Conflict resolution finished; you may save the buffer"))))) |
| 663 | (message "Please resolve conflicts now; exit ediff when done"))) |
| 664 | |
| 665 | |
| 666 | ;;;###autoload |
| 667 | (define-minor-mode smerge-mode |
| 668 | "Minor mode to simplify editing output from the diff3 program. |
| 669 | \\{smerge-mode-map}" |
| 670 | nil " SMerge" nil |
| 671 | (when (and (boundp 'font-lock-mode) font-lock-mode) |
| 672 | (set (make-local-variable 'font-lock-multiline) t) |
| 673 | (save-excursion |
| 674 | (if smerge-mode |
| 675 | (font-lock-add-keywords nil smerge-font-lock-keywords 'append) |
| 676 | (font-lock-remove-keywords nil smerge-font-lock-keywords)) |
| 677 | (goto-char (point-min)) |
| 678 | (while (smerge-find-conflict) |
| 679 | (save-excursion |
| 680 | (font-lock-fontify-region (match-beginning 0) (match-end 0) nil)))))) |
| 681 | |
| 682 | |
| 683 | (provide 'smerge-mode) |
| 684 | |
| 685 | ;; arch-tag: 605c8d1e-e43d-4943-a6f3-1bcc4333e690 |
| 686 | ;;; smerge-mode.el ends here |