Function: smerge-apply-resolution-patch

smerge-apply-resolution-patch is a byte-compiled function defined in smerge-mode.el.gz.

Signature

(smerge-apply-resolution-patch BUF M0B M0E M3B M3E &optional M2B)

Documentation

Replace the conflict with a bunch of subconflicts.

BUF contains a plain diff between match-1 and match-3.

Source Code

;; Defined in /usr/src/emacs/lisp/vc/smerge-mode.el.gz
(defun smerge-apply-resolution-patch (buf m0b m0e m3b m3e &optional m2b)
  "Replace the conflict with a bunch of subconflicts.
BUF contains a plain diff between match-1 and match-3."
  (let ((line 1)
        (textbuf (current-buffer))
        (name1 (progn (goto-char m0b)
                      (buffer-substring (+ (point) 8) (line-end-position))))
        (name2 (when m2b (goto-char m2b) (forward-line -1)
                     (buffer-substring (+ (point) 8) (line-end-position))))
        (name3 (progn (goto-char m0e) (forward-line -1)
                      (buffer-substring (+ (point) 8) (line-end-position)))))
    (smerge-remove-props m0b m0e)
    (delete-region m3e m0e)
    (delete-region m0b m3b)
    (setq m3b m0b)
    (setq m3e (- m3e (- m3b m0b)))
    (goto-char m3b)
    (with-current-buffer buf
      (goto-char (point-min))
      (while (not (eobp))
        (if (not (looking-at "\\([0-9]+\\)\\(?:,\\([0-9]+\\)\\)?\\([acd]\\)\\([0-9]+\\)\\(?:,\\([0-9]+\\)\\)?$"))
            (error "Unexpected patch hunk header: %s"
                   (buffer-substring (point) (line-end-position)))
          (let* ((op (char-after (match-beginning 3)))
                 (startline (+ (string-to-number (match-string 1))
                               ;; No clue why this is the way it is, but line
                               ;; numbers seem to be off-by-one for `a' ops.
                               (if (eq op ?a) 1 0)))
                 (endline (if (eq op ?a) startline
                            (1+ (if (match-end 2)
                                    (string-to-number (match-string 2))
                                  startline))))
                 (lines (- endline startline))
                 (otherlines (cond
                              ((eq op ?d) nil)
                              ((null (match-end 5)) 1)
                              (t (- (string-to-number (match-string 5))
                                    (string-to-number (match-string 4)) -1))))
                 othertext)
            (forward-line 1)                             ;Skip header.
            (forward-line lines)                         ;Skip deleted text.
            (if (eq op ?c) (forward-line 1))             ;Skip separator.
            (setq othertext
                  (if (null otherlines) ""
                    (let ((pos (point)))
                      (dotimes (_i otherlines) (delete-char 2) (forward-line 1))
                      (buffer-substring pos (point)))))
            (with-current-buffer textbuf
              (forward-line (- startline line))
              (insert "<<<<<<< " name1 "\n" othertext
                      (if name2 (concat "||||||| " name2 "\n") "")
                      "=======\n")
              (forward-line lines)
              (insert ">>>>>>> " name3 "\n")
              (setq line endline))))))))