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