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