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