| 1 | ;;; smerge-mode.el --- Minor mode to resolve diff3 conflicts |
| 2 | |
| 3 | ;; Copyright (C) 1999, 2000 Free Software Foundation, Inc. |
| 4 | |
| 5 | ;; Author: Stefan Monnier <monnier@cs.yale.edu> |
| 6 | ;; Keywords: merge diff3 cvs conflict |
| 7 | ;; Revision: $Id: smerge-mode.el,v 1.6 2000/10/05 06:05:51 miles Exp $ |
| 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 | |
| 28 | ;; Provides a lightweight alternative to emerge/ediff. |
| 29 | ;; To use it, simply add to your .emacs the following lines: |
| 30 | ;; |
| 31 | ;; (autoload 'smerge-mode "smerge-mode" nil t) |
| 32 | ;; |
| 33 | ;; you can even have it turned on automatically with the following |
| 34 | ;; piece of code in your .emacs: |
| 35 | ;; |
| 36 | ;; (defun sm-try-smerge () |
| 37 | ;; (save-excursion |
| 38 | ;; (goto-char (point-min)) |
| 39 | ;; (when (re-search-forward "^<<<<<<< " nil t) |
| 40 | ;; (smerge-mode 1)))) |
| 41 | ;; (add-hook 'find-file-hooks 'sm-try-smerge t) |
| 42 | |
| 43 | ;;; Todo: |
| 44 | |
| 45 | ;; - if requested, ask the user whether he wants to call ediff right away |
| 46 | |
| 47 | ;;; Code: |
| 48 | |
| 49 | (eval-when-compile (require 'cl)) |
| 50 | |
| 51 | |
| 52 | (defgroup smerge () |
| 53 | "Minor mode to resolve diff3 conflicts." |
| 54 | :group 'tools |
| 55 | :prefix "smerge-") |
| 56 | |
| 57 | (defcustom smerge-diff-buffer-name "*smerge-diff*" |
| 58 | "Buffer name to use for displaying diffs." |
| 59 | :group 'smerge |
| 60 | :type '(choice |
| 61 | (const "*vc-diff*") |
| 62 | (const "*cvs-diff*") |
| 63 | (const "*smerge-diff*") |
| 64 | string)) |
| 65 | |
| 66 | (defcustom smerge-diff-switches |
| 67 | (append '("-d" "-b") |
| 68 | (if (listp diff-switches) diff-switches (list diff-switches))) |
| 69 | "*A list of strings specifying switches to be be passed to diff. |
| 70 | Used in `smerge-diff-base-mine' and related functions." |
| 71 | :group 'smerge |
| 72 | :type '(repeat string)) |
| 73 | |
| 74 | (defcustom smerge-auto-leave t |
| 75 | "*Non-nil means to leave `smerge-mode' when the last conflict is resolved." |
| 76 | :group 'smerge |
| 77 | :type 'boolean) |
| 78 | |
| 79 | (defface smerge-mine-face |
| 80 | '((((background light)) |
| 81 | (:foreground "blue")) |
| 82 | (((background dark)) |
| 83 | (:foreground "cyan"))) |
| 84 | "Face for your code." |
| 85 | :group 'smerge) |
| 86 | (defvar smerge-mine-face 'smerge-mine-face) |
| 87 | |
| 88 | (defface smerge-other-face |
| 89 | '((((background light)) |
| 90 | (:foreground "darkgreen")) |
| 91 | (((background dark)) |
| 92 | (:foreground "lightgreen"))) |
| 93 | "Face for the other code." |
| 94 | :group 'smerge) |
| 95 | (defvar smerge-other-face 'smerge-other-face) |
| 96 | |
| 97 | (defface smerge-base-face |
| 98 | '((((background light)) |
| 99 | (:foreground "red")) |
| 100 | (((background dark)) |
| 101 | (:foreground "orange"))) |
| 102 | "Face for the base code." |
| 103 | :group 'smerge) |
| 104 | (defvar smerge-base-face 'smerge-base-face) |
| 105 | |
| 106 | (defface smerge-markers-face |
| 107 | '((((background light)) |
| 108 | (:background "grey85")) |
| 109 | (((background dark)) |
| 110 | (:background "grey30"))) |
| 111 | "Face for the conflict markers." |
| 112 | :group 'smerge) |
| 113 | (defvar smerge-markers-face 'smerge-markers-face) |
| 114 | |
| 115 | (easy-mmode-defmap smerge-basic-map |
| 116 | '(("n" . smerge-next) |
| 117 | ("p" . smerge-prev) |
| 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 | ("d<" . smerge-diff-base-mine) |
| 125 | ("d>" . smerge-diff-base-other) |
| 126 | ("d=" . smerge-diff-mine-other)) |
| 127 | "The base keymap for `smerge-mode'.") |
| 128 | |
| 129 | (defcustom smerge-command-prefix "\e" |
| 130 | "Prefix for `smerge-mode' commands." |
| 131 | :group 'smerge |
| 132 | :type '(choice (string "\e") (string "C-x^") (string "") string)) |
| 133 | |
| 134 | (easy-mmode-defmap smerge-mode-map |
| 135 | `((,smerge-command-prefix . ,smerge-basic-map)) |
| 136 | "Keymap for `smerge-mode'.") |
| 137 | |
| 138 | (easy-menu-define smerge-mode-menu smerge-mode-map |
| 139 | "Menu for `smerge-mode'." |
| 140 | '("SMerge" |
| 141 | ["Next" smerge-next :help "Go to next conflict"] |
| 142 | ["Previous" smerge-prev :help "Go to previous conflict"] |
| 143 | ["Keep All" smerge-keep-all :help "Keep all three versions"] |
| 144 | ["Revert to Base" smerge-keep-base :help "Revert to base version"] |
| 145 | ["Keep Other" smerge-keep-other :help "Keep `other' version"] |
| 146 | ["Keep Yours" smerge-keep-mine :help "Keep your version"] |
| 147 | ["Keep Current" smerge-keep-current :help "Use current (at point) version"] |
| 148 | "--" |
| 149 | ["Diff Base/Mine" smerge-diff-base-mine |
| 150 | :help "Diff `base' and `mine' for current conflict"] |
| 151 | ["Diff Base/Other" smerge-diff-base-other |
| 152 | :help "Diff `base' and `other' for current conflict"] |
| 153 | ["Diff Mine/Other" smerge-diff-mine-other |
| 154 | :help "Diff `mine' and `other' for current conflict"] |
| 155 | "--" |
| 156 | ["Invoke Ediff" smerge-ediff |
| 157 | :help "Use Ediff to resolve the conflicts"] |
| 158 | )) |
| 159 | |
| 160 | (defconst smerge-font-lock-keywords |
| 161 | '((smerge-find-conflict |
| 162 | (1 smerge-mine-face prepend) |
| 163 | (2 smerge-base-face prepend t) |
| 164 | (3 smerge-other-face prepend t) |
| 165 | (0 smerge-markers-face keep) |
| 166 | (4 nil t t) |
| 167 | (5 nil t t))) |
| 168 | "Font lock patterns for `smerge-mode'.") |
| 169 | |
| 170 | (defconst smerge-begin-re "^<<<<<<< \\(.*\\)\n") |
| 171 | (defconst smerge-end-re "^>>>>>>> .*\n") |
| 172 | (defconst smerge-base-re "^||||||| .*\n") |
| 173 | (defconst smerge-other-re "^=======\n") |
| 174 | |
| 175 | (defvar smerge-conflict-style nil |
| 176 | "Keep track of which style of conflict is in use. |
| 177 | Can be nil if the style is undecided, or else: |
| 178 | - `diff3-E' |
| 179 | - `diff3-A'") |
| 180 | |
| 181 | ;; Compiler pacifiers |
| 182 | (defvar font-lock-mode nil) |
| 183 | (defvar font-lock-keywords nil) |
| 184 | (eval-when-compile |
| 185 | (unless (fboundp 'font-lock-fontify-region) |
| 186 | (autoload 'font-lock-fontify-region "font-lock"))) |
| 187 | |
| 188 | ;;;; |
| 189 | ;;;; Actual code |
| 190 | ;;;; |
| 191 | |
| 192 | ;; Define smerge-next and smerge-prev |
| 193 | (easy-mmode-define-navigation smerge smerge-begin-re "conflict") |
| 194 | |
| 195 | (defconst smerge-match-names ["conflict" "mine" "base" "other"]) |
| 196 | |
| 197 | (defun smerge-ensure-match (n) |
| 198 | (unless (match-end n) |
| 199 | (error (format "No `%s'" (aref smerge-match-names n))))) |
| 200 | |
| 201 | (defun smerge-auto-leave () |
| 202 | (when (and smerge-auto-leave |
| 203 | (save-excursion (goto-char (point-min)) |
| 204 | (not (re-search-forward smerge-begin-re nil t)))) |
| 205 | (smerge-mode -1))) |
| 206 | |
| 207 | |
| 208 | (defun smerge-keep-all () |
| 209 | "Keep all three versions. |
| 210 | Convenient for the kind of conflicts that can arise in ChangeLog files." |
| 211 | (interactive) |
| 212 | (smerge-match-conflict) |
| 213 | (replace-match (concat (or (match-string 1) "") |
| 214 | (or (match-string 2) "") |
| 215 | (or (match-string 3) "")) |
| 216 | t t) |
| 217 | (smerge-auto-leave)) |
| 218 | |
| 219 | (defun smerge-keep-base () |
| 220 | "Revert to the base version." |
| 221 | (interactive) |
| 222 | (smerge-match-conflict) |
| 223 | (smerge-ensure-match 2) |
| 224 | (replace-match (match-string 2) t t) |
| 225 | (smerge-auto-leave)) |
| 226 | |
| 227 | (defun smerge-keep-other () |
| 228 | "Use \"other\" version." |
| 229 | (interactive) |
| 230 | (smerge-match-conflict) |
| 231 | ;;(smerge-ensure-match 3) |
| 232 | (replace-match (match-string 3) t t) |
| 233 | (smerge-auto-leave)) |
| 234 | |
| 235 | (defun smerge-keep-mine () |
| 236 | "Keep your version." |
| 237 | (interactive) |
| 238 | (smerge-match-conflict) |
| 239 | ;;(smerge-ensure-match 1) |
| 240 | (replace-match (match-string 1) t t) |
| 241 | (smerge-auto-leave)) |
| 242 | |
| 243 | (defun smerge-keep-current () |
| 244 | "Use the current (under the cursor) version." |
| 245 | (interactive) |
| 246 | (smerge-match-conflict) |
| 247 | (let ((i 3)) |
| 248 | (while (or (not (match-end i)) |
| 249 | (< (point) (match-beginning i)) |
| 250 | (>= (point) (match-end i))) |
| 251 | (decf i)) |
| 252 | (if (<= i 0) (error "Not inside a version") |
| 253 | (replace-match (match-string i) t t) |
| 254 | (smerge-auto-leave)))) |
| 255 | |
| 256 | (defun smerge-diff-base-mine () |
| 257 | "Diff 'base' and 'mine' version in current conflict region." |
| 258 | (interactive) |
| 259 | (smerge-diff 2 1)) |
| 260 | |
| 261 | (defun smerge-diff-base-other () |
| 262 | "Diff 'base' and 'other' version in current conflict region." |
| 263 | (interactive) |
| 264 | (smerge-diff 2 3)) |
| 265 | |
| 266 | (defun smerge-diff-mine-other () |
| 267 | "Diff 'mine' and 'other' version in current conflict region." |
| 268 | (interactive) |
| 269 | (smerge-diff 1 3)) |
| 270 | |
| 271 | (defun smerge-match-conflict () |
| 272 | "Get info about the conflict. Puts the info in the `match-data'. |
| 273 | The submatches contain: |
| 274 | 0: the whole conflict. |
| 275 | 1: your code. |
| 276 | 2: the base code. |
| 277 | 3: other code. |
| 278 | An error is raised if not inside a conflict." |
| 279 | (save-excursion |
| 280 | (condition-case nil |
| 281 | (let* ((orig-point (point)) |
| 282 | |
| 283 | (_ (forward-line 1)) |
| 284 | (_ (re-search-backward smerge-begin-re)) |
| 285 | |
| 286 | (start (match-beginning 0)) |
| 287 | (mine-start (match-end 0)) |
| 288 | (filename (match-string 1)) |
| 289 | |
| 290 | (_ (re-search-forward smerge-end-re)) |
| 291 | (_ (assert (< orig-point (match-end 0)))) |
| 292 | |
| 293 | (other-end (match-beginning 0)) |
| 294 | (end (match-end 0)) |
| 295 | |
| 296 | (_ (re-search-backward smerge-other-re start)) |
| 297 | |
| 298 | (mine-end (match-beginning 0)) |
| 299 | (other-start (match-end 0)) |
| 300 | |
| 301 | base-start base-end) |
| 302 | |
| 303 | ;; handle the various conflict styles |
| 304 | (cond |
| 305 | ((re-search-backward smerge-base-re start t) |
| 306 | ;; a 3-parts conflict |
| 307 | (set (make-local-variable 'smerge-conflict-style) 'diff3-A) |
| 308 | (setq base-end mine-end) |
| 309 | (setq mine-end (match-beginning 0)) |
| 310 | (setq base-start (match-end 0))) |
| 311 | |
| 312 | ((string= filename (file-name-nondirectory |
| 313 | (or buffer-file-name ""))) |
| 314 | ;; a 2-parts conflict |
| 315 | (set (make-local-variable 'smerge-conflict-style) 'diff3-E)) |
| 316 | |
| 317 | ((and (not base-start) |
| 318 | (or (eq smerge-conflict-style 'diff3-A) |
| 319 | (string-match "^[.0-9]+\\'" filename))) |
| 320 | ;; a same-diff conflict |
| 321 | (setq base-start mine-start) |
| 322 | (setq base-end mine-end) |
| 323 | (setq mine-start other-start) |
| 324 | (setq mine-end other-end))) |
| 325 | |
| 326 | (store-match-data (list start end |
| 327 | mine-start mine-end |
| 328 | base-start base-end |
| 329 | other-start other-end |
| 330 | (when base-start (1- base-start)) base-start |
| 331 | (1- other-start) other-start)) |
| 332 | t) |
| 333 | (error "Point not in conflict region")))) |
| 334 | |
| 335 | (defun smerge-find-conflict (&optional limit) |
| 336 | "Find and match a conflict region. Intended as a font-lock MATCHER. |
| 337 | The submatches are the same as in `smerge-match-conflict'. |
| 338 | Returns non-nil if a match is found between the point and LIMIT. |
| 339 | The point is moved to the end of the conflict." |
| 340 | (when (re-search-forward smerge-begin-re limit t) |
| 341 | (ignore-errors |
| 342 | (smerge-match-conflict) |
| 343 | (goto-char (match-end 0))))) |
| 344 | |
| 345 | (defun smerge-diff (n1 n2) |
| 346 | (smerge-match-conflict) |
| 347 | (smerge-ensure-match n1) |
| 348 | (smerge-ensure-match n2) |
| 349 | (let ((name1 (aref smerge-match-names n1)) |
| 350 | (name2 (aref smerge-match-names n2)) |
| 351 | (file1 (make-temp-file "smerge1")) |
| 352 | (file2 (make-temp-file "smerge2")) |
| 353 | (dir default-directory) |
| 354 | (file (file-relative-name buffer-file-name))) |
| 355 | (write-region (match-beginning n1) (match-end n1) file1) |
| 356 | (write-region (match-beginning n2) (match-end n2) file2) |
| 357 | (unwind-protect |
| 358 | (with-current-buffer (get-buffer-create smerge-diff-buffer-name) |
| 359 | (setq default-directory dir) |
| 360 | (let ((inhibit-read-only t)) |
| 361 | (erase-buffer) |
| 362 | (apply 'call-process diff-command nil t nil |
| 363 | (append smerge-diff-switches |
| 364 | (list "-L" (concat name1 "/" file) |
| 365 | "-L" (concat name2 "/" file) |
| 366 | file1 file2)))) |
| 367 | (goto-char (point-min)) |
| 368 | (diff-mode) |
| 369 | (display-buffer (current-buffer) t)) |
| 370 | (delete-file file1) |
| 371 | (delete-file file2)))) |
| 372 | |
| 373 | (eval-when-compile |
| 374 | ;; compiler pacifiers |
| 375 | (defvar smerge-ediff-windows) |
| 376 | (defvar smerge-ediff-buf) |
| 377 | (defvar ediff-buffer-A) |
| 378 | (defvar ediff-buffer-B) |
| 379 | (defvar ediff-buffer-C) |
| 380 | (unless (fboundp 'ediff-cleanup-mess) |
| 381 | (autoload 'ediff-cleanup-mess "ediff-util"))) |
| 382 | |
| 383 | (defun smerge-ediff () |
| 384 | "Invoke ediff to resolve the conflicts." |
| 385 | (interactive) |
| 386 | (let* ((buf (current-buffer)) |
| 387 | (mode major-mode) |
| 388 | ;;(ediff-default-variant 'default-B) |
| 389 | (config (current-window-configuration)) |
| 390 | (filename (file-name-nondirectory buffer-file-name)) |
| 391 | (mine (generate-new-buffer (concat "*" filename " MINE*"))) |
| 392 | (other (generate-new-buffer (concat "*" filename " OTHER*"))) |
| 393 | base) |
| 394 | (with-current-buffer mine |
| 395 | (buffer-disable-undo) |
| 396 | (insert-buffer-substring buf) |
| 397 | (goto-char (point-min)) |
| 398 | (while (smerge-find-conflict) |
| 399 | (when (match-beginning 2) (setq base t)) |
| 400 | (replace-match (match-string 1) t t)) |
| 401 | (buffer-enable-undo) |
| 402 | (set-buffer-modified-p nil) |
| 403 | (funcall mode)) |
| 404 | |
| 405 | (with-current-buffer other |
| 406 | (buffer-disable-undo) |
| 407 | (insert-buffer-substring buf) |
| 408 | (goto-char (point-min)) |
| 409 | (while (smerge-find-conflict) |
| 410 | (replace-match (match-string 3) t t)) |
| 411 | (buffer-enable-undo) |
| 412 | (set-buffer-modified-p nil) |
| 413 | (funcall mode)) |
| 414 | |
| 415 | (when base |
| 416 | (setq base (generate-new-buffer (concat "*" filename " BASE*"))) |
| 417 | (with-current-buffer base |
| 418 | (buffer-disable-undo) |
| 419 | (insert-buffer-substring buf) |
| 420 | (goto-char (point-min)) |
| 421 | (while (smerge-find-conflict) |
| 422 | (replace-match (or (match-string 2) "") t t)) |
| 423 | (buffer-enable-undo) |
| 424 | (set-buffer-modified-p nil) |
| 425 | (funcall mode))) |
| 426 | |
| 427 | ;; the rest of the code is inspired from vc.el |
| 428 | ;; Fire up ediff. |
| 429 | (set-buffer |
| 430 | (if base |
| 431 | (ediff-merge-buffers-with-ancestor mine other base) |
| 432 | ;; nil 'ediff-merge-revisions-with-ancestor buffer-file-name) |
| 433 | (ediff-merge-buffers mine other))) |
| 434 | ;; nil 'ediff-merge-revisions buffer-file-name))) |
| 435 | |
| 436 | ;; Ediff is now set up, and we are in the control buffer. |
| 437 | ;; Do a few further adjustments and take precautions for exit. |
| 438 | (set (make-local-variable 'smerge-ediff-windows) config) |
| 439 | (set (make-local-variable 'smerge-ediff-buf) buf) |
| 440 | (set (make-local-variable 'ediff-quit-hook) |
| 441 | (lambda () |
| 442 | (let ((buffer-A ediff-buffer-A) |
| 443 | (buffer-B ediff-buffer-B) |
| 444 | (buffer-C ediff-buffer-C) |
| 445 | (buffer-Ancestor ediff-ancestor-buffer) |
| 446 | (buf smerge-ediff-buf) |
| 447 | (windows smerge-ediff-windows)) |
| 448 | (ediff-cleanup-mess) |
| 449 | (with-current-buffer buf |
| 450 | (erase-buffer) |
| 451 | (insert-buffer buffer-C) |
| 452 | (kill-buffer buffer-A) |
| 453 | (kill-buffer buffer-B) |
| 454 | (kill-buffer buffer-C) |
| 455 | (when (bufferp buffer-Ancestor) (kill-buffer buffer-Ancestor)) |
| 456 | (set-window-configuration windows) |
| 457 | (message "Conflict resolution finished; you may save the buffer"))))) |
| 458 | (message "Please resolve conflicts now; exit ediff when done"))) |
| 459 | |
| 460 | |
| 461 | ;;;###autoload |
| 462 | (define-minor-mode smerge-mode |
| 463 | "Minor mode to simplify editing output from the diff3 program. |
| 464 | \\{smerge-mode-map}" |
| 465 | nil " SMerge" nil |
| 466 | (when font-lock-mode |
| 467 | (save-excursion |
| 468 | (if smerge-mode |
| 469 | (font-lock-add-keywords nil smerge-font-lock-keywords 'append) |
| 470 | (font-lock-remove-keywords nil smerge-font-lock-keywords)) |
| 471 | (goto-char (point-min)) |
| 472 | (while (smerge-find-conflict) |
| 473 | (font-lock-fontify-region (match-beginning 0) (match-end 0) nil))))) |
| 474 | |
| 475 | |
| 476 | (provide 'smerge-mode) |
| 477 | |
| 478 | ;;; Change Log: |
| 479 | ;; $Log: smerge-mode.el,v $ |
| 480 | ;; Revision 1.6 2000/10/05 06:05:51 miles |
| 481 | ;; (smerge-mine-face, smerge-other-face, smerge-base-face, smerge-markers-face): |
| 482 | ;; Add dark-background variants. |
| 483 | ;; |
| 484 | ;; Revision 1.5 2000/08/16 19:51:55 monnier |
| 485 | ;; (smerge-mode-menu): Doc fix. |
| 486 | ;; |
| 487 | ;; Revision 1.4 2000/07/21 13:52:19 fx |
| 488 | ;; (smerge-mode-menu): Fill it out. |
| 489 | ;; |
| 490 | ;; Revision 1.3 2000/05/25 18:08:26 fx |
| 491 | ;; (smerge-diff-switches): Don't use list* in defcustom. |
| 492 | ;; |
| 493 | ;; Revision 1.2 2000/03/22 00:54:55 monnier |
| 494 | ;; (smerge-auto-leave): New function and variable. |
| 495 | ;; (smerge-basic-map): Rename from smerge-basic-keymap. |
| 496 | ;; Change the bindings for smerge-diff-*. |
| 497 | ;; (smerge-*-map): Use easy-mmode-defmap. |
| 498 | ;; (smerge-(next|prev)): Use easy-mmode-define-navigation. |
| 499 | ;; (smerge-keep-*): Use smerge-auto-leave. |
| 500 | ;; |
| 501 | ;; Revision 1.1 1999/12/09 13:00:21 monnier |
| 502 | ;; New file. Provides a simple minor-mode for files containing |
| 503 | ;; diff3-style conflict markers, such as generated by RCS |
| 504 | ;; |
| 505 | |
| 506 | ;;; smerge-mode.el ends here |