smerge-mode.el 50 KB

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