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