ediff.el 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565
  1. ;;; ediff.el --- a comprehensive visual interface to diff & patch
  2. ;; Copyright (C) 1994-2012 Free Software Foundation, Inc.
  3. ;; Author: Michael Kifer <kifer@cs.stonybrook.edu>
  4. ;; Created: February 2, 1994
  5. ;; Keywords: comparing, merging, patching, vc, tools, unix
  6. ;; Version: 2.81.4
  7. ;; Yoni Rabkin <yoni@rabkins.net> contacted the maintainer of this
  8. ;; file on 20/3/2008, and the maintainer agreed that when a bug is
  9. ;; filed in the Emacs bug reporting system against this file, a copy
  10. ;; of the bug report be sent to the maintainer's email address.
  11. (defconst ediff-version "2.81.4" "The current version of Ediff")
  12. (defconst ediff-date "December 7, 2009" "Date of last update")
  13. ;; This file is part of GNU Emacs.
  14. ;; GNU Emacs is free software: you can redistribute it and/or modify
  15. ;; it under the terms of the GNU General Public License as published by
  16. ;; the Free Software Foundation, either version 3 of the License, or
  17. ;; (at your option) any later version.
  18. ;; GNU Emacs is distributed in the hope that it will be useful,
  19. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. ;; GNU General Public License for more details.
  22. ;; You should have received a copy of the GNU General Public License
  23. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  24. ;;; Commentary:
  25. ;; Never read that diff output again!
  26. ;; Apply patch interactively!
  27. ;; Merge with ease!
  28. ;; This package provides a convenient way of simultaneous browsing through
  29. ;; the differences between a pair (or a triple) of files or buffers. The
  30. ;; files being compared, file-A, file-B, and file-C (if applicable) are
  31. ;; shown in separate windows (side by side, one above the another, or in
  32. ;; separate frames), and the differences are highlighted as you step
  33. ;; through them. You can also copy difference regions from one buffer to
  34. ;; another (and recover old differences if you change your mind).
  35. ;; Ediff also supports merging operations on files and buffers, including
  36. ;; merging using ancestor versions. Both comparison and merging operations can
  37. ;; be performed on directories, i.e., by pairwise comparison of files in those
  38. ;; directories.
  39. ;; In addition, Ediff can apply a patch to a file and then let you step
  40. ;; though both files, the patched and the original one, simultaneously,
  41. ;; difference-by-difference. You can even apply a patch right out of a
  42. ;; mail buffer, i.e., patches received by mail don't even have to be saved.
  43. ;; Since Ediff lets you copy differences between buffers, you can, in
  44. ;; effect, apply patches selectively (i.e., you can copy a difference
  45. ;; region from file_orig to file, thereby undoing any particular patch that
  46. ;; you don't like).
  47. ;; Ediff is aware of version control, which lets the user compare
  48. ;; files with their older versions. Ediff can also work with remote and
  49. ;; compressed files. Details are given below.
  50. ;; Finally, Ediff supports directory-level comparison, merging and patching.
  51. ;; See the on-line manual for details.
  52. ;; This package builds upon the ideas borrowed from emerge.el and several
  53. ;; Ediff's functions are adaptations from emerge.el. Much of the functionality
  54. ;; Ediff provides is also influenced by emerge.el.
  55. ;; The present version of Ediff supersedes Emerge. It provides a superior user
  56. ;; interface and has numerous major features not found in Emerge. In
  57. ;; particular, it can do patching, and 2-way and 3-way file comparison,
  58. ;; merging, and directory operations.
  59. ;;; Bugs:
  60. ;; 1. The undo command doesn't restore deleted regions well. That is, if
  61. ;; you delete all characters in a difference region and then invoke
  62. ;; `undo', the reinstated text will most likely be inserted outside of
  63. ;; what Ediff thinks is the current difference region. (This problem
  64. ;; doesn't seem to exist with XEmacs.)
  65. ;;
  66. ;; If at any point you feel that difference regions are no longer correct,
  67. ;; you can hit '!' to recompute the differences.
  68. ;; 2. On a monochrome display, the repertoire of faces with which to
  69. ;; highlight fine differences is limited. By default, Ediff is using
  70. ;; underlining. However, if the region is already underlined by some other
  71. ;; overlays, there is no simple way to temporarily remove that residual
  72. ;; underlining. This problem occurs when a buffer is highlighted with
  73. ;; hilit19.el or font-lock.el packages. If this residual highlighting gets
  74. ;; in the way, you can do the following. Both font-lock.el and hilit19.el
  75. ;; provide commands for unhighlighting buffers. You can either place these
  76. ;; commands in `ediff-prepare-buffer-hook' (which will unhighlight every
  77. ;; buffer used by Ediff) or you can execute them interactively, at any time
  78. ;; and on any buffer.
  79. ;;; Acknowledgements:
  80. ;; Ediff was inspired by Dale R. Worley's <drw@math.mit.edu> emerge.el.
  81. ;; Ediff would not have been possible without the help and encouragement of
  82. ;; its many users. See Ediff on-line Info for the full list of those who
  83. ;; helped. Improved defaults in Ediff file-name reading commands.
  84. ;;; Code:
  85. (provide 'ediff)
  86. ;; Compiler pacifier
  87. (eval-and-compile
  88. (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
  89. (eval-when-compile
  90. (require 'dired)
  91. (require 'ediff-util)
  92. (require 'ediff-ptch))
  93. ;; end pacifier
  94. (require 'ediff-init)
  95. (require 'ediff-mult) ; required because of the registry stuff
  96. (defgroup ediff nil
  97. "A comprehensive visual interface to diff & patch."
  98. :tag "Ediff"
  99. :group 'tools)
  100. (defcustom ediff-use-last-dir nil
  101. "If t, Ediff will use previous directory as default when reading file name."
  102. :type 'boolean
  103. :group 'ediff)
  104. ;; Last directory used by an Ediff command for file-A.
  105. (defvar ediff-last-dir-A nil)
  106. ;; Last directory used by an Ediff command for file-B.
  107. (defvar ediff-last-dir-B nil)
  108. ;; Last directory used by an Ediff command for file-C.
  109. (defvar ediff-last-dir-C nil)
  110. ;; Last directory used by an Ediff command for the ancestor file.
  111. (defvar ediff-last-dir-ancestor nil)
  112. ;; Last directory used by an Ediff command as the output directory for merge.
  113. (defvar ediff-last-merge-autostore-dir nil)
  114. ;; Used as a startup hook to set `_orig' patch file read-only.
  115. (defun ediff-set-read-only-in-buf-A ()
  116. (ediff-with-current-buffer ediff-buffer-A
  117. (toggle-read-only 1)))
  118. ;; Return a plausible default for ediff's first file:
  119. ;; In dired, return the file number FILENO (or 0) in the list
  120. ;; (all-selected-files, filename under the cursor), where directories are
  121. ;; ignored. Otherwise, return DEFAULT file name, if non-nil. Else,
  122. ;; if the buffer is visiting a file, return that file name.
  123. (defun ediff-get-default-file-name (&optional default fileno)
  124. (cond ((eq major-mode 'dired-mode)
  125. (let ((current (dired-get-filename nil 'no-error))
  126. (marked (condition-case nil
  127. (dired-get-marked-files 'no-dir)
  128. (error nil)))
  129. aux-list choices result)
  130. (or (integerp fileno) (setq fileno 0))
  131. (if (stringp default)
  132. (setq aux-list (cons default aux-list)))
  133. (if (and (stringp current) (not (file-directory-p current)))
  134. (setq aux-list (cons current aux-list)))
  135. (setq choices (nconc marked aux-list))
  136. (setq result (elt choices fileno))
  137. (or result
  138. default)))
  139. ((stringp default) default)
  140. ((buffer-file-name (current-buffer))
  141. (file-name-nondirectory (buffer-file-name (current-buffer))))
  142. ))
  143. ;;; Compare files/buffers
  144. ;;;###autoload
  145. (defun ediff-files (file-A file-B &optional startup-hooks)
  146. "Run Ediff on a pair of files, FILE-A and FILE-B."
  147. (interactive
  148. (let ((dir-A (if ediff-use-last-dir
  149. ediff-last-dir-A
  150. default-directory))
  151. dir-B f)
  152. (list (setq f (ediff-read-file-name
  153. "File A to compare"
  154. dir-A
  155. (ediff-get-default-file-name)
  156. 'no-dirs))
  157. (ediff-read-file-name "File B to compare"
  158. (setq dir-B
  159. (if ediff-use-last-dir
  160. ediff-last-dir-B
  161. (file-name-directory f)))
  162. (progn
  163. (ediff-add-to-history
  164. 'file-name-history
  165. (ediff-abbreviate-file-name
  166. (expand-file-name
  167. (file-name-nondirectory f)
  168. dir-B)))
  169. (ediff-get-default-file-name f 1)))
  170. )))
  171. (ediff-files-internal file-A
  172. (if (file-directory-p file-B)
  173. (expand-file-name
  174. (file-name-nondirectory file-A) file-B)
  175. file-B)
  176. nil ; file-C
  177. startup-hooks
  178. 'ediff-files))
  179. ;;;###autoload
  180. (defun ediff-files3 (file-A file-B file-C &optional startup-hooks)
  181. "Run Ediff on three files, FILE-A, FILE-B, and FILE-C."
  182. (interactive
  183. (let ((dir-A (if ediff-use-last-dir
  184. ediff-last-dir-A
  185. default-directory))
  186. dir-B dir-C f ff)
  187. (list (setq f (ediff-read-file-name
  188. "File A to compare"
  189. dir-A
  190. (ediff-get-default-file-name)
  191. 'no-dirs))
  192. (setq ff (ediff-read-file-name "File B to compare"
  193. (setq dir-B
  194. (if ediff-use-last-dir
  195. ediff-last-dir-B
  196. (file-name-directory f)))
  197. (progn
  198. (ediff-add-to-history
  199. 'file-name-history
  200. (ediff-abbreviate-file-name
  201. (expand-file-name
  202. (file-name-nondirectory f)
  203. dir-B)))
  204. (ediff-get-default-file-name f 1))))
  205. (ediff-read-file-name "File C to compare"
  206. (setq dir-C (if ediff-use-last-dir
  207. ediff-last-dir-C
  208. (file-name-directory ff)))
  209. (progn
  210. (ediff-add-to-history
  211. 'file-name-history
  212. (ediff-abbreviate-file-name
  213. (expand-file-name
  214. (file-name-nondirectory ff)
  215. dir-C)))
  216. (ediff-get-default-file-name ff 2)))
  217. )))
  218. (ediff-files-internal file-A
  219. (if (file-directory-p file-B)
  220. (expand-file-name
  221. (file-name-nondirectory file-A) file-B)
  222. file-B)
  223. (if (file-directory-p file-C)
  224. (expand-file-name
  225. (file-name-nondirectory file-A) file-C)
  226. file-C)
  227. startup-hooks
  228. 'ediff-files3))
  229. ;;;###autoload
  230. (defalias 'ediff3 'ediff-files3)
  231. (defun ediff-find-file (file-var buffer-name &optional last-dir hooks-var)
  232. "Visit FILE and arrange its buffer to Ediff's liking.
  233. FILE-VAR is actually a variable symbol whose value must contain a true
  234. file name.
  235. BUFFER-NAME is a variable symbol, which will get the buffer object into
  236. which FILE is read.
  237. LAST-DIR is the directory variable symbol where FILE's
  238. directory name should be returned. HOOKS-VAR is a variable symbol that will
  239. be assigned the hook to be executed after `ediff-startup' is finished.
  240. `ediff-find-file' arranges that the temp files it might create will be
  241. deleted."
  242. (let* ((file (symbol-value file-var))
  243. (file-magic (ediff-filename-magic-p file))
  244. (temp-file-name-prefix (file-name-nondirectory file)))
  245. (cond ((not (file-readable-p file))
  246. (error "File `%s' does not exist or is not readable" file))
  247. ((file-directory-p file)
  248. (error "File `%s' is a directory" file)))
  249. ;; some of the commands, below, require full file name
  250. (setq file (expand-file-name file))
  251. ;; Record the directory of the file
  252. (if last-dir
  253. (set last-dir (expand-file-name (file-name-directory file))))
  254. ;; Setup the buffer
  255. (set buffer-name (find-file-noselect file))
  256. (ediff-with-current-buffer (symbol-value buffer-name)
  257. (widen) ; Make sure the entire file is seen
  258. (cond (file-magic ; file has a handler, such as jka-compr-handler or
  259. ;;; ange-ftp-hook-function--arrange for temp file
  260. (ediff-verify-file-buffer 'magic)
  261. (setq file
  262. (ediff-make-temp-file
  263. (current-buffer) temp-file-name-prefix))
  264. (set hooks-var (cons `(lambda () (delete-file ,file))
  265. (symbol-value hooks-var))))
  266. ;; file processed via auto-mode-alist, a la uncompress.el
  267. ((not (equal (file-truename file)
  268. (file-truename (buffer-file-name))))
  269. (setq file
  270. (ediff-make-temp-file
  271. (current-buffer) temp-file-name-prefix))
  272. (set hooks-var (cons `(lambda () (delete-file ,file))
  273. (symbol-value hooks-var))))
  274. (t ;; plain file---just check that the file matches the buffer
  275. (ediff-verify-file-buffer))))
  276. (set file-var file)))
  277. ;; MERGE-BUFFER-FILE is the file to be associated with the merge buffer
  278. (defun ediff-files-internal (file-A file-B file-C startup-hooks job-name
  279. &optional merge-buffer-file)
  280. (let (buf-A buf-B buf-C)
  281. (if (string= file-A file-B)
  282. (error "Files A and B are the same"))
  283. (if (stringp file-C)
  284. (or (and (string= file-A file-C) (error "Files A and C are the same"))
  285. (and (string= file-B file-C) (error "Files B and C are the same"))))
  286. (message "Reading file %s ... " file-A)
  287. ;;(sit-for 0)
  288. (ediff-find-file 'file-A 'buf-A 'ediff-last-dir-A 'startup-hooks)
  289. (message "Reading file %s ... " file-B)
  290. ;;(sit-for 0)
  291. (ediff-find-file 'file-B 'buf-B 'ediff-last-dir-B 'startup-hooks)
  292. (if (stringp file-C)
  293. (progn
  294. (message "Reading file %s ... " file-C)
  295. ;;(sit-for 0)
  296. (ediff-find-file
  297. 'file-C 'buf-C
  298. (if (eq job-name 'ediff-merge-files-with-ancestor)
  299. 'ediff-last-dir-ancestor 'ediff-last-dir-C)
  300. 'startup-hooks)))
  301. (ediff-setup buf-A file-A
  302. buf-B file-B
  303. buf-C file-C
  304. startup-hooks
  305. (list (cons 'ediff-job-name job-name))
  306. merge-buffer-file)))
  307. (declare-function diff-latest-backup-file "diff" (fn))
  308. ;;;###autoload
  309. (defalias 'ediff 'ediff-files)
  310. ;;;###autoload
  311. (defun ediff-current-file ()
  312. "Start ediff between current buffer and its file on disk.
  313. This command can be used instead of `revert-buffer'. If there is
  314. nothing to revert then this command fails."
  315. (interactive)
  316. (unless (or revert-buffer-function
  317. revert-buffer-insert-file-contents-function
  318. (and buffer-file-number
  319. (or (buffer-modified-p)
  320. (not (verify-visited-file-modtime
  321. (current-buffer))))))
  322. (error "Nothing to revert"))
  323. (let* ((auto-save-p (and (recent-auto-save-p)
  324. buffer-auto-save-file-name
  325. (file-readable-p buffer-auto-save-file-name)
  326. (y-or-n-p
  327. "Buffer has been auto-saved recently. Compare with auto-save file? ")))
  328. (file-name (if auto-save-p
  329. buffer-auto-save-file-name
  330. buffer-file-name))
  331. (revert-buf-name (concat "FILE=" file-name))
  332. (revert-buf (get-buffer revert-buf-name))
  333. (current-major major-mode))
  334. (unless file-name
  335. (error "Buffer does not seem to be associated with any file"))
  336. (when revert-buf
  337. (kill-buffer revert-buf)
  338. (setq revert-buf nil))
  339. (setq revert-buf (get-buffer-create revert-buf-name))
  340. (with-current-buffer revert-buf
  341. (insert-file-contents file-name)
  342. ;; Assume same modes:
  343. (funcall current-major))
  344. (ediff-buffers revert-buf (current-buffer))))
  345. ;;;###autoload
  346. (defun ediff-backup (file)
  347. "Run Ediff on FILE and its backup file.
  348. Uses the latest backup, if there are several numerical backups.
  349. If this file is a backup, `ediff' it with its original."
  350. (interactive (list (read-file-name "Ediff (file with backup): ")))
  351. ;; The code is taken from `diff-backup'.
  352. (require 'diff)
  353. (let (bak ori)
  354. (if (backup-file-name-p file)
  355. (setq bak file
  356. ori (file-name-sans-versions file))
  357. (setq bak (or (diff-latest-backup-file file)
  358. (error "No backup found for %s" file))
  359. ori file))
  360. (ediff-files bak ori)))
  361. ;;;###autoload
  362. (defun ediff-buffers (buffer-A buffer-B &optional startup-hooks job-name)
  363. "Run Ediff on a pair of buffers, BUFFER-A and BUFFER-B."
  364. (interactive
  365. (let (bf)
  366. (list (setq bf (read-buffer "Buffer A to compare: "
  367. (ediff-other-buffer "") t))
  368. (read-buffer "Buffer B to compare: "
  369. (progn
  370. ;; realign buffers so that two visible bufs will be
  371. ;; at the top
  372. (save-window-excursion (other-window 1))
  373. (ediff-other-buffer bf))
  374. t))))
  375. (or job-name (setq job-name 'ediff-buffers))
  376. (ediff-buffers-internal buffer-A buffer-B nil startup-hooks job-name))
  377. ;;;###autoload
  378. (defalias 'ebuffers 'ediff-buffers)
  379. ;;;###autoload
  380. (defun ediff-buffers3 (buffer-A buffer-B buffer-C
  381. &optional startup-hooks job-name)
  382. "Run Ediff on three buffers, BUFFER-A, BUFFER-B, and BUFFER-C."
  383. (interactive
  384. (let (bf bff)
  385. (list (setq bf (read-buffer "Buffer A to compare: "
  386. (ediff-other-buffer "") t))
  387. (setq bff (read-buffer "Buffer B to compare: "
  388. (progn
  389. ;; realign buffers so that two visible
  390. ;; bufs will be at the top
  391. (save-window-excursion (other-window 1))
  392. (ediff-other-buffer bf))
  393. t))
  394. (read-buffer "Buffer C to compare: "
  395. (progn
  396. ;; realign buffers so that three visible
  397. ;; bufs will be at the top
  398. (save-window-excursion (other-window 1))
  399. (ediff-other-buffer (list bf bff)))
  400. t)
  401. )))
  402. (or job-name (setq job-name 'ediff-buffers3))
  403. (ediff-buffers-internal buffer-A buffer-B buffer-C startup-hooks job-name))
  404. ;;;###autoload
  405. (defalias 'ebuffers3 'ediff-buffers3)
  406. ;; MERGE-BUFFER-FILE is the file to be associated with the merge buffer
  407. (defun ediff-buffers-internal (buf-A buf-B buf-C startup-hooks job-name
  408. &optional merge-buffer-file)
  409. (let* ((buf-A-file-name (buffer-file-name (get-buffer buf-A)))
  410. (buf-B-file-name (buffer-file-name (get-buffer buf-B)))
  411. (buf-C-is-alive (ediff-buffer-live-p buf-C))
  412. (buf-C-file-name (if buf-C-is-alive
  413. (buffer-file-name (get-buffer buf-B))))
  414. file-A file-B file-C)
  415. (unwind-protect
  416. (progn
  417. (if (not (ediff-buffer-live-p buf-A))
  418. (error "Buffer %S doesn't exist" buf-A))
  419. (if (not (ediff-buffer-live-p buf-B))
  420. (error "Buffer %S doesn't exist" buf-B))
  421. (let ((ediff-job-name job-name))
  422. (if (and ediff-3way-comparison-job
  423. (not buf-C-is-alive))
  424. (error "Buffer %S doesn't exist" buf-C)))
  425. (if (stringp buf-A-file-name)
  426. (setq buf-A-file-name (file-name-nondirectory buf-A-file-name)))
  427. (if (stringp buf-B-file-name)
  428. (setq buf-B-file-name (file-name-nondirectory buf-B-file-name)))
  429. (if (stringp buf-C-file-name)
  430. (setq buf-C-file-name (file-name-nondirectory buf-C-file-name)))
  431. (setq file-A (ediff-make-temp-file buf-A buf-A-file-name)
  432. file-B (ediff-make-temp-file buf-B buf-B-file-name))
  433. (if buf-C-is-alive
  434. (setq file-C (ediff-make-temp-file buf-C buf-C-file-name)))
  435. (ediff-setup (get-buffer buf-A) file-A
  436. (get-buffer buf-B) file-B
  437. (if buf-C-is-alive (get-buffer buf-C))
  438. file-C
  439. (cons `(lambda ()
  440. (delete-file ,file-A)
  441. (delete-file ,file-B)
  442. (if (stringp ,file-C) (delete-file ,file-C)))
  443. startup-hooks)
  444. (list (cons 'ediff-job-name job-name))
  445. merge-buffer-file))
  446. (if (and (stringp file-A) (file-exists-p file-A))
  447. (delete-file file-A))
  448. (if (and (stringp file-B) (file-exists-p file-B))
  449. (delete-file file-B))
  450. (if (and (stringp file-C) (file-exists-p file-C))
  451. (delete-file file-C)))))
  452. ;;; Directory and file group operations
  453. ;; Get appropriate default name for directory:
  454. ;; If ediff-use-last-dir, use ediff-last-dir-A.
  455. ;; In dired mode, use the directory that is under the point (if any);
  456. ;; otherwise, use default-directory
  457. (defun ediff-get-default-directory-name ()
  458. (cond (ediff-use-last-dir ediff-last-dir-A)
  459. ((eq major-mode 'dired-mode)
  460. (let ((f (dired-get-filename nil 'noerror)))
  461. (if (and (stringp f) (file-directory-p f))
  462. f
  463. default-directory)))
  464. (t default-directory)))
  465. ;;;###autoload
  466. (defun ediff-directories (dir1 dir2 regexp)
  467. "Run Ediff on a pair of directories, DIR1 and DIR2, comparing files that have
  468. the same name in both. The third argument, REGEXP, is nil or a regular
  469. expression; only file names that match the regexp are considered."
  470. (interactive
  471. (let ((dir-A (ediff-get-default-directory-name))
  472. (default-regexp (eval ediff-default-filtering-regexp))
  473. f)
  474. (list (setq f (read-directory-name
  475. "Directory A to compare:" dir-A nil 'must-match))
  476. (read-directory-name "Directory B to compare:"
  477. (if ediff-use-last-dir
  478. ediff-last-dir-B
  479. (ediff-strip-last-dir f))
  480. nil 'must-match)
  481. (read-string
  482. (if (stringp default-regexp)
  483. (format "Filter through regular expression (default %s): "
  484. default-regexp)
  485. "Filter through regular expression: ")
  486. nil
  487. 'ediff-filtering-regexp-history
  488. (eval ediff-default-filtering-regexp))
  489. )))
  490. (ediff-directories-internal
  491. dir1 dir2 nil regexp 'ediff-files 'ediff-directories
  492. ))
  493. ;;;###autoload
  494. (defalias 'edirs 'ediff-directories)
  495. ;;;###autoload
  496. (defun ediff-directory-revisions (dir1 regexp)
  497. "Run Ediff on a directory, DIR1, comparing its files with their revisions.
  498. The second argument, REGEXP, is a regular expression that filters the file
  499. names. Only the files that are under revision control are taken into account."
  500. (interactive
  501. (let ((dir-A (ediff-get-default-directory-name))
  502. (default-regexp (eval ediff-default-filtering-regexp))
  503. )
  504. (list (read-directory-name
  505. "Directory to compare with revision:" dir-A nil 'must-match)
  506. (read-string
  507. (if (stringp default-regexp)
  508. (format "Filter through regular expression (default %s): "
  509. default-regexp)
  510. "Filter through regular expression: ")
  511. nil
  512. 'ediff-filtering-regexp-history
  513. (eval ediff-default-filtering-regexp))
  514. )))
  515. (ediff-directory-revisions-internal
  516. dir1 regexp 'ediff-revision 'ediff-directory-revisions
  517. ))
  518. ;;;###autoload
  519. (defalias 'edir-revisions 'ediff-directory-revisions)
  520. ;;;###autoload
  521. (defun ediff-directories3 (dir1 dir2 dir3 regexp)
  522. "Run Ediff on three directories, DIR1, DIR2, and DIR3, comparing files that
  523. have the same name in all three. The last argument, REGEXP, is nil or a
  524. regular expression; only file names that match the regexp are considered."
  525. (interactive
  526. (let ((dir-A (ediff-get-default-directory-name))
  527. (default-regexp (eval ediff-default-filtering-regexp))
  528. f)
  529. (list (setq f (read-directory-name "Directory A to compare:" dir-A nil))
  530. (setq f (read-directory-name "Directory B to compare:"
  531. (if ediff-use-last-dir
  532. ediff-last-dir-B
  533. (ediff-strip-last-dir f))
  534. nil 'must-match))
  535. (read-directory-name "Directory C to compare:"
  536. (if ediff-use-last-dir
  537. ediff-last-dir-C
  538. (ediff-strip-last-dir f))
  539. nil 'must-match)
  540. (read-string
  541. (if (stringp default-regexp)
  542. (format "Filter through regular expression (default %s): "
  543. default-regexp)
  544. "Filter through regular expression: ")
  545. nil
  546. 'ediff-filtering-regexp-history
  547. (eval ediff-default-filtering-regexp))
  548. )))
  549. (ediff-directories-internal
  550. dir1 dir2 dir3 regexp 'ediff-files3 'ediff-directories3
  551. ))
  552. ;;;###autoload
  553. (defalias 'edirs3 'ediff-directories3)
  554. ;;;###autoload
  555. (defun ediff-merge-directories (dir1 dir2 regexp &optional merge-autostore-dir)
  556. "Run Ediff on a pair of directories, DIR1 and DIR2, merging files that have
  557. the same name in both. The third argument, REGEXP, is nil or a regular
  558. expression; only file names that match the regexp are considered."
  559. (interactive
  560. (let ((dir-A (ediff-get-default-directory-name))
  561. (default-regexp (eval ediff-default-filtering-regexp))
  562. f)
  563. (list (setq f (read-directory-name "Directory A to merge:"
  564. dir-A nil 'must-match))
  565. (read-directory-name "Directory B to merge:"
  566. (if ediff-use-last-dir
  567. ediff-last-dir-B
  568. (ediff-strip-last-dir f))
  569. nil 'must-match)
  570. (read-string
  571. (if (stringp default-regexp)
  572. (format "Filter through regular expression (default %s): "
  573. default-regexp)
  574. "Filter through regular expression: ")
  575. nil
  576. 'ediff-filtering-regexp-history
  577. (eval ediff-default-filtering-regexp))
  578. )))
  579. (ediff-directories-internal
  580. dir1 dir2 nil regexp 'ediff-merge-files 'ediff-merge-directories
  581. nil merge-autostore-dir
  582. ))
  583. ;;;###autoload
  584. (defalias 'edirs-merge 'ediff-merge-directories)
  585. ;;;###autoload
  586. (defun ediff-merge-directories-with-ancestor (dir1 dir2 ancestor-dir regexp
  587. &optional
  588. merge-autostore-dir)
  589. "Merge files in directories DIR1 and DIR2 using files in ANCESTOR-DIR as ancestors.
  590. Ediff merges files that have identical names in DIR1, DIR2. If a pair of files
  591. in DIR1 and DIR2 doesn't have an ancestor in ANCESTOR-DIR, Ediff will merge
  592. without ancestor. The fourth argument, REGEXP, is nil or a regular expression;
  593. only file names that match the regexp are considered."
  594. (interactive
  595. (let ((dir-A (ediff-get-default-directory-name))
  596. (default-regexp (eval ediff-default-filtering-regexp))
  597. f)
  598. (list (setq f (read-directory-name "Directory A to merge:" dir-A nil))
  599. (setq f (read-directory-name "Directory B to merge:"
  600. (if ediff-use-last-dir
  601. ediff-last-dir-B
  602. (ediff-strip-last-dir f))
  603. nil 'must-match))
  604. (read-directory-name "Ancestor directory:"
  605. (if ediff-use-last-dir
  606. ediff-last-dir-C
  607. (ediff-strip-last-dir f))
  608. nil 'must-match)
  609. (read-string
  610. (if (stringp default-regexp)
  611. (format "Filter through regular expression (default %s): "
  612. default-regexp)
  613. "Filter through regular expression: ")
  614. nil
  615. 'ediff-filtering-regexp-history
  616. (eval ediff-default-filtering-regexp))
  617. )))
  618. (ediff-directories-internal
  619. dir1 dir2 ancestor-dir regexp
  620. 'ediff-merge-files-with-ancestor 'ediff-merge-directories-with-ancestor
  621. nil merge-autostore-dir
  622. ))
  623. ;;;###autoload
  624. (defun ediff-merge-directory-revisions (dir1 regexp
  625. &optional merge-autostore-dir)
  626. "Run Ediff on a directory, DIR1, merging its files with their revisions.
  627. The second argument, REGEXP, is a regular expression that filters the file
  628. names. Only the files that are under revision control are taken into account."
  629. (interactive
  630. (let ((dir-A (ediff-get-default-directory-name))
  631. (default-regexp (eval ediff-default-filtering-regexp))
  632. )
  633. (list (read-directory-name
  634. "Directory to merge with revisions:" dir-A nil 'must-match)
  635. (read-string
  636. (if (stringp default-regexp)
  637. (format "Filter through regular expression (default %s): "
  638. default-regexp)
  639. "Filter through regular expression: ")
  640. nil
  641. 'ediff-filtering-regexp-history
  642. (eval ediff-default-filtering-regexp))
  643. )))
  644. (ediff-directory-revisions-internal
  645. dir1 regexp 'ediff-merge-revisions 'ediff-merge-directory-revisions
  646. nil merge-autostore-dir
  647. ))
  648. ;;;###autoload
  649. (defalias 'edir-merge-revisions 'ediff-merge-directory-revisions)
  650. ;;;###autoload
  651. (defun ediff-merge-directory-revisions-with-ancestor (dir1 regexp
  652. &optional
  653. merge-autostore-dir)
  654. "Run Ediff on a directory, DIR1, merging its files with their revisions and ancestors.
  655. The second argument, REGEXP, is a regular expression that filters the file
  656. names. Only the files that are under revision control are taken into account."
  657. (interactive
  658. (let ((dir-A (ediff-get-default-directory-name))
  659. (default-regexp (eval ediff-default-filtering-regexp))
  660. )
  661. (list (read-directory-name
  662. "Directory to merge with revisions and ancestors:"
  663. dir-A nil 'must-match)
  664. (read-string
  665. (if (stringp default-regexp)
  666. (format "Filter through regular expression (default %s): "
  667. default-regexp)
  668. "Filter through regular expression: ")
  669. nil
  670. 'ediff-filtering-regexp-history
  671. (eval ediff-default-filtering-regexp))
  672. )))
  673. (ediff-directory-revisions-internal
  674. dir1 regexp 'ediff-merge-revisions-with-ancestor
  675. 'ediff-merge-directory-revisions-with-ancestor
  676. nil merge-autostore-dir
  677. ))
  678. ;;;###autoload
  679. (defalias
  680. 'edir-merge-revisions-with-ancestor
  681. 'ediff-merge-directory-revisions-with-ancestor)
  682. ;;;###autoload
  683. (defalias 'edirs-merge-with-ancestor 'ediff-merge-directories-with-ancestor)
  684. ;; Run ediff-action (ediff-files, ediff-merge, ediff-merge-with-ancestors)
  685. ;; on a pair of directories (three directories, in case of ancestor).
  686. ;; The third argument, REGEXP, is nil or a regular expression;
  687. ;; only file names that match the regexp are considered.
  688. ;; JOBNAME is the symbol indicating the meta-job to be performed.
  689. ;; MERGE-AUTOSTORE-DIR is the directory in which to store merged files.
  690. (defun ediff-directories-internal (dir1 dir2 dir3 regexp action jobname
  691. &optional startup-hooks
  692. merge-autostore-dir)
  693. (if (stringp dir3)
  694. (setq dir3 (if (file-directory-p dir3) dir3 (file-name-directory dir3))))
  695. (cond ((string= dir1 dir2)
  696. (error "Directories A and B are the same: %s" dir1))
  697. ((and (eq jobname 'ediff-directories3)
  698. (string= dir1 dir3))
  699. (error "Directories A and C are the same: %s" dir1))
  700. ((and (eq jobname 'ediff-directories3)
  701. (string= dir2 dir3))
  702. (error "Directories B and C are the same: %s" dir1)))
  703. (if merge-autostore-dir
  704. (or (stringp merge-autostore-dir)
  705. (error "%s: Directory for storing merged files must be a string"
  706. jobname)))
  707. (let (;; dir-diff-struct is of the form (common-list diff-list)
  708. ;; It is a structure where ediff-intersect-directories returns
  709. ;; commonalities and differences among directories
  710. dir-diff-struct
  711. meta-buf)
  712. (if (and ediff-autostore-merges
  713. (ediff-merge-metajob jobname)
  714. (not merge-autostore-dir))
  715. (setq merge-autostore-dir
  716. (read-directory-name "Save merged files in directory: "
  717. (if ediff-use-last-dir
  718. ediff-last-merge-autostore-dir
  719. (ediff-strip-last-dir dir1))
  720. nil
  721. 'must-match)))
  722. ;; verify we are not merging into an orig directory
  723. (if merge-autostore-dir
  724. (cond ((and (stringp dir1) (string= merge-autostore-dir dir1))
  725. (or (y-or-n-p
  726. "Directory for saving merged files = Directory A. Sure? ")
  727. (error "Directory merge aborted")))
  728. ((and (stringp dir2) (string= merge-autostore-dir dir2))
  729. (or (y-or-n-p
  730. "Directory for saving merged files = Directory B. Sure? ")
  731. (error "Directory merge aborted")))
  732. ((and (stringp dir3) (string= merge-autostore-dir dir3))
  733. (or (y-or-n-p
  734. "Directory for saving merged files = Ancestor Directory. Sure? ")
  735. (error "Directory merge aborted")))))
  736. (setq dir-diff-struct (ediff-intersect-directories
  737. jobname
  738. regexp dir1 dir2 dir3 merge-autostore-dir))
  739. (setq startup-hooks
  740. ;; this sets various vars in the meta buffer inside
  741. ;; ediff-prepare-meta-buffer
  742. (cons `(lambda ()
  743. ;; tell what to do if the user clicks on a session record
  744. (setq ediff-session-action-function (quote ,action))
  745. ;; set ediff-dir-difference-list
  746. (setq ediff-dir-difference-list
  747. (cdr (quote ,dir-diff-struct))))
  748. startup-hooks))
  749. (setq meta-buf (ediff-prepare-meta-buffer
  750. 'ediff-filegroup-action
  751. (car dir-diff-struct)
  752. "*Ediff Session Group Panel"
  753. 'ediff-redraw-directory-group-buffer
  754. jobname
  755. startup-hooks))
  756. (ediff-show-meta-buffer meta-buf)
  757. ))
  758. ;; MERGE-AUTOSTORE-DIR can be given to tell ediff where to store the merged
  759. ;; files
  760. (defun ediff-directory-revisions-internal (dir1 regexp action jobname
  761. &optional startup-hooks
  762. merge-autostore-dir)
  763. (setq dir1 (if (file-directory-p dir1) dir1 (file-name-directory dir1)))
  764. (if merge-autostore-dir
  765. (or (stringp merge-autostore-dir)
  766. (error "%S: Directory for storing merged files must be a string"
  767. jobname)))
  768. (let (file-list meta-buf)
  769. (if (and ediff-autostore-merges
  770. (ediff-merge-metajob jobname)
  771. (not merge-autostore-dir))
  772. (setq merge-autostore-dir
  773. (read-directory-name "Save merged files in directory: "
  774. (if ediff-use-last-dir
  775. ediff-last-merge-autostore-dir
  776. (ediff-strip-last-dir dir1))
  777. nil
  778. 'must-match)))
  779. ;; verify merge-autostore-dir != dir1
  780. (if (and merge-autostore-dir
  781. (stringp dir1)
  782. (string= merge-autostore-dir dir1))
  783. (or (y-or-n-p
  784. "Directory for saving merged file = directory A. Sure? ")
  785. (error "Merge of directory revisions aborted")))
  786. (setq file-list
  787. (ediff-get-directory-files-under-revision
  788. jobname regexp dir1 merge-autostore-dir))
  789. (setq startup-hooks
  790. ;; this sets various vars in the meta buffer inside
  791. ;; ediff-prepare-meta-buffer
  792. (cons `(lambda ()
  793. ;; tell what to do if the user clicks on a session record
  794. (setq ediff-session-action-function (quote ,action)))
  795. startup-hooks))
  796. (setq meta-buf (ediff-prepare-meta-buffer
  797. 'ediff-filegroup-action
  798. file-list
  799. "*Ediff Session Group Panel"
  800. 'ediff-redraw-directory-group-buffer
  801. jobname
  802. startup-hooks))
  803. (ediff-show-meta-buffer meta-buf)
  804. ))
  805. ;;; Compare regions and windows
  806. ;;;###autoload
  807. (defun ediff-windows-wordwise (dumb-mode &optional wind-A wind-B startup-hooks)
  808. "Compare WIND-A and WIND-B, which are selected by clicking, wordwise.
  809. With prefix argument, DUMB-MODE, or on a non-windowing display, works as
  810. follows:
  811. If WIND-A is nil, use selected window.
  812. If WIND-B is nil, use window next to WIND-A."
  813. (interactive "P")
  814. (ediff-windows dumb-mode wind-A wind-B
  815. startup-hooks 'ediff-windows-wordwise 'word-mode))
  816. ;;;###autoload
  817. (defun ediff-windows-linewise (dumb-mode &optional wind-A wind-B startup-hooks)
  818. "Compare WIND-A and WIND-B, which are selected by clicking, linewise.
  819. With prefix argument, DUMB-MODE, or on a non-windowing display, works as
  820. follows:
  821. If WIND-A is nil, use selected window.
  822. If WIND-B is nil, use window next to WIND-A."
  823. (interactive "P")
  824. (ediff-windows dumb-mode wind-A wind-B
  825. startup-hooks 'ediff-windows-linewise nil))
  826. ;; Compare WIND-A and WIND-B, which are selected by clicking.
  827. ;; With prefix argument, DUMB-MODE, or on a non-windowing display,
  828. ;; works as follows:
  829. ;; If WIND-A is nil, use selected window.
  830. ;; If WIND-B is nil, use window next to WIND-A.
  831. (defun ediff-windows (dumb-mode wind-A wind-B startup-hooks job-name word-mode)
  832. (if (or dumb-mode (not (ediff-window-display-p)))
  833. (setq wind-A (ediff-get-next-window wind-A nil)
  834. wind-B (ediff-get-next-window wind-B wind-A))
  835. (setq wind-A (ediff-get-window-by-clicking wind-A nil 1)
  836. wind-B (ediff-get-window-by-clicking wind-B wind-A 2)))
  837. (let ((buffer-A (window-buffer wind-A))
  838. (buffer-B (window-buffer wind-B))
  839. beg-A end-A beg-B end-B)
  840. (save-excursion
  841. (save-window-excursion
  842. (sit-for 0) ; sync before using window-start/end -- a precaution
  843. (select-window wind-A)
  844. (setq beg-A (window-start)
  845. end-A (window-end))
  846. (select-window wind-B)
  847. (setq beg-B (window-start)
  848. end-B (window-end))))
  849. (setq buffer-A
  850. (ediff-clone-buffer-for-window-comparison
  851. buffer-A wind-A "-Window.A-")
  852. buffer-B
  853. (ediff-clone-buffer-for-window-comparison
  854. buffer-B wind-B "-Window.B-"))
  855. (ediff-regions-internal
  856. buffer-A beg-A end-A buffer-B beg-B end-B
  857. startup-hooks job-name word-mode nil)))
  858. ;;;###autoload
  859. (defun ediff-regions-wordwise (buffer-A buffer-B &optional startup-hooks)
  860. "Run Ediff on a pair of regions in specified buffers.
  861. Regions \(i.e., point and mark\) can be set in advance or marked interactively.
  862. This function is effective only for relatively small regions, up to 200
  863. lines. For large regions, use `ediff-regions-linewise'."
  864. (interactive
  865. (let (bf)
  866. (list (setq bf (read-buffer "Region's A buffer: "
  867. (ediff-other-buffer "") t))
  868. (read-buffer "Region's B buffer: "
  869. (progn
  870. ;; realign buffers so that two visible bufs will be
  871. ;; at the top
  872. (save-window-excursion (other-window 1))
  873. (ediff-other-buffer bf))
  874. t))))
  875. (if (not (ediff-buffer-live-p buffer-A))
  876. (error "Buffer %S doesn't exist" buffer-A))
  877. (if (not (ediff-buffer-live-p buffer-B))
  878. (error "Buffer %S doesn't exist" buffer-B))
  879. (let ((buffer-A
  880. (ediff-clone-buffer-for-region-comparison buffer-A "-Region.A-"))
  881. (buffer-B
  882. (ediff-clone-buffer-for-region-comparison buffer-B "-Region.B-"))
  883. reg-A-beg reg-A-end reg-B-beg reg-B-end)
  884. (with-current-buffer buffer-A
  885. (setq reg-A-beg (region-beginning)
  886. reg-A-end (region-end))
  887. (set-buffer buffer-B)
  888. (setq reg-B-beg (region-beginning)
  889. reg-B-end (region-end)))
  890. (ediff-regions-internal
  891. (get-buffer buffer-A) reg-A-beg reg-A-end
  892. (get-buffer buffer-B) reg-B-beg reg-B-end
  893. startup-hooks 'ediff-regions-wordwise 'word-mode nil)))
  894. ;;;###autoload
  895. (defun ediff-regions-linewise (buffer-A buffer-B &optional startup-hooks)
  896. "Run Ediff on a pair of regions in specified buffers.
  897. Regions \(i.e., point and mark\) can be set in advance or marked interactively.
  898. Each region is enlarged to contain full lines.
  899. This function is effective for large regions, over 100-200
  900. lines. For small regions, use `ediff-regions-wordwise'."
  901. (interactive
  902. (let (bf)
  903. (list (setq bf (read-buffer "Region A's buffer: "
  904. (ediff-other-buffer "") t))
  905. (read-buffer "Region B's buffer: "
  906. (progn
  907. ;; realign buffers so that two visible bufs will be
  908. ;; at the top
  909. (save-window-excursion (other-window 1))
  910. (ediff-other-buffer bf))
  911. t))))
  912. (if (not (ediff-buffer-live-p buffer-A))
  913. (error "Buffer %S doesn't exist" buffer-A))
  914. (if (not (ediff-buffer-live-p buffer-B))
  915. (error "Buffer %S doesn't exist" buffer-B))
  916. (let ((buffer-A
  917. (ediff-clone-buffer-for-region-comparison buffer-A "-Region.A-"))
  918. (buffer-B
  919. (ediff-clone-buffer-for-region-comparison buffer-B "-Region.B-"))
  920. reg-A-beg reg-A-end reg-B-beg reg-B-end)
  921. (with-current-buffer buffer-A
  922. (setq reg-A-beg (region-beginning)
  923. reg-A-end (region-end))
  924. ;; enlarge the region to hold full lines
  925. (goto-char reg-A-beg)
  926. (beginning-of-line)
  927. (setq reg-A-beg (point))
  928. (goto-char reg-A-end)
  929. (end-of-line)
  930. (or (eobp) (forward-char)) ; include the newline char
  931. (setq reg-A-end (point))
  932. (set-buffer buffer-B)
  933. (setq reg-B-beg (region-beginning)
  934. reg-B-end (region-end))
  935. ;; enlarge the region to hold full lines
  936. (goto-char reg-B-beg)
  937. (beginning-of-line)
  938. (setq reg-B-beg (point))
  939. (goto-char reg-B-end)
  940. (end-of-line)
  941. (or (eobp) (forward-char)) ; include the newline char
  942. (setq reg-B-end (point))
  943. ) ; save excursion
  944. (ediff-regions-internal
  945. (get-buffer buffer-A) reg-A-beg reg-A-end
  946. (get-buffer buffer-B) reg-B-beg reg-B-end
  947. startup-hooks 'ediff-regions-linewise nil nil))) ; no word mode
  948. ;; compare region beg-A to end-A of buffer-A
  949. ;; to regions beg-B -- end-B in buffer-B.
  950. (defun ediff-regions-internal (buffer-A beg-A end-A buffer-B beg-B end-B
  951. startup-hooks job-name word-mode
  952. setup-parameters)
  953. (let ((tmp-buffer (get-buffer-create ediff-tmp-buffer))
  954. overl-A overl-B
  955. file-A file-B)
  956. (unwind-protect
  957. (progn
  958. ;; in case beg/end-A/B aren't markers--make them into markers
  959. (ediff-with-current-buffer buffer-A
  960. (setq beg-A (move-marker (make-marker) beg-A)
  961. end-A (move-marker (make-marker) end-A)))
  962. (ediff-with-current-buffer buffer-B
  963. (setq beg-B (move-marker (make-marker) beg-B)
  964. end-B (move-marker (make-marker) end-B)))
  965. ;; make file-A
  966. (if word-mode
  967. (ediff-wordify beg-A end-A buffer-A tmp-buffer)
  968. (ediff-copy-to-buffer beg-A end-A buffer-A tmp-buffer))
  969. (setq file-A (ediff-make-temp-file tmp-buffer "regA"))
  970. ;; make file-B
  971. (if word-mode
  972. (ediff-wordify beg-B end-B buffer-B tmp-buffer)
  973. (ediff-copy-to-buffer beg-B end-B buffer-B tmp-buffer))
  974. (setq file-B (ediff-make-temp-file tmp-buffer "regB"))
  975. (setq overl-A (ediff-make-bullet-proof-overlay beg-A end-A buffer-A))
  976. (setq overl-B (ediff-make-bullet-proof-overlay beg-B end-B buffer-B))
  977. (ediff-setup buffer-A file-A
  978. buffer-B file-B
  979. nil nil ; buffer & file C
  980. (cons `(lambda ()
  981. (delete-file ,file-A)
  982. (delete-file ,file-B))
  983. startup-hooks)
  984. (append
  985. (list (cons 'ediff-word-mode word-mode)
  986. (cons 'ediff-narrow-bounds (list overl-A overl-B))
  987. (cons 'ediff-job-name job-name))
  988. setup-parameters)))
  989. (if (and (stringp file-A) (file-exists-p file-A))
  990. (delete-file file-A))
  991. (if (and (stringp file-B) (file-exists-p file-B))
  992. (delete-file file-B)))
  993. ))
  994. ;;; Merge files and buffers
  995. ;;;###autoload
  996. (defalias 'ediff-merge 'ediff-merge-files)
  997. (defsubst ediff-merge-on-startup ()
  998. (ediff-do-merge 0)
  999. ;; Can't remember why this is here, but it may cause the automatically merged
  1000. ;; buffer to be lost. So, keep the buffer modified.
  1001. ;;(ediff-with-current-buffer ediff-buffer-C
  1002. ;; (set-buffer-modified-p nil))
  1003. )
  1004. ;;;###autoload
  1005. (defun ediff-merge-files (file-A file-B
  1006. ;; MERGE-BUFFER-FILE is the file to be
  1007. ;; associated with the merge buffer
  1008. &optional startup-hooks merge-buffer-file)
  1009. "Merge two files without ancestor."
  1010. (interactive
  1011. (let ((dir-A (if ediff-use-last-dir
  1012. ediff-last-dir-A
  1013. default-directory))
  1014. dir-B f)
  1015. (list (setq f (ediff-read-file-name
  1016. "File A to merge"
  1017. dir-A
  1018. (ediff-get-default-file-name)
  1019. 'no-dirs))
  1020. (ediff-read-file-name "File B to merge"
  1021. (setq dir-B
  1022. (if ediff-use-last-dir
  1023. ediff-last-dir-B
  1024. (file-name-directory f)))
  1025. (progn
  1026. (ediff-add-to-history
  1027. 'file-name-history
  1028. (ediff-abbreviate-file-name
  1029. (expand-file-name
  1030. (file-name-nondirectory f)
  1031. dir-B)))
  1032. (ediff-get-default-file-name f 1)))
  1033. )))
  1034. (setq startup-hooks (cons 'ediff-merge-on-startup startup-hooks))
  1035. (ediff-files-internal file-A
  1036. (if (file-directory-p file-B)
  1037. (expand-file-name
  1038. (file-name-nondirectory file-A) file-B)
  1039. file-B)
  1040. nil ; file-C
  1041. startup-hooks
  1042. 'ediff-merge-files
  1043. merge-buffer-file))
  1044. ;;;###autoload
  1045. (defun ediff-merge-files-with-ancestor (file-A file-B file-ancestor
  1046. &optional
  1047. startup-hooks
  1048. ;; MERGE-BUFFER-FILE is the file
  1049. ;; to be associated with the
  1050. ;; merge buffer
  1051. merge-buffer-file)
  1052. "Merge two files with ancestor."
  1053. (interactive
  1054. (let ((dir-A (if ediff-use-last-dir
  1055. ediff-last-dir-A
  1056. default-directory))
  1057. dir-B dir-ancestor f ff)
  1058. (list (setq f (ediff-read-file-name
  1059. "File A to merge"
  1060. dir-A
  1061. (ediff-get-default-file-name)
  1062. 'no-dirs))
  1063. (setq ff (ediff-read-file-name "File B to merge"
  1064. (setq dir-B
  1065. (if ediff-use-last-dir
  1066. ediff-last-dir-B
  1067. (file-name-directory f)))
  1068. (progn
  1069. (ediff-add-to-history
  1070. 'file-name-history
  1071. (ediff-abbreviate-file-name
  1072. (expand-file-name
  1073. (file-name-nondirectory f)
  1074. dir-B)))
  1075. (ediff-get-default-file-name f 1))))
  1076. (ediff-read-file-name "Ancestor file"
  1077. (setq dir-ancestor
  1078. (if ediff-use-last-dir
  1079. ediff-last-dir-ancestor
  1080. (file-name-directory ff)))
  1081. (progn
  1082. (ediff-add-to-history
  1083. 'file-name-history
  1084. (ediff-abbreviate-file-name
  1085. (expand-file-name
  1086. (file-name-nondirectory ff)
  1087. dir-ancestor)))
  1088. (ediff-get-default-file-name ff 2)))
  1089. )))
  1090. (setq startup-hooks (cons 'ediff-merge-on-startup startup-hooks))
  1091. (ediff-files-internal file-A
  1092. (if (file-directory-p file-B)
  1093. (expand-file-name
  1094. (file-name-nondirectory file-A) file-B)
  1095. file-B)
  1096. file-ancestor
  1097. startup-hooks
  1098. 'ediff-merge-files-with-ancestor
  1099. merge-buffer-file))
  1100. ;;;###autoload
  1101. (defalias 'ediff-merge-with-ancestor 'ediff-merge-files-with-ancestor)
  1102. ;;;###autoload
  1103. (defun ediff-merge-buffers (buffer-A buffer-B
  1104. &optional
  1105. ;; MERGE-BUFFER-FILE is the file to be
  1106. ;; associated with the merge buffer
  1107. startup-hooks job-name merge-buffer-file)
  1108. "Merge buffers without ancestor."
  1109. (interactive
  1110. (let (bf)
  1111. (list (setq bf (read-buffer "Buffer A to merge: "
  1112. (ediff-other-buffer "") t))
  1113. (read-buffer "Buffer B to merge: "
  1114. (progn
  1115. ;; realign buffers so that two visible bufs will be
  1116. ;; at the top
  1117. (save-window-excursion (other-window 1))
  1118. (ediff-other-buffer bf))
  1119. t))))
  1120. (setq startup-hooks (cons 'ediff-merge-on-startup startup-hooks))
  1121. (or job-name (setq job-name 'ediff-merge-buffers))
  1122. (ediff-buffers-internal
  1123. buffer-A buffer-B nil startup-hooks job-name merge-buffer-file))
  1124. ;;;###autoload
  1125. (defun ediff-merge-buffers-with-ancestor (buffer-A buffer-B buffer-ancestor
  1126. &optional
  1127. startup-hooks
  1128. job-name
  1129. ;; MERGE-BUFFER-FILE is the
  1130. ;; file to be associated
  1131. ;; with the merge buffer
  1132. merge-buffer-file)
  1133. "Merge buffers with ancestor."
  1134. (interactive
  1135. (let (bf bff)
  1136. (list (setq bf (read-buffer "Buffer A to merge: "
  1137. (ediff-other-buffer "") t))
  1138. (setq bff (read-buffer "Buffer B to merge: "
  1139. (progn
  1140. ;; realign buffers so that two visible
  1141. ;; bufs will be at the top
  1142. (save-window-excursion (other-window 1))
  1143. (ediff-other-buffer bf))
  1144. t))
  1145. (read-buffer "Ancestor buffer: "
  1146. (progn
  1147. ;; realign buffers so that three visible
  1148. ;; bufs will be at the top
  1149. (save-window-excursion (other-window 1))
  1150. (ediff-other-buffer (list bf bff)))
  1151. t)
  1152. )))
  1153. (setq startup-hooks (cons 'ediff-merge-on-startup startup-hooks))
  1154. (or job-name (setq job-name 'ediff-merge-buffers-with-ancestor))
  1155. (ediff-buffers-internal
  1156. buffer-A buffer-B buffer-ancestor startup-hooks job-name merge-buffer-file))
  1157. ;;;###autoload
  1158. (defun ediff-merge-revisions (&optional file startup-hooks merge-buffer-file)
  1159. ;; MERGE-BUFFER-FILE is the file to be associated with the merge buffer
  1160. "Run Ediff by merging two revisions of a file.
  1161. The file is the optional FILE argument or the file visited by the current
  1162. buffer."
  1163. (interactive)
  1164. (if (stringp file) (find-file file))
  1165. (let (rev1 rev2)
  1166. (setq rev1
  1167. (read-string
  1168. (format
  1169. "Version 1 to merge (default %s's working version): "
  1170. (if (stringp file)
  1171. (file-name-nondirectory file) "current buffer")))
  1172. rev2
  1173. (read-string
  1174. (format
  1175. "Version 2 to merge (default %s): "
  1176. (if (stringp file)
  1177. (file-name-nondirectory file) "current buffer"))))
  1178. (ediff-load-version-control)
  1179. ;; ancestor-revision=nil
  1180. (funcall
  1181. (intern (format "ediff-%S-merge-internal" ediff-version-control-package))
  1182. rev1 rev2 nil startup-hooks merge-buffer-file)))
  1183. ;;;###autoload
  1184. (defun ediff-merge-revisions-with-ancestor (&optional
  1185. file startup-hooks
  1186. ;; MERGE-BUFFER-FILE is the file to
  1187. ;; be associated with the merge
  1188. ;; buffer
  1189. merge-buffer-file)
  1190. "Run Ediff by merging two revisions of a file with a common ancestor.
  1191. The file is the optional FILE argument or the file visited by the current
  1192. buffer."
  1193. (interactive)
  1194. (if (stringp file) (find-file file))
  1195. (let (rev1 rev2 ancestor-rev)
  1196. (setq rev1
  1197. (read-string
  1198. (format
  1199. "Version 1 to merge (default %s's working version): "
  1200. (if (stringp file)
  1201. (file-name-nondirectory file) "current buffer")))
  1202. rev2
  1203. (read-string
  1204. (format
  1205. "Version 2 to merge (default %s): "
  1206. (if (stringp file)
  1207. (file-name-nondirectory file) "current buffer")))
  1208. ancestor-rev
  1209. (read-string
  1210. (format
  1211. "Ancestor version (default %s's base revision): "
  1212. (if (stringp file)
  1213. (file-name-nondirectory file) "current buffer"))))
  1214. (ediff-load-version-control)
  1215. (funcall
  1216. (intern (format "ediff-%S-merge-internal" ediff-version-control-package))
  1217. rev1 rev2 ancestor-rev startup-hooks merge-buffer-file)))
  1218. ;;; Apply patch
  1219. ;;;###autoload
  1220. (defun ediff-patch-file (&optional arg patch-buf)
  1221. "Query for a file name, and then run Ediff by patching that file.
  1222. If optional PATCH-BUF is given, use the patch in that buffer
  1223. and don't ask the user.
  1224. If prefix argument, then: if even argument, assume that the patch is in a
  1225. buffer. If odd -- assume it is in a file."
  1226. (interactive "P")
  1227. (let (source-dir source-file)
  1228. (require 'ediff-ptch)
  1229. (setq patch-buf
  1230. (ediff-get-patch-buffer
  1231. (if arg (prefix-numeric-value arg)) patch-buf))
  1232. (setq source-dir (cond (ediff-use-last-dir ediff-last-dir-patch)
  1233. ((and (not ediff-patch-default-directory)
  1234. (buffer-file-name patch-buf))
  1235. (file-name-directory
  1236. (expand-file-name
  1237. (buffer-file-name patch-buf))))
  1238. (t default-directory)))
  1239. (setq source-file
  1240. (read-file-name
  1241. "File to patch (directory, if multifile patch): "
  1242. ;; use an explicit initial file
  1243. source-dir nil nil (ediff-get-default-file-name)))
  1244. (ediff-dispatch-file-patching-job patch-buf source-file)))
  1245. ;;;###autoload
  1246. (defun ediff-patch-buffer (&optional arg patch-buf)
  1247. "Run Ediff by patching the buffer specified at prompt.
  1248. Without the optional prefix ARG, asks if the patch is in some buffer and
  1249. prompts for the buffer or a file, depending on the answer.
  1250. With ARG=1, assumes the patch is in a file and prompts for the file.
  1251. With ARG=2, assumes the patch is in a buffer and prompts for the buffer.
  1252. PATCH-BUF is an optional argument, which specifies the buffer that contains the
  1253. patch. If not given, the user is prompted according to the prefix argument."
  1254. (interactive "P")
  1255. (require 'ediff-ptch)
  1256. (setq patch-buf
  1257. (ediff-get-patch-buffer
  1258. (if arg (prefix-numeric-value arg)) patch-buf))
  1259. (ediff-patch-buffer-internal
  1260. patch-buf
  1261. (read-buffer
  1262. "Which buffer to patch? "
  1263. (ediff-other-buffer patch-buf))))
  1264. ;;;###autoload
  1265. (defalias 'epatch 'ediff-patch-file)
  1266. ;;;###autoload
  1267. (defalias 'epatch-buffer 'ediff-patch-buffer)
  1268. ;;; Versions Control functions
  1269. ;;;###autoload
  1270. (defun ediff-revision (&optional file startup-hooks)
  1271. "Run Ediff by comparing versions of a file.
  1272. The file is an optional FILE argument or the file entered at the prompt.
  1273. Default: the file visited by the current buffer.
  1274. Uses `vc.el' or `rcs.el' depending on `ediff-version-control-package'."
  1275. ;; if buffer is non-nil, use that buffer instead of the current buffer
  1276. (interactive "P")
  1277. (if (not (stringp file))
  1278. (setq file
  1279. (ediff-read-file-name "Compare revisions for file"
  1280. (if ediff-use-last-dir
  1281. ediff-last-dir-A
  1282. default-directory)
  1283. (ediff-get-default-file-name)
  1284. 'no-dirs)))
  1285. (find-file file)
  1286. (if (and (buffer-modified-p)
  1287. (y-or-n-p (format "Buffer %s is modified. Save buffer? "
  1288. (buffer-name))))
  1289. (save-buffer (current-buffer)))
  1290. (let (rev1 rev2)
  1291. (setq rev1
  1292. (read-string
  1293. (format "Revision 1 to compare (default %s's latest revision): "
  1294. (file-name-nondirectory file)))
  1295. rev2
  1296. (read-string
  1297. (format "Revision 2 to compare (default %s's current state): "
  1298. (file-name-nondirectory file))))
  1299. (ediff-load-version-control)
  1300. (funcall
  1301. (intern (format "ediff-%S-internal" ediff-version-control-package))
  1302. rev1 rev2 startup-hooks)
  1303. ))
  1304. ;;;###autoload
  1305. (defalias 'erevision 'ediff-revision)
  1306. ;; Test if version control package is loaded and load if not
  1307. ;; Is SILENT is non-nil, don't report error if package is not found.
  1308. (defun ediff-load-version-control (&optional silent)
  1309. (require 'ediff-vers)
  1310. (or (featurep ediff-version-control-package)
  1311. (if (locate-library (symbol-name ediff-version-control-package))
  1312. (progn
  1313. (message "") ; kill the message from `locate-library'
  1314. (require ediff-version-control-package))
  1315. (or silent
  1316. (error "Version control package %S.el not found. Use vc.el instead"
  1317. ediff-version-control-package)))))
  1318. ;;;###autoload
  1319. (defun ediff-version ()
  1320. "Return string describing the version of Ediff.
  1321. When called interactively, displays the version."
  1322. (interactive)
  1323. ;; called-interactively-p - not in XEmacs
  1324. ;; (if (called-interactively-p 'interactive)
  1325. (if (interactive-p)
  1326. (message "%s" (ediff-version))
  1327. (format "Ediff %s of %s" ediff-version ediff-date)))
  1328. ;; info is run first, and will autoload info.el.
  1329. (declare-function Info-goto-node "info" (nodename &optional fork))
  1330. ;;;###autoload
  1331. (defun ediff-documentation (&optional node)
  1332. "Display Ediff's manual.
  1333. With optional NODE, goes to that node."
  1334. (interactive)
  1335. (let ((ctl-window ediff-control-window)
  1336. (ctl-buf ediff-control-buffer))
  1337. (ediff-skip-unsuitable-frames)
  1338. (condition-case nil
  1339. (progn
  1340. (pop-to-buffer (get-buffer-create "*info*"))
  1341. (info (if (featurep 'xemacs) "ediff.info" "ediff"))
  1342. (if node
  1343. (Info-goto-node node)
  1344. (message "Type `i' to search for a specific topic"))
  1345. (raise-frame (selected-frame)))
  1346. (error (beep 1)
  1347. (with-output-to-temp-buffer ediff-msg-buffer
  1348. (ediff-with-current-buffer standard-output
  1349. (fundamental-mode))
  1350. (princ ediff-BAD-INFO))
  1351. (if (window-live-p ctl-window)
  1352. (progn
  1353. (select-window ctl-window)
  1354. (set-window-buffer ctl-window ctl-buf)))))))
  1355. (dolist (mess '("^Errors in diff output. Diff output is in "
  1356. "^Hmm... I don't see an Ediff command around here...$"
  1357. "^Undocumented command! Type `G' in Ediff Control Panel to drop a note to the Ediff maintainer$"
  1358. ": This command runs in Ediff Control Buffer only!$"
  1359. ": Invalid op in ediff-check-version$"
  1360. "^ediff-shrink-window-C can be used only for merging jobs$"
  1361. "^Lost difference info on these directories$"
  1362. "^This command is inapplicable in the present context$"
  1363. "^This session group has no parent$"
  1364. "^Can't hide active session, $"
  1365. "^Ediff: something wrong--no multiple diffs buffer$"
  1366. "^Can't make context diff for Session $"
  1367. "^The patch buffer wasn't found$"
  1368. "^Aborted$"
  1369. "^This Ediff session is not part of a session group$"
  1370. "^No active Ediff sessions or corrupted session registry$"
  1371. "^No session info in this line$"
  1372. "^`.*' is not an ordinary file$"
  1373. "^Patch appears to have failed$"
  1374. "^Recomputation of differences cancelled$"
  1375. "^No fine differences in this mode$"
  1376. "^Lost connection to ancestor buffer...sorry$"
  1377. "^Not merging with ancestor$"
  1378. "^Don't know how to toggle read-only in buffer "
  1379. "Emacs is not running as a window application$"
  1380. "^This command makes sense only when merging with an ancestor$"
  1381. "^At end of the difference list$"
  1382. "^At beginning of the difference list$"
  1383. "^Nothing saved for diff .* in buffer "
  1384. "^Buffer is out of sync for file "
  1385. "^Buffer out of sync for file "
  1386. "^Output from `diff' not found$"
  1387. "^You forgot to specify a region in buffer "
  1388. "^All right. Make up your mind and come back...$"
  1389. "^Current buffer is not visiting any file$"
  1390. "^Failed to retrieve revision: $"
  1391. "^Can't determine display width.$"
  1392. "^File `.*' does not exist or is not readable$"
  1393. "^File `.*' is a directory$"
  1394. "^Buffer .* doesn't exist$"
  1395. "^Directories . and . are the same: "
  1396. "^Directory merge aborted$"
  1397. "^Merge of directory revisions aborted$"
  1398. "^Buffer .* doesn't exist$"
  1399. "^There is no file to merge$"
  1400. "^Version control package .*.el not found. Use vc.el instead$"))
  1401. (add-to-list 'debug-ignored-errors mess))
  1402. (require 'ediff-util)
  1403. (run-hooks 'ediff-load-hook)
  1404. ;; Local Variables:
  1405. ;; eval: (put 'ediff-defvar-local 'lisp-indent-hook 'defun)
  1406. ;; eval: (put 'ediff-with-current-buffer 'lisp-indent-hook 1)
  1407. ;; eval: (put 'ediff-with-current-buffer 'edebug-form-spec '(form body))
  1408. ;; End:
  1409. ;;; ediff.el ends here