* lisp/emacs-lisp/lisp.el (up-list): Fix forward movement.
[emacs.git] / lisp / vc / emerge.el
blob5435a840ac9b1ddb6c86ab100e9db17bd25500f7
1 ;;; emerge.el --- merge diffs under Emacs control
3 ;;; The author has placed this file in the public domain.
5 ;; This file is part of GNU Emacs.
7 ;; Author: Dale R. Worley <worley@world.std.com>
8 ;; Keywords: unix, vc, tools
10 ;; This software was created by Dale R. Worley and is
11 ;; distributed free of charge. It is placed in the public domain and
12 ;; permission is granted to anyone to use, duplicate, modify and redistribute
13 ;; it provided that this notice is attached.
15 ;; Dale R. Worley provides absolutely NO WARRANTY OF ANY KIND
16 ;; with respect to this software. The entire risk as to the quality and
17 ;; performance of this software is with the user. IN NO EVENT WILL DALE
18 ;; R. WORLEY BE LIABLE TO ANYONE FOR ANY DAMAGES ARISING OUT THE
19 ;; USE OF THIS SOFTWARE, INCLUDING, WITHOUT LIMITATION, DAMAGES RESULTING FROM
20 ;; LOST DATA OR LOST PROFITS, OR FOR ANY SPECIAL, INCIDENTAL OR CONSEQUENTIAL
21 ;; DAMAGES.
23 ;;; Commentary:
25 ;;; Code:
27 ;; There aren't really global variables, just dynamic bindings
28 (defvar A-begin)
29 (defvar A-end)
30 (defvar B-begin)
31 (defvar B-end)
32 (defvar diff-vector)
33 (defvar merge-begin)
34 (defvar merge-end)
35 (defvar valid-diff)
37 ;;; Macros
39 (defmacro emerge-defvar-local (var value doc)
40 "Defines SYMBOL as an advertised variable.
41 Performs a defvar, then executes `make-variable-buffer-local' on
42 the variable. Also sets the `preserved' property, so that
43 `kill-all-local-variables' (called by major-mode setting commands)
44 won't destroy Emerge control variables."
45 `(progn
46 (defvar ,var ,value ,doc)
47 (make-variable-buffer-local ',var)
48 (put ',var 'preserved t)))
50 ;; Add entries to minor-mode-alist so that emerge modes show correctly
51 (defvar emerge-minor-modes-list
52 '((emerge-mode " Emerge")
53 (emerge-fast-mode " F")
54 (emerge-edit-mode " E")
55 (emerge-auto-advance " A")
56 (emerge-skip-prefers " S")))
57 (if (not (assq 'emerge-mode minor-mode-alist))
58 (setq minor-mode-alist (append emerge-minor-modes-list
59 minor-mode-alist)))
61 ;; We need to define this function so describe-mode can describe Emerge mode.
62 (defun emerge-mode ()
63 "Emerge mode is used by the Emerge file-merging package.
64 It is entered only through one of the functions:
65 `emerge-files'
66 `emerge-files-with-ancestor'
67 `emerge-buffers'
68 `emerge-buffers-with-ancestor'
69 `emerge-files-command'
70 `emerge-files-with-ancestor-command'
71 `emerge-files-remote'
72 `emerge-files-with-ancestor-remote'
74 Commands:
75 \\{emerge-basic-keymap}
76 Commands must be prefixed by \\<emerge-fast-keymap>\\[emerge-basic-keymap] in `edit' mode,
77 but can be invoked directly in `fast' mode.")
79 (define-obsolete-variable-alias 'emerge-version 'emacs-version "23.2")
81 (defun emerge-version ()
82 "Return string describing the version of Emerge.
83 When called interactively, displays the version."
84 (interactive)
85 (if (called-interactively-p 'interactive)
86 (message "Emerge version %s" emacs-version)
87 emacs-version))
89 (make-obsolete 'emerge-version 'emacs-version "23.2")
91 ;;; Emerge configuration variables
93 (defgroup emerge nil
94 "Merge diffs under Emacs control."
95 :group 'tools)
97 ;; Commands that produce difference files
98 ;; All that can be configured is the name of the programs to execute
99 ;; (emerge-diff-program and emerge-diff3-program) and the options
100 ;; to be provided (emerge-diff-options). The order in which the file names
101 ;; are given is fixed.
102 ;; The file names are always expanded (see expand-file-name) before being
103 ;; passed to diff, thus they need not be invoked under a shell that
104 ;; understands `~'.
105 ;; The code which processes the diff/diff3 output depends on all the
106 ;; finicky details of their output, including the somewhat strange
107 ;; way they number lines of a file.
108 (defcustom emerge-diff-program "diff"
109 "Name of the program which compares two files."
110 :type 'string
111 :group 'emerge)
112 (defcustom emerge-diff3-program "diff3"
113 "Name of the program which compares three files.
114 Its arguments are the ancestor file and the two variant files."
115 :type 'string
116 :group 'emerge)
117 (defcustom emerge-diff-options ""
118 "Options to pass to `emerge-diff-program' and `emerge-diff3-program'."
119 :type 'string
120 :group 'emerge)
121 (defcustom emerge-match-diff-line
122 (let ((x "\\([0-9]+\\)\\(\\|,\\([0-9]+\\)\\)"))
123 (concat "^" x "\\([acd]\\)" x "$"))
124 "Pattern to match lines produced by diff that describe differences.
125 This is as opposed to lines from the source files."
126 :type 'regexp
127 :group 'emerge)
128 (defcustom emerge-diff-ok-lines-regexp
129 "^\\([0-9,]+[acd][0-9,]+$\\|[<>] \\|---\\)"
130 "Regexp that matches normal output lines from `emerge-diff-program'.
131 Lines that do not match are assumed to be error messages."
132 :type 'regexp
133 :group 'emerge)
134 (defcustom emerge-diff3-ok-lines-regexp
135 "^\\([1-3]:\\|====\\| \\)"
136 "Regexp that matches normal output lines from `emerge-diff3-program'.
137 Lines that do not match are assumed to be error messages."
138 :type 'regexp
139 :group 'emerge)
141 (defcustom emerge-rcs-ci-program "ci"
142 "Name of the program that checks in RCS revisions."
143 :type 'string
144 :group 'emerge)
145 (defcustom emerge-rcs-co-program "co"
146 "Name of the program that checks out RCS revisions."
147 :type 'string
148 :group 'emerge)
150 (defcustom emerge-process-local-variables nil
151 "Non-nil if Emerge should process local-variables lists in merge buffers.
152 \(You can explicitly request processing the local-variables
153 by executing `(hack-local-variables)'.)"
154 :type 'boolean
155 :group 'emerge)
156 (defcustom emerge-execute-line-deletions nil
157 "If non-nil: `emerge-execute-line' makes no output if an input was deleted.
158 It concludes that an input version has been deleted when an ancestor entry
159 is present, only one A or B entry is present, and an output entry is present.
160 If nil: In such circumstances, the A or B file that is present will be
161 copied to the designated output file."
162 :type 'boolean
163 :group 'emerge)
165 (defcustom emerge-before-flag "vvvvvvvvvvvvvvvvvvvv\n"
166 "Flag placed above the highlighted block of code. Must end with newline.
167 Must be set before Emerge is loaded, or emerge-new-flags must be run
168 after setting."
169 :type 'string
170 :group 'emerge)
171 (defcustom emerge-after-flag "^^^^^^^^^^^^^^^^^^^^\n"
172 "Flag placed below the highlighted block of code. Must end with newline.
173 Must be set before Emerge is loaded, or emerge-new-flags must be run
174 after setting."
175 :type 'string
176 :group 'emerge)
178 ;; Hook variables
180 (defcustom emerge-startup-hook nil
181 "Hook to run in the merge buffer after the merge has been set up."
182 :type 'hook
183 :group 'emerge)
184 (defcustom emerge-select-hook nil
185 "Hook to run after a difference has been selected.
186 The variable `n' holds the (internal) number of the difference."
187 :type 'hook
188 :group 'emerge)
189 (defcustom emerge-unselect-hook nil
190 "Hook to run after a difference has been unselected.
191 The variable `n' holds the (internal) number of the difference."
192 :type 'hook
193 :group 'emerge)
195 ;; Variables to control the default directories of the arguments to
196 ;; Emerge commands.
198 (defcustom emerge-default-last-directories nil
199 "If nil, default dir for filenames in emerge is `default-directory'.
200 If non-nil, filenames complete in the directory of the last argument of the
201 same type to an `emerge-files...' command."
202 :type 'boolean
203 :group 'emerge)
205 (defvar emerge-last-dir-A nil
206 "Last directory for the first file of an `emerge-files...' command.")
207 (defvar emerge-last-dir-B nil
208 "Last directory for the second file of an `emerge-files...' command.")
209 (defvar emerge-last-dir-ancestor nil
210 "Last directory for the ancestor file of an `emerge-files...' command.")
211 (defvar emerge-last-dir-output nil
212 "Last directory for the output file of an `emerge-files...' command.")
213 (defvar emerge-last-revision-A nil
214 "Last RCS revision used for first file of an `emerge-revisions...' command.")
215 (defvar emerge-last-revision-B nil
216 "Last RCS revision used for second file of an `emerge-revisions...' command.")
217 (defvar emerge-last-revision-ancestor nil
218 "Last RCS revision used for ancestor file of an `emerge-revisions...' command.")
220 (defvar emerge-before-flag-length)
221 (defvar emerge-before-flag-lines)
222 (defvar emerge-before-flag-match)
223 (defvar emerge-after-flag-length)
224 (defvar emerge-after-flag-lines)
225 (defvar emerge-after-flag-match)
226 (defvar emerge-diff-buffer)
227 (defvar emerge-diff-error-buffer)
228 (defvar emerge-prefix-argument)
229 (defvar emerge-file-out)
230 (defvar emerge-exit-func)
231 (defvar emerge-globalized-difference-list)
232 (defvar emerge-globalized-number-of-differences)
234 ;; The flags used to mark differences in the buffers.
236 ;; These function definitions need to be up here, because they are used
237 ;; during loading.
238 (defun emerge-new-flags ()
239 "Function to be called after `emerge-{before,after}-flag'.
240 This is called after these functions are changed to compute values that
241 depend on the flags."
242 (setq emerge-before-flag-length (length emerge-before-flag))
243 (setq emerge-before-flag-lines
244 (emerge-count-matches-string emerge-before-flag "\n"))
245 (setq emerge-before-flag-match (regexp-quote emerge-before-flag))
246 (setq emerge-after-flag-length (length emerge-after-flag))
247 (setq emerge-after-flag-lines
248 (emerge-count-matches-string emerge-after-flag "\n"))
249 (setq emerge-after-flag-match (regexp-quote emerge-after-flag)))
251 (defun emerge-count-matches-string (string regexp)
252 "Return the number of matches in STRING for REGEXP."
253 (let ((i 0)
254 (count 0))
255 (while (string-match regexp string i)
256 (setq count (1+ count))
257 (setq i (match-end 0)))
258 count))
260 ;; Calculate dependent variables
261 (emerge-new-flags)
263 (defcustom emerge-min-visible-lines 3
264 "Number of lines that we want to show above and below the flags when we are
265 displaying a difference."
266 :type 'integer
267 :group 'emerge)
269 (defcustom emerge-temp-file-prefix
270 (expand-file-name "emerge" temporary-file-directory)
271 "Prefix to put on Emerge temporary file names.
272 Do not start with `~/' or `~USERNAME/'."
273 :type 'string
274 :group 'emerge)
276 (defcustom emerge-temp-file-mode 384 ; u=rw only
277 "Mode for Emerge temporary files."
278 :type 'integer
279 :group 'emerge)
281 (defcustom emerge-combine-versions-template
282 "#ifdef NEW\n%b#else /* not NEW */\n%a#endif /* not NEW */\n"
283 "Template for `emerge-combine-versions' to combine the two versions.
284 The template is inserted as a string, with the following interpolations:
285 %a the A version of the difference
286 %b the B version of the difference
287 %% the character `%'
288 Don't forget to end the template with a newline.
289 Note that this variable can be made local to a particular merge buffer by
290 giving a prefix argument to `emerge-set-combine-versions-template'."
291 :type 'string
292 :group 'emerge)
294 ;; Build keymaps
296 (defvar emerge-basic-keymap nil
297 "Keymap of Emerge commands.
298 Directly available in `fast' mode;
299 must be prefixed by \\<emerge-fast-keymap>\\[emerge-basic-keymap] in `edit' mode.")
301 (defvar emerge-fast-keymap nil
302 "Local keymap used in Emerge `fast' mode.
303 Makes Emerge commands directly available.")
305 (defvar emerge-options-menu
306 (make-sparse-keymap "Options"))
308 (defvar emerge-merge-menu
309 (make-sparse-keymap "Merge"))
311 (defvar emerge-move-menu
312 (make-sparse-keymap "Move"))
314 (defcustom emerge-command-prefix "\C-c\C-c"
315 "Command prefix for Emerge commands in `edit' mode.
316 Must be set before Emerge is loaded."
317 :type 'string
318 :group 'emerge)
320 ;; This function sets up the fixed keymaps. It is executed when the first
321 ;; Emerge is done to allow the user maximum time to set up the global keymap.
322 (defun emerge-setup-fixed-keymaps ()
323 ;; Set up the basic keymap
324 (setq emerge-basic-keymap (make-keymap))
325 (suppress-keymap emerge-basic-keymap) ; this sets 0..9 to digit-argument and
326 ; - to negative-argument
327 (define-key emerge-basic-keymap "p" 'emerge-previous-difference)
328 (define-key emerge-basic-keymap "n" 'emerge-next-difference)
329 (define-key emerge-basic-keymap "a" 'emerge-select-A)
330 (define-key emerge-basic-keymap "b" 'emerge-select-B)
331 (define-key emerge-basic-keymap "j" 'emerge-jump-to-difference)
332 (define-key emerge-basic-keymap "." 'emerge-find-difference)
333 (define-key emerge-basic-keymap "q" 'emerge-quit)
334 (define-key emerge-basic-keymap "\C-]" 'emerge-abort)
335 (define-key emerge-basic-keymap "f" 'emerge-fast-mode)
336 (define-key emerge-basic-keymap "e" 'emerge-edit-mode)
337 (define-key emerge-basic-keymap "s" nil)
338 (define-key emerge-basic-keymap "sa" 'emerge-auto-advance)
339 (define-key emerge-basic-keymap "ss" 'emerge-skip-prefers)
340 (define-key emerge-basic-keymap "l" 'emerge-recenter)
341 (define-key emerge-basic-keymap "d" nil)
342 (define-key emerge-basic-keymap "da" 'emerge-default-A)
343 (define-key emerge-basic-keymap "db" 'emerge-default-B)
344 (define-key emerge-basic-keymap "c" nil)
345 (define-key emerge-basic-keymap "ca" 'emerge-copy-as-kill-A)
346 (define-key emerge-basic-keymap "cb" 'emerge-copy-as-kill-B)
347 (define-key emerge-basic-keymap "i" nil)
348 (define-key emerge-basic-keymap "ia" 'emerge-insert-A)
349 (define-key emerge-basic-keymap "ib" 'emerge-insert-B)
350 (define-key emerge-basic-keymap "m" 'emerge-mark-difference)
351 (define-key emerge-basic-keymap "v" 'emerge-scroll-up)
352 (define-key emerge-basic-keymap "^" 'emerge-scroll-down)
353 (define-key emerge-basic-keymap "<" 'emerge-scroll-left)
354 (define-key emerge-basic-keymap ">" 'emerge-scroll-right)
355 (define-key emerge-basic-keymap "|" 'emerge-scroll-reset)
356 (define-key emerge-basic-keymap "x" nil)
357 (define-key emerge-basic-keymap "x1" 'emerge-one-line-window)
358 (define-key emerge-basic-keymap "xc" 'emerge-combine-versions)
359 (define-key emerge-basic-keymap "xC" 'emerge-combine-versions-register)
360 (define-key emerge-basic-keymap "xf" 'emerge-file-names)
361 (define-key emerge-basic-keymap "xj" 'emerge-join-differences)
362 (define-key emerge-basic-keymap "xl" 'emerge-line-numbers)
363 (define-key emerge-basic-keymap "xm" 'emerge-set-merge-mode)
364 (define-key emerge-basic-keymap "xs" 'emerge-split-difference)
365 (define-key emerge-basic-keymap "xt" 'emerge-trim-difference)
366 (define-key emerge-basic-keymap "xx" 'emerge-set-combine-versions-template)
367 ;; Allow emerge-basic-keymap to be referenced indirectly
368 (fset 'emerge-basic-keymap emerge-basic-keymap)
369 ;; Set up the fast mode keymap
370 (setq emerge-fast-keymap (copy-keymap emerge-basic-keymap))
371 ;; Allow prefixed commands to work in fast mode
372 (define-key emerge-fast-keymap emerge-command-prefix 'emerge-basic-keymap)
373 ;; Allow emerge-fast-keymap to be referenced indirectly
374 (fset 'emerge-fast-keymap emerge-fast-keymap)
375 ;; Suppress write-file and save-buffer
376 (define-key emerge-fast-keymap [remap write-file] 'emerge-query-write-file)
377 (define-key emerge-fast-keymap [remap save-buffer] 'emerge-query-save-buffer)
379 (define-key emerge-basic-keymap [menu-bar] (make-sparse-keymap))
381 (define-key emerge-fast-keymap [menu-bar emerge-options]
382 (cons "Merge-Options" emerge-options-menu))
383 (define-key emerge-fast-keymap [menu-bar merge]
384 (cons "Merge" emerge-merge-menu))
385 (define-key emerge-fast-keymap [menu-bar move]
386 (cons "Move" emerge-move-menu))
388 (define-key emerge-move-menu [emerge-scroll-reset]
389 '("Scroll Reset" . emerge-scroll-reset))
390 (define-key emerge-move-menu [emerge-scroll-right]
391 '("Scroll Right" . emerge-scroll-right))
392 (define-key emerge-move-menu [emerge-scroll-left]
393 '("Scroll Left" . emerge-scroll-left))
394 (define-key emerge-move-menu [emerge-scroll-down]
395 '("Scroll Down" . emerge-scroll-down))
396 (define-key emerge-move-menu [emerge-scroll-up]
397 '("Scroll Up" . emerge-scroll-up))
398 (define-key emerge-move-menu [emerge-recenter]
399 '("Recenter" . emerge-recenter))
400 (define-key emerge-move-menu [emerge-mark-difference]
401 '("Mark Difference" . emerge-mark-difference))
402 (define-key emerge-move-menu [emerge-jump-to-difference]
403 '("Jump To Difference" . emerge-jump-to-difference))
404 (define-key emerge-move-menu [emerge-find-difference]
405 '("Find Difference" . emerge-find-difference))
406 (define-key emerge-move-menu [emerge-previous-difference]
407 '("Previous Difference" . emerge-previous-difference))
408 (define-key emerge-move-menu [emerge-next-difference]
409 '("Next Difference" . emerge-next-difference))
412 (define-key emerge-options-menu [emerge-one-line-window]
413 '("One Line Window" . emerge-one-line-window))
414 (define-key emerge-options-menu [emerge-set-merge-mode]
415 '("Set Merge Mode..." . emerge-set-merge-mode))
416 (define-key emerge-options-menu [emerge-set-combine-template]
417 '("Set Combine Template..." . emerge-set-combine-template))
418 (define-key emerge-options-menu [emerge-default-B]
419 '("Default B" . emerge-default-B))
420 (define-key emerge-options-menu [emerge-default-A]
421 '("Default A" . emerge-default-A))
422 (define-key emerge-options-menu [emerge-skip-prefers]
423 '(menu-item "Skip Prefers" emerge-skip-prefers
424 :button (:toggle . emerge-skip-prefers)))
425 (define-key emerge-options-menu [emerge-auto-advance]
426 '(menu-item "Auto Advance" emerge-auto-advance
427 :button (:toggle . emerge-auto-advance)))
428 (define-key emerge-options-menu [emerge-edit-mode]
429 '(menu-item "Edit Mode" emerge-edit-mode :enable (not emerge-edit-mode)))
430 (define-key emerge-options-menu [emerge-fast-mode]
431 '(menu-item "Fast Mode" emerge-fast-mode :enable (not emerge-fast-mode)))
433 (define-key emerge-merge-menu [emerge-abort] '("Abort" . emerge-abort))
434 (define-key emerge-merge-menu [emerge-quit] '("Quit" . emerge-quit))
435 (define-key emerge-merge-menu [emerge-split-difference]
436 '("Split Difference" . emerge-split-difference))
437 (define-key emerge-merge-menu [emerge-join-differences]
438 '("Join Differences" . emerge-join-differences))
439 (define-key emerge-merge-menu [emerge-trim-difference]
440 '("Trim Difference" . emerge-trim-difference))
441 (define-key emerge-merge-menu [emerge-combine-versions]
442 '("Combine Versions" . emerge-combine-versions))
443 (define-key emerge-merge-menu [emerge-copy-as-kill-B]
444 '("Copy B as Kill" . emerge-copy-as-kill-B))
445 (define-key emerge-merge-menu [emerge-copy-as-kill-A]
446 '("Copy A as Kill" . emerge-copy-as-kill-A))
447 (define-key emerge-merge-menu [emerge-insert-B]
448 '("Insert B" . emerge-insert-B))
449 (define-key emerge-merge-menu [emerge-insert-A]
450 '("Insert A" . emerge-insert-A))
451 (define-key emerge-merge-menu [emerge-select-B]
452 '("Select B" . emerge-select-B))
453 (define-key emerge-merge-menu [emerge-select-A]
454 '("Select A" . emerge-select-A)))
457 ;; Variables which control each merge. They are local to the merge buffer.
459 ;; Mode variables
460 (emerge-defvar-local emerge-mode nil
461 "Indicator for emerge-mode.")
462 (emerge-defvar-local emerge-fast-mode nil
463 "Indicator for emerge-mode fast submode.")
464 (emerge-defvar-local emerge-edit-mode nil
465 "Indicator for emerge-mode edit submode.")
466 (emerge-defvar-local emerge-A-buffer nil
467 "The buffer in which the A variant is stored.")
468 (emerge-defvar-local emerge-B-buffer nil
469 "The buffer in which the B variant is stored.")
470 (emerge-defvar-local emerge-merge-buffer nil
471 "The buffer in which the merged file is manipulated.")
472 (emerge-defvar-local emerge-ancestor-buffer nil
473 "The buffer in which the ancestor variant is stored,
474 or nil if there is none.")
476 (defconst emerge-saved-variables
477 '((buffer-modified-p set-buffer-modified-p)
478 buffer-read-only
479 buffer-auto-save-file-name)
480 "Variables and properties of a buffer which are saved, modified and restored
481 during a merge.")
482 (defconst emerge-merging-values '(nil t nil)
483 "Values to be assigned to emerge-saved-variables during a merge.")
485 (emerge-defvar-local emerge-A-buffer-values nil
486 "Remembers emerge-saved-variables for emerge-A-buffer.")
487 (emerge-defvar-local emerge-B-buffer-values nil
488 "Remembers emerge-saved-variables for emerge-B-buffer.")
490 (emerge-defvar-local emerge-difference-list nil
491 "Vector of differences between the variants, and markers in the buffers to
492 show where they are. Each difference is represented by a vector of seven
493 elements. The first two are markers to the beginning and end of the difference
494 section in the A buffer, the second two are markers for the B buffer, the third
495 two are markers for the merge buffer, and the last element is the \"state\" of
496 that difference in the merge buffer.
497 A section of a buffer is described by two markers, one to the beginning of
498 the first line of the section, and one to the beginning of the first line
499 after the section. (If the section is empty, both markers point to the same
500 point.) If the section is part of the selected difference, then the markers
501 are moved into the flags, so the user can edit the section without disturbing
502 the markers.
503 The \"states\" are:
504 A the merge buffer currently contains the A variant
505 B the merge buffer currently contains the B variant
506 default-A the merge buffer contains the A variant by default,
507 but this difference hasn't been selected yet, so
508 change-default commands can alter it
509 default-B the merge buffer contains the B variant by default,
510 but this difference hasn't been selected yet, so
511 change-default commands can alter it
512 prefer-A in a three-file merge, the A variant is the preferred
513 choice
514 prefer-B in a three-file merge, the B variant is the preferred
515 choice")
516 (emerge-defvar-local emerge-current-difference -1
517 "The difference that is currently selected.")
518 (emerge-defvar-local emerge-number-of-differences nil
519 "Number of differences found.")
520 (emerge-defvar-local emerge-edit-keymap nil
521 "The local keymap for the merge buffer, with the emerge commands defined in
522 it. Used to save the local keymap during fast mode, when the local keymap is
523 replaced by emerge-fast-keymap.")
524 (emerge-defvar-local emerge-old-keymap nil
525 "The original local keymap for the merge buffer.")
526 (emerge-defvar-local emerge-auto-advance nil
527 "*If non-nil, emerge-select-A and emerge-select-B automatically advance to
528 the next difference.")
529 (emerge-defvar-local emerge-skip-prefers nil
530 "*If non-nil, differences for which there is a preference are automatically
531 skipped.")
532 (emerge-defvar-local emerge-quit-hook nil
533 "Hooks to run in the merge buffer after the merge has been finished.
534 `emerge-prefix-argument' will hold the prefix argument of the `emerge-quit'
535 command.
536 This is *not* a user option, since Emerge uses it for its own processing.")
537 (emerge-defvar-local emerge-output-description nil
538 "Describes output destination of emerge, for `emerge-file-names'.")
540 ;;; Setup functions for two-file mode.
542 (defun emerge-files-internal (file-A file-B &optional startup-hooks quit-hooks
543 output-file)
544 (if (not (file-readable-p file-A))
545 (error "File `%s' does not exist or is not readable" file-A))
546 (if (not (file-readable-p file-B))
547 (error "File `%s' does not exist or is not readable" file-B))
548 (let ((buffer-A (find-file-noselect file-A))
549 (buffer-B (find-file-noselect file-B)))
550 ;; Record the directories of the files
551 (setq emerge-last-dir-A (file-name-directory file-A))
552 (setq emerge-last-dir-B (file-name-directory file-B))
553 (if output-file
554 (setq emerge-last-dir-output (file-name-directory output-file)))
555 ;; Make sure the entire files are seen, and they reflect what is on disk
556 (with-current-buffer
557 buffer-A
558 (widen)
559 (let ((temp (file-local-copy file-A)))
560 (if temp
561 (setq file-A temp
562 startup-hooks
563 (cons `(lambda () (delete-file ,file-A))
564 startup-hooks))
565 ;; Verify that the file matches the buffer
566 (emerge-verify-file-buffer))))
567 (with-current-buffer
568 buffer-B
569 (widen)
570 (let ((temp (file-local-copy file-B)))
571 (if temp
572 (setq file-B temp
573 startup-hooks
574 (cons `(lambda () (delete-file ,file-B))
575 startup-hooks))
576 ;; Verify that the file matches the buffer
577 (emerge-verify-file-buffer))))
578 (emerge-setup buffer-A file-A buffer-B file-B startup-hooks quit-hooks
579 output-file)))
581 ;; Start up Emerge on two files
582 (defun emerge-setup (buffer-A file-A buffer-B file-B startup-hooks quit-hooks
583 output-file)
584 (setq file-A (expand-file-name file-A))
585 (setq file-B (expand-file-name file-B))
586 (setq output-file (and output-file (expand-file-name output-file)))
587 (let* ((merge-buffer-name (emerge-unique-buffer-name "*merge" "*"))
588 ;; create the merge buffer from buffer A, so it inherits buffer A's
589 ;; default directory, etc.
590 (merge-buffer (with-current-buffer
591 buffer-A
592 (get-buffer-create merge-buffer-name))))
593 (with-current-buffer
594 merge-buffer
595 (emerge-copy-modes buffer-A)
596 (setq buffer-read-only nil)
597 (auto-save-mode 1)
598 (setq emerge-mode t)
599 (setq emerge-A-buffer buffer-A)
600 (setq emerge-B-buffer buffer-B)
601 (setq emerge-ancestor-buffer nil)
602 (setq emerge-merge-buffer merge-buffer)
603 (setq emerge-output-description
604 (if output-file
605 (concat "Output to file: " output-file)
606 (concat "Output to buffer: " (buffer-name merge-buffer))))
607 (save-excursion (insert-buffer-substring emerge-A-buffer))
608 (emerge-set-keys)
609 (setq emerge-difference-list (emerge-make-diff-list file-A file-B))
610 (setq emerge-number-of-differences (length emerge-difference-list))
611 (setq emerge-current-difference -1)
612 (setq emerge-quit-hook quit-hooks)
613 (emerge-remember-buffer-characteristics)
614 (emerge-handle-local-variables))
615 (emerge-setup-windows buffer-A buffer-B merge-buffer t)
616 (with-current-buffer merge-buffer
617 (run-hooks 'startup-hooks 'emerge-startup-hook)
618 (setq buffer-read-only t))))
620 ;; Generate the Emerge difference list between two files
621 (defun emerge-make-diff-list (file-A file-B)
622 (setq emerge-diff-buffer (get-buffer-create "*emerge-diff*"))
623 (with-current-buffer
624 emerge-diff-buffer
625 (erase-buffer)
626 (shell-command
627 (format "%s %s %s %s"
628 emerge-diff-program emerge-diff-options
629 (emerge-protect-metachars file-A)
630 (emerge-protect-metachars file-B))
632 (emerge-prepare-error-list emerge-diff-ok-lines-regexp)
633 (emerge-convert-diffs-to-markers
634 emerge-A-buffer emerge-B-buffer emerge-merge-buffer
635 (emerge-extract-diffs emerge-diff-buffer)))
637 (defun emerge-extract-diffs (diff-buffer)
638 (let (list)
639 (with-current-buffer
640 diff-buffer
641 (goto-char (point-min))
642 (while (re-search-forward emerge-match-diff-line nil t)
643 (let* ((a-begin (string-to-number (buffer-substring (match-beginning 1)
644 (match-end 1))))
645 (a-end (let ((b (match-beginning 3))
646 (e (match-end 3)))
647 (if b
648 (string-to-number (buffer-substring b e))
649 a-begin)))
650 (diff-type (buffer-substring (match-beginning 4) (match-end 4)))
651 (b-begin (string-to-number (buffer-substring (match-beginning 5)
652 (match-end 5))))
653 (b-end (let ((b (match-beginning 7))
654 (e (match-end 7)))
655 (if b
656 (string-to-number (buffer-substring b e))
657 b-begin))))
658 ;; fix the beginning and end numbers, because diff is somewhat
659 ;; strange about how it numbers lines
660 (if (string-equal diff-type "a")
661 (progn
662 (setq b-end (1+ b-end))
663 (setq a-begin (1+ a-begin))
664 (setq a-end a-begin))
665 (if (string-equal diff-type "d")
666 (progn
667 (setq a-end (1+ a-end))
668 (setq b-begin (1+ b-begin))
669 (setq b-end b-begin))
670 ;; (string-equal diff-type "c")
671 (progn
672 (setq a-end (1+ a-end))
673 (setq b-end (1+ b-end)))))
674 (setq list (cons (vector a-begin a-end
675 b-begin b-end
676 'default-A)
677 list)))))
678 (nreverse list)))
680 ;; Set up buffer of diff/diff3 error messages.
681 (defun emerge-prepare-error-list (ok-regexp)
682 (setq emerge-diff-error-buffer (get-buffer-create "*emerge-diff-errors*"))
683 (with-current-buffer
684 emerge-diff-error-buffer
685 (erase-buffer)
686 (save-excursion (insert-buffer-substring emerge-diff-buffer))
687 (delete-matching-lines ok-regexp)))
689 ;;; Top-level and setup functions for three-file mode.
691 (defun emerge-files-with-ancestor-internal (file-A file-B file-ancestor
692 &optional startup-hooks quit-hooks
693 output-file)
694 (if (not (file-readable-p file-A))
695 (error "File `%s' does not exist or is not readable" file-A))
696 (if (not (file-readable-p file-B))
697 (error "File `%s' does not exist or is not readable" file-B))
698 (if (not (file-readable-p file-ancestor))
699 (error "File `%s' does not exist or is not readable" file-ancestor))
700 (let ((buffer-A (find-file-noselect file-A))
701 (buffer-B (find-file-noselect file-B))
702 (buffer-ancestor (find-file-noselect file-ancestor)))
703 ;; Record the directories of the files
704 (setq emerge-last-dir-A (file-name-directory file-A))
705 (setq emerge-last-dir-B (file-name-directory file-B))
706 (setq emerge-last-dir-ancestor (file-name-directory file-ancestor))
707 (if output-file
708 (setq emerge-last-dir-output (file-name-directory output-file)))
709 ;; Make sure the entire files are seen, and they reflect what is on disk
710 (with-current-buffer
711 buffer-A
712 (widen)
713 (let ((temp (file-local-copy file-A)))
714 (if temp
715 (setq file-A temp
716 startup-hooks
717 (cons `(lambda () (delete-file ,file-A))
718 startup-hooks))
719 ;; Verify that the file matches the buffer
720 (emerge-verify-file-buffer))))
721 (with-current-buffer
722 buffer-B
723 (widen)
724 (let ((temp (file-local-copy file-B)))
725 (if temp
726 (setq file-B temp
727 startup-hooks
728 (cons `(lambda () (delete-file ,file-B))
729 startup-hooks))
730 ;; Verify that the file matches the buffer
731 (emerge-verify-file-buffer))))
732 (with-current-buffer
733 buffer-ancestor
734 (widen)
735 (let ((temp (file-local-copy file-ancestor)))
736 (if temp
737 (setq file-ancestor temp
738 startup-hooks
739 (cons `(lambda () (delete-file ,file-ancestor))
740 startup-hooks))
741 ;; Verify that the file matches the buffer
742 (emerge-verify-file-buffer))))
743 (emerge-setup-with-ancestor buffer-A file-A buffer-B file-B
744 buffer-ancestor file-ancestor
745 startup-hooks quit-hooks output-file)))
747 ;; Start up Emerge on two files with an ancestor
748 (defun emerge-setup-with-ancestor (buffer-A file-A buffer-B file-B
749 buffer-ancestor file-ancestor
750 &optional startup-hooks quit-hooks
751 output-file)
752 (setq file-A (expand-file-name file-A))
753 (setq file-B (expand-file-name file-B))
754 (setq file-ancestor (expand-file-name file-ancestor))
755 (setq output-file (and output-file (expand-file-name output-file)))
756 (let* ((merge-buffer-name (emerge-unique-buffer-name "*merge" "*"))
757 ;; create the merge buffer from buffer A, so it inherits buffer A's
758 ;; default directory, etc.
759 (merge-buffer (with-current-buffer
760 buffer-A
761 (get-buffer-create merge-buffer-name))))
762 (with-current-buffer
763 merge-buffer
764 (emerge-copy-modes buffer-A)
765 (setq buffer-read-only nil)
766 (auto-save-mode 1)
767 (setq emerge-mode t)
768 (setq emerge-A-buffer buffer-A)
769 (setq emerge-B-buffer buffer-B)
770 (setq emerge-ancestor-buffer buffer-ancestor)
771 (setq emerge-merge-buffer merge-buffer)
772 (setq emerge-output-description
773 (if output-file
774 (concat "Output to file: " output-file)
775 (concat "Output to buffer: " (buffer-name merge-buffer))))
776 (save-excursion (insert-buffer-substring emerge-A-buffer))
777 (emerge-set-keys)
778 (setq emerge-difference-list
779 (emerge-make-diff3-list file-A file-B file-ancestor))
780 (setq emerge-number-of-differences (length emerge-difference-list))
781 (setq emerge-current-difference -1)
782 (setq emerge-quit-hook quit-hooks)
783 (emerge-remember-buffer-characteristics)
784 (emerge-select-prefer-Bs)
785 (emerge-handle-local-variables))
786 (emerge-setup-windows buffer-A buffer-B merge-buffer t)
787 (with-current-buffer merge-buffer
788 (run-hooks 'startup-hooks 'emerge-startup-hook)
789 (setq buffer-read-only t))))
791 ;; Generate the Emerge difference list between two files with an ancestor
792 (defun emerge-make-diff3-list (file-A file-B file-ancestor)
793 (setq emerge-diff-buffer (get-buffer-create "*emerge-diff*"))
794 (with-current-buffer
795 emerge-diff-buffer
796 (erase-buffer)
797 (shell-command
798 (format "%s %s %s %s %s"
799 emerge-diff3-program emerge-diff-options
800 (emerge-protect-metachars file-A)
801 (emerge-protect-metachars file-ancestor)
802 (emerge-protect-metachars file-B))
804 (emerge-prepare-error-list emerge-diff3-ok-lines-regexp)
805 (emerge-convert-diffs-to-markers
806 emerge-A-buffer emerge-B-buffer emerge-merge-buffer
807 (emerge-extract-diffs3 emerge-diff-buffer)))
809 (defun emerge-extract-diffs3 (diff-buffer)
810 (let (list)
811 (with-current-buffer
812 diff-buffer
813 (while (re-search-forward "^====\\(.?\\)$" nil t)
814 ;; leave point after matched line
815 (beginning-of-line 2)
816 (let ((agreement (buffer-substring (match-beginning 1) (match-end 1))))
817 ;; if the A and B files are the same, ignore the difference
818 (if (not (string-equal agreement "2"))
819 (setq list
820 (cons
821 (let (group-1 group-3 pos)
822 (setq pos (point))
823 (setq group-1 (emerge-get-diff3-group "1"))
824 (goto-char pos)
825 (setq group-3 (emerge-get-diff3-group "3"))
826 (vector (car group-1) (car (cdr group-1))
827 (car group-3) (car (cdr group-3))
828 (cond ((string-equal agreement "1") 'prefer-A)
829 ((string-equal agreement "3") 'prefer-B)
830 (t 'default-A))))
831 list))))))
832 (nreverse list)))
834 (defun emerge-get-diff3-group (file)
835 ;; This save-excursion allows emerge-get-diff3-group to be called for the
836 ;; various groups of lines (1, 2, 3) in any order, and for the lines to
837 ;; appear in any order. The reason this is necessary is that Gnu diff3
838 ;; can produce the groups in the order 1, 2, 3 or 1, 3, 2.
839 (save-excursion
840 (re-search-forward
841 (concat "^" file ":\\([0-9]+\\)\\(,\\([0-9]+\\)\\)?\\([ac]\\)$"))
842 (beginning-of-line 2)
843 ;; treatment depends on whether it is an "a" group or a "c" group
844 (if (string-equal (buffer-substring (match-beginning 4) (match-end 4)) "c")
845 ;; it is a "c" group
846 (if (match-beginning 2)
847 ;; it has two numbers
848 (list (string-to-number
849 (buffer-substring (match-beginning 1) (match-end 1)))
850 (1+ (string-to-number
851 (buffer-substring (match-beginning 3) (match-end 3)))))
852 ;; it has one number
853 (let ((x (string-to-number
854 (buffer-substring (match-beginning 1) (match-end 1)))))
855 (list x (1+ x))))
856 ;; it is an "a" group
857 (let ((x (1+ (string-to-number
858 (buffer-substring (match-beginning 1) (match-end 1))))))
859 (list x x)))))
861 ;;; Functions to start Emerge on files
863 ;;;###autoload
864 (defun emerge-files (arg file-A file-B file-out &optional startup-hooks
865 quit-hooks)
866 "Run Emerge on two files."
867 (interactive
868 (let (f)
869 (list current-prefix-arg
870 (setq f (emerge-read-file-name "File A to merge" emerge-last-dir-A
871 nil nil t))
872 (emerge-read-file-name "File B to merge" emerge-last-dir-B nil f t)
873 (and current-prefix-arg
874 (emerge-read-file-name "Output file" emerge-last-dir-output
875 f f nil)))))
876 (if file-out
877 (add-hook 'quit-hooks `(lambda () (emerge-files-exit ,file-out))))
878 (emerge-files-internal
879 file-A file-B startup-hooks
880 quit-hooks
881 file-out))
883 ;;;###autoload
884 (defun emerge-files-with-ancestor (arg file-A file-B file-ancestor file-out
885 &optional startup-hooks quit-hooks)
886 "Run Emerge on two files, giving another file as the ancestor."
887 (interactive
888 (let (f)
889 (list current-prefix-arg
890 (setq f (emerge-read-file-name "File A to merge" emerge-last-dir-A
891 nil nil t))
892 (emerge-read-file-name "File B to merge" emerge-last-dir-B nil f t)
893 (emerge-read-file-name "Ancestor file" emerge-last-dir-ancestor
894 nil f t)
895 (and current-prefix-arg
896 (emerge-read-file-name "Output file" emerge-last-dir-output
897 f f nil)))))
898 (if file-out
899 (add-hook 'quit-hooks `(lambda () (emerge-files-exit ,file-out))))
900 (emerge-files-with-ancestor-internal
901 file-A file-B file-ancestor startup-hooks
902 quit-hooks
903 file-out))
905 ;; Write the merge buffer out in place of the file the A buffer is visiting.
906 (defun emerge-files-exit (file-out)
907 ;; if merge was successful was given, save to disk
908 (if (not emerge-prefix-argument)
909 (emerge-write-and-delete file-out)))
911 ;;; Functions to start Emerge on buffers
913 ;;;###autoload
914 (defun emerge-buffers (buffer-A buffer-B &optional startup-hooks quit-hooks)
915 "Run Emerge on two buffers."
916 (interactive "bBuffer A to merge: \nbBuffer B to merge: ")
917 (let ((emerge-file-A (emerge-make-temp-file "A"))
918 (emerge-file-B (emerge-make-temp-file "B")))
919 (with-current-buffer
920 buffer-A
921 (write-region (point-min) (point-max) emerge-file-A nil 'no-message))
922 (with-current-buffer
923 buffer-B
924 (write-region (point-min) (point-max) emerge-file-B nil 'no-message))
925 (emerge-setup (get-buffer buffer-A) emerge-file-A
926 (get-buffer buffer-B) emerge-file-B
927 (cons `(lambda ()
928 (delete-file ,emerge-file-A)
929 (delete-file ,emerge-file-B))
930 startup-hooks)
931 quit-hooks
932 nil)))
934 ;;;###autoload
935 (defun emerge-buffers-with-ancestor (buffer-A buffer-B buffer-ancestor
936 &optional startup-hooks
937 quit-hooks)
938 "Run Emerge on two buffers, giving another buffer as the ancestor."
939 (interactive
940 "bBuffer A to merge: \nbBuffer B to merge: \nbAncestor buffer: ")
941 (let ((emerge-file-A (emerge-make-temp-file "A"))
942 (emerge-file-B (emerge-make-temp-file "B"))
943 (emerge-file-ancestor (emerge-make-temp-file "anc")))
944 (with-current-buffer
945 buffer-A
946 (write-region (point-min) (point-max) emerge-file-A nil 'no-message))
947 (with-current-buffer
948 buffer-B
949 (write-region (point-min) (point-max) emerge-file-B nil 'no-message))
950 (with-current-buffer
951 buffer-ancestor
952 (write-region (point-min) (point-max) emerge-file-ancestor nil
953 'no-message))
954 (emerge-setup-with-ancestor (get-buffer buffer-A) emerge-file-A
955 (get-buffer buffer-B) emerge-file-B
956 (get-buffer buffer-ancestor)
957 emerge-file-ancestor
958 (cons `(lambda ()
959 (delete-file ,emerge-file-A)
960 (delete-file ,emerge-file-B)
961 (delete-file
962 ,emerge-file-ancestor))
963 startup-hooks)
964 quit-hooks
965 nil)))
967 ;;; Functions to start Emerge from the command line
969 ;;;###autoload
970 (defun emerge-files-command ()
971 (let ((file-a (nth 0 command-line-args-left))
972 (file-b (nth 1 command-line-args-left))
973 (file-out (nth 2 command-line-args-left)))
974 (setq command-line-args-left (nthcdr 3 command-line-args-left))
975 (emerge-files-internal
976 file-a file-b nil
977 (list `(lambda () (emerge-command-exit ,file-out))))))
979 ;;;###autoload
980 (defun emerge-files-with-ancestor-command ()
981 (let (file-a file-b file-anc file-out)
982 ;; check for a -a flag, for filemerge compatibility
983 (if (string= (car command-line-args-left) "-a")
984 ;; arguments are "-a ancestor file-a file-b file-out"
985 (progn
986 (setq file-a (nth 2 command-line-args-left))
987 (setq file-b (nth 3 command-line-args-left))
988 (setq file-anc (nth 1 command-line-args-left))
989 (setq file-out (nth 4 command-line-args-left))
990 (setq command-line-args-left (nthcdr 5 command-line-args-left)))
991 ;; arguments are "file-a file-b ancestor file-out"
992 (setq file-a (nth 0 command-line-args-left))
993 (setq file-b (nth 1 command-line-args-left))
994 (setq file-anc (nth 2 command-line-args-left))
995 (setq file-out (nth 3 command-line-args-left))
996 (setq command-line-args-left (nthcdr 4 command-line-args-left)))
997 (emerge-files-with-ancestor-internal
998 file-a file-b file-anc nil
999 (list `(lambda () (emerge-command-exit ,file-out))))))
1001 (defun emerge-command-exit (file-out)
1002 (emerge-write-and-delete file-out)
1003 (kill-emacs (if emerge-prefix-argument 1 0)))
1005 ;;; Functions to start Emerge via remote request
1007 ;;;###autoload
1008 (defun emerge-files-remote (file-a file-b file-out)
1009 (setq emerge-file-out file-out)
1010 (emerge-files-internal
1011 file-a file-b nil
1012 (list `(lambda () (emerge-remote-exit ,file-out ',emerge-exit-func)))
1013 file-out)
1014 (throw 'client-wait nil))
1016 ;;;###autoload
1017 (defun emerge-files-with-ancestor-remote (file-a file-b file-anc file-out)
1018 (setq emerge-file-out file-out)
1019 (emerge-files-with-ancestor-internal
1020 file-a file-b file-anc nil
1021 (list `(lambda () (emerge-remote-exit ,file-out ',emerge-exit-func)))
1022 file-out)
1023 (throw 'client-wait nil))
1025 (defun emerge-remote-exit (file-out emerge-exit-func)
1026 (emerge-write-and-delete file-out)
1027 (kill-buffer emerge-merge-buffer)
1028 (funcall emerge-exit-func (if emerge-prefix-argument 1 0)))
1030 ;;; Functions to start Emerge on RCS versions
1032 ;;;###autoload
1033 (defun emerge-revisions (arg file revision-A revision-B
1034 &optional startup-hooks quit-hooks)
1035 "Emerge two RCS revisions of a file."
1036 (interactive
1037 (list current-prefix-arg
1038 (read-file-name "File to merge: " nil nil 'confirm)
1039 (read-string "Revision A to merge: " emerge-last-revision-A)
1040 (read-string "Revision B to merge: " emerge-last-revision-B)))
1041 (setq emerge-last-revision-A revision-A
1042 emerge-last-revision-B revision-B)
1043 (emerge-revisions-internal
1044 file revision-A revision-B startup-hooks
1045 (if arg
1046 (cons `(lambda ()
1047 (shell-command
1048 ,(format "%s %s" emerge-rcs-ci-program file)))
1049 quit-hooks)
1050 quit-hooks)))
1052 ;;;###autoload
1053 (defun emerge-revisions-with-ancestor (arg file revision-A
1054 revision-B ancestor
1055 &optional
1056 startup-hooks quit-hooks)
1057 "Emerge two RCS revisions of a file, with another revision as ancestor."
1058 (interactive
1059 (list current-prefix-arg
1060 (read-file-name "File to merge: " nil nil 'confirm)
1061 (read-string "Revision A to merge: " emerge-last-revision-A)
1062 (read-string "Revision B to merge: " emerge-last-revision-B)
1063 (read-string "Ancestor: " emerge-last-revision-ancestor)))
1064 (setq emerge-last-revision-A revision-A
1065 emerge-last-revision-B revision-B
1066 emerge-last-revision-ancestor ancestor)
1067 (emerge-revision-with-ancestor-internal
1068 file revision-A revision-B ancestor startup-hooks
1069 (if arg
1070 (let ((cmd ))
1071 (cons `(lambda ()
1072 (shell-command
1073 ,(format "%s %s" emerge-rcs-ci-program file)))
1074 quit-hooks))
1075 quit-hooks)))
1077 (defun emerge-revisions-internal (file revision-A revision-B &optional
1078 startup-hooks quit-hooks output-file)
1079 (let ((buffer-A (get-buffer-create (format "%s,%s" file revision-A)))
1080 (buffer-B (get-buffer-create (format "%s,%s" file revision-B)))
1081 (emerge-file-A (emerge-make-temp-file "A"))
1082 (emerge-file-B (emerge-make-temp-file "B")))
1083 ;; Get the revisions into buffers
1084 (with-current-buffer
1085 buffer-A
1086 (erase-buffer)
1087 (shell-command
1088 (format "%s -q -p%s %s" emerge-rcs-co-program revision-A file)
1090 (write-region (point-min) (point-max) emerge-file-A nil 'no-message)
1091 (set-buffer-modified-p nil))
1092 (with-current-buffer
1093 buffer-B
1094 (erase-buffer)
1095 (shell-command
1096 (format "%s -q -p%s %s" emerge-rcs-co-program revision-B file)
1098 (write-region (point-min) (point-max) emerge-file-B nil 'no-message)
1099 (set-buffer-modified-p nil))
1100 ;; Do the merge
1101 (emerge-setup buffer-A emerge-file-A
1102 buffer-B emerge-file-B
1103 (cons `(lambda ()
1104 (delete-file ,emerge-file-A)
1105 (delete-file ,emerge-file-B))
1106 startup-hooks)
1107 (cons `(lambda () (emerge-files-exit ,file))
1108 quit-hooks)
1109 nil)))
1111 (defun emerge-revision-with-ancestor-internal (file revision-A revision-B
1112 ancestor
1113 &optional startup-hooks
1114 quit-hooks output-file)
1115 (let ((buffer-A (get-buffer-create (format "%s,%s" file revision-A)))
1116 (buffer-B (get-buffer-create (format "%s,%s" file revision-B)))
1117 (buffer-ancestor (get-buffer-create (format "%s,%s" file ancestor)))
1118 (emerge-file-A (emerge-make-temp-file "A"))
1119 (emerge-file-B (emerge-make-temp-file "B"))
1120 (emerge-ancestor (emerge-make-temp-file "ancestor")))
1121 ;; Get the revisions into buffers
1122 (with-current-buffer
1123 buffer-A
1124 (erase-buffer)
1125 (shell-command
1126 (format "%s -q -p%s %s" emerge-rcs-co-program
1127 revision-A file)
1129 (write-region (point-min) (point-max) emerge-file-A nil 'no-message)
1130 (set-buffer-modified-p nil))
1131 (with-current-buffer
1132 buffer-B
1133 (erase-buffer)
1134 (shell-command
1135 (format "%s -q -p%s %s" emerge-rcs-co-program revision-B file)
1137 (write-region (point-min) (point-max) emerge-file-B nil 'no-message)
1138 (set-buffer-modified-p nil))
1139 (with-current-buffer
1140 buffer-ancestor
1141 (erase-buffer)
1142 (shell-command
1143 (format "%s -q -p%s %s" emerge-rcs-co-program ancestor file)
1145 (write-region (point-min) (point-max) emerge-ancestor nil 'no-message)
1146 (set-buffer-modified-p nil))
1147 ;; Do the merge
1148 (emerge-setup-with-ancestor
1149 buffer-A emerge-file-A buffer-B emerge-file-B
1150 buffer-ancestor emerge-ancestor
1151 (cons `(lambda ()
1152 (delete-file ,emerge-file-A)
1153 (delete-file ,emerge-file-B)
1154 (delete-file ,emerge-ancestor))
1155 startup-hooks)
1156 (cons `(lambda () (emerge-files-exit ,file))
1157 quit-hooks)
1158 output-file)))
1160 ;;; Function to start Emerge based on a line in a file
1162 (defun emerge-execute-line ()
1163 "Run Emerge using files named in current text line.
1164 Looks in that line for whitespace-separated entries of these forms:
1165 a=file1
1166 b=file2
1167 ancestor=file3
1168 output=file4
1169 to specify the files to use in Emerge.
1171 In addition, if only one of `a=file' or `b=file' is present, and `output=file'
1172 is present:
1173 If `emerge-execute-line-deletions' is non-nil and `ancestor=file' is present,
1174 it is assumed that the file in question has been deleted, and it is
1175 not copied to the output file.
1176 Otherwise, the A or B file present is copied to the output file."
1177 (interactive)
1178 (let (file-A file-B file-ancestor file-out
1179 (case-fold-search t))
1180 ;; Stop if at end of buffer (even though we might be in a line, if
1181 ;; the line does not end with newline)
1182 (if (eobp)
1183 (error "At end of buffer"))
1184 ;; Go to the beginning of the line
1185 (beginning-of-line)
1186 ;; Skip any initial whitespace
1187 (if (looking-at "[ \t]*")
1188 (goto-char (match-end 0)))
1189 ;; Process the entire line
1190 (while (not (eolp))
1191 ;; Get the next entry
1192 (if (looking-at "\\([a-z]+\\)=\\([^ \t\n]+\\)[ \t]*")
1193 ;; Break apart the tab (before =) and the filename (after =)
1194 (let ((tag (downcase
1195 (buffer-substring (match-beginning 1) (match-end 1))))
1196 (file (buffer-substring (match-beginning 2) (match-end 2))))
1197 ;; Move point after the entry
1198 (goto-char (match-end 0))
1199 ;; Store the filename in the right variable
1200 (cond
1201 ((string-equal tag "a")
1202 (if file-A
1203 (error "This line has two `A' entries"))
1204 (setq file-A file))
1205 ((string-equal tag "b")
1206 (if file-B
1207 (error "This line has two `B' entries"))
1208 (setq file-B file))
1209 ((or (string-equal tag "anc") (string-equal tag "ancestor"))
1210 (if file-ancestor
1211 (error "This line has two `ancestor' entries"))
1212 (setq file-ancestor file))
1213 ((or (string-equal tag "out") (string-equal tag "output"))
1214 (if file-out
1215 (error "This line has two `output' entries"))
1216 (setq file-out file))
1218 (error "Unrecognized entry"))))
1219 ;; If the match on the entry pattern failed
1220 (error "Unparsable entry")))
1221 ;; Make sure that file-A and file-B are present
1222 (if (not (or (and file-A file-B) file-out))
1223 (error "Must have both `A' and `B' entries"))
1224 (if (not (or file-A file-B))
1225 (error "Must have `A' or `B' entry"))
1226 ;; Go to the beginning of the next line, so next execution will use
1227 ;; next line in buffer.
1228 (beginning-of-line 2)
1229 ;; Execute the correct command
1230 (cond
1231 ;; Merge of two files with ancestor
1232 ((and file-A file-B file-ancestor)
1233 (message "Merging %s and %s..." file-A file-B)
1234 (emerge-files-with-ancestor (not (not file-out)) file-A file-B
1235 file-ancestor file-out
1237 ;; When done, return to this buffer.
1238 (list
1239 `(lambda ()
1240 (switch-to-buffer ,(current-buffer))
1241 (message "Merge done.")))))
1242 ;; Merge of two files without ancestor
1243 ((and file-A file-B)
1244 (message "Merging %s and %s..." file-A file-B)
1245 (emerge-files (not (not file-out)) file-A file-B file-out
1247 ;; When done, return to this buffer.
1248 (list
1249 `(lambda ()
1250 (switch-to-buffer ,(current-buffer))
1251 (message "Merge done.")))))
1252 ;; There is an output file (or there would have been an error above),
1253 ;; but only one input file.
1254 ;; The file appears to have been deleted in one version; do nothing.
1255 ((and file-ancestor emerge-execute-line-deletions)
1256 (message "No action."))
1257 ;; The file should be copied from the version that contains it
1258 (t (let ((input-file (or file-A file-B)))
1259 (message "Copying...")
1260 (copy-file input-file file-out)
1261 (message "%s copied to %s." input-file file-out))))))
1263 ;;; Sample function for creating information for emerge-execute-line
1265 (defcustom emerge-merge-directories-filename-regexp "[^.]"
1266 "Regexp describing files to be processed by `emerge-merge-directories'."
1267 :type 'regexp
1268 :group 'emerge)
1270 ;;;###autoload
1271 (defun emerge-merge-directories (a-dir b-dir ancestor-dir output-dir)
1272 (interactive
1273 (list
1274 (read-directory-name "A directory: " nil nil 'confirm)
1275 (read-directory-name "B directory: " nil nil 'confirm)
1276 (read-directory-name "Ancestor directory (null for none): " nil nil 'confirm)
1277 (read-directory-name "Output directory (null for none): " nil nil 'confirm)))
1278 ;; Check that we're not on a line
1279 (if (not (and (bolp) (eolp)))
1280 (error "There is text on this line"))
1281 ;; Turn null strings into nil to indicate directories not used.
1282 (if (and ancestor-dir (string-equal ancestor-dir ""))
1283 (setq ancestor-dir nil))
1284 (if (and output-dir (string-equal output-dir ""))
1285 (setq output-dir nil))
1286 ;; Canonicalize the directory names
1287 (setq a-dir (expand-file-name a-dir))
1288 (if (not (string-equal (substring a-dir -1) "/"))
1289 (setq a-dir (concat a-dir "/")))
1290 (setq b-dir (expand-file-name b-dir))
1291 (if (not (string-equal (substring b-dir -1) "/"))
1292 (setq b-dir (concat b-dir "/")))
1293 (if ancestor-dir
1294 (progn
1295 (setq ancestor-dir (expand-file-name ancestor-dir))
1296 (if (not (string-equal (substring ancestor-dir -1) "/"))
1297 (setq ancestor-dir (concat ancestor-dir "/")))))
1298 (if output-dir
1299 (progn
1300 (setq output-dir (expand-file-name output-dir))
1301 (if (not (string-equal (substring output-dir -1) "/"))
1302 (setq output-dir (concat output-dir "/")))))
1303 ;; Set the mark to where we start
1304 (push-mark)
1305 ;; Find out what files are in the directories.
1306 (let* ((a-dir-files
1307 (directory-files a-dir nil emerge-merge-directories-filename-regexp))
1308 (b-dir-files
1309 (directory-files b-dir nil emerge-merge-directories-filename-regexp))
1310 (ancestor-dir-files
1311 (and ancestor-dir
1312 (directory-files ancestor-dir nil
1313 emerge-merge-directories-filename-regexp)))
1314 (all-files (sort (nconc (copy-sequence a-dir-files)
1315 (copy-sequence b-dir-files)
1316 (copy-sequence ancestor-dir-files))
1317 (function string-lessp))))
1318 ;; Remove duplicates from all-files.
1319 (let ((p all-files))
1320 (while p
1321 (if (and (cdr p) (string-equal (car p) (car (cdr p))))
1322 (setcdr p (cdr (cdr p)))
1323 (setq p (cdr p)))))
1324 ;; Generate the control lines for the various files.
1325 (while all-files
1326 (let ((f (car all-files)))
1327 (setq all-files (cdr all-files))
1328 (if (and a-dir-files (string-equal (car a-dir-files) f))
1329 (progn
1330 (insert "A=" a-dir f "\t")
1331 (setq a-dir-files (cdr a-dir-files))))
1332 (if (and b-dir-files (string-equal (car b-dir-files) f))
1333 (progn
1334 (insert "B=" b-dir f "\t")
1335 (setq b-dir-files (cdr b-dir-files))))
1336 (if (and ancestor-dir-files (string-equal (car ancestor-dir-files) f))
1337 (progn
1338 (insert "ancestor=" ancestor-dir f "\t")
1339 (setq ancestor-dir-files (cdr ancestor-dir-files))))
1340 (if output-dir
1341 (insert "output=" output-dir f "\t"))
1342 (backward-delete-char 1)
1343 (insert "\n")))))
1345 ;;; Common setup routines
1347 ;; Set up the window configuration. If POS is given, set the points to
1348 ;; the beginnings of the buffers.
1349 (defun emerge-setup-windows (buffer-A buffer-B merge-buffer &optional pos)
1350 ;; Make sure we are not in the minibuffer window when we try to delete
1351 ;; all other windows.
1352 (if (eq (selected-window) (minibuffer-window))
1353 (other-window 1))
1354 (delete-other-windows)
1355 (switch-to-buffer merge-buffer)
1356 (emerge-refresh-mode-line)
1357 (split-window-vertically)
1358 (split-window-horizontally)
1359 (switch-to-buffer buffer-A)
1360 (if pos
1361 (goto-char (point-min)))
1362 (other-window 1)
1363 (switch-to-buffer buffer-B)
1364 (if pos
1365 (goto-char (point-min)))
1366 (other-window 1)
1367 (if pos
1368 (goto-char (point-min)))
1369 ;; If diff/diff3 reports errors, display them rather than the merge buffer.
1370 (if (/= 0 (with-current-buffer emerge-diff-error-buffer (buffer-size)))
1371 (progn
1372 (ding)
1373 (message "Errors found in diff/diff3 output. Merge buffer is %s."
1374 (buffer-name emerge-merge-buffer))
1375 (switch-to-buffer emerge-diff-error-buffer))))
1377 ;; Set up the keymap in the merge buffer
1378 (defun emerge-set-keys ()
1379 ;; Set up fixed keymaps if necessary
1380 (if (not emerge-basic-keymap)
1381 (emerge-setup-fixed-keymaps))
1382 ;; Save the old local map
1383 (setq emerge-old-keymap (current-local-map))
1384 ;; Construct the edit keymap
1385 (setq emerge-edit-keymap (if emerge-old-keymap
1386 (copy-keymap emerge-old-keymap)
1387 (make-sparse-keymap)))
1388 ;; Install the Emerge commands
1389 (emerge-force-define-key emerge-edit-keymap emerge-command-prefix
1390 'emerge-basic-keymap)
1391 (define-key emerge-edit-keymap [menu-bar] (make-sparse-keymap))
1393 ;; Create the additional menu bar items.
1394 (define-key emerge-edit-keymap [menu-bar emerge-options]
1395 (cons "Merge-Options" emerge-options-menu))
1396 (define-key emerge-edit-keymap [menu-bar merge]
1397 (cons "Merge" emerge-merge-menu))
1398 (define-key emerge-edit-keymap [menu-bar move]
1399 (cons "Move" emerge-move-menu))
1401 ;; Suppress write-file and save-buffer
1402 (substitute-key-definition 'write-file
1403 'emerge-query-write-file
1404 emerge-edit-keymap)
1405 (substitute-key-definition 'save-buffer
1406 'emerge-query-save-buffer
1407 emerge-edit-keymap)
1408 (define-key emerge-edit-keymap [remap write-file] 'emerge-query-write-file)
1409 (define-key emerge-edit-keymap [remap save-buffer] 'emerge-query-save-buffer)
1410 (use-local-map emerge-fast-keymap)
1411 (setq emerge-edit-mode nil)
1412 (setq emerge-fast-mode t))
1414 (defun emerge-remember-buffer-characteristics ()
1415 "Record certain properties of the buffers being merged.
1416 Must be called in the merge buffer. Remembers read-only, modified,
1417 auto-save, and saves them in buffer local variables. Sets the buffers
1418 read-only and turns off `auto-save-mode'.
1419 These characteristics are restored by `emerge-restore-buffer-characteristics'."
1420 ;; force auto-save, because we will turn off auto-saving in buffers for the
1421 ;; duration
1422 (do-auto-save)
1423 ;; remember and alter buffer characteristics
1424 (setq emerge-A-buffer-values
1425 (with-current-buffer
1426 emerge-A-buffer
1427 (prog1
1428 (emerge-save-variables emerge-saved-variables)
1429 (emerge-restore-variables emerge-saved-variables
1430 emerge-merging-values))))
1431 (setq emerge-B-buffer-values
1432 (with-current-buffer
1433 emerge-B-buffer
1434 (prog1
1435 (emerge-save-variables emerge-saved-variables)
1436 (emerge-restore-variables emerge-saved-variables
1437 emerge-merging-values)))))
1439 (defun emerge-restore-buffer-characteristics ()
1440 "Restore characteristics saved by `emerge-remember-buffer-characteristics'."
1441 (let ((A-values emerge-A-buffer-values)
1442 (B-values emerge-B-buffer-values))
1443 (with-current-buffer emerge-A-buffer
1444 (emerge-restore-variables emerge-saved-variables
1445 A-values))
1446 (with-current-buffer emerge-B-buffer
1447 (emerge-restore-variables emerge-saved-variables
1448 B-values))))
1450 ;; Move to line DESIRED-LINE assuming we are at line CURRENT-LINE.
1451 ;; Return DESIRED-LINE.
1452 (defun emerge-goto-line (desired-line current-line)
1453 (forward-line (- desired-line current-line))
1454 desired-line)
1456 (defun emerge-convert-diffs-to-markers (A-buffer
1457 B-buffer
1458 merge-buffer
1459 lineno-list)
1460 (let* (marker-list
1461 (A-point-min (with-current-buffer A-buffer (point-min)))
1462 (offset (1- A-point-min))
1463 (B-point-min (with-current-buffer B-buffer (point-min)))
1464 ;; Record current line number in each buffer
1465 ;; so we don't have to count from the beginning.
1466 (a-line 1)
1467 (b-line 1))
1468 (with-current-buffer A-buffer (goto-char (point-min)))
1469 (with-current-buffer B-buffer (goto-char (point-min)))
1470 (while lineno-list
1471 (let* ((list-element (car lineno-list))
1472 a-begin-marker
1473 a-end-marker
1474 b-begin-marker
1475 b-end-marker
1476 merge-begin-marker
1477 merge-end-marker
1478 (a-begin (aref list-element 0))
1479 (a-end (aref list-element 1))
1480 (b-begin (aref list-element 2))
1481 (b-end (aref list-element 3))
1482 (state (aref list-element 4)))
1483 ;; place markers at the appropriate places in the buffers
1484 (with-current-buffer
1485 A-buffer
1486 (setq a-line (emerge-goto-line a-begin a-line))
1487 (setq a-begin-marker (point-marker))
1488 (setq a-line (emerge-goto-line a-end a-line))
1489 (setq a-end-marker (point-marker)))
1490 (with-current-buffer
1491 B-buffer
1492 (setq b-line (emerge-goto-line b-begin b-line))
1493 (setq b-begin-marker (point-marker))
1494 (setq b-line (emerge-goto-line b-end b-line))
1495 (setq b-end-marker (point-marker)))
1496 (setq merge-begin-marker (set-marker
1497 (make-marker)
1498 (- (marker-position a-begin-marker)
1499 offset)
1500 merge-buffer))
1501 (setq merge-end-marker (set-marker
1502 (make-marker)
1503 (- (marker-position a-end-marker)
1504 offset)
1505 merge-buffer))
1506 ;; record all the markers for this difference
1507 (setq marker-list (cons (vector a-begin-marker a-end-marker
1508 b-begin-marker b-end-marker
1509 merge-begin-marker merge-end-marker
1510 state)
1511 marker-list)))
1512 (setq lineno-list (cdr lineno-list)))
1513 ;; convert the list of difference information into a vector for
1514 ;; fast access
1515 (setq emerge-difference-list (apply 'vector (nreverse marker-list)))))
1517 ;; If we have an ancestor, select all B variants that we prefer
1518 (defun emerge-select-prefer-Bs ()
1519 (let ((n 0))
1520 (while (< n emerge-number-of-differences)
1521 (if (eq (aref (aref emerge-difference-list n) 6) 'prefer-B)
1522 (progn
1523 (emerge-unselect-and-select-difference n t)
1524 (emerge-select-B)
1525 (aset (aref emerge-difference-list n) 6 'prefer-B)))
1526 (setq n (1+ n))))
1527 (emerge-unselect-and-select-difference -1))
1529 ;; Process the local-variables list at the end of the merged file, if
1530 ;; requested.
1531 (defun emerge-handle-local-variables ()
1532 (if emerge-process-local-variables
1533 (condition-case err
1534 (hack-local-variables)
1535 (error (message "Local-variables error in merge buffer: %s"
1536 (prin1-to-string err))))))
1538 ;;; Common exit routines
1540 (defun emerge-write-and-delete (file-out)
1541 ;; clear screen format
1542 (delete-other-windows)
1543 ;; delete A, B, and ancestor buffers, if they haven't been changed
1544 (if (not (buffer-modified-p emerge-A-buffer))
1545 (kill-buffer emerge-A-buffer))
1546 (if (not (buffer-modified-p emerge-B-buffer))
1547 (kill-buffer emerge-B-buffer))
1548 (if (and emerge-ancestor-buffer
1549 (not (buffer-modified-p emerge-ancestor-buffer)))
1550 (kill-buffer emerge-ancestor-buffer))
1551 ;; Write merge buffer to file
1552 (and file-out
1553 (write-file file-out)))
1555 ;;; Commands
1557 (defun emerge-recenter (&optional arg)
1558 "Bring the highlighted region of all three merge buffers into view.
1559 This brings the buffers into view if they are in windows.
1560 With an argument, reestablish the default three-window display."
1561 (interactive "P")
1562 ;; If there is an argument, rebuild the window structure
1563 (if arg
1564 (emerge-setup-windows emerge-A-buffer emerge-B-buffer
1565 emerge-merge-buffer))
1566 ;; Redisplay whatever buffers are showing, if there is a selected difference
1567 (if (and (>= emerge-current-difference 0)
1568 (< emerge-current-difference emerge-number-of-differences))
1569 (let* ((merge-buffer emerge-merge-buffer)
1570 (buffer-A emerge-A-buffer)
1571 (buffer-B emerge-B-buffer)
1572 (window-A (get-buffer-window buffer-A 'visible))
1573 (window-B (get-buffer-window buffer-B 'visible))
1574 (merge-window (get-buffer-window merge-buffer))
1575 (diff-vector
1576 (aref emerge-difference-list emerge-current-difference)))
1577 (if window-A (progn
1578 (select-window window-A)
1579 (emerge-position-region
1580 (- (aref diff-vector 0)
1581 (1- emerge-before-flag-length))
1582 (+ (aref diff-vector 1)
1583 (1- emerge-after-flag-length))
1584 (1+ (aref diff-vector 0)))))
1585 (if window-B (progn
1586 (select-window window-B)
1587 (emerge-position-region
1588 (- (aref diff-vector 2)
1589 (1- emerge-before-flag-length))
1590 (+ (aref diff-vector 3)
1591 (1- emerge-after-flag-length))
1592 (1+ (aref diff-vector 2)))))
1593 (if merge-window (progn
1594 (select-window merge-window)
1595 (emerge-position-region
1596 (- (aref diff-vector 4)
1597 (1- emerge-before-flag-length))
1598 (+ (aref diff-vector 5)
1599 (1- emerge-after-flag-length))
1600 (1+ (aref diff-vector 4))))))))
1602 ;;; Window scrolling operations
1603 ;; These operations are designed to scroll all three windows the same amount,
1604 ;; so as to keep the text in them aligned.
1606 ;; Perform some operation on all three windows (if they are showing).
1607 ;; Catches all errors on the operation in the A and B windows, but not
1608 ;; in the merge window. Usually, errors come from scrolling off the
1609 ;; beginning or end of the buffer, and this gives a nice error message:
1610 ;; End of buffer is reported in the merge buffer, but if the scroll was
1611 ;; possible in the A or B windows, it is performed there before the error
1612 ;; is reported.
1613 (defun emerge-operate-on-windows (operation arg)
1614 (let* ((merge-buffer emerge-merge-buffer)
1615 (buffer-A emerge-A-buffer)
1616 (buffer-B emerge-B-buffer)
1617 (window-A (get-buffer-window buffer-A 'visible))
1618 (window-B (get-buffer-window buffer-B 'visible))
1619 (merge-window (get-buffer-window merge-buffer)))
1620 (if window-A (progn
1621 (select-window window-A)
1622 (condition-case nil
1623 (funcall operation arg)
1624 (error))))
1625 (if window-B (progn
1626 (select-window window-B)
1627 (condition-case nil
1628 (funcall operation arg)
1629 (error))))
1630 (if merge-window (progn
1631 (select-window merge-window)
1632 (funcall operation arg)))))
1634 (defun emerge-scroll-up (&optional arg)
1635 "Scroll up all three merge buffers, if they are in windows.
1636 With argument N, scroll N lines; otherwise scroll by nearly
1637 the height of the merge window.
1638 `C-u -' alone as argument scrolls half the height of the merge window."
1639 (interactive "P")
1640 (emerge-operate-on-windows
1641 'scroll-up
1642 ;; calculate argument to scroll-up
1643 ;; if there is an explicit argument
1644 (if (and arg (not (equal arg '-)))
1645 ;; use it
1646 (prefix-numeric-value arg)
1647 ;; if not, see if we can determine a default amount (the window height)
1648 (let ((merge-window (get-buffer-window emerge-merge-buffer)))
1649 (if (null merge-window)
1650 ;; no window, use nil
1652 (let ((default-amount
1653 (- (window-height merge-window) 1 next-screen-context-lines)))
1654 ;; the window was found
1655 (if arg
1656 ;; C-u as argument means half of default amount
1657 (/ default-amount 2)
1658 ;; no argument means default amount
1659 default-amount)))))))
1661 (defun emerge-scroll-down (&optional arg)
1662 "Scroll down all three merge buffers, if they are in windows.
1663 With argument N, scroll N lines; otherwise scroll by nearly
1664 the height of the merge window.
1665 `C-u -' alone as argument scrolls half the height of the merge window."
1666 (interactive "P")
1667 (emerge-operate-on-windows
1668 'scroll-down
1669 ;; calculate argument to scroll-down
1670 ;; if there is an explicit argument
1671 (if (and arg (not (equal arg '-)))
1672 ;; use it
1673 (prefix-numeric-value arg)
1674 ;; if not, see if we can determine a default amount (the window height)
1675 (let ((merge-window (get-buffer-window emerge-merge-buffer)))
1676 (if (null merge-window)
1677 ;; no window, use nil
1679 (let ((default-amount
1680 (- (window-height merge-window) 1 next-screen-context-lines)))
1681 ;; the window was found
1682 (if arg
1683 ;; C-u as argument means half of default amount
1684 (/ default-amount 2)
1685 ;; no argument means default amount
1686 default-amount)))))))
1688 (defun emerge-scroll-left (&optional arg)
1689 "Scroll left all three merge buffers, if they are in windows.
1690 If an argument is given, that is how many columns are scrolled, else nearly
1691 the width of the A and B windows. `C-u -' alone as argument scrolls half the
1692 width of the A and B windows."
1693 (interactive "P")
1694 (emerge-operate-on-windows
1695 'scroll-left
1696 ;; calculate argument to scroll-left
1697 ;; if there is an explicit argument
1698 (if (and arg (not (equal arg '-)))
1699 ;; use it
1700 (prefix-numeric-value arg)
1701 ;; if not, see if we can determine a default amount
1702 ;; (half the window width)
1703 (let ((merge-window (get-buffer-window emerge-merge-buffer)))
1704 (if (null merge-window)
1705 ;; no window, use nil
1707 (let ((default-amount
1708 (- (/ (window-width merge-window) 2) 3)))
1709 ;; the window was found
1710 (if arg
1711 ;; C-u as argument means half of default amount
1712 (/ default-amount 2)
1713 ;; no argument means default amount
1714 default-amount)))))))
1716 (defun emerge-scroll-right (&optional arg)
1717 "Scroll right all three merge buffers, if they are in windows.
1718 If an argument is given, that is how many columns are scrolled, else nearly
1719 the width of the A and B windows. `C-u -' alone as argument scrolls half the
1720 width of the A and B windows."
1721 (interactive "P")
1722 (emerge-operate-on-windows
1723 'scroll-right
1724 ;; calculate argument to scroll-right
1725 ;; if there is an explicit argument
1726 (if (and arg (not (equal arg '-)))
1727 ;; use it
1728 (prefix-numeric-value arg)
1729 ;; if not, see if we can determine a default amount
1730 ;; (half the window width)
1731 (let ((merge-window (get-buffer-window emerge-merge-buffer)))
1732 (if (null merge-window)
1733 ;; no window, use nil
1735 (let ((default-amount
1736 (- (/ (window-width merge-window) 2) 3)))
1737 ;; the window was found
1738 (if arg
1739 ;; C-u as argument means half of default amount
1740 (/ default-amount 2)
1741 ;; no argument means default amount
1742 default-amount)))))))
1744 (defun emerge-scroll-reset ()
1745 "Reset horizontal scrolling in Emerge.
1746 This resets the horizontal scrolling of all three merge buffers
1747 to the left margin, if they are in windows."
1748 (interactive)
1749 (emerge-operate-on-windows
1750 (lambda (x) (set-window-hscroll (selected-window) 0))
1751 nil))
1753 ;; Attempt to show the region nicely.
1754 ;; If there are min-lines lines above and below the region, then don't do
1755 ;; anything.
1756 ;; If not, recenter the region to make it so.
1757 ;; If that isn't possible, remove context lines balancedly from top and bottom
1758 ;; so the entire region shows.
1759 ;; If that isn't possible, show the top of the region.
1760 ;; BEG must be at the beginning of a line.
1761 (defun emerge-position-region (beg end pos)
1762 ;; First test whether the entire region is visible with
1763 ;; emerge-min-visible-lines above and below it
1764 (if (not (and (<= (progn
1765 (move-to-window-line emerge-min-visible-lines)
1766 (point))
1767 beg)
1768 (<= end (progn
1769 (move-to-window-line
1770 (- (1+ emerge-min-visible-lines)))
1771 (point)))))
1772 ;; We failed that test, see if it fits at all
1773 ;; Meanwhile positioning it correctly in case it doesn't fit
1774 (progn
1775 (set-window-start (selected-window) beg)
1776 (if (pos-visible-in-window-p end)
1777 ;; Determine the number of lines that the region occupies
1778 (let ((lines 0))
1779 (while (> end (progn
1780 (move-to-window-line lines)
1781 (point)))
1782 (setq lines (1+ lines)))
1783 ;; And position the beginning on the right line
1784 (goto-char beg)
1785 (recenter (/ (1+ (- (1- (window-height (selected-window)))
1786 lines))
1787 2))))))
1788 (goto-char pos))
1790 (defun emerge-next-difference ()
1791 "Advance to the next difference."
1792 (interactive)
1793 (if (< emerge-current-difference emerge-number-of-differences)
1794 (let ((n (1+ emerge-current-difference)))
1795 (while (and emerge-skip-prefers
1796 (< n emerge-number-of-differences)
1797 (memq (aref (aref emerge-difference-list n) 6)
1798 '(prefer-A prefer-B)))
1799 (setq n (1+ n)))
1800 (let ((buffer-read-only nil))
1801 (emerge-unselect-and-select-difference n)))
1802 (error "At end")))
1804 (defun emerge-previous-difference ()
1805 "Go to the previous difference."
1806 (interactive)
1807 (if (> emerge-current-difference -1)
1808 (let ((n (1- emerge-current-difference)))
1809 (while (and emerge-skip-prefers
1810 (> n -1)
1811 (memq (aref (aref emerge-difference-list n) 6)
1812 '(prefer-A prefer-B)))
1813 (setq n (1- n)))
1814 (let ((buffer-read-only nil))
1815 (emerge-unselect-and-select-difference n)))
1816 (error "At beginning")))
1818 (defun emerge-jump-to-difference (difference-number)
1819 "Go to the N-th difference."
1820 (interactive "p")
1821 (let ((buffer-read-only nil))
1822 (setq difference-number (1- difference-number))
1823 (if (and (>= difference-number -1)
1824 (< difference-number (1+ emerge-number-of-differences)))
1825 (emerge-unselect-and-select-difference difference-number)
1826 (error "Bad difference number"))))
1828 (defun emerge-abort ()
1829 "Abort the Emerge session."
1830 (interactive)
1831 (emerge-quit t))
1833 (defun emerge-quit (arg)
1834 "Finish the Emerge session and exit Emerge.
1835 Prefix argument means to abort rather than successfully finish.
1836 The difference depends on how the merge was started,
1837 but usually means to not write over one of the original files, or to signal
1838 to some process which invoked Emerge a failure code.
1840 Unselects the selected difference, if any, restores the read-only and modified
1841 flags of the merged file buffers, restores the local keymap of the merge
1842 buffer, and sets off various emerge flags. Using Emerge commands in this
1843 buffer after this will cause serious problems."
1844 (interactive "P")
1845 (if (prog1
1846 (y-or-n-p
1847 (if (not arg)
1848 "Do you really want to successfully finish this merge? "
1849 "Do you really want to abort this merge? "))
1850 (message ""))
1851 (emerge-really-quit arg)))
1853 ;; Perform the quit operations.
1854 (defun emerge-really-quit (arg)
1855 (setq buffer-read-only nil)
1856 (emerge-unselect-and-select-difference -1)
1857 (emerge-restore-buffer-characteristics)
1858 ;; null out the difference markers so they don't slow down future editing
1859 ;; operations
1860 (mapc (lambda (d)
1861 (set-marker (aref d 0) nil)
1862 (set-marker (aref d 1) nil)
1863 (set-marker (aref d 2) nil)
1864 (set-marker (aref d 3) nil)
1865 (set-marker (aref d 4) nil)
1866 (set-marker (aref d 5) nil))
1867 emerge-difference-list)
1868 ;; allow them to be garbage collected
1869 (setq emerge-difference-list nil)
1870 ;; restore the local map
1871 (use-local-map emerge-old-keymap)
1872 ;; turn off all the emerge modes
1873 (setq emerge-mode nil)
1874 (setq emerge-fast-mode nil)
1875 (setq emerge-edit-mode nil)
1876 (setq emerge-auto-advance nil)
1877 (setq emerge-skip-prefers nil)
1878 ;; restore mode line
1879 (kill-local-variable 'mode-line-buffer-identification)
1880 (let ((emerge-prefix-argument arg))
1881 (run-hooks 'emerge-quit-hook)))
1883 (defun emerge-select-A (&optional force)
1884 "Select the A variant of this difference.
1885 Refuses to function if this difference has been edited, i.e., if it
1886 is neither the A nor the B variant.
1887 A prefix argument forces the variant to be selected
1888 even if the difference has been edited."
1889 (interactive "P")
1890 (let ((operate
1891 (lambda ()
1892 (emerge-select-A-edit merge-begin merge-end A-begin A-end)
1893 (if emerge-auto-advance
1894 (emerge-next-difference))))
1895 (operate-no-change
1896 (lambda () (if emerge-auto-advance
1897 (emerge-next-difference)))))
1898 (emerge-select-version force operate-no-change operate operate)))
1900 ;; Actually select the A variant
1901 (defun emerge-select-A-edit (merge-begin merge-end A-begin A-end)
1902 (with-current-buffer
1903 emerge-merge-buffer
1904 (delete-region merge-begin merge-end)
1905 (goto-char merge-begin)
1906 (insert-buffer-substring emerge-A-buffer A-begin A-end)
1907 (goto-char merge-begin)
1908 (aset diff-vector 6 'A)
1909 (emerge-refresh-mode-line)))
1911 (defun emerge-select-B (&optional force)
1912 "Select the B variant of this difference.
1913 Refuses to function if this difference has been edited, i.e., if it
1914 is neither the A nor the B variant.
1915 A prefix argument forces the variant to be selected
1916 even if the difference has been edited."
1917 (interactive "P")
1918 (let ((operate
1919 (lambda ()
1920 (emerge-select-B-edit merge-begin merge-end B-begin B-end)
1921 (if emerge-auto-advance
1922 (emerge-next-difference))))
1923 (operate-no-change
1924 (lambda () (if emerge-auto-advance
1925 (emerge-next-difference)))))
1926 (emerge-select-version force operate operate-no-change operate)))
1928 ;; Actually select the B variant
1929 (defun emerge-select-B-edit (merge-begin merge-end B-begin B-end)
1930 (with-current-buffer
1931 emerge-merge-buffer
1932 (delete-region merge-begin merge-end)
1933 (goto-char merge-begin)
1934 (insert-buffer-substring emerge-B-buffer B-begin B-end)
1935 (goto-char merge-begin)
1936 (aset diff-vector 6 'B)
1937 (emerge-refresh-mode-line)))
1939 (defun emerge-default-A ()
1940 "Make the A variant the default from here down.
1941 This selects the A variant for all differences from here down in the buffer
1942 which are still defaulted, i.e., which the user has not selected and for
1943 which there is no preference."
1944 (interactive)
1945 (let ((buffer-read-only nil))
1946 (let ((selected-difference emerge-current-difference)
1947 (n (max emerge-current-difference 0)))
1948 (while (< n emerge-number-of-differences)
1949 (let ((diff-vector (aref emerge-difference-list n)))
1950 (if (eq (aref diff-vector 6) 'default-B)
1951 (progn
1952 (emerge-unselect-and-select-difference n t)
1953 (emerge-select-A)
1954 (aset diff-vector 6 'default-A))))
1955 (setq n (1+ n))
1956 (if (zerop (% n 10))
1957 (message "Setting default to A...%d" n)))
1958 (emerge-unselect-and-select-difference selected-difference)))
1959 (message "Default choice is now A"))
1961 (defun emerge-default-B ()
1962 "Make the B variant the default from here down.
1963 This selects the B variant for all differences from here down in the buffer
1964 which are still defaulted, i.e., which the user has not selected and for
1965 which there is no preference."
1966 (interactive)
1967 (let ((buffer-read-only nil))
1968 (let ((selected-difference emerge-current-difference)
1969 (n (max emerge-current-difference 0)))
1970 (while (< n emerge-number-of-differences)
1971 (let ((diff-vector (aref emerge-difference-list n)))
1972 (if (eq (aref diff-vector 6) 'default-A)
1973 (progn
1974 (emerge-unselect-and-select-difference n t)
1975 (emerge-select-B)
1976 (aset diff-vector 6 'default-B))))
1977 (setq n (1+ n))
1978 (if (zerop (% n 10))
1979 (message "Setting default to B...%d" n)))
1980 (emerge-unselect-and-select-difference selected-difference)))
1981 (message "Default choice is now B"))
1983 (defun emerge-fast-mode ()
1984 "Set fast mode, for Emerge.
1985 In this mode ordinary Emacs commands are disabled, and Emerge commands
1986 need not be prefixed with \\<emerge-fast-keymap>\\[emerge-basic-keymap]."
1987 (interactive)
1988 (setq buffer-read-only t)
1989 (use-local-map emerge-fast-keymap)
1990 (setq emerge-mode t)
1991 (setq emerge-fast-mode t)
1992 (setq emerge-edit-mode nil)
1993 (message "Fast mode set")
1994 (force-mode-line-update))
1996 (defun emerge-edit-mode ()
1997 "Set edit mode, for Emerge.
1998 In this mode ordinary Emacs commands are available, and Emerge commands
1999 must be prefixed with \\<emerge-fast-keymap>\\[emerge-basic-keymap]."
2000 (interactive)
2001 (setq buffer-read-only nil)
2002 (use-local-map emerge-edit-keymap)
2003 (setq emerge-mode t)
2004 (setq emerge-fast-mode nil)
2005 (setq emerge-edit-mode t)
2006 (message "Edit mode set")
2007 (force-mode-line-update))
2009 (defun emerge-auto-advance (arg)
2010 "Toggle Auto-Advance mode, for Emerge.
2011 This mode causes `emerge-select-A' and `emerge-select-B' to automatically
2012 advance to the next difference.
2013 With a positive argument, turn on Auto-Advance mode.
2014 With a negative argument, turn off Auto-Advance mode."
2015 (interactive "P")
2016 (setq emerge-auto-advance (if (null arg)
2017 (not emerge-auto-advance)
2018 (> (prefix-numeric-value arg) 0)))
2019 (message (if emerge-auto-advance
2020 "Auto-advance set"
2021 "Auto-advance cleared"))
2022 (force-mode-line-update))
2024 (defun emerge-skip-prefers (arg)
2025 "Toggle Skip-Prefers mode, for Emerge.
2026 This mode causes `emerge-next-difference' and `emerge-previous-difference'
2027 to automatically skip over differences for which there is a preference.
2028 With a positive argument, turn on Skip-Prefers mode.
2029 With a negative argument, turn off Skip-Prefers mode."
2030 (interactive "P")
2031 (setq emerge-skip-prefers (if (null arg)
2032 (not emerge-skip-prefers)
2033 (> (prefix-numeric-value arg) 0)))
2034 (message (if emerge-skip-prefers
2035 "Skip-prefers set"
2036 "Skip-prefers cleared"))
2037 (force-mode-line-update))
2039 (defun emerge-copy-as-kill-A ()
2040 "Put the A variant of this difference in the kill ring."
2041 (interactive)
2042 (emerge-validate-difference)
2043 (let* ((diff-vector
2044 (aref emerge-difference-list emerge-current-difference))
2045 (A-begin (1+ (aref diff-vector 0)))
2046 (A-end (1- (aref diff-vector 1)))
2047 ;; so further kills don't append
2048 this-command)
2049 (with-current-buffer emerge-A-buffer
2050 (copy-region-as-kill A-begin A-end))))
2052 (defun emerge-copy-as-kill-B ()
2053 "Put the B variant of this difference in the kill ring."
2054 (interactive)
2055 (emerge-validate-difference)
2056 (let* ((diff-vector
2057 (aref emerge-difference-list emerge-current-difference))
2058 (B-begin (1+ (aref diff-vector 2)))
2059 (B-end (1- (aref diff-vector 3)))
2060 ;; so further kills don't append
2061 this-command)
2062 (with-current-buffer emerge-B-buffer
2063 (copy-region-as-kill B-begin B-end))))
2065 (defun emerge-insert-A (arg)
2066 "Insert the A variant of this difference at the point.
2067 Leaves point after text, mark before.
2068 With prefix argument, puts point before, mark after."
2069 (interactive "P")
2070 (emerge-validate-difference)
2071 (let* ((diff-vector
2072 (aref emerge-difference-list emerge-current-difference))
2073 (A-begin (1+ (aref diff-vector 0)))
2074 (A-end (1- (aref diff-vector 1)))
2075 (opoint (point))
2076 (buffer-read-only nil))
2077 (insert-buffer-substring emerge-A-buffer A-begin A-end)
2078 (if (not arg)
2079 (set-mark opoint)
2080 (set-mark (point))
2081 (goto-char opoint))))
2083 (defun emerge-insert-B (arg)
2084 "Insert the B variant of this difference at the point.
2085 Leaves point after text, mark before.
2086 With prefix argument, puts point before, mark after."
2087 (interactive "P")
2088 (emerge-validate-difference)
2089 (let* ((diff-vector
2090 (aref emerge-difference-list emerge-current-difference))
2091 (B-begin (1+ (aref diff-vector 2)))
2092 (B-end (1- (aref diff-vector 3)))
2093 (opoint (point))
2094 (buffer-read-only nil))
2095 (insert-buffer-substring emerge-B-buffer B-begin B-end)
2096 (if (not arg)
2097 (set-mark opoint)
2098 (set-mark (point))
2099 (goto-char opoint))))
2101 (defun emerge-mark-difference (arg)
2102 "Leaves the point before this difference and the mark after it.
2103 With prefix argument, puts mark before, point after."
2104 (interactive "P")
2105 (emerge-validate-difference)
2106 (let* ((diff-vector
2107 (aref emerge-difference-list emerge-current-difference))
2108 (merge-begin (1+ (aref diff-vector 4)))
2109 (merge-end (1- (aref diff-vector 5))))
2110 (if (not arg)
2111 (progn
2112 (goto-char merge-begin)
2113 (set-mark merge-end))
2114 (goto-char merge-end)
2115 (set-mark merge-begin))))
2117 (defun emerge-file-names ()
2118 "Show the names of the buffers or files being operated on by Emerge.
2119 Use C-u l to reset the windows afterward."
2120 (interactive)
2121 (delete-other-windows)
2122 (let ((temp-buffer-show-function
2123 (lambda (buf)
2124 (split-window-vertically)
2125 (switch-to-buffer buf)
2126 (other-window 1))))
2127 (with-output-to-temp-buffer "*Help*"
2128 (with-current-buffer emerge-A-buffer
2129 (if buffer-file-name
2130 (progn
2131 (princ "File A is: ")
2132 (princ buffer-file-name))
2133 (progn
2134 (princ "Buffer A is: ")
2135 (princ (buffer-name))))
2136 (princ "\n"))
2137 (with-current-buffer emerge-B-buffer
2138 (if buffer-file-name
2139 (progn
2140 (princ "File B is: ")
2141 (princ buffer-file-name))
2142 (progn
2143 (princ "Buffer B is: ")
2144 (princ (buffer-name))))
2145 (princ "\n"))
2146 (if emerge-ancestor-buffer
2147 (with-current-buffer emerge-ancestor-buffer
2148 (if buffer-file-name
2149 (progn
2150 (princ "Ancestor file is: ")
2151 (princ buffer-file-name))
2152 (progn
2153 (princ "Ancestor buffer is: ")
2154 (princ (buffer-name))))
2155 (princ "\n")))
2156 (princ emerge-output-description)
2157 (with-current-buffer standard-output
2158 (help-mode)))))
2160 (defun emerge-join-differences (arg)
2161 "Join the selected difference with the following one.
2162 With a prefix argument, join with the preceding one."
2163 (interactive "P")
2164 (let ((n emerge-current-difference))
2165 ;; adjust n to be first difference to join
2166 (if arg
2167 (setq n (1- n)))
2168 ;; n and n+1 are the differences to join
2169 ;; check that they are both differences
2170 (if (or (< n 0) (>= n (1- emerge-number-of-differences)))
2171 (error "Incorrect differences to join"))
2172 ;; remove the flags
2173 (emerge-unselect-difference emerge-current-difference)
2174 ;; decrement total number of differences
2175 (setq emerge-number-of-differences (1- emerge-number-of-differences))
2176 ;; build new differences vector
2177 (let ((i 0)
2178 (new-differences (make-vector emerge-number-of-differences nil)))
2179 (while (< i emerge-number-of-differences)
2180 (aset new-differences i
2181 (cond
2182 ((< i n) (aref emerge-difference-list i))
2183 ((> i n) (aref emerge-difference-list (1+ i)))
2184 (t (let ((prev (aref emerge-difference-list i))
2185 (next (aref emerge-difference-list (1+ i))))
2186 (vector (aref prev 0)
2187 (aref next 1)
2188 (aref prev 2)
2189 (aref next 3)
2190 (aref prev 4)
2191 (aref next 5)
2192 (let ((ps (aref prev 6))
2193 (ns (aref next 6)))
2194 (cond
2195 ((eq ps ns)
2197 ((and (or (eq ps 'B) (eq ps 'prefer-B))
2198 (or (eq ns 'B) (eq ns 'prefer-B)))
2200 (t 'A))))))))
2201 (setq i (1+ i)))
2202 (setq emerge-difference-list new-differences))
2203 ;; set the current difference correctly
2204 (setq emerge-current-difference n)
2205 ;; fix the mode line
2206 (emerge-refresh-mode-line)
2207 ;; reinsert the flags
2208 (emerge-select-difference emerge-current-difference)
2209 (emerge-recenter)))
2211 (defun emerge-split-difference ()
2212 "Split the current difference where the points are in the three windows."
2213 (interactive)
2214 (let ((n emerge-current-difference))
2215 ;; check that this is a valid difference
2216 (emerge-validate-difference)
2217 ;; get the point values and old difference
2218 (let ((A-point (with-current-buffer emerge-A-buffer
2219 (point-marker)))
2220 (B-point (with-current-buffer emerge-B-buffer
2221 (point-marker)))
2222 (merge-point (point-marker))
2223 (old-diff (aref emerge-difference-list n)))
2224 ;; check location of the points, give error if they aren't in the
2225 ;; differences
2226 (if (or (< A-point (aref old-diff 0))
2227 (> A-point (aref old-diff 1)))
2228 (error "Point outside of difference in A buffer"))
2229 (if (or (< B-point (aref old-diff 2))
2230 (> B-point (aref old-diff 3)))
2231 (error "Point outside of difference in B buffer"))
2232 (if (or (< merge-point (aref old-diff 4))
2233 (> merge-point (aref old-diff 5)))
2234 (error "Point outside of difference in merge buffer"))
2235 ;; remove the flags
2236 (emerge-unselect-difference emerge-current-difference)
2237 ;; increment total number of differences
2238 (setq emerge-number-of-differences (1+ emerge-number-of-differences))
2239 ;; build new differences vector
2240 (let ((i 0)
2241 (new-differences (make-vector emerge-number-of-differences nil)))
2242 (while (< i emerge-number-of-differences)
2243 (aset new-differences i
2244 (cond
2245 ((< i n)
2246 (aref emerge-difference-list i))
2247 ((> i (1+ n))
2248 (aref emerge-difference-list (1- i)))
2249 ((= i n)
2250 (vector (aref old-diff 0)
2251 A-point
2252 (aref old-diff 2)
2253 B-point
2254 (aref old-diff 4)
2255 merge-point
2256 (aref old-diff 6)))
2258 (vector (copy-marker A-point)
2259 (aref old-diff 1)
2260 (copy-marker B-point)
2261 (aref old-diff 3)
2262 (copy-marker merge-point)
2263 (aref old-diff 5)
2264 (aref old-diff 6)))))
2265 (setq i (1+ i)))
2266 (setq emerge-difference-list new-differences))
2267 ;; set the current difference correctly
2268 (setq emerge-current-difference n)
2269 ;; fix the mode line
2270 (emerge-refresh-mode-line)
2271 ;; reinsert the flags
2272 (emerge-select-difference emerge-current-difference)
2273 (emerge-recenter))))
2275 (defun emerge-trim-difference ()
2276 "Trim lines off top and bottom of difference that are the same.
2277 If lines are the same in both the A and the B versions, strip them off.
2278 \(This can happen when the A and B versions have common lines that the
2279 ancestor version does not share.)"
2280 (interactive)
2281 ;; make sure we are in a real difference
2282 (emerge-validate-difference)
2283 ;; remove the flags
2284 (emerge-unselect-difference emerge-current-difference)
2285 (let* ((diff (aref emerge-difference-list emerge-current-difference))
2286 (top-a (marker-position (aref diff 0)))
2287 (bottom-a (marker-position (aref diff 1)))
2288 (top-b (marker-position (aref diff 2)))
2289 (bottom-b (marker-position (aref diff 3)))
2290 (top-m (marker-position (aref diff 4)))
2291 (bottom-m (marker-position (aref diff 5)))
2292 size success sa sb sm)
2293 ;; move down the tops of the difference regions as much as possible
2294 ;; Try advancing comparing 1000 chars at a time.
2295 ;; When that fails, go 500 chars at a time, and so on.
2296 (setq size 1000)
2297 (while (> size 0)
2298 (setq success t)
2299 (while success
2300 (setq size (min size (- bottom-a top-a) (- bottom-b top-b)
2301 (- bottom-m top-m)))
2302 (setq sa (with-current-buffer emerge-A-buffer
2303 (buffer-substring top-a
2304 (+ size top-a))))
2305 (setq sb (with-current-buffer emerge-B-buffer
2306 (buffer-substring top-b
2307 (+ size top-b))))
2308 (setq sm (buffer-substring top-m (+ size top-m)))
2309 (setq success (and (> size 0) (equal sa sb) (equal sb sm)))
2310 (if success
2311 (setq top-a (+ top-a size)
2312 top-b (+ top-b size)
2313 top-m (+ top-m size))))
2314 (setq size (/ size 2)))
2315 ;; move up the bottoms of the difference regions as much as possible
2316 ;; Try advancing comparing 1000 chars at a time.
2317 ;; When that fails, go 500 chars at a time, and so on.
2318 (setq size 1000)
2319 (while (> size 0)
2320 (setq success t)
2321 (while success
2322 (setq size (min size (- bottom-a top-a) (- bottom-b top-b)
2323 (- bottom-m top-m)))
2324 (setq sa (with-current-buffer emerge-A-buffer
2325 (buffer-substring (- bottom-a size)
2326 bottom-a)))
2327 (setq sb (with-current-buffer emerge-B-buffer
2328 (buffer-substring (- bottom-b size)
2329 bottom-b)))
2330 (setq sm (buffer-substring (- bottom-m size) bottom-m))
2331 (setq success (and (> size 0) (equal sa sb) (equal sb sm)))
2332 (if success
2333 (setq bottom-a (- bottom-a size)
2334 bottom-b (- bottom-b size)
2335 bottom-m (- bottom-m size))))
2336 (setq size (/ size 2)))
2337 ;; {top,bottom}-{a,b,m} are now set at the new beginnings and ends
2338 ;; of the difference regions. Move them to the beginning of lines, as
2339 ;; appropriate.
2340 (with-current-buffer emerge-A-buffer
2341 (goto-char top-a)
2342 (beginning-of-line)
2343 (aset diff 0 (point-marker))
2344 (goto-char bottom-a)
2345 (beginning-of-line 2)
2346 (aset diff 1 (point-marker)))
2347 (with-current-buffer emerge-B-buffer
2348 (goto-char top-b)
2349 (beginning-of-line)
2350 (aset diff 2 (point-marker))
2351 (goto-char bottom-b)
2352 (beginning-of-line 2)
2353 (aset diff 3 (point-marker)))
2354 (goto-char top-m)
2355 (beginning-of-line)
2356 (aset diff 4 (point-marker))
2357 (goto-char bottom-m)
2358 (beginning-of-line 2)
2359 (aset diff 5 (point-marker))
2360 ;; put the flags back in, recenter the display
2361 (emerge-select-difference emerge-current-difference)
2362 (emerge-recenter)))
2364 ;; FIXME the manual advertised this as working in the A or B buffers,
2365 ;; but it does not, because all the buffer locals are nil there.
2366 ;; It would work to call it from the merge buffer and specify that one
2367 ;; wants to use the value of point in the A or B buffer.
2368 ;; But with the prefix argument already in use, there is no easy way
2369 ;; to have it ask for a buffer.
2370 (defun emerge-find-difference (arg)
2371 "Find the difference containing the current position of the point.
2372 If there is no containing difference and the prefix argument is positive,
2373 it finds the nearest following difference. A negative prefix argument finds
2374 the nearest previous difference."
2375 (interactive "P")
2376 (cond ((eq (current-buffer) emerge-A-buffer)
2377 (emerge-find-difference-A arg))
2378 ((eq (current-buffer) emerge-B-buffer)
2379 (emerge-find-difference-B arg))
2380 (t (emerge-find-difference-merge arg))))
2382 (defun emerge-find-difference-merge (arg)
2383 "Find the difference containing point, in the merge buffer.
2384 If there is no containing difference and the prefix argument is positive,
2385 it finds the nearest following difference. A negative prefix argument finds
2386 the nearest previous difference."
2387 (interactive "P")
2388 ;; search for the point in the merge buffer, using the markers
2389 ;; for the beginning and end of the differences in the merge buffer
2390 (emerge-find-difference1 arg (point) 4 5))
2392 (defun emerge-find-difference-A (arg)
2393 "Find the difference containing point, in the A buffer.
2394 This command must be executed in the merge buffer.
2395 If there is no containing difference and the prefix argument is positive,
2396 it finds the nearest following difference. A negative prefix argument finds
2397 the nearest previous difference."
2398 (interactive "P")
2399 ;; search for the point in the A buffer, using the markers
2400 ;; for the beginning and end of the differences in the A buffer
2401 (emerge-find-difference1 arg
2402 (with-current-buffer emerge-A-buffer (point))
2403 0 1))
2405 (defun emerge-find-difference-B (arg)
2406 "Find the difference containing point, in the B buffer.
2407 This command must be executed in the merge buffer.
2408 If there is no containing difference and the prefix argument is positive,
2409 it finds the nearest following difference. A negative prefix argument finds
2410 the nearest previous difference."
2411 (interactive "P")
2412 ;; search for the point in the B buffer, using the markers
2413 ;; for the beginning and end of the differences in the B buffer
2414 (emerge-find-difference1 arg
2415 (with-current-buffer emerge-B-buffer (point))
2416 2 3))
2418 (defun emerge-find-difference1 (arg location begin end)
2419 (let* ((index
2420 ;; find first difference containing or after the current position
2421 (catch 'search
2422 (let ((n 0))
2423 (while (< n emerge-number-of-differences)
2424 (let ((diff-vector (aref emerge-difference-list n)))
2425 (if (<= location (marker-position (aref diff-vector end)))
2426 (throw 'search n)))
2427 (setq n (1+ n))))
2428 emerge-number-of-differences))
2429 (contains
2430 ;; whether the found difference contains the current position
2431 (and (< index emerge-number-of-differences)
2432 (<= (marker-position (aref (aref emerge-difference-list index)
2433 begin))
2434 location)))
2435 (arg-value
2436 ;; numeric value of prefix argument
2437 (prefix-numeric-value arg)))
2438 (emerge-unselect-and-select-difference
2439 (cond
2440 ;; if the point is in a difference, select it
2441 (contains index)
2442 ;; if the arg is nil and the point is not in a difference, error
2443 ((null arg) (error "No difference contains point"))
2444 ;; if the arg is positive, select the following difference
2445 ((> arg-value 0)
2446 (if (< index emerge-number-of-differences)
2447 index
2448 (error "No difference contains or follows point")))
2449 ;; if the arg is negative, select the preceding difference
2451 (if (> index 0)
2452 (1- index)
2453 (error "No difference contains or precedes point")))))))
2455 (defun emerge-line-numbers ()
2456 "Display the current line numbers.
2457 This function displays the line numbers of the points in the A, B, and
2458 merge buffers."
2459 (interactive)
2460 (let* ((valid-diff
2461 (and (>= emerge-current-difference 0)
2462 (< emerge-current-difference emerge-number-of-differences)))
2463 (emerge-line-diff (and valid-diff
2464 (aref emerge-difference-list
2465 emerge-current-difference)))
2466 (merge-line (emerge-line-number-in-buf 4 5))
2467 (A-line (with-current-buffer emerge-A-buffer
2468 (emerge-line-number-in-buf 0 1)))
2469 (B-line (with-current-buffer emerge-B-buffer
2470 (emerge-line-number-in-buf 2 3))))
2471 (message "At lines: merge = %d, A = %d, B = %d"
2472 merge-line A-line B-line)))
2474 (defvar emerge-line-diff)
2476 (defun emerge-line-number-in-buf (begin-marker end-marker)
2477 ;; FIXME point-min rather than 1? widen?
2478 (let ((temp (1+ (count-lines 1 (line-beginning-position)))))
2479 (if valid-diff
2480 (progn
2481 (if (> (point) (aref emerge-line-diff begin-marker))
2482 (setq temp (- temp emerge-before-flag-lines)))
2483 (if (> (point) (aref emerge-line-diff end-marker))
2484 (setq temp (- temp emerge-after-flag-lines)))))
2485 temp))
2487 (defun emerge-set-combine-template (string &optional localize)
2488 "Set `emerge-combine-versions-template' to STRING.
2489 This value controls how `emerge-combine-versions' combines the two versions.
2490 With prefix argument, `emerge-combine-versions-template' is made local to this
2491 merge buffer. Localization is permanent for any particular merge buffer."
2492 (interactive "s\nP")
2493 (if localize
2494 (make-local-variable 'emerge-combine-versions-template))
2495 (setq emerge-combine-versions-template string)
2496 (message
2497 (if (assq 'emerge-combine-versions-template (buffer-local-variables))
2498 "emerge-set-combine-versions-template set locally"
2499 "emerge-set-combine-versions-template set")))
2501 (defun emerge-set-combine-versions-template (start end &optional localize)
2502 "Copy region into `emerge-combine-versions-template'.
2503 This controls how `emerge-combine-versions' will combine the two versions.
2504 With prefix argument, `emerge-combine-versions-template' is made local to this
2505 merge buffer. Localization is permanent for any particular merge buffer."
2506 (interactive "r\nP")
2507 (if localize
2508 (make-local-variable 'emerge-combine-versions-template))
2509 (setq emerge-combine-versions-template (buffer-substring start end))
2510 (message
2511 (if (assq 'emerge-combine-versions-template (buffer-local-variables))
2512 "emerge-set-combine-versions-template set locally."
2513 "emerge-set-combine-versions-template set.")))
2515 (defun emerge-combine-versions (&optional force)
2516 "Combine versions using the template in `emerge-combine-versions-template'.
2517 Refuses to function if this difference has been edited, i.e., if it is
2518 neither the A nor the B variant.
2519 An argument forces the variant to be selected even if the difference has
2520 been edited."
2521 (interactive "P")
2522 (emerge-combine-versions-internal emerge-combine-versions-template force))
2524 (defun emerge-combine-versions-register (char &optional force)
2525 "Combine the two versions using the template in register REG.
2526 See documentation of the variable `emerge-combine-versions-template'
2527 for how the template is interpreted.
2528 Refuses to function if this difference has been edited, i.e., if it is
2529 neither the A nor the B variant.
2530 An argument forces the variant to be selected even if the difference has
2531 been edited."
2532 (interactive "cRegister containing template: \nP")
2533 (let ((template (get-register char)))
2534 (if (not (stringp template))
2535 (error "Register does not contain text"))
2536 (emerge-combine-versions-internal template force)))
2538 (defun emerge-combine-versions-internal (emerge-combine-template force)
2539 (let ((operate
2540 (lambda ()
2541 (emerge-combine-versions-edit merge-begin merge-end
2542 A-begin A-end B-begin B-end)
2543 (if emerge-auto-advance
2544 (emerge-next-difference)))))
2545 (emerge-select-version force operate operate operate)))
2547 (defvar emerge-combine-template)
2549 (defun emerge-combine-versions-edit (merge-begin merge-end
2550 A-begin A-end B-begin B-end)
2551 (with-current-buffer
2552 emerge-merge-buffer
2553 (delete-region merge-begin merge-end)
2554 (goto-char merge-begin)
2555 (let ((i 0))
2556 (while (< i (length emerge-combine-template))
2557 (let ((c (aref emerge-combine-template i)))
2558 (if (= c ?%)
2559 (progn
2560 (setq i (1+ i))
2561 (setq c
2562 (condition-case nil
2563 (aref emerge-combine-template i)
2564 (error ?%)))
2565 (cond ((= c ?a)
2566 (insert-buffer-substring emerge-A-buffer A-begin A-end))
2567 ((= c ?b)
2568 (insert-buffer-substring emerge-B-buffer B-begin B-end))
2569 ((= c ?%)
2570 (insert ?%))
2572 (insert c))))
2573 (insert c)))
2574 (setq i (1+ i))))
2575 (goto-char merge-begin)
2576 (aset diff-vector 6 'combined)
2577 (emerge-refresh-mode-line)))
2579 (defun emerge-set-merge-mode (mode)
2580 "Set the major mode in a merge buffer.
2581 Overrides any change that the mode might make to the mode line or local
2582 keymap. Leaves merge in fast mode."
2583 (interactive
2584 (list (intern (completing-read "New major mode for merge buffer: "
2585 obarray 'commandp t nil))))
2586 (funcall mode)
2587 (emerge-refresh-mode-line)
2588 (if emerge-fast-mode
2589 (emerge-fast-mode)
2590 (emerge-edit-mode)))
2592 (defun emerge-one-line-window ()
2593 (interactive)
2594 (let ((window-min-height 1))
2595 (shrink-window (- (window-height) 2))))
2597 ;;; Support routines
2599 ;; Select a difference by placing the visual flags around the appropriate
2600 ;; group of lines in the A, B, and merge buffers
2601 (defun emerge-select-difference (n)
2602 (let ((emerge-globalized-difference-list emerge-difference-list)
2603 (emerge-globalized-number-of-differences emerge-number-of-differences))
2604 (emerge-place-flags-in-buffer emerge-A-buffer n 0 1)
2605 (emerge-place-flags-in-buffer emerge-B-buffer n 2 3)
2606 (emerge-place-flags-in-buffer nil n 4 5))
2607 (run-hooks 'emerge-select-hook))
2609 (defun emerge-place-flags-in-buffer (buffer difference before-index
2610 after-index)
2611 (if buffer
2612 (with-current-buffer
2613 buffer
2614 (emerge-place-flags-in-buffer1 difference before-index after-index))
2615 (emerge-place-flags-in-buffer1 difference before-index after-index)))
2617 (defun emerge-place-flags-in-buffer1 (difference before-index after-index)
2618 (let ((buffer-read-only nil))
2619 ;; insert the flag before the difference
2620 (let ((before (aref (aref emerge-globalized-difference-list difference)
2621 before-index))
2622 here)
2623 (goto-char before)
2624 ;; insert the flag itself
2625 (insert-before-markers emerge-before-flag)
2626 (setq here (point))
2627 ;; Put the marker(s) referring to this position 1 character before the
2628 ;; end of the flag, so it won't be damaged by the user.
2629 ;; This gets a bit tricky, as there could be a number of markers
2630 ;; that have to be moved.
2631 (set-marker before (1- before))
2632 (let ((n (1- difference)) after-marker before-marker diff-list)
2633 (while (and
2634 (>= n 0)
2635 (progn
2636 (setq diff-list (aref emerge-globalized-difference-list n)
2637 after-marker (aref diff-list after-index))
2638 (= after-marker here)))
2639 (set-marker after-marker (1- after-marker))
2640 (setq before-marker (aref diff-list before-index))
2641 (if (= before-marker here)
2642 (setq before-marker (1- before-marker)))
2643 (setq n (1- n)))))
2644 ;; insert the flag after the difference
2645 (let* ((after (aref (aref emerge-globalized-difference-list difference)
2646 after-index))
2647 (here (marker-position after)))
2648 (goto-char here)
2649 ;; insert the flag itself
2650 (insert emerge-after-flag)
2651 ;; Put the marker(s) referring to this position 1 character after the
2652 ;; beginning of the flag, so it won't be damaged by the user.
2653 ;; This gets a bit tricky, as there could be a number of markers
2654 ;; that have to be moved.
2655 (set-marker after (1+ after))
2656 (let ((n (1+ difference)) before-marker after-marker diff-list)
2657 (while (and
2658 (< n emerge-globalized-number-of-differences)
2659 (progn
2660 (setq diff-list (aref emerge-globalized-difference-list n)
2661 before-marker (aref diff-list before-index))
2662 (= before-marker here)))
2663 (set-marker before-marker (1+ before-marker))
2664 (setq after-marker (aref diff-list after-index))
2665 (if (= after-marker here)
2666 (setq after-marker (1+ after-marker)))
2667 (setq n (1+ n)))))))
2669 ;; Unselect a difference by removing the visual flags in the buffers.
2670 (defun emerge-unselect-difference (n)
2671 (let ((diff-vector (aref emerge-difference-list n)))
2672 (emerge-remove-flags-in-buffer emerge-A-buffer
2673 (aref diff-vector 0) (aref diff-vector 1))
2674 (emerge-remove-flags-in-buffer emerge-B-buffer
2675 (aref diff-vector 2) (aref diff-vector 3))
2676 (emerge-remove-flags-in-buffer emerge-merge-buffer
2677 (aref diff-vector 4) (aref diff-vector 5)))
2678 (run-hooks 'emerge-unselect-hook))
2680 (defun emerge-remove-flags-in-buffer (buffer before after)
2681 (with-current-buffer
2682 buffer
2683 (let ((buffer-read-only nil))
2684 ;; remove the flags, if they're there
2685 (goto-char (- before (1- emerge-before-flag-length)))
2686 (if (looking-at emerge-before-flag-match)
2687 (delete-char emerge-before-flag-length)
2688 ;; the flag isn't there
2689 (ding)
2690 (message "Trouble removing flag"))
2691 (goto-char (1- after))
2692 (if (looking-at emerge-after-flag-match)
2693 (delete-char emerge-after-flag-length)
2694 ;; the flag isn't there
2695 (ding)
2696 (message "Trouble removing flag")))))
2698 ;; Select a difference, removing any flags that exist now.
2699 (defun emerge-unselect-and-select-difference (n &optional suppress-display)
2700 (if (and (>= emerge-current-difference 0)
2701 (< emerge-current-difference emerge-number-of-differences))
2702 (emerge-unselect-difference emerge-current-difference))
2703 (if (and (>= n 0) (< n emerge-number-of-differences))
2704 (progn
2705 (emerge-select-difference n)
2706 (let* ((diff-vector (aref emerge-difference-list n))
2707 (selection-type (aref diff-vector 6)))
2708 (if (eq selection-type 'default-A)
2709 (aset diff-vector 6 'A)
2710 (if (eq selection-type 'default-B)
2711 (aset diff-vector 6 'B))))))
2712 (setq emerge-current-difference n)
2713 (if (not suppress-display)
2714 (progn
2715 (emerge-recenter)
2716 (emerge-refresh-mode-line))))
2718 ;; Perform tests to see whether user should be allowed to select a version
2719 ;; of this difference:
2720 ;; a valid difference has been selected; and
2721 ;; the difference text in the merge buffer is:
2722 ;; the A version (execute a-version), or
2723 ;; the B version (execute b-version), or
2724 ;; empty (execute neither-version), or
2725 ;; argument FORCE is true (execute neither-version)
2726 ;; Otherwise, signal an error.
2727 (defun emerge-select-version (force a-version b-version neither-version)
2728 (emerge-validate-difference)
2729 (let ((buffer-read-only nil))
2730 (let* ((diff-vector
2731 (aref emerge-difference-list emerge-current-difference))
2732 (A-begin (1+ (aref diff-vector 0)))
2733 (A-end (1- (aref diff-vector 1)))
2734 (B-begin (1+ (aref diff-vector 2)))
2735 (B-end (1- (aref diff-vector 3)))
2736 (merge-begin (1+ (aref diff-vector 4)))
2737 (merge-end (1- (aref diff-vector 5))))
2738 (if (emerge-compare-buffers emerge-A-buffer A-begin A-end
2739 emerge-merge-buffer merge-begin
2740 merge-end)
2741 (funcall a-version)
2742 (if (emerge-compare-buffers emerge-B-buffer B-begin B-end
2743 emerge-merge-buffer merge-begin
2744 merge-end)
2745 (funcall b-version)
2746 (if (or force (= merge-begin merge-end))
2747 (funcall neither-version)
2748 (error "This difference region has been edited")))))))
2750 ;; Read a file name, handling all of the various defaulting rules.
2752 (defun emerge-read-file-name (prompt alternative-default-dir default-file
2753 A-file must-match)
2754 ;; `prompt' should not have trailing ": ", so that it can be modified
2755 ;; according to context.
2756 ;; If alternative-default-dir is non-nil, it should be used as the default
2757 ;; directory instead if default-directory, if emerge-default-last-directories
2758 ;; is set.
2759 ;; If default-file is set, it should be used as the default value.
2760 ;; If A-file is set, and its directory is different from
2761 ;; alternative-default-dir, and if emerge-default-last-directories is set,
2762 ;; the default file should be the last part of A-file in the default
2763 ;; directory. (Overriding default-file.)
2764 (cond
2765 ;; If this is not the A-file argument (shown by non-nil A-file), and
2766 ;; if emerge-default-last-directories is set, and
2767 ;; the default directory exists but is not the same as the directory of the
2768 ;; A-file,
2769 ;; then make the default file have the same name as the A-file, but in
2770 ;; the default directory.
2771 ((and emerge-default-last-directories
2772 A-file
2773 alternative-default-dir
2774 (not (string-equal alternative-default-dir
2775 (file-name-directory A-file))))
2776 (read-file-name (format "%s (default %s): "
2777 prompt (file-name-nondirectory A-file))
2778 alternative-default-dir
2779 (concat alternative-default-dir
2780 (file-name-nondirectory A-file))
2781 (and must-match 'confirm)))
2782 ;; If there is a default file, use it.
2783 (default-file
2784 (read-file-name (format "%s (default %s): " prompt default-file)
2785 ;; If emerge-default-last-directories is set, use the
2786 ;; directory from the same argument of the last call of
2787 ;; Emerge as the default for this argument.
2788 (and emerge-default-last-directories
2789 alternative-default-dir)
2790 default-file (and must-match 'confirm)))
2792 (read-file-name (concat prompt ": ")
2793 ;; If emerge-default-last-directories is set, use the
2794 ;; directory from the same argument of the last call of
2795 ;; Emerge as the default for this argument.
2796 (and emerge-default-last-directories
2797 alternative-default-dir)
2798 nil (and must-match 'confirm)))))
2800 ;; Revise the mode line to display which difference we have selected
2802 (defun emerge-refresh-mode-line ()
2803 (setq mode-line-buffer-identification
2804 (list (format "Emerge: %%b diff %d of %d%s"
2805 (1+ emerge-current-difference)
2806 emerge-number-of-differences
2807 (if (and (>= emerge-current-difference 0)
2808 (< emerge-current-difference
2809 emerge-number-of-differences))
2810 (cdr (assq (aref (aref emerge-difference-list
2811 emerge-current-difference)
2813 '((A . " - A")
2814 (B . " - B")
2815 (prefer-A . " - A*")
2816 (prefer-B . " - B*")
2817 (combined . " - comb"))))
2818 ""))))
2819 (force-mode-line-update))
2821 ;; compare two regions in two buffers for containing the same text
2822 (defun emerge-compare-buffers (buffer-x x-begin x-end buffer-y y-begin y-end)
2823 ;; first check that the two regions are the same length
2824 (if (not (and (= (- x-end x-begin) (- y-end y-begin))))
2826 (catch 'exit
2827 (while (< x-begin x-end)
2828 ;; bite off and compare no more than 1000 characters at a time
2829 (let* ((compare-length (min (- x-end x-begin) 1000))
2830 (x-string (with-current-buffer
2831 buffer-x
2832 (buffer-substring x-begin
2833 (+ x-begin compare-length))))
2834 (y-string (with-current-buffer
2835 buffer-y
2836 (buffer-substring y-begin
2837 (+ y-begin compare-length)))))
2838 (if (not (string-equal x-string y-string))
2839 (throw 'exit nil)
2840 (setq x-begin (+ x-begin compare-length))
2841 (setq y-begin (+ y-begin compare-length)))))
2842 t)))
2844 ;; Construct a unique buffer name.
2845 ;; The first one tried is prefixsuffix, then prefix<2>suffix,
2846 ;; prefix<3>suffix, etc.
2847 (defun emerge-unique-buffer-name (prefix suffix)
2848 (if (null (get-buffer (concat prefix suffix)))
2849 (concat prefix suffix)
2850 (let ((n 2))
2851 (while (get-buffer (format "%s<%d>%s" prefix n suffix))
2852 (setq n (1+ n)))
2853 (format "%s<%d>%s" prefix n suffix))))
2855 ;; Verify that we have a difference selected.
2856 (defun emerge-validate-difference ()
2857 (if (not (and (>= emerge-current-difference 0)
2858 (< emerge-current-difference emerge-number-of-differences)))
2859 (error "No difference selected")))
2861 ;;; Functions for saving and restoring a batch of variables
2863 ;; These functions save (get the values of) and restore (set the values of)
2864 ;; a list of variables. The argument is a list of symbols (the names of
2865 ;; the variables). A list element can also be a list of two functions,
2866 ;; the first of which (when called with no arguments) gets the value, and
2867 ;; the second (when called with a value as an argument) sets the value.
2868 ;; A "function" is anything that funcall can handle as an argument.
2870 (defun emerge-save-variables (vars)
2871 (mapcar (lambda (v) (if (symbolp v)
2872 (symbol-value v)
2873 (funcall (car v))))
2874 vars))
2876 (defun emerge-restore-variables (vars values)
2877 (while vars
2878 (let ((var (car vars))
2879 (value (car values)))
2880 (if (symbolp var)
2881 (set var value)
2882 (funcall (car (cdr var)) value)))
2883 (setq vars (cdr vars))
2884 (setq values (cdr values))))
2886 ;; Make a temporary file that only we have access to.
2887 ;; PREFIX is appended to emerge-temp-file-prefix to make the filename prefix.
2888 (defun emerge-make-temp-file (prefix)
2889 (let (f (old-modes (default-file-modes)))
2890 (unwind-protect
2891 (progn
2892 (set-default-file-modes emerge-temp-file-mode)
2893 (setq f (make-temp-file (concat emerge-temp-file-prefix prefix))))
2894 (set-default-file-modes old-modes))
2897 ;;; Functions that query the user before he can write out the current buffer.
2899 (defun emerge-query-write-file ()
2900 "Ask the user whether to write out an incomplete merge.
2901 If answer is yes, call `write-file' to do so. See `emerge-query-and-call'
2902 for details of the querying process."
2903 (interactive)
2904 (emerge-query-and-call 'write-file))
2906 (defun emerge-query-save-buffer ()
2907 "Ask the user whether to save an incomplete merge.
2908 If answer is yes, call `save-buffer' to do so. See `emerge-query-and-call'
2909 for details of the querying process."
2910 (interactive)
2911 (emerge-query-and-call 'save-buffer))
2913 (defun emerge-query-and-call (command)
2914 "Ask the user whether to save or write out the incomplete merge.
2915 If answer is yes, call COMMAND interactively. During the call, the flags
2916 around the current difference are removed."
2917 (if (yes-or-no-p "Do you really write to write out this unfinished merge? ")
2918 ;; He really wants to do it -- unselect the difference for the duration
2919 (progn
2920 (if (and (>= emerge-current-difference 0)
2921 (< emerge-current-difference emerge-number-of-differences))
2922 (emerge-unselect-difference emerge-current-difference))
2923 ;; call-interactively takes the value of current-prefix-arg as the
2924 ;; prefix argument value to be passed to the command. Thus, we have
2925 ;; to do nothing special to make sure the prefix argument is
2926 ;; transmitted to the command.
2927 (call-interactively command)
2928 (if (and (>= emerge-current-difference 0)
2929 (< emerge-current-difference emerge-number-of-differences))
2930 (progn
2931 (emerge-select-difference emerge-current-difference)
2932 (emerge-recenter))))
2933 ;; He's being smart and not doing it
2934 (message "Not written")))
2936 ;; Make sure the current buffer (for a file) has the same contents as the
2937 ;; file on disk, and attempt to remedy the situation if not.
2938 ;; Signal an error if we can't make them the same, or the user doesn't want
2939 ;; to do what is necessary to make them the same.
2940 (defun emerge-verify-file-buffer ()
2941 ;; First check if the file has been modified since the buffer visited it.
2942 (if (verify-visited-file-modtime (current-buffer))
2943 (if (buffer-modified-p)
2944 ;; If buffer is not obsolete and is modified, offer to save
2945 (if (yes-or-no-p (format "Save file %s? " buffer-file-name))
2946 (save-buffer)
2947 (error "Buffer out of sync for file %s" buffer-file-name))
2948 ;; If buffer is not obsolete and is not modified, do nothing
2949 nil)
2950 (if (buffer-modified-p)
2951 ;; If buffer is obsolete and is modified, give error
2952 (error "Buffer out of sync for file %s" buffer-file-name)
2953 ;; If buffer is obsolete and is not modified, offer to revert
2954 (if (yes-or-no-p (format "Revert file %s? " buffer-file-name))
2955 (revert-buffer t t)
2956 (error "Buffer out of sync for file %s" buffer-file-name)))))
2958 ;; Utilities that might have value outside of Emerge.
2960 ;; Set up the mode in the current buffer to duplicate the mode in another
2961 ;; buffer.
2962 (defun emerge-copy-modes (buffer)
2963 ;; Set the major mode
2964 (funcall (with-current-buffer buffer major-mode)))
2966 ;; Define a key, even if a prefix of it is defined
2967 (defun emerge-force-define-key (keymap key definition)
2968 "Like `define-key', but forcibly creates prefix characters as needed.
2969 If some prefix of KEY has a non-prefix definition, it is redefined."
2970 ;; Find out if a prefix of key is defined
2971 (let ((v (lookup-key keymap key)))
2972 ;; If so, undefine it
2973 (if (integerp v)
2974 (define-key keymap (substring key 0 v) nil)))
2975 ;; Now define the key
2976 (define-key keymap key definition))
2978 ;;;;; Improvements to describe-mode, so that it describes minor modes as well
2979 ;;;;; as the major mode
2980 ;;(defun describe-mode (&optional minor)
2981 ;; "Display documentation of current major mode.
2982 ;;If optional arg MINOR is non-nil (or prefix argument is given if interactive),
2983 ;;display documentation of active minor modes as well.
2984 ;;For this to work correctly for a minor mode, the mode's indicator variable
2985 ;;\(listed in `minor-mode-alist') must also be a function whose documentation
2986 ;;describes the minor mode."
2987 ;; (interactive)
2988 ;; (with-output-to-temp-buffer "*Help*"
2989 ;; (princ mode-name)
2990 ;; (princ " Mode:\n")
2991 ;; (princ (documentation major-mode))
2992 ;; (let ((minor-modes minor-mode-alist)
2993 ;; (locals (buffer-local-variables)))
2994 ;; (while minor-modes
2995 ;; (let* ((minor-mode (car (car minor-modes)))
2996 ;; (indicator (car (cdr (car minor-modes))))
2997 ;; (local-binding (assq minor-mode locals)))
2998 ;; ;; Document a minor mode if it is listed in minor-mode-alist,
2999 ;; ;; bound locally in this buffer, non-nil, and has a function
3000 ;; ;; definition.
3001 ;; (if (and local-binding
3002 ;; (cdr local-binding)
3003 ;; (fboundp minor-mode))
3004 ;; (progn
3005 ;; (princ (format "\n\n\n%s minor mode (indicator%s):\n"
3006 ;; minor-mode indicator))
3007 ;; (princ (documentation minor-mode)))))
3008 ;; (setq minor-modes (cdr minor-modes))))
3009 ;; (with-current-buffer standard-output
3010 ;; (help-mode))
3011 ;; (help-print-return-message)))
3013 ;; This goes with the redefinition of describe-mode.
3014 ;;;; Adjust things so that keyboard macro definitions are documented correctly.
3015 ;;(fset 'defining-kbd-macro (symbol-function 'start-kbd-macro))
3017 ;; substitute-key-definition should work now.
3018 ;;;; Function to shadow a definition in a keymap with definitions in another.
3019 ;;(defun emerge-shadow-key-definition (olddef newdef keymap shadowmap)
3020 ;; "Shadow OLDDEF with NEWDEF for any keys in KEYMAP with entries in SHADOWMAP.
3021 ;;In other words, SHADOWMAP will now shadow all definitions of OLDDEF in KEYMAP
3022 ;;with NEWDEF. Does not affect keys that are already defined in SHADOWMAP,
3023 ;;including those whose definition is OLDDEF."
3024 ;; ;; loop through all keymaps accessible from keymap
3025 ;; (let ((maps (accessible-keymaps keymap)))
3026 ;; (while maps
3027 ;; (let ((prefix (car (car maps)))
3028 ;; (map (cdr (car maps))))
3029 ;; ;; examine a keymap
3030 ;; (if (arrayp map)
3031 ;; ;; array keymap
3032 ;; (let ((len (length map))
3033 ;; (i 0))
3034 ;; (while (< i len)
3035 ;; (if (eq (aref map i) olddef)
3036 ;; ;; set the shadowing definition
3037 ;; (let ((key (concat prefix (char-to-string i))))
3038 ;; (emerge-define-key-if-possible shadowmap key newdef)))
3039 ;; (setq i (1+ i))))
3040 ;; ;; sparse keymap
3041 ;; (while map
3042 ;; (if (eq (cdr-safe (car-safe map)) olddef)
3043 ;; ;; set the shadowing definition
3044 ;; (let ((key
3045 ;; (concat prefix (char-to-string (car (car map))))))
3046 ;; (emerge-define-key-if-possible shadowmap key newdef)))
3047 ;; (setq map (cdr map)))))
3048 ;; (setq maps (cdr maps)))))
3050 ;; Define a key if it (or a prefix) is not already defined in the map.
3051 (defun emerge-define-key-if-possible (keymap key definition)
3052 ;; look up the present definition of the key
3053 (let ((present (lookup-key keymap key)))
3054 (if (integerp present)
3055 ;; if it is "too long", look up the valid prefix
3056 (if (not (lookup-key keymap (substring key 0 present)))
3057 ;; if the prefix isn't defined, define it
3058 (define-key keymap key definition))
3059 ;; if there is no present definition, define it
3060 (if (not present)
3061 (define-key keymap key definition)))))
3063 ;; Ordinary substitute-key-definition should do this now.
3064 ;;(defun emerge-recursively-substitute-key-definition (olddef newdef keymap)
3065 ;; "Like `substitute-key-definition', but act recursively on subkeymaps.
3066 ;;Make sure that subordinate keymaps aren't shared with other keymaps!
3067 ;;\(`copy-keymap' will suffice.)"
3068 ;; ;; Loop through all keymaps accessible from keymap
3069 ;; (let ((maps (accessible-keymaps keymap)))
3070 ;; (while maps
3071 ;; ;; Substitute in this keymap
3072 ;; (substitute-key-definition olddef newdef (cdr (car maps)))
3073 ;; (setq maps (cdr maps)))))
3075 ;; Show the name of the file in the buffer.
3076 (defun emerge-show-file-name ()
3077 "Displays the name of the file loaded into the current buffer.
3078 If the name won't fit on one line, the minibuffer is expanded to hold it,
3079 and the command waits for a keystroke from the user. If the keystroke is
3080 SPC, it is ignored; if it is anything else, it is processed as a command."
3081 (interactive)
3082 (let ((name (buffer-file-name)))
3083 (or name
3084 (setq name "Buffer has no file name."))
3085 (save-window-excursion
3086 (select-window (minibuffer-window))
3087 (unwind-protect
3088 (progn
3089 (erase-buffer)
3090 (insert name)
3091 (while (and (not (pos-visible-in-window-p))
3092 (not (window-full-height-p)))
3093 (enlarge-window 1))
3094 (let* ((echo-keystrokes 0)
3095 (c (read-event)))
3096 (if (not (eq c 32))
3097 (setq unread-command-events (list c)))))
3098 (erase-buffer)))))
3100 ;; Improved auto-save file names.
3101 ;; This function fixes many problems with the standard auto-save file names:
3102 ;; Auto-save files for non-file buffers get put in the default directory
3103 ;; for the buffer, whether that makes sense or not.
3104 ;; Auto-save files for file buffers get put in the directory of the file,
3105 ;; regardless of whether we can write into it or not.
3106 ;; Auto-save files for non-file buffers don't use the process id, so if a
3107 ;; user runs more than on Emacs, they can make auto-save files that overwrite
3108 ;; each other.
3109 ;; To use this function, do:
3110 ;; (fset 'make-auto-save-file-name
3111 ;; (symbol-function 'emerge-make-auto-save-file-name))
3112 (defun emerge-make-auto-save-file-name ()
3113 "Return file name to use for auto-saves of current buffer.
3114 Does not consider `auto-save-visited-file-name';
3115 that is checked before calling this function.
3116 You can redefine this for customization.
3117 See also `auto-save-file-name-p'."
3118 (if buffer-file-name
3119 ;; if buffer has a file, try the format <file directory>/#<file name>#
3120 (let ((f (concat (file-name-directory buffer-file-name)
3122 (file-name-nondirectory buffer-file-name)
3123 "#")))
3124 (if (file-writable-p f)
3125 ;; the file is writable, so use it
3127 ;; the file isn't writable, so use the format
3128 ;; ~/#&<file name>&<hash of directory>#
3129 (concat (getenv "HOME")
3130 "/#&"
3131 (file-name-nondirectory buffer-file-name)
3133 (emerge-hash-string-into-string
3134 (file-name-directory buffer-file-name))
3135 "#")))
3136 ;; if buffer has no file, use the format ~/#%<buffer name>%<process id>#
3137 (expand-file-name (concat (getenv "HOME")
3138 "/#%"
3139 ;; quote / into \! and \ into \\
3140 (emerge-unslashify-name (buffer-name))
3142 (make-temp-name "")
3143 "#"))))
3145 ;; Hash a string into five characters more-or-less suitable for use in a file
3146 ;; name. (Allowed characters are ! through ~, except /.)
3147 (defun emerge-hash-string-into-string (s)
3148 (let ((bins (vector 0 0 0 0 0))
3149 (i 0))
3150 (while (< i (length s))
3151 (aset bins (% i 5) (% (+ (* (aref bins (% i 5)) 35)
3152 (aref s i))
3153 65536))
3154 (setq i (1+ i)))
3155 (mapconcat (lambda (b)
3156 (setq b (+ (% b 93) ?!))
3157 (if (>= b ?/)
3158 (setq b (1+ b)))
3159 (char-to-string b))
3160 bins "")))
3162 ;; Quote any /s in a string by replacing them with \!.
3163 ;; Also, replace any \s by \\, to make it one-to-one.
3164 (defun emerge-unslashify-name (s)
3165 (let ((limit 0))
3166 (while (string-match "[/\\]" s limit)
3167 (setq s (concat (substring s 0 (match-beginning 0))
3168 (if (string= (substring s (match-beginning 0)
3169 (match-end 0))
3170 "/")
3171 "\\!"
3172 "\\\\")
3173 (substring s (match-end 0))))
3174 (setq limit (1+ (match-end 0)))))
3177 ;; Metacharacters that have to be protected from the shell when executing
3178 ;; a diff/diff3 command.
3179 (defcustom emerge-metachars
3180 (if (memq system-type '(ms-dos windows-nt))
3181 "[ \t\"<>|?*^&=]"
3182 "[ \t\n!\"#$&'()*;<=>?[\\^`{|~]")
3183 "Characters that must be quoted when used in a shell command line.
3184 More precisely, a [...] regexp to match any one such character."
3185 :type 'regexp
3186 :group 'emerge)
3188 ;; Quote metacharacters (using \) when executing a diff/diff3 command.
3189 (defun emerge-protect-metachars (s)
3190 (if (memq system-type '(ms-dos windows-nt))
3191 (shell-quote-argument s)
3192 (let ((limit 0))
3193 (while (string-match emerge-metachars s limit)
3194 (setq s (concat (substring s 0 (match-beginning 0))
3195 "\\"
3196 (substring s (match-beginning 0))))
3197 (setq limit (1+ (match-end 0)))))
3200 (provide 'emerge)
3202 ;;; emerge.el ends here