Switch to recommended form of GPLv3 permissions notice.
[emacs.git] / lisp / smerge-mode.el
blob3195a67e6ccf900415237f03f7a0b3faa6de66bd
1 ;;; smerge-mode.el --- Minor mode to resolve diff3 conflicts
3 ;; Copyright (C) 1999, 2000, 2001, 2002, 2003,
4 ;; 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
6 ;; Author: Stefan Monnier <monnier@iro.umontreal.ca>
7 ;; Keywords: tools revision-control merge diff3 cvs conflict
9 ;; This file is part of GNU Emacs.
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 3 of the License, or
14 ;; (at your option) any later version.
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.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; Provides a lightweight alternative to emerge/ediff.
27 ;; To use it, simply add to your .emacs the following lines:
29 ;; (autoload 'smerge-mode "smerge-mode" nil t)
31 ;; you can even have it turned on automatically with the following
32 ;; piece of code in your .emacs:
34 ;; (defun sm-try-smerge ()
35 ;; (save-excursion
36 ;; (goto-char (point-min))
37 ;; (when (re-search-forward "^<<<<<<< " nil t)
38 ;; (smerge-mode 1))))
39 ;; (add-hook 'find-file-hook 'sm-try-smerge t)
41 ;;; Todo:
43 ;; - if requested, ask the user whether he wants to call ediff right away
45 ;;; Code:
47 (eval-when-compile (require 'cl))
50 ;;; The real definition comes later.
51 (defvar smerge-mode)
53 (defgroup smerge ()
54 "Minor mode to highlight and resolve diff3 conflicts."
55 :group 'tools
56 :prefix "smerge-")
58 (defcustom smerge-diff-buffer-name "*vc-diff*"
59 "Buffer name to use for displaying diffs."
60 :group 'smerge
61 :type '(choice
62 (const "*vc-diff*")
63 (const "*cvs-diff*")
64 (const "*smerge-diff*")
65 string))
67 (defcustom smerge-diff-switches
68 (append '("-d" "-b")
69 (if (listp diff-switches) diff-switches (list diff-switches)))
70 "A list of strings specifying switches to be passed to diff.
71 Used in `smerge-diff-base-mine' and related functions."
72 :group 'smerge
73 :type '(repeat string))
75 (defcustom smerge-auto-leave t
76 "Non-nil means to leave `smerge-mode' when the last conflict is resolved."
77 :group 'smerge
78 :type 'boolean)
80 (defcustom smerge-auto-refine t
81 "Automatically highlight changes in detail as the user visits conflicts."
82 :group 'smerge
83 :type 'boolean)
85 (defface smerge-mine
86 '((((min-colors 88) (background light))
87 (:foreground "blue1"))
88 (((background light))
89 (:foreground "blue"))
90 (((min-colors 88) (background dark))
91 (:foreground "cyan1"))
92 (((background dark))
93 (:foreground "cyan")))
94 "Face for your code."
95 :group 'smerge)
96 ;; backward-compatibility alias
97 (put 'smerge-mine-face 'face-alias 'smerge-mine)
98 (defvar smerge-mine-face 'smerge-mine)
100 (defface smerge-other
101 '((((background light))
102 (:foreground "darkgreen"))
103 (((background dark))
104 (:foreground "lightgreen")))
105 "Face for the other code."
106 :group 'smerge)
107 ;; backward-compatibility alias
108 (put 'smerge-other-face 'face-alias 'smerge-other)
109 (defvar smerge-other-face 'smerge-other)
111 (defface smerge-base
112 '((((min-colors 88) (background light))
113 (:foreground "red1"))
114 (((background light))
115 (:foreground "red"))
116 (((background dark))
117 (:foreground "orange")))
118 "Face for the base code."
119 :group 'smerge)
120 ;; backward-compatibility alias
121 (put 'smerge-base-face 'face-alias 'smerge-base)
122 (defvar smerge-base-face 'smerge-base)
124 (defface smerge-markers
125 '((((background light))
126 (:background "grey85"))
127 (((background dark))
128 (:background "grey30")))
129 "Face for the conflict markers."
130 :group 'smerge)
131 ;; backward-compatibility alias
132 (put 'smerge-markers-face 'face-alias 'smerge-markers)
133 (defvar smerge-markers-face 'smerge-markers)
135 (defface smerge-refined-change
136 '((t :background "yellow"))
137 "Face used for char-based changes shown by `smerge-refine'."
138 :group 'smerge)
140 (easy-mmode-defmap smerge-basic-map
141 `(("n" . smerge-next)
142 ("p" . smerge-prev)
143 ("r" . smerge-resolve)
144 ("a" . smerge-keep-all)
145 ("b" . smerge-keep-base)
146 ("o" . smerge-keep-other)
147 ("m" . smerge-keep-mine)
148 ("E" . smerge-ediff)
149 ("C" . smerge-combine-with-next)
150 ("R" . smerge-refine)
151 ("\C-m" . smerge-keep-current)
152 ("=" . ,(make-sparse-keymap "Diff"))
153 ("=<" "base-mine" . smerge-diff-base-mine)
154 ("=>" "base-other" . smerge-diff-base-other)
155 ("==" "mine-other" . smerge-diff-mine-other))
156 "The base keymap for `smerge-mode'.")
158 (defcustom smerge-command-prefix "\C-c^"
159 "Prefix for `smerge-mode' commands."
160 :group 'smerge
161 :type '(choice (string "\e") (string "\C-c^") (string "") string))
163 (easy-mmode-defmap smerge-mode-map
164 `((,smerge-command-prefix . ,smerge-basic-map))
165 "Keymap for `smerge-mode'.")
167 (defvar smerge-check-cache nil)
168 (make-variable-buffer-local 'smerge-check-cache)
169 (defun smerge-check (n)
170 (condition-case nil
171 (let ((state (cons (point) (buffer-modified-tick))))
172 (unless (equal (cdr smerge-check-cache) state)
173 (smerge-match-conflict)
174 (setq smerge-check-cache (cons (match-data) state)))
175 (nth (* 2 n) (car smerge-check-cache)))
176 (error nil)))
178 (easy-menu-define smerge-mode-menu smerge-mode-map
179 "Menu for `smerge-mode'."
180 '("SMerge"
181 ["Next" smerge-next :help "Go to next conflict"]
182 ["Previous" smerge-prev :help "Go to previous conflict"]
183 "--"
184 ["Keep All" smerge-keep-all :help "Keep all three versions"
185 :active (smerge-check 1)]
186 ["Keep Current" smerge-keep-current :help "Use current (at point) version"
187 :active (and (smerge-check 1) (> (smerge-get-current) 0))]
188 "--"
189 ["Revert to Base" smerge-keep-base :help "Revert to base version"
190 :active (smerge-check 2)]
191 ["Keep Other" smerge-keep-other :help "Keep `other' version"
192 :active (smerge-check 3)]
193 ["Keep Yours" smerge-keep-mine :help "Keep your version"
194 :active (smerge-check 1)]
195 "--"
196 ["Diff Base/Mine" smerge-diff-base-mine
197 :help "Diff `base' and `mine' for current conflict"
198 :active (smerge-check 2)]
199 ["Diff Base/Other" smerge-diff-base-other
200 :help "Diff `base' and `other' for current conflict"
201 :active (smerge-check 2)]
202 ["Diff Mine/Other" smerge-diff-mine-other
203 :help "Diff `mine' and `other' for current conflict"
204 :active (smerge-check 1)]
205 "--"
206 ["Invoke Ediff" smerge-ediff
207 :help "Use Ediff to resolve the conflicts"
208 :active (smerge-check 1)]
209 ["Auto Resolve" smerge-resolve
210 :help "Try auto-resolution heuristics"
211 :active (smerge-check 1)]
212 ["Combine" smerge-combine-with-next
213 :help "Combine current conflict with next"
214 :active (smerge-check 1)]
217 (easy-menu-define smerge-context-menu nil
218 "Context menu for mine area in `smerge-mode'."
219 '(nil
220 ["Keep Current" smerge-keep-current :help "Use current (at point) version"]
221 ["Kill Current" smerge-kill-current :help "Remove current (at point) version"]
222 ["Keep All" smerge-keep-all :help "Keep all three versions"]
223 "---"
224 ["More..." (popup-menu smerge-mode-menu) :help "Show full SMerge mode menu"]
227 (defconst smerge-font-lock-keywords
228 '((smerge-find-conflict
229 (1 smerge-mine-face prepend t)
230 (2 smerge-base-face prepend t)
231 (3 smerge-other-face prepend t)
232 ;; FIXME: `keep' doesn't work right with syntactic fontification.
233 (0 smerge-markers-face keep)
234 (4 nil t t)
235 (5 nil t t)))
236 "Font lock patterns for `smerge-mode'.")
238 (defconst smerge-begin-re "^<<<<<<< \\(.*\\)\n")
239 (defconst smerge-end-re "^>>>>>>> .*\n")
240 (defconst smerge-base-re "^||||||| .*\n")
241 (defconst smerge-other-re "^=======\n")
243 (defvar smerge-conflict-style nil
244 "Keep track of which style of conflict is in use.
245 Can be nil if the style is undecided, or else:
246 - `diff3-E'
247 - `diff3-A'")
249 ;; Compiler pacifiers
250 (defvar font-lock-mode)
251 (defvar font-lock-keywords)
253 ;;;;
254 ;;;; Actual code
255 ;;;;
257 ;; Define smerge-next and smerge-prev
258 (easy-mmode-define-navigation smerge smerge-begin-re "conflict" nil nil
259 (if smerge-auto-refine
260 (condition-case nil (smerge-refine) (error nil))))
262 (defconst smerge-match-names ["conflict" "mine" "base" "other"])
264 (defun smerge-ensure-match (n)
265 (unless (match-end n)
266 (error "No `%s'" (aref smerge-match-names n))))
268 (defun smerge-auto-leave ()
269 (when (and smerge-auto-leave
270 (save-excursion (goto-char (point-min))
271 (not (re-search-forward smerge-begin-re nil t))))
272 (when (and (listp buffer-undo-list) smerge-mode)
273 (push (list 'apply 'smerge-mode 1) buffer-undo-list))
274 (smerge-mode -1)))
277 (defun smerge-keep-all ()
278 "Concatenate all versions."
279 (interactive)
280 (smerge-match-conflict)
281 (let ((mb2 (or (match-beginning 2) (point-max)))
282 (me2 (or (match-end 2) (point-min))))
283 (delete-region (match-end 3) (match-end 0))
284 (delete-region (max me2 (match-end 1)) (match-beginning 3))
285 (if (and (match-end 2) (/= (match-end 1) (match-end 3)))
286 (delete-region (match-end 1) (match-beginning 2)))
287 (delete-region (match-beginning 0) (min (match-beginning 1) mb2))
288 (smerge-auto-leave)))
290 (defun smerge-keep-n (n)
291 (smerge-remove-props (match-beginning 0) (match-end 0))
292 ;; We used to use replace-match, but that did not preserve markers so well.
293 (delete-region (match-end n) (match-end 0))
294 (delete-region (match-beginning 0) (match-beginning n)))
296 (defun smerge-combine-with-next ()
297 "Combine the current conflict with the next one."
298 ;; `smerge-auto-combine' relies on the finish position (at the beginning
299 ;; of the closing marker).
300 (interactive)
301 (smerge-match-conflict)
302 (let ((ends nil))
303 (dolist (i '(3 2 1 0))
304 (push (if (match-end i) (copy-marker (match-end i) t)) ends))
305 (setq ends (apply 'vector ends))
306 (goto-char (aref ends 0))
307 (if (not (re-search-forward smerge-begin-re nil t))
308 (error "No next conflict")
309 (smerge-match-conflict)
310 (let ((match-data (mapcar (lambda (m) (if m (copy-marker m)))
311 (match-data))))
312 ;; First copy the in-between text in each alternative.
313 (dolist (i '(1 2 3))
314 (when (aref ends i)
315 (goto-char (aref ends i))
316 (insert-buffer-substring (current-buffer)
317 (aref ends 0) (car match-data))))
318 (delete-region (aref ends 0) (car match-data))
319 ;; Then move the second conflict's alternatives into the first.
320 (dolist (i '(1 2 3))
321 (set-match-data match-data)
322 (when (and (aref ends i) (match-end i))
323 (goto-char (aref ends i))
324 (insert-buffer-substring (current-buffer)
325 (match-beginning i) (match-end i))))
326 (delete-region (car match-data) (cadr match-data))
327 ;; Free the markers.
328 (dolist (m match-data) (if m (move-marker m nil)))
329 (mapc (lambda (m) (if m (move-marker m nil))) ends)))))
331 (defvar smerge-auto-combine-max-separation 2
332 "Max number of lines between conflicts that should be combined.")
334 (defun smerge-auto-combine ()
335 "Automatically combine conflicts that are near each other."
336 (interactive)
337 (save-excursion
338 (goto-char (point-min))
339 (while (smerge-find-conflict)
340 ;; 2 is 1 (default) + 1 (the begin markers).
341 (while (save-excursion
342 (smerge-find-conflict
343 (line-beginning-position
344 (+ 2 smerge-auto-combine-max-separation))))
345 (forward-line -1) ;Go back inside the conflict.
346 (smerge-combine-with-next)
347 (forward-line 1) ;Move past the end of the conflict.
348 ))))
350 (defvar smerge-resolve-function
351 (lambda () (error "Don't know how to resolve"))
352 "Mode-specific merge function.
353 The function is called with zero or one argument (non-nil if the resolution
354 function should only apply safe heuristics) and with the match data set
355 according to `smerge-match-conflict'.")
356 (add-to-list 'debug-ignored-errors "Don't know how to resolve")
358 (defvar smerge-text-properties
359 `(help-echo "merge conflict: mouse-3 shows a menu"
360 ;; mouse-face highlight
361 keymap (keymap (down-mouse-3 . smerge-popup-context-menu))))
363 (defun smerge-remove-props (beg end)
364 (remove-overlays beg end 'smerge 'refine)
365 (remove-overlays beg end 'smerge 'conflict)
366 ;; Now that we use overlays rather than text-properties, this function
367 ;; does not cause refontification any more. It can be seen very clearly
368 ;; in buffers where jit-lock-contextually is not t, in which case deleting
369 ;; the "<<<<<<< foobar" leading line leaves the rest of the conflict
370 ;; highlighted as if it were still a valid conflict. Note that in many
371 ;; important cases (such as the previous example) we're actually called
372 ;; during font-locking so inhibit-modification-hooks is non-nil, so we
373 ;; can't just modify the buffer and expect font-lock to be triggered as in:
374 ;; (put-text-property beg end 'smerge-force-highlighting nil)
375 (let ((modified (buffer-modified-p)))
376 (remove-text-properties beg end '(fontified nil))
377 (restore-buffer-modified-p modified)))
379 (defun smerge-popup-context-menu (event)
380 "Pop up the Smerge mode context menu under mouse."
381 (interactive "e")
382 (if (and smerge-mode
383 (save-excursion (posn-set-point (event-end event)) (smerge-check 1)))
384 (progn
385 (posn-set-point (event-end event))
386 (smerge-match-conflict)
387 (let ((i (smerge-get-current))
389 (if (<= i 0)
390 ;; Out of range
391 (popup-menu smerge-mode-menu)
392 ;; Install overlay.
393 (setq o (make-overlay (match-beginning i) (match-end i)))
394 (unwind-protect
395 (progn
396 (overlay-put o 'face 'highlight)
397 (sit-for 0) ;Display the new highlighting.
398 (popup-menu smerge-context-menu))
399 ;; Delete overlay.
400 (delete-overlay o)))))
401 ;; There's no conflict at point, the text-props are just obsolete.
402 (save-excursion
403 (let ((beg (re-search-backward smerge-end-re nil t))
404 (end (re-search-forward smerge-begin-re nil t)))
405 (smerge-remove-props (or beg (point-min)) (or end (point-max)))
406 (push event unread-command-events)))))
408 (defun smerge-apply-resolution-patch (buf m0b m0e m3b m3e &optional m2b)
409 "Replace the conflict with a bunch of subconflicts.
410 BUF contains a plain diff between match-1 and match-3."
411 (let ((line 1)
412 (textbuf (current-buffer))
413 (name1 (progn (goto-char m0b)
414 (buffer-substring (+ (point) 8) (line-end-position))))
415 (name2 (when m2b (goto-char m2b) (forward-line -1)
416 (buffer-substring (+ (point) 8) (line-end-position))))
417 (name3 (progn (goto-char m0e) (forward-line -1)
418 (buffer-substring (+ (point) 8) (line-end-position)))))
419 (smerge-remove-props m0b m0e)
420 (delete-region m3e m0e)
421 (delete-region m0b m3b)
422 (setq m3b m0b)
423 (setq m3e (- m3e (- m3b m0b)))
424 (goto-char m3b)
425 (with-current-buffer buf
426 (goto-char (point-min))
427 (while (not (eobp))
428 (if (not (looking-at "\\([0-9]+\\)\\(?:,\\([0-9]+\\)\\)?\\([acd]\\)\\([0-9]+\\)\\(?:,\\([0-9]+\\)\\)?$"))
429 (error "Unexpected patch hunk header: %s"
430 (buffer-substring (point) (line-end-position)))
431 (let* ((op (char-after (match-beginning 3)))
432 (startline (+ (string-to-number (match-string 1))
433 ;; No clue why this is the way it is, but line
434 ;; numbers seem to be off-by-one for `a' ops.
435 (if (eq op ?a) 1 0)))
436 (endline (if (eq op ?a) startline
437 (1+ (if (match-end 2)
438 (string-to-number (match-string 2))
439 startline))))
440 (lines (- endline startline))
441 (otherlines (cond
442 ((eq op ?d) nil)
443 ((null (match-end 5)) 1)
444 (t (- (string-to-number (match-string 5))
445 (string-to-number (match-string 4)) -1))))
446 othertext)
447 (forward-line 1) ;Skip header.
448 (forward-line lines) ;Skip deleted text.
449 (if (eq op ?c) (forward-line 1)) ;Skip separator.
450 (setq othertext
451 (if (null otherlines) ""
452 (let ((pos (point)))
453 (dotimes (i otherlines) (delete-char 2) (forward-line 1))
454 (buffer-substring pos (point)))))
455 (with-current-buffer textbuf
456 (forward-line (- startline line))
457 (insert "<<<<<<< " name1 "\n" othertext
458 (if name2 (concat "||||||| " name2 "\n"))
459 "=======\n")
460 (forward-line lines)
461 (insert ">>>>>>> " name3 "\n")
462 (setq line endline))))))))
464 (defun smerge-resolve (&optional safe)
465 "Resolve the conflict at point intelligently.
466 This relies on mode-specific knowledge and thus only works in
467 some major modes. Uses `smerge-resolve-function' to do the actual work."
468 (interactive)
469 (smerge-match-conflict)
470 (smerge-remove-props (match-beginning 0) (match-end 0))
471 (let ((md (match-data))
472 (m0b (match-beginning 0))
473 (m1b (match-beginning 1))
474 (m2b (match-beginning 2))
475 (m3b (match-beginning 3))
476 (m0e (match-end 0))
477 (m1e (match-end 1))
478 (m2e (match-end 2))
479 (m3e (match-end 3))
480 (buf (generate-new-buffer " *smerge*"))
481 m b o)
482 (unwind-protect
483 (progn
484 (cond
485 ;; Trivial diff3 -A non-conflicts.
486 ((and (eq (match-end 1) (match-end 3))
487 (eq (match-beginning 1) (match-beginning 3)))
488 (smerge-keep-n 3))
489 ;; Mode-specific conflict resolution.
490 ((condition-case nil
491 (atomic-change-group
492 (if safe
493 (funcall smerge-resolve-function safe)
494 (funcall smerge-resolve-function))
496 (error nil))
497 ;; Nothing to do: the resolution function has done it already.
498 nil)
499 ;; Non-conflict.
500 ((and (eq m1e m3e) (eq m1b m3b))
501 (set-match-data md) (smerge-keep-n 3))
502 ;; Refine a 2-way conflict using "diff -b".
503 ;; In case of a 3-way conflict with an empty base
504 ;; (i.e. 2 conflicting additions), we do the same, presuming
505 ;; that the 2 additions should be somehow merged rather
506 ;; than concatenated.
507 ((let ((lines (count-lines m3b m3e)))
508 (setq m (make-temp-file "smm"))
509 (write-region m1b m1e m nil 'silent)
510 (setq o (make-temp-file "smo"))
511 (write-region m3b m3e o nil 'silent)
512 (not (or (eq m1b m1e) (eq m3b m3e)
513 (and (not (zerop (call-process diff-command
514 nil buf nil "-b" o m)))
515 ;; TODO: We don't know how to do the refinement
516 ;; if there's a non-empty ancestor and m1 and m3
517 ;; aren't just plain equal.
518 m2b (not (eq m2b m2e)))
519 (with-current-buffer buf
520 (goto-char (point-min))
521 ;; Make sure there's some refinement.
522 (looking-at
523 (concat "1," (number-to-string lines) "c"))))))
524 (smerge-apply-resolution-patch buf m0b m0e m3b m3e m2b))
525 ;; "Mere whitespace changes" conflicts.
526 ((when m2e
527 (setq b (make-temp-file "smb"))
528 (write-region m2b m2e b nil 'silent)
529 (with-current-buffer buf (erase-buffer))
530 ;; Only minor whitespace changes made locally.
531 ;; BEWARE: pass "-c" 'cause the output is reused in the next test.
532 (zerop (call-process diff-command nil buf nil "-bc" b m)))
533 (set-match-data md)
534 (smerge-keep-n 3))
535 ;; Try "diff -b BASE MINE | patch OTHER".
536 ((when (and (not safe) m2e b
537 ;; If the BASE is empty, this would just concatenate
538 ;; the two, which is rarely right.
539 (not (eq m2b m2e)))
540 ;; BEWARE: we're using here the patch of the previous test.
541 (with-current-buffer buf
542 (zerop (call-process-region
543 (point-min) (point-max) "patch" t nil nil
544 "-r" "/dev/null" "--no-backup-if-mismatch"
545 "-fl" o))))
546 (save-restriction
547 (narrow-to-region m0b m0e)
548 (smerge-remove-props m0b m0e)
549 (insert-file-contents o nil nil nil t)))
550 ;; Try "diff -b BASE OTHER | patch MINE".
551 ((when (and (not safe) m2e b
552 ;; If the BASE is empty, this would just concatenate
553 ;; the two, which is rarely right.
554 (not (eq m2b m2e)))
555 (write-region m3b m3e o nil 'silent)
556 (call-process diff-command nil buf nil "-bc" b o)
557 (with-current-buffer buf
558 (zerop (call-process-region
559 (point-min) (point-max) "patch" t nil nil
560 "-r" "/dev/null" "--no-backup-if-mismatch"
561 "-fl" m))))
562 (save-restriction
563 (narrow-to-region m0b m0e)
564 (smerge-remove-props m0b m0e)
565 (insert-file-contents m nil nil nil t)))
567 (error "Don't know how to resolve"))))
568 (if (buffer-name buf) (kill-buffer buf))
569 (if m (delete-file m))
570 (if b (delete-file b))
571 (if o (delete-file o))))
572 (smerge-auto-leave))
574 (defun smerge-resolve-all ()
575 "Perform automatic resolution on all conflicts."
576 (interactive)
577 (save-excursion
578 (goto-char (point-min))
579 (while (re-search-forward smerge-begin-re nil t)
580 (condition-case nil
581 (progn
582 (smerge-match-conflict)
583 (smerge-resolve 'safe))
584 (error nil)))))
586 (defun smerge-batch-resolve ()
587 ;; command-line-args-left is what is left of the command line.
588 (if (not noninteractive)
589 (error "`smerge-batch-resolve' is to be used only with -batch"))
590 (while command-line-args-left
591 (let ((file (pop command-line-args-left)))
592 (if (string-match "\\.rej\\'" file)
593 ;; .rej files should never contain diff3 markers, on the other hand,
594 ;; in Arch, .rej files are sometimes used to indicate that the
595 ;; main file has diff3 markers. So you can pass **/*.rej and
596 ;; it will DTRT.
597 (setq file (substring file 0 (match-beginning 0))))
598 (message "Resolving conflicts in %s..." file)
599 (when (file-readable-p file)
600 (with-current-buffer (find-file-noselect file)
601 (smerge-resolve-all)
602 (save-buffer)
603 (kill-buffer (current-buffer)))))))
605 (defun smerge-keep-base ()
606 "Revert to the base version."
607 (interactive)
608 (smerge-match-conflict)
609 (smerge-ensure-match 2)
610 (smerge-keep-n 2)
611 (smerge-auto-leave))
613 (defun smerge-keep-other ()
614 "Use \"other\" version."
615 (interactive)
616 (smerge-match-conflict)
617 ;;(smerge-ensure-match 3)
618 (smerge-keep-n 3)
619 (smerge-auto-leave))
621 (defun smerge-keep-mine ()
622 "Keep your version."
623 (interactive)
624 (smerge-match-conflict)
625 ;;(smerge-ensure-match 1)
626 (smerge-keep-n 1)
627 (smerge-auto-leave))
629 (defun smerge-get-current ()
630 (let ((i 3))
631 (while (or (not (match-end i))
632 (< (point) (match-beginning i))
633 (>= (point) (match-end i)))
634 (decf i))
637 (defun smerge-keep-current ()
638 "Use the current (under the cursor) version."
639 (interactive)
640 (smerge-match-conflict)
641 (let ((i (smerge-get-current)))
642 (if (<= i 0) (error "Not inside a version")
643 (smerge-keep-n i)
644 (smerge-auto-leave))))
646 (defun smerge-kill-current ()
647 "Remove the current (under the cursor) version."
648 (interactive)
649 (smerge-match-conflict)
650 (let ((i (smerge-get-current)))
651 (if (<= i 0) (error "Not inside a version")
652 (let ((left nil))
653 (dolist (n '(3 2 1))
654 (if (and (match-end n) (/= (match-end n) (match-end i)))
655 (push n left)))
656 (if (and (cdr left)
657 (/= (match-end (car left)) (match-end (cadr left))))
658 (ding) ;We don't know how to do that.
659 (smerge-keep-n (car left))
660 (smerge-auto-leave))))))
662 (defun smerge-diff-base-mine ()
663 "Diff 'base' and 'mine' version in current conflict region."
664 (interactive)
665 (smerge-diff 2 1))
667 (defun smerge-diff-base-other ()
668 "Diff 'base' and 'other' version in current conflict region."
669 (interactive)
670 (smerge-diff 2 3))
672 (defun smerge-diff-mine-other ()
673 "Diff 'mine' and 'other' version in current conflict region."
674 (interactive)
675 (smerge-diff 1 3))
677 (defun smerge-match-conflict ()
678 "Get info about the conflict. Puts the info in the `match-data'.
679 The submatches contain:
680 0: the whole conflict.
681 1: your code.
682 2: the base code.
683 3: other code.
684 An error is raised if not inside a conflict."
685 (save-excursion
686 (condition-case nil
687 (let* ((orig-point (point))
689 (_ (forward-line 1))
690 (_ (re-search-backward smerge-begin-re))
692 (start (match-beginning 0))
693 (mine-start (match-end 0))
694 (filename (or (match-string 1) ""))
696 (_ (re-search-forward smerge-end-re))
697 (_ (assert (< orig-point (match-end 0))))
699 (other-end (match-beginning 0))
700 (end (match-end 0))
702 (_ (re-search-backward smerge-other-re start))
704 (mine-end (match-beginning 0))
705 (other-start (match-end 0))
707 base-start base-end)
709 ;; handle the various conflict styles
710 (cond
711 ((save-excursion
712 (goto-char mine-start)
713 (re-search-forward smerge-begin-re end t))
714 ;; There's a nested conflict and we're after the the beginning
715 ;; of the outer one but before the beginning of the inner one.
716 ;; Of course, maybe this is not a nested conflict but in that
717 ;; case it can only be something nastier that we don't know how
718 ;; to handle, so may as well arbitrarily decide to treat it as
719 ;; a nested conflict. --Stef
720 (error "There is a nested conflict"))
722 ((re-search-backward smerge-base-re start t)
723 ;; a 3-parts conflict
724 (set (make-local-variable 'smerge-conflict-style) 'diff3-A)
725 (setq base-end mine-end)
726 (setq mine-end (match-beginning 0))
727 (setq base-start (match-end 0)))
729 ((string= filename (file-name-nondirectory
730 (or buffer-file-name "")))
731 ;; a 2-parts conflict
732 (set (make-local-variable 'smerge-conflict-style) 'diff3-E))
734 ((and (not base-start)
735 (or (eq smerge-conflict-style 'diff3-A)
736 (equal filename "ANCESTOR")
737 (string-match "\\`[.0-9]+\\'" filename)))
738 ;; a same-diff conflict
739 (setq base-start mine-start)
740 (setq base-end mine-end)
741 (setq mine-start other-start)
742 (setq mine-end other-end)))
744 (store-match-data (list start end
745 mine-start mine-end
746 base-start base-end
747 other-start other-end
748 (when base-start (1- base-start)) base-start
749 (1- other-start) other-start))
751 (search-failed (error "Point not in conflict region")))))
753 (add-to-list 'debug-ignored-errors "Point not in conflict region")
755 (defun smerge-conflict-overlay (pos)
756 "Return the conflict overlay at POS if any."
757 (let ((ols (overlays-at pos))
758 conflict)
759 (dolist (ol ols)
760 (if (and (eq (overlay-get ol 'smerge) 'conflict)
761 (> (overlay-end ol) pos))
762 (setq conflict ol)))
763 conflict))
765 (defun smerge-find-conflict (&optional limit)
766 "Find and match a conflict region. Intended as a font-lock MATCHER.
767 The submatches are the same as in `smerge-match-conflict'.
768 Returns non-nil if a match is found between point and LIMIT.
769 Point is moved to the end of the conflict."
770 (let ((found nil)
771 (pos (point))
772 conflict)
773 ;; First check to see if point is already inside a conflict, using
774 ;; the conflict overlays.
775 (while (and (not found) (setq conflict (smerge-conflict-overlay pos)))
776 ;; Check the overlay's validity and kill it if it's out of date.
777 (condition-case nil
778 (progn
779 (goto-char (overlay-start conflict))
780 (smerge-match-conflict)
781 (goto-char (match-end 0))
782 (if (<= (point) pos)
783 (error "Matching backward!")
784 (setq found t)))
785 (error (smerge-remove-props
786 (overlay-start conflict) (overlay-end conflict))
787 (goto-char pos))))
788 ;; If we're not already inside a conflict, look for the next conflict
789 ;; and add/update its overlay.
790 (while (and (not found) (re-search-forward smerge-begin-re limit t))
791 (condition-case nil
792 (progn
793 (smerge-match-conflict)
794 (goto-char (match-end 0))
795 (let ((conflict (smerge-conflict-overlay (1- (point)))))
796 (if conflict
797 ;; Update its location, just in case it got messed up.
798 (move-overlay conflict (match-beginning 0) (match-end 0))
799 (setq conflict (make-overlay (match-beginning 0) (match-end 0)
800 nil 'front-advance nil))
801 (overlay-put conflict 'evaporate t)
802 (overlay-put conflict 'smerge 'conflict)
803 (let ((props smerge-text-properties))
804 (while props
805 (overlay-put conflict (pop props) (pop props))))))
806 (setq found t))
807 (error nil)))
808 found))
810 ;;; Refined change highlighting
812 (defvar smerge-refine-forward-function 'smerge-refine-forward
813 "Function used to determine an \"atomic\" element.
814 You can set it to `forward-char' to get char-level granularity.
815 Its behavior has mainly two restrictions:
816 - if this function encounters a newline, it's important that it stops right
817 after the newline.
818 This only matters if `smerge-refine-ignore-whitespace' is nil.
819 - it needs to be unaffected by changes performed by the `preproc' argument
820 to `smerge-refine-subst'.
821 This only matters if `smerge-refine-weight-hack' is nil.")
823 (defvar smerge-refine-ignore-whitespace t
824 "If non-nil,Indicate that smerge-refine should try to ignore change in whitespace.")
826 (defvar smerge-refine-weight-hack t
827 "If non-nil, pass to diff as many lines as there are chars in the region.
828 I.e. each atomic element (e.g. word) will be copied as many times (on different
829 lines) as it has chars. This has 2 advantages:
830 - if `diff' tries to minimize the number *lines* (rather than chars)
831 added/removed, this adjust the weights so that adding/removing long
832 symbols is considered correspondingly more costly.
833 - `smerge-refine-forward-function' only needs to be called when chopping up
834 the regions, and `forward-char' can be used afterwards.
835 It has the following disadvantages:
836 - cannot use `diff -w' because the weighting causes added spaces in a line
837 to be represented as added copies of some line, so `diff -w' can't do the
838 right thing any more.
839 - may in degenerate cases take a 1KB input region and turn it into a 1MB
840 file to pass to diff.")
842 (defun smerge-refine-forward (n)
843 (let ((case-fold-search nil)
844 (re "[[:upper:]]?[[:lower:]]+\\|[[:upper:]]+\\|[[:digit:]]+\\|.\\|\n"))
845 (when (and smerge-refine-ignore-whitespace
846 ;; smerge-refine-weight-hack causes additional spaces to
847 ;; appear as additional lines as well, so even if diff ignore
848 ;; whitespace changes, it'll report added/removed lines :-(
849 (not smerge-refine-weight-hack))
850 (setq re (concat "[ \t]*\\(?:" re "\\)")))
851 (dotimes (i n)
852 (unless (looking-at re) (error "Smerge refine internal error"))
853 (goto-char (match-end 0)))))
855 (defun smerge-refine-chopup-region (beg end file &optional preproc)
856 "Chopup the region into small elements, one per line.
857 Save the result into FILE.
858 If non-nil, PREPROC is called with no argument in a buffer that contains
859 a copy of the text, just before chopping it up. It can be used to replace
860 chars to try and eliminate some spurious differences."
861 ;; We used to chop up char-by-char rather than word-by-word like ediff
862 ;; does. It had the benefit of simplicity and very fine results, but it
863 ;; often suffered from problem that diff would find correlations where
864 ;; there aren't any, so the resulting "change" didn't make much sense.
865 ;; You can still get this behavior by setting
866 ;; `smerge-refine-forward-function' to `forward-char'.
867 (let ((buf (current-buffer)))
868 (with-temp-buffer
869 (insert-buffer-substring buf beg end)
870 (when preproc (goto-char (point-min)) (funcall preproc))
871 (when smerge-refine-ignore-whitespace
872 ;; It doesn't make much of a difference for diff-fine-highlight
873 ;; because we still have the _/+/</>/! prefix anyway. Can still be
874 ;; useful in other circumstances.
875 (subst-char-in-region (point-min) (point-max) ?\n ?\s))
876 (goto-char (point-min))
877 (while (not (eobp))
878 (funcall smerge-refine-forward-function 1)
879 (let ((s (if (prog2 (forward-char -1) (bolp) (forward-char 1))
881 (buffer-substring (line-beginning-position) (point)))))
882 ;; We add \n after each char except after \n, so we get
883 ;; one line per text char, where each line contains
884 ;; just one char, except for \n chars which are
885 ;; represented by the empty line.
886 (unless (eq (char-before) ?\n) (insert ?\n))
887 ;; HACK ALERT!!
888 (if smerge-refine-weight-hack
889 (dotimes (i (1- (length s))) (insert s "\n")))))
890 (unless (bolp) (error "Smerge refine internal error"))
891 (let ((coding-system-for-write 'emacs-mule))
892 (write-region (point-min) (point-max) file nil 'nomessage)))))
894 (defun smerge-refine-highlight-change (buf beg match-num1 match-num2 props)
895 (with-current-buffer buf
896 (goto-char beg)
897 (let* ((startline (- (string-to-number match-num1) 1))
898 (beg (progn (funcall (if smerge-refine-weight-hack
899 'forward-char
900 smerge-refine-forward-function)
901 startline)
902 (point)))
903 (end (progn (funcall (if smerge-refine-weight-hack
904 'forward-char
905 smerge-refine-forward-function)
906 (if match-num2
907 (- (string-to-number match-num2)
908 startline)
910 (point))))
911 (when smerge-refine-ignore-whitespace
912 (skip-chars-backward " \t\n" beg) (setq end (point))
913 (goto-char beg)
914 (skip-chars-forward " \t\n" end) (setq beg (point)))
915 (when (> end beg)
916 (let ((ol (make-overlay
917 beg end nil
918 ;; Make them tend to shrink rather than spread when editing.
919 'front-advance nil)))
920 (overlay-put ol 'evaporate t)
921 (dolist (x props) (overlay-put ol (car x) (cdr x)))
922 ol)))))
924 (defun smerge-refine-subst (beg1 end1 beg2 end2 props &optional preproc)
925 "Show fine differences in the two regions BEG1..END1 and BEG2..END2.
926 PROPS is an alist of properties to put (via overlays) on the changes.
927 If non-nil, PREPROC is called with no argument in a buffer that contains
928 a copy of a region, just before preparing it to for `diff'. It can be used to
929 replace chars to try and eliminate some spurious differences."
930 (let* ((buf (current-buffer))
931 (pos (point))
932 (file1 (make-temp-file "diff1"))
933 (file2 (make-temp-file "diff2")))
934 ;; Chop up regions into smaller elements and save into files.
935 (smerge-refine-chopup-region beg1 end1 file1 preproc)
936 (smerge-refine-chopup-region beg2 end2 file2 preproc)
938 ;; Call diff on those files.
939 (unwind-protect
940 (with-temp-buffer
941 (let ((coding-system-for-read 'emacs-mule))
942 (call-process diff-command nil t nil
943 (if (and smerge-refine-ignore-whitespace
944 (not smerge-refine-weight-hack))
945 ;; Pass -a so diff treats it as a text file even
946 ;; if it contains \0 and such.
947 ;; Pass -d so as to get the smallest change, but
948 ;; also and more importantly because otherwise it
949 ;; may happen that diff doesn't behave like
950 ;; smerge-refine-weight-hack expects it to.
951 ;; See http://thread.gmane.org/gmane.emacs.devel/82685.
952 "-awd" "-ad")
953 file1 file2))
954 ;; Process diff's output.
955 (goto-char (point-min))
956 (let ((last1 nil)
957 (last2 nil))
958 (while (not (eobp))
959 (if (not (looking-at "\\([0-9]+\\)\\(?:,\\([0-9]+\\)\\)?\\([acd]\\)\\([0-9]+\\)\\(?:,\\([0-9]+\\)\\)?$"))
960 (error "Unexpected patch hunk header: %s"
961 (buffer-substring (point) (line-end-position))))
962 (let ((op (char-after (match-beginning 3)))
963 (m1 (match-string 1))
964 (m2 (match-string 2))
965 (m4 (match-string 4))
966 (m5 (match-string 5)))
967 (when (memq op '(?d ?c))
968 (setq last1
969 (smerge-refine-highlight-change buf beg1 m1 m2 props)))
970 (when (memq op '(?a ?c))
971 (setq last2
972 (smerge-refine-highlight-change buf beg2 m4 m5 props))))
973 (forward-line 1) ;Skip hunk header.
974 (and (re-search-forward "^[0-9]" nil 'move) ;Skip hunk body.
975 (goto-char (match-beginning 0))))
976 ;; (assert (or (null last1) (< (overlay-start last1) end1)))
977 ;; (assert (or (null last2) (< (overlay-start last2) end2)))
978 (if smerge-refine-weight-hack
979 (progn
980 ;; (assert (or (null last1) (<= (overlay-end last1) end1)))
981 ;; (assert (or (null last2) (<= (overlay-end last2) end2)))
983 ;; smerge-refine-forward-function when calling in chopup may
984 ;; have stopped because it bumped into EOB whereas in
985 ;; smerge-refine-weight-hack it may go a bit further.
986 (if (and last1 (> (overlay-end last1) end1))
987 (move-overlay last1 (overlay-start last1) end1))
988 (if (and last2 (> (overlay-end last2) end2))
989 (move-overlay last2 (overlay-start last2) end2))
991 (goto-char pos)
992 (delete-file file1)
993 (delete-file file2))))
995 (defun smerge-refine (&optional part)
996 "Highlight the words of the conflict that are different.
997 For 3-way conflicts, highlights only 2 of the 3 parts.
998 A numeric argument PART can be used to specify which 2 parts;
999 repeating the command will highlight other 2 parts."
1000 (interactive
1001 (if (integerp current-prefix-arg) (list current-prefix-arg)
1002 (smerge-match-conflict)
1003 (let* ((prop (get-text-property (match-beginning 0) 'smerge-refine-part))
1004 (part (if (and (consp prop)
1005 (eq (buffer-chars-modified-tick) (car prop)))
1006 (cdr prop))))
1007 ;; If already highlighted, cycle.
1008 (list (if (integerp part) (1+ (mod part 3)))))))
1010 (if (and (integerp part) (or (< part 1) (> part 3)))
1011 (error "No conflict part nb %s" part))
1012 (smerge-match-conflict)
1013 (remove-overlays (match-beginning 0) (match-end 0) 'smerge 'refine)
1014 ;; Ignore `part' if not applicable, and default it if not provided.
1015 (setq part (cond ((null (match-end 2)) 2)
1016 ((eq (match-end 1) (match-end 3)) 1)
1017 ((integerp part) part)
1018 (t 2)))
1019 (let ((n1 (if (eq part 1) 2 1))
1020 (n2 (if (eq part 3) 2 3)))
1021 (smerge-ensure-match n1)
1022 (smerge-ensure-match n2)
1023 (put-text-property (match-beginning 0) (1+ (match-beginning 0))
1024 'smerge-refine-part
1025 (cons (buffer-chars-modified-tick) part))
1026 (smerge-refine-subst (match-beginning n1) (match-end n1)
1027 (match-beginning n2) (match-end n2)
1028 '((smerge . refine)
1029 (face . smerge-refined-change)))))
1031 (defun smerge-diff (n1 n2)
1032 (smerge-match-conflict)
1033 (smerge-ensure-match n1)
1034 (smerge-ensure-match n2)
1035 (let ((name1 (aref smerge-match-names n1))
1036 (name2 (aref smerge-match-names n2))
1037 ;; Read them before the match-data gets clobbered.
1038 (beg1 (match-beginning n1))
1039 (end1 (match-end n1))
1040 (beg2 (match-beginning n2))
1041 (end2 (match-end n2))
1042 (file1 (make-temp-file "smerge1"))
1043 (file2 (make-temp-file "smerge2"))
1044 (dir default-directory)
1045 (file (if buffer-file-name (file-relative-name buffer-file-name)))
1046 ;; We would want to use `emacs-mule-unix' for read&write, but we
1047 ;; bump into problems with the coding-system used by diff to write
1048 ;; the file names and the time stamps in the header.
1049 ;; `buffer-file-coding-system' is not always correct either, but if
1050 ;; the OS/user uses only one coding-system, then it works.
1051 (coding-system-for-read buffer-file-coding-system))
1052 (write-region beg1 end1 file1 nil 'nomessage)
1053 (write-region beg2 end2 file2 nil 'nomessage)
1054 (unwind-protect
1055 (with-current-buffer (get-buffer-create smerge-diff-buffer-name)
1056 (setq default-directory dir)
1057 (let ((inhibit-read-only t))
1058 (erase-buffer)
1059 (let ((status
1060 (apply 'call-process diff-command nil t nil
1061 (append smerge-diff-switches
1062 (list "-L" (concat name1 "/" file)
1063 "-L" (concat name2 "/" file)
1064 file1 file2)))))
1065 (if (eq status 0) (insert "No differences found.\n"))))
1066 (goto-char (point-min))
1067 (diff-mode)
1068 (display-buffer (current-buffer) t))
1069 (delete-file file1)
1070 (delete-file file2))))
1072 ;; compiler pacifiers
1073 (defvar smerge-ediff-windows)
1074 (defvar smerge-ediff-buf)
1075 (defvar ediff-buffer-A)
1076 (defvar ediff-buffer-B)
1077 (defvar ediff-buffer-C)
1078 (defvar ediff-ancestor-buffer)
1079 (defvar ediff-quit-hook)
1080 (declare-function ediff-cleanup-mess "ediff-util" nil)
1082 ;;;###autoload
1083 (defun smerge-ediff (&optional name-mine name-other name-base)
1084 "Invoke ediff to resolve the conflicts.
1085 NAME-MINE, NAME-OTHER, and NAME-BASE, if non-nil, are used for the
1086 buffer names."
1087 (interactive)
1088 (let* ((buf (current-buffer))
1089 (mode major-mode)
1090 ;;(ediff-default-variant 'default-B)
1091 (config (current-window-configuration))
1092 (filename (file-name-nondirectory buffer-file-name))
1093 (mine (generate-new-buffer
1094 (or name-mine (concat "*" filename " MINE*"))))
1095 (other (generate-new-buffer
1096 (or name-other (concat "*" filename " OTHER*"))))
1097 base)
1098 (with-current-buffer mine
1099 (buffer-disable-undo)
1100 (insert-buffer-substring buf)
1101 (goto-char (point-min))
1102 (while (smerge-find-conflict)
1103 (when (match-beginning 2) (setq base t))
1104 (smerge-keep-n 1))
1105 (buffer-enable-undo)
1106 (set-buffer-modified-p nil)
1107 (funcall mode))
1109 (with-current-buffer other
1110 (buffer-disable-undo)
1111 (insert-buffer-substring buf)
1112 (goto-char (point-min))
1113 (while (smerge-find-conflict)
1114 (smerge-keep-n 3))
1115 (buffer-enable-undo)
1116 (set-buffer-modified-p nil)
1117 (funcall mode))
1119 (when base
1120 (setq base (generate-new-buffer
1121 (or name-base (concat "*" filename " BASE*"))))
1122 (with-current-buffer base
1123 (buffer-disable-undo)
1124 (insert-buffer-substring buf)
1125 (goto-char (point-min))
1126 (while (smerge-find-conflict)
1127 (if (match-end 2)
1128 (smerge-keep-n 2)
1129 (delete-region (match-beginning 0) (match-end 0))))
1130 (buffer-enable-undo)
1131 (set-buffer-modified-p nil)
1132 (funcall mode)))
1134 ;; the rest of the code is inspired from vc.el
1135 ;; Fire up ediff.
1136 (set-buffer
1137 (if base
1138 (ediff-merge-buffers-with-ancestor mine other base)
1139 ;; nil 'ediff-merge-revisions-with-ancestor buffer-file-name)
1140 (ediff-merge-buffers mine other)))
1141 ;; nil 'ediff-merge-revisions buffer-file-name)))
1143 ;; Ediff is now set up, and we are in the control buffer.
1144 ;; Do a few further adjustments and take precautions for exit.
1145 (set (make-local-variable 'smerge-ediff-windows) config)
1146 (set (make-local-variable 'smerge-ediff-buf) buf)
1147 (set (make-local-variable 'ediff-quit-hook)
1148 (lambda ()
1149 (let ((buffer-A ediff-buffer-A)
1150 (buffer-B ediff-buffer-B)
1151 (buffer-C ediff-buffer-C)
1152 (buffer-Ancestor ediff-ancestor-buffer)
1153 (buf smerge-ediff-buf)
1154 (windows smerge-ediff-windows))
1155 (ediff-cleanup-mess)
1156 (with-current-buffer buf
1157 (erase-buffer)
1158 (insert-buffer-substring buffer-C)
1159 (kill-buffer buffer-A)
1160 (kill-buffer buffer-B)
1161 (kill-buffer buffer-C)
1162 (when (bufferp buffer-Ancestor) (kill-buffer buffer-Ancestor))
1163 (set-window-configuration windows)
1164 (message "Conflict resolution finished; you may save the buffer")))))
1165 (message "Please resolve conflicts now; exit ediff when done")))
1167 (defun smerge-makeup-conflict (pt1 pt2 pt3 &optional pt4)
1168 "Insert diff3 markers to make a new conflict.
1169 Uses point and mark for 2 of the relevant positions and previous marks
1170 for the other ones.
1171 By default, makes up a 2-way conflict,
1172 with a \\[universal-argument] prefix, makes up a 3-way conflict."
1173 (interactive
1174 (list (point)
1175 (mark)
1176 (progn (pop-mark) (mark))
1177 (when current-prefix-arg (pop-mark) (mark))))
1178 ;; Start from the end so as to avoid problems with pos-changes.
1179 (destructuring-bind (pt1 pt2 pt3 &optional pt4)
1180 (sort (list* pt1 pt2 pt3 (if pt4 (list pt4))) '>=)
1181 (goto-char pt1) (beginning-of-line)
1182 (insert ">>>>>>> OTHER\n")
1183 (goto-char pt2) (beginning-of-line)
1184 (insert "=======\n")
1185 (goto-char pt3) (beginning-of-line)
1186 (when pt4
1187 (insert "||||||| BASE\n")
1188 (goto-char pt4) (beginning-of-line))
1189 (insert "<<<<<<< MINE\n"))
1190 (if smerge-mode nil (smerge-mode 1))
1191 (smerge-refine))
1194 (defconst smerge-parsep-re
1195 (concat smerge-begin-re "\\|" smerge-end-re "\\|"
1196 smerge-base-re "\\|" smerge-other-re "\\|"))
1198 ;;;###autoload
1199 (define-minor-mode smerge-mode
1200 "Minor mode to simplify editing output from the diff3 program.
1201 \\{smerge-mode-map}"
1202 :group 'smerge :lighter " SMerge"
1203 (when (and (boundp 'font-lock-mode) font-lock-mode)
1204 (save-excursion
1205 (if smerge-mode
1206 (font-lock-add-keywords nil smerge-font-lock-keywords 'append)
1207 (font-lock-remove-keywords nil smerge-font-lock-keywords))
1208 (goto-char (point-min))
1209 (while (smerge-find-conflict)
1210 (save-excursion
1211 (font-lock-fontify-region (match-beginning 0) (match-end 0) nil)))))
1212 (if (string-match (regexp-quote smerge-parsep-re) paragraph-separate)
1213 (unless smerge-mode
1214 (set (make-local-variable 'paragraph-separate)
1215 (replace-match "" t t paragraph-separate)))
1216 (when smerge-mode
1217 (set (make-local-variable 'paragraph-separate)
1218 (concat smerge-parsep-re paragraph-separate))))
1219 (unless smerge-mode
1220 (smerge-remove-props (point-min) (point-max))))
1222 ;;;###autoload
1223 (defun smerge-start-session ()
1224 "Turn on `smerge-mode' and move point to first conflict marker.
1225 If no conflict maker is found, turn off `smerge-mode'."
1226 (smerge-mode 1)
1227 (condition-case nil
1228 (unless (looking-at smerge-begin-re)
1229 (smerge-next))
1230 (error (smerge-auto-leave))))
1232 (provide 'smerge-mode)
1234 ;; arch-tag: 605c8d1e-e43d-4943-a6f3-1bcc4333e690
1235 ;;; smerge-mode.el ends here