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