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