ediff-diff.el 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534
  1. ;;; ediff-diff.el --- diff-related utilities
  2. ;; Copyright (C) 1994-2012 Free Software Foundation, Inc.
  3. ;; Author: Michael Kifer <kifer@cs.stonybrook.edu>
  4. ;; Package: ediff
  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. ;;; Code:
  18. (provide 'ediff-diff)
  19. (eval-when-compile
  20. (require 'ediff-util))
  21. (require 'ediff-init)
  22. (defgroup ediff-diff nil
  23. "Diff related utilities."
  24. :prefix "ediff-"
  25. :group 'ediff)
  26. (defcustom ediff-diff-program "diff"
  27. "Program to use for generating the differential of the two files."
  28. :type 'string
  29. :group 'ediff-diff)
  30. (defcustom ediff-diff3-program "diff3"
  31. "Program to be used for three-way comparison.
  32. Must produce output compatible with Unix's diff3 program."
  33. :type 'string
  34. :group 'ediff-diff)
  35. ;; The following functions must precede all defcustom-defined variables.
  36. (fset 'ediff-set-actual-diff-options (lambda () nil))
  37. (defcustom ediff-shell
  38. (cond ((memq system-type '(ms-dos windows-nt))
  39. shell-file-name) ; no standard name on MS-DOS
  40. (t "sh")) ; UNIX
  41. "The shell used to run diff and patch.
  42. If user's .profile or .cshrc files are set up correctly, any shell
  43. will do. However, some people set $prompt or other things
  44. incorrectly, which leads to undesirable output messages. These may
  45. cause Ediff to fail. In such a case, set `ediff-shell' to a shell that
  46. you are not using or, better, fix your shell's startup file."
  47. :type 'string
  48. :group 'ediff-diff)
  49. (defcustom ediff-cmp-program "cmp"
  50. "Utility to use to determine if two files are identical.
  51. It must return code 0, if its arguments are identical files."
  52. :type 'string
  53. :group 'ediff-diff)
  54. (defcustom ediff-cmp-options nil
  55. "Options to pass to `ediff-cmp-program'.
  56. If GNU diff is used as `ediff-cmp-program', then the most useful options
  57. are `-I REGEXP', to ignore changes whose lines match the REGEXP."
  58. :type '(repeat string)
  59. :group 'ediff-diff)
  60. (defun ediff-set-diff-options (symbol value)
  61. (set symbol value)
  62. (ediff-set-actual-diff-options))
  63. (defcustom ediff-diff-options
  64. (if (memq system-type '(ms-dos windows-nt)) "--binary" "")
  65. "Options to pass to `ediff-diff-program'.
  66. If Unix diff is used as `ediff-diff-program',
  67. then a useful option is `-w', to ignore space.
  68. Options `-c', `-u', and `-i' are not allowed. Case sensitivity can be
  69. toggled interactively using \\[ediff-toggle-ignore-case].
  70. Do not remove the default options. If you need to change this variable, add new
  71. options after the default ones.
  72. This variable is not for customizing the look of the differences produced by
  73. the command \\[ediff-show-diff-output]. Use the variable
  74. `ediff-custom-diff-options' for that."
  75. :set 'ediff-set-diff-options
  76. :type 'string
  77. :group 'ediff-diff)
  78. (ediff-defvar-local ediff-ignore-case nil
  79. "*If t, skip over difference regions that differ only in letter case.
  80. This variable can be set either in .emacs or toggled interactively.
  81. Use `setq-default' if setting it in .emacs")
  82. (defcustom ediff-ignore-case-option "-i"
  83. "Option that causes the diff program to ignore case of letters."
  84. :type 'string
  85. :group 'ediff-diff)
  86. (defcustom ediff-ignore-case-option3 ""
  87. "Option that causes the diff3 program to ignore case of letters.
  88. GNU diff3 doesn't have such an option."
  89. :type 'string
  90. :group 'ediff-diff)
  91. ;; the actual options used in comparison
  92. (ediff-defvar-local ediff-actual-diff-options ediff-diff-options "")
  93. (defcustom ediff-custom-diff-program ediff-diff-program
  94. "Program to use for generating custom diff output for saving it in a file.
  95. This output is not used by Ediff internally."
  96. :type 'string
  97. :group 'ediff-diff)
  98. (defcustom ediff-custom-diff-options "-c"
  99. "Options to pass to `ediff-custom-diff-program'."
  100. :type 'string
  101. :group 'ediff-diff)
  102. ;;; Support for diff3
  103. (defvar ediff-match-diff3-line "^====\\(.?\\)\C-m?$"
  104. "Pattern to match lines produced by diff3 that describe differences.")
  105. (defcustom ediff-diff3-options ""
  106. "Options to pass to `ediff-diff3-program'."
  107. :set 'ediff-set-diff-options
  108. :type 'string
  109. :group 'ediff-diff)
  110. ;; the actual options used in comparison
  111. (ediff-defvar-local ediff-actual-diff3-options ediff-diff3-options "")
  112. (defcustom ediff-diff3-ok-lines-regexp
  113. "^\\([1-3]:\\|====\\| \\|.*Warning *:\\|.*No newline\\|.*missing newline\\|^\C-m$\\)"
  114. "Regexp that matches normal output lines from `ediff-diff3-program'.
  115. Lines that do not match are assumed to be error messages."
  116. :type 'regexp
  117. :group 'ediff-diff)
  118. ;; keeps the status of the current diff in 3-way jobs.
  119. ;; the status can be =diff(A), =diff(B), or =diff(A+B)
  120. (ediff-defvar-local ediff-diff-status "" "")
  121. ;;; Fine differences
  122. (ediff-defvar-local ediff-auto-refine (if (ediff-has-face-support-p) 'on 'nix)
  123. "If `on', Ediff auto-highlights fine diffs for the current diff region.
  124. If `off', auto-highlighting is not used. If `nix', no fine diffs are shown
  125. at all, unless the user force-refines the region by hitting `*'.
  126. This variable can be set either in .emacs or toggled interactively.
  127. Use `setq-default' if setting it in .emacs")
  128. (ediff-defvar-local ediff-ignore-similar-regions nil
  129. "*If t, skip over difference regions that differ only in the white space and line breaks.
  130. This variable can be set either in .emacs or toggled interactively.
  131. Use `setq-default' if setting it in .emacs")
  132. (ediff-defvar-local ediff-auto-refine-limit 14000
  133. "*Auto-refine only the regions of this size \(in bytes\) or less.")
  134. ;;; General
  135. (defvar ediff-diff-ok-lines-regexp
  136. (concat
  137. "^\\("
  138. "[0-9,]+[acd][0-9,]+\C-m?$"
  139. "\\|[<>] "
  140. "\\|---"
  141. "\\|.*Warning *:"
  142. "\\|.*No +newline"
  143. "\\|.*missing +newline"
  144. "\\|^\C-m?$"
  145. "\\)")
  146. "Regexp that matches normal output lines from `ediff-diff-program'.
  147. This is mostly lifted from Emerge, except that Ediff also considers
  148. warnings and `Missing newline'-type messages to be normal output.
  149. Lines that do not match are assumed to be error messages.")
  150. (defvar ediff-match-diff-line
  151. (let ((x "\\([0-9]+\\)\\(\\|,\\([0-9]+\\)\\)"))
  152. (concat "^" x "\\([acd]\\)" x "\C-m?$"))
  153. "Pattern to match lines produced by diff that describe differences.")
  154. (ediff-defvar-local ediff-setup-diff-regions-function nil
  155. "value is a function symbol depending on the kind of job is to be done.
  156. For 2-way jobs and for ediff-merge, it should be `ediff-setup-diff-regions'.
  157. For jobs requiring diff3, it should be `ediff-setup-diff-regions3'.
  158. The function should take three mandatory arguments, file-A, file-B, and
  159. file-C. It may ignore file C for diff2 jobs. It should also take
  160. one optional arguments, diff-number to refine.")
  161. ;;; Functions
  162. ;; Generate the difference vector and overlays for the two files
  163. ;; With optional arg REG-TO-REFINE, refine this region.
  164. ;; File-C argument is not used here. It is there just because
  165. ;; ediff-setup-diff-regions is called via a funcall to
  166. ;; ediff-setup-diff-regions-function, which can also have the value
  167. ;; ediff-setup-diff-regions3, which takes 4 arguments.
  168. (defun ediff-setup-diff-regions (file-A file-B file-C)
  169. ;; looking for '-c', '-i', '-u', or 'c', 'i', 'u' among clustered non-long options
  170. (if (string-match "^-[ciu]\\| -[ciu]\\|\\(^\\| \\)-[^- ]+[ciu]"
  171. ediff-diff-options)
  172. (error "Options `-c', `-u', and `-i' are not allowed in `ediff-diff-options'"))
  173. ;; create, if it doesn't exist
  174. (or (ediff-buffer-live-p ediff-diff-buffer)
  175. (setq ediff-diff-buffer
  176. (get-buffer-create (ediff-unique-buffer-name "*ediff-diff" "*"))))
  177. (ediff-make-diff2-buffer ediff-diff-buffer file-A file-B)
  178. (ediff-prepare-error-list ediff-diff-ok-lines-regexp ediff-diff-buffer)
  179. (ediff-convert-diffs-to-overlays
  180. (ediff-extract-diffs
  181. ediff-diff-buffer ediff-word-mode ediff-narrow-bounds)))
  182. ;; Run the diff program on FILE1 and FILE2 and put the output in DIFF-BUFFER
  183. ;; Return the size of DIFF-BUFFER
  184. ;; The return code isn't used in the program at present.
  185. (defun ediff-make-diff2-buffer (diff-buffer file1 file2)
  186. (let ((file1-size (ediff-file-size file1))
  187. (file2-size (ediff-file-size file2)))
  188. (cond ((not (numberp file1-size))
  189. (message "Can't find file: %s"
  190. (ediff-abbreviate-file-name file1))
  191. (sit-for 2)
  192. ;; 1 is an error exit code
  193. 1)
  194. ((not (numberp file2-size))
  195. (message "Can't find file: %s"
  196. (ediff-abbreviate-file-name file2))
  197. (sit-for 2)
  198. ;; 1 is an error exit code
  199. 1)
  200. (t (message "Computing differences between %s and %s ..."
  201. (file-name-nondirectory file1)
  202. (file-name-nondirectory file2))
  203. ;; this erases the diff buffer automatically
  204. (ediff-exec-process ediff-diff-program
  205. diff-buffer
  206. 'synchronize
  207. ediff-actual-diff-options file1 file2)
  208. (message "")
  209. (ediff-with-current-buffer diff-buffer
  210. (buffer-size))))))
  211. ;; If file-A/B/C is nil, do 2-way comparison with the non-nil buffers
  212. ;; This function works for diff3 and diff2 jobs
  213. (defun ediff-setup-fine-diff-regions (file-A file-B file-C reg-num)
  214. (or (ediff-buffer-live-p ediff-fine-diff-buffer)
  215. (setq ediff-fine-diff-buffer
  216. (get-buffer-create
  217. (ediff-unique-buffer-name "*ediff-fine-diff" "*"))))
  218. (let (diff3-job diff-program diff-options ok-regexp diff-list)
  219. (setq diff3-job ediff-3way-job
  220. diff-program (if diff3-job ediff-diff3-program ediff-diff-program)
  221. diff-options (if diff3-job
  222. ediff-actual-diff3-options
  223. ediff-actual-diff-options)
  224. ok-regexp (if diff3-job
  225. ediff-diff3-ok-lines-regexp
  226. ediff-diff-ok-lines-regexp))
  227. (ediff-message-if-verbose "Refining difference region %d ..." (1+ reg-num))
  228. (ediff-exec-process diff-program ediff-fine-diff-buffer 'synchronize
  229. diff-options
  230. ;; The shuffle below is because we can compare 3-way
  231. ;; or in several 2-way fashions, like fA fC, fA fB,
  232. ;; or fB fC.
  233. (if file-A file-A file-B)
  234. (if file-B file-B file-A)
  235. (if diff3-job
  236. (if file-C file-C file-B))
  237. ) ; exec process
  238. (ediff-prepare-error-list ok-regexp ediff-fine-diff-buffer)
  239. (ediff-message-if-verbose
  240. "")
  241. ;; "Refining difference region %d ... done" (1+ reg-num))
  242. (setq diff-list
  243. (if diff3-job
  244. (ediff-extract-diffs3
  245. ediff-fine-diff-buffer '3way-comparison 'word-mode)
  246. (ediff-extract-diffs ediff-fine-diff-buffer 'word-mode)))
  247. ;; fixup diff-list
  248. (if diff3-job
  249. (cond ((not file-A)
  250. (mapc (lambda (elt)
  251. (aset elt 0 nil)
  252. (aset elt 1 nil))
  253. (cdr diff-list)))
  254. ((not file-B)
  255. (mapc (lambda (elt)
  256. (aset elt 2 nil)
  257. (aset elt 3 nil))
  258. (cdr diff-list)))
  259. ((not file-C)
  260. (mapc (lambda (elt)
  261. (aset elt 4 nil)
  262. (aset elt 5 nil))
  263. (cdr diff-list)))
  264. ))
  265. (ediff-convert-fine-diffs-to-overlays diff-list reg-num)
  266. ))
  267. (defun ediff-prepare-error-list (ok-regexp diff-buff)
  268. (or (ediff-buffer-live-p ediff-error-buffer)
  269. (setq ediff-error-buffer
  270. (get-buffer-create (ediff-unique-buffer-name
  271. "*ediff-errors" "*"))))
  272. (ediff-with-current-buffer ediff-error-buffer
  273. (setq buffer-undo-list t)
  274. (erase-buffer)
  275. (insert (ediff-with-current-buffer diff-buff (buffer-string)))
  276. (goto-char (point-min))
  277. (delete-matching-lines ok-regexp))
  278. ;; If diff reports errors, show them then quit.
  279. (if (/= 0 (ediff-with-current-buffer ediff-error-buffer (buffer-size)))
  280. (let ((ctl-buf ediff-control-buffer)
  281. (error-buf ediff-error-buffer))
  282. (ediff-skip-unsuitable-frames)
  283. (switch-to-buffer error-buf)
  284. (ediff-kill-buffer-carefully ctl-buf)
  285. (error "Errors in diff output. Diff output is in %S" diff-buff))))
  286. ;; BOUNDS specifies visibility bounds to use.
  287. ;; WORD-MODE tells whether we are in the word-mode or not.
  288. ;; If WORD-MODE, also construct vector of diffs using word numbers.
  289. ;; Else, use point values.
  290. ;; This function handles diff-2 jobs including the case of
  291. ;; merging buffers and files without ancestor.
  292. (defun ediff-extract-diffs (diff-buffer word-mode &optional bounds)
  293. (let ((A-buffer ediff-buffer-A)
  294. (B-buffer ediff-buffer-B)
  295. (C-buffer ediff-buffer-C)
  296. (a-prev 1) ; this is needed to set the first diff line correctly
  297. (a-prev-pt nil)
  298. (b-prev 1)
  299. (b-prev-pt nil)
  300. (c-prev 1)
  301. (c-prev-pt nil)
  302. diff-list shift-A shift-B
  303. )
  304. ;; diff list contains word numbers, unless changed later
  305. (setq diff-list (cons (if word-mode 'words 'points)
  306. diff-list))
  307. ;; we don't use visibility bounds for buffer C when merging
  308. (if bounds
  309. (setq shift-A
  310. (ediff-overlay-start
  311. (ediff-get-value-according-to-buffer-type 'A bounds))
  312. shift-B
  313. (ediff-overlay-start
  314. (ediff-get-value-according-to-buffer-type 'B bounds))))
  315. ;; reset point in buffers A/B/C
  316. (ediff-with-current-buffer A-buffer
  317. (goto-char (if shift-A shift-A (point-min))))
  318. (ediff-with-current-buffer B-buffer
  319. (goto-char (if shift-B shift-B (point-min))))
  320. (if (ediff-buffer-live-p C-buffer)
  321. (ediff-with-current-buffer C-buffer
  322. (goto-char (point-min))))
  323. (ediff-with-current-buffer diff-buffer
  324. (goto-char (point-min))
  325. (while (re-search-forward ediff-match-diff-line nil t)
  326. (let* ((a-begin (string-to-number (buffer-substring (match-beginning 1)
  327. (match-end 1))))
  328. (a-end (let ((b (match-beginning 3))
  329. (e (match-end 3)))
  330. (if b
  331. (string-to-number (buffer-substring b e))
  332. a-begin)))
  333. (diff-type (buffer-substring (match-beginning 4) (match-end 4)))
  334. (b-begin (string-to-number (buffer-substring (match-beginning 5)
  335. (match-end 5))))
  336. (b-end (let ((b (match-beginning 7))
  337. (e (match-end 7)))
  338. (if b
  339. (string-to-number (buffer-substring b e))
  340. b-begin)))
  341. a-begin-pt a-end-pt b-begin-pt b-end-pt
  342. c-begin c-end c-begin-pt c-end-pt)
  343. ;; fix the beginning and end numbers, because diff is somewhat
  344. ;; strange about how it numbers lines
  345. (if (string-equal diff-type "a")
  346. (setq b-end (1+ b-end)
  347. a-begin (1+ a-begin)
  348. a-end a-begin)
  349. (if (string-equal diff-type "d")
  350. (setq a-end (1+ a-end)
  351. b-begin (1+ b-begin)
  352. b-end b-begin)
  353. ;; (string-equal diff-type "c")
  354. (setq a-end (1+ a-end)
  355. b-end (1+ b-end))))
  356. (if (eq ediff-default-variant 'default-B)
  357. (setq c-begin b-begin
  358. c-end b-end)
  359. (setq c-begin a-begin
  360. c-end a-end))
  361. ;; compute main diff vector
  362. (if word-mode
  363. ;; make diff-list contain word numbers
  364. (setq diff-list
  365. (nconc diff-list
  366. (list
  367. (if (ediff-buffer-live-p C-buffer)
  368. (vector (- a-begin a-prev) (- a-end a-begin)
  369. (- b-begin b-prev) (- b-end b-begin)
  370. (- c-begin c-prev) (- c-end c-begin)
  371. nil nil ; dummy ancestor
  372. nil ; state of diff
  373. nil ; state of merge
  374. nil ; state of ancestor
  375. )
  376. (vector (- a-begin a-prev) (- a-end a-begin)
  377. (- b-begin b-prev) (- b-end b-begin)
  378. nil nil ; dummy buf C
  379. nil nil ; dummy ancestor
  380. nil ; state of diff
  381. nil ; state of merge
  382. nil ; state of ancestor
  383. ))
  384. ))
  385. a-prev a-end
  386. b-prev b-end
  387. c-prev c-end)
  388. ;; else convert lines to points
  389. (ediff-with-current-buffer A-buffer
  390. (let ((longlines-mode-val
  391. (if (and (boundp 'longlines-mode) longlines-mode) 1 0)))
  392. ;; we must disable and then restore longlines-mode
  393. (if (eq longlines-mode-val 1)
  394. (longlines-mode 0))
  395. (goto-char (or a-prev-pt shift-A (point-min)))
  396. (forward-line (- a-begin a-prev))
  397. (setq a-begin-pt (point))
  398. (forward-line (- a-end a-begin))
  399. (setq a-end-pt (point)
  400. a-prev a-end
  401. a-prev-pt a-end-pt)
  402. (if (eq longlines-mode-val 1)
  403. (longlines-mode longlines-mode-val))
  404. ))
  405. (ediff-with-current-buffer B-buffer
  406. (let ((longlines-mode-val
  407. (if (and (boundp 'longlines-mode) longlines-mode) 1 0)))
  408. (if (eq longlines-mode-val 1)
  409. (longlines-mode 0))
  410. (goto-char (or b-prev-pt shift-B (point-min)))
  411. (forward-line (- b-begin b-prev))
  412. (setq b-begin-pt (point))
  413. (forward-line (- b-end b-begin))
  414. (setq b-end-pt (point)
  415. b-prev b-end
  416. b-prev-pt b-end-pt)
  417. (if (eq longlines-mode-val 1)
  418. (longlines-mode longlines-mode-val))
  419. ))
  420. (if (ediff-buffer-live-p C-buffer)
  421. (ediff-with-current-buffer C-buffer
  422. (let ((longlines-mode-val
  423. (if (and (boundp 'longlines-mode) longlines-mode) 1 0)))
  424. (if (eq longlines-mode-val 1)
  425. (longlines-mode 0))
  426. (goto-char (or c-prev-pt (point-min)))
  427. (forward-line (- c-begin c-prev))
  428. (setq c-begin-pt (point))
  429. (forward-line (- c-end c-begin))
  430. (setq c-end-pt (point)
  431. c-prev c-end
  432. c-prev-pt c-end-pt)
  433. (if (eq longlines-mode-val 1)
  434. (longlines-mode longlines-mode-val))
  435. )))
  436. (setq diff-list
  437. (nconc
  438. diff-list
  439. (list
  440. (if (ediff-buffer-live-p C-buffer)
  441. (vector
  442. a-begin-pt a-end-pt b-begin-pt b-end-pt
  443. c-begin-pt c-end-pt
  444. nil nil ; dummy ancestor
  445. ;; state of diff
  446. ;; shows which buff is different from the other two
  447. (if (eq ediff-default-variant 'default-B) 'A 'B)
  448. ediff-default-variant ; state of merge
  449. nil ; state of ancestor
  450. )
  451. (vector a-begin-pt a-end-pt
  452. b-begin-pt b-end-pt
  453. nil nil ; dummy buf C
  454. nil nil ; dummy ancestor
  455. nil nil ; dummy state of diff & merge
  456. nil ; dummy state of ancestor
  457. )))
  458. )))
  459. ))) ; end ediff-with-current-buffer
  460. diff-list
  461. ))
  462. (defun ediff-convert-diffs-to-overlays (diff-list)
  463. (ediff-set-diff-overlays-in-one-buffer 'A diff-list)
  464. (ediff-set-diff-overlays-in-one-buffer 'B diff-list)
  465. (if ediff-3way-job
  466. (ediff-set-diff-overlays-in-one-buffer 'C diff-list))
  467. (if ediff-merge-with-ancestor-job
  468. (ediff-set-diff-overlays-in-one-buffer 'Ancestor diff-list))
  469. ;; set up vector showing the status of merge regions
  470. (if ediff-merge-job
  471. (setq ediff-state-of-merge
  472. (vconcat
  473. (mapcar (lambda (elt)
  474. (let ((state-of-merge (aref elt 9))
  475. (state-of-ancestor (aref elt 10)))
  476. (vector
  477. ;; state of merge: prefers/default-A/B or combined
  478. (if state-of-merge (format "%S" state-of-merge))
  479. ;; whether the ancestor region is empty
  480. state-of-ancestor)))
  481. ;; the first elt designates type of list
  482. (cdr diff-list))
  483. )))
  484. (message "Processing difference regions ... done"))
  485. (defun ediff-set-diff-overlays-in-one-buffer (buf-type diff-list)
  486. (let* ((current-diff -1)
  487. (buff (ediff-get-buffer buf-type))
  488. (ctl-buf ediff-control-buffer)
  489. ;; ediff-extract-diffs puts the type of diff-list as the first elt
  490. ;; of this list. The type is either 'points or 'words
  491. (diff-list-type (car diff-list))
  492. (shift (ediff-overlay-start
  493. (ediff-get-value-according-to-buffer-type
  494. buf-type ediff-narrow-bounds)))
  495. (limit (ediff-overlay-end
  496. (ediff-get-value-according-to-buffer-type
  497. buf-type ediff-narrow-bounds)))
  498. diff-overlay-list list-element total-diffs
  499. begin end pt-saved overlay state-of-diff)
  500. (setq diff-list (cdr diff-list)) ; discard diff list type
  501. (setq total-diffs (length diff-list))
  502. ;; shift, if necessary
  503. (ediff-with-current-buffer buff (setq pt-saved shift))
  504. (while diff-list
  505. (setq current-diff (1+ current-diff)
  506. list-element (car diff-list)
  507. begin (aref list-element (cond ((eq buf-type 'A) 0)
  508. ((eq buf-type 'B) 2)
  509. ((eq buf-type 'C) 4)
  510. (t 6))) ; Ancestor
  511. end (aref list-element (cond ((eq buf-type 'A) 1)
  512. ((eq buf-type 'B) 3)
  513. ((eq buf-type 'C) 5)
  514. (t 7))) ; Ancestor
  515. state-of-diff (aref list-element 8)
  516. )
  517. (cond ((and (not (eq buf-type state-of-diff))
  518. (not (eq buf-type 'Ancestor))
  519. (memq state-of-diff '(A B C)))
  520. (setq state-of-diff
  521. (car (delq buf-type (delq state-of-diff (list 'A 'B 'C)))))
  522. (setq state-of-diff (format "=diff(%S)" state-of-diff))
  523. )
  524. (t (setq state-of-diff nil)))
  525. ;; Put overlays at appropriate places in buffer
  526. ;; convert word numbers to points, if necessary
  527. (if (eq diff-list-type 'words)
  528. (progn
  529. (ediff-with-current-buffer buff (goto-char pt-saved))
  530. (ediff-with-current-buffer ctl-buf
  531. (setq begin (ediff-goto-word (1+ begin) buff)
  532. end (ediff-goto-word end buff 'end)))
  533. (if (> end limit) (setq end limit))
  534. (if (> begin end) (setq begin end))
  535. (setq pt-saved (ediff-with-current-buffer buff (point)))))
  536. (setq overlay (ediff-make-bullet-proof-overlay begin end buff))
  537. (ediff-overlay-put overlay 'priority ediff-shadow-overlay-priority)
  538. (ediff-overlay-put overlay 'ediff-diff-num current-diff)
  539. (if (and (ediff-has-face-support-p)
  540. ediff-use-faces ediff-highlight-all-diffs)
  541. (ediff-set-overlay-face
  542. overlay (ediff-background-face buf-type current-diff)))
  543. (if (= 0 (mod current-diff 10))
  544. (message "Buffer %S: Processing difference region %d of %d"
  545. buf-type current-diff total-diffs))
  546. ;; Record all overlays for this difference.
  547. ;; The 2-d elt, nil, is a place holder for the fine diff vector.
  548. ;; The 3-d elt, nil, is a place holder for no-fine-diffs flag.
  549. ;; The 4-th elt says which diff region is different from the other two
  550. ;; (3-way jobs only).
  551. (setq diff-overlay-list
  552. (nconc
  553. diff-overlay-list
  554. (list (vector overlay nil nil state-of-diff)))
  555. diff-list
  556. (cdr diff-list))
  557. ) ; while
  558. (set (ediff-get-symbol-from-alist buf-type ediff-difference-vector-alist)
  559. (vconcat diff-overlay-list))
  560. ))
  561. ;; `n' is the diff region to work on. Default is ediff-current-difference.
  562. ;; if `flag' is 'noforce then make fine-diffs only if this region's fine
  563. ;; diffs have not been computed before.
  564. ;; if `flag' is 'skip then don't compute fine diffs for this region.
  565. (defun ediff-make-fine-diffs (&optional n flag)
  566. (or n (setq n ediff-current-difference))
  567. (if (< ediff-number-of-differences 1)
  568. (error ediff-NO-DIFFERENCES))
  569. (if ediff-word-mode
  570. (setq flag 'skip
  571. ediff-auto-refine 'nix))
  572. (or (< n 0)
  573. (>= n ediff-number-of-differences)
  574. ;; n is within the range
  575. (let ((tmp-buffer (get-buffer-create ediff-tmp-buffer))
  576. (file-A ediff-temp-file-A)
  577. (file-B ediff-temp-file-B)
  578. (file-C ediff-temp-file-C)
  579. (empty-A (ediff-empty-diff-region-p n 'A))
  580. (empty-B (ediff-empty-diff-region-p n 'B))
  581. (empty-C (ediff-empty-diff-region-p n 'C))
  582. (whitespace-A (ediff-whitespace-diff-region-p n 'A))
  583. (whitespace-B (ediff-whitespace-diff-region-p n 'B))
  584. (whitespace-C (ediff-whitespace-diff-region-p n 'C))
  585. cumulative-fine-diff-length)
  586. (cond ;; If one of the regions is empty (or 2 in 3way comparison)
  587. ;; then don't refine.
  588. ;; If the region happens to be entirely whitespace or empty then
  589. ;; mark as such.
  590. ((> (length (delq nil (list empty-A empty-B empty-C))) 1)
  591. (if (and (ediff-looks-like-combined-merge n)
  592. ediff-merge-job)
  593. (ediff-set-fine-overlays-in-one-buffer 'C nil n))
  594. (if ediff-3way-comparison-job
  595. (ediff-message-if-verbose
  596. "Region %d is empty in all buffers but %S"
  597. (1+ n)
  598. (cond ((not empty-A) 'A)
  599. ((not empty-B) 'B)
  600. ((not empty-C) 'C)))
  601. (ediff-message-if-verbose
  602. "Region %d in buffer %S is empty"
  603. (1+ n)
  604. (cond (empty-A 'A)
  605. (empty-B 'B)
  606. (empty-C 'C)))
  607. )
  608. ;; if all regions happen to be whitespace
  609. (if (and whitespace-A whitespace-B whitespace-C)
  610. ;; mark as space only
  611. (ediff-mark-diff-as-space-only n t)
  612. ;; if some regions are white and others don't, then mark as
  613. ;; non-white-space-only
  614. (ediff-mark-diff-as-space-only n nil)))
  615. ;; don't compute fine diffs if diff vector exists
  616. ((and (eq flag 'noforce) (ediff-get-fine-diff-vector n 'A))
  617. (if (ediff-no-fine-diffs-p n)
  618. (message
  619. "Only white-space differences in region %d %s"
  620. (1+ n)
  621. (cond ((eq (ediff-no-fine-diffs-p n) 'A)
  622. "in buffers B & C")
  623. ((eq (ediff-no-fine-diffs-p n) 'B)
  624. "in buffers A & C")
  625. ((eq (ediff-no-fine-diffs-p n) 'C)
  626. "in buffers A & B")
  627. (t "")))))
  628. ;; don't compute fine diffs for this region
  629. ((eq flag 'skip)
  630. (or (ediff-get-fine-diff-vector n 'A)
  631. (memq ediff-auto-refine '(off nix))
  632. (ediff-message-if-verbose
  633. "Region %d exceeds the auto-refinement limit. Type `%s' to refine"
  634. (1+ n)
  635. (substitute-command-keys
  636. "\\[ediff-make-or-kill-fine-diffs]")
  637. )))
  638. (t
  639. ;; recompute fine diffs
  640. (ediff-wordify
  641. (ediff-get-diff-posn 'A 'beg n)
  642. (ediff-get-diff-posn 'A 'end n)
  643. ediff-buffer-A
  644. tmp-buffer
  645. ediff-control-buffer)
  646. (setq file-A
  647. (ediff-make-temp-file tmp-buffer "fineDiffA" file-A))
  648. (ediff-wordify
  649. (ediff-get-diff-posn 'B 'beg n)
  650. (ediff-get-diff-posn 'B 'end n)
  651. ediff-buffer-B
  652. tmp-buffer
  653. ediff-control-buffer)
  654. (setq file-B
  655. (ediff-make-temp-file tmp-buffer "fineDiffB" file-B))
  656. (if ediff-3way-job
  657. (progn
  658. (ediff-wordify
  659. (ediff-get-diff-posn 'C 'beg n)
  660. (ediff-get-diff-posn 'C 'end n)
  661. ediff-buffer-C
  662. tmp-buffer
  663. ediff-control-buffer)
  664. (setq file-C
  665. (ediff-make-temp-file
  666. tmp-buffer "fineDiffC" file-C))))
  667. ;; save temp file names.
  668. (setq ediff-temp-file-A file-A
  669. ediff-temp-file-B file-B
  670. ediff-temp-file-C file-C)
  671. ;; set the new vector of fine diffs, if none exists
  672. (cond ((and ediff-3way-job whitespace-A)
  673. (ediff-setup-fine-diff-regions nil file-B file-C n))
  674. ((and ediff-3way-job whitespace-B)
  675. (ediff-setup-fine-diff-regions file-A nil file-C n))
  676. ((and ediff-3way-job
  677. ;; In merge-jobs, whitespace-C is t, since
  678. ;; ediff-empty-diff-region-p returns t in this case
  679. whitespace-C)
  680. (ediff-setup-fine-diff-regions file-A file-B nil n))
  681. (t
  682. (ediff-setup-fine-diff-regions file-A file-B file-C n)))
  683. (setq cumulative-fine-diff-length
  684. (+ (length (ediff-get-fine-diff-vector n 'A))
  685. (length (ediff-get-fine-diff-vector n 'B))
  686. ;; in merge jobs, the merge buffer is never refined
  687. (if (and file-C (not ediff-merge-job))
  688. (length (ediff-get-fine-diff-vector n 'C))
  689. 0)))
  690. (cond ((or
  691. ;; all regions are white space
  692. (and whitespace-A whitespace-B whitespace-C)
  693. ;; none is white space and no fine diffs detected
  694. (and (not whitespace-A)
  695. (not whitespace-B)
  696. (not (and ediff-3way-job whitespace-C))
  697. (eq cumulative-fine-diff-length 0)))
  698. (ediff-mark-diff-as-space-only n t)
  699. (ediff-message-if-verbose
  700. "Only white-space differences in region %d" (1+ n)))
  701. ((eq cumulative-fine-diff-length 0)
  702. (ediff-message-if-verbose
  703. "Only white-space differences in region %d %s"
  704. (1+ n)
  705. (cond (whitespace-A (ediff-mark-diff-as-space-only n 'A)
  706. "in buffers B & C")
  707. (whitespace-B (ediff-mark-diff-as-space-only n 'B)
  708. "in buffers A & C")
  709. (whitespace-C (ediff-mark-diff-as-space-only n 'C)
  710. "in buffers A & B"))))
  711. (t
  712. (ediff-mark-diff-as-space-only n nil)))
  713. )
  714. ) ; end cond
  715. (ediff-set-fine-diff-properties n)
  716. )))
  717. ;; Interface to ediff-make-fine-diffs. Checks for auto-refine limit, etc.
  718. (defun ediff-install-fine-diff-if-necessary (n)
  719. (cond ((and (eq ediff-auto-refine 'on)
  720. ediff-use-faces
  721. (not (eq ediff-highlighting-style 'off))
  722. (not (eq ediff-highlighting-style 'ascii)))
  723. (if (and
  724. (> ediff-auto-refine-limit
  725. (- (ediff-get-diff-posn 'A 'end n)
  726. (ediff-get-diff-posn 'A 'beg n)))
  727. (> ediff-auto-refine-limit
  728. (- (ediff-get-diff-posn 'B 'end n)
  729. (ediff-get-diff-posn 'B 'beg n))))
  730. (ediff-make-fine-diffs n 'noforce)
  731. (ediff-make-fine-diffs n 'skip)))
  732. ;; highlight if fine diffs already exist
  733. ((eq ediff-auto-refine 'off)
  734. (ediff-make-fine-diffs n 'skip))))
  735. ;; if fine diff vector is not set for diff N, then do nothing
  736. (defun ediff-set-fine-diff-properties (n &optional default)
  737. (or (not (ediff-has-face-support-p))
  738. (< n 0)
  739. (>= n ediff-number-of-differences)
  740. ;; when faces are supported, set faces and priorities of fine overlays
  741. (progn
  742. (ediff-set-fine-diff-properties-in-one-buffer 'A n default)
  743. (ediff-set-fine-diff-properties-in-one-buffer 'B n default)
  744. (if ediff-3way-job
  745. (ediff-set-fine-diff-properties-in-one-buffer 'C n default)))))
  746. (defun ediff-set-fine-diff-properties-in-one-buffer (buf-type
  747. n &optional default)
  748. (let ((fine-diff-vector (ediff-get-fine-diff-vector n buf-type))
  749. (face (if default
  750. 'default
  751. (ediff-get-symbol-from-alist
  752. buf-type ediff-fine-diff-face-alist)
  753. ))
  754. (priority (if default
  755. 0
  756. (1+ (or (ediff-overlay-get
  757. (symbol-value
  758. (ediff-get-symbol-from-alist
  759. buf-type
  760. ediff-current-diff-overlay-alist))
  761. 'priority)
  762. 0)))))
  763. (mapcar (lambda (overl)
  764. (ediff-set-overlay-face overl face)
  765. (ediff-overlay-put overl 'priority priority))
  766. fine-diff-vector)))
  767. ;; Set overlays over the regions that denote delimiters
  768. (defun ediff-set-fine-overlays-for-combined-merge (diff-list reg-num)
  769. (let (overlay overlay-list)
  770. (while diff-list
  771. (condition-case nil
  772. (setq overlay
  773. (ediff-make-bullet-proof-overlay
  774. (nth 0 diff-list) (nth 1 diff-list) ediff-buffer-C))
  775. (error ""))
  776. (setq overlay-list (cons overlay overlay-list))
  777. (if (> (length diff-list) 1)
  778. (setq diff-list (cdr (cdr diff-list)))
  779. (error "ediff-set-fine-overlays-for-combined-merge: corrupt list of
  780. delimiter regions"))
  781. )
  782. (setq overlay-list (reverse overlay-list))
  783. (ediff-set-fine-diff-vector
  784. reg-num 'C (apply 'vector overlay-list))
  785. ))
  786. ;; Convert diff list to overlays for a given DIFF-REGION
  787. ;; in buffer of type BUF-TYPE
  788. (defun ediff-set-fine-overlays-in-one-buffer (buf-type diff-list region-num)
  789. (let* ((current-diff -1)
  790. (reg-start (ediff-get-diff-posn buf-type 'beg region-num))
  791. (buff (ediff-get-buffer buf-type))
  792. (ctl-buf ediff-control-buffer)
  793. combined-merge-diff-list
  794. diff-overlay-list list-element
  795. begin end overlay)
  796. (ediff-clear-fine-differences-in-one-buffer region-num buf-type)
  797. (setq diff-list (cdr diff-list)) ; discard list type (words or points)
  798. (ediff-with-current-buffer buff (goto-char reg-start))
  799. ;; if it is a combined merge then set overlays in buff C specially
  800. (if (and ediff-merge-job (eq buf-type 'C)
  801. (setq combined-merge-diff-list
  802. (ediff-looks-like-combined-merge region-num)))
  803. (ediff-set-fine-overlays-for-combined-merge
  804. combined-merge-diff-list region-num)
  805. ;; regular fine diff
  806. (while diff-list
  807. (setq current-diff (1+ current-diff)
  808. list-element (car diff-list)
  809. begin (aref list-element (cond ((eq buf-type 'A) 0)
  810. ((eq buf-type 'B) 2)
  811. (t 4))) ; buf C
  812. end (aref list-element (cond ((eq buf-type 'A) 1)
  813. ((eq buf-type 'B) 3)
  814. (t 5)))) ; buf C
  815. (if (not (or begin end))
  816. () ; skip this diff
  817. ;; Put overlays at appropriate places in buffers
  818. ;; convert lines to points, if necessary
  819. (ediff-with-current-buffer ctl-buf
  820. (setq begin (ediff-goto-word (1+ begin) buff)
  821. end (ediff-goto-word end buff 'end)))
  822. (setq overlay (ediff-make-bullet-proof-overlay begin end buff))
  823. ;; record all overlays for this difference region
  824. (setq diff-overlay-list (nconc diff-overlay-list (list overlay))))
  825. (setq diff-list (cdr diff-list))
  826. ) ; while
  827. ;; convert the list of difference information into a vector
  828. ;; for fast access
  829. (ediff-set-fine-diff-vector
  830. region-num buf-type (vconcat diff-overlay-list))
  831. )))
  832. (defun ediff-convert-fine-diffs-to-overlays (diff-list region-num)
  833. (ediff-set-fine-overlays-in-one-buffer 'A diff-list region-num)
  834. (ediff-set-fine-overlays-in-one-buffer 'B diff-list region-num)
  835. (if ediff-3way-job
  836. (ediff-set-fine-overlays-in-one-buffer 'C diff-list region-num)
  837. ))
  838. ;; Stolen from emerge.el
  839. (defun ediff-get-diff3-group (file)
  840. ;; This save-excursion allows ediff-get-diff3-group to be called for the
  841. ;; various groups of lines (1, 2, 3) in any order, and for the lines to
  842. ;; appear in any order. The reason this is necessary is that Gnu diff3
  843. ;; can produce the groups in the order 1, 2, 3 or 1, 3, 2.
  844. (save-excursion
  845. (re-search-forward
  846. (concat "^" file ":\\([0-9]+\\)\\(,\\([0-9]+\\)\\)?\\([ac]\\)\C-m?$"))
  847. (beginning-of-line 2)
  848. ;; treatment depends on whether it is an "a" group or a "c" group
  849. (if (string-equal (buffer-substring (match-beginning 4) (match-end 4)) "c")
  850. ;; it is a "c" group
  851. (if (match-beginning 2)
  852. ;; it has two numbers
  853. (list (string-to-number
  854. (buffer-substring (match-beginning 1) (match-end 1)))
  855. (1+ (string-to-number
  856. (buffer-substring (match-beginning 3) (match-end 3)))))
  857. ;; it has one number
  858. (let ((x (string-to-number
  859. (buffer-substring (match-beginning 1) (match-end 1)))))
  860. (list x (1+ x))))
  861. ;; it is an "a" group
  862. (let ((x (1+ (string-to-number
  863. (buffer-substring (match-beginning 1) (match-end 1))))))
  864. (list x x)))))
  865. ;; If WORD-MODE, construct vector of diffs using word numbers.
  866. ;; Else, use point values.
  867. ;; WORD-MODE also tells if we are in the word-mode or not.
  868. ;; If THREE-WAY-COMP, then it is a 3-way comparison. Else, it is merging
  869. ;; with ancestor, in which case buffer-C contents is identical to buffer-A/B,
  870. ;; contents (unless buffer-A is narrowed) depending on ediff-default-variant's
  871. ;; value.
  872. ;; BOUNDS specifies visibility bounds to use.
  873. (defun ediff-extract-diffs3 (diff-buffer word-mode three-way-comp
  874. &optional bounds)
  875. (let ((A-buffer ediff-buffer-A)
  876. (B-buffer ediff-buffer-B)
  877. (C-buffer ediff-buffer-C)
  878. (anc-buffer ediff-ancestor-buffer)
  879. (a-prev 1) ; needed to set the first diff line correctly
  880. (a-prev-pt nil)
  881. (b-prev 1)
  882. (b-prev-pt nil)
  883. (c-prev 1)
  884. (c-prev-pt nil)
  885. (anc-prev 1)
  886. diff-list shift-A shift-B shift-C
  887. )
  888. ;; diff list contains word numbers or points, depending on word-mode
  889. (setq diff-list (cons (if word-mode 'words 'points)
  890. diff-list))
  891. (if bounds
  892. (setq shift-A
  893. (ediff-overlay-start
  894. (ediff-get-value-according-to-buffer-type 'A bounds))
  895. shift-B
  896. (ediff-overlay-start
  897. (ediff-get-value-according-to-buffer-type 'B bounds))
  898. shift-C
  899. (if three-way-comp
  900. (ediff-overlay-start
  901. (ediff-get-value-according-to-buffer-type 'C bounds)))))
  902. ;; reset point in buffers A, B, C
  903. (ediff-with-current-buffer A-buffer
  904. (goto-char (if shift-A shift-A (point-min))))
  905. (ediff-with-current-buffer B-buffer
  906. (goto-char (if shift-B shift-B (point-min))))
  907. (if three-way-comp
  908. (ediff-with-current-buffer C-buffer
  909. (goto-char (if shift-C shift-C (point-min)))))
  910. (if (ediff-buffer-live-p anc-buffer)
  911. (ediff-with-current-buffer anc-buffer
  912. (goto-char (point-min))))
  913. (ediff-with-current-buffer diff-buffer
  914. (goto-char (point-min))
  915. (while (re-search-forward ediff-match-diff3-line nil t)
  916. ;; leave point after matched line
  917. (beginning-of-line 2)
  918. (let ((agreement (buffer-substring (match-beginning 1) (match-end 1))))
  919. ;; if the files A and B are the same and not 3way-comparison,
  920. ;; ignore the difference
  921. (if (or three-way-comp (not (string-equal agreement "3")))
  922. (let* ((a-begin (car (ediff-get-diff3-group "1")))
  923. (a-end (nth 1 (ediff-get-diff3-group "1")))
  924. (b-begin (car (ediff-get-diff3-group "2")))
  925. (b-end (nth 1 (ediff-get-diff3-group "2")))
  926. (c-or-anc-begin (car (ediff-get-diff3-group "3")))
  927. (c-or-anc-end (nth 1 (ediff-get-diff3-group "3")))
  928. (state-of-merge
  929. (cond ((string-equal agreement "1") 'prefer-A)
  930. ((string-equal agreement "2") 'prefer-B)
  931. (t ediff-default-variant)))
  932. (state-of-diff-merge
  933. (if (memq state-of-merge '(default-A prefer-A)) 'B 'A))
  934. (state-of-diff-comparison
  935. (cond ((string-equal agreement "1") 'A)
  936. ((string-equal agreement "2") 'B)
  937. ((string-equal agreement "3") 'C)))
  938. state-of-ancestor
  939. c-begin c-end
  940. a-begin-pt a-end-pt
  941. b-begin-pt b-end-pt
  942. c-begin-pt c-end-pt
  943. anc-begin-pt anc-end-pt)
  944. (setq state-of-ancestor
  945. (= c-or-anc-begin c-or-anc-end))
  946. (cond (three-way-comp
  947. (setq c-begin c-or-anc-begin
  948. c-end c-or-anc-end))
  949. ((eq ediff-default-variant 'default-B)
  950. (setq c-begin b-begin
  951. c-end b-end))
  952. (t
  953. (setq c-begin a-begin
  954. c-end a-end)))
  955. ;; compute main diff vector
  956. (if word-mode
  957. ;; make diff-list contain word numbers
  958. (setq diff-list
  959. (nconc diff-list
  960. (list (vector
  961. (- a-begin a-prev) (- a-end a-begin)
  962. (- b-begin b-prev) (- b-end b-begin)
  963. (- c-begin c-prev) (- c-end c-begin)
  964. nil nil ; dummy ancestor
  965. nil ; state of diff
  966. nil ; state of merge
  967. nil ; state of ancestor
  968. )))
  969. a-prev a-end
  970. b-prev b-end
  971. c-prev c-end)
  972. ;; else convert lines to points
  973. (ediff-with-current-buffer A-buffer
  974. (let ((longlines-mode-val
  975. (if (and (boundp 'longlines-mode) longlines-mode) 1 0)))
  976. ;; we must disable and then restore longlines-mode
  977. (if (eq longlines-mode-val 1)
  978. (longlines-mode 0))
  979. (goto-char (or a-prev-pt shift-A (point-min)))
  980. (forward-line (- a-begin a-prev))
  981. (setq a-begin-pt (point))
  982. (forward-line (- a-end a-begin))
  983. (setq a-end-pt (point)
  984. a-prev a-end
  985. a-prev-pt a-end-pt)
  986. (if (eq longlines-mode-val 1)
  987. (longlines-mode longlines-mode-val))
  988. ))
  989. (ediff-with-current-buffer B-buffer
  990. (let ((longlines-mode-val
  991. (if (and (boundp 'longlines-mode) longlines-mode) 1 0)))
  992. (if (eq longlines-mode-val 1)
  993. (longlines-mode 0))
  994. (goto-char (or b-prev-pt shift-B (point-min)))
  995. (forward-line (- b-begin b-prev))
  996. (setq b-begin-pt (point))
  997. (forward-line (- b-end b-begin))
  998. (setq b-end-pt (point)
  999. b-prev b-end
  1000. b-prev-pt b-end-pt)
  1001. (if (eq longlines-mode-val 1)
  1002. (longlines-mode longlines-mode-val))
  1003. ))
  1004. (ediff-with-current-buffer C-buffer
  1005. (let ((longlines-mode-val
  1006. (if (and (boundp 'longlines-mode) longlines-mode) 1 0)))
  1007. (if (eq longlines-mode-val 1)
  1008. (longlines-mode 0))
  1009. (goto-char (or c-prev-pt shift-C (point-min)))
  1010. (forward-line (- c-begin c-prev))
  1011. (setq c-begin-pt (point))
  1012. (forward-line (- c-end c-begin))
  1013. (setq c-end-pt (point)
  1014. c-prev c-end
  1015. c-prev-pt c-end-pt)
  1016. (if (eq longlines-mode-val 1)
  1017. (longlines-mode longlines-mode-val))
  1018. ))
  1019. (if (ediff-buffer-live-p anc-buffer)
  1020. (ediff-with-current-buffer anc-buffer
  1021. (let ((longlines-mode-val
  1022. (if (and (boundp 'longlines-mode) longlines-mode) 1 0)))
  1023. (if (eq longlines-mode-val 1)
  1024. (longlines-mode 0))
  1025. (forward-line (- c-or-anc-begin anc-prev))
  1026. (setq anc-begin-pt (point))
  1027. (forward-line (- c-or-anc-end c-or-anc-begin))
  1028. (setq anc-end-pt (point)
  1029. anc-prev c-or-anc-end)
  1030. (if (eq longlines-mode-val 1)
  1031. (longlines-mode longlines-mode-val))
  1032. )))
  1033. (setq diff-list
  1034. (nconc
  1035. diff-list
  1036. ;; if comparing with ancestor, then there also is a
  1037. ;; state-of-difference marker
  1038. (if three-way-comp
  1039. (list (vector
  1040. a-begin-pt a-end-pt
  1041. b-begin-pt b-end-pt
  1042. c-begin-pt c-end-pt
  1043. nil nil ; ancestor begin/end
  1044. state-of-diff-comparison
  1045. nil ; state of merge
  1046. nil ; state of ancestor
  1047. ))
  1048. (list (vector a-begin-pt a-end-pt
  1049. b-begin-pt b-end-pt
  1050. c-begin-pt c-end-pt
  1051. anc-begin-pt anc-end-pt
  1052. state-of-diff-merge
  1053. state-of-merge
  1054. state-of-ancestor
  1055. )))
  1056. )))
  1057. ))
  1058. ))) ; end ediff-with-current-buffer
  1059. diff-list
  1060. ))
  1061. ;; Generate the difference vector and overlays for three files
  1062. ;; File-C is either the third file to compare (in case of 3-way comparison)
  1063. ;; or it is the ancestor file.
  1064. (defun ediff-setup-diff-regions3 (file-A file-B file-C)
  1065. ;; looking for '-i' or a 'i' among clustered non-long options
  1066. (if (string-match "^-i\\| -i\\|\\(^\\| \\)-[^- ]+i" ediff-diff-options)
  1067. (error "Option `-i' is not allowed in `ediff-diff3-options'"))
  1068. (or (ediff-buffer-live-p ediff-diff-buffer)
  1069. (setq ediff-diff-buffer
  1070. (get-buffer-create (ediff-unique-buffer-name "*ediff-diff" "*"))))
  1071. (message "Computing differences ...")
  1072. (ediff-exec-process ediff-diff3-program ediff-diff-buffer 'synchronize
  1073. ediff-actual-diff3-options file-A file-B file-C)
  1074. (ediff-prepare-error-list ediff-diff3-ok-lines-regexp ediff-diff-buffer)
  1075. ;;(message "Computing differences ... done")
  1076. (ediff-convert-diffs-to-overlays
  1077. (ediff-extract-diffs3
  1078. ediff-diff-buffer
  1079. ediff-word-mode ediff-3way-comparison-job ediff-narrow-bounds)
  1080. ))
  1081. ;; Execute PROGRAM asynchronously, unless OS/2, Windows-*, or DOS, or unless
  1082. ;; SYNCH is non-nil. BUFFER must be a buffer object, and must be alive. The
  1083. ;; OPTIONS arg is a list of options to pass to PROGRAM. It may be a blank
  1084. ;; string. All elements in FILES must be strings. We also delete nil from
  1085. ;; args.
  1086. (defun ediff-exec-process (program buffer synch options &rest files)
  1087. (let ((data (match-data))
  1088. ;; If this is a buffer job, we are diffing temporary files
  1089. ;; produced by Emacs with ediff-coding-system-for-write, so
  1090. ;; use the same encoding to read the results.
  1091. (coding-system-for-read
  1092. (if (string-match "buffer" (symbol-name ediff-job-name))
  1093. ediff-coding-system-for-write
  1094. ediff-coding-system-for-read))
  1095. args)
  1096. (setq args (append (split-string options) files))
  1097. (setq args (delete "" (delq nil args))) ; delete nil and "" from arguments
  1098. ;; the --binary option, if present, should be used only for buffer jobs
  1099. ;; or for refining the differences
  1100. (or (string-match "buffer" (symbol-name ediff-job-name))
  1101. (eq buffer ediff-fine-diff-buffer)
  1102. (setq args (delete "--binary" args)))
  1103. (unwind-protect
  1104. (let ((directory default-directory)
  1105. proc)
  1106. (with-current-buffer buffer
  1107. (erase-buffer)
  1108. (setq default-directory directory)
  1109. (if (or (memq system-type '(ms-dos windows-nt))
  1110. synch)
  1111. ;; In Windows do it synchronously, since Windows doesn't let us
  1112. ;; delete files used by other processes. Thus, in ediff-buffers
  1113. ;; and similar functions, we can't delete temp files because
  1114. ;; they might be used by the asynch process that computes
  1115. ;; custom diffs. So, we have to wait till custom diff
  1116. ;; subprocess is done.
  1117. ;; In DOS, must synchronize because DOS doesn't have
  1118. ;; asynchronous processes.
  1119. (apply 'call-process program nil buffer nil args)
  1120. ;; On other systems, do it asynchronously.
  1121. (setq proc (get-buffer-process buffer))
  1122. (if proc (kill-process proc))
  1123. (setq proc
  1124. (apply 'start-process "Custom Diff" buffer program args))
  1125. (setq mode-line-process '(":%s"))
  1126. (set-process-sentinel proc 'ediff-process-sentinel)
  1127. (set-process-filter proc 'ediff-process-filter)
  1128. )))
  1129. (store-match-data data))))
  1130. ;; This is shell-command-filter from simple.el in Emacs.
  1131. ;; Copied here because XEmacs doesn't have it.
  1132. (defun ediff-process-filter (proc string)
  1133. ;; Do save-excursion by hand so that we can leave point numerically unchanged
  1134. ;; despite an insertion immediately after it.
  1135. (let* ((obuf (current-buffer))
  1136. (buffer (process-buffer proc))
  1137. opoint
  1138. (window (get-buffer-window buffer))
  1139. (pos (window-start window)))
  1140. (unwind-protect
  1141. (progn
  1142. (set-buffer buffer)
  1143. (or (= (point) (point-max))
  1144. (setq opoint (point)))
  1145. (goto-char (point-max))
  1146. (insert-before-markers string))
  1147. ;; insert-before-markers moved this marker: set it back.
  1148. (set-window-start window pos)
  1149. ;; Finish our save-excursion.
  1150. (if opoint
  1151. (goto-char opoint))
  1152. (set-buffer obuf))))
  1153. ;; like shell-command-sentinel but doesn't print an exit status message
  1154. ;; we do this because diff always exits with status 1, if diffs are found
  1155. ;; so shell-command-sentinel displays a confusing message to the user
  1156. (defun ediff-process-sentinel (process signal)
  1157. (if (and (memq (process-status process) '(exit signal))
  1158. (buffer-name (process-buffer process)))
  1159. (progn
  1160. (with-current-buffer (process-buffer process)
  1161. (setq mode-line-process nil))
  1162. (delete-process process))))
  1163. ;;; Word functions used to refine the current diff
  1164. (defvar ediff-forward-word-function 'ediff-forward-word
  1165. "*Function to call to move to the next word.
  1166. Used for splitting difference regions into individual words.")
  1167. (make-variable-buffer-local 'ediff-forward-word-function)
  1168. ;; \240 is Unicode symbol for nonbreakable whitespace
  1169. (defvar ediff-whitespace " \n\t\f\r\240"
  1170. "*Characters constituting white space.
  1171. These characters are ignored when differing regions are split into words.")
  1172. (make-variable-buffer-local 'ediff-whitespace)
  1173. (defvar ediff-word-1
  1174. (if (featurep 'xemacs) "a-zA-Z---_" "-[:word:]_")
  1175. "*Characters that constitute words of type 1.
  1176. More precisely, [ediff-word-1] is a regexp that matches type 1 words.
  1177. See `ediff-forward-word' for more details.")
  1178. (make-variable-buffer-local 'ediff-word-1)
  1179. (defvar ediff-word-2 "0-9.,"
  1180. "*Characters that constitute words of type 2.
  1181. More precisely, [ediff-word-2] is a regexp that matches type 2 words.
  1182. See `ediff-forward-word' for more details.")
  1183. (make-variable-buffer-local 'ediff-word-2)
  1184. (defvar ediff-word-3 "`'?!:;\"{}[]()"
  1185. "*Characters that constitute words of type 3.
  1186. More precisely, [ediff-word-3] is a regexp that matches type 3 words.
  1187. See `ediff-forward-word' for more details.")
  1188. (make-variable-buffer-local 'ediff-word-3)
  1189. (defvar ediff-word-4
  1190. (concat "^" ediff-word-1 ediff-word-2 ediff-word-3 ediff-whitespace)
  1191. "*Characters that constitute words of type 4.
  1192. More precisely, [ediff-word-4] is a regexp that matches type 4 words.
  1193. See `ediff-forward-word' for more details.")
  1194. (make-variable-buffer-local 'ediff-word-4)
  1195. ;; Split region along word boundaries. Each word will be on its own line.
  1196. ;; Output to buffer out-buffer.
  1197. (defun ediff-forward-word ()
  1198. "Move point one word forward.
  1199. There are four types of words, each of which consists entirely of
  1200. characters in `ediff-word-1', `ediff-word-2', `ediff-word-3', or
  1201. `ediff-word-4'. Words are recognized by passing these one after another as
  1202. arguments to `skip-chars-forward'."
  1203. (or (> (+ (skip-chars-forward ediff-word-1)
  1204. (skip-syntax-forward "w"))
  1205. 0)
  1206. (> (skip-chars-forward ediff-word-2) 0)
  1207. (> (skip-chars-forward ediff-word-3) 0)
  1208. (> (skip-chars-forward ediff-word-4) 0)
  1209. ))
  1210. (defun ediff-wordify (beg end in-buffer out-buffer &optional control-buf)
  1211. (let ((forward-word-function
  1212. ;; eval in control buf to let user create local versions for
  1213. ;; different invocations
  1214. (if control-buf
  1215. (ediff-with-current-buffer control-buf
  1216. ediff-forward-word-function)
  1217. ediff-forward-word-function))
  1218. inbuf-syntax-tbl sv-point diff-string)
  1219. (with-current-buffer in-buffer
  1220. (setq inbuf-syntax-tbl
  1221. (if control-buf
  1222. (ediff-with-current-buffer control-buf
  1223. ediff-syntax-table)
  1224. (syntax-table)))
  1225. (setq diff-string (buffer-substring-no-properties beg end))
  1226. (set-buffer out-buffer)
  1227. ;; Make sure that temp buff syntax table is the same as the original buf
  1228. ;; syntax tbl, because we use ediff-forward-word in both and
  1229. ;; ediff-forward-word depends on the syntax classes of characters.
  1230. (set-syntax-table inbuf-syntax-tbl)
  1231. (erase-buffer)
  1232. (insert diff-string)
  1233. (goto-char (point-min))
  1234. (skip-chars-forward ediff-whitespace)
  1235. (delete-region (point-min) (point))
  1236. (while (not (eobp))
  1237. (funcall forward-word-function)
  1238. (setq sv-point (point))
  1239. (skip-chars-forward ediff-whitespace)
  1240. (delete-region sv-point (point))
  1241. (insert "\n")))))
  1242. ;; copy string specified as BEG END from IN-BUF to OUT-BUF
  1243. (defun ediff-copy-to-buffer (beg end in-buffer out-buffer)
  1244. (with-current-buffer out-buffer
  1245. (erase-buffer)
  1246. (insert-buffer-substring in-buffer beg end)
  1247. (goto-char (point-min))))
  1248. ;; goto word #n starting at current position in buffer `buf'
  1249. ;; For ediff, a word is determined by ediff-forward-word-function
  1250. ;; If `flag' is non-nil, goto the end of the n-th word.
  1251. (defun ediff-goto-word (n buf &optional flag)
  1252. ;; remember val ediff-forward-word-function has in ctl buf
  1253. (let ((fwd-word-fun ediff-forward-word-function)
  1254. (syntax-tbl ediff-syntax-table))
  1255. (ediff-with-current-buffer buf
  1256. (skip-chars-forward ediff-whitespace)
  1257. (ediff-with-syntax-table syntax-tbl
  1258. (while (> n 1)
  1259. (funcall fwd-word-fun)
  1260. (skip-chars-forward ediff-whitespace)
  1261. (setq n (1- n)))
  1262. (if (and flag (> n 0))
  1263. (funcall fwd-word-fun)))
  1264. (point))))
  1265. (defun ediff-same-file-contents (f1 f2)
  1266. "Return t if files F1 and F2 have identical contents."
  1267. (if (and (not (file-directory-p f1))
  1268. (not (file-directory-p f2)))
  1269. (let ((res
  1270. (apply 'call-process ediff-cmp-program nil nil nil
  1271. (append ediff-cmp-options (list (expand-file-name f1)
  1272. (expand-file-name f2))))
  1273. ))
  1274. (and (numberp res) (eq res 0)))
  1275. ))
  1276. (defun ediff-same-contents (d1 d2 &optional filter-re)
  1277. "Return t if D1 and D2 have the same content.
  1278. D1 and D2 can either be both directories or both regular files.
  1279. Symlinks and the likes are not handled.
  1280. If FILTER-RE is non-nil, recursive checking in directories
  1281. affects only files whose names match the expression."
  1282. ;; Normalize empty filter RE to nil.
  1283. (unless (> (length filter-re) 0) (setq filter-re nil))
  1284. ;; Indicate progress
  1285. (message "Comparing '%s' and '%s' modulo '%s'" d1 d2 filter-re)
  1286. (cond
  1287. ;; D1 & D2 directories => recurse
  1288. ((and (file-directory-p d1)
  1289. (file-directory-p d2))
  1290. (if (null ediff-recurse-to-subdirectories)
  1291. (if (y-or-n-p "Compare subdirectories recursively? ")
  1292. (setq ediff-recurse-to-subdirectories 'yes)
  1293. (setq ediff-recurse-to-subdirectories 'no)))
  1294. (if (eq ediff-recurse-to-subdirectories 'yes)
  1295. (let* ((all-entries-1 (directory-files d1 t filter-re))
  1296. (all-entries-2 (directory-files d2 t filter-re))
  1297. (entries-1 (ediff-delete-all-matches "^\\.\\.?$" all-entries-1))
  1298. (entries-2 (ediff-delete-all-matches "^\\.\\.?$" all-entries-2))
  1299. )
  1300. (ediff-same-file-contents-lists entries-1 entries-2 filter-re)
  1301. ))
  1302. ) ; end of the directories case
  1303. ;; D1 & D2 are both files => compare directly
  1304. ((and (file-regular-p d1)
  1305. (file-regular-p d2))
  1306. (ediff-same-file-contents d1 d2))
  1307. ;; Otherwise => false: unequal contents
  1308. )
  1309. )
  1310. ;; If lists have the same length and names of files are pairwise equal
  1311. ;; (removing the directories) then compare contents pairwise.
  1312. ;; True if all contents are the same; false otherwise
  1313. (defun ediff-same-file-contents-lists (entries-1 entries-2 filter-re)
  1314. ;; First, check only the names (works quickly and ensures a
  1315. ;; precondition for subsequent code)
  1316. (if (and (= (length entries-1) (length entries-2))
  1317. (equal (mapcar 'file-name-nondirectory entries-1)
  1318. (mapcar 'file-name-nondirectory entries-2)))
  1319. ;; With name equality established, compare the entries
  1320. ;; through recursion.
  1321. (let ((continue t))
  1322. (while (and entries-1 continue)
  1323. (if (ediff-same-contents
  1324. (car entries-1) (car entries-2) filter-re)
  1325. (setq entries-1 (cdr entries-1)
  1326. entries-2 (cdr entries-2))
  1327. (setq continue nil))
  1328. )
  1329. ;; if reached the end then lists are equal
  1330. (null entries-1))
  1331. )
  1332. )
  1333. ;; ARG1 is a regexp, ARG2 is a list of full-filenames
  1334. ;; Delete all entries that match the regexp
  1335. (defun ediff-delete-all-matches (regex file-list-list)
  1336. (let (result elt)
  1337. (while file-list-list
  1338. (setq elt (car file-list-list))
  1339. (or (string-match regex (file-name-nondirectory elt))
  1340. (setq result (cons elt result)))
  1341. (setq file-list-list (cdr file-list-list)))
  1342. (reverse result)))
  1343. (defun ediff-set-actual-diff-options ()
  1344. (if ediff-ignore-case
  1345. (setq ediff-actual-diff-options
  1346. (concat ediff-diff-options " " ediff-ignore-case-option)
  1347. ediff-actual-diff3-options
  1348. (concat ediff-diff3-options " " ediff-ignore-case-option3))
  1349. (setq ediff-actual-diff-options ediff-diff-options
  1350. ediff-actual-diff3-options ediff-diff3-options)
  1351. )
  1352. (setq-default ediff-actual-diff-options ediff-actual-diff-options
  1353. ediff-actual-diff3-options ediff-actual-diff3-options)
  1354. )
  1355. ;; Ignore case handling - some ideas from drew.adams@@oracle.com
  1356. (defun ediff-toggle-ignore-case ()
  1357. (interactive)
  1358. (ediff-barf-if-not-control-buffer)
  1359. (setq ediff-ignore-case (not ediff-ignore-case))
  1360. (ediff-set-actual-diff-options)
  1361. (if ediff-ignore-case
  1362. (message "Ignoring regions that differ only in case")
  1363. (message "Ignoring case differences turned OFF"))
  1364. (cond (ediff-merge-job
  1365. (message "Ignoring letter case is too dangerous in merge jobs"))
  1366. ((and ediff-diff3-job (string= ediff-ignore-case-option3 ""))
  1367. (message "Ignoring letter case is not supported by this diff3 program"))
  1368. ((and (not ediff-3way-job) (string= ediff-ignore-case-option ""))
  1369. (message "Ignoring letter case is not supported by this diff program"))
  1370. (t
  1371. (sit-for 1)
  1372. (ediff-update-diffs)))
  1373. )
  1374. ;; Local Variables:
  1375. ;; eval: (put 'ediff-defvar-local 'lisp-indent-hook 'defun)
  1376. ;; eval: (put 'ediff-with-current-buffer 'lisp-indent-hook 1)
  1377. ;; eval: (put 'ediff-with-current-buffer 'edebug-form-spec '(form body))
  1378. ;; End:
  1379. ;;; ediff-diff.el ends here