ediff-util.el 150 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286
  1. ;;; ediff-util.el --- the core commands and utilities of ediff
  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-util)
  19. ;; Compiler pacifier
  20. (defvar ediff-use-toolbar-p)
  21. (defvar ediff-toolbar-height)
  22. (defvar ediff-toolbar)
  23. (defvar ediff-toolbar-3way)
  24. (defvar bottom-toolbar)
  25. (defvar bottom-toolbar-visible-p)
  26. (defvar bottom-toolbar-height)
  27. (defvar mark-active)
  28. (defvar ediff-after-quit-hook-internal nil)
  29. (eval-and-compile
  30. (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
  31. (eval-when-compile
  32. (require 'ediff))
  33. ;; end pacifier
  34. (require 'ediff-init)
  35. (require 'ediff-help)
  36. (require 'ediff-mult)
  37. (require 'ediff-wind)
  38. (require 'ediff-diff)
  39. (require 'ediff-merg)
  40. ;; for compatibility with current stable version of xemacs
  41. (if (featurep 'xemacs)
  42. (require 'ediff-tbar))
  43. ;;; Functions
  44. (defun ediff-mode ()
  45. "Ediff mode controls all operations in a single Ediff session.
  46. This mode is entered through one of the following commands:
  47. `ediff'
  48. `ediff-files'
  49. `ediff-buffers'
  50. `ebuffers'
  51. `ediff3'
  52. `ediff-files3'
  53. `ediff-buffers3'
  54. `ebuffers3'
  55. `ediff-merge'
  56. `ediff-merge-files'
  57. `ediff-merge-files-with-ancestor'
  58. `ediff-merge-buffers'
  59. `ediff-merge-buffers-with-ancestor'
  60. `ediff-merge-revisions'
  61. `ediff-merge-revisions-with-ancestor'
  62. `ediff-windows-wordwise'
  63. `ediff-windows-linewise'
  64. `ediff-regions-wordwise'
  65. `ediff-regions-linewise'
  66. `epatch'
  67. `ediff-patch-file'
  68. `ediff-patch-buffer'
  69. `epatch-buffer'
  70. `erevision'
  71. `ediff-revision'
  72. Commands:
  73. \\{ediff-mode-map}"
  74. ;; FIXME: Use define-derived-mode.
  75. (kill-all-local-variables)
  76. (setq major-mode 'ediff-mode)
  77. (setq mode-name "Ediff")
  78. ;; We use run-hooks instead of run-mode-hooks for two reasons.
  79. ;; The ediff control buffer is read-only and it is not supposed to be
  80. ;; modified by minor modes and such. So, run-mode-hooks doesn't do anything
  81. ;; useful here on top of what run-hooks does.
  82. ;; Second, changing run-hooks to run-mode-hooks would require an
  83. ;; if-statement, since XEmacs doesn't have this.
  84. (run-hooks 'ediff-mode-hook))
  85. ;;; Build keymaps
  86. (ediff-defvar-local ediff-mode-map nil
  87. "Local keymap used in Ediff mode.
  88. This is local to each Ediff Control Panel, so they may vary from invocation
  89. to invocation.")
  90. ;; Set up the keymap in the control buffer
  91. (defun ediff-set-keys ()
  92. "Set up Ediff keymap, if necessary."
  93. (if (null ediff-mode-map)
  94. (ediff-setup-keymap))
  95. (use-local-map ediff-mode-map))
  96. ;; Reload Ediff keymap. For debugging only.
  97. (defun ediff-reload-keymap ()
  98. (interactive)
  99. (setq ediff-mode-map nil)
  100. (ediff-set-keys))
  101. (defun ediff-setup-keymap ()
  102. "Set up the keymap used in the control buffer of Ediff."
  103. (setq ediff-mode-map (make-sparse-keymap))
  104. (suppress-keymap ediff-mode-map)
  105. (define-key ediff-mode-map
  106. (if (featurep 'emacs) [mouse-2] [button2]) 'ediff-help-for-quick-help)
  107. (define-key ediff-mode-map "\C-m" 'ediff-help-for-quick-help)
  108. (define-key ediff-mode-map "p" 'ediff-previous-difference)
  109. (define-key ediff-mode-map "\C-?" 'ediff-previous-difference)
  110. (define-key ediff-mode-map [delete] 'ediff-previous-difference)
  111. (define-key ediff-mode-map "\C-h" (if ediff-no-emacs-help-in-control-buffer
  112. 'ediff-previous-difference nil))
  113. ;; must come after C-h, or else C-h wipes out backspace's binding in XEmacs
  114. (define-key ediff-mode-map [backspace] 'ediff-previous-difference)
  115. (define-key ediff-mode-map "n" 'ediff-next-difference)
  116. (define-key ediff-mode-map " " 'ediff-next-difference)
  117. (define-key ediff-mode-map "j" 'ediff-jump-to-difference)
  118. (define-key ediff-mode-map "g" nil)
  119. (define-key ediff-mode-map "ga" 'ediff-jump-to-difference-at-point)
  120. (define-key ediff-mode-map "gb" 'ediff-jump-to-difference-at-point)
  121. (define-key ediff-mode-map "q" 'ediff-quit)
  122. (define-key ediff-mode-map "D" 'ediff-show-diff-output)
  123. (define-key ediff-mode-map "z" 'ediff-suspend)
  124. (define-key ediff-mode-map "\C-l" 'ediff-recenter)
  125. (define-key ediff-mode-map "|" 'ediff-toggle-split)
  126. (define-key ediff-mode-map "h" 'ediff-toggle-hilit)
  127. (or ediff-word-mode
  128. (define-key ediff-mode-map "@" 'ediff-toggle-autorefine))
  129. (if ediff-narrow-job
  130. (define-key ediff-mode-map "%" 'ediff-toggle-narrow-region))
  131. (define-key ediff-mode-map "~" 'ediff-swap-buffers)
  132. (define-key ediff-mode-map "v" 'ediff-scroll-vertically)
  133. (define-key ediff-mode-map "\C-v" 'ediff-scroll-vertically)
  134. (define-key ediff-mode-map "^" 'ediff-scroll-vertically)
  135. (define-key ediff-mode-map "\M-v" 'ediff-scroll-vertically)
  136. (define-key ediff-mode-map "V" 'ediff-scroll-vertically)
  137. (define-key ediff-mode-map "<" 'ediff-scroll-horizontally)
  138. (define-key ediff-mode-map ">" 'ediff-scroll-horizontally)
  139. (define-key ediff-mode-map "i" 'ediff-status-info)
  140. (define-key ediff-mode-map "E" 'ediff-documentation)
  141. (define-key ediff-mode-map "?" 'ediff-toggle-help)
  142. (define-key ediff-mode-map "!" 'ediff-update-diffs)
  143. (define-key ediff-mode-map "M" 'ediff-show-current-session-meta-buffer)
  144. (define-key ediff-mode-map "R" 'ediff-show-registry)
  145. (or ediff-word-mode
  146. (define-key ediff-mode-map "*" 'ediff-make-or-kill-fine-diffs))
  147. (define-key ediff-mode-map "a" nil)
  148. (define-key ediff-mode-map "b" nil)
  149. (define-key ediff-mode-map "r" nil)
  150. (cond (ediff-merge-job
  151. ;; Will barf if no ancestor
  152. (define-key ediff-mode-map "/" 'ediff-show-ancestor)
  153. ;; In merging, we allow only A->C and B->C copying.
  154. (define-key ediff-mode-map "a" 'ediff-copy-A-to-C)
  155. (define-key ediff-mode-map "b" 'ediff-copy-B-to-C)
  156. (define-key ediff-mode-map "r" 'ediff-restore-diff-in-merge-buffer)
  157. (define-key ediff-mode-map "s" 'ediff-shrink-window-C)
  158. (define-key ediff-mode-map "+" 'ediff-combine-diffs)
  159. (define-key ediff-mode-map "$" nil)
  160. (define-key ediff-mode-map "$$" 'ediff-toggle-show-clashes-only)
  161. (define-key ediff-mode-map "$*" 'ediff-toggle-skip-changed-regions)
  162. (define-key ediff-mode-map "&" 'ediff-re-merge))
  163. (ediff-3way-comparison-job
  164. (define-key ediff-mode-map "ab" 'ediff-copy-A-to-B)
  165. (define-key ediff-mode-map "ba" 'ediff-copy-B-to-A)
  166. (define-key ediff-mode-map "ac" 'ediff-copy-A-to-C)
  167. (define-key ediff-mode-map "bc" 'ediff-copy-B-to-C)
  168. (define-key ediff-mode-map "c" nil)
  169. (define-key ediff-mode-map "ca" 'ediff-copy-C-to-A)
  170. (define-key ediff-mode-map "cb" 'ediff-copy-C-to-B)
  171. (define-key ediff-mode-map "ra" 'ediff-restore-diff)
  172. (define-key ediff-mode-map "rb" 'ediff-restore-diff)
  173. (define-key ediff-mode-map "rc" 'ediff-restore-diff)
  174. (define-key ediff-mode-map "C" 'ediff-toggle-read-only))
  175. (t ; 2-way comparison
  176. (define-key ediff-mode-map "a" 'ediff-copy-A-to-B)
  177. (define-key ediff-mode-map "b" 'ediff-copy-B-to-A)
  178. (define-key ediff-mode-map "ra" 'ediff-restore-diff)
  179. (define-key ediff-mode-map "rb" 'ediff-restore-diff))
  180. ) ; cond
  181. (define-key ediff-mode-map "G" 'ediff-submit-report)
  182. (define-key ediff-mode-map "#" nil)
  183. (define-key ediff-mode-map "#h" 'ediff-toggle-regexp-match)
  184. (define-key ediff-mode-map "#f" 'ediff-toggle-regexp-match)
  185. (define-key ediff-mode-map "#c" 'ediff-toggle-ignore-case)
  186. (or ediff-word-mode
  187. (define-key ediff-mode-map "##" 'ediff-toggle-skip-similar))
  188. (define-key ediff-mode-map "o" nil)
  189. (define-key ediff-mode-map "A" 'ediff-toggle-read-only)
  190. (define-key ediff-mode-map "B" 'ediff-toggle-read-only)
  191. (define-key ediff-mode-map "w" nil)
  192. (define-key ediff-mode-map "wa" 'ediff-save-buffer)
  193. (define-key ediff-mode-map "wb" 'ediff-save-buffer)
  194. (define-key ediff-mode-map "wd" 'ediff-save-buffer)
  195. (define-key ediff-mode-map "=" 'ediff-inferior-compare-regions)
  196. (if (and (fboundp 'ediff-show-patch-diagnostics) (ediff-patch-job))
  197. (define-key ediff-mode-map "P" 'ediff-show-patch-diagnostics))
  198. (if ediff-3way-job
  199. (progn
  200. (define-key ediff-mode-map "wc" 'ediff-save-buffer)
  201. (define-key ediff-mode-map "gc" 'ediff-jump-to-difference-at-point)
  202. ))
  203. (define-key ediff-mode-map "m" 'ediff-toggle-wide-display)
  204. ;; Allow ediff-mode-map to be referenced indirectly
  205. (fset 'ediff-mode-map ediff-mode-map)
  206. (run-hooks 'ediff-keymap-setup-hook))
  207. ;;; Setup functions
  208. ;; Common startup entry for all Ediff functions It now returns control buffer
  209. ;; so other functions can do post-processing SETUP-PARAMETERS is a list of the
  210. ;; form ((param .val) (param . val)...) This serves a similar purpose to
  211. ;; STARTUP-HOOKS, but these parameters are set in the new control buffer right
  212. ;; after this buf is created and before any windows are set and such.
  213. (defun ediff-setup (buffer-A file-A buffer-B file-B buffer-C file-C
  214. startup-hooks setup-parameters
  215. &optional merge-buffer-file)
  216. (run-hooks 'ediff-before-setup-hook)
  217. ;; ediff-convert-standard-filename puts file names in the form appropriate
  218. ;; for the OS at hand.
  219. (setq file-A (ediff-convert-standard-filename (expand-file-name file-A)))
  220. (setq file-B (ediff-convert-standard-filename (expand-file-name file-B)))
  221. (if (stringp file-C)
  222. (setq file-C
  223. (ediff-convert-standard-filename (expand-file-name file-C))))
  224. (if (stringp merge-buffer-file)
  225. (progn
  226. (setq merge-buffer-file
  227. (ediff-convert-standard-filename
  228. (expand-file-name merge-buffer-file)))
  229. ;; check the directory exists
  230. (or (file-exists-p (file-name-directory merge-buffer-file))
  231. (error "Directory %s given as place to save the merge doesn't exist"
  232. (abbreviate-file-name
  233. (file-name-directory merge-buffer-file))))
  234. (if (and (file-exists-p merge-buffer-file)
  235. (file-directory-p merge-buffer-file))
  236. (error "The merge buffer file %s must not be a directory"
  237. (abbreviate-file-name merge-buffer-file)))
  238. ))
  239. (let* ((control-buffer-name
  240. (ediff-unique-buffer-name "*Ediff Control Panel" "*"))
  241. (control-buffer (ediff-with-current-buffer buffer-A
  242. (get-buffer-create control-buffer-name))))
  243. (ediff-with-current-buffer control-buffer
  244. (ediff-mode)
  245. (make-local-variable 'ediff-use-long-help-message)
  246. (make-local-variable 'ediff-prefer-iconified-control-frame)
  247. (make-local-variable 'ediff-split-window-function)
  248. (make-local-variable 'ediff-default-variant)
  249. (make-local-variable 'ediff-merge-window-share)
  250. (make-local-variable 'ediff-window-setup-function)
  251. (make-local-variable 'ediff-keep-variants)
  252. (make-local-variable 'window-min-height)
  253. (setq window-min-height 2)
  254. (if (featurep 'xemacs)
  255. (make-local-hook 'ediff-after-quit-hook-internal))
  256. ;; unwrap set up parameters passed as argument
  257. (while setup-parameters
  258. (set (car (car setup-parameters)) (cdr (car setup-parameters)))
  259. (setq setup-parameters (cdr setup-parameters)))
  260. ;; set variables classifying the current ediff job
  261. ;; must come AFTER setup-parameters
  262. (setq ediff-3way-comparison-job (ediff-3way-comparison-job)
  263. ediff-merge-job (ediff-merge-job)
  264. ediff-merge-with-ancestor-job (ediff-merge-with-ancestor-job)
  265. ediff-3way-job (ediff-3way-job)
  266. ediff-diff3-job (ediff-diff3-job)
  267. ediff-narrow-job (ediff-narrow-job)
  268. ediff-windows-job (ediff-windows-job)
  269. ediff-word-mode-job (ediff-word-mode-job))
  270. ;; Don't delete variants in case of ediff-buffer-* jobs without asking.
  271. ;; This is because one may lose work---dangerous.
  272. (if (string-match "buffer" (symbol-name ediff-job-name))
  273. (setq ediff-keep-variants t))
  274. (if (featurep 'xemacs)
  275. (make-local-hook 'pre-command-hook))
  276. (if (ediff-window-display-p)
  277. (add-hook 'pre-command-hook 'ediff-spy-after-mouse nil 'local))
  278. (setq ediff-mouse-pixel-position (mouse-pixel-position))
  279. ;; adjust for merge jobs
  280. (if ediff-merge-job
  281. (let ((buf
  282. ;; If default variant is `combined', the right stuff is
  283. ;; inserted by ediff-do-merge
  284. ;; Note: at some point, we tried to put ancestor buffer here
  285. ;; (which is currently buffer C. This didn't work right
  286. ;; because the merge buffer will contain lossage: diff regions
  287. ;; in the ancestor, which correspond to revisions that agree
  288. ;; in both buf A and B.
  289. (cond ((eq ediff-default-variant 'default-B)
  290. buffer-B)
  291. (t buffer-A))))
  292. (setq ediff-split-window-function
  293. ediff-merge-split-window-function)
  294. ;; remember the ancestor buffer, if any
  295. (setq ediff-ancestor-buffer buffer-C)
  296. (setq buffer-C
  297. (get-buffer-create
  298. (ediff-unique-buffer-name "*ediff-merge" "*")))
  299. (with-current-buffer buffer-C
  300. (insert-buffer-substring buf)
  301. (goto-char (point-min))
  302. (funcall (ediff-with-current-buffer buf major-mode))
  303. (widen) ; merge buffer is always widened
  304. (add-hook 'local-write-file-hooks 'ediff-set-merge-mode nil t)
  305. )))
  306. (setq buffer-read-only nil
  307. ediff-buffer-A buffer-A
  308. ediff-buffer-B buffer-B
  309. ediff-buffer-C buffer-C
  310. ediff-control-buffer control-buffer)
  311. (ediff-choose-syntax-table)
  312. (setq ediff-control-buffer-suffix
  313. (if (string-match "<[0-9]*>" control-buffer-name)
  314. (substring control-buffer-name
  315. (match-beginning 0) (match-end 0))
  316. "")
  317. ediff-control-buffer-number
  318. (max
  319. 0
  320. (1-
  321. (string-to-number
  322. (substring
  323. ediff-control-buffer-suffix
  324. (or
  325. (string-match "[0-9]+" ediff-control-buffer-suffix)
  326. 0))))))
  327. (setq ediff-error-buffer
  328. (get-buffer-create (ediff-unique-buffer-name "*ediff-errors" "*")))
  329. (with-current-buffer ediff-error-buffer
  330. (setq buffer-undo-list t))
  331. (ediff-with-current-buffer buffer-A (ediff-strip-mode-line-format))
  332. (ediff-with-current-buffer buffer-B (ediff-strip-mode-line-format))
  333. (if ediff-3way-job
  334. (ediff-with-current-buffer buffer-C (ediff-strip-mode-line-format)))
  335. (if (ediff-buffer-live-p ediff-ancestor-buffer)
  336. (ediff-with-current-buffer ediff-ancestor-buffer
  337. (ediff-strip-mode-line-format)))
  338. (ediff-save-protected-variables) ; save variables to be restored on exit
  339. ;; ediff-setup-diff-regions-function must be set after setup
  340. ;; parameters are processed.
  341. (setq ediff-setup-diff-regions-function
  342. (if ediff-diff3-job
  343. 'ediff-setup-diff-regions3
  344. 'ediff-setup-diff-regions))
  345. (setq ediff-wide-bounds
  346. (list (ediff-make-bullet-proof-overlay
  347. '(point-min) '(point-max) ediff-buffer-A)
  348. (ediff-make-bullet-proof-overlay
  349. '(point-min) '(point-max) ediff-buffer-B)
  350. (ediff-make-bullet-proof-overlay
  351. '(point-min) '(point-max) ediff-buffer-C)))
  352. ;; This has effect only on ediff-windows/regions
  353. ;; In all other cases, ediff-visible-region sets visibility bounds to
  354. ;; ediff-wide-bounds, and ediff-narrow-bounds are ignored.
  355. (if ediff-start-narrowed
  356. (setq ediff-visible-bounds ediff-narrow-bounds)
  357. (setq ediff-visible-bounds ediff-wide-bounds))
  358. (ediff-set-keys) ; comes after parameter setup
  359. ;; set up ediff-narrow-bounds, if not set
  360. (or ediff-narrow-bounds
  361. (setq ediff-narrow-bounds ediff-wide-bounds))
  362. ;; All these must be inside ediff-with-current-buffer control-buffer,
  363. ;; since these vars are local to control-buffer
  364. ;; These won't run if there are errors in diff
  365. (ediff-with-current-buffer ediff-buffer-A
  366. (ediff-nuke-selective-display)
  367. (run-hooks 'ediff-prepare-buffer-hook)
  368. (if (ediff-with-current-buffer control-buffer ediff-merge-job)
  369. (setq buffer-read-only t))
  370. ;; add control-buffer to the list of sessions--no longer used, but may
  371. ;; be used again in the future
  372. (or (memq control-buffer ediff-this-buffer-ediff-sessions)
  373. (setq ediff-this-buffer-ediff-sessions
  374. (cons control-buffer ediff-this-buffer-ediff-sessions)))
  375. (if ediff-make-buffers-readonly-at-startup
  376. (setq buffer-read-only t))
  377. )
  378. (ediff-with-current-buffer ediff-buffer-B
  379. (ediff-nuke-selective-display)
  380. (run-hooks 'ediff-prepare-buffer-hook)
  381. (if (ediff-with-current-buffer control-buffer ediff-merge-job)
  382. (setq buffer-read-only t))
  383. ;; add control-buffer to the list of sessions
  384. (or (memq control-buffer ediff-this-buffer-ediff-sessions)
  385. (setq ediff-this-buffer-ediff-sessions
  386. (cons control-buffer ediff-this-buffer-ediff-sessions)))
  387. (if ediff-make-buffers-readonly-at-startup
  388. (setq buffer-read-only t))
  389. )
  390. (if ediff-3way-job
  391. (ediff-with-current-buffer ediff-buffer-C
  392. (ediff-nuke-selective-display)
  393. ;; the merge buffer should never be narrowed
  394. ;; (it can happen if it is on rmail-mode or similar)
  395. (if (ediff-with-current-buffer control-buffer ediff-merge-job)
  396. (widen))
  397. (run-hooks 'ediff-prepare-buffer-hook)
  398. ;; add control-buffer to the list of sessions
  399. (or (memq control-buffer ediff-this-buffer-ediff-sessions)
  400. (setq ediff-this-buffer-ediff-sessions
  401. (cons control-buffer
  402. ediff-this-buffer-ediff-sessions)))
  403. (if ediff-make-buffers-readonly-at-startup
  404. (setq buffer-read-only t)
  405. (setq buffer-read-only nil))
  406. ))
  407. (if (ediff-buffer-live-p ediff-ancestor-buffer)
  408. (ediff-with-current-buffer ediff-ancestor-buffer
  409. (ediff-nuke-selective-display)
  410. (setq buffer-read-only t)
  411. (run-hooks 'ediff-prepare-buffer-hook)
  412. (or (memq control-buffer ediff-this-buffer-ediff-sessions)
  413. (setq ediff-this-buffer-ediff-sessions
  414. (cons control-buffer
  415. ediff-this-buffer-ediff-sessions)))
  416. ))
  417. ;; the following must be after setting up ediff-narrow-bounds AND after
  418. ;; nuking selective display
  419. (funcall ediff-setup-diff-regions-function file-A file-B file-C)
  420. (setq ediff-number-of-differences (length ediff-difference-vector-A))
  421. (setq ediff-current-difference -1)
  422. (ediff-make-current-diff-overlay 'A)
  423. (ediff-make-current-diff-overlay 'B)
  424. (if ediff-3way-job
  425. (ediff-make-current-diff-overlay 'C))
  426. (if ediff-merge-with-ancestor-job
  427. (ediff-make-current-diff-overlay 'Ancestor))
  428. (ediff-setup-windows buffer-A buffer-B buffer-C control-buffer)
  429. (let ((shift-A (ediff-overlay-start
  430. (ediff-get-value-according-to-buffer-type
  431. 'A ediff-narrow-bounds)))
  432. (shift-B (ediff-overlay-start
  433. (ediff-get-value-according-to-buffer-type
  434. 'B ediff-narrow-bounds)))
  435. (shift-C (ediff-overlay-start
  436. (ediff-get-value-according-to-buffer-type
  437. 'C ediff-narrow-bounds))))
  438. ;; position point in buf A
  439. (save-excursion
  440. (select-window ediff-window-A)
  441. (goto-char shift-A))
  442. ;; position point in buf B
  443. (save-excursion
  444. (select-window ediff-window-B)
  445. (goto-char shift-B))
  446. (if ediff-3way-job
  447. (save-excursion
  448. (select-window ediff-window-C)
  449. (goto-char shift-C)))
  450. )
  451. (select-window ediff-control-window)
  452. (ediff-visible-region)
  453. (run-hooks 'startup-hooks)
  454. (ediff-arrange-autosave-in-merge-jobs merge-buffer-file)
  455. (ediff-refresh-mode-lines)
  456. (setq buffer-read-only t)
  457. (setq ediff-session-registry
  458. (cons control-buffer ediff-session-registry))
  459. (ediff-update-registry)
  460. (if (ediff-buffer-live-p ediff-meta-buffer)
  461. (ediff-update-meta-buffer
  462. ediff-meta-buffer nil ediff-meta-session-number))
  463. (run-hooks 'ediff-startup-hook)
  464. ) ; eval in control-buffer
  465. control-buffer))
  466. ;; This function assumes that we are in the window where control buffer is
  467. ;; to reside.
  468. (defun ediff-setup-control-buffer (ctl-buf)
  469. "Set up window for control buffer."
  470. (if (window-dedicated-p (selected-window))
  471. (set-buffer ctl-buf) ; we are in control frame but just in case
  472. (switch-to-buffer ctl-buf))
  473. (let ((window-min-height 2))
  474. (erase-buffer)
  475. (ediff-set-help-message)
  476. (insert ediff-help-message)
  477. (shrink-window-if-larger-than-buffer)
  478. (or (ediff-multiframe-setup-p)
  479. (ediff-indent-help-message))
  480. (ediff-set-help-overlays)
  481. (set-buffer-modified-p nil)
  482. (ediff-refresh-mode-lines)
  483. (setq ediff-control-window (selected-window))
  484. (setq ediff-window-config-saved
  485. (format "%S%S%S%S%S%S%S"
  486. ediff-control-window
  487. ediff-window-A
  488. ediff-window-B
  489. ediff-window-C
  490. ediff-split-window-function
  491. (ediff-multiframe-setup-p)
  492. ediff-wide-display-p))
  493. (set-window-dedicated-p (selected-window) t)
  494. ;; In multiframe, toolbar is set in ediff-setup-control-frame
  495. (if (not (ediff-multiframe-setup-p))
  496. (ediff-make-bottom-toolbar)) ; this checks if toolbar is requested
  497. (goto-char (point-min))
  498. (skip-chars-forward ediff-whitespace)))
  499. ;; This executes in control buffer and sets auto-save, visited file name, etc,
  500. ;; in the merge buffer
  501. (defun ediff-arrange-autosave-in-merge-jobs (merge-buffer-file)
  502. (if (not ediff-merge-job)
  503. ()
  504. (if (stringp merge-buffer-file)
  505. (setq ediff-autostore-merges t
  506. ediff-merge-store-file merge-buffer-file))
  507. (if (stringp ediff-merge-store-file)
  508. (progn
  509. ;; save before leaving ctl buffer
  510. (ediff-verify-file-merge-buffer ediff-merge-store-file)
  511. (setq merge-buffer-file ediff-merge-store-file)
  512. (ediff-with-current-buffer ediff-buffer-C
  513. (set-visited-file-name merge-buffer-file))))
  514. (ediff-with-current-buffer ediff-buffer-C
  515. (setq buffer-offer-save t) ; ask before killing buffer
  516. ;; make sure the contents is auto-saved
  517. (auto-save-mode 1))
  518. ))
  519. ;;; Commands for working with Ediff
  520. (defun ediff-update-diffs ()
  521. "Recompute difference regions in buffers A, B, and C.
  522. Buffers are not synchronized with their respective files, so changes done
  523. to these buffers are not saved at this point---the user can do this later,
  524. if necessary."
  525. (interactive)
  526. (ediff-barf-if-not-control-buffer)
  527. (if (and (ediff-buffer-live-p ediff-ancestor-buffer)
  528. (not
  529. (y-or-n-p
  530. "Ancestor buffer will not be used. Recompute diffs anyway? ")))
  531. (error "Recomputation of differences canceled"))
  532. (let ((point-A (ediff-with-current-buffer ediff-buffer-A (point)))
  533. ;;(point-B (ediff-with-current-buffer ediff-buffer-B (point)))
  534. (tmp-buffer (get-buffer-create ediff-tmp-buffer))
  535. (buf-A-file-name (buffer-file-name ediff-buffer-A))
  536. (buf-B-file-name (buffer-file-name ediff-buffer-B))
  537. ;; (null ediff-buffer-C) is no problem, as we later check if
  538. ;; ediff-buffer-C is alive
  539. (buf-C-file-name (buffer-file-name ediff-buffer-C))
  540. (overl-A (ediff-get-value-according-to-buffer-type
  541. 'A ediff-narrow-bounds))
  542. (overl-B (ediff-get-value-according-to-buffer-type
  543. 'B ediff-narrow-bounds))
  544. (overl-C (ediff-get-value-according-to-buffer-type
  545. 'C ediff-narrow-bounds))
  546. beg-A end-A beg-B end-B beg-C end-C
  547. file-A file-B file-C)
  548. (if (stringp buf-A-file-name)
  549. (setq buf-A-file-name (file-name-nondirectory buf-A-file-name)))
  550. (if (stringp buf-B-file-name)
  551. (setq buf-B-file-name (file-name-nondirectory buf-B-file-name)))
  552. (if (stringp buf-C-file-name)
  553. (setq buf-C-file-name (file-name-nondirectory buf-C-file-name)))
  554. (ediff-unselect-and-select-difference -1)
  555. (setq beg-A (ediff-overlay-start overl-A)
  556. beg-B (ediff-overlay-start overl-B)
  557. beg-C (ediff-overlay-start overl-C)
  558. end-A (ediff-overlay-end overl-A)
  559. end-B (ediff-overlay-end overl-B)
  560. end-C (ediff-overlay-end overl-C))
  561. (if ediff-word-mode
  562. (progn
  563. (ediff-wordify beg-A end-A ediff-buffer-A tmp-buffer)
  564. (setq file-A (ediff-make-temp-file tmp-buffer "regA"))
  565. (ediff-wordify beg-B end-B ediff-buffer-B tmp-buffer)
  566. (setq file-B (ediff-make-temp-file tmp-buffer "regB"))
  567. (if ediff-3way-job
  568. (progn
  569. (ediff-wordify beg-C end-C ediff-buffer-C tmp-buffer)
  570. (setq file-C (ediff-make-temp-file tmp-buffer "regC"))))
  571. )
  572. ;; not word-mode
  573. (setq file-A (ediff-make-temp-file ediff-buffer-A buf-A-file-name))
  574. (setq file-B (ediff-make-temp-file ediff-buffer-B buf-B-file-name))
  575. (if ediff-3way-job
  576. (setq file-C (ediff-make-temp-file ediff-buffer-C buf-C-file-name)))
  577. )
  578. (ediff-clear-diff-vector 'ediff-difference-vector-A 'fine-diffs-also)
  579. (ediff-clear-diff-vector 'ediff-difference-vector-B 'fine-diffs-also)
  580. (ediff-clear-diff-vector 'ediff-difference-vector-C 'fine-diffs-also)
  581. (ediff-clear-diff-vector
  582. 'ediff-difference-vector-Ancestor 'fine-diffs-also)
  583. ;; let them garbage collect. we can't use the ancestor after recomputing
  584. ;; the diffs.
  585. (setq ediff-difference-vector-Ancestor nil
  586. ediff-ancestor-buffer nil
  587. ediff-state-of-merge nil)
  588. (setq ediff-killed-diffs-alist nil) ; invalidate saved killed diff regions
  589. ;; In case of merge job, fool it into thinking that it is just doing
  590. ;; comparison
  591. (let ((ediff-setup-diff-regions-function ediff-setup-diff-regions-function)
  592. (ediff-3way-comparison-job ediff-3way-comparison-job)
  593. (ediff-merge-job ediff-merge-job)
  594. (ediff-merge-with-ancestor-job ediff-merge-with-ancestor-job)
  595. (ediff-job-name ediff-job-name))
  596. (if ediff-merge-job
  597. (setq ediff-setup-diff-regions-function 'ediff-setup-diff-regions3
  598. ediff-3way-comparison-job t
  599. ediff-merge-job nil
  600. ediff-merge-with-ancestor-job nil
  601. ediff-job-name 'ediff-files3))
  602. (funcall ediff-setup-diff-regions-function file-A file-B file-C))
  603. (setq ediff-number-of-differences (length ediff-difference-vector-A))
  604. (delete-file file-A)
  605. (delete-file file-B)
  606. (if file-C
  607. (delete-file file-C))
  608. (if ediff-3way-job
  609. (ediff-set-state-of-all-diffs-in-all-buffers ediff-control-buffer))
  610. (ediff-jump-to-difference (ediff-diff-at-point 'A point-A))
  611. (message "")
  612. ))
  613. ;; Not bound to any key---to dangerous. A user can do it if necessary.
  614. (defun ediff-revert-buffers-then-recompute-diffs (noconfirm)
  615. "Revert buffers A, B and C. Then rerun Ediff on file A and file B."
  616. (interactive "P")
  617. (ediff-barf-if-not-control-buffer)
  618. (let ((bufA ediff-buffer-A)
  619. (bufB ediff-buffer-B)
  620. (bufC ediff-buffer-C)
  621. (ctl-buf ediff-control-buffer)
  622. (keep-variants ediff-keep-variants)
  623. (ancestor-buf ediff-ancestor-buffer)
  624. (ancestor-job ediff-merge-with-ancestor-job)
  625. (merge ediff-merge-job)
  626. (comparison ediff-3way-comparison-job))
  627. (ediff-with-current-buffer bufA
  628. (revert-buffer t noconfirm))
  629. (ediff-with-current-buffer bufB
  630. (revert-buffer t noconfirm))
  631. ;; this should only be executed in a 3way comparison, not in merge
  632. (if comparison
  633. (ediff-with-current-buffer bufC
  634. (revert-buffer t noconfirm)))
  635. (if merge
  636. (progn
  637. (set-buffer ctl-buf)
  638. ;; the argument says whether to reverse the meaning of
  639. ;; ediff-keep-variants, i.e., ediff-really-quit runs here with
  640. ;; variants kept.
  641. (ediff-really-quit (not keep-variants))
  642. (kill-buffer bufC)
  643. (if ancestor-job
  644. (ediff-merge-buffers-with-ancestor bufA bufB ancestor-buf)
  645. (ediff-merge-buffers bufA bufB)))
  646. (ediff-update-diffs))))
  647. ;; optional NO-REHIGHLIGHT says to not rehighlight buffers
  648. (defun ediff-recenter (&optional no-rehighlight)
  649. "Bring the highlighted region of all buffers being compared into view.
  650. Reestablish the default three-window display."
  651. (interactive)
  652. (ediff-barf-if-not-control-buffer)
  653. (let (buffer-read-only)
  654. (if (and (ediff-buffer-live-p ediff-buffer-A)
  655. (ediff-buffer-live-p ediff-buffer-B)
  656. (or (not ediff-3way-job)
  657. (ediff-buffer-live-p ediff-buffer-C)))
  658. (ediff-setup-windows
  659. ediff-buffer-A ediff-buffer-B ediff-buffer-C ediff-control-buffer)
  660. (or (eq this-command 'ediff-quit)
  661. (message ediff-KILLED-VITAL-BUFFER
  662. (beep 1)))
  663. ))
  664. ;; set visibility range appropriate to this invocation of Ediff.
  665. (ediff-visible-region)
  666. ;; raise
  667. (if (and (ediff-window-display-p)
  668. (symbolp this-command)
  669. (symbolp last-command)
  670. ;; Either one of the display-changing commands
  671. (or (memq this-command
  672. '(ediff-recenter
  673. ediff-dir-action ediff-registry-action
  674. ediff-patch-action
  675. ediff-toggle-wide-display ediff-toggle-multiframe))
  676. ;; Or one of the movement cmds and prev cmd was an Ediff cmd
  677. ;; This avoids raising frames unnecessarily.
  678. (and (memq this-command
  679. '(ediff-next-difference
  680. ediff-previous-difference
  681. ediff-jump-to-difference
  682. ediff-jump-to-difference-at-point))
  683. (not (string-match "^ediff-" (symbol-name last-command)))
  684. )))
  685. (progn
  686. (if (window-live-p ediff-window-A)
  687. (raise-frame (window-frame ediff-window-A)))
  688. (if (window-live-p ediff-window-B)
  689. (raise-frame (window-frame ediff-window-B)))
  690. (if (window-live-p ediff-window-C)
  691. (raise-frame (window-frame ediff-window-C)))))
  692. (if (and (ediff-window-display-p)
  693. (frame-live-p ediff-control-frame)
  694. (not ediff-use-long-help-message)
  695. (not (ediff-frame-iconified-p ediff-control-frame)))
  696. (raise-frame ediff-control-frame))
  697. ;; Redisplay whatever buffers are showing, if there is a selected difference
  698. (let ((control-frame ediff-control-frame)
  699. (control-buf ediff-control-buffer))
  700. (if (and (ediff-buffer-live-p ediff-buffer-A)
  701. (ediff-buffer-live-p ediff-buffer-B)
  702. (or (not ediff-3way-job)
  703. (ediff-buffer-live-p ediff-buffer-C)))
  704. (progn
  705. (or no-rehighlight
  706. (ediff-select-difference ediff-current-difference))
  707. (ediff-recenter-one-window 'A)
  708. (ediff-recenter-one-window 'B)
  709. (if ediff-3way-job
  710. (ediff-recenter-one-window 'C))
  711. (ediff-with-current-buffer control-buf
  712. (ediff-recenter-ancestor) ; check if ancestor is alive
  713. (if (and (ediff-multiframe-setup-p)
  714. (not ediff-use-long-help-message)
  715. (not (ediff-frame-iconified-p ediff-control-frame)))
  716. ;; never grab mouse on quit in this place
  717. (ediff-reset-mouse
  718. control-frame
  719. (eq this-command 'ediff-quit))))
  720. ))
  721. (or no-rehighlight
  722. (ediff-restore-highlighting))
  723. (ediff-with-current-buffer control-buf (ediff-refresh-mode-lines))
  724. ))
  725. ;; this function returns to the window it was called from
  726. ;; (which was the control window)
  727. (defun ediff-recenter-one-window (buf-type)
  728. (if (ediff-valid-difference-p)
  729. ;; context must be saved before switching to windows A/B/C
  730. (let* ((ctl-wind (selected-window))
  731. (shift (ediff-overlay-start
  732. (ediff-get-value-according-to-buffer-type
  733. buf-type ediff-narrow-bounds)))
  734. (job-name ediff-job-name)
  735. (control-buf ediff-control-buffer)
  736. (window-name (ediff-get-symbol-from-alist
  737. buf-type ediff-window-alist))
  738. (window (if (window-live-p (symbol-value window-name))
  739. (symbol-value window-name))))
  740. (if (and window ediff-windows-job)
  741. (set-window-start window shift))
  742. (if window
  743. (progn
  744. (select-window window)
  745. (ediff-deactivate-mark)
  746. (ediff-position-region
  747. (ediff-get-diff-posn buf-type 'beg nil control-buf)
  748. (ediff-get-diff-posn buf-type 'end nil control-buf)
  749. (ediff-get-diff-posn buf-type 'beg nil control-buf)
  750. job-name
  751. )))
  752. (select-window ctl-wind)
  753. )))
  754. (defun ediff-recenter-ancestor ()
  755. ;; do half-hearted job by recentering the ancestor buffer, if it is alive and
  756. ;; visible.
  757. (if (and (ediff-buffer-live-p ediff-ancestor-buffer)
  758. (ediff-valid-difference-p))
  759. (let ((window (ediff-get-visible-buffer-window ediff-ancestor-buffer))
  760. (ctl-wind (selected-window))
  761. (job-name ediff-job-name)
  762. (ctl-buf ediff-control-buffer))
  763. (ediff-with-current-buffer ediff-ancestor-buffer
  764. (goto-char (ediff-get-diff-posn 'Ancestor 'beg nil ctl-buf))
  765. (if window
  766. (progn
  767. (select-window window)
  768. (ediff-position-region
  769. (ediff-get-diff-posn 'Ancestor 'beg nil ctl-buf)
  770. (ediff-get-diff-posn 'Ancestor 'end nil ctl-buf)
  771. (ediff-get-diff-posn 'Ancestor 'beg nil ctl-buf)
  772. job-name))))
  773. (select-window ctl-wind)
  774. )))
  775. ;; This will have to be refined for 3way jobs
  776. (defun ediff-toggle-split ()
  777. "Toggle vertical/horizontal window split.
  778. Does nothing if file-A and file-B are in different frames."
  779. (interactive)
  780. (ediff-barf-if-not-control-buffer)
  781. (let* ((wind-A (if (window-live-p ediff-window-A) ediff-window-A))
  782. (wind-B (if (window-live-p ediff-window-B) ediff-window-B))
  783. (wind-C (if (window-live-p ediff-window-C) ediff-window-C))
  784. (frame-A (if wind-A (window-frame wind-A)))
  785. (frame-B (if wind-B (window-frame wind-B)))
  786. (frame-C (if wind-C (window-frame wind-C))))
  787. (if (or (eq frame-A frame-B)
  788. (not (frame-live-p frame-A))
  789. (not (frame-live-p frame-B))
  790. (if ediff-3way-comparison-job
  791. (or (not (frame-live-p frame-C))
  792. (eq frame-A frame-C) (eq frame-B frame-C))))
  793. (setq ediff-split-window-function
  794. (if (eq ediff-split-window-function 'split-window-vertically)
  795. 'split-window-horizontally
  796. 'split-window-vertically))
  797. (message "Buffers being compared are in different frames"))
  798. (ediff-recenter 'no-rehighlight)))
  799. (defun ediff-toggle-hilit ()
  800. "Switch between highlighting using ASCII flags and highlighting using faces.
  801. On a dumb terminal, switches between ASCII highlighting and no highlighting."
  802. (interactive)
  803. (ediff-barf-if-not-control-buffer)
  804. (ediff-unselect-and-select-difference
  805. ediff-current-difference 'unselect-only)
  806. ;; cycle through highlighting
  807. (cond ((and ediff-use-faces
  808. (ediff-has-face-support-p)
  809. ediff-highlight-all-diffs)
  810. (message "Unhighlighting unselected difference regions")
  811. (setq ediff-highlight-all-diffs nil
  812. ediff-highlighting-style 'face))
  813. ((or (and ediff-use-faces (ediff-has-face-support-p)
  814. (eq ediff-highlighting-style 'face)) ; has face support
  815. (and (not (ediff-has-face-support-p)) ; no face support
  816. (eq ediff-highlighting-style 'off)))
  817. (message "Highlighting with ASCII flags")
  818. (setq ediff-highlighting-style 'ascii
  819. ediff-highlight-all-diffs nil
  820. ediff-use-faces nil))
  821. ((eq ediff-highlighting-style 'ascii)
  822. (message "ASCII highlighting flags removed")
  823. (setq ediff-highlighting-style 'off
  824. ediff-highlight-all-diffs nil))
  825. ((ediff-has-face-support-p) ; catch-all for cases with face support
  826. (message "Re-highlighting all difference regions")
  827. (setq ediff-use-faces t
  828. ediff-highlighting-style 'face
  829. ediff-highlight-all-diffs t)))
  830. (if (and ediff-use-faces ediff-highlight-all-diffs)
  831. (ediff-paint-background-regions)
  832. (ediff-paint-background-regions 'unhighlight))
  833. (ediff-unselect-and-select-difference
  834. ediff-current-difference 'select-only))
  835. (defun ediff-toggle-autorefine ()
  836. "Toggle auto-refine mode."
  837. (interactive)
  838. (ediff-barf-if-not-control-buffer)
  839. (if ediff-word-mode
  840. (error "No fine differences in this mode"))
  841. (cond ((eq ediff-auto-refine 'nix)
  842. (setq ediff-auto-refine 'on)
  843. (ediff-make-fine-diffs ediff-current-difference 'noforce)
  844. (message "Auto-refining is ON"))
  845. ((eq ediff-auto-refine 'on)
  846. (message "Auto-refining is OFF")
  847. (setq ediff-auto-refine 'off))
  848. (t ;; nix 'em
  849. (ediff-set-fine-diff-properties ediff-current-difference 'default)
  850. (message "Refinements are HIDDEN")
  851. (setq ediff-auto-refine 'nix))
  852. ))
  853. (defun ediff-show-ancestor ()
  854. "Show the ancestor buffer in a suitable window."
  855. (interactive)
  856. (ediff-recenter)
  857. (or (ediff-buffer-live-p ediff-ancestor-buffer)
  858. (if ediff-merge-with-ancestor-job
  859. (error "Lost connection to ancestor buffer...sorry")
  860. (error "Not merging with ancestor")))
  861. (let (wind)
  862. (cond ((setq wind (ediff-get-visible-buffer-window ediff-ancestor-buffer))
  863. (raise-frame (window-frame wind)))
  864. (t (set-window-buffer ediff-window-C ediff-ancestor-buffer)))))
  865. (defun ediff-make-or-kill-fine-diffs (arg)
  866. "Compute fine diffs. With negative prefix arg, kill fine diffs.
  867. In both cases, operates on the current difference region."
  868. (interactive "P")
  869. (ediff-barf-if-not-control-buffer)
  870. (cond ((eq arg '-)
  871. (ediff-clear-fine-differences ediff-current-difference))
  872. ((and (numberp arg) (< arg 0))
  873. (ediff-clear-fine-differences ediff-current-difference))
  874. (t (ediff-make-fine-diffs))))
  875. (defun ediff-toggle-help ()
  876. "Toggle short/long help message."
  877. (interactive)
  878. (ediff-barf-if-not-control-buffer)
  879. (let (buffer-read-only)
  880. (erase-buffer)
  881. (setq ediff-use-long-help-message (not ediff-use-long-help-message))
  882. (ediff-set-help-message))
  883. ;; remember the icon status of the control frame when the user requested
  884. ;; full control message
  885. (if (and ediff-use-long-help-message (ediff-multiframe-setup-p))
  886. (setq ediff-prefer-iconified-control-frame
  887. (ediff-frame-iconified-p ediff-control-frame)))
  888. (setq ediff-window-config-saved "") ; force redisplay
  889. (ediff-recenter 'no-rehighlight))
  890. ;; If BUF, this is the buffer to toggle, not current buffer.
  891. (defun ediff-toggle-read-only (&optional buf)
  892. "Toggle read-only in current buffer.
  893. If buffer is under version control and locked, check it out first.
  894. If optional argument BUF is specified, toggle read-only in that buffer instead
  895. of the current buffer."
  896. (interactive)
  897. (ediff-barf-if-not-control-buffer)
  898. (let ((ctl-buf (if (null buf) (current-buffer)))
  899. (buf-type (ediff-char-to-buftype (ediff-last-command-char))))
  900. (or buf (ediff-recenter))
  901. (or buf
  902. (setq buf (ediff-get-buffer buf-type)))
  903. (ediff-with-current-buffer buf ; eval in buf A/B/C
  904. (let* ((file (buffer-file-name buf))
  905. (file-writable (and file
  906. (file-exists-p file)
  907. (file-writable-p file)))
  908. (toggle-ro-cmd (cond (ediff-toggle-read-only-function)
  909. ((ediff-file-checked-out-p file)
  910. 'toggle-read-only)
  911. (file-writable 'toggle-read-only)
  912. (t (key-binding "\C-x\C-q")))))
  913. ;; If the file is checked in, make sure we don't make buffer modifiable
  914. ;; without warning the user. The user can fool our checks by making the
  915. ;; buffer non-RO without checking the file out. We regard this as a
  916. ;; user problem.
  917. (if (and (ediff-file-checked-in-p file)
  918. ;; If ctl-buf is null, this means we called this
  919. ;; non-interactively, in which case don't ask questions
  920. ctl-buf)
  921. (cond ((not buffer-read-only)
  922. (setq toggle-ro-cmd 'toggle-read-only))
  923. ((and (or (beep 1) t) ; always beep
  924. (y-or-n-p
  925. (format
  926. "File %s is under version control. Check it out? "
  927. (ediff-abbreviate-file-name file))))
  928. ;; if we checked the file out, we should also change the
  929. ;; original state of buffer-read-only to nil. If we don't
  930. ;; do this, the mode line will show %%, since the file was
  931. ;; RO before ediff started, so the user will think the file
  932. ;; is checked in.
  933. (ediff-with-current-buffer ctl-buf
  934. (ediff-change-saved-variable
  935. 'buffer-read-only nil buf-type)))
  936. (t
  937. (setq toggle-ro-cmd 'toggle-read-only)
  938. (beep 1) (beep 1)
  939. (message
  940. "Boy, this is risky! Don't modify this file...")
  941. (sit-for 3)))) ; let the user see the warning
  942. (if (and toggle-ro-cmd
  943. (string-match "toggle-read-only" (symbol-name toggle-ro-cmd)))
  944. (save-excursion
  945. (save-window-excursion
  946. (select-window (ediff-get-visible-buffer-window buf))
  947. (command-execute toggle-ro-cmd)))
  948. (error "Don't know how to toggle read-only in buffer %S" buf))
  949. ;; Check if we made the current buffer updatable, but its file is RO.
  950. ;; Signal a warning in this case.
  951. (if (and file (not buffer-read-only)
  952. (eq this-command 'ediff-toggle-read-only)
  953. (file-exists-p file)
  954. (not (file-writable-p file)))
  955. (progn
  956. (beep 1)
  957. (message "Warning: file %s is read-only"
  958. (ediff-abbreviate-file-name file))))
  959. ))))
  960. ;; checkout if visited file is checked in
  961. (defun ediff-maybe-checkout (buf)
  962. (let ((file (expand-file-name (buffer-file-name buf)))
  963. (checkout-function (key-binding "\C-x\C-q")))
  964. (if (and (ediff-file-checked-in-p file)
  965. (or (beep 1) t)
  966. (y-or-n-p
  967. (format
  968. "File %s is under version control. Check it out? "
  969. (ediff-abbreviate-file-name file))))
  970. (ediff-with-current-buffer buf
  971. (command-execute checkout-function)))))
  972. ;; This is a simple-minded check for whether a file is under version control.
  973. ;; If file,v exists but file doesn't, this file is considered to be not checked
  974. ;; in and not checked out for the purpose of patching (since patch won't be
  975. ;; able to read such a file anyway).
  976. ;; FILE is a string representing file name
  977. ;;(defun ediff-file-under-version-control (file)
  978. ;; (let* ((filedir (file-name-directory file))
  979. ;; (file-nondir (file-name-nondirectory file))
  980. ;; (trial (concat file-nondir ",v"))
  981. ;; (full-trial (concat filedir trial))
  982. ;; (full-rcs-trial (concat filedir "RCS/" trial)))
  983. ;; (and (stringp file)
  984. ;; (file-exists-p file)
  985. ;; (or
  986. ;; (and
  987. ;; (file-exists-p full-trial)
  988. ;; ;; in FAT FS, `file,v' and `file' may turn out to be the same!
  989. ;; ;; don't be fooled by this!
  990. ;; (not (equal (file-attributes file)
  991. ;; (file-attributes full-trial))))
  992. ;; ;; check if a version is in RCS/ directory
  993. ;; (file-exists-p full-rcs-trial)))
  994. ;; ))
  995. (defun ediff-file-checked-out-p (file)
  996. (or (not (featurep 'vc-hooks))
  997. (and (vc-backend file)
  998. (if (fboundp 'vc-state)
  999. (or (memq (vc-state file) '(edited needs-merge))
  1000. (stringp (vc-state file)))
  1001. ;; XEmacs has no vc-state
  1002. (when (featurep 'xemacs) (vc-locking-user file)))
  1003. )))
  1004. (defun ediff-file-checked-in-p (file)
  1005. (and (featurep 'vc-hooks)
  1006. ;; Only RCS and SCCS files are considered checked in
  1007. (memq (vc-backend file) '(RCS SCCS))
  1008. (if (fboundp 'vc-state)
  1009. (and
  1010. (not (memq (vc-state file) '(edited needs-merge)))
  1011. (not (stringp (vc-state file))))
  1012. ;; XEmacs has no vc-state
  1013. (when (featurep 'xemacs) (not (vc-locking-user file))))
  1014. ))
  1015. (defun ediff-file-compressed-p (file)
  1016. (condition-case nil
  1017. (require 'jka-compr)
  1018. (error))
  1019. (if (featurep 'jka-compr)
  1020. (string-match (jka-compr-build-file-regexp) file)))
  1021. (defun ediff-swap-buffers ()
  1022. "Rotate the display of buffers A, B, and C."
  1023. (interactive)
  1024. (ediff-barf-if-not-control-buffer)
  1025. (if (and (window-live-p ediff-window-A) (window-live-p ediff-window-B))
  1026. (let ((buf ediff-buffer-A)
  1027. (values ediff-buffer-values-orig-A)
  1028. (diff-vec ediff-difference-vector-A)
  1029. (hide-regexp ediff-regexp-hide-A)
  1030. (focus-regexp ediff-regexp-focus-A)
  1031. (wide-visibility-p (eq ediff-visible-bounds ediff-wide-bounds))
  1032. (overlay (if (ediff-has-face-support-p)
  1033. ediff-current-diff-overlay-A)))
  1034. (if ediff-3way-comparison-job
  1035. (progn
  1036. (set-window-buffer ediff-window-A ediff-buffer-C)
  1037. (set-window-buffer ediff-window-B ediff-buffer-A)
  1038. (set-window-buffer ediff-window-C ediff-buffer-B)
  1039. )
  1040. (set-window-buffer ediff-window-A ediff-buffer-B)
  1041. (set-window-buffer ediff-window-B ediff-buffer-A))
  1042. ;; swap diff buffers
  1043. (if ediff-3way-comparison-job
  1044. (setq ediff-buffer-A ediff-buffer-C
  1045. ediff-buffer-C ediff-buffer-B
  1046. ediff-buffer-B buf)
  1047. (setq ediff-buffer-A ediff-buffer-B
  1048. ediff-buffer-B buf))
  1049. ;; swap saved buffer characteristics
  1050. (if ediff-3way-comparison-job
  1051. (setq ediff-buffer-values-orig-A ediff-buffer-values-orig-C
  1052. ediff-buffer-values-orig-C ediff-buffer-values-orig-B
  1053. ediff-buffer-values-orig-B values)
  1054. (setq ediff-buffer-values-orig-A ediff-buffer-values-orig-B
  1055. ediff-buffer-values-orig-B values))
  1056. ;; swap diff vectors
  1057. (if ediff-3way-comparison-job
  1058. (setq ediff-difference-vector-A ediff-difference-vector-C
  1059. ediff-difference-vector-C ediff-difference-vector-B
  1060. ediff-difference-vector-B diff-vec)
  1061. (setq ediff-difference-vector-A ediff-difference-vector-B
  1062. ediff-difference-vector-B diff-vec))
  1063. ;; swap hide/focus regexp
  1064. (if ediff-3way-comparison-job
  1065. (setq ediff-regexp-hide-A ediff-regexp-hide-C
  1066. ediff-regexp-hide-C ediff-regexp-hide-B
  1067. ediff-regexp-hide-B hide-regexp
  1068. ediff-regexp-focus-A ediff-regexp-focus-C
  1069. ediff-regexp-focus-C ediff-regexp-focus-B
  1070. ediff-regexp-focus-B focus-regexp)
  1071. (setq ediff-regexp-hide-A ediff-regexp-hide-B
  1072. ediff-regexp-hide-B hide-regexp
  1073. ediff-regexp-focus-A ediff-regexp-focus-B
  1074. ediff-regexp-focus-B focus-regexp))
  1075. ;; The following is needed for XEmacs, since there one can't move
  1076. ;; overlay to another buffer. In Emacs, this swap is redundant.
  1077. (if (ediff-has-face-support-p)
  1078. (if ediff-3way-comparison-job
  1079. (setq ediff-current-diff-overlay-A ediff-current-diff-overlay-C
  1080. ediff-current-diff-overlay-C ediff-current-diff-overlay-B
  1081. ediff-current-diff-overlay-B overlay)
  1082. (setq ediff-current-diff-overlay-A ediff-current-diff-overlay-B
  1083. ediff-current-diff-overlay-B overlay)))
  1084. ;; swap wide bounds
  1085. (setq ediff-wide-bounds
  1086. (cond (ediff-3way-comparison-job
  1087. (list (nth 2 ediff-wide-bounds)
  1088. (nth 0 ediff-wide-bounds)
  1089. (nth 1 ediff-wide-bounds)))
  1090. (ediff-3way-job
  1091. (list (nth 1 ediff-wide-bounds)
  1092. (nth 0 ediff-wide-bounds)
  1093. (nth 2 ediff-wide-bounds)))
  1094. (t
  1095. (list (nth 1 ediff-wide-bounds)
  1096. (nth 0 ediff-wide-bounds)))))
  1097. ;; swap narrow bounds
  1098. (setq ediff-narrow-bounds
  1099. (cond (ediff-3way-comparison-job
  1100. (list (nth 2 ediff-narrow-bounds)
  1101. (nth 0 ediff-narrow-bounds)
  1102. (nth 1 ediff-narrow-bounds)))
  1103. (ediff-3way-job
  1104. (list (nth 1 ediff-narrow-bounds)
  1105. (nth 0 ediff-narrow-bounds)
  1106. (nth 2 ediff-narrow-bounds)))
  1107. (t
  1108. (list (nth 1 ediff-narrow-bounds)
  1109. (nth 0 ediff-narrow-bounds)))))
  1110. (if wide-visibility-p
  1111. (setq ediff-visible-bounds ediff-wide-bounds)
  1112. (setq ediff-visible-bounds ediff-narrow-bounds))
  1113. ))
  1114. (if ediff-3way-job
  1115. (ediff-set-state-of-all-diffs-in-all-buffers ediff-control-buffer))
  1116. (ediff-recenter 'no-rehighlight)
  1117. )
  1118. (defun ediff-toggle-wide-display ()
  1119. "Toggle wide/regular display.
  1120. This is especially useful when comparing buffers side-by-side."
  1121. (interactive)
  1122. (ediff-barf-if-not-control-buffer)
  1123. (or (ediff-window-display-p)
  1124. (error "%sEmacs is not running as a window application"
  1125. (if (featurep 'emacs) "" "X")))
  1126. (ediff-recenter 'no-rehighlight) ; make sure buffs are displayed in windows
  1127. (let ((ctl-buf ediff-control-buffer))
  1128. (setq ediff-wide-display-p (not ediff-wide-display-p))
  1129. (if (not ediff-wide-display-p)
  1130. (ediff-with-current-buffer ctl-buf
  1131. (modify-frame-parameters
  1132. ediff-wide-display-frame ediff-wide-display-orig-parameters)
  1133. ;;(sit-for (if (featurep 'xemacs) 0.4 0))
  1134. ;; restore control buf, since ctl window may have been deleted
  1135. ;; during resizing
  1136. (set-buffer ctl-buf)
  1137. (setq ediff-wide-display-orig-parameters nil
  1138. ediff-window-B nil) ; force update of window config
  1139. (ediff-recenter 'no-rehighlight))
  1140. (funcall ediff-make-wide-display-function)
  1141. ;;(sit-for (if (featurep 'xemacs) 0.4 0))
  1142. (ediff-with-current-buffer ctl-buf
  1143. (setq ediff-window-B nil) ; force update of window config
  1144. (ediff-recenter 'no-rehighlight)))))
  1145. ;;;###autoload
  1146. (defun ediff-toggle-multiframe ()
  1147. "Switch from multiframe display to single-frame display and back.
  1148. To change the default, set the variable `ediff-window-setup-function',
  1149. which see."
  1150. (interactive)
  1151. (let (window-setup-func)
  1152. (or (ediff-window-display-p)
  1153. (error "%sEmacs is not running as a window application"
  1154. (if (featurep 'emacs) "" "X")))
  1155. (cond ((eq ediff-window-setup-function 'ediff-setup-windows-multiframe)
  1156. (setq ediff-multiframe nil)
  1157. (setq window-setup-func 'ediff-setup-windows-plain))
  1158. ((eq ediff-window-setup-function 'ediff-setup-windows-plain)
  1159. (if (ediff-in-control-buffer-p)
  1160. (ediff-kill-bottom-toolbar))
  1161. (if (and (ediff-buffer-live-p ediff-control-buffer)
  1162. (window-live-p ediff-control-window))
  1163. (set-window-dedicated-p ediff-control-window nil))
  1164. (setq ediff-multiframe t)
  1165. (setq window-setup-func 'ediff-setup-windows-multiframe))
  1166. (t
  1167. (if (and (ediff-buffer-live-p ediff-control-buffer)
  1168. (window-live-p ediff-control-window))
  1169. (set-window-dedicated-p ediff-control-window nil))
  1170. (setq ediff-multiframe t)
  1171. (setq window-setup-func 'ediff-setup-windows-multiframe))
  1172. )
  1173. ;; change default
  1174. (setq-default ediff-window-setup-function window-setup-func)
  1175. ;; change in all active ediff sessions
  1176. (mapc (lambda(buf) (ediff-with-current-buffer buf
  1177. (setq ediff-window-setup-function window-setup-func
  1178. ediff-window-B nil)))
  1179. ediff-session-registry)
  1180. (if (ediff-in-control-buffer-p)
  1181. (progn
  1182. (set-window-dedicated-p (selected-window) nil)
  1183. (ediff-recenter 'no-rehighlight)))))
  1184. ;;;###autoload
  1185. (defun ediff-toggle-use-toolbar ()
  1186. "Enable or disable Ediff toolbar.
  1187. Works only in versions of Emacs that support toolbars.
  1188. To change the default, set the variable `ediff-use-toolbar-p', which see."
  1189. (interactive)
  1190. (if (featurep 'ediff-tbar)
  1191. (progn
  1192. (or (ediff-window-display-p)
  1193. (error "%sEmacs is not running as a window application"
  1194. (if (featurep 'emacs) "" "X")))
  1195. (if (ediff-use-toolbar-p)
  1196. (ediff-kill-bottom-toolbar))
  1197. ;; do this only after killing the toolbar
  1198. (setq ediff-use-toolbar-p (not ediff-use-toolbar-p))
  1199. (mapc (lambda(buf)
  1200. (ediff-with-current-buffer buf
  1201. ;; force redisplay
  1202. (setq ediff-window-config-saved "")
  1203. ))
  1204. ediff-session-registry)
  1205. (if (ediff-in-control-buffer-p)
  1206. (ediff-recenter 'no-rehighlight)))))
  1207. ;; if was using toolbar, kill it
  1208. (defun ediff-kill-bottom-toolbar ()
  1209. ;; Using ctl-buffer or ediff-control-window for LOCALE does not
  1210. ;; work properly in XEmacs 19.14: we have to use
  1211. ;;(selected-frame).
  1212. ;; The problem with this is that any previous bottom-toolbar
  1213. ;; will not re-appear after our cleanup here. Is there a way
  1214. ;; to do "push" and "pop" toolbars ? --marcpa
  1215. (if (featurep 'xemacs)
  1216. (when (ediff-use-toolbar-p)
  1217. (set-specifier bottom-toolbar (list (selected-frame) nil))
  1218. (set-specifier bottom-toolbar-visible-p (list (selected-frame) nil)))))
  1219. ;; If wants to use toolbar, make it.
  1220. ;; If not, zero the toolbar for XEmacs.
  1221. ;; Do nothing for Emacs.
  1222. (defun ediff-make-bottom-toolbar (&optional frame)
  1223. (when (ediff-window-display-p)
  1224. (setq frame (or frame (selected-frame)))
  1225. (if (featurep 'xemacs)
  1226. (cond ((ediff-use-toolbar-p) ; this checks for XEmacs
  1227. (set-specifier
  1228. bottom-toolbar
  1229. (list frame (if (ediff-3way-comparison-job)
  1230. ediff-toolbar-3way ediff-toolbar)))
  1231. (set-specifier bottom-toolbar-visible-p (list frame t))
  1232. (set-specifier bottom-toolbar-height
  1233. (list frame ediff-toolbar-height)))
  1234. ((ediff-has-toolbar-support-p)
  1235. (set-specifier bottom-toolbar-height (list frame 0)))))))
  1236. ;; Merging
  1237. (defun ediff-toggle-show-clashes-only ()
  1238. "Toggle the mode that shows only the merge regions where both variants differ from the ancestor."
  1239. (interactive)
  1240. (ediff-barf-if-not-control-buffer)
  1241. (if (not ediff-merge-with-ancestor-job)
  1242. (error "This command makes sense only when merging with an ancestor"))
  1243. (setq ediff-show-clashes-only (not ediff-show-clashes-only))
  1244. (if ediff-show-clashes-only
  1245. (message "Focus on regions where both buffers differ from the ancestor")
  1246. (message "Canceling focus on regions where changes clash")))
  1247. (defun ediff-toggle-skip-changed-regions ()
  1248. "Toggle the mode that skips the merge regions that differ from the default."
  1249. (interactive)
  1250. (ediff-barf-if-not-control-buffer)
  1251. (setq ediff-skip-merge-regions-that-differ-from-default
  1252. (not ediff-skip-merge-regions-that-differ-from-default))
  1253. (if ediff-skip-merge-regions-that-differ-from-default
  1254. (message "Skipping regions that differ from default setting")
  1255. (message "Showing regions that differ from default setting")))
  1256. ;; Widening/narrowing
  1257. (defun ediff-toggle-narrow-region ()
  1258. "Toggle narrowing in buffers A, B, and C.
  1259. Used in ediff-windows/regions only."
  1260. (interactive)
  1261. (if (eq ediff-buffer-A ediff-buffer-B)
  1262. (error ediff-NO-DIFFERENCES))
  1263. (if (eq ediff-visible-bounds ediff-wide-bounds)
  1264. (setq ediff-visible-bounds ediff-narrow-bounds)
  1265. (setq ediff-visible-bounds ediff-wide-bounds))
  1266. (ediff-recenter 'no-rehighlight))
  1267. ;; Narrow bufs A/B/C to ediff-visible-bounds. If this is currently set to
  1268. ;; ediff-wide-bounds, then this actually widens.
  1269. ;; This function does nothing if job-name is not
  1270. ;; ediff-regions-wordwise/linewise or ediff-windows-wordwise/linewise.
  1271. ;; Does nothing if buffer-A = buffer-B since we can't narrow
  1272. ;; to two different regions in one buffer.
  1273. (defun ediff-visible-region ()
  1274. (if (or (eq ediff-buffer-A ediff-buffer-B)
  1275. (eq ediff-buffer-A ediff-buffer-C)
  1276. (eq ediff-buffer-C ediff-buffer-B))
  1277. ()
  1278. ;; If ediff-*-regions/windows, ediff-visible-bounds is already set
  1279. ;; Otherwise, always use full range.
  1280. (if (not ediff-narrow-job)
  1281. (setq ediff-visible-bounds ediff-wide-bounds))
  1282. (let ((overl-A (ediff-get-value-according-to-buffer-type
  1283. 'A ediff-visible-bounds))
  1284. (overl-B (ediff-get-value-according-to-buffer-type
  1285. 'B ediff-visible-bounds))
  1286. (overl-C (ediff-get-value-according-to-buffer-type
  1287. 'C ediff-visible-bounds))
  1288. )
  1289. (ediff-with-current-buffer ediff-buffer-A
  1290. (if (ediff-overlay-buffer overl-A)
  1291. (narrow-to-region
  1292. (ediff-overlay-start overl-A) (ediff-overlay-end overl-A))))
  1293. (ediff-with-current-buffer ediff-buffer-B
  1294. (if (ediff-overlay-buffer overl-B)
  1295. (narrow-to-region
  1296. (ediff-overlay-start overl-B) (ediff-overlay-end overl-B))))
  1297. (if (and ediff-3way-job (ediff-overlay-buffer overl-C))
  1298. (ediff-with-current-buffer ediff-buffer-C
  1299. (narrow-to-region
  1300. (ediff-overlay-start overl-C) (ediff-overlay-end overl-C))))
  1301. )))
  1302. ;; Window scrolling operations
  1303. ;; Performs some operation on the two file windows (if they are showing).
  1304. ;; Traps all errors on the operation in windows A/B/C.
  1305. ;; Usually, errors come from scrolling off the
  1306. ;; beginning or end of the buffer, and this gives error messages.
  1307. (defun ediff-operate-on-windows (operation arg)
  1308. ;; make sure windows aren't dead
  1309. (if (not (and (window-live-p ediff-window-A) (window-live-p ediff-window-B)))
  1310. (ediff-recenter 'no-rehighlight))
  1311. (if (not (and (ediff-buffer-live-p ediff-buffer-A)
  1312. (ediff-buffer-live-p ediff-buffer-B)
  1313. (or (not ediff-3way-job) ediff-buffer-C)
  1314. ))
  1315. (error ediff-KILLED-VITAL-BUFFER))
  1316. (let* ((wind (selected-window))
  1317. (wind-A ediff-window-A)
  1318. (wind-B ediff-window-B)
  1319. (wind-C ediff-window-C)
  1320. (coefA (ediff-get-region-size-coefficient 'A operation))
  1321. (coefB (ediff-get-region-size-coefficient 'B operation))
  1322. (three-way ediff-3way-job)
  1323. (coefC (if three-way
  1324. (ediff-get-region-size-coefficient 'C operation))))
  1325. (select-window wind-A)
  1326. (condition-case nil
  1327. (funcall operation (round (* coefA arg)))
  1328. (error))
  1329. (select-window wind-B)
  1330. (condition-case nil
  1331. (funcall operation (round (* coefB arg)))
  1332. (error))
  1333. (if three-way
  1334. (progn
  1335. (select-window wind-C)
  1336. (condition-case nil
  1337. (funcall operation (round (* coefC arg)))
  1338. (error))))
  1339. (select-window wind)))
  1340. (defun ediff-scroll-vertically (&optional arg)
  1341. "Vertically scroll buffers A, B \(and C if appropriate\).
  1342. With optional argument ARG, scroll ARG lines; otherwise scroll by nearly
  1343. the one half of the height of window-A."
  1344. (interactive "P")
  1345. (ediff-barf-if-not-control-buffer)
  1346. ;; make sure windows aren't dead
  1347. (if (not (and (window-live-p ediff-window-A) (window-live-p ediff-window-B)))
  1348. (ediff-recenter 'no-rehighlight))
  1349. (if (not (and (ediff-buffer-live-p ediff-buffer-A)
  1350. (ediff-buffer-live-p ediff-buffer-B)
  1351. (or (not ediff-3way-job)
  1352. (ediff-buffer-live-p ediff-buffer-C))
  1353. ))
  1354. (error ediff-KILLED-VITAL-BUFFER))
  1355. (ediff-operate-on-windows
  1356. (if (memq (ediff-last-command-char) '(?v ?\C-v))
  1357. 'scroll-up
  1358. 'scroll-down)
  1359. ;; calculate argument to scroll-up/down
  1360. ;; if there is an explicit argument
  1361. (if (and arg (not (equal arg '-)))
  1362. ;; use it
  1363. (prefix-numeric-value arg)
  1364. ;; if not, see if we can determine a default amount (the window height)
  1365. (let (default-amount)
  1366. (setq default-amount
  1367. (- (/ (min (window-height ediff-window-A)
  1368. (window-height ediff-window-B)
  1369. (if ediff-3way-job
  1370. (window-height ediff-window-C)
  1371. 500)) ; some large number
  1372. 2)
  1373. 1 next-screen-context-lines))
  1374. ;; window found
  1375. (if arg
  1376. ;; C-u as argument means half of default amount
  1377. (/ default-amount 2)
  1378. ;; no argument means default amount
  1379. default-amount)))))
  1380. (defun ediff-scroll-horizontally (&optional arg)
  1381. "Horizontally scroll buffers A, B \(and C if appropriate\).
  1382. If an argument is given, that is how many columns are scrolled, else nearly
  1383. the width of the A/B/C windows."
  1384. (interactive "P")
  1385. (ediff-barf-if-not-control-buffer)
  1386. ;; make sure windows aren't dead
  1387. (if (not (and (window-live-p ediff-window-A) (window-live-p ediff-window-B)))
  1388. (ediff-recenter 'no-rehighlight))
  1389. (if (not (and (ediff-buffer-live-p ediff-buffer-A)
  1390. (ediff-buffer-live-p ediff-buffer-B)
  1391. (or (not ediff-3way-job)
  1392. (ediff-buffer-live-p ediff-buffer-C))
  1393. ))
  1394. (error ediff-KILLED-VITAL-BUFFER))
  1395. (ediff-operate-on-windows
  1396. ;; Arrange for scroll-left and scroll-right being called
  1397. ;; interactively so that they set the window's min_hscroll.
  1398. ;; Otherwise, automatic hscrolling will undo the effect of
  1399. ;; hscrolling.
  1400. (if (= (ediff-last-command-char) ?<)
  1401. (lambda (arg)
  1402. (let ((prefix-arg arg))
  1403. (call-interactively 'scroll-left)))
  1404. (lambda (arg)
  1405. (let ((prefix-arg arg))
  1406. (call-interactively 'scroll-right))))
  1407. ;; calculate argument to scroll-left/right
  1408. ;; if there is an explicit argument
  1409. (if (and arg (not (equal arg '-)))
  1410. ;; use it
  1411. (prefix-numeric-value arg)
  1412. ;; if not, see if we can determine a default amount
  1413. ;; (half the window width)
  1414. (if (null ediff-control-window)
  1415. ;; no control window, use nil
  1416. nil
  1417. (let ((default-amount
  1418. (- (/ (min (window-width ediff-window-A)
  1419. (window-width ediff-window-B)
  1420. (if ediff-3way-comparison-job
  1421. (window-width ediff-window-C)
  1422. 500) ; some large number
  1423. )
  1424. 2)
  1425. 3)))
  1426. ;; window found
  1427. (if arg
  1428. ;; C-u as argument means half of default amount
  1429. (/ default-amount 2)
  1430. ;; no argument means default amount
  1431. default-amount))))))
  1432. ;;BEG, END show the region to be positioned.
  1433. ;;JOB-NAME holds ediff-job-name. The ediff-windows job positions regions
  1434. ;;differently.
  1435. (defun ediff-position-region (beg end pos job-name)
  1436. (if (> end (point-max))
  1437. (setq end (point-max)))
  1438. (if ediff-windows-job
  1439. (if (pos-visible-in-window-p end)
  1440. () ; do nothing, wind is already positioned
  1441. ;; at this point, windows are positioned at the beginning of the
  1442. ;; file regions (not diff-regions) being compared.
  1443. (save-excursion
  1444. (move-to-window-line (- (window-height) 2))
  1445. (let ((amount (+ 2 (count-lines (point) end))))
  1446. (scroll-up amount))))
  1447. (set-window-start (selected-window) beg)
  1448. (if (pos-visible-in-window-p end)
  1449. ;; Determine the number of lines that the region occupies
  1450. (let ((lines 0)
  1451. (prev-point 0))
  1452. (while ( and (> end (progn
  1453. (move-to-window-line lines)
  1454. (point)))
  1455. ;; `end' may be beyond the window bottom, so check
  1456. ;; that we are making progress
  1457. (< prev-point (point)))
  1458. (setq prev-point (point))
  1459. (setq lines (1+ lines)))
  1460. ;; And position the beginning on the right line
  1461. (goto-char beg)
  1462. (recenter (/ (1+ (max (- (1- (window-height (selected-window)))
  1463. lines)
  1464. 1)
  1465. )
  1466. 2))))
  1467. (goto-char pos)
  1468. ))
  1469. ;; get number of lines from window start to region end
  1470. (defun ediff-get-lines-to-region-end (buf-type &optional n ctl-buf)
  1471. (or n (setq n ediff-current-difference))
  1472. (or ctl-buf (setq ctl-buf ediff-control-buffer))
  1473. (ediff-with-current-buffer ctl-buf
  1474. (let* ((buf (ediff-get-buffer buf-type))
  1475. (wind (eval (ediff-get-symbol-from-alist
  1476. buf-type ediff-window-alist)))
  1477. (beg (window-start wind))
  1478. (end (ediff-get-diff-posn buf-type 'end))
  1479. lines)
  1480. (ediff-with-current-buffer buf
  1481. (if (< beg end)
  1482. (setq lines (count-lines beg end))
  1483. (setq lines 0))
  1484. lines
  1485. ))))
  1486. ;; Calculate the number of lines from window end to the start of diff region
  1487. (defun ediff-get-lines-to-region-start (buf-type &optional diff-num ctl-buf)
  1488. (or diff-num (setq diff-num ediff-current-difference))
  1489. (or ctl-buf (setq ctl-buf ediff-control-buffer))
  1490. (ediff-with-current-buffer ctl-buf
  1491. (let* ((buf (ediff-get-buffer buf-type))
  1492. (wind (eval (ediff-get-symbol-from-alist
  1493. buf-type ediff-window-alist)))
  1494. (end (or (window-end wind) (window-end wind t)))
  1495. (beg (ediff-get-diff-posn buf-type 'beg diff-num)))
  1496. (ediff-with-current-buffer buf
  1497. (if (< beg end)
  1498. (count-lines (max beg (point-min)) (min end (point-max))) 0))
  1499. )))
  1500. ;; region size coefficient is a coefficient by which to adjust scrolling
  1501. ;; up/down of the window displaying buffer of type BUFTYPE.
  1502. ;; The purpose of this coefficient is to make the windows scroll in sync, so
  1503. ;; that it won't happen that one diff region is scrolled off while the other is
  1504. ;; still seen.
  1505. ;;
  1506. ;; If the difference region is invalid, the coefficient is 1
  1507. (defun ediff-get-region-size-coefficient (buf-type op &optional n ctl-buf)
  1508. (ediff-with-current-buffer (or ctl-buf ediff-control-buffer)
  1509. (if (ediff-valid-difference-p n)
  1510. (let* ((func (cond ((eq op 'scroll-down)
  1511. 'ediff-get-lines-to-region-start)
  1512. ((eq op 'scroll-up)
  1513. 'ediff-get-lines-to-region-end)
  1514. (t (lambda (a b c) 0))))
  1515. (max-lines (max (funcall func 'A n ctl-buf)
  1516. (funcall func 'B n ctl-buf)
  1517. (if (ediff-buffer-live-p ediff-buffer-C)
  1518. (funcall func 'C n ctl-buf)
  1519. 0))))
  1520. ;; this covers the horizontal coefficient as well:
  1521. ;; if max-lines = 0 then coef = 1
  1522. (if (> max-lines 0)
  1523. (/ (+ (funcall func buf-type n ctl-buf) 0.0)
  1524. (+ max-lines 0.0))
  1525. 1))
  1526. 1)))
  1527. (defun ediff-next-difference (&optional arg)
  1528. "Advance to the next difference.
  1529. With a prefix argument, go forward that many differences."
  1530. (interactive "p")
  1531. (ediff-barf-if-not-control-buffer)
  1532. (if (< ediff-current-difference ediff-number-of-differences)
  1533. (let ((n (min ediff-number-of-differences
  1534. (+ ediff-current-difference (or arg 1))))
  1535. non-clash-skip skip-changed regexp-skip)
  1536. (ediff-visible-region)
  1537. (or (>= n ediff-number-of-differences)
  1538. (setq regexp-skip (funcall ediff-skip-diff-region-function n))
  1539. ;; this won't exec if regexp-skip is t
  1540. (setq non-clash-skip (ediff-merge-region-is-non-clash-to-skip n)
  1541. skip-changed
  1542. (ediff-skip-merge-region-if-changed-from-default-p n))
  1543. (ediff-install-fine-diff-if-necessary n))
  1544. ;; Skip loop
  1545. (while (and (< n ediff-number-of-differences)
  1546. (or
  1547. ;; regexp skip
  1548. regexp-skip
  1549. ;; skip clashes, if necessary
  1550. non-clash-skip
  1551. ;; skip processed regions
  1552. skip-changed
  1553. ;; skip difference regions that differ in white space
  1554. (and ediff-ignore-similar-regions
  1555. (ediff-merge-region-is-non-clash n)
  1556. (or (eq (ediff-no-fine-diffs-p n) t)
  1557. (and (ediff-merge-job)
  1558. (eq (ediff-no-fine-diffs-p n) 'C)))
  1559. )))
  1560. (setq n (1+ n))
  1561. (if (= 0 (mod n 20))
  1562. (message "Skipped over region %d and counting ..." n))
  1563. (or (>= n ediff-number-of-differences)
  1564. (setq regexp-skip (funcall ediff-skip-diff-region-function n))
  1565. ;; this won't exec if regexp-skip is t
  1566. (setq non-clash-skip (ediff-merge-region-is-non-clash-to-skip n)
  1567. skip-changed
  1568. (ediff-skip-merge-region-if-changed-from-default-p n))
  1569. (ediff-install-fine-diff-if-necessary n))
  1570. )
  1571. (message "")
  1572. (ediff-unselect-and-select-difference n)
  1573. ) ; let
  1574. (ediff-visible-region)
  1575. (error "At end of the difference list")))
  1576. (defun ediff-previous-difference (&optional arg)
  1577. "Go to the previous difference.
  1578. With a prefix argument, go back that many differences."
  1579. (interactive "p")
  1580. (ediff-barf-if-not-control-buffer)
  1581. (if (> ediff-current-difference -1)
  1582. (let ((n (max -1 (- ediff-current-difference (or arg 1))))
  1583. non-clash-skip skip-changed regexp-skip)
  1584. (ediff-visible-region)
  1585. (or (< n 0)
  1586. (setq regexp-skip (funcall ediff-skip-diff-region-function n))
  1587. ;; this won't exec if regexp-skip is t
  1588. (setq non-clash-skip (ediff-merge-region-is-non-clash-to-skip n)
  1589. skip-changed
  1590. (ediff-skip-merge-region-if-changed-from-default-p n))
  1591. (ediff-install-fine-diff-if-necessary n))
  1592. (while (and (> n -1)
  1593. (or
  1594. ;; regexp skip
  1595. regexp-skip
  1596. ;; skip clashes, if necessary
  1597. non-clash-skip
  1598. ;; skipp changed regions
  1599. skip-changed
  1600. ;; skip difference regions that differ in white space
  1601. (and ediff-ignore-similar-regions
  1602. (ediff-merge-region-is-non-clash n)
  1603. (or (eq (ediff-no-fine-diffs-p n) t)
  1604. (and (ediff-merge-job)
  1605. (eq (ediff-no-fine-diffs-p n) 'C)))
  1606. )))
  1607. (if (= 0 (mod (1+ n) 20))
  1608. (message "Skipped over region %d and counting ..." (1+ n)))
  1609. (setq n (1- n))
  1610. (or (< n 0)
  1611. (setq regexp-skip (funcall ediff-skip-diff-region-function n))
  1612. ;; this won't exec if regexp-skip is t
  1613. (setq non-clash-skip (ediff-merge-region-is-non-clash-to-skip n)
  1614. skip-changed
  1615. (ediff-skip-merge-region-if-changed-from-default-p n))
  1616. (ediff-install-fine-diff-if-necessary n))
  1617. )
  1618. (message "")
  1619. (ediff-unselect-and-select-difference n)
  1620. ) ; let
  1621. (ediff-visible-region)
  1622. (error "At beginning of the difference list")))
  1623. ;; The diff number is as perceived by the user (i.e., 1+ the internal
  1624. ;; representation)
  1625. (defun ediff-jump-to-difference (difference-number)
  1626. "Go to the difference specified as a prefix argument.
  1627. If the prefix is negative, count differences from the end."
  1628. (interactive "p")
  1629. (ediff-barf-if-not-control-buffer)
  1630. (setq difference-number
  1631. (cond ((< difference-number 0)
  1632. (+ ediff-number-of-differences difference-number))
  1633. ((> difference-number 0) (1- difference-number))
  1634. (t -1)))
  1635. ;; -1 is allowed by ediff-unselect-and-select-difference --- it is the
  1636. ;; position before the first one.
  1637. (if (and (>= difference-number -1)
  1638. (<= difference-number ediff-number-of-differences))
  1639. (ediff-unselect-and-select-difference difference-number)
  1640. (error ediff-BAD-DIFF-NUMBER
  1641. this-command (1+ difference-number) ediff-number-of-differences)))
  1642. (defun ediff-jump-to-difference-at-point (arg)
  1643. "Go to difference closest to the point in buffer A, B, or C.
  1644. The buffer depends on last command character \(a, b, or c\) that invoked this
  1645. command. For instance, if the command was `ga' then the point value in buffer
  1646. A is used.
  1647. With a prefix argument, synchronize all files around the current point position
  1648. in the specified buffer."
  1649. (interactive "P")
  1650. (ediff-barf-if-not-control-buffer)
  1651. (let* ((buf-type (ediff-char-to-buftype (ediff-last-command-char)))
  1652. (buffer (ediff-get-buffer buf-type))
  1653. (pt (ediff-with-current-buffer buffer (point)))
  1654. (diff-no (ediff-diff-at-point buf-type nil (if arg 'after)))
  1655. (past-last-diff (< ediff-number-of-differences diff-no))
  1656. (beg (if past-last-diff
  1657. (ediff-with-current-buffer buffer (point-max))
  1658. (ediff-get-diff-posn buf-type 'beg (1- diff-no))))
  1659. ctl-wind wind-A wind-B wind-C
  1660. shift)
  1661. (if past-last-diff
  1662. (ediff-jump-to-difference -1)
  1663. (ediff-jump-to-difference diff-no))
  1664. (setq ctl-wind (selected-window)
  1665. wind-A ediff-window-A
  1666. wind-B ediff-window-B
  1667. wind-C ediff-window-C)
  1668. (if arg
  1669. (progn
  1670. (ediff-with-current-buffer buffer
  1671. (setq shift (- beg pt)))
  1672. (select-window wind-A)
  1673. (if past-last-diff (goto-char (point-max)))
  1674. (condition-case nil
  1675. (backward-char shift) ; noerror, if beginning of buffer
  1676. (error))
  1677. (recenter)
  1678. (select-window wind-B)
  1679. (if past-last-diff (goto-char (point-max)))
  1680. (condition-case nil
  1681. (backward-char shift) ; noerror, if beginning of buffer
  1682. (error))
  1683. (recenter)
  1684. (if (window-live-p wind-C)
  1685. (progn
  1686. (select-window wind-C)
  1687. (if past-last-diff (goto-char (point-max)))
  1688. (condition-case nil
  1689. (backward-char shift) ; noerror, if beginning of buffer
  1690. (error))
  1691. (recenter)
  1692. ))
  1693. (select-window ctl-wind)
  1694. ))
  1695. ))
  1696. ;; find region most related to the current point position (or POS, if given)
  1697. ;; returns diff number as seen by the user (i.e., 1+ the internal
  1698. ;; representation)
  1699. ;; The optional argument WHICH-DIFF can be `after' or `before'. If `after',
  1700. ;; find the diff after the point. If `before', find the diff before the
  1701. ;; point. If the point is inside a diff, return that diff.
  1702. (defun ediff-diff-at-point (buf-type &optional pos which-diff)
  1703. (let ((buffer (ediff-get-buffer buf-type))
  1704. (ctl-buffer ediff-control-buffer)
  1705. (max-dif-num (1- ediff-number-of-differences))
  1706. (diff-no -1)
  1707. (prev-beg 0)
  1708. (prev-end 0)
  1709. (beg 0)
  1710. (end 0))
  1711. (ediff-with-current-buffer buffer
  1712. (setq pos (or pos (point)))
  1713. (while (and (or (< pos prev-beg) (> pos beg))
  1714. (< diff-no max-dif-num))
  1715. (setq diff-no (1+ diff-no))
  1716. (setq prev-beg beg
  1717. prev-end end)
  1718. (setq beg (ediff-get-diff-posn buf-type 'beg diff-no ctl-buffer)
  1719. end (ediff-get-diff-posn buf-type 'end diff-no ctl-buffer))
  1720. )
  1721. ;; boost diff-no by 1, if past the last diff region
  1722. (if (and (memq which-diff '(after before))
  1723. (> pos beg) (= diff-no max-dif-num))
  1724. (setq diff-no (1+ diff-no)))
  1725. (cond ((eq which-diff 'after) (1+ diff-no))
  1726. ((eq which-diff 'before) diff-no)
  1727. ((< (abs (count-lines pos (max 1 prev-end)))
  1728. (abs (count-lines pos (max 1 beg))))
  1729. diff-no) ; choose prev difference
  1730. (t
  1731. (1+ diff-no))) ; choose next difference
  1732. )))
  1733. ;;; Copying diffs.
  1734. (defun ediff-diff-to-diff (arg &optional keys)
  1735. "Copy buffer-X'th difference region to buffer Y \(X,Y are A, B, or C\).
  1736. If numerical prefix argument, copy the difference specified in the arg.
  1737. Otherwise, copy the difference given by `ediff-current-difference'.
  1738. This command assumes it is bound to a 2-character key sequence, `ab', `ba',
  1739. `ac', etc., which is used to determine the types of buffers to be used for
  1740. copying difference regions. The first character in the sequence specifies
  1741. the source buffer and the second specifies the target.
  1742. If the second optional argument, a 2-character string, is given, use it to
  1743. determine the source and the target buffers instead of the command keys."
  1744. (interactive "P")
  1745. (ediff-barf-if-not-control-buffer)
  1746. (or keys (setq keys (this-command-keys)))
  1747. (if (eq arg '-) (setq arg -1)) ; translate neg arg to -1
  1748. (if (numberp arg) (ediff-jump-to-difference arg))
  1749. (let* ((key1 (aref keys 0))
  1750. (key2 (aref keys 1))
  1751. (char1 (ediff-event-key key1))
  1752. (char2 (ediff-event-key key2))
  1753. ediff-verbose-p)
  1754. (ediff-copy-diff ediff-current-difference
  1755. (ediff-char-to-buftype char1)
  1756. (ediff-char-to-buftype char2))
  1757. ;; recenter with rehighlighting, but no messages
  1758. (ediff-recenter)))
  1759. (defun ediff-copy-A-to-B (arg)
  1760. "Copy ARGth difference region from buffer A to B.
  1761. ARG is a prefix argument. If nil, copy the current difference region."
  1762. (interactive "P")
  1763. (ediff-diff-to-diff arg "ab"))
  1764. (defun ediff-copy-B-to-A (arg)
  1765. "Copy ARGth difference region from buffer B to A.
  1766. ARG is a prefix argument. If nil, copy the current difference region."
  1767. (interactive "P")
  1768. (ediff-diff-to-diff arg "ba"))
  1769. (defun ediff-copy-A-to-C (arg)
  1770. "Copy ARGth difference region from buffer A to buffer C.
  1771. ARG is a prefix argument. If nil, copy the current difference region."
  1772. (interactive "P")
  1773. (ediff-diff-to-diff arg "ac"))
  1774. (defun ediff-copy-B-to-C (arg)
  1775. "Copy ARGth difference region from buffer B to buffer C.
  1776. ARG is a prefix argument. If nil, copy the current difference region."
  1777. (interactive "P")
  1778. (ediff-diff-to-diff arg "bc"))
  1779. (defun ediff-copy-C-to-B (arg)
  1780. "Copy ARGth difference region from buffer C to B.
  1781. ARG is a prefix argument. If nil, copy the current difference region."
  1782. (interactive "P")
  1783. (ediff-diff-to-diff arg "cb"))
  1784. (defun ediff-copy-C-to-A (arg)
  1785. "Copy ARGth difference region from buffer C to A.
  1786. ARG is a prefix argument. If nil, copy the current difference region."
  1787. (interactive "P")
  1788. (ediff-diff-to-diff arg "ca"))
  1789. ;; Copy diff N from FROM-BUF-TYPE \(given as A, B or C\) to TO-BUF-TYPE.
  1790. ;; If optional DO-NOT-SAVE is non-nil, do not save the old value of the
  1791. ;; target diff. This is used in merging, when constructing the merged
  1792. ;; version.
  1793. (defun ediff-copy-diff (n from-buf-type to-buf-type
  1794. &optional batch-invocation reg-to-copy)
  1795. (let* ((to-buf (ediff-get-buffer to-buf-type))
  1796. ;;(from-buf (if (not reg-to-copy) (ediff-get-buffer from-buf-type)))
  1797. (ctrl-buf ediff-control-buffer)
  1798. (saved-p t)
  1799. (three-way ediff-3way-job)
  1800. messg
  1801. ediff-verbose-p
  1802. reg-to-delete reg-to-delete-beg reg-to-delete-end)
  1803. (setq reg-to-delete-beg
  1804. (ediff-get-diff-posn to-buf-type 'beg n ctrl-buf))
  1805. (setq reg-to-delete-end
  1806. (ediff-get-diff-posn to-buf-type 'end n ctrl-buf))
  1807. (if reg-to-copy
  1808. (setq from-buf-type nil)
  1809. (setq reg-to-copy (ediff-get-region-contents n from-buf-type ctrl-buf)))
  1810. (setq reg-to-delete (ediff-get-region-contents
  1811. n to-buf-type ctrl-buf
  1812. reg-to-delete-beg reg-to-delete-end))
  1813. (if (string= reg-to-delete reg-to-copy)
  1814. (setq saved-p nil) ; don't copy identical buffers
  1815. ;; seems ok to copy
  1816. (if (or batch-invocation (ediff-test-save-region n to-buf-type))
  1817. (condition-case conds
  1818. (progn
  1819. (ediff-with-current-buffer to-buf
  1820. ;; to prevent flags from interfering if buffer is writable
  1821. (let ((inhibit-read-only (null buffer-read-only)))
  1822. (goto-char reg-to-delete-end)
  1823. (insert reg-to-copy)
  1824. (if (> reg-to-delete-end reg-to-delete-beg)
  1825. (kill-region reg-to-delete-beg reg-to-delete-end))
  1826. ))
  1827. (or batch-invocation
  1828. (setq
  1829. messg
  1830. (ediff-save-diff-region n to-buf-type reg-to-delete))))
  1831. (error (message "ediff-copy-diff: %s %s"
  1832. (car conds)
  1833. (mapconcat 'prin1-to-string (cdr conds) " "))
  1834. (beep 1)
  1835. (sit-for 2) ; let the user see the error msg
  1836. (setq saved-p nil)
  1837. )))
  1838. )
  1839. ;; adjust state of difference in case 3-way and diff was copied ok
  1840. (if (and saved-p three-way)
  1841. (ediff-set-state-of-diff-in-all-buffers n ctrl-buf))
  1842. (if batch-invocation
  1843. (ediff-clear-fine-differences n)
  1844. ;; If diff3 job, we should recompute fine diffs so we clear them
  1845. ;; before reinserting flags (and thus before ediff-recenter).
  1846. (if (and saved-p three-way)
  1847. (ediff-clear-fine-differences n))
  1848. (ediff-refresh-mode-lines)
  1849. ;; For diff2 jobs, don't recompute fine diffs, since we know there
  1850. ;; aren't any. So we clear diffs after ediff-recenter.
  1851. (if (and saved-p (not three-way))
  1852. (ediff-clear-fine-differences n))
  1853. ;; Make sure that the message about saving and how to restore is seen
  1854. ;; by the user
  1855. (message "%s" messg))
  1856. ))
  1857. ;; Save Nth diff of buffer BUF-TYPE \(A, B, or C\).
  1858. ;; That is to say, the Nth diff on the `ediff-killed-diffs-alist'. REG
  1859. ;; is the region to save. It is redundant here, but is passed anyway, for
  1860. ;; convenience.
  1861. (defun ediff-save-diff-region (n buf-type reg)
  1862. (let* ((n-th-diff-saved (assoc n ediff-killed-diffs-alist))
  1863. (buf (ediff-get-buffer buf-type))
  1864. (this-buf-n-th-diff-saved (assoc buf (cdr n-th-diff-saved))))
  1865. (if this-buf-n-th-diff-saved
  1866. ;; either nothing saved for n-th diff and buffer or we OK'ed
  1867. ;; overriding
  1868. (setcdr this-buf-n-th-diff-saved reg)
  1869. (if n-th-diff-saved ;; n-th diff saved, but for another buffer
  1870. (nconc n-th-diff-saved (list (cons buf reg)))
  1871. (setq ediff-killed-diffs-alist ;; create record for n-th diff
  1872. (cons (list n (cons buf reg))
  1873. ediff-killed-diffs-alist))))
  1874. (message "Saving old diff region #%d of buffer %S. To recover, type `r%s'"
  1875. (1+ n) buf-type
  1876. (if ediff-merge-job
  1877. "" (downcase (symbol-name buf-type))))
  1878. ))
  1879. ;; Test if saving Nth difference region of buffer BUF-TYPE is possible.
  1880. (defun ediff-test-save-region (n buf-type)
  1881. (let* ((n-th-diff-saved (assoc n ediff-killed-diffs-alist))
  1882. (buf (ediff-get-buffer buf-type))
  1883. (this-buf-n-th-diff-saved (assoc buf (cdr n-th-diff-saved))))
  1884. (if this-buf-n-th-diff-saved
  1885. (if (yes-or-no-p
  1886. (format
  1887. "You've previously copied diff region %d to buffer %S. Confirm? "
  1888. (1+ n) buf-type))
  1889. t
  1890. (error "Quit"))
  1891. t)))
  1892. (defun ediff-pop-diff (n buf-type)
  1893. "Pop last killed Nth diff region from buffer BUF-TYPE."
  1894. (let* ((n-th-record (assoc n ediff-killed-diffs-alist))
  1895. (buf (ediff-get-buffer buf-type))
  1896. (saved-rec (assoc buf (cdr n-th-record)))
  1897. (three-way ediff-3way-job)
  1898. (ctl-buf ediff-control-buffer)
  1899. ediff-verbose-p
  1900. saved-diff reg-beg reg-end recovered)
  1901. (if (cdr saved-rec)
  1902. (setq saved-diff (cdr saved-rec))
  1903. (if (> ediff-number-of-differences 0)
  1904. (error "Nothing saved for diff %d in buffer %S" (1+ n) buf-type)
  1905. (error ediff-NO-DIFFERENCES)))
  1906. (setq reg-beg (ediff-get-diff-posn buf-type 'beg n ediff-control-buffer))
  1907. (setq reg-end (ediff-get-diff-posn buf-type 'end n ediff-control-buffer))
  1908. (condition-case conds
  1909. (ediff-with-current-buffer buf
  1910. (let ((inhibit-read-only (null buffer-read-only)))
  1911. (goto-char reg-end)
  1912. (insert saved-diff)
  1913. (if (> reg-end reg-beg)
  1914. (kill-region reg-beg reg-end))
  1915. (setq recovered t)
  1916. ))
  1917. (error (message "ediff-pop-diff: %s %s"
  1918. (car conds)
  1919. (mapconcat 'prin1-to-string (cdr conds) " "))
  1920. (beep 1)))
  1921. ;; Clearing fine diffs is necessary for
  1922. ;; ediff-unselect-and-select-difference to properly recompute them. We
  1923. ;; can't rely on ediff-copy-diff to clear this vector, as the user might
  1924. ;; have modified diff regions after copying and, thus, may have recomputed
  1925. ;; fine diffs.
  1926. (if recovered
  1927. (ediff-clear-fine-differences n))
  1928. ;; adjust state of difference
  1929. (if (and three-way recovered)
  1930. (ediff-set-state-of-diff-in-all-buffers n ctl-buf))
  1931. (ediff-refresh-mode-lines)
  1932. (if recovered
  1933. (progn
  1934. (setq n-th-record (delq saved-rec n-th-record))
  1935. (message "Diff region %d in buffer %S restored" (1+ n) buf-type)
  1936. ))
  1937. ))
  1938. (defun ediff-restore-diff (arg &optional key)
  1939. "Restore ARGth diff from `ediff-killed-diffs-alist'.
  1940. ARG is a prefix argument. If ARG is nil, restore the current-difference.
  1941. If the second optional argument, a character, is given, use it to
  1942. determine the target buffer instead of (ediff-last-command-char)"
  1943. (interactive "P")
  1944. (ediff-barf-if-not-control-buffer)
  1945. (if (numberp arg)
  1946. (ediff-jump-to-difference arg))
  1947. (ediff-pop-diff ediff-current-difference
  1948. (ediff-char-to-buftype (or key (ediff-last-command-char))))
  1949. ;; recenter with rehighlighting, but no messages
  1950. (let (ediff-verbose-p)
  1951. (ediff-recenter)))
  1952. (defun ediff-restore-diff-in-merge-buffer (arg)
  1953. "Restore ARGth diff in the merge buffer.
  1954. ARG is a prefix argument. If nil, restore the current diff."
  1955. (interactive "P")
  1956. (ediff-restore-diff arg ?c))
  1957. (defun ediff-toggle-regexp-match ()
  1958. "Toggle between focusing and hiding of difference regions that match
  1959. a regular expression typed in by the user."
  1960. (interactive)
  1961. (ediff-barf-if-not-control-buffer)
  1962. (let ((regexp-A "")
  1963. (regexp-B "")
  1964. (regexp-C "")
  1965. msg-connective alt-msg-connective alt-connective)
  1966. (cond
  1967. ((or (and (eq ediff-skip-diff-region-function
  1968. ediff-focus-on-regexp-matches-function)
  1969. (eq (ediff-last-command-char) ?f))
  1970. (and (eq ediff-skip-diff-region-function
  1971. ediff-hide-regexp-matches-function)
  1972. (eq (ediff-last-command-char) ?h)))
  1973. (message "Selective browsing by regexp turned off")
  1974. (setq ediff-skip-diff-region-function 'ediff-show-all-diffs))
  1975. ((eq (ediff-last-command-char) ?h)
  1976. (setq ediff-skip-diff-region-function ediff-hide-regexp-matches-function
  1977. regexp-A
  1978. (read-string
  1979. (format
  1980. "Ignore A-regions matching this regexp (default %s): "
  1981. ediff-regexp-hide-A))
  1982. regexp-B
  1983. (read-string
  1984. (format
  1985. "Ignore B-regions matching this regexp (default %s): "
  1986. ediff-regexp-hide-B)))
  1987. (if ediff-3way-comparison-job
  1988. (setq regexp-C
  1989. (read-string
  1990. (format
  1991. "Ignore C-regions matching this regexp (default %s): "
  1992. ediff-regexp-hide-C))))
  1993. (if (eq ediff-hide-regexp-connective 'and)
  1994. (setq msg-connective "BOTH"
  1995. alt-msg-connective "ONE OF"
  1996. alt-connective 'or)
  1997. (setq msg-connective "ONE OF"
  1998. alt-msg-connective "BOTH"
  1999. alt-connective 'and))
  2000. (if (y-or-n-p
  2001. (format
  2002. "Ignore regions that match %s regexps, OK? "
  2003. msg-connective))
  2004. (message "Will ignore regions that match %s regexps" msg-connective)
  2005. (setq ediff-hide-regexp-connective alt-connective)
  2006. (message "Will ignore regions that match %s regexps"
  2007. alt-msg-connective))
  2008. (or (string= regexp-A "") (setq ediff-regexp-hide-A regexp-A))
  2009. (or (string= regexp-B "") (setq ediff-regexp-hide-B regexp-B))
  2010. (or (string= regexp-C "") (setq ediff-regexp-hide-C regexp-C)))
  2011. ((eq (ediff-last-command-char) ?f)
  2012. (setq ediff-skip-diff-region-function
  2013. ediff-focus-on-regexp-matches-function
  2014. regexp-A
  2015. (read-string
  2016. (format
  2017. "Focus on A-regions matching this regexp (default %s): "
  2018. ediff-regexp-focus-A))
  2019. regexp-B
  2020. (read-string
  2021. (format
  2022. "Focus on B-regions matching this regexp (default %s): "
  2023. ediff-regexp-focus-B)))
  2024. (if ediff-3way-comparison-job
  2025. (setq regexp-C
  2026. (read-string
  2027. (format
  2028. "Focus on C-regions matching this regexp (default %s): "
  2029. ediff-regexp-focus-C))))
  2030. (if (eq ediff-focus-regexp-connective 'and)
  2031. (setq msg-connective "BOTH"
  2032. alt-msg-connective "ONE OF"
  2033. alt-connective 'or)
  2034. (setq msg-connective "ONE OF"
  2035. alt-msg-connective "BOTH"
  2036. alt-connective 'and))
  2037. (if (y-or-n-p
  2038. (format
  2039. "Focus on regions that match %s regexps, OK? "
  2040. msg-connective))
  2041. (message "Will focus on regions that match %s regexps"
  2042. msg-connective)
  2043. (setq ediff-focus-regexp-connective alt-connective)
  2044. (message "Will focus on regions that match %s regexps"
  2045. alt-msg-connective))
  2046. (or (string= regexp-A "") (setq ediff-regexp-focus-A regexp-A))
  2047. (or (string= regexp-B "") (setq ediff-regexp-focus-B regexp-B))
  2048. (or (string= regexp-C "") (setq ediff-regexp-focus-C regexp-C))))))
  2049. (defun ediff-toggle-skip-similar ()
  2050. (interactive)
  2051. (ediff-barf-if-not-control-buffer)
  2052. (if (not (eq ediff-auto-refine 'on))
  2053. (error
  2054. "Can't skip over whitespace regions: first turn auto-refining on"))
  2055. (setq ediff-ignore-similar-regions (not ediff-ignore-similar-regions))
  2056. (if ediff-ignore-similar-regions
  2057. (message
  2058. "Skipping regions that differ only in white space & line breaks")
  2059. (message "Skipping over white-space differences turned off")))
  2060. (defun ediff-focus-on-regexp-matches (n)
  2061. "Focus on diffs that match regexp `ediff-regexp-focus-A/B'.
  2062. Regions to be ignored according to this function are those where
  2063. buf A region doesn't match `ediff-regexp-focus-A' and buf B region
  2064. doesn't match `ediff-regexp-focus-B'.
  2065. This function returns nil if the region number N (specified as
  2066. an argument) is not to be ignored and t if region N is to be ignored.
  2067. N is a region number used by Ediff internally. It is 1 less
  2068. the number seen by the user."
  2069. (if (ediff-valid-difference-p n)
  2070. (let* ((ctl-buf ediff-control-buffer)
  2071. (regex-A ediff-regexp-focus-A)
  2072. (regex-B ediff-regexp-focus-B)
  2073. (regex-C ediff-regexp-focus-C)
  2074. (reg-A-match (ediff-with-current-buffer ediff-buffer-A
  2075. (save-restriction
  2076. (narrow-to-region
  2077. (ediff-get-diff-posn 'A 'beg n ctl-buf)
  2078. (ediff-get-diff-posn 'A 'end n ctl-buf))
  2079. (goto-char (point-min))
  2080. (re-search-forward regex-A nil t))))
  2081. (reg-B-match (ediff-with-current-buffer ediff-buffer-B
  2082. (save-restriction
  2083. (narrow-to-region
  2084. (ediff-get-diff-posn 'B 'beg n ctl-buf)
  2085. (ediff-get-diff-posn 'B 'end n ctl-buf))
  2086. (re-search-forward regex-B nil t))))
  2087. (reg-C-match (if ediff-3way-comparison-job
  2088. (ediff-with-current-buffer ediff-buffer-C
  2089. (save-restriction
  2090. (narrow-to-region
  2091. (ediff-get-diff-posn 'C 'beg n ctl-buf)
  2092. (ediff-get-diff-posn 'C 'end n ctl-buf))
  2093. (re-search-forward regex-C nil t))))))
  2094. (not (eval (if ediff-3way-comparison-job
  2095. (list ediff-focus-regexp-connective
  2096. reg-A-match reg-B-match reg-C-match)
  2097. (list ediff-focus-regexp-connective
  2098. reg-A-match reg-B-match))))
  2099. )))
  2100. (defun ediff-hide-regexp-matches (n)
  2101. "Hide diffs that match regexp `ediff-regexp-hide-A/B/C'.
  2102. Regions to be ignored are those where buf A region matches
  2103. `ediff-regexp-hide-A' and buf B region matches `ediff-regexp-hide-B'.
  2104. This function returns nil if the region number N (specified as
  2105. an argument) is not to be ignored and t if region N is to be ignored.
  2106. N is a region number used by Ediff internally. It is 1 less
  2107. the number seen by the user."
  2108. (if (ediff-valid-difference-p n)
  2109. (let* ((ctl-buf ediff-control-buffer)
  2110. (regex-A ediff-regexp-hide-A)
  2111. (regex-B ediff-regexp-hide-B)
  2112. (regex-C ediff-regexp-hide-C)
  2113. (reg-A-match (ediff-with-current-buffer ediff-buffer-A
  2114. (save-restriction
  2115. (narrow-to-region
  2116. (ediff-get-diff-posn 'A 'beg n ctl-buf)
  2117. (ediff-get-diff-posn 'A 'end n ctl-buf))
  2118. (goto-char (point-min))
  2119. (re-search-forward regex-A nil t))))
  2120. (reg-B-match (ediff-with-current-buffer ediff-buffer-B
  2121. (save-restriction
  2122. (narrow-to-region
  2123. (ediff-get-diff-posn 'B 'beg n ctl-buf)
  2124. (ediff-get-diff-posn 'B 'end n ctl-buf))
  2125. (goto-char (point-min))
  2126. (re-search-forward regex-B nil t))))
  2127. (reg-C-match (if ediff-3way-comparison-job
  2128. (ediff-with-current-buffer ediff-buffer-C
  2129. (save-restriction
  2130. (narrow-to-region
  2131. (ediff-get-diff-posn 'C 'beg n ctl-buf)
  2132. (ediff-get-diff-posn 'C 'end n ctl-buf))
  2133. (goto-char (point-min))
  2134. (re-search-forward regex-C nil t))))))
  2135. (eval (if ediff-3way-comparison-job
  2136. (list ediff-hide-regexp-connective
  2137. reg-A-match reg-B-match reg-C-match)
  2138. (list ediff-hide-regexp-connective reg-A-match reg-B-match)))
  2139. )))
  2140. ;;; Quitting, suspending, etc.
  2141. (defun ediff-quit (reverse-default-keep-variants)
  2142. "Finish an Ediff session and exit Ediff.
  2143. Unselects the selected difference, if any, restores the read-only and modified
  2144. flags of the compared file buffers, kills Ediff buffers for this session
  2145. \(but not buffers A, B, C\).
  2146. If `ediff-keep-variants' is nil, the user will be asked whether the buffers
  2147. containing the variants should be removed \(if they haven't been modified\).
  2148. If it is t, they will be preserved unconditionally. A prefix argument,
  2149. temporarily reverses the meaning of this variable."
  2150. (interactive "P")
  2151. (ediff-barf-if-not-control-buffer)
  2152. (let ((ctl-buf (current-buffer))
  2153. (ctl-frm (selected-frame))
  2154. (minibuffer-auto-raise t))
  2155. (if (y-or-n-p (format "Quit this Ediff session%s? "
  2156. (if (ediff-buffer-live-p ediff-meta-buffer)
  2157. " & show containing session group" "")))
  2158. (progn
  2159. (message "")
  2160. (set-buffer ctl-buf)
  2161. (ediff-really-quit reverse-default-keep-variants))
  2162. (select-frame ctl-frm)
  2163. (raise-frame ctl-frm)
  2164. (message ""))))
  2165. ;; Perform the quit operations.
  2166. (defun ediff-really-quit (reverse-default-keep-variants)
  2167. (ediff-unhighlight-diffs-totally)
  2168. (ediff-clear-diff-vector 'ediff-difference-vector-A 'fine-diffs-also)
  2169. (ediff-clear-diff-vector 'ediff-difference-vector-B 'fine-diffs-also)
  2170. (ediff-clear-diff-vector 'ediff-difference-vector-C 'fine-diffs-also)
  2171. (ediff-clear-diff-vector 'ediff-difference-vector-Ancestor 'fine-diffs-also)
  2172. (ediff-delete-temp-files)
  2173. ;; Restore the visibility range. This affects only ediff-*-regions/windows.
  2174. ;; Since for other job names ediff-visible-region sets
  2175. ;; ediff-visible-bounds to ediff-wide-bounds, the settings below are
  2176. ;; ignored for such jobs.
  2177. (if ediff-quit-widened
  2178. (setq ediff-visible-bounds ediff-wide-bounds)
  2179. (setq ediff-visible-bounds ediff-narrow-bounds))
  2180. ;; Apply selective display to narrow or widen
  2181. (ediff-visible-region)
  2182. (mapc (lambda (overl)
  2183. (if (ediff-overlayp overl)
  2184. (ediff-delete-overlay overl)))
  2185. ediff-wide-bounds)
  2186. (mapc (lambda (overl)
  2187. (if (ediff-overlayp overl)
  2188. (ediff-delete-overlay overl)))
  2189. ediff-narrow-bounds)
  2190. ;; restore buffer mode line id's in buffer-A/B/C
  2191. (let ((control-buffer ediff-control-buffer)
  2192. (meta-buffer ediff-meta-buffer)
  2193. (after-quit-hook-internal ediff-after-quit-hook-internal)
  2194. (session-number ediff-meta-session-number)
  2195. ;; suitable working frame
  2196. (warp-frame (if (and (ediff-window-display-p) (eq ediff-grab-mouse t))
  2197. (cond ((window-live-p ediff-window-A)
  2198. (window-frame ediff-window-A))
  2199. ((window-live-p ediff-window-B)
  2200. (window-frame ediff-window-B))
  2201. (t (next-frame))))))
  2202. (condition-case nil
  2203. (ediff-with-current-buffer ediff-buffer-A
  2204. (setq ediff-this-buffer-ediff-sessions
  2205. (delq control-buffer ediff-this-buffer-ediff-sessions))
  2206. (kill-local-variable 'mode-line-buffer-identification)
  2207. (kill-local-variable 'mode-line-format)
  2208. )
  2209. (error))
  2210. (condition-case nil
  2211. (ediff-with-current-buffer ediff-buffer-B
  2212. (setq ediff-this-buffer-ediff-sessions
  2213. (delq control-buffer ediff-this-buffer-ediff-sessions))
  2214. (kill-local-variable 'mode-line-buffer-identification)
  2215. (kill-local-variable 'mode-line-format)
  2216. )
  2217. (error))
  2218. (condition-case nil
  2219. (ediff-with-current-buffer ediff-buffer-C
  2220. (setq ediff-this-buffer-ediff-sessions
  2221. (delq control-buffer ediff-this-buffer-ediff-sessions))
  2222. (kill-local-variable 'mode-line-buffer-identification)
  2223. (kill-local-variable 'mode-line-format)
  2224. )
  2225. (error))
  2226. (condition-case nil
  2227. (ediff-with-current-buffer ediff-ancestor-buffer
  2228. (setq ediff-this-buffer-ediff-sessions
  2229. (delq control-buffer ediff-this-buffer-ediff-sessions))
  2230. (kill-local-variable 'mode-line-buffer-identification)
  2231. (kill-local-variable 'mode-line-format)
  2232. )
  2233. (error))
  2234. (setq ediff-session-registry
  2235. (delq ediff-control-buffer ediff-session-registry))
  2236. (ediff-update-registry)
  2237. ;; restore state of buffers to what it was before ediff
  2238. (ediff-restore-protected-variables)
  2239. ;; If the user interrupts (canceling saving the merge buffer), continue
  2240. ;; normally.
  2241. (condition-case nil
  2242. (if (ediff-merge-job)
  2243. (run-hooks 'ediff-quit-merge-hook))
  2244. (quit))
  2245. (run-hooks 'ediff-cleanup-hook)
  2246. (ediff-janitor
  2247. 'ask
  2248. ;; reverse-default-keep-variants is t if the user quits with a prefix arg
  2249. (if reverse-default-keep-variants
  2250. (not ediff-keep-variants)
  2251. ediff-keep-variants))
  2252. ;; one hook here is ediff-cleanup-mess, which kills the control buffer and
  2253. ;; other auxiliary buffers. we made it into a hook to let the users do their
  2254. ;; own cleanup, if needed.
  2255. (run-hooks 'ediff-quit-hook)
  2256. (ediff-update-meta-buffer meta-buffer nil session-number)
  2257. ;; warp mouse into a working window
  2258. (setq warp-frame ; if mouse is over a reasonable frame, use it
  2259. (cond ((ediff-good-frame-under-mouse))
  2260. (t warp-frame)))
  2261. (if (and (ediff-window-display-p) (frame-live-p warp-frame) ediff-grab-mouse)
  2262. (set-mouse-position (if (featurep 'emacs)
  2263. warp-frame
  2264. (frame-selected-window warp-frame))
  2265. 2 1))
  2266. (run-hooks 'after-quit-hook-internal)
  2267. ))
  2268. ;; Returns frame under mouse, if this frame is not a minibuffer
  2269. ;; frame. Otherwise: nil
  2270. (defun ediff-good-frame-under-mouse ()
  2271. (let ((frame-or-win (car (mouse-position)))
  2272. (buf-name "")
  2273. frame obj-ok)
  2274. (setq obj-ok
  2275. (if (featurep 'emacs)
  2276. (frame-live-p frame-or-win)
  2277. (window-live-p frame-or-win)))
  2278. (if obj-ok
  2279. (setq frame (if (featurep 'emacs) frame-or-win (window-frame frame-or-win))
  2280. buf-name
  2281. (buffer-name (window-buffer (frame-selected-window frame)))))
  2282. (if (string-match "Minibuf" buf-name)
  2283. nil
  2284. frame)))
  2285. (defun ediff-delete-temp-files ()
  2286. (if (and (stringp ediff-temp-file-A) (file-exists-p ediff-temp-file-A))
  2287. (delete-file ediff-temp-file-A))
  2288. (if (and (stringp ediff-temp-file-B) (file-exists-p ediff-temp-file-B))
  2289. (delete-file ediff-temp-file-B))
  2290. (if (and (stringp ediff-temp-file-C) (file-exists-p ediff-temp-file-C))
  2291. (delete-file ediff-temp-file-C)))
  2292. ;; Kill control buffer, other auxiliary Ediff buffers.
  2293. ;; Leave one of the frames split between buffers A/B/C
  2294. (defun ediff-cleanup-mess ()
  2295. (let* ((buff-A ediff-buffer-A)
  2296. (buff-B ediff-buffer-B)
  2297. (buff-C ediff-buffer-C)
  2298. (ctl-buf ediff-control-buffer)
  2299. (ctl-wind (ediff-get-visible-buffer-window ctl-buf))
  2300. (ctl-frame ediff-control-frame)
  2301. (three-way-job ediff-3way-job)
  2302. (main-frame (cond ((window-live-p ediff-window-A)
  2303. (window-frame ediff-window-A))
  2304. ((window-live-p ediff-window-B)
  2305. (window-frame ediff-window-B)))))
  2306. (ediff-kill-buffer-carefully ediff-diff-buffer)
  2307. (ediff-kill-buffer-carefully ediff-custom-diff-buffer)
  2308. (ediff-kill-buffer-carefully ediff-fine-diff-buffer)
  2309. (ediff-kill-buffer-carefully ediff-tmp-buffer)
  2310. (ediff-kill-buffer-carefully ediff-error-buffer)
  2311. (ediff-kill-buffer-carefully ediff-msg-buffer)
  2312. (ediff-kill-buffer-carefully ediff-debug-buffer)
  2313. (if (boundp 'ediff-patch-diagnostics)
  2314. (ediff-kill-buffer-carefully ediff-patch-diagnostics))
  2315. ;; delete control frame or window
  2316. (cond ((and (ediff-window-display-p) (frame-live-p ctl-frame))
  2317. (delete-frame ctl-frame))
  2318. ((window-live-p ctl-wind)
  2319. (delete-window ctl-wind)))
  2320. ;; Hide bottom toolbar. --marcpa
  2321. (if (not (ediff-multiframe-setup-p))
  2322. (ediff-kill-bottom-toolbar))
  2323. (ediff-kill-buffer-carefully ctl-buf)
  2324. (if (frame-live-p main-frame)
  2325. (select-frame main-frame))
  2326. ;; display only if not visible
  2327. (condition-case nil
  2328. (or (ediff-get-visible-buffer-window buff-B)
  2329. (switch-to-buffer buff-B))
  2330. (error))
  2331. (condition-case nil
  2332. (or (ediff-get-visible-buffer-window buff-A)
  2333. (progn
  2334. (if (and (ediff-get-visible-buffer-window buff-B)
  2335. (ediff-buffer-live-p buff-A))
  2336. (funcall ediff-split-window-function))
  2337. (switch-to-buffer buff-A)))
  2338. (error))
  2339. (if three-way-job
  2340. (condition-case nil
  2341. (or (ediff-get-visible-buffer-window buff-C)
  2342. (progn
  2343. (if (and (or (ediff-get-visible-buffer-window buff-A)
  2344. (ediff-get-visible-buffer-window buff-B))
  2345. (ediff-buffer-live-p buff-C))
  2346. (funcall ediff-split-window-function))
  2347. (switch-to-buffer buff-C)))
  2348. (error)))
  2349. (balance-windows)
  2350. (message "")
  2351. ))
  2352. (defun ediff-janitor (ask keep-variants)
  2353. "Kill buffers A, B, and, possibly, C, if these buffers aren't modified.
  2354. In merge jobs, buffer C is not deleted here, but rather according to
  2355. ediff-quit-merge-hook.
  2356. A side effect of cleaning up may be that you should be careful when comparing
  2357. the same buffer in two separate Ediff sessions: quitting one of them might
  2358. delete this buffer in another session as well."
  2359. (ediff-dispose-of-variant-according-to-user
  2360. ediff-buffer-A 'A ask keep-variants)
  2361. (ediff-dispose-of-variant-according-to-user
  2362. ediff-buffer-B 'B ask keep-variants)
  2363. (if ediff-merge-job ; don't del buf C if merging--del ancestor buf instead
  2364. (ediff-dispose-of-variant-according-to-user
  2365. ediff-ancestor-buffer 'Ancestor ask keep-variants)
  2366. (ediff-dispose-of-variant-according-to-user
  2367. ediff-buffer-C 'C ask keep-variants)
  2368. ))
  2369. ;; Kill the variant buffer, according to user directives (ask, kill
  2370. ;; unconditionally, keep)
  2371. ;; BUFF is the buffer, BUFF-TYPE is either 'A, or 'B, 'C, 'Ancestor
  2372. (defun ediff-dispose-of-variant-according-to-user (buff bufftype ask keep-variants)
  2373. ;; if this is indirect buffer, kill it and substitute with direct buf
  2374. (if (and (ediff-buffer-live-p buff)
  2375. (ediff-with-current-buffer buff ediff-temp-indirect-buffer))
  2376. (let ((wind (ediff-get-visible-buffer-window buff))
  2377. (base (buffer-base-buffer buff))
  2378. (modified-p (buffer-modified-p buff)))
  2379. (if (and (window-live-p wind) (ediff-buffer-live-p base))
  2380. (set-window-buffer wind base))
  2381. ;; Kill indirect buffer even if it is modified, because the base buffer
  2382. ;; is still there. Note that if the base buffer is dead then so will be
  2383. ;; the indirect buffer
  2384. (ediff-with-current-buffer buff
  2385. (set-buffer-modified-p nil))
  2386. (ediff-kill-buffer-carefully buff)
  2387. (ediff-with-current-buffer base
  2388. (set-buffer-modified-p modified-p)))
  2389. ;; otherwise, ask or use the value of keep-variants
  2390. (or (not (ediff-buffer-live-p buff))
  2391. keep-variants
  2392. (buffer-modified-p buff)
  2393. (and ask
  2394. (not (y-or-n-p (format "Kill buffer %S [%s]? "
  2395. bufftype (buffer-name buff)))))
  2396. (ediff-kill-buffer-carefully buff))
  2397. ))
  2398. (defun ediff-maybe-save-and-delete-merge (&optional save-and-continue)
  2399. "Default hook to run on quitting a merge job.
  2400. This can also be used to save merge buffer in the middle of an Ediff session.
  2401. If the optional SAVE-AND-CONTINUE argument is non-nil, save merge buffer and
  2402. continue. Otherwise:
  2403. If `ediff-autostore-merges' is nil, this does nothing.
  2404. If it is t, it saves the merge buffer in the file `ediff-merge-store-file'
  2405. or asks the user, if the latter is nil. It then asks the user whether to
  2406. delete the merge buffer.
  2407. If `ediff-autostore-merges' is neither nil nor t, the merge buffer is saved
  2408. only if this merge job is part of a group, i.e., was invoked from within
  2409. `ediff-merge-directories', `ediff-merge-directory-revisions', and such."
  2410. (let ((merge-store-file ediff-merge-store-file)
  2411. (ediff-autostore-merges ; fake ediff-autostore-merges, if necessary
  2412. (if save-and-continue t ediff-autostore-merges)))
  2413. (if ediff-autostore-merges
  2414. (cond ((stringp merge-store-file)
  2415. ;; store, ask to delete
  2416. (ediff-write-merge-buffer-and-maybe-kill
  2417. ediff-buffer-C merge-store-file 'show-file save-and-continue))
  2418. ((eq ediff-autostore-merges t)
  2419. ;; ask for file name
  2420. (setq merge-store-file
  2421. (read-file-name "Save the result of the merge in file: "))
  2422. (ediff-write-merge-buffer-and-maybe-kill
  2423. ediff-buffer-C merge-store-file nil save-and-continue))
  2424. ((and (ediff-buffer-live-p ediff-meta-buffer)
  2425. (ediff-with-current-buffer ediff-meta-buffer
  2426. (ediff-merge-metajob)))
  2427. ;; The parent metajob passed nil as the autostore file.
  2428. nil)))
  2429. ))
  2430. ;; write merge buffer. If the optional argument save-and-continue is non-nil,
  2431. ;; then don't kill the merge buffer
  2432. (defun ediff-write-merge-buffer-and-maybe-kill (buf file
  2433. &optional
  2434. show-file save-and-continue)
  2435. (if (not (eq (find-buffer-visiting file) buf))
  2436. (let ((warn-message
  2437. (format "Another buffer is visiting file %s. Too dangerous to save the merge buffer"
  2438. file)))
  2439. (beep)
  2440. (message "%s" warn-message)
  2441. (with-output-to-temp-buffer ediff-msg-buffer
  2442. (princ "\n\n")
  2443. (princ warn-message)
  2444. (princ "\n\n")
  2445. )
  2446. (sit-for 2))
  2447. (ediff-with-current-buffer buf
  2448. (if (or (not (file-exists-p file))
  2449. (y-or-n-p (format "File %s exists, overwrite? " file)))
  2450. (progn
  2451. ;;(write-region nil nil file)
  2452. (ediff-with-current-buffer buf
  2453. (set-visited-file-name file)
  2454. (save-buffer))
  2455. (if show-file
  2456. (progn
  2457. (message "Merge buffer saved in: %s" file)
  2458. (set-buffer-modified-p nil)
  2459. (sit-for 3)))
  2460. (if (and
  2461. (not save-and-continue)
  2462. (y-or-n-p "Merge buffer saved. Now kill the buffer? "))
  2463. (ediff-kill-buffer-carefully buf)))))
  2464. ))
  2465. ;; The default way of suspending Ediff.
  2466. ;; Buries Ediff buffers, kills all windows.
  2467. (defun ediff-default-suspend-function ()
  2468. (let* ((buf-A ediff-buffer-A)
  2469. (buf-B ediff-buffer-B)
  2470. (buf-C ediff-buffer-C)
  2471. (buf-A-wind (ediff-get-visible-buffer-window buf-A))
  2472. (buf-B-wind (ediff-get-visible-buffer-window buf-B))
  2473. (buf-C-wind (ediff-get-visible-buffer-window buf-C))
  2474. (buf-patch (if (boundp 'ediff-patchbufer) ediff-patchbufer nil))
  2475. (buf-patch-diag (if (boundp 'ediff-patch-diagnostics)
  2476. ediff-patch-diagnostics nil))
  2477. (buf-err ediff-error-buffer)
  2478. (buf-diff ediff-diff-buffer)
  2479. (buf-custom-diff ediff-custom-diff-buffer)
  2480. (buf-fine-diff ediff-fine-diff-buffer))
  2481. ;; hide the control panel
  2482. (if (and (ediff-window-display-p) (frame-live-p ediff-control-frame))
  2483. (iconify-frame ediff-control-frame)
  2484. (bury-buffer))
  2485. (if buf-err (bury-buffer buf-err))
  2486. (if buf-diff (bury-buffer buf-diff))
  2487. (if buf-custom-diff (bury-buffer buf-custom-diff))
  2488. (if buf-fine-diff (bury-buffer buf-fine-diff))
  2489. (if buf-patch (bury-buffer buf-patch))
  2490. (if buf-patch-diag (bury-buffer buf-patch-diag))
  2491. (if (window-live-p buf-A-wind)
  2492. (progn
  2493. (select-window buf-A-wind)
  2494. (delete-other-windows)
  2495. (bury-buffer))
  2496. (if (ediff-buffer-live-p buf-A)
  2497. (progn
  2498. (set-buffer buf-A)
  2499. (bury-buffer))))
  2500. (if (window-live-p buf-B-wind)
  2501. (progn
  2502. (select-window buf-B-wind)
  2503. (delete-other-windows)
  2504. (bury-buffer))
  2505. (if (ediff-buffer-live-p buf-B)
  2506. (progn
  2507. (set-buffer buf-B)
  2508. (bury-buffer))))
  2509. (if (window-live-p buf-C-wind)
  2510. (progn
  2511. (select-window buf-C-wind)
  2512. (delete-other-windows)
  2513. (bury-buffer))
  2514. (if (ediff-buffer-live-p buf-C)
  2515. (progn
  2516. (set-buffer buf-C)
  2517. (bury-buffer))))
  2518. ))
  2519. (defun ediff-suspend ()
  2520. "Suspend Ediff.
  2521. To resume, switch to the appropriate `Ediff Control Panel'
  2522. buffer and then type \\[ediff-recenter]. Ediff will automatically set
  2523. up an appropriate window config."
  2524. (interactive)
  2525. (ediff-barf-if-not-control-buffer)
  2526. (run-hooks 'ediff-suspend-hook)
  2527. (message
  2528. "To resume, type M-x eregistry and select the desired Ediff session"))
  2529. ;; ediff-barf-if-not-control-buffer ensures only called from ediff.
  2530. (declare-function ediff-version "ediff" ())
  2531. (defun ediff-status-info ()
  2532. "Show the names of the buffers or files being operated on by Ediff.
  2533. Hit \\[ediff-recenter] to reset the windows afterward."
  2534. (interactive)
  2535. (ediff-barf-if-not-control-buffer)
  2536. (save-excursion
  2537. (ediff-skip-unsuitable-frames))
  2538. (with-output-to-temp-buffer ediff-msg-buffer
  2539. (ediff-with-current-buffer standard-output
  2540. (fundamental-mode))
  2541. (raise-frame (selected-frame))
  2542. (princ (ediff-version))
  2543. (princ "\n\n")
  2544. (ediff-with-current-buffer ediff-buffer-A
  2545. (if buffer-file-name
  2546. (princ
  2547. (format "File A = %S\n" buffer-file-name))
  2548. (princ
  2549. (format "Buffer A = %S\n" (buffer-name)))))
  2550. (ediff-with-current-buffer ediff-buffer-B
  2551. (if buffer-file-name
  2552. (princ
  2553. (format "File B = %S\n" buffer-file-name))
  2554. (princ
  2555. (format "Buffer B = %S\n" (buffer-name)))))
  2556. (if ediff-3way-job
  2557. (ediff-with-current-buffer ediff-buffer-C
  2558. (if buffer-file-name
  2559. (princ
  2560. (format "File C = %S\n" buffer-file-name))
  2561. (princ
  2562. (format "Buffer C = %S\n" (buffer-name))))))
  2563. (princ (format "Customized diff output %s\n"
  2564. (if (ediff-buffer-live-p ediff-custom-diff-buffer)
  2565. (concat "\tin buffer "
  2566. (buffer-name ediff-custom-diff-buffer))
  2567. " is not available")))
  2568. (princ (format "Plain diff output %s\n"
  2569. (if (ediff-buffer-live-p ediff-diff-buffer)
  2570. (concat "\tin buffer "
  2571. (buffer-name ediff-diff-buffer))
  2572. " is not available")))
  2573. (let* ((A-line (ediff-with-current-buffer ediff-buffer-A
  2574. (1+ (count-lines (point-min) (point)))))
  2575. (B-line (ediff-with-current-buffer ediff-buffer-B
  2576. (1+ (count-lines (point-min) (point)))))
  2577. C-line)
  2578. (princ (format "\Buffer A's point is on line %d\n" A-line))
  2579. (princ (format "Buffer B's point is on line %d\n" B-line))
  2580. (if ediff-3way-job
  2581. (progn
  2582. (setq C-line (ediff-with-current-buffer ediff-buffer-C
  2583. (1+ (count-lines (point-min) (point)))))
  2584. (princ (format "Buffer C's point is on line %d\n" C-line)))))
  2585. (princ (format "\nCurrent difference number = %S\n"
  2586. (cond ((< ediff-current-difference 0) 'start)
  2587. ((>= ediff-current-difference
  2588. ediff-number-of-differences) 'end)
  2589. (t (1+ ediff-current-difference)))))
  2590. (princ
  2591. (format "\n%s regions that differ in white space & line breaks only"
  2592. (if ediff-ignore-similar-regions
  2593. "Ignoring" "Showing")))
  2594. (if (and ediff-merge-job ediff-show-clashes-only)
  2595. (princ
  2596. "\nFocusing on regions where both buffers differ from the ancestor"))
  2597. (if (and ediff-skip-merge-regions-that-differ-from-default ediff-merge-job)
  2598. (princ
  2599. "\nSkipping merge regions that differ from default setting"))
  2600. (cond ((eq ediff-skip-diff-region-function 'ediff-show-all-diffs)
  2601. (princ "\nSelective browsing by regexp is off\n"))
  2602. ((eq ediff-skip-diff-region-function
  2603. ediff-hide-regexp-matches-function)
  2604. (princ
  2605. "\nIgnoring regions that match")
  2606. (princ
  2607. (format
  2608. "\n\t regexp `%s' in buffer A %S\n\t regexp `%s' in buffer B\n"
  2609. ediff-regexp-hide-A ediff-hide-regexp-connective
  2610. ediff-regexp-hide-B)))
  2611. ((eq ediff-skip-diff-region-function
  2612. ediff-focus-on-regexp-matches-function)
  2613. (princ
  2614. "\nFocusing on regions that match")
  2615. (princ
  2616. (format
  2617. "\n\t regexp `%s' in buffer A %S\n\t regexp `%s' in buffer B\n"
  2618. ediff-regexp-focus-A ediff-focus-regexp-connective
  2619. ediff-regexp-focus-B)))
  2620. (t (princ "\nSelective browsing via a user-defined method.\n")))
  2621. (princ
  2622. (format "\nBugs/suggestions: type `%s' while in Ediff Control Panel."
  2623. (substitute-command-keys "\\[ediff-submit-report]")))
  2624. ) ; with output
  2625. (if (frame-live-p ediff-control-frame)
  2626. (ediff-reset-mouse ediff-control-frame))
  2627. (if (window-live-p ediff-control-window)
  2628. (select-window ediff-control-window)))
  2629. ;;; Support routines
  2630. ;; Select a difference by placing the ASCII flags around the appropriate
  2631. ;; group of lines in the A, B buffers
  2632. ;; This may have to be modified for buffer C, when it will be supported.
  2633. (defun ediff-select-difference (n)
  2634. (if (and (ediff-buffer-live-p ediff-buffer-A)
  2635. (ediff-buffer-live-p ediff-buffer-B)
  2636. (ediff-valid-difference-p n))
  2637. (progn
  2638. (cond
  2639. ((and (ediff-has-face-support-p) ediff-use-faces)
  2640. (ediff-highlight-diff n))
  2641. ((eq ediff-highlighting-style 'ascii)
  2642. (ediff-place-flags-in-buffer
  2643. 'A ediff-buffer-A ediff-control-buffer n)
  2644. (ediff-place-flags-in-buffer
  2645. 'B ediff-buffer-B ediff-control-buffer n)
  2646. (if ediff-3way-job
  2647. (ediff-place-flags-in-buffer
  2648. 'C ediff-buffer-C ediff-control-buffer n))
  2649. (if (ediff-buffer-live-p ediff-ancestor-buffer)
  2650. (ediff-place-flags-in-buffer
  2651. 'Ancestor ediff-ancestor-buffer
  2652. ediff-control-buffer n))
  2653. ))
  2654. (ediff-install-fine-diff-if-necessary n)
  2655. ;; set current difference here so the hook will be able to refer to it
  2656. (setq ediff-current-difference n)
  2657. (run-hooks 'ediff-select-hook))))
  2658. ;; Unselect a difference by removing the ASCII flags in the buffers.
  2659. ;; This may have to be modified for buffer C, when it will be supported.
  2660. (defun ediff-unselect-difference (n)
  2661. (if (ediff-valid-difference-p n)
  2662. (progn
  2663. (cond ((and (ediff-has-face-support-p) ediff-use-faces)
  2664. (ediff-unhighlight-diff))
  2665. ((eq ediff-highlighting-style 'ascii)
  2666. (ediff-remove-flags-from-buffer
  2667. ediff-buffer-A
  2668. (ediff-get-diff-overlay n 'A))
  2669. (ediff-remove-flags-from-buffer
  2670. ediff-buffer-B
  2671. (ediff-get-diff-overlay n 'B))
  2672. (if ediff-3way-job
  2673. (ediff-remove-flags-from-buffer
  2674. ediff-buffer-C
  2675. (ediff-get-diff-overlay n 'C)))
  2676. (if (ediff-buffer-live-p ediff-ancestor-buffer)
  2677. (ediff-remove-flags-from-buffer
  2678. ediff-ancestor-buffer
  2679. (ediff-get-diff-overlay n 'Ancestor)))
  2680. ))
  2681. ;; unhighlight fine diffs
  2682. (ediff-set-fine-diff-properties ediff-current-difference 'default)
  2683. (run-hooks 'ediff-unselect-hook))))
  2684. ;; Unselects prev diff and selects a new one, if FLAG has value other than
  2685. ;; 'select-only or 'unselect-only. If FLAG is 'select-only, the
  2686. ;; next difference is selected, but the current selection is not
  2687. ;; unselected. If FLAG is 'unselect-only then the current selection is
  2688. ;; unselected, but the next one is not selected. If NO-RECENTER is non-nil,
  2689. ;; don't recenter buffers after selecting/unselecting.
  2690. (defun ediff-unselect-and-select-difference (n &optional flag no-recenter)
  2691. (let ((ediff-current-difference n))
  2692. (or no-recenter
  2693. (ediff-recenter 'no-rehighlight)))
  2694. (let ((control-buf ediff-control-buffer))
  2695. (unwind-protect
  2696. (progn
  2697. (or (eq flag 'select-only)
  2698. (ediff-unselect-difference ediff-current-difference))
  2699. (or (eq flag 'unselect-only)
  2700. (ediff-select-difference n))
  2701. ;; need to set current diff here even though it is also set in
  2702. ;; ediff-select-difference because ediff-select-difference might not
  2703. ;; be called if unselect-only is specified
  2704. (setq ediff-current-difference n)
  2705. ) ; end protected section
  2706. (ediff-with-current-buffer control-buf (ediff-refresh-mode-lines)))
  2707. ))
  2708. (defun ediff-highlight-diff-in-one-buffer (n buf-type)
  2709. (if (ediff-buffer-live-p (ediff-get-buffer buf-type))
  2710. (let* ((buff (ediff-get-buffer buf-type))
  2711. (last (ediff-with-current-buffer buff (point-max)))
  2712. (begin (ediff-get-diff-posn buf-type 'beg n))
  2713. (end (ediff-get-diff-posn buf-type 'end n))
  2714. (xtra (if (equal begin end) 1 0))
  2715. (end-hilit (min last (+ end xtra)))
  2716. (current-diff-overlay
  2717. (symbol-value
  2718. (ediff-get-symbol-from-alist
  2719. buf-type ediff-current-diff-overlay-alist))))
  2720. (if (featurep 'xemacs)
  2721. (ediff-move-overlay current-diff-overlay begin end-hilit)
  2722. (ediff-move-overlay current-diff-overlay begin end-hilit buff))
  2723. (ediff-overlay-put current-diff-overlay 'priority
  2724. (ediff-highest-priority begin end-hilit buff))
  2725. (ediff-overlay-put current-diff-overlay 'ediff-diff-num n)
  2726. ;; unhighlight the background overlay for diff n so it won't
  2727. ;; interfere with the current diff overlay
  2728. (ediff-set-overlay-face (ediff-get-diff-overlay n buf-type) nil)
  2729. )))
  2730. (defun ediff-unhighlight-diff-in-one-buffer (buf-type)
  2731. (if (ediff-buffer-live-p (ediff-get-buffer buf-type))
  2732. (let ((current-diff-overlay
  2733. (symbol-value
  2734. (ediff-get-symbol-from-alist
  2735. buf-type ediff-current-diff-overlay-alist)))
  2736. (overlay
  2737. (ediff-get-diff-overlay ediff-current-difference buf-type))
  2738. )
  2739. (ediff-move-overlay current-diff-overlay 1 1)
  2740. ;; rehighlight the overlay in the background of the
  2741. ;; current difference region
  2742. (ediff-set-overlay-face
  2743. overlay
  2744. (if (and (ediff-has-face-support-p)
  2745. ediff-use-faces ediff-highlight-all-diffs)
  2746. (ediff-background-face buf-type ediff-current-difference)))
  2747. )))
  2748. (defun ediff-unhighlight-diffs-totally-in-one-buffer (buf-type)
  2749. (ediff-unselect-and-select-difference -1)
  2750. (if (and (ediff-has-face-support-p) ediff-use-faces)
  2751. (let* ((inhibit-quit t)
  2752. (current-diff-overlay-var
  2753. (ediff-get-symbol-from-alist
  2754. buf-type ediff-current-diff-overlay-alist))
  2755. (current-diff-overlay (symbol-value current-diff-overlay-var)))
  2756. (ediff-paint-background-regions 'unhighlight)
  2757. (if (ediff-overlayp current-diff-overlay)
  2758. (ediff-delete-overlay current-diff-overlay))
  2759. (set current-diff-overlay-var nil)
  2760. )))
  2761. (defun ediff-highlight-diff (n)
  2762. "Put face on diff N. Invoked for X displays only."
  2763. (ediff-highlight-diff-in-one-buffer n 'A)
  2764. (ediff-highlight-diff-in-one-buffer n 'B)
  2765. (ediff-highlight-diff-in-one-buffer n 'C)
  2766. (ediff-highlight-diff-in-one-buffer n 'Ancestor)
  2767. )
  2768. (defun ediff-unhighlight-diff ()
  2769. "Remove overlays from buffers A, B, and C."
  2770. (ediff-unhighlight-diff-in-one-buffer 'A)
  2771. (ediff-unhighlight-diff-in-one-buffer 'B)
  2772. (ediff-unhighlight-diff-in-one-buffer 'C)
  2773. (ediff-unhighlight-diff-in-one-buffer 'Ancestor)
  2774. )
  2775. ;; delete highlighting overlays, restore faces to their original form
  2776. (defun ediff-unhighlight-diffs-totally ()
  2777. (ediff-unhighlight-diffs-totally-in-one-buffer 'A)
  2778. (ediff-unhighlight-diffs-totally-in-one-buffer 'B)
  2779. (ediff-unhighlight-diffs-totally-in-one-buffer 'C)
  2780. (ediff-unhighlight-diffs-totally-in-one-buffer 'Ancestor)
  2781. )
  2782. ;; for compatibility
  2783. (defmacro ediff-minibuffer-with-setup-hook (fun &rest body)
  2784. `(if (fboundp 'minibuffer-with-setup-hook)
  2785. (minibuffer-with-setup-hook ,fun ,@body)
  2786. ,@body))
  2787. ;; This is adapted from a similar function in `emerge.el'.
  2788. ;; PROMPT should not have a trailing ': ', so that it can be modified
  2789. ;; according to context.
  2790. ;; If DEFAULT-FILE is set, it should be used as the default value.
  2791. ;; If DEFAULT-DIR is non-nil, use it as the default directory.
  2792. ;; Otherwise, use the value of `default-directory.'
  2793. (defun ediff-read-file-name (prompt default-dir default-file &optional no-dirs)
  2794. ;; hack default-dir if it is not set
  2795. (setq default-dir
  2796. (file-name-as-directory
  2797. (ediff-abbreviate-file-name
  2798. (expand-file-name (or default-dir
  2799. (and default-file
  2800. (file-name-directory default-file))
  2801. default-directory)))))
  2802. ;; strip the directory from default-file
  2803. (if default-file
  2804. (setq default-file (file-name-nondirectory default-file)))
  2805. (if (string= default-file "")
  2806. (setq default-file nil))
  2807. (let ((defaults (and (fboundp 'dired-dwim-target-defaults)
  2808. (dired-dwim-target-defaults
  2809. (and default-file (list default-file))
  2810. default-dir)))
  2811. f)
  2812. (setq f (ediff-minibuffer-with-setup-hook
  2813. (lambda () (when defaults
  2814. (setq minibuffer-default defaults)))
  2815. (read-file-name
  2816. (format "%s%s "
  2817. prompt
  2818. (cond (default-file
  2819. (concat " (default " default-file "):"))
  2820. (t (concat " (default " default-dir "):"))))
  2821. default-dir
  2822. (or default-file default-dir)
  2823. t ; must match, no-confirm
  2824. (if default-file (file-name-directory default-file)))))
  2825. (setq f (expand-file-name f default-dir))
  2826. ;; If user entered a directory name, expand the default file in that
  2827. ;; directory. This allows the user to enter a directory name for the
  2828. ;; B-file and diff against the default-file in that directory instead
  2829. ;; of a DIRED listing!
  2830. (if (and (file-directory-p f) default-file)
  2831. (setq f (expand-file-name
  2832. (file-name-nondirectory default-file) f)))
  2833. (if (and no-dirs (file-directory-p f))
  2834. (error "File %s is a directory" f))
  2835. f))
  2836. ;; If PREFIX is given, then it is used as a prefix for the temp file
  2837. ;; name. Otherwise, `ediff' is used. If FILE is given, use this
  2838. ;; file and don't create a new one.
  2839. ;; In MS-DOS, make sure the prefix isn't too long, or else
  2840. ;; `make-temp-name' isn't guaranteed to return a unique filename.
  2841. ;; Also, save buffer from START to END in the file.
  2842. ;; START defaults to (point-min), END to (point-max)
  2843. (defun ediff-make-temp-file (buff &optional prefix given-file start end)
  2844. (let* ((p (ediff-convert-standard-filename (or prefix "ediff")))
  2845. (short-p p)
  2846. (coding-system-for-write ediff-coding-system-for-write)
  2847. f short-f)
  2848. (if (and (fboundp 'msdos-long-file-names)
  2849. (not (msdos-long-file-names))
  2850. (> (length p) 2))
  2851. (setq short-p (substring p 0 2)))
  2852. (setq f (concat ediff-temp-file-prefix p)
  2853. short-f (concat ediff-temp-file-prefix short-p)
  2854. f (cond (given-file)
  2855. ((find-file-name-handler f 'insert-file-contents)
  2856. ;; to thwart file handlers in write-region, e.g., if file
  2857. ;; name ends with .Z or .gz
  2858. ;; This is needed so that patches produced by ediff will
  2859. ;; have more meaningful names
  2860. (ediff-make-empty-tmp-file short-f))
  2861. (prefix
  2862. ;; Prefix is most often the same as the file name for the
  2863. ;; variant. Here we are trying to use the original file
  2864. ;; name but in the temp directory.
  2865. (ediff-make-empty-tmp-file f 'keep-name))
  2866. (t
  2867. ;; If don't care about name, add some random stuff
  2868. ;; to proposed file name.
  2869. (ediff-make-empty-tmp-file short-f))))
  2870. ;; create the file
  2871. (ediff-with-current-buffer buff
  2872. (write-region (if start start (point-min))
  2873. (if end end (point-max))
  2874. f
  2875. nil ; don't append---erase
  2876. 'no-message)
  2877. (set-file-modes f ediff-temp-file-mode)
  2878. (expand-file-name f))))
  2879. ;; Create a temporary file.
  2880. ;; The returned file name (created by appending some random characters at the
  2881. ;; end of PROPOSED-NAME is guaranteed to point to a newly created empty file.
  2882. ;; This is a replacement for make-temp-name, which eliminates a security hole.
  2883. ;; If KEEP-PROPOSED-NAME isn't nil, try to keep PROPOSED-NAME, unless such file
  2884. ;; already exists.
  2885. ;; It is a modified version of make-temp-file in emacs 20.5
  2886. (defun ediff-make-empty-tmp-file (proposed-name &optional keep-proposed-name)
  2887. (let ((file proposed-name))
  2888. (while (condition-case ()
  2889. (progn
  2890. (if (or (file-exists-p file) (not keep-proposed-name))
  2891. (setq file (make-temp-name proposed-name)))
  2892. ;; the with-temp-buffer thing is a workaround for an XEmacs
  2893. ;; bug: write-region complains that we are trying to visit a
  2894. ;; file in an indirect buffer, failing to notice that the
  2895. ;; VISIT flag is unset and that we are actually writing from a
  2896. ;; string and not from any buffer.
  2897. (with-temp-buffer
  2898. (write-region "" nil file nil 'silent nil 'excl))
  2899. nil)
  2900. (file-already-exists t))
  2901. ;; the file was somehow created by someone else between
  2902. ;; `make-temp-name' and `write-region', let's try again.
  2903. nil)
  2904. file))
  2905. ;; Quote metacharacters (using \) when executing diff in Unix, but not in
  2906. ;; EMX OS/2
  2907. ;;(defun ediff-protect-metachars (str)
  2908. ;; (or (memq system-type '(emx))
  2909. ;; (let ((limit 0))
  2910. ;; (while (string-match ediff-metachars str limit)
  2911. ;; (setq str (concat (substring str 0 (match-beginning 0))
  2912. ;; "\\"
  2913. ;; (substring str (match-beginning 0))))
  2914. ;; (setq limit (1+ (match-end 0))))))
  2915. ;; str)
  2916. ;; Make sure the current buffer (for a file) has the same contents as the
  2917. ;; file on disk, and attempt to remedy the situation if not.
  2918. ;; Signal an error if we can't make them the same, or the user doesn't want
  2919. ;; to do what is necessary to make them the same.
  2920. ;; Also, Ediff always offers to revert obsolete buffers, whether they
  2921. ;; are modified or not.
  2922. (defun ediff-verify-file-buffer (&optional file-magic)
  2923. ;; First check if the file has been modified since the buffer visited it.
  2924. (if (verify-visited-file-modtime (current-buffer))
  2925. (if (buffer-modified-p)
  2926. ;; If buffer is not obsolete and is modified, offer to save
  2927. (if (yes-or-no-p
  2928. (format "Buffer %s has been modified. Save it in file %s? "
  2929. (buffer-name)
  2930. buffer-file-name))
  2931. (condition-case nil
  2932. (save-buffer)
  2933. (error
  2934. (beep)
  2935. (message "Couldn't save %s" buffer-file-name)))
  2936. (error "Buffer is out of sync for file %s" buffer-file-name))
  2937. ;; If buffer is not obsolete and is not modified, do nothing
  2938. nil)
  2939. ;; If buffer is obsolete, offer to revert
  2940. (if (yes-or-no-p
  2941. (format "File %s was modified since visited by buffer %s. REVERT file %s? "
  2942. buffer-file-name
  2943. (buffer-name)
  2944. buffer-file-name))
  2945. (progn
  2946. (if file-magic
  2947. (erase-buffer))
  2948. (revert-buffer t t))
  2949. (error "Buffer out of sync for file %s" buffer-file-name))))
  2950. ;; if there is another buffer visiting the file of the merge buffer, offer to
  2951. ;; save and delete the buffer; else bark
  2952. (defun ediff-verify-file-merge-buffer (file)
  2953. (let ((buff (if (stringp file) (find-buffer-visiting file)))
  2954. warn-message)
  2955. (or (null buff)
  2956. (progn
  2957. (setq warn-message
  2958. (format "Buffer %s is visiting %s. Save and kill the buffer? "
  2959. (buffer-name buff) file))
  2960. (with-output-to-temp-buffer ediff-msg-buffer
  2961. (princ "\n\n")
  2962. (princ warn-message)
  2963. (princ "\n\n"))
  2964. (if (y-or-n-p
  2965. (message "%s" warn-message))
  2966. (with-current-buffer buff
  2967. (save-buffer)
  2968. (kill-buffer (current-buffer)))
  2969. (error "Too dangerous to merge versions of a file visited by another buffer"))))
  2970. ))
  2971. (defun ediff-filename-magic-p (file)
  2972. (or (ediff-file-compressed-p file)
  2973. (ediff-file-remote-p file)))
  2974. (defun ediff-save-buffer (arg)
  2975. "Safe way of saving buffers A, B, C, and the diff output.
  2976. `wa' saves buffer A, `wb' saves buffer B, `wc' saves buffer C,
  2977. and `wd' saves the diff output.
  2978. With prefix argument, `wd' saves plain diff output.
  2979. Without an argument, it saves customized diff argument, if available
  2980. \(and plain output, if customized output was not generated\)."
  2981. (interactive "P")
  2982. (ediff-barf-if-not-control-buffer)
  2983. (ediff-compute-custom-diffs-maybe)
  2984. (ediff-with-current-buffer
  2985. (cond ((memq (ediff-last-command-char) '(?a ?b ?c))
  2986. (ediff-get-buffer
  2987. (ediff-char-to-buftype (ediff-last-command-char))))
  2988. ((eq (ediff-last-command-char) ?d)
  2989. (message "Saving diff output ...")
  2990. (sit-for 1) ; let the user see the message
  2991. (cond ((and arg (ediff-buffer-live-p ediff-diff-buffer))
  2992. ediff-diff-buffer)
  2993. ((ediff-buffer-live-p ediff-custom-diff-buffer)
  2994. ediff-custom-diff-buffer)
  2995. ((ediff-buffer-live-p ediff-diff-buffer)
  2996. ediff-diff-buffer)
  2997. (t (error "Output from `diff' not found"))))
  2998. )
  2999. (let ((window-min-height 2))
  3000. (save-buffer))))
  3001. ;; idea suggested by Hannu Koivisto <azure@iki.fi>
  3002. (defun ediff-clone-buffer-for-region-comparison (buff region-name)
  3003. (let ((cloned-buff (ediff-make-cloned-buffer buff region-name))
  3004. (pop-up-windows t)
  3005. wind
  3006. other-wind
  3007. msg-buf)
  3008. (ediff-with-current-buffer cloned-buff
  3009. (setq ediff-temp-indirect-buffer t))
  3010. (pop-to-buffer cloned-buff)
  3011. (setq wind (ediff-get-visible-buffer-window cloned-buff))
  3012. (select-window wind)
  3013. (delete-other-windows)
  3014. (ediff-activate-mark)
  3015. (split-window-vertically)
  3016. (ediff-select-lowest-window)
  3017. (setq other-wind (selected-window))
  3018. (with-temp-buffer
  3019. (erase-buffer)
  3020. (insert
  3021. (format "\n ******* Mark a region in buffer %s (or confirm the existing one) *******\n"
  3022. (buffer-name cloned-buff)))
  3023. (insert
  3024. (ediff-with-current-buffer buff
  3025. (format "\n\t When done, type %s Use %s to abort\n "
  3026. (ediff-format-bindings-of 'exit-recursive-edit)
  3027. (ediff-format-bindings-of 'abort-recursive-edit))))
  3028. (goto-char (point-min))
  3029. (setq msg-buf (current-buffer))
  3030. (set-window-buffer other-wind msg-buf)
  3031. (shrink-window-if-larger-than-buffer)
  3032. (if (window-live-p wind)
  3033. (select-window wind))
  3034. (condition-case nil
  3035. (recursive-edit)
  3036. (quit
  3037. (ediff-kill-buffer-carefully cloned-buff)))
  3038. )
  3039. cloned-buff))
  3040. (defun ediff-clone-buffer-for-window-comparison (buff wind region-name)
  3041. (let ((cloned-buff (ediff-make-cloned-buffer buff region-name)))
  3042. (ediff-with-current-buffer cloned-buff
  3043. (setq ediff-temp-indirect-buffer t))
  3044. (set-window-buffer wind cloned-buff)
  3045. cloned-buff))
  3046. (defun ediff-clone-buffer-for-current-diff-comparison (buff buf-type reg-name)
  3047. (let ((cloned-buff (ediff-make-cloned-buffer buff reg-name))
  3048. (reg-start (ediff-get-diff-posn buf-type 'beg))
  3049. (reg-end (ediff-get-diff-posn buf-type 'end)))
  3050. (ediff-with-current-buffer cloned-buff
  3051. ;; set region to be the current diff region
  3052. (goto-char reg-start)
  3053. (set-mark reg-end)
  3054. (setq ediff-temp-indirect-buffer t))
  3055. cloned-buff))
  3056. (defun ediff-make-cloned-buffer (buff region-name)
  3057. (ediff-make-indirect-buffer
  3058. buff (generate-new-buffer-name
  3059. (concat (if (stringp buff) buff (buffer-name buff)) region-name))))
  3060. (defun ediff-make-indirect-buffer (base-buf indirect-buf-name)
  3061. (if (featurep 'xemacs)
  3062. (make-indirect-buffer base-buf indirect-buf-name)
  3063. (make-indirect-buffer base-buf indirect-buf-name 'clone)))
  3064. ;; This function operates only from an ediff control buffer
  3065. (defun ediff-compute-custom-diffs-maybe ()
  3066. (let ((buf-A-file-name (buffer-file-name ediff-buffer-A))
  3067. (buf-B-file-name (buffer-file-name ediff-buffer-B))
  3068. file-A file-B)
  3069. (unless (and buf-A-file-name
  3070. (file-exists-p buf-A-file-name)
  3071. (not (ediff-file-remote-p buf-A-file-name)))
  3072. (setq file-A (ediff-make-temp-file ediff-buffer-A)))
  3073. (unless (and buf-B-file-name
  3074. (file-exists-p buf-B-file-name)
  3075. (not (ediff-file-remote-p buf-B-file-name)))
  3076. (setq file-B (ediff-make-temp-file ediff-buffer-B)))
  3077. (or (ediff-buffer-live-p ediff-custom-diff-buffer)
  3078. (setq ediff-custom-diff-buffer
  3079. (get-buffer-create
  3080. (ediff-unique-buffer-name "*ediff-custom-diff" "*"))))
  3081. (ediff-with-current-buffer ediff-custom-diff-buffer
  3082. (setq buffer-read-only nil)
  3083. (erase-buffer))
  3084. (ediff-exec-process
  3085. ediff-custom-diff-program ediff-custom-diff-buffer 'synchronize
  3086. ediff-custom-diff-options
  3087. (or file-A buf-A-file-name)
  3088. (or file-B buf-B-file-name))
  3089. ;; put the diff file in diff-mode, if it is available
  3090. (if (fboundp 'diff-mode)
  3091. (with-current-buffer ediff-custom-diff-buffer
  3092. (diff-mode)))
  3093. (and file-A (file-exists-p file-A) (delete-file file-A))
  3094. (and file-B (file-exists-p file-B) (delete-file file-B))
  3095. ))
  3096. (defun ediff-show-diff-output (arg)
  3097. (interactive "P")
  3098. (ediff-barf-if-not-control-buffer)
  3099. (ediff-compute-custom-diffs-maybe)
  3100. (save-excursion
  3101. (ediff-skip-unsuitable-frames ' ok-unsplittable))
  3102. (let ((buf (cond ((and arg (ediff-buffer-live-p ediff-diff-buffer))
  3103. ediff-diff-buffer)
  3104. ((ediff-buffer-live-p ediff-custom-diff-buffer)
  3105. ediff-custom-diff-buffer)
  3106. ((ediff-buffer-live-p ediff-diff-buffer)
  3107. ediff-diff-buffer)
  3108. (t
  3109. (beep)
  3110. (message "Output from `diff' not found")
  3111. nil))))
  3112. (if buf
  3113. (progn
  3114. (ediff-with-current-buffer buf
  3115. (goto-char (point-min)))
  3116. (switch-to-buffer buf)
  3117. (raise-frame (selected-frame)))))
  3118. (if (frame-live-p ediff-control-frame)
  3119. (ediff-reset-mouse ediff-control-frame))
  3120. (if (window-live-p ediff-control-window)
  3121. (select-window ediff-control-window)))
  3122. (defun ediff-inferior-compare-regions ()
  3123. "Compare regions in an active Ediff session.
  3124. Like ediff-regions-linewise but is called from under an active Ediff session on
  3125. the files that belong to that session.
  3126. After quitting the session invoked via this function, type C-l to the parent
  3127. Ediff Control Panel to restore highlighting."
  3128. (interactive)
  3129. (let ((answer "")
  3130. (possibilities (list ?A ?B ?C))
  3131. (zmacs-regions t)
  3132. use-current-diff-p
  3133. begA begB endA endB bufA bufB)
  3134. (if (ediff-valid-difference-p ediff-current-difference)
  3135. (progn
  3136. (ediff-set-fine-diff-properties ediff-current-difference 'default)
  3137. (ediff-unhighlight-diff)))
  3138. (ediff-paint-background-regions 'unhighlight)
  3139. (cond ((ediff-merge-job)
  3140. (setq bufB ediff-buffer-C)
  3141. ;; ask which buffer to compare to the merge buffer
  3142. (while (cond ((eq answer ?A)
  3143. (setq bufA ediff-buffer-A
  3144. possibilities '(?B))
  3145. nil)
  3146. ((eq answer ?B)
  3147. (setq bufA ediff-buffer-B
  3148. possibilities '(?A))
  3149. nil)
  3150. ((equal answer ""))
  3151. (t (beep 1)
  3152. (message "Valid values are A or B")
  3153. (sit-for 2)
  3154. t))
  3155. (let ((cursor-in-echo-area t))
  3156. (message
  3157. "Which buffer to compare to the merge buffer (A or B)? ")
  3158. (setq answer (capitalize (read-char-exclusive))))))
  3159. ((ediff-3way-comparison-job)
  3160. ;; ask which two buffers to compare
  3161. (while (cond ((memq answer possibilities)
  3162. (setq possibilities (delq answer possibilities))
  3163. (setq bufA
  3164. (eval
  3165. (ediff-get-symbol-from-alist
  3166. answer ediff-buffer-alist)))
  3167. nil)
  3168. ((equal answer ""))
  3169. (t (beep 1)
  3170. (message
  3171. "Valid values are %s"
  3172. (mapconcat 'char-to-string possibilities " or "))
  3173. (sit-for 2)
  3174. t))
  3175. (let ((cursor-in-echo-area t))
  3176. (message "Enter the 1st buffer you want to compare (%s): "
  3177. (mapconcat 'char-to-string possibilities " or "))
  3178. (setq answer (capitalize (read-char-exclusive)))))
  3179. (setq answer "") ; silence error msg
  3180. (while (cond ((memq answer possibilities)
  3181. (setq possibilities (delq answer possibilities))
  3182. (setq bufB
  3183. (eval
  3184. (ediff-get-symbol-from-alist
  3185. answer ediff-buffer-alist)))
  3186. nil)
  3187. ((equal answer ""))
  3188. (t (beep 1)
  3189. (message
  3190. "Valid values are %s"
  3191. (mapconcat 'char-to-string possibilities " or "))
  3192. (sit-for 2)
  3193. t))
  3194. (let ((cursor-in-echo-area t))
  3195. (message "Enter the 2nd buffer you want to compare (%s): "
  3196. (mapconcat 'char-to-string possibilities "/"))
  3197. (setq answer (capitalize (read-char-exclusive))))))
  3198. (t ; 2way comparison
  3199. (setq bufA ediff-buffer-A
  3200. bufB ediff-buffer-B
  3201. possibilities nil)))
  3202. (if (and (ediff-valid-difference-p ediff-current-difference)
  3203. (y-or-n-p "Compare currently highlighted difference regions? "))
  3204. (setq use-current-diff-p t))
  3205. (setq bufA (if use-current-diff-p
  3206. (ediff-clone-buffer-for-current-diff-comparison
  3207. bufA 'A "-Region.A-")
  3208. (ediff-clone-buffer-for-region-comparison bufA "-Region.A-")))
  3209. (ediff-with-current-buffer bufA
  3210. (setq begA (region-beginning)
  3211. endA (region-end))
  3212. (goto-char begA)
  3213. (beginning-of-line)
  3214. (setq begA (point))
  3215. (goto-char endA)
  3216. (end-of-line)
  3217. (or (eobp) (forward-char)) ; include the newline char
  3218. (setq endA (point)))
  3219. (setq bufB (if use-current-diff-p
  3220. (ediff-clone-buffer-for-current-diff-comparison
  3221. bufB 'B "-Region.B-")
  3222. (ediff-clone-buffer-for-region-comparison bufB "-Region.B-")))
  3223. (ediff-with-current-buffer bufB
  3224. (setq begB (region-beginning)
  3225. endB (region-end))
  3226. (goto-char begB)
  3227. (beginning-of-line)
  3228. (setq begB (point))
  3229. (goto-char endB)
  3230. (end-of-line)
  3231. (or (eobp) (forward-char)) ; include the newline char
  3232. (setq endB (point)))
  3233. (ediff-regions-internal
  3234. bufA begA endA bufB begB endB
  3235. nil ; setup-hook
  3236. (if use-current-diff-p ; job name
  3237. 'ediff-regions-wordwise
  3238. 'ediff-regions-linewise)
  3239. (if use-current-diff-p ; word mode, if diffing current diff
  3240. t nil)
  3241. ;; setup param to pass to ediff-setup
  3242. (list (cons 'ediff-split-window-function ediff-split-window-function)))
  3243. ))
  3244. (defun ediff-remove-flags-from-buffer (buffer overlay)
  3245. (ediff-with-current-buffer buffer
  3246. (let ((inhibit-read-only t))
  3247. (if (featurep 'xemacs)
  3248. (ediff-overlay-put overlay 'begin-glyph nil)
  3249. (ediff-overlay-put overlay 'before-string nil))
  3250. (if (featurep 'xemacs)
  3251. (ediff-overlay-put overlay 'end-glyph nil)
  3252. (ediff-overlay-put overlay 'after-string nil))
  3253. )))
  3254. (defun ediff-place-flags-in-buffer (buf-type buffer ctl-buffer diff)
  3255. (ediff-with-current-buffer buffer
  3256. (ediff-place-flags-in-buffer1 buf-type ctl-buffer diff)))
  3257. (defun ediff-place-flags-in-buffer1 (buf-type ctl-buffer diff-no)
  3258. (let* ((curr-overl (ediff-with-current-buffer ctl-buffer
  3259. (ediff-get-diff-overlay diff-no buf-type)))
  3260. (before (ediff-get-diff-posn buf-type 'beg diff-no ctl-buffer))
  3261. after beg-of-line flag)
  3262. ;; insert flag before the difference
  3263. (goto-char before)
  3264. (setq beg-of-line (bolp))
  3265. (setq flag (ediff-with-current-buffer ctl-buffer
  3266. (if (eq ediff-highlighting-style 'ascii)
  3267. (if beg-of-line
  3268. ediff-before-flag-bol ediff-before-flag-mol))))
  3269. ;; insert the flag itself
  3270. (if (featurep 'xemacs)
  3271. (ediff-overlay-put curr-overl 'begin-glyph flag)
  3272. (ediff-overlay-put curr-overl 'before-string flag))
  3273. ;; insert the flag after the difference
  3274. ;; `after' must be set here, after the before-flag was inserted
  3275. (setq after (ediff-get-diff-posn buf-type 'end diff-no ctl-buffer))
  3276. (goto-char after)
  3277. (setq beg-of-line (bolp))
  3278. (setq flag (ediff-with-current-buffer ctl-buffer
  3279. (if (eq ediff-highlighting-style 'ascii)
  3280. (if beg-of-line
  3281. ediff-after-flag-eol ediff-after-flag-mol))))
  3282. ;; insert the flag itself
  3283. (if (featurep 'xemacs)
  3284. (ediff-overlay-put curr-overl 'end-glyph flag)
  3285. (ediff-overlay-put curr-overl 'after-string flag))
  3286. ))
  3287. ;;; Some diff region tests
  3288. ;; t if diff region is empty.
  3289. ;; In case of buffer C, t also if it is not a 3way
  3290. ;; comparison job (merging jobs return t as well).
  3291. (defun ediff-empty-diff-region-p (n buf-type)
  3292. (if (eq buf-type 'C)
  3293. (or (not ediff-3way-comparison-job)
  3294. (= (ediff-get-diff-posn 'C 'beg n)
  3295. (ediff-get-diff-posn 'C 'end n)))
  3296. (= (ediff-get-diff-posn buf-type 'beg n)
  3297. (ediff-get-diff-posn buf-type 'end n))))
  3298. ;; Test if diff region is white space only.
  3299. ;; If 2-way job and buf-type = C, then returns t.
  3300. (defun ediff-whitespace-diff-region-p (n buf-type)
  3301. (or (and (eq buf-type 'C) (not ediff-3way-job))
  3302. (ediff-empty-diff-region-p n buf-type)
  3303. (let ((beg (ediff-get-diff-posn buf-type 'beg n))
  3304. (end (ediff-get-diff-posn buf-type 'end n)))
  3305. (ediff-with-current-buffer (ediff-get-buffer buf-type)
  3306. (save-excursion
  3307. (goto-char beg)
  3308. (skip-chars-forward ediff-whitespace)
  3309. (>= (point) end))))))
  3310. (defun ediff-get-region-contents (n buf-type ctrl-buf &optional start end)
  3311. (ediff-with-current-buffer
  3312. (ediff-with-current-buffer ctrl-buf (ediff-get-buffer buf-type))
  3313. (buffer-substring
  3314. (or start (ediff-get-diff-posn buf-type 'beg n ctrl-buf))
  3315. (or end (ediff-get-diff-posn buf-type 'end n ctrl-buf)))))
  3316. ;; Returns positions of difference sectors in the BUF-TYPE buffer.
  3317. ;; BUF-TYPE should be a symbol -- `A', `B', or `C'.
  3318. ;; POS is either `beg' or `end'--it specifies whether you want the position at
  3319. ;; the beginning of a difference or at the end.
  3320. ;;
  3321. ;; The optional argument N says which difference (default:
  3322. ;; `ediff-current-difference'). N is the internal difference number (1- what
  3323. ;; the user sees). The optional argument CONTROL-BUF says
  3324. ;; which control buffer is in effect in case it is not the current
  3325. ;; buffer.
  3326. (defun ediff-get-diff-posn (buf-type pos &optional n control-buf)
  3327. (let (diff-overlay)
  3328. (or control-buf
  3329. (setq control-buf (current-buffer)))
  3330. (ediff-with-current-buffer control-buf
  3331. (or n (setq n ediff-current-difference))
  3332. (if (or (< n 0) (>= n ediff-number-of-differences))
  3333. (if (> ediff-number-of-differences 0)
  3334. (error ediff-BAD-DIFF-NUMBER
  3335. this-command (1+ n) ediff-number-of-differences)
  3336. (error ediff-NO-DIFFERENCES)))
  3337. (setq diff-overlay (ediff-get-diff-overlay n buf-type)))
  3338. (if (not (ediff-buffer-live-p (ediff-overlay-buffer diff-overlay)))
  3339. (error ediff-KILLED-VITAL-BUFFER))
  3340. (if (eq pos 'beg)
  3341. (ediff-overlay-start diff-overlay)
  3342. (ediff-overlay-end diff-overlay))
  3343. ))
  3344. ;; Restore highlighting to what it should be according to ediff-use-faces,
  3345. ;; ediff-highlighting-style, and ediff-highlight-all-diffs variables.
  3346. (defun ediff-restore-highlighting (&optional ctl-buf)
  3347. (ediff-with-current-buffer (or ctl-buf (current-buffer))
  3348. (if (and (ediff-has-face-support-p)
  3349. ediff-use-faces
  3350. ediff-highlight-all-diffs)
  3351. (ediff-paint-background-regions))
  3352. (ediff-select-difference ediff-current-difference)))
  3353. ;; null out difference overlays so they won't slow down future
  3354. ;; editing operations
  3355. ;; VEC is either a difference vector or a fine-diff vector
  3356. (defun ediff-clear-diff-vector (vec-var &optional fine-diffs-also)
  3357. (if (vectorp (symbol-value vec-var))
  3358. (mapc (lambda (elt)
  3359. (ediff-delete-overlay
  3360. (ediff-get-diff-overlay-from-diff-record elt))
  3361. (if fine-diffs-also
  3362. (ediff-clear-fine-diff-vector elt))
  3363. )
  3364. (symbol-value vec-var)))
  3365. ;; allow them to be garbage collected
  3366. (set vec-var nil))
  3367. ;;; Misc
  3368. ;; In Emacs, this just makes overlay. In the future, when Emacs will start
  3369. ;; supporting sticky overlays, this function will make a sticky overlay.
  3370. ;; BEG and END are expressions telling where overlay starts.
  3371. ;; If they are numbers or buffers, then all is well. Otherwise, they must
  3372. ;; be expressions to be evaluated in buffer BUF in order to get the overlay
  3373. ;; bounds.
  3374. ;; If BUFF is not a live buffer, then return nil; otherwise, return the
  3375. ;; newly created overlay.
  3376. (defun ediff-make-bullet-proof-overlay (beg end buff)
  3377. (if (ediff-buffer-live-p buff)
  3378. (let (overl)
  3379. (ediff-with-current-buffer buff
  3380. (or (number-or-marker-p beg)
  3381. (setq beg (eval beg)))
  3382. (or (number-or-marker-p end)
  3383. (setq end (eval end)))
  3384. (setq overl
  3385. (if (featurep 'xemacs)
  3386. (make-extent beg end buff)
  3387. ;; advance front and rear of the overlay
  3388. (make-overlay beg end buff nil 'rear-advance)))
  3389. ;; never detach
  3390. (ediff-overlay-put
  3391. overl (if (featurep 'emacs) 'evaporate 'detachable) nil)
  3392. ;; make overlay open-ended
  3393. ;; In emacs, it is made open ended at creation time
  3394. (when (featurep 'xemacs)
  3395. (ediff-overlay-put overl 'start-open nil)
  3396. (ediff-overlay-put overl 'end-open nil))
  3397. (ediff-overlay-put overl 'ediff-diff-num 0)
  3398. overl))))
  3399. (defun ediff-make-current-diff-overlay (type)
  3400. (if (ediff-has-face-support-p)
  3401. (let ((overlay (ediff-get-symbol-from-alist
  3402. type ediff-current-diff-overlay-alist))
  3403. (buffer (ediff-get-buffer type))
  3404. (face (ediff-get-symbol-from-alist
  3405. type ediff-current-diff-face-alist)))
  3406. (set overlay
  3407. (ediff-make-bullet-proof-overlay (point-max) (point-max) buffer))
  3408. (ediff-set-overlay-face (symbol-value overlay) face)
  3409. (ediff-overlay-put (symbol-value overlay) 'ediff ediff-control-buffer))
  3410. ))
  3411. ;; Like other-buffer, but prefers visible buffers and ignores temporary or
  3412. ;; other insignificant buffers (those beginning with "^[ *]").
  3413. ;; Gets one arg--buffer name or a list of buffer names (it won't return
  3414. ;; these buffers).
  3415. ;; EXCL-BUFF-LIST is an exclusion list.
  3416. (defun ediff-other-buffer (excl-buff-lst)
  3417. (or (listp excl-buff-lst) (setq excl-buff-lst (list excl-buff-lst)))
  3418. (let* ((all-buffers (nconc (ediff-get-selected-buffers) (buffer-list)))
  3419. ;; we compute this the second time because we need to do memq on it
  3420. ;; later, and nconc above will break it. Either this or use slow
  3421. ;; append instead of nconc
  3422. (selected-buffers (ediff-get-selected-buffers))
  3423. (preferred-buffer (car all-buffers))
  3424. visible-dired-buffers
  3425. (excl-buff-name-list
  3426. (mapcar
  3427. (lambda (b) (cond ((stringp b) b)
  3428. ((bufferp b) (buffer-name b))))
  3429. excl-buff-lst))
  3430. ;; if at least one buffer on the exclusion list is dired, then force
  3431. ;; all others to be dired. This is because this means that the user
  3432. ;; has already chosen a dired buffer before
  3433. (use-dired-major-mode
  3434. (cond ((null (ediff-buffer-live-p (car excl-buff-lst))) 'unknown)
  3435. ((eq (ediff-with-current-buffer (car excl-buff-lst) major-mode)
  3436. 'dired-mode)
  3437. 'yes)
  3438. (t 'no)))
  3439. ;; significant-buffers must be visible and not belong
  3440. ;; to the exclusion list `buff-list'
  3441. ;; We also exclude temporary buffers, but keep mail and gnus buffers
  3442. ;; Furthermore, we exclude dired buffers, unless they are the only
  3443. ;; ones visible (and there are at least two of them).
  3444. ;; Also, any visible window not on the exclusion list that is first in
  3445. ;; the buffer list is chosen regardless. (This is because the user
  3446. ;; clicked on it or did something to distinguish it).
  3447. (significant-buffers
  3448. (mapcar
  3449. (lambda (x)
  3450. (cond ((member (buffer-name x) excl-buff-name-list) nil)
  3451. ((memq x selected-buffers) x)
  3452. ((not (ediff-get-visible-buffer-window x)) nil)
  3453. ((eq x preferred-buffer) x)
  3454. ;; if prev selected buffer is dired, look only at
  3455. ;; dired.
  3456. ((eq use-dired-major-mode 'yes)
  3457. (if (eq (ediff-with-current-buffer x major-mode)
  3458. 'dired-mode)
  3459. x nil))
  3460. ((eq (ediff-with-current-buffer x major-mode)
  3461. 'dired-mode)
  3462. (if (null use-dired-major-mode)
  3463. ;; don't know if we must enforce dired.
  3464. ;; Remember this buffer in case
  3465. ;; dired buffs are the only ones visible.
  3466. (setq visible-dired-buffers
  3467. (cons x visible-dired-buffers)))
  3468. ;; skip, if dired is not forced
  3469. nil)
  3470. ((memq (ediff-with-current-buffer x major-mode)
  3471. '(rmail-mode
  3472. vm-mode
  3473. gnus-article-mode
  3474. mh-show-mode))
  3475. x)
  3476. ((string-match "^[ *]" (buffer-name x)) nil)
  3477. ((string= "*scratch*" (buffer-name x)) nil)
  3478. (t x)))
  3479. all-buffers))
  3480. (clean-significant-buffers (delq nil significant-buffers))
  3481. less-significant-buffers)
  3482. (if (and (null clean-significant-buffers)
  3483. (> (length visible-dired-buffers) 0))
  3484. (setq clean-significant-buffers visible-dired-buffers))
  3485. (cond (clean-significant-buffers (car clean-significant-buffers))
  3486. ;; try also buffers that are not displayed in windows
  3487. ((setq less-significant-buffers
  3488. (delq nil
  3489. (mapcar
  3490. (lambda (x)
  3491. (cond ((member (buffer-name x) excl-buff-name-list)
  3492. nil)
  3493. ((eq use-dired-major-mode 'yes)
  3494. (if (eq (ediff-with-current-buffer
  3495. x major-mode)
  3496. 'dired-mode)
  3497. x nil))
  3498. ((eq (ediff-with-current-buffer x major-mode)
  3499. 'dired-mode)
  3500. nil)
  3501. ((string-match "^[ *]" (buffer-name x)) nil)
  3502. ((string= "*scratch*" (buffer-name x)) nil)
  3503. (t x)))
  3504. all-buffers)))
  3505. (car less-significant-buffers))
  3506. (t "*scratch*"))
  3507. ))
  3508. ;; If current buffer is a Buffer-menu buffer, then take the selected buffers
  3509. ;; and append the buffer at the cursor to the end.
  3510. ;; This list would be the preferred list.
  3511. (defun ediff-get-selected-buffers ()
  3512. (if (eq major-mode 'Buffer-menu-mode)
  3513. (let ((lis (condition-case nil
  3514. (list (Buffer-menu-buffer t))
  3515. (error))
  3516. ))
  3517. (save-excursion
  3518. (goto-char (point-max))
  3519. (while (search-backward "\n>" nil t)
  3520. (forward-char 1)
  3521. (setq lis (cons (Buffer-menu-buffer t) lis)))
  3522. lis))
  3523. ))
  3524. ;; Construct a unique buffer name.
  3525. ;; The first one tried is prefixsuffix, then prefix<2>suffix,
  3526. ;; prefix<3>suffix, etc.
  3527. (defun ediff-unique-buffer-name (prefix suffix)
  3528. (if (null (get-buffer (concat prefix suffix)))
  3529. (concat prefix suffix)
  3530. (let ((n 2))
  3531. (while (get-buffer (format "%s<%d>%s" prefix n suffix))
  3532. (setq n (1+ n)))
  3533. (format "%s<%d>%s" prefix n suffix))))
  3534. (defun ediff-submit-report ()
  3535. "Submit bug report on Ediff."
  3536. (interactive)
  3537. (ediff-barf-if-not-control-buffer)
  3538. (let ((reporter-prompt-for-summary-p t)
  3539. (ctl-buf ediff-control-buffer)
  3540. (ediff-device-type (ediff-device-type))
  3541. varlist salutation buffer-name)
  3542. (setq varlist '(ediff-diff-program ediff-diff-options
  3543. ediff-diff3-program ediff-diff3-options
  3544. ediff-patch-program ediff-patch-options
  3545. ediff-shell
  3546. ediff-use-faces
  3547. ediff-auto-refine ediff-highlighting-style
  3548. ediff-buffer-A ediff-buffer-B ediff-control-buffer
  3549. ediff-forward-word-function
  3550. ediff-control-frame
  3551. ediff-control-frame-parameters
  3552. ediff-control-frame-position-function
  3553. ediff-prefer-iconified-control-frame
  3554. ediff-window-setup-function
  3555. ediff-split-window-function
  3556. ediff-job-name
  3557. ediff-word-mode
  3558. buffer-name
  3559. ediff-device-type
  3560. ))
  3561. (setq salutation "
  3562. Congratulations! You may have unearthed a bug in Ediff!
  3563. Please make a concise and accurate summary of what happened
  3564. and mail it to the address above.
  3565. -----------------------------------------------------------
  3566. ")
  3567. (ediff-skip-unsuitable-frames)
  3568. (ediff-reset-mouse)
  3569. (switch-to-buffer ediff-msg-buffer)
  3570. (erase-buffer)
  3571. (delete-other-windows)
  3572. (insert "
  3573. Please read this first:
  3574. ----------------------
  3575. Some ``bugs'' may actually be no bugs at all. For instance, if you are
  3576. reporting that certain difference regions are not matched as you think they
  3577. should, this is most likely due to the way Unix diff program decides what
  3578. constitutes a difference region. Ediff is an Emacs interface to diff, and
  3579. it has nothing to do with those decisions---it only takes the output from
  3580. diff and presents it in a way that is better suited for human browsing and
  3581. manipulation.
  3582. If Emacs happens to dump core, this is NOT an Ediff problem---it is
  3583. an Emacs bug. Report this to Emacs maintainers.
  3584. Another popular topic for reports is compilation messages. Because Ediff
  3585. interfaces to several other packages and runs under Emacs and XEmacs,
  3586. byte-compilation may produce output like this:
  3587. While compiling toplevel forms in file ediff.el:
  3588. ** reference to free variable pm-color-alist
  3589. ........................
  3590. While compiling the end of the data:
  3591. ** The following functions are not known to be defined:
  3592. xxx, yyy
  3593. ........................
  3594. These are NOT errors, but inevitable warnings, which ought to be ignored.
  3595. Please do not report those and similar things. However, comments and
  3596. suggestions are always welcome.
  3597. Mail anyway? (y or n) ")
  3598. (if (y-or-n-p "Mail anyway? ")
  3599. (progn
  3600. (if (ediff-buffer-live-p ctl-buf)
  3601. (set-buffer ctl-buf))
  3602. (setq buffer-name (buffer-name))
  3603. (require 'reporter)
  3604. (reporter-submit-bug-report "kifer@cs.stonybrook.edu"
  3605. (ediff-version)
  3606. varlist
  3607. nil
  3608. 'delete-other-windows
  3609. salutation))
  3610. (bury-buffer)
  3611. (beep 1)(message "Bug report aborted")
  3612. (if (ediff-buffer-live-p ctl-buf)
  3613. (ediff-with-current-buffer ctl-buf
  3614. (ediff-recenter 'no-rehighlight))))
  3615. ))
  3616. ;; Find an appropriate syntax table for everyone to use
  3617. ;; If buffer B is not fundamental or text mode, use its syntax table
  3618. ;; Otherwise, use buffer B's.
  3619. ;; The syntax mode is used in ediff-forward-word-function
  3620. ;; The important thing is that every buffer should use the same syntax table
  3621. ;; during the refinement operation
  3622. (defun ediff-choose-syntax-table ()
  3623. (setq ediff-syntax-table
  3624. (ediff-with-current-buffer ediff-buffer-A
  3625. (if (not (memq major-mode
  3626. '(fundamental-mode text-mode indented-text-mode)))
  3627. (syntax-table))))
  3628. (if (not ediff-syntax-table)
  3629. (setq ediff-syntax-table
  3630. (ediff-with-current-buffer ediff-buffer-B
  3631. (syntax-table))))
  3632. )
  3633. (defun ediff-deactivate-mark ()
  3634. (if (featurep 'xemacs)
  3635. (zmacs-deactivate-region)
  3636. (deactivate-mark)))
  3637. (defun ediff-activate-mark ()
  3638. (if (featurep 'xemacs)
  3639. (zmacs-activate-region)
  3640. (make-local-variable 'transient-mark-mode)
  3641. (setq mark-active t transient-mark-mode t)))
  3642. (defun ediff-nuke-selective-display ()
  3643. (if (featurep 'xemacs)
  3644. (nuke-selective-display)
  3645. (save-excursion
  3646. (save-restriction
  3647. (widen)
  3648. (goto-char (point-min))
  3649. (let ((mod-p (buffer-modified-p))
  3650. buffer-read-only end)
  3651. (and (eq t selective-display)
  3652. (while (search-forward "\^M" nil t)
  3653. (end-of-line)
  3654. (setq end (point))
  3655. (beginning-of-line)
  3656. (while (search-forward "\^M" end t)
  3657. (delete-char -1)
  3658. (insert "\^J"))))
  3659. (set-buffer-modified-p mod-p)
  3660. (setq selective-display nil))))))
  3661. ;; The next two are modified versions from emerge.el.
  3662. ;; VARS must be a list of symbols
  3663. ;; ediff-save-variables returns an association list: ((var . val) ...)
  3664. (defsubst ediff-save-variables (vars)
  3665. (mapcar (lambda (v) (cons v (symbol-value v)))
  3666. vars))
  3667. ;; VARS is a list of variable symbols.
  3668. (defun ediff-restore-variables (vars assoc-list)
  3669. (while vars
  3670. (set (car vars) (cdr (assoc (car vars) assoc-list)))
  3671. (setq vars (cdr vars))))
  3672. (defun ediff-change-saved-variable (var value buf-type)
  3673. (let* ((assoc-list
  3674. (symbol-value (ediff-get-symbol-from-alist
  3675. buf-type
  3676. ediff-buffer-values-orig-alist)))
  3677. (assoc-elt (assoc var assoc-list)))
  3678. (if assoc-elt
  3679. (setcdr assoc-elt value))))
  3680. ;; must execute in control buf
  3681. (defun ediff-save-protected-variables ()
  3682. (setq ediff-buffer-values-orig-A
  3683. (ediff-with-current-buffer ediff-buffer-A
  3684. (ediff-save-variables ediff-protected-variables)))
  3685. (setq ediff-buffer-values-orig-B
  3686. (ediff-with-current-buffer ediff-buffer-B
  3687. (ediff-save-variables ediff-protected-variables)))
  3688. (if ediff-3way-comparison-job
  3689. (setq ediff-buffer-values-orig-C
  3690. (ediff-with-current-buffer ediff-buffer-C
  3691. (ediff-save-variables ediff-protected-variables))))
  3692. (if (ediff-buffer-live-p ediff-ancestor-buffer)
  3693. (setq ediff-buffer-values-orig-Ancestor
  3694. (ediff-with-current-buffer ediff-ancestor-buffer
  3695. (ediff-save-variables ediff-protected-variables)))))
  3696. ;; must execute in control buf
  3697. (defun ediff-restore-protected-variables ()
  3698. (let ((values-A ediff-buffer-values-orig-A)
  3699. (values-B ediff-buffer-values-orig-B)
  3700. (values-C ediff-buffer-values-orig-C)
  3701. (values-Ancestor ediff-buffer-values-orig-Ancestor))
  3702. (ediff-with-current-buffer ediff-buffer-A
  3703. (ediff-restore-variables ediff-protected-variables values-A))
  3704. (ediff-with-current-buffer ediff-buffer-B
  3705. (ediff-restore-variables ediff-protected-variables values-B))
  3706. (if ediff-3way-comparison-job
  3707. (ediff-with-current-buffer ediff-buffer-C
  3708. (ediff-restore-variables ediff-protected-variables values-C)))
  3709. (if (ediff-buffer-live-p ediff-ancestor-buffer)
  3710. (ediff-with-current-buffer ediff-ancestor-buffer
  3711. (ediff-restore-variables ediff-protected-variables values-Ancestor)))
  3712. ))
  3713. ;; save BUFFER in FILE. used in hooks.
  3714. (defun ediff-save-buffer-in-file (buffer file)
  3715. (ediff-with-current-buffer buffer
  3716. (write-file file)))
  3717. ;;; Debug
  3718. (ediff-defvar-local ediff-command-begin-time '(0 0 0) "")
  3719. ;; calculate time used by command
  3720. (defun ediff-calc-command-time ()
  3721. (or (equal ediff-command-begin-time '(0 0 0))
  3722. (message "Elapsed time: %g second(s)"
  3723. (float-time (time-since ediff-command-begin-time)))))
  3724. (defsubst ediff-save-time ()
  3725. (setq ediff-command-begin-time (current-time)))
  3726. (defun ediff-profile ()
  3727. "Toggle profiling Ediff commands."
  3728. (interactive)
  3729. (ediff-barf-if-not-control-buffer)
  3730. (if (featurep 'xemacs)
  3731. (make-local-hook 'post-command-hook))
  3732. (let ((pre-hook 'pre-command-hook)
  3733. (post-hook 'post-command-hook))
  3734. (if (not (equal ediff-command-begin-time '(0 0 0)))
  3735. (progn (remove-hook pre-hook 'ediff-save-time)
  3736. (remove-hook post-hook 'ediff-calc-command-time)
  3737. (setq ediff-command-begin-time '(0 0 0))
  3738. (message "Ediff profiling disabled"))
  3739. (add-hook pre-hook 'ediff-save-time t 'local)
  3740. (add-hook post-hook 'ediff-calc-command-time nil 'local)
  3741. (message "Ediff profiling enabled"))))
  3742. (defun ediff-print-diff-vector (diff-vector-var)
  3743. (princ (format "\n*** %S ***\n" diff-vector-var))
  3744. (mapcar (lambda (overl-vec)
  3745. (princ
  3746. (format
  3747. "Diff %d: \tOverlay: %S
  3748. \t\tFine diffs: %s
  3749. \t\tNo-fine-diff-flag: %S
  3750. \t\tState-of-diff:\t %S
  3751. \t\tState-of-merge:\t %S
  3752. "
  3753. (1+ (ediff-overlay-get (aref overl-vec 0) 'ediff-diff-num))
  3754. (aref overl-vec 0)
  3755. ;; fine-diff-vector
  3756. (if (= (length (aref overl-vec 1)) 0)
  3757. "none\n"
  3758. (mapconcat 'prin1-to-string
  3759. (aref overl-vec 1) "\n\t\t\t "))
  3760. (aref overl-vec 2) ; no fine diff flag
  3761. (aref overl-vec 3) ; state-of-diff
  3762. (aref overl-vec 4) ; state-of-merge
  3763. )))
  3764. (eval diff-vector-var)))
  3765. (defun ediff-debug-info ()
  3766. (interactive)
  3767. (ediff-barf-if-not-control-buffer)
  3768. (with-output-to-temp-buffer ediff-debug-buffer
  3769. (ediff-with-current-buffer standard-output
  3770. (fundamental-mode))
  3771. (princ (format "\nCtl buffer: %S\n" ediff-control-buffer))
  3772. (ediff-print-diff-vector (intern "ediff-difference-vector-A"))
  3773. (ediff-print-diff-vector (intern "ediff-difference-vector-B"))
  3774. (ediff-print-diff-vector (intern "ediff-difference-vector-C"))
  3775. (ediff-print-diff-vector (intern "ediff-difference-vector-Ancestor"))
  3776. ))
  3777. ;;; General utilities
  3778. ;; this uses comparison-func to decide who is a member
  3779. (defun ediff-member (elt lis comparison-func)
  3780. (while (and lis (not (funcall comparison-func (car lis) elt)))
  3781. (setq lis (cdr lis)))
  3782. lis)
  3783. ;; Make a readable representation of the invocation sequence for FUNC-DEF.
  3784. ;; It would either be a key or M-x something.
  3785. (defun ediff-format-bindings-of (func-def)
  3786. (let ((desc (car (where-is-internal func-def
  3787. overriding-local-map
  3788. nil nil))))
  3789. (if desc
  3790. (key-description desc)
  3791. (format "M-x %s" func-def))))
  3792. ;; this uses comparison-func to decide who is a member, and this determines how
  3793. ;; intersection looks like
  3794. (defun ediff-intersection (lis1 lis2 comparison-func)
  3795. (let ((result (list 'a)))
  3796. (while lis1
  3797. (if (ediff-member (car lis1) lis2 comparison-func)
  3798. (nconc result (list (car lis1))))
  3799. (setq lis1 (cdr lis1)))
  3800. (cdr result)))
  3801. ;; eliminates duplicates using comparison-func
  3802. (defun ediff-union (lis1 lis2 comparison-func)
  3803. (let ((result (list 'a)))
  3804. (while lis1
  3805. (or (ediff-member (car lis1) (cdr result) comparison-func)
  3806. (nconc result (list (car lis1))))
  3807. (setq lis1 (cdr lis1)))
  3808. (while lis2
  3809. (or (ediff-member (car lis2) (cdr result) comparison-func)
  3810. (nconc result (list (car lis2))))
  3811. (setq lis2 (cdr lis2)))
  3812. (cdr result)))
  3813. ;; eliminates duplicates using comparison-func
  3814. (defun ediff-set-difference (lis1 lis2 comparison-func)
  3815. (let ((result (list 'a)))
  3816. (while lis1
  3817. (or (ediff-member (car lis1) (cdr result) comparison-func)
  3818. (ediff-member (car lis1) lis2 comparison-func)
  3819. (nconc result (list (car lis1))))
  3820. (setq lis1 (cdr lis1)))
  3821. (cdr result)))
  3822. (defun ediff-add-to-history (history-var newelt)
  3823. (if (fboundp 'add-to-history)
  3824. (add-to-history history-var newelt)
  3825. (set history-var (cons newelt (symbol-value history-var)))))
  3826. (defalias 'ediff-copy-list 'copy-sequence)
  3827. ;; don't report error if version control package wasn't found
  3828. ;;(ediff-load-version-control 'silent)
  3829. (run-hooks 'ediff-load-hook)
  3830. ;; Local Variables:
  3831. ;; eval: (put 'ediff-defvar-local 'lisp-indent-hook 'defun)
  3832. ;; eval: (put 'ediff-with-current-buffer 'lisp-indent-hook 1)
  3833. ;; eval: (put 'ediff-with-current-buffer 'edebug-form-spec '(form body))
  3834. ;; End:
  3835. ;;; ediff-util.el ends here