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