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