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