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