CC Mode now uses the new :after-hook feature of define-derived-mode
[emacs.git] / lisp / vc / emerge.el
blobde25cbafb0d5a479aeab72175f11a6d083d2d4bf
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 `permanent-local' 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 'permanent-local 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 ;;; Emerge configuration variables
81 (defgroup emerge nil
82 "Merge diffs under Emacs control."
83 :group 'tools)
85 ;; Commands that produce difference files
86 ;; All that can be configured is the name of the programs to execute
87 ;; (emerge-diff-program and emerge-diff3-program) and the options
88 ;; to be provided (emerge-diff-options). The order in which the file names
89 ;; are given is fixed.
90 ;; The file names are always expanded (see expand-file-name) before being
91 ;; passed to diff, thus they need not be invoked under a shell that
92 ;; understands `~'.
93 ;; The code which processes the diff/diff3 output depends on all the
94 ;; finicky details of their output, including the somewhat strange
95 ;; way they number lines of a file.
96 (defcustom emerge-diff-program "diff"
97 "Name of the program which compares two files."
98 :type 'string
99 :group 'emerge)
100 (defcustom emerge-diff3-program "diff3"
101 "Name of the program which compares three files.
102 Its arguments are the ancestor file and the two variant files."
103 :type 'string
104 :group 'emerge)
105 (defcustom emerge-diff-options ""
106 "Options to pass to `emerge-diff-program' and `emerge-diff3-program'."
107 :type 'string
108 :group 'emerge)
109 (defcustom emerge-match-diff-line
110 (let ((x "\\([0-9]+\\)\\(\\|,\\([0-9]+\\)\\)"))
111 (concat "^" x "\\([acd]\\)" x "$"))
112 "Pattern to match lines produced by diff that describe differences.
113 This is as opposed to lines from the source files."
114 :type 'regexp
115 :group 'emerge)
116 (defcustom emerge-diff-ok-lines-regexp
117 "^\\([0-9,]+[acd][0-9,]+$\\|[<>] \\|---\\)"
118 "Regexp that matches normal output lines from `emerge-diff-program'.
119 Lines that do not match are assumed to be error messages."
120 :type 'regexp
121 :group 'emerge)
122 (defcustom emerge-diff3-ok-lines-regexp
123 "^\\([1-3]:\\|====\\| \\)"
124 "Regexp that matches normal output lines from `emerge-diff3-program'.
125 Lines that do not match are assumed to be error messages."
126 :type 'regexp
127 :group 'emerge)
129 (defcustom emerge-rcs-ci-program "ci"
130 "Name of the program that checks in RCS revisions."
131 :type 'string
132 :group 'emerge)
133 (defcustom emerge-rcs-co-program "co"
134 "Name of the program that checks out RCS revisions."
135 :type 'string
136 :group 'emerge)
138 (defcustom emerge-process-local-variables nil
139 "Non-nil if Emerge should process local-variables lists in merge buffers.
140 \(You can explicitly request processing the local-variables
141 by executing `(hack-local-variables)'.)"
142 :type 'boolean
143 :group 'emerge)
144 (defcustom emerge-execute-line-deletions nil
145 "If non-nil: `emerge-execute-line' makes no output if an input was deleted.
146 It concludes that an input version has been deleted when an ancestor entry
147 is present, only one A or B entry is present, and an output entry is present.
148 If nil: In such circumstances, the A or B file that is present will be
149 copied to the designated output file."
150 :type 'boolean
151 :group 'emerge)
153 (defcustom emerge-before-flag "vvvvvvvvvvvvvvvvvvvv\n"
154 "Flag placed above the highlighted block of code. Must end with newline.
155 Must be set before Emerge is loaded, or emerge-new-flags must be run
156 after setting."
157 :type 'string
158 :group 'emerge)
159 (defcustom emerge-after-flag "^^^^^^^^^^^^^^^^^^^^\n"
160 "Flag placed below the highlighted block of code. Must end with newline.
161 Must be set before Emerge is loaded, or emerge-new-flags must be run
162 after setting."
163 :type 'string
164 :group 'emerge)
166 ;; Hook variables
168 (defcustom emerge-startup-hook nil
169 "Hook to run in the merge buffer after the merge has been set up."
170 :type 'hook
171 :group 'emerge)
172 (defcustom emerge-select-hook nil
173 "Hook to run after a difference has been selected.
174 The variable `n' holds the (internal) number of the difference."
175 :type 'hook
176 :group 'emerge)
177 (defcustom emerge-unselect-hook nil
178 "Hook to run after a difference has been unselected.
179 The variable `n' holds the (internal) number of the difference."
180 :type 'hook
181 :group 'emerge)
183 ;; Variables to control the default directories of the arguments to
184 ;; Emerge commands.
186 (defcustom emerge-default-last-directories nil
187 "If nil, default dir for filenames in emerge is `default-directory'.
188 If non-nil, filenames complete in the directory of the last argument of the
189 same type to an `emerge-files...' command."
190 :type 'boolean
191 :group 'emerge)
193 (defvar emerge-last-dir-A nil
194 "Last directory for the first file of an `emerge-files...' command.")
195 (defvar emerge-last-dir-B nil
196 "Last directory for the second file of an `emerge-files...' command.")
197 (defvar emerge-last-dir-ancestor nil
198 "Last directory for the ancestor file of an `emerge-files...' command.")
199 (defvar emerge-last-dir-output nil
200 "Last directory for the output file of an `emerge-files...' command.")
201 (defvar emerge-last-revision-A nil
202 "Last RCS revision used for first file of an `emerge-revisions...' command.")
203 (defvar emerge-last-revision-B nil
204 "Last RCS revision used for second file of an `emerge-revisions...' command.")
205 (defvar emerge-last-revision-ancestor nil
206 "Last RCS revision used for ancestor file of an `emerge-revisions...' command.")
208 (defvar emerge-before-flag-length)
209 (defvar emerge-before-flag-lines)
210 (defvar emerge-before-flag-match)
211 (defvar emerge-after-flag-length)
212 (defvar emerge-after-flag-lines)
213 (defvar emerge-after-flag-match)
214 (defvar emerge-diff-buffer)
215 (defvar emerge-diff-error-buffer)
216 (defvar emerge-prefix-argument)
217 (defvar emerge-file-out)
218 (defvar emerge-exit-func)
219 (defvar emerge-globalized-difference-list)
220 (defvar emerge-globalized-number-of-differences)
222 ;; The flags used to mark differences in the buffers.
224 ;; These function definitions need to be up here, because they are used
225 ;; during loading.
226 (defun emerge-new-flags ()
227 "Function to be called after `emerge-{before,after}-flag'.
228 This is called after these functions are changed to compute values that
229 depend on the flags."
230 (setq emerge-before-flag-length (length emerge-before-flag))
231 (setq emerge-before-flag-lines
232 (emerge-count-matches-string emerge-before-flag "\n"))
233 (setq emerge-before-flag-match (regexp-quote emerge-before-flag))
234 (setq emerge-after-flag-length (length emerge-after-flag))
235 (setq emerge-after-flag-lines
236 (emerge-count-matches-string emerge-after-flag "\n"))
237 (setq emerge-after-flag-match (regexp-quote emerge-after-flag)))
239 (defun emerge-count-matches-string (string regexp)
240 "Return the number of matches in STRING for REGEXP."
241 (let ((i 0)
242 (count 0))
243 (while (string-match regexp string i)
244 (setq count (1+ count))
245 (setq i (match-end 0)))
246 count))
248 ;; Calculate dependent variables
249 (emerge-new-flags)
251 (defcustom emerge-min-visible-lines 3
252 "Number of lines that we want to show above and below the flags when we are
253 displaying a difference."
254 :type 'integer
255 :group 'emerge)
257 (defcustom emerge-temp-file-prefix
258 (expand-file-name "emerge" temporary-file-directory)
259 "Prefix to put on Emerge temporary file names.
260 Do not start with `~/' or `~USERNAME/'."
261 :type 'string
262 :group 'emerge)
264 (make-obsolete-variable 'emerge-temp-file-prefix
265 "customize `temporary-file-directory' instead."
266 "24.4" 'set)
268 (defcustom emerge-temp-file-mode 384 ; u=rw only
269 "Mode for Emerge temporary files."
270 :type 'integer
271 :group 'emerge)
273 (make-obsolete-variable 'emerge-temp-file-mode
274 "it has no effect, temporary files are always private."
275 "24.4" 'set)
277 (defcustom emerge-combine-versions-template
278 "#ifdef NEW\n%b#else /* not NEW */\n%a#endif /* not NEW */\n"
279 "Template for `emerge-combine-versions' to combine the two versions.
280 The template is inserted as a string, with the following interpolations:
281 %a the A version of the difference
282 %b the B version of the difference
283 %% the character `%'
284 Don't forget to end the template with a newline.
285 Note that this variable can be made local to a particular merge buffer by
286 giving a prefix argument to `emerge-set-combine-versions-template'."
287 :type 'string
288 :group 'emerge)
290 ;; Build keymaps
292 (defvar emerge-basic-keymap nil
293 "Keymap of Emerge commands.
294 Directly available in `fast' mode;
295 must be prefixed by \\<emerge-fast-keymap>\\[emerge-basic-keymap] in `edit' mode.")
297 (defvar emerge-fast-keymap nil
298 "Local keymap used in Emerge `fast' mode.
299 Makes Emerge commands directly available.")
301 (defvar emerge-options-menu
302 (make-sparse-keymap "Options"))
304 (defvar emerge-merge-menu
305 (make-sparse-keymap "Merge"))
307 (defvar emerge-move-menu
308 (make-sparse-keymap "Move"))
310 (defcustom emerge-command-prefix "\C-c\C-c"
311 "Command prefix for Emerge commands in `edit' mode.
312 Must be set before Emerge is loaded."
313 :type 'string
314 :group 'emerge)
316 ;; This function sets up the fixed keymaps. It is executed when the first
317 ;; Emerge is done to allow the user maximum time to set up the global keymap.
318 (defun emerge-setup-fixed-keymaps ()
319 ;; Set up the basic keymap
320 (setq emerge-basic-keymap (make-keymap))
321 (suppress-keymap emerge-basic-keymap) ; this sets 0..9 to digit-argument and
322 ; - to negative-argument
323 (define-key emerge-basic-keymap "p" 'emerge-previous-difference)
324 (define-key emerge-basic-keymap "n" 'emerge-next-difference)
325 (define-key emerge-basic-keymap "a" 'emerge-select-A)
326 (define-key emerge-basic-keymap "b" 'emerge-select-B)
327 (define-key emerge-basic-keymap "j" 'emerge-jump-to-difference)
328 (define-key emerge-basic-keymap "." 'emerge-find-difference)
329 (define-key emerge-basic-keymap "q" 'emerge-quit)
330 (define-key emerge-basic-keymap "\C-]" 'emerge-abort)
331 (define-key emerge-basic-keymap "f" 'emerge-fast-mode)
332 (define-key emerge-basic-keymap "e" 'emerge-edit-mode)
333 (define-key emerge-basic-keymap "s" nil)
334 (define-key emerge-basic-keymap "sa" 'emerge-auto-advance)
335 (define-key emerge-basic-keymap "ss" 'emerge-skip-prefers)
336 (define-key emerge-basic-keymap "l" 'emerge-recenter)
337 (define-key emerge-basic-keymap "d" nil)
338 (define-key emerge-basic-keymap "da" 'emerge-default-A)
339 (define-key emerge-basic-keymap "db" 'emerge-default-B)
340 (define-key emerge-basic-keymap "c" nil)
341 (define-key emerge-basic-keymap "ca" 'emerge-copy-as-kill-A)
342 (define-key emerge-basic-keymap "cb" 'emerge-copy-as-kill-B)
343 (define-key emerge-basic-keymap "i" nil)
344 (define-key emerge-basic-keymap "ia" 'emerge-insert-A)
345 (define-key emerge-basic-keymap "ib" 'emerge-insert-B)
346 (define-key emerge-basic-keymap "m" 'emerge-mark-difference)
347 (define-key emerge-basic-keymap "v" 'emerge-scroll-up)
348 (define-key emerge-basic-keymap "^" 'emerge-scroll-down)
349 (define-key emerge-basic-keymap "<" 'emerge-scroll-left)
350 (define-key emerge-basic-keymap ">" 'emerge-scroll-right)
351 (define-key emerge-basic-keymap "|" 'emerge-scroll-reset)
352 (define-key emerge-basic-keymap "x" nil)
353 (define-key emerge-basic-keymap "x1" 'emerge-one-line-window)
354 (define-key emerge-basic-keymap "xc" 'emerge-combine-versions)
355 (define-key emerge-basic-keymap "xC" 'emerge-combine-versions-register)
356 (define-key emerge-basic-keymap "xf" 'emerge-file-names)
357 (define-key emerge-basic-keymap "xj" 'emerge-join-differences)
358 (define-key emerge-basic-keymap "xl" 'emerge-line-numbers)
359 (define-key emerge-basic-keymap "xm" 'emerge-set-merge-mode)
360 (define-key emerge-basic-keymap "xs" 'emerge-split-difference)
361 (define-key emerge-basic-keymap "xt" 'emerge-trim-difference)
362 (define-key emerge-basic-keymap "xx" 'emerge-set-combine-versions-template)
363 ;; Allow emerge-basic-keymap to be referenced indirectly
364 (fset 'emerge-basic-keymap emerge-basic-keymap)
365 ;; Set up the fast mode keymap
366 (setq emerge-fast-keymap (copy-keymap emerge-basic-keymap))
367 ;; Allow prefixed commands to work in fast mode
368 (define-key emerge-fast-keymap emerge-command-prefix 'emerge-basic-keymap)
369 ;; Allow emerge-fast-keymap to be referenced indirectly
370 (fset 'emerge-fast-keymap emerge-fast-keymap)
371 ;; Suppress write-file and save-buffer
372 (define-key emerge-fast-keymap [remap write-file] 'emerge-query-write-file)
373 (define-key emerge-fast-keymap [remap save-buffer] 'emerge-query-save-buffer)
375 (define-key emerge-basic-keymap [menu-bar] (make-sparse-keymap))
377 (define-key emerge-fast-keymap [menu-bar emerge-options]
378 (cons "Merge-Options" emerge-options-menu))
379 (define-key emerge-fast-keymap [menu-bar merge]
380 (cons "Merge" emerge-merge-menu))
381 (define-key emerge-fast-keymap [menu-bar move]
382 (cons "Move" emerge-move-menu))
384 (define-key emerge-move-menu [emerge-scroll-reset]
385 '("Scroll Reset" . emerge-scroll-reset))
386 (define-key emerge-move-menu [emerge-scroll-right]
387 '("Scroll Right" . emerge-scroll-right))
388 (define-key emerge-move-menu [emerge-scroll-left]
389 '("Scroll Left" . emerge-scroll-left))
390 (define-key emerge-move-menu [emerge-scroll-down]
391 '("Scroll Down" . emerge-scroll-down))
392 (define-key emerge-move-menu [emerge-scroll-up]
393 '("Scroll Up" . emerge-scroll-up))
394 (define-key emerge-move-menu [emerge-recenter]
395 '("Recenter" . emerge-recenter))
396 (define-key emerge-move-menu [emerge-mark-difference]
397 '("Mark Difference" . emerge-mark-difference))
398 (define-key emerge-move-menu [emerge-jump-to-difference]
399 '("Jump To Difference" . emerge-jump-to-difference))
400 (define-key emerge-move-menu [emerge-find-difference]
401 '("Find Difference" . emerge-find-difference))
402 (define-key emerge-move-menu [emerge-previous-difference]
403 '("Previous Difference" . emerge-previous-difference))
404 (define-key emerge-move-menu [emerge-next-difference]
405 '("Next Difference" . emerge-next-difference))
408 (define-key emerge-options-menu [emerge-one-line-window]
409 '("One Line Window" . emerge-one-line-window))
410 (define-key emerge-options-menu [emerge-set-merge-mode]
411 '("Set Merge Mode..." . emerge-set-merge-mode))
412 (define-key emerge-options-menu [emerge-set-combine-template]
413 '("Set Combine Template..." . emerge-set-combine-template))
414 (define-key emerge-options-menu [emerge-default-B]
415 '("Default B" . emerge-default-B))
416 (define-key emerge-options-menu [emerge-default-A]
417 '("Default A" . emerge-default-A))
418 (define-key emerge-options-menu [emerge-skip-prefers]
419 '(menu-item "Skip Prefers" emerge-skip-prefers
420 :button (:toggle . emerge-skip-prefers)))
421 (define-key emerge-options-menu [emerge-auto-advance]
422 '(menu-item "Auto Advance" emerge-auto-advance
423 :button (:toggle . emerge-auto-advance)))
424 (define-key emerge-options-menu [emerge-edit-mode]
425 '(menu-item "Edit Mode" emerge-edit-mode :enable (not emerge-edit-mode)))
426 (define-key emerge-options-menu [emerge-fast-mode]
427 '(menu-item "Fast Mode" emerge-fast-mode :enable (not emerge-fast-mode)))
429 (define-key emerge-merge-menu [emerge-abort] '("Abort" . emerge-abort))
430 (define-key emerge-merge-menu [emerge-quit] '("Quit" . emerge-quit))
431 (define-key emerge-merge-menu [emerge-split-difference]
432 '("Split Difference" . emerge-split-difference))
433 (define-key emerge-merge-menu [emerge-join-differences]
434 '("Join Differences" . emerge-join-differences))
435 (define-key emerge-merge-menu [emerge-trim-difference]
436 '("Trim Difference" . emerge-trim-difference))
437 (define-key emerge-merge-menu [emerge-combine-versions]
438 '("Combine Versions" . emerge-combine-versions))
439 (define-key emerge-merge-menu [emerge-copy-as-kill-B]
440 '("Copy B as Kill" . emerge-copy-as-kill-B))
441 (define-key emerge-merge-menu [emerge-copy-as-kill-A]
442 '("Copy A as Kill" . emerge-copy-as-kill-A))
443 (define-key emerge-merge-menu [emerge-insert-B]
444 '("Insert B" . emerge-insert-B))
445 (define-key emerge-merge-menu [emerge-insert-A]
446 '("Insert A" . emerge-insert-A))
447 (define-key emerge-merge-menu [emerge-select-B]
448 '("Select B" . emerge-select-B))
449 (define-key emerge-merge-menu [emerge-select-A]
450 '("Select A" . emerge-select-A)))
453 ;; Variables which control each merge. They are local to the merge buffer.
455 ;; Mode variables
456 (emerge-defvar-local emerge-mode nil
457 "Indicator for emerge-mode.")
458 (emerge-defvar-local emerge-fast-mode nil
459 "Indicator for emerge-mode fast submode.")
460 (emerge-defvar-local emerge-edit-mode nil
461 "Indicator for emerge-mode edit submode.")
462 (emerge-defvar-local emerge-A-buffer nil
463 "The buffer in which the A variant is stored.")
464 (emerge-defvar-local emerge-B-buffer nil
465 "The buffer in which the B variant is stored.")
466 (emerge-defvar-local emerge-merge-buffer nil
467 "The buffer in which the merged file is manipulated.")
468 (emerge-defvar-local emerge-ancestor-buffer nil
469 "The buffer in which the ancestor variant is stored,
470 or nil if there is none.")
472 (defconst emerge-saved-variables
473 '((buffer-modified-p set-buffer-modified-p)
474 buffer-read-only
475 buffer-auto-save-file-name)
476 "Variables and properties of a buffer which are saved, modified and restored
477 during a merge.")
478 (defconst emerge-merging-values '(nil t nil)
479 "Values to be assigned to emerge-saved-variables during a merge.")
481 (emerge-defvar-local emerge-A-buffer-values nil
482 "Remembers emerge-saved-variables for emerge-A-buffer.")
483 (emerge-defvar-local emerge-B-buffer-values nil
484 "Remembers emerge-saved-variables for emerge-B-buffer.")
486 (emerge-defvar-local emerge-difference-list nil
487 "Vector of differences between the variants, and markers in the buffers to
488 show where they are. Each difference is represented by a vector of seven
489 elements. The first two are markers to the beginning and end of the difference
490 section in the A buffer, the second two are markers for the B buffer, the third
491 two are markers for the merge buffer, and the last element is the \"state\" of
492 that difference in the merge buffer.
493 A section of a buffer is described by two markers, one to the beginning of
494 the first line of the section, and one to the beginning of the first line
495 after the section. (If the section is empty, both markers point to the same
496 point.) If the section is part of the selected difference, then the markers
497 are moved into the flags, so the user can edit the section without disturbing
498 the markers.
499 The \"states\" are:
500 A the merge buffer currently contains the A variant
501 B the merge buffer currently contains the B variant
502 default-A the merge buffer contains the A variant by default,
503 but this difference hasn't been selected yet, so
504 change-default commands can alter it
505 default-B the merge buffer contains the B variant by default,
506 but this difference hasn't been selected yet, so
507 change-default commands can alter it
508 prefer-A in a three-file merge, the A variant is the preferred
509 choice
510 prefer-B in a three-file merge, the B variant is the preferred
511 choice")
512 (emerge-defvar-local emerge-current-difference -1
513 "The difference that is currently selected.")
514 (emerge-defvar-local emerge-number-of-differences nil
515 "Number of differences found.")
516 (emerge-defvar-local emerge-edit-keymap nil
517 "The local keymap for the merge buffer, with the emerge commands defined in
518 it. Used to save the local keymap during fast mode, when the local keymap is
519 replaced by emerge-fast-keymap.")
520 (emerge-defvar-local emerge-old-keymap nil
521 "The original local keymap for the merge buffer.")
522 (emerge-defvar-local emerge-auto-advance nil
523 "If non-nil, emerge-select-A and emerge-select-B automatically advance to
524 the next difference.")
525 (emerge-defvar-local emerge-skip-prefers nil
526 "If non-nil, differences for which there is a preference are automatically
527 skipped.")
528 (emerge-defvar-local emerge-quit-hook nil
529 "Hooks to run in the merge buffer after the merge has been finished.
530 `emerge-prefix-argument' will hold the prefix argument of the `emerge-quit'
531 command.
532 This is *not* a user option, since Emerge uses it for its own processing.")
533 (emerge-defvar-local emerge-output-description nil
534 "Describes output destination of emerge, for `emerge-file-names'.")
536 ;;; Setup functions for two-file mode.
538 (defun emerge-files-internal (file-A file-B &optional startup-hooks quit-hooks
539 output-file)
540 (if (not (file-readable-p file-A))
541 (error "File `%s' does not exist or is not readable" file-A))
542 (if (not (file-readable-p file-B))
543 (error "File `%s' does not exist or is not readable" file-B))
544 (let ((buffer-A (find-file-noselect file-A))
545 (buffer-B (find-file-noselect file-B)))
546 ;; Record the directories of the files
547 (setq emerge-last-dir-A (file-name-directory file-A))
548 (setq emerge-last-dir-B (file-name-directory file-B))
549 (if output-file
550 (setq emerge-last-dir-output (file-name-directory output-file)))
551 ;; Make sure the entire files are seen, and they reflect what is on disk
552 (with-current-buffer
553 buffer-A
554 (widen)
555 (let ((temp (file-local-copy file-A)))
556 (if temp
557 (setq file-A temp
558 startup-hooks
559 (cons `(lambda () (delete-file ,file-A))
560 startup-hooks))
561 ;; Verify that the file matches the buffer
562 (emerge-verify-file-buffer))))
563 (with-current-buffer
564 buffer-B
565 (widen)
566 (let ((temp (file-local-copy file-B)))
567 (if temp
568 (setq file-B temp
569 startup-hooks
570 (cons `(lambda () (delete-file ,file-B))
571 startup-hooks))
572 ;; Verify that the file matches the buffer
573 (emerge-verify-file-buffer))))
574 (emerge-setup buffer-A file-A buffer-B file-B startup-hooks quit-hooks
575 output-file)))
577 ;; Start up Emerge on two files
578 (defun emerge-setup (buffer-A file-A buffer-B file-B startup-hooks quit-hooks
579 output-file)
580 (setq file-A (expand-file-name file-A))
581 (setq file-B (expand-file-name file-B))
582 (setq output-file (and output-file (expand-file-name output-file)))
583 (let* ((merge-buffer-name (emerge-unique-buffer-name "*merge" "*"))
584 ;; create the merge buffer from buffer A, so it inherits buffer A's
585 ;; default directory, etc.
586 (merge-buffer (with-current-buffer
587 buffer-A
588 (get-buffer-create merge-buffer-name))))
589 (with-current-buffer
590 merge-buffer
591 (emerge-copy-modes buffer-A)
592 (setq buffer-read-only nil)
593 (auto-save-mode 1)
594 (setq emerge-mode t)
595 (setq emerge-A-buffer buffer-A)
596 (setq emerge-B-buffer buffer-B)
597 (setq emerge-ancestor-buffer nil)
598 (setq emerge-merge-buffer merge-buffer)
599 (setq emerge-output-description
600 (if output-file
601 (concat "Output to file: " output-file)
602 (concat "Output to buffer: " (buffer-name merge-buffer))))
603 (save-excursion (insert-buffer-substring emerge-A-buffer))
604 (emerge-set-keys)
605 (setq emerge-difference-list (emerge-make-diff-list file-A file-B))
606 (setq emerge-number-of-differences (length emerge-difference-list))
607 (setq emerge-current-difference -1)
608 (setq emerge-quit-hook quit-hooks)
609 (emerge-remember-buffer-characteristics)
610 (emerge-handle-local-variables))
611 (emerge-setup-windows buffer-A buffer-B merge-buffer t)
612 (with-current-buffer merge-buffer
613 (run-hooks 'startup-hooks 'emerge-startup-hook)
614 (setq buffer-read-only t))))
616 ;; Generate the Emerge difference list between two files
617 (defun emerge-make-diff-list (file-A file-B)
618 (setq emerge-diff-buffer (get-buffer-create "*emerge-diff*"))
619 (with-current-buffer
620 emerge-diff-buffer
621 (erase-buffer)
622 (shell-command
623 (format "%s %s %s %s"
624 emerge-diff-program emerge-diff-options
625 (emerge-protect-metachars file-A)
626 (emerge-protect-metachars file-B))
628 (emerge-prepare-error-list emerge-diff-ok-lines-regexp)
629 (emerge-convert-diffs-to-markers
630 emerge-A-buffer emerge-B-buffer emerge-merge-buffer
631 (emerge-extract-diffs emerge-diff-buffer)))
633 (defun emerge-extract-diffs (diff-buffer)
634 (let (list)
635 (with-current-buffer
636 diff-buffer
637 (goto-char (point-min))
638 (while (re-search-forward emerge-match-diff-line nil t)
639 (let* ((a-begin (string-to-number (buffer-substring (match-beginning 1)
640 (match-end 1))))
641 (a-end (let ((b (match-beginning 3))
642 (e (match-end 3)))
643 (if b
644 (string-to-number (buffer-substring b e))
645 a-begin)))
646 (diff-type (buffer-substring (match-beginning 4) (match-end 4)))
647 (b-begin (string-to-number (buffer-substring (match-beginning 5)
648 (match-end 5))))
649 (b-end (let ((b (match-beginning 7))
650 (e (match-end 7)))
651 (if b
652 (string-to-number (buffer-substring b e))
653 b-begin))))
654 ;; fix the beginning and end numbers, because diff is somewhat
655 ;; strange about how it numbers lines
656 (if (string-equal diff-type "a")
657 (progn
658 (setq b-end (1+ b-end))
659 (setq a-begin (1+ a-begin))
660 (setq a-end a-begin))
661 (if (string-equal diff-type "d")
662 (progn
663 (setq a-end (1+ a-end))
664 (setq b-begin (1+ b-begin))
665 (setq b-end b-begin))
666 ;; (string-equal diff-type "c")
667 (progn
668 (setq a-end (1+ a-end))
669 (setq b-end (1+ b-end)))))
670 (setq list (cons (vector a-begin a-end
671 b-begin b-end
672 'default-A)
673 list)))))
674 (nreverse list)))
676 ;; Set up buffer of diff/diff3 error messages.
677 (defun emerge-prepare-error-list (ok-regexp)
678 (setq emerge-diff-error-buffer (get-buffer-create "*emerge-diff-errors*"))
679 (with-current-buffer
680 emerge-diff-error-buffer
681 (erase-buffer)
682 (save-excursion (insert-buffer-substring emerge-diff-buffer))
683 (delete-matching-lines ok-regexp)))
685 ;;; Top-level and setup functions for three-file mode.
687 (defun emerge-files-with-ancestor-internal (file-A file-B file-ancestor
688 &optional startup-hooks quit-hooks
689 output-file)
690 (if (not (file-readable-p file-A))
691 (error "File `%s' does not exist or is not readable" file-A))
692 (if (not (file-readable-p file-B))
693 (error "File `%s' does not exist or is not readable" file-B))
694 (if (not (file-readable-p file-ancestor))
695 (error "File `%s' does not exist or is not readable" file-ancestor))
696 (let ((buffer-A (find-file-noselect file-A))
697 (buffer-B (find-file-noselect file-B))
698 (buffer-ancestor (find-file-noselect file-ancestor)))
699 ;; Record the directories of the files
700 (setq emerge-last-dir-A (file-name-directory file-A))
701 (setq emerge-last-dir-B (file-name-directory file-B))
702 (setq emerge-last-dir-ancestor (file-name-directory file-ancestor))
703 (if output-file
704 (setq emerge-last-dir-output (file-name-directory output-file)))
705 ;; Make sure the entire files are seen, and they reflect what is on disk
706 (with-current-buffer
707 buffer-A
708 (widen)
709 (let ((temp (file-local-copy file-A)))
710 (if temp
711 (setq file-A temp
712 startup-hooks
713 (cons `(lambda () (delete-file ,file-A))
714 startup-hooks))
715 ;; Verify that the file matches the buffer
716 (emerge-verify-file-buffer))))
717 (with-current-buffer
718 buffer-B
719 (widen)
720 (let ((temp (file-local-copy file-B)))
721 (if temp
722 (setq file-B temp
723 startup-hooks
724 (cons `(lambda () (delete-file ,file-B))
725 startup-hooks))
726 ;; Verify that the file matches the buffer
727 (emerge-verify-file-buffer))))
728 (with-current-buffer
729 buffer-ancestor
730 (widen)
731 (let ((temp (file-local-copy file-ancestor)))
732 (if temp
733 (setq file-ancestor temp
734 startup-hooks
735 (cons `(lambda () (delete-file ,file-ancestor))
736 startup-hooks))
737 ;; Verify that the file matches the buffer
738 (emerge-verify-file-buffer))))
739 (emerge-setup-with-ancestor buffer-A file-A buffer-B file-B
740 buffer-ancestor file-ancestor
741 startup-hooks quit-hooks output-file)))
743 ;; Start up Emerge on two files with an ancestor
744 (defun emerge-setup-with-ancestor (buffer-A file-A buffer-B file-B
745 buffer-ancestor file-ancestor
746 &optional startup-hooks quit-hooks
747 output-file)
748 (setq file-A (expand-file-name file-A))
749 (setq file-B (expand-file-name file-B))
750 (setq file-ancestor (expand-file-name file-ancestor))
751 (setq output-file (and output-file (expand-file-name output-file)))
752 (let* ((merge-buffer-name (emerge-unique-buffer-name "*merge" "*"))
753 ;; create the merge buffer from buffer A, so it inherits buffer A's
754 ;; default directory, etc.
755 (merge-buffer (with-current-buffer
756 buffer-A
757 (get-buffer-create merge-buffer-name))))
758 (with-current-buffer
759 merge-buffer
760 (emerge-copy-modes buffer-A)
761 (setq buffer-read-only nil)
762 (auto-save-mode 1)
763 (setq emerge-mode t)
764 (setq emerge-A-buffer buffer-A)
765 (setq emerge-B-buffer buffer-B)
766 (setq emerge-ancestor-buffer buffer-ancestor)
767 (setq emerge-merge-buffer merge-buffer)
768 (setq emerge-output-description
769 (if output-file
770 (concat "Output to file: " output-file)
771 (concat "Output to buffer: " (buffer-name merge-buffer))))
772 (save-excursion (insert-buffer-substring emerge-A-buffer))
773 (emerge-set-keys)
774 (setq emerge-difference-list
775 (emerge-make-diff3-list file-A file-B file-ancestor))
776 (setq emerge-number-of-differences (length emerge-difference-list))
777 (setq emerge-current-difference -1)
778 (setq emerge-quit-hook quit-hooks)
779 (emerge-remember-buffer-characteristics)
780 (emerge-select-prefer-Bs)
781 (emerge-handle-local-variables))
782 (emerge-setup-windows buffer-A buffer-B merge-buffer t)
783 (with-current-buffer merge-buffer
784 (run-hooks 'startup-hooks 'emerge-startup-hook)
785 (setq buffer-read-only t))))
787 ;; Generate the Emerge difference list between two files with an ancestor
788 (defun emerge-make-diff3-list (file-A file-B file-ancestor)
789 (setq emerge-diff-buffer (get-buffer-create "*emerge-diff*"))
790 (with-current-buffer
791 emerge-diff-buffer
792 (erase-buffer)
793 (shell-command
794 (format "%s %s %s %s %s"
795 emerge-diff3-program emerge-diff-options
796 (emerge-protect-metachars file-A)
797 (emerge-protect-metachars file-ancestor)
798 (emerge-protect-metachars file-B))
800 (emerge-prepare-error-list emerge-diff3-ok-lines-regexp)
801 (emerge-convert-diffs-to-markers
802 emerge-A-buffer emerge-B-buffer emerge-merge-buffer
803 (emerge-extract-diffs3 emerge-diff-buffer)))
805 (defun emerge-extract-diffs3 (diff-buffer)
806 (let (list)
807 (with-current-buffer
808 diff-buffer
809 (while (re-search-forward "^====\\(.?\\)$" nil t)
810 ;; leave point after matched line
811 (beginning-of-line 2)
812 (let ((agreement (buffer-substring (match-beginning 1) (match-end 1))))
813 ;; if the A and B files are the same, ignore the difference
814 (if (not (string-equal agreement "2"))
815 (setq list
816 (cons
817 (let (group-1 group-3 pos)
818 (setq pos (point))
819 (setq group-1 (emerge-get-diff3-group "1"))
820 (goto-char pos)
821 (setq group-3 (emerge-get-diff3-group "3"))
822 (vector (car group-1) (car (cdr group-1))
823 (car group-3) (car (cdr group-3))
824 (cond ((string-equal agreement "1") 'prefer-A)
825 ((string-equal agreement "3") 'prefer-B)
826 (t 'default-A))))
827 list))))))
828 (nreverse list)))
830 (defun emerge-get-diff3-group (file)
831 ;; This save-excursion allows emerge-get-diff3-group to be called for the
832 ;; various groups of lines (1, 2, 3) in any order, and for the lines to
833 ;; appear in any order. The reason this is necessary is that Gnu diff3
834 ;; can produce the groups in the order 1, 2, 3 or 1, 3, 2.
835 (save-excursion
836 (re-search-forward
837 (concat "^" file ":\\([0-9]+\\)\\(,\\([0-9]+\\)\\)?\\([ac]\\)$"))
838 (beginning-of-line 2)
839 ;; treatment depends on whether it is an "a" group or a "c" group
840 (if (string-equal (buffer-substring (match-beginning 4) (match-end 4)) "c")
841 ;; it is a "c" group
842 (if (match-beginning 2)
843 ;; it has two numbers
844 (list (string-to-number
845 (buffer-substring (match-beginning 1) (match-end 1)))
846 (1+ (string-to-number
847 (buffer-substring (match-beginning 3) (match-end 3)))))
848 ;; it has one number
849 (let ((x (string-to-number
850 (buffer-substring (match-beginning 1) (match-end 1)))))
851 (list x (1+ x))))
852 ;; it is an "a" group
853 (let ((x (1+ (string-to-number
854 (buffer-substring (match-beginning 1) (match-end 1))))))
855 (list x x)))))
857 ;;; Functions to start Emerge on files
859 ;;;###autoload
860 (defun emerge-files (_arg file-A file-B file-out &optional startup-hooks
861 quit-hooks)
862 "Run Emerge on two files."
863 (interactive
864 (let (f)
865 (list current-prefix-arg
866 (setq f (emerge-read-file-name "File A to merge" emerge-last-dir-A
867 nil nil t))
868 (emerge-read-file-name "File B to merge" emerge-last-dir-B nil f t)
869 (and current-prefix-arg
870 (emerge-read-file-name "Output file" emerge-last-dir-output
871 f f nil)))))
872 (if file-out
873 (add-hook 'quit-hooks `(lambda () (emerge-files-exit ,file-out))))
874 (emerge-files-internal
875 file-A file-B startup-hooks
876 quit-hooks
877 file-out))
879 ;;;###autoload
880 (defun emerge-files-with-ancestor (_arg file-A file-B file-ancestor file-out
881 &optional startup-hooks quit-hooks)
882 "Run Emerge on two files, giving another file as the ancestor."
883 (interactive
884 (let (f)
885 (list current-prefix-arg
886 (setq f (emerge-read-file-name "File A to merge" emerge-last-dir-A
887 nil nil t))
888 (emerge-read-file-name "File B to merge" emerge-last-dir-B nil f t)
889 (emerge-read-file-name "Ancestor file" emerge-last-dir-ancestor
890 nil f t)
891 (and current-prefix-arg
892 (emerge-read-file-name "Output file" emerge-last-dir-output
893 f f nil)))))
894 (if file-out
895 (add-hook 'quit-hooks `(lambda () (emerge-files-exit ,file-out))))
896 (emerge-files-with-ancestor-internal
897 file-A file-B file-ancestor startup-hooks
898 quit-hooks
899 file-out))
901 ;; Write the merge buffer out in place of the file the A buffer is visiting.
902 (defun emerge-files-exit (file-out)
903 ;; if merge was successful was given, save to disk
904 (if (not emerge-prefix-argument)
905 (emerge-write-and-delete file-out)))
907 ;;; Functions to start Emerge on buffers
909 ;;;###autoload
910 (defun emerge-buffers (buffer-A buffer-B &optional startup-hooks quit-hooks)
911 "Run Emerge on two buffers."
912 (interactive "bBuffer A to merge: \nbBuffer B to merge: ")
913 (let ((emerge-file-A (emerge-make-temp-file "A"))
914 (emerge-file-B (emerge-make-temp-file "B")))
915 (with-current-buffer
916 buffer-A
917 (write-region (point-min) (point-max) emerge-file-A nil 'no-message))
918 (with-current-buffer
919 buffer-B
920 (write-region (point-min) (point-max) emerge-file-B nil 'no-message))
921 (emerge-setup (get-buffer buffer-A) emerge-file-A
922 (get-buffer buffer-B) emerge-file-B
923 (cons `(lambda ()
924 (delete-file ,emerge-file-A)
925 (delete-file ,emerge-file-B))
926 startup-hooks)
927 quit-hooks
928 nil)))
930 ;;;###autoload
931 (defun emerge-buffers-with-ancestor (buffer-A buffer-B buffer-ancestor
932 &optional startup-hooks
933 quit-hooks)
934 "Run Emerge on two buffers, giving another buffer as the ancestor."
935 (interactive
936 "bBuffer A to merge: \nbBuffer B to merge: \nbAncestor buffer: ")
937 (let ((emerge-file-A (emerge-make-temp-file "A"))
938 (emerge-file-B (emerge-make-temp-file "B"))
939 (emerge-file-ancestor (emerge-make-temp-file "anc")))
940 (with-current-buffer
941 buffer-A
942 (write-region (point-min) (point-max) emerge-file-A nil 'no-message))
943 (with-current-buffer
944 buffer-B
945 (write-region (point-min) (point-max) emerge-file-B nil 'no-message))
946 (with-current-buffer
947 buffer-ancestor
948 (write-region (point-min) (point-max) emerge-file-ancestor nil
949 'no-message))
950 (emerge-setup-with-ancestor (get-buffer buffer-A) emerge-file-A
951 (get-buffer buffer-B) emerge-file-B
952 (get-buffer buffer-ancestor)
953 emerge-file-ancestor
954 (cons `(lambda ()
955 (delete-file ,emerge-file-A)
956 (delete-file ,emerge-file-B)
957 (delete-file
958 ,emerge-file-ancestor))
959 startup-hooks)
960 quit-hooks
961 nil)))
963 ;;; Functions to start Emerge from the command line
965 ;;;###autoload
966 (defun emerge-files-command ()
967 (let ((file-a (nth 0 command-line-args-left))
968 (file-b (nth 1 command-line-args-left))
969 (file-out (nth 2 command-line-args-left)))
970 (setq command-line-args-left (nthcdr 3 command-line-args-left))
971 (emerge-files-internal
972 file-a file-b nil
973 (list `(lambda () (emerge-command-exit ,file-out))))))
975 ;;;###autoload
976 (defun emerge-files-with-ancestor-command ()
977 (let (file-a file-b file-anc file-out)
978 ;; check for a -a flag, for filemerge compatibility
979 (if (string= (car command-line-args-left) "-a")
980 ;; arguments are "-a ancestor file-a file-b file-out"
981 (progn
982 (setq file-a (nth 2 command-line-args-left))
983 (setq file-b (nth 3 command-line-args-left))
984 (setq file-anc (nth 1 command-line-args-left))
985 (setq file-out (nth 4 command-line-args-left))
986 (setq command-line-args-left (nthcdr 5 command-line-args-left)))
987 ;; arguments are "file-a file-b ancestor file-out"
988 (setq file-a (nth 0 command-line-args-left))
989 (setq file-b (nth 1 command-line-args-left))
990 (setq file-anc (nth 2 command-line-args-left))
991 (setq file-out (nth 3 command-line-args-left))
992 (setq command-line-args-left (nthcdr 4 command-line-args-left)))
993 (emerge-files-with-ancestor-internal
994 file-a file-b file-anc nil
995 (list `(lambda () (emerge-command-exit ,file-out))))))
997 (defun emerge-command-exit (file-out)
998 (emerge-write-and-delete file-out)
999 (kill-emacs (if emerge-prefix-argument 1 0)))
1001 ;;; Functions to start Emerge via remote request
1003 ;;;###autoload
1004 (defun emerge-files-remote (file-a file-b file-out)
1005 (setq emerge-file-out file-out)
1006 (emerge-files-internal
1007 file-a file-b nil
1008 (list `(lambda () (emerge-remote-exit ,file-out ',emerge-exit-func)))
1009 file-out)
1010 (throw 'client-wait nil))
1012 ;;;###autoload
1013 (defun emerge-files-with-ancestor-remote (file-a file-b file-anc file-out)
1014 (setq emerge-file-out file-out)
1015 (emerge-files-with-ancestor-internal
1016 file-a file-b file-anc nil
1017 (list `(lambda () (emerge-remote-exit ,file-out ',emerge-exit-func)))
1018 file-out)
1019 (throw 'client-wait nil))
1021 (defun emerge-remote-exit (file-out emerge-exit-func)
1022 (emerge-write-and-delete file-out)
1023 (kill-buffer emerge-merge-buffer)
1024 (funcall emerge-exit-func (if emerge-prefix-argument 1 0)))
1026 ;;; Functions to start Emerge on RCS versions
1028 ;;;###autoload
1029 (defun emerge-revisions (arg file revision-A revision-B
1030 &optional startup-hooks quit-hooks)
1031 "Emerge two RCS revisions of a file."
1032 (interactive
1033 (list current-prefix-arg
1034 (read-file-name "File to merge: " nil nil 'confirm)
1035 (read-string "Revision A to merge: " emerge-last-revision-A)
1036 (read-string "Revision B to merge: " emerge-last-revision-B)))
1037 (setq emerge-last-revision-A revision-A
1038 emerge-last-revision-B revision-B)
1039 (emerge-revisions-internal
1040 file revision-A revision-B startup-hooks
1041 (if arg
1042 (cons `(lambda ()
1043 (shell-command
1044 ,(format "%s %s" emerge-rcs-ci-program file)))
1045 quit-hooks)
1046 quit-hooks)))
1048 ;;;###autoload
1049 (defun emerge-revisions-with-ancestor (arg file revision-A
1050 revision-B ancestor
1051 &optional
1052 startup-hooks quit-hooks)
1053 "Emerge two RCS revisions of a file, with another revision as ancestor."
1054 (interactive
1055 (list current-prefix-arg
1056 (read-file-name "File to merge: " nil nil 'confirm)
1057 (read-string "Revision A to merge: " emerge-last-revision-A)
1058 (read-string "Revision B to merge: " emerge-last-revision-B)
1059 (read-string "Ancestor: " emerge-last-revision-ancestor)))
1060 (setq emerge-last-revision-A revision-A
1061 emerge-last-revision-B revision-B
1062 emerge-last-revision-ancestor ancestor)
1063 (emerge-revision-with-ancestor-internal
1064 file revision-A revision-B ancestor startup-hooks
1065 (if arg
1066 (let ((cmd ))
1067 (cons `(lambda ()
1068 (shell-command
1069 ,(format "%s %s" emerge-rcs-ci-program file)))
1070 quit-hooks))
1071 quit-hooks)))
1073 (defun emerge-revisions-internal (file revision-A revision-B &optional
1074 startup-hooks quit-hooks _output-file)
1075 (let ((buffer-A (get-buffer-create (format "%s,%s" file revision-A)))
1076 (buffer-B (get-buffer-create (format "%s,%s" file revision-B)))
1077 (emerge-file-A (emerge-make-temp-file "A"))
1078 (emerge-file-B (emerge-make-temp-file "B")))
1079 ;; Get the revisions into buffers
1080 (with-current-buffer
1081 buffer-A
1082 (erase-buffer)
1083 (shell-command
1084 (format "%s -q -p%s %s" emerge-rcs-co-program revision-A file)
1086 (write-region (point-min) (point-max) emerge-file-A nil 'no-message)
1087 (set-buffer-modified-p nil))
1088 (with-current-buffer
1089 buffer-B
1090 (erase-buffer)
1091 (shell-command
1092 (format "%s -q -p%s %s" emerge-rcs-co-program revision-B file)
1094 (write-region (point-min) (point-max) emerge-file-B nil 'no-message)
1095 (set-buffer-modified-p nil))
1096 ;; Do the merge
1097 (emerge-setup buffer-A emerge-file-A
1098 buffer-B emerge-file-B
1099 (cons `(lambda ()
1100 (delete-file ,emerge-file-A)
1101 (delete-file ,emerge-file-B))
1102 startup-hooks)
1103 (cons `(lambda () (emerge-files-exit ,file))
1104 quit-hooks)
1105 nil)))
1107 (defun emerge-revision-with-ancestor-internal (file revision-A revision-B
1108 ancestor
1109 &optional startup-hooks
1110 quit-hooks output-file)
1111 (let ((buffer-A (get-buffer-create (format "%s,%s" file revision-A)))
1112 (buffer-B (get-buffer-create (format "%s,%s" file revision-B)))
1113 (buffer-ancestor (get-buffer-create (format "%s,%s" file ancestor)))
1114 (emerge-file-A (emerge-make-temp-file "A"))
1115 (emerge-file-B (emerge-make-temp-file "B"))
1116 (emerge-ancestor (emerge-make-temp-file "ancestor")))
1117 ;; Get the revisions into buffers
1118 (with-current-buffer
1119 buffer-A
1120 (erase-buffer)
1121 (shell-command
1122 (format "%s -q -p%s %s" emerge-rcs-co-program
1123 revision-A file)
1125 (write-region (point-min) (point-max) emerge-file-A nil 'no-message)
1126 (set-buffer-modified-p nil))
1127 (with-current-buffer
1128 buffer-B
1129 (erase-buffer)
1130 (shell-command
1131 (format "%s -q -p%s %s" emerge-rcs-co-program revision-B file)
1133 (write-region (point-min) (point-max) emerge-file-B nil 'no-message)
1134 (set-buffer-modified-p nil))
1135 (with-current-buffer
1136 buffer-ancestor
1137 (erase-buffer)
1138 (shell-command
1139 (format "%s -q -p%s %s" emerge-rcs-co-program ancestor file)
1141 (write-region (point-min) (point-max) emerge-ancestor nil 'no-message)
1142 (set-buffer-modified-p nil))
1143 ;; Do the merge
1144 (emerge-setup-with-ancestor
1145 buffer-A emerge-file-A buffer-B emerge-file-B
1146 buffer-ancestor emerge-ancestor
1147 (cons `(lambda ()
1148 (delete-file ,emerge-file-A)
1149 (delete-file ,emerge-file-B)
1150 (delete-file ,emerge-ancestor))
1151 startup-hooks)
1152 (cons `(lambda () (emerge-files-exit ,file))
1153 quit-hooks)
1154 output-file)))
1156 ;;; Function to start Emerge based on a line in a file
1158 (defun emerge-execute-line ()
1159 "Run Emerge using files named in current text line.
1160 Looks in that line for whitespace-separated entries of these forms:
1161 a=file1
1162 b=file2
1163 ancestor=file3
1164 output=file4
1165 to specify the files to use in Emerge.
1167 In addition, if only one of `a=file' or `b=file' is present, and `output=file'
1168 is present:
1169 If `emerge-execute-line-deletions' is non-nil and `ancestor=file' is present,
1170 it is assumed that the file in question has been deleted, and it is
1171 not copied to the output file.
1172 Otherwise, the A or B file present is copied to the output file."
1173 (interactive)
1174 (let (file-A file-B file-ancestor file-out
1175 (case-fold-search t))
1176 ;; Stop if at end of buffer (even though we might be in a line, if
1177 ;; the line does not end with newline)
1178 (if (eobp)
1179 (error "At end of buffer"))
1180 ;; Go to the beginning of the line
1181 (beginning-of-line)
1182 ;; Skip any initial whitespace
1183 (if (looking-at "[ \t]*")
1184 (goto-char (match-end 0)))
1185 ;; Process the entire line
1186 (while (not (eolp))
1187 ;; Get the next entry
1188 (if (looking-at "\\([a-z]+\\)=\\([^ \t\n]+\\)[ \t]*")
1189 ;; Break apart the tab (before =) and the filename (after =)
1190 (let ((tag (downcase
1191 (buffer-substring (match-beginning 1) (match-end 1))))
1192 (file (buffer-substring (match-beginning 2) (match-end 2))))
1193 ;; Move point after the entry
1194 (goto-char (match-end 0))
1195 ;; Store the filename in the right variable
1196 (cond
1197 ((string-equal tag "a")
1198 (if file-A
1199 (error "This line has two `A' entries"))
1200 (setq file-A file))
1201 ((string-equal tag "b")
1202 (if file-B
1203 (error "This line has two `B' entries"))
1204 (setq file-B file))
1205 ((or (string-equal tag "anc") (string-equal tag "ancestor"))
1206 (if file-ancestor
1207 (error "This line has two `ancestor' entries"))
1208 (setq file-ancestor file))
1209 ((or (string-equal tag "out") (string-equal tag "output"))
1210 (if file-out
1211 (error "This line has two `output' entries"))
1212 (setq file-out file))
1214 (error "Unrecognized entry"))))
1215 ;; If the match on the entry pattern failed
1216 (error "Unparsable entry")))
1217 ;; Make sure that file-A and file-B are present
1218 (if (not (or (and file-A file-B) file-out))
1219 (error "Must have both `A' and `B' entries"))
1220 (if (not (or file-A file-B))
1221 (error "Must have `A' or `B' entry"))
1222 ;; Go to the beginning of the next line, so next execution will use
1223 ;; next line in buffer.
1224 (beginning-of-line 2)
1225 ;; Execute the correct command
1226 (cond
1227 ;; Merge of two files with ancestor
1228 ((and file-A file-B file-ancestor)
1229 (message "Merging %s and %s..." file-A file-B)
1230 (emerge-files-with-ancestor (not (not file-out)) file-A file-B
1231 file-ancestor file-out
1233 ;; When done, return to this buffer.
1234 (list
1235 `(lambda ()
1236 (switch-to-buffer ,(current-buffer))
1237 (message "Merge done.")))))
1238 ;; Merge of two files without ancestor
1239 ((and file-A file-B)
1240 (message "Merging %s and %s..." file-A file-B)
1241 (emerge-files (not (not file-out)) file-A file-B file-out
1243 ;; When done, return to this buffer.
1244 (list
1245 `(lambda ()
1246 (switch-to-buffer ,(current-buffer))
1247 (message "Merge done.")))))
1248 ;; There is an output file (or there would have been an error above),
1249 ;; but only one input file.
1250 ;; The file appears to have been deleted in one version; do nothing.
1251 ((and file-ancestor emerge-execute-line-deletions)
1252 (message "No action."))
1253 ;; The file should be copied from the version that contains it
1254 (t (let ((input-file (or file-A file-B)))
1255 (message "Copying...")
1256 (copy-file input-file file-out)
1257 (message "%s copied to %s." input-file file-out))))))
1259 ;;; Sample function for creating information for emerge-execute-line
1261 (defcustom emerge-merge-directories-filename-regexp "[^.]"
1262 "Regexp describing files to be processed by `emerge-merge-directories'."
1263 :type 'regexp
1264 :group 'emerge)
1266 ;;;###autoload
1267 (defun emerge-merge-directories (a-dir b-dir ancestor-dir output-dir)
1268 (interactive
1269 (list
1270 (read-directory-name "A directory: " nil nil 'confirm)
1271 (read-directory-name "B directory: " nil nil 'confirm)
1272 (read-directory-name "Ancestor directory (null for none): " nil nil 'confirm)
1273 (read-directory-name "Output directory (null for none): " nil nil 'confirm)))
1274 ;; Check that we're not on a line
1275 (if (not (and (bolp) (eolp)))
1276 (error "There is text on this line"))
1277 ;; Turn null strings into nil to indicate directories not used.
1278 (if (and ancestor-dir (string-equal ancestor-dir ""))
1279 (setq ancestor-dir nil))
1280 (if (and output-dir (string-equal output-dir ""))
1281 (setq output-dir nil))
1282 ;; Canonicalize the directory names
1283 (setq a-dir (expand-file-name a-dir))
1284 (if (not (string-equal (substring a-dir -1) "/"))
1285 (setq a-dir (concat a-dir "/")))
1286 (setq b-dir (expand-file-name b-dir))
1287 (if (not (string-equal (substring b-dir -1) "/"))
1288 (setq b-dir (concat b-dir "/")))
1289 (if ancestor-dir
1290 (progn
1291 (setq ancestor-dir (expand-file-name ancestor-dir))
1292 (if (not (string-equal (substring ancestor-dir -1) "/"))
1293 (setq ancestor-dir (concat ancestor-dir "/")))))
1294 (if output-dir
1295 (progn
1296 (setq output-dir (expand-file-name output-dir))
1297 (if (not (string-equal (substring output-dir -1) "/"))
1298 (setq output-dir (concat output-dir "/")))))
1299 ;; Set the mark to where we start
1300 (push-mark)
1301 ;; Find out what files are in the directories.
1302 (let* ((a-dir-files
1303 (directory-files a-dir nil emerge-merge-directories-filename-regexp))
1304 (b-dir-files
1305 (directory-files b-dir nil emerge-merge-directories-filename-regexp))
1306 (ancestor-dir-files
1307 (and ancestor-dir
1308 (directory-files ancestor-dir nil
1309 emerge-merge-directories-filename-regexp)))
1310 (all-files (sort (nconc (copy-sequence a-dir-files)
1311 (copy-sequence b-dir-files)
1312 (copy-sequence ancestor-dir-files))
1313 (function string-lessp))))
1314 ;; Remove duplicates from all-files.
1315 (let ((p all-files))
1316 (while p
1317 (if (and (cdr p) (string-equal (car p) (car (cdr p))))
1318 (setcdr p (cdr (cdr p)))
1319 (setq p (cdr p)))))
1320 ;; Generate the control lines for the various files.
1321 (while all-files
1322 (let ((f (car all-files)))
1323 (setq all-files (cdr all-files))
1324 (if (and a-dir-files (string-equal (car a-dir-files) f))
1325 (progn
1326 (insert "A=" a-dir f "\t")
1327 (setq a-dir-files (cdr a-dir-files))))
1328 (if (and b-dir-files (string-equal (car b-dir-files) f))
1329 (progn
1330 (insert "B=" b-dir f "\t")
1331 (setq b-dir-files (cdr b-dir-files))))
1332 (if (and ancestor-dir-files (string-equal (car ancestor-dir-files) f))
1333 (progn
1334 (insert "ancestor=" ancestor-dir f "\t")
1335 (setq ancestor-dir-files (cdr ancestor-dir-files))))
1336 (if output-dir
1337 (insert "output=" output-dir f "\t"))
1338 (backward-delete-char 1)
1339 (insert "\n")))))
1341 ;;; Common setup routines
1343 ;; Set up the window configuration. If POS is given, set the points to
1344 ;; the beginnings of the buffers.
1345 (defun emerge-setup-windows (buffer-A buffer-B merge-buffer &optional pos)
1346 ;; Make sure we are not in the minibuffer window when we try to delete
1347 ;; all other windows.
1348 (if (eq (selected-window) (minibuffer-window))
1349 (other-window 1))
1350 (delete-other-windows)
1351 (switch-to-buffer merge-buffer)
1352 (emerge-refresh-mode-line)
1353 (split-window-below)
1354 (split-window-right)
1355 (switch-to-buffer buffer-A)
1356 (if pos
1357 (goto-char (point-min)))
1358 (other-window 1)
1359 (switch-to-buffer buffer-B)
1360 (if pos
1361 (goto-char (point-min)))
1362 (other-window 1)
1363 (if pos
1364 (goto-char (point-min)))
1365 ;; If diff/diff3 reports errors, display them rather than the merge buffer.
1366 (if (/= 0 (with-current-buffer emerge-diff-error-buffer (buffer-size)))
1367 (progn
1368 (ding)
1369 (message "Errors found in diff/diff3 output. Merge buffer is %s."
1370 (buffer-name emerge-merge-buffer))
1371 (switch-to-buffer emerge-diff-error-buffer))))
1373 ;; Set up the keymap in the merge buffer
1374 (defun emerge-set-keys ()
1375 ;; Set up fixed keymaps if necessary
1376 (if (not emerge-basic-keymap)
1377 (emerge-setup-fixed-keymaps))
1378 ;; Save the old local map
1379 (setq emerge-old-keymap (current-local-map))
1380 ;; Construct the edit keymap
1381 (setq emerge-edit-keymap (if emerge-old-keymap
1382 (copy-keymap emerge-old-keymap)
1383 (make-sparse-keymap)))
1384 ;; Install the Emerge commands
1385 (emerge-force-define-key emerge-edit-keymap emerge-command-prefix
1386 'emerge-basic-keymap)
1387 (define-key emerge-edit-keymap [menu-bar] (make-sparse-keymap))
1389 ;; Create the additional menu bar items.
1390 (define-key emerge-edit-keymap [menu-bar emerge-options]
1391 (cons "Merge-Options" emerge-options-menu))
1392 (define-key emerge-edit-keymap [menu-bar merge]
1393 (cons "Merge" emerge-merge-menu))
1394 (define-key emerge-edit-keymap [menu-bar move]
1395 (cons "Move" emerge-move-menu))
1397 ;; Suppress write-file and save-buffer
1398 (substitute-key-definition 'write-file
1399 'emerge-query-write-file
1400 emerge-edit-keymap)
1401 (substitute-key-definition 'save-buffer
1402 'emerge-query-save-buffer
1403 emerge-edit-keymap)
1404 (define-key emerge-edit-keymap [remap write-file] 'emerge-query-write-file)
1405 (define-key emerge-edit-keymap [remap save-buffer] 'emerge-query-save-buffer)
1406 (use-local-map emerge-fast-keymap)
1407 (setq emerge-edit-mode nil)
1408 (setq emerge-fast-mode t))
1410 (defun emerge-remember-buffer-characteristics ()
1411 "Record certain properties of the buffers being merged.
1412 Must be called in the merge buffer. Remembers read-only, modified,
1413 auto-save, and saves them in buffer local variables. Sets the buffers
1414 read-only and turns off `auto-save-mode'.
1415 These characteristics are restored by `emerge-restore-buffer-characteristics'."
1416 ;; force auto-save, because we will turn off auto-saving in buffers for the
1417 ;; duration
1418 (do-auto-save)
1419 ;; remember and alter buffer characteristics
1420 (setq emerge-A-buffer-values
1421 (with-current-buffer
1422 emerge-A-buffer
1423 (prog1
1424 (emerge-save-variables emerge-saved-variables)
1425 (emerge-restore-variables emerge-saved-variables
1426 emerge-merging-values))))
1427 (setq emerge-B-buffer-values
1428 (with-current-buffer
1429 emerge-B-buffer
1430 (prog1
1431 (emerge-save-variables emerge-saved-variables)
1432 (emerge-restore-variables emerge-saved-variables
1433 emerge-merging-values)))))
1435 (defun emerge-restore-buffer-characteristics ()
1436 "Restore characteristics saved by `emerge-remember-buffer-characteristics'."
1437 (let ((A-values emerge-A-buffer-values)
1438 (B-values emerge-B-buffer-values))
1439 (with-current-buffer emerge-A-buffer
1440 (emerge-restore-variables emerge-saved-variables
1441 A-values))
1442 (with-current-buffer emerge-B-buffer
1443 (emerge-restore-variables emerge-saved-variables
1444 B-values))))
1446 ;; Move to line DESIRED-LINE assuming we are at line CURRENT-LINE.
1447 ;; Return DESIRED-LINE.
1448 (defun emerge-goto-line (desired-line current-line)
1449 (forward-line (- desired-line current-line))
1450 desired-line)
1452 (defun emerge-convert-diffs-to-markers (A-buffer
1453 B-buffer
1454 merge-buffer
1455 lineno-list)
1456 (let* (marker-list
1457 (A-point-min (with-current-buffer A-buffer (point-min)))
1458 (offset (1- A-point-min))
1459 (B-point-min (with-current-buffer B-buffer (point-min)))
1460 ;; Record current line number in each buffer
1461 ;; so we don't have to count from the beginning.
1462 (a-line 1)
1463 (b-line 1))
1464 (with-current-buffer A-buffer (goto-char (point-min)))
1465 (with-current-buffer B-buffer (goto-char (point-min)))
1466 (while lineno-list
1467 (let* ((list-element (car lineno-list))
1468 a-begin-marker
1469 a-end-marker
1470 b-begin-marker
1471 b-end-marker
1472 merge-begin-marker
1473 merge-end-marker
1474 (a-begin (aref list-element 0))
1475 (a-end (aref list-element 1))
1476 (b-begin (aref list-element 2))
1477 (b-end (aref list-element 3))
1478 (state (aref list-element 4)))
1479 ;; place markers at the appropriate places in the buffers
1480 (with-current-buffer
1481 A-buffer
1482 (setq a-line (emerge-goto-line a-begin a-line))
1483 (setq a-begin-marker (point-marker))
1484 (setq a-line (emerge-goto-line a-end a-line))
1485 (setq a-end-marker (point-marker)))
1486 (with-current-buffer
1487 B-buffer
1488 (setq b-line (emerge-goto-line b-begin b-line))
1489 (setq b-begin-marker (point-marker))
1490 (setq b-line (emerge-goto-line b-end b-line))
1491 (setq b-end-marker (point-marker)))
1492 (setq merge-begin-marker (set-marker
1493 (make-marker)
1494 (- (marker-position a-begin-marker)
1495 offset)
1496 merge-buffer))
1497 (setq merge-end-marker (set-marker
1498 (make-marker)
1499 (- (marker-position a-end-marker)
1500 offset)
1501 merge-buffer))
1502 ;; record all the markers for this difference
1503 (setq marker-list (cons (vector a-begin-marker a-end-marker
1504 b-begin-marker b-end-marker
1505 merge-begin-marker merge-end-marker
1506 state)
1507 marker-list)))
1508 (setq lineno-list (cdr lineno-list)))
1509 ;; convert the list of difference information into a vector for
1510 ;; fast access
1511 (setq emerge-difference-list (apply 'vector (nreverse marker-list)))))
1513 ;; If we have an ancestor, select all B variants that we prefer
1514 (defun emerge-select-prefer-Bs ()
1515 (let ((n 0))
1516 (while (< n emerge-number-of-differences)
1517 (if (eq (aref (aref emerge-difference-list n) 6) 'prefer-B)
1518 (progn
1519 (emerge-unselect-and-select-difference n t)
1520 (emerge-select-B)
1521 (aset (aref emerge-difference-list n) 6 'prefer-B)))
1522 (setq n (1+ n))))
1523 (emerge-unselect-and-select-difference -1))
1525 ;; Process the local-variables list at the end of the merged file, if
1526 ;; requested.
1527 (defun emerge-handle-local-variables ()
1528 (if emerge-process-local-variables
1529 (condition-case err
1530 (hack-local-variables)
1531 (error (message "Local-variables error in merge buffer: %s"
1532 (prin1-to-string err))))))
1534 ;;; Common exit routines
1536 (defun emerge-write-and-delete (file-out)
1537 ;; clear screen format
1538 (delete-other-windows)
1539 ;; delete A, B, and ancestor buffers, if they haven't been changed
1540 (if (not (buffer-modified-p emerge-A-buffer))
1541 (kill-buffer emerge-A-buffer))
1542 (if (not (buffer-modified-p emerge-B-buffer))
1543 (kill-buffer emerge-B-buffer))
1544 (if (and emerge-ancestor-buffer
1545 (not (buffer-modified-p emerge-ancestor-buffer)))
1546 (kill-buffer emerge-ancestor-buffer))
1547 ;; Write merge buffer to file
1548 (and file-out
1549 (write-file file-out)))
1551 ;;; Commands
1553 (defun emerge-recenter (&optional arg)
1554 "Bring the highlighted region of all three merge buffers into view.
1555 This brings the buffers into view if they are in windows.
1556 With an argument, reestablish the default three-window display."
1557 (interactive "P")
1558 ;; If there is an argument, rebuild the window structure
1559 (if arg
1560 (emerge-setup-windows emerge-A-buffer emerge-B-buffer
1561 emerge-merge-buffer))
1562 ;; Redisplay whatever buffers are showing, if there is a selected difference
1563 (if (and (>= emerge-current-difference 0)
1564 (< emerge-current-difference emerge-number-of-differences))
1565 (let* ((merge-buffer emerge-merge-buffer)
1566 (buffer-A emerge-A-buffer)
1567 (buffer-B emerge-B-buffer)
1568 (window-A (get-buffer-window buffer-A 'visible))
1569 (window-B (get-buffer-window buffer-B 'visible))
1570 (merge-window (get-buffer-window merge-buffer))
1571 (diff-vector
1572 (aref emerge-difference-list emerge-current-difference)))
1573 (if window-A (progn
1574 (select-window window-A)
1575 (emerge-position-region
1576 (- (aref diff-vector 0)
1577 (1- emerge-before-flag-length))
1578 (+ (aref diff-vector 1)
1579 (1- emerge-after-flag-length))
1580 (1+ (aref diff-vector 0)))))
1581 (if window-B (progn
1582 (select-window window-B)
1583 (emerge-position-region
1584 (- (aref diff-vector 2)
1585 (1- emerge-before-flag-length))
1586 (+ (aref diff-vector 3)
1587 (1- emerge-after-flag-length))
1588 (1+ (aref diff-vector 2)))))
1589 (if merge-window (progn
1590 (select-window merge-window)
1591 (emerge-position-region
1592 (- (aref diff-vector 4)
1593 (1- emerge-before-flag-length))
1594 (+ (aref diff-vector 5)
1595 (1- emerge-after-flag-length))
1596 (1+ (aref diff-vector 4))))))))
1598 ;;; Window scrolling operations
1599 ;; These operations are designed to scroll all three windows the same amount,
1600 ;; so as to keep the text in them aligned.
1602 ;; Perform some operation on all three windows (if they are showing).
1603 ;; Catches all errors on the operation in the A and B windows, but not
1604 ;; in the merge window. Usually, errors come from scrolling off the
1605 ;; beginning or end of the buffer, and this gives a nice error message:
1606 ;; End of buffer is reported in the merge buffer, but if the scroll was
1607 ;; possible in the A or B windows, it is performed there before the error
1608 ;; is reported.
1609 (defun emerge-operate-on-windows (operation arg)
1610 (let* ((merge-buffer emerge-merge-buffer)
1611 (buffer-A emerge-A-buffer)
1612 (buffer-B emerge-B-buffer)
1613 (window-A (get-buffer-window buffer-A 'visible))
1614 (window-B (get-buffer-window buffer-B 'visible))
1615 (merge-window (get-buffer-window merge-buffer)))
1616 (if window-A (progn
1617 (select-window window-A)
1618 (condition-case nil
1619 (funcall operation arg)
1620 (error))))
1621 (if window-B (progn
1622 (select-window window-B)
1623 (condition-case nil
1624 (funcall operation arg)
1625 (error))))
1626 (if merge-window (progn
1627 (select-window merge-window)
1628 (funcall operation arg)))))
1630 (defun emerge-scroll-up (&optional arg)
1631 "Scroll up all three merge buffers, if they are in windows.
1632 With argument N, scroll N lines; otherwise scroll by nearly
1633 the height of the merge window.
1634 `C-u -' alone as argument scrolls half the height of the merge window."
1635 (interactive "P")
1636 (emerge-operate-on-windows
1637 'scroll-up
1638 ;; calculate argument to scroll-up
1639 ;; if there is an explicit argument
1640 (if (and arg (not (equal arg '-)))
1641 ;; use it
1642 (prefix-numeric-value arg)
1643 ;; if not, see if we can determine a default amount (the window height)
1644 (let ((merge-window (get-buffer-window emerge-merge-buffer)))
1645 (if (null merge-window)
1646 ;; no window, use nil
1648 (let ((default-amount
1649 (- (window-height merge-window) 1 next-screen-context-lines)))
1650 ;; the window was found
1651 (if arg
1652 ;; C-u as argument means half of default amount
1653 (/ default-amount 2)
1654 ;; no argument means default amount
1655 default-amount)))))))
1657 (defun emerge-scroll-down (&optional arg)
1658 "Scroll down all three merge buffers, if they are in windows.
1659 With argument N, scroll N lines; otherwise scroll by nearly
1660 the height of the merge window.
1661 `C-u -' alone as argument scrolls half the height of the merge window."
1662 (interactive "P")
1663 (emerge-operate-on-windows
1664 'scroll-down
1665 ;; calculate argument to scroll-down
1666 ;; if there is an explicit argument
1667 (if (and arg (not (equal arg '-)))
1668 ;; use it
1669 (prefix-numeric-value arg)
1670 ;; if not, see if we can determine a default amount (the window height)
1671 (let ((merge-window (get-buffer-window emerge-merge-buffer)))
1672 (if (null merge-window)
1673 ;; no window, use nil
1675 (let ((default-amount
1676 (- (window-height merge-window) 1 next-screen-context-lines)))
1677 ;; the window was found
1678 (if arg
1679 ;; C-u as argument means half of default amount
1680 (/ default-amount 2)
1681 ;; no argument means default amount
1682 default-amount)))))))
1684 (defun emerge-scroll-left (&optional arg)
1685 "Scroll left all three merge buffers, if they are in windows.
1686 If an argument is given, that is how many columns are scrolled, else nearly
1687 the width of the A and B windows. `C-u -' alone as argument scrolls half the
1688 width of the A and B windows."
1689 (interactive "P")
1690 (emerge-operate-on-windows
1691 'scroll-left
1692 ;; calculate argument to scroll-left
1693 ;; if there is an explicit argument
1694 (if (and arg (not (equal arg '-)))
1695 ;; use it
1696 (prefix-numeric-value arg)
1697 ;; if not, see if we can determine a default amount
1698 ;; (half the window width)
1699 (let ((merge-window (get-buffer-window emerge-merge-buffer)))
1700 (if (null merge-window)
1701 ;; no window, use nil
1703 (let ((default-amount
1704 (- (/ (window-width merge-window) 2) 3)))
1705 ;; the window was found
1706 (if arg
1707 ;; C-u as argument means half of default amount
1708 (/ default-amount 2)
1709 ;; no argument means default amount
1710 default-amount)))))))
1712 (defun emerge-scroll-right (&optional arg)
1713 "Scroll right all three merge buffers, if they are in windows.
1714 If an argument is given, that is how many columns are scrolled, else nearly
1715 the width of the A and B windows. `C-u -' alone as argument scrolls half the
1716 width of the A and B windows."
1717 (interactive "P")
1718 (emerge-operate-on-windows
1719 'scroll-right
1720 ;; calculate argument to scroll-right
1721 ;; if there is an explicit argument
1722 (if (and arg (not (equal arg '-)))
1723 ;; use it
1724 (prefix-numeric-value arg)
1725 ;; if not, see if we can determine a default amount
1726 ;; (half the window width)
1727 (let ((merge-window (get-buffer-window emerge-merge-buffer)))
1728 (if (null merge-window)
1729 ;; no window, use nil
1731 (let ((default-amount
1732 (- (/ (window-width merge-window) 2) 3)))
1733 ;; the window was found
1734 (if arg
1735 ;; C-u as argument means half of default amount
1736 (/ default-amount 2)
1737 ;; no argument means default amount
1738 default-amount)))))))
1740 (defun emerge-scroll-reset ()
1741 "Reset horizontal scrolling in Emerge.
1742 This resets the horizontal scrolling of all three merge buffers
1743 to the left margin, if they are in windows."
1744 (interactive)
1745 (emerge-operate-on-windows
1746 (lambda (x) (set-window-hscroll (selected-window) 0))
1747 nil))
1749 ;; Attempt to show the region nicely.
1750 ;; If there are min-lines lines above and below the region, then don't do
1751 ;; anything.
1752 ;; If not, recenter the region to make it so.
1753 ;; If that isn't possible, remove context lines evenly from top and bottom
1754 ;; so the entire region shows.
1755 ;; If that isn't possible, show the top of the region.
1756 ;; BEG must be at the beginning of a line.
1757 (defun emerge-position-region (beg end pos)
1758 ;; First test whether the entire region is visible with
1759 ;; emerge-min-visible-lines above and below it
1760 (if (not (and (<= (progn
1761 (move-to-window-line emerge-min-visible-lines)
1762 (point))
1763 beg)
1764 (<= end (progn
1765 (move-to-window-line
1766 (- (1+ emerge-min-visible-lines)))
1767 (point)))))
1768 ;; We failed that test, see if it fits at all
1769 ;; Meanwhile positioning it correctly in case it doesn't fit
1770 (progn
1771 (set-window-start (selected-window) beg)
1772 (if (pos-visible-in-window-p end)
1773 ;; Determine the number of lines that the region occupies
1774 (let ((lines 0))
1775 (while (> end (progn
1776 (move-to-window-line lines)
1777 (point)))
1778 (setq lines (1+ lines)))
1779 ;; And position the beginning on the right line
1780 (goto-char beg)
1781 (recenter (/ (1+ (- (1- (window-height))
1782 lines))
1783 2))))))
1784 (goto-char pos))
1786 (defun emerge-next-difference ()
1787 "Advance to the next difference."
1788 (interactive)
1789 (if (< emerge-current-difference emerge-number-of-differences)
1790 (let ((n (1+ emerge-current-difference)))
1791 (while (and emerge-skip-prefers
1792 (< n emerge-number-of-differences)
1793 (memq (aref (aref emerge-difference-list n) 6)
1794 '(prefer-A prefer-B)))
1795 (setq n (1+ n)))
1796 (let ((buffer-read-only nil))
1797 (emerge-unselect-and-select-difference n)))
1798 (error "At end")))
1800 (defun emerge-previous-difference ()
1801 "Go to the previous difference."
1802 (interactive)
1803 (if (> emerge-current-difference -1)
1804 (let ((n (1- emerge-current-difference)))
1805 (while (and emerge-skip-prefers
1806 (> n -1)
1807 (memq (aref (aref emerge-difference-list n) 6)
1808 '(prefer-A prefer-B)))
1809 (setq n (1- n)))
1810 (let ((buffer-read-only nil))
1811 (emerge-unselect-and-select-difference n)))
1812 (error "At beginning")))
1814 (defun emerge-jump-to-difference (difference-number)
1815 "Go to the N-th difference."
1816 (interactive "p")
1817 (let ((buffer-read-only nil))
1818 (setq difference-number (1- difference-number))
1819 (if (and (>= difference-number -1)
1820 (< difference-number (1+ emerge-number-of-differences)))
1821 (emerge-unselect-and-select-difference difference-number)
1822 (error "Bad difference number"))))
1824 (defun emerge-abort ()
1825 "Abort the Emerge session."
1826 (interactive)
1827 (emerge-quit t))
1829 (defun emerge-quit (arg)
1830 "Finish the Emerge session and exit Emerge.
1831 Prefix argument means to abort rather than successfully finish.
1832 The difference depends on how the merge was started,
1833 but usually means to not write over one of the original files, or to signal
1834 to some process which invoked Emerge a failure code.
1836 Unselects the selected difference, if any, restores the read-only and modified
1837 flags of the merged file buffers, restores the local keymap of the merge
1838 buffer, and sets off various emerge flags. Using Emerge commands in this
1839 buffer after this will cause serious problems."
1840 (interactive "P")
1841 (if (prog1
1842 (y-or-n-p
1843 (if (not arg)
1844 "Do you really want to successfully finish this merge? "
1845 "Do you really want to abort this merge? "))
1846 (message ""))
1847 (emerge-really-quit arg)))
1849 ;; Perform the quit operations.
1850 (defun emerge-really-quit (arg)
1851 (setq buffer-read-only nil)
1852 (emerge-unselect-and-select-difference -1)
1853 (emerge-restore-buffer-characteristics)
1854 ;; null out the difference markers so they don't slow down future editing
1855 ;; operations
1856 (mapc (lambda (d)
1857 (set-marker (aref d 0) nil)
1858 (set-marker (aref d 1) nil)
1859 (set-marker (aref d 2) nil)
1860 (set-marker (aref d 3) nil)
1861 (set-marker (aref d 4) nil)
1862 (set-marker (aref d 5) nil))
1863 emerge-difference-list)
1864 ;; allow them to be garbage collected
1865 (setq emerge-difference-list nil)
1866 ;; restore the local map
1867 (use-local-map emerge-old-keymap)
1868 ;; turn off all the emerge modes
1869 (setq emerge-mode nil)
1870 (setq emerge-fast-mode nil)
1871 (setq emerge-edit-mode nil)
1872 (setq emerge-auto-advance nil)
1873 (setq emerge-skip-prefers nil)
1874 ;; restore mode line
1875 (kill-local-variable 'mode-line-buffer-identification)
1876 (let ((emerge-prefix-argument arg))
1877 (run-hooks 'emerge-quit-hook)))
1879 (defun emerge-select-A (&optional force)
1880 "Select the A variant of this difference.
1881 Refuses to function if this difference has been edited, i.e., if it
1882 is neither the A nor the B variant.
1883 A prefix argument forces the variant to be selected
1884 even if the difference has been edited."
1885 (interactive "P")
1886 (let ((operate
1887 (lambda ()
1888 (emerge-select-A-edit merge-begin merge-end A-begin A-end)
1889 (if emerge-auto-advance
1890 (emerge-next-difference))))
1891 (operate-no-change
1892 (lambda () (if emerge-auto-advance
1893 (emerge-next-difference)))))
1894 (emerge-select-version force operate-no-change operate operate)))
1896 ;; Actually select the A variant
1897 (defun emerge-select-A-edit (merge-begin merge-end A-begin A-end)
1898 (with-current-buffer
1899 emerge-merge-buffer
1900 (delete-region merge-begin merge-end)
1901 (goto-char merge-begin)
1902 (insert-buffer-substring emerge-A-buffer A-begin A-end)
1903 (goto-char merge-begin)
1904 (aset diff-vector 6 'A)
1905 (emerge-refresh-mode-line)))
1907 (defun emerge-select-B (&optional force)
1908 "Select the B variant of this difference.
1909 Refuses to function if this difference has been edited, i.e., if it
1910 is neither the A nor the B variant.
1911 A prefix argument forces the variant to be selected
1912 even if the difference has been edited."
1913 (interactive "P")
1914 (let ((operate
1915 (lambda ()
1916 (emerge-select-B-edit merge-begin merge-end B-begin B-end)
1917 (if emerge-auto-advance
1918 (emerge-next-difference))))
1919 (operate-no-change
1920 (lambda () (if emerge-auto-advance
1921 (emerge-next-difference)))))
1922 (emerge-select-version force operate operate-no-change operate)))
1924 ;; Actually select the B variant
1925 (defun emerge-select-B-edit (merge-begin merge-end B-begin B-end)
1926 (with-current-buffer
1927 emerge-merge-buffer
1928 (delete-region merge-begin merge-end)
1929 (goto-char merge-begin)
1930 (insert-buffer-substring emerge-B-buffer B-begin B-end)
1931 (goto-char merge-begin)
1932 (aset diff-vector 6 'B)
1933 (emerge-refresh-mode-line)))
1935 (defun emerge-default-A ()
1936 "Make the A variant the default from here down.
1937 This selects the A variant for all differences from here down in the buffer
1938 which are still defaulted, i.e., which the user has not selected and for
1939 which there is no preference."
1940 (interactive)
1941 (let ((buffer-read-only nil))
1942 (let ((selected-difference emerge-current-difference)
1943 (n (max emerge-current-difference 0)))
1944 (while (< n emerge-number-of-differences)
1945 (let ((diff-vector (aref emerge-difference-list n)))
1946 (if (eq (aref diff-vector 6) 'default-B)
1947 (progn
1948 (emerge-unselect-and-select-difference n t)
1949 (emerge-select-A)
1950 (aset diff-vector 6 'default-A))))
1951 (setq n (1+ n))
1952 (if (zerop (% n 10))
1953 (message "Setting default to A...%d" n)))
1954 (emerge-unselect-and-select-difference selected-difference)))
1955 (message "Default choice is now A"))
1957 (defun emerge-default-B ()
1958 "Make the B variant the default from here down.
1959 This selects the B variant for all differences from here down in the buffer
1960 which are still defaulted, i.e., which the user has not selected and for
1961 which there is no preference."
1962 (interactive)
1963 (let ((buffer-read-only nil))
1964 (let ((selected-difference emerge-current-difference)
1965 (n (max emerge-current-difference 0)))
1966 (while (< n emerge-number-of-differences)
1967 (let ((diff-vector (aref emerge-difference-list n)))
1968 (if (eq (aref diff-vector 6) 'default-A)
1969 (progn
1970 (emerge-unselect-and-select-difference n t)
1971 (emerge-select-B)
1972 (aset diff-vector 6 'default-B))))
1973 (setq n (1+ n))
1974 (if (zerop (% n 10))
1975 (message "Setting default to B...%d" n)))
1976 (emerge-unselect-and-select-difference selected-difference)))
1977 (message "Default choice is now B"))
1979 (defun emerge-fast-mode ()
1980 "Set fast mode, for Emerge.
1981 In this mode ordinary Emacs commands are disabled, and Emerge commands
1982 need not be prefixed with \\<emerge-fast-keymap>\\[emerge-basic-keymap]."
1983 (interactive)
1984 (setq buffer-read-only t)
1985 (use-local-map emerge-fast-keymap)
1986 (setq emerge-mode t)
1987 (setq emerge-fast-mode t)
1988 (setq emerge-edit-mode nil)
1989 (message "Fast mode set")
1990 (force-mode-line-update))
1992 (defun emerge-edit-mode ()
1993 "Set edit mode, for Emerge.
1994 In this mode ordinary Emacs commands are available, and Emerge commands
1995 must be prefixed with \\<emerge-fast-keymap>\\[emerge-basic-keymap]."
1996 (interactive)
1997 (setq buffer-read-only nil)
1998 (use-local-map emerge-edit-keymap)
1999 (setq emerge-mode t)
2000 (setq emerge-fast-mode nil)
2001 (setq emerge-edit-mode t)
2002 (message "Edit mode set")
2003 (force-mode-line-update))
2005 (defun emerge-auto-advance (arg)
2006 "Toggle Auto-Advance mode, for Emerge.
2007 This mode causes `emerge-select-A' and `emerge-select-B' to automatically
2008 advance to the next difference.
2009 With a positive argument, turn on Auto-Advance mode.
2010 With a negative argument, turn off Auto-Advance mode."
2011 (interactive "P")
2012 (setq emerge-auto-advance (if (null arg)
2013 (not emerge-auto-advance)
2014 (> (prefix-numeric-value arg) 0)))
2015 (message (if emerge-auto-advance
2016 "Auto-advance set"
2017 "Auto-advance cleared"))
2018 (force-mode-line-update))
2020 (defun emerge-skip-prefers (arg)
2021 "Toggle Skip-Prefers mode, for Emerge.
2022 This mode causes `emerge-next-difference' and `emerge-previous-difference'
2023 to automatically skip over differences for which there is a preference.
2024 With a positive argument, turn on Skip-Prefers mode.
2025 With a negative argument, turn off Skip-Prefers mode."
2026 (interactive "P")
2027 (setq emerge-skip-prefers (if (null arg)
2028 (not emerge-skip-prefers)
2029 (> (prefix-numeric-value arg) 0)))
2030 (message (if emerge-skip-prefers
2031 "Skip-prefers set"
2032 "Skip-prefers cleared"))
2033 (force-mode-line-update))
2035 (defun emerge-copy-as-kill-A ()
2036 "Put the A variant of this difference in the kill ring."
2037 (interactive)
2038 (emerge-validate-difference)
2039 (let* ((diff-vector
2040 (aref emerge-difference-list emerge-current-difference))
2041 (A-begin (1+ (aref diff-vector 0)))
2042 (A-end (1- (aref diff-vector 1)))
2043 ;; so further kills don't append
2044 this-command)
2045 (with-current-buffer emerge-A-buffer
2046 (copy-region-as-kill A-begin A-end))))
2048 (defun emerge-copy-as-kill-B ()
2049 "Put the B variant of this difference in the kill ring."
2050 (interactive)
2051 (emerge-validate-difference)
2052 (let* ((diff-vector
2053 (aref emerge-difference-list emerge-current-difference))
2054 (B-begin (1+ (aref diff-vector 2)))
2055 (B-end (1- (aref diff-vector 3)))
2056 ;; so further kills don't append
2057 this-command)
2058 (with-current-buffer emerge-B-buffer
2059 (copy-region-as-kill B-begin B-end))))
2061 (defun emerge-insert-A (arg)
2062 "Insert the A variant of this difference at the point.
2063 Leaves point after text, mark before.
2064 With prefix argument, puts point before, mark after."
2065 (interactive "P")
2066 (emerge-validate-difference)
2067 (let* ((diff-vector
2068 (aref emerge-difference-list emerge-current-difference))
2069 (A-begin (1+ (aref diff-vector 0)))
2070 (A-end (1- (aref diff-vector 1)))
2071 (opoint (point))
2072 (buffer-read-only nil))
2073 (insert-buffer-substring emerge-A-buffer A-begin A-end)
2074 (if (not arg)
2075 (set-mark opoint)
2076 (set-mark (point))
2077 (goto-char opoint))))
2079 (defun emerge-insert-B (arg)
2080 "Insert the B variant of this difference at the point.
2081 Leaves point after text, mark before.
2082 With prefix argument, puts point before, mark after."
2083 (interactive "P")
2084 (emerge-validate-difference)
2085 (let* ((diff-vector
2086 (aref emerge-difference-list emerge-current-difference))
2087 (B-begin (1+ (aref diff-vector 2)))
2088 (B-end (1- (aref diff-vector 3)))
2089 (opoint (point))
2090 (buffer-read-only nil))
2091 (insert-buffer-substring emerge-B-buffer B-begin B-end)
2092 (if (not arg)
2093 (set-mark opoint)
2094 (set-mark (point))
2095 (goto-char opoint))))
2097 (defun emerge-mark-difference (arg)
2098 "Leaves the point before this difference and the mark after it.
2099 With prefix argument, puts mark before, point after."
2100 (interactive "P")
2101 (emerge-validate-difference)
2102 (let* ((diff-vector
2103 (aref emerge-difference-list emerge-current-difference))
2104 (merge-begin (1+ (aref diff-vector 4)))
2105 (merge-end (1- (aref diff-vector 5))))
2106 (if (not arg)
2107 (progn
2108 (goto-char merge-begin)
2109 (set-mark merge-end))
2110 (goto-char merge-end)
2111 (set-mark merge-begin))))
2113 (defun emerge-file-names ()
2114 "Show the names of the buffers or files being operated on by Emerge.
2115 Use C-u l to reset the windows afterward."
2116 (interactive)
2117 (delete-other-windows)
2118 (let ((temp-buffer-show-function
2119 (lambda (buf)
2120 (split-window-below)
2121 (switch-to-buffer buf)
2122 (other-window 1))))
2123 (with-output-to-temp-buffer "*Help*"
2124 (with-current-buffer emerge-A-buffer
2125 (if buffer-file-name
2126 (progn
2127 (princ "File A is: ")
2128 (princ buffer-file-name))
2129 (progn
2130 (princ "Buffer A is: ")
2131 (princ (buffer-name))))
2132 (princ "\n"))
2133 (with-current-buffer emerge-B-buffer
2134 (if buffer-file-name
2135 (progn
2136 (princ "File B is: ")
2137 (princ buffer-file-name))
2138 (progn
2139 (princ "Buffer B is: ")
2140 (princ (buffer-name))))
2141 (princ "\n"))
2142 (if emerge-ancestor-buffer
2143 (with-current-buffer emerge-ancestor-buffer
2144 (if buffer-file-name
2145 (progn
2146 (princ "Ancestor file is: ")
2147 (princ buffer-file-name))
2148 (progn
2149 (princ "Ancestor buffer is: ")
2150 (princ (buffer-name))))
2151 (princ "\n")))
2152 (princ emerge-output-description)
2153 (with-current-buffer standard-output
2154 (help-mode)))))
2156 (defun emerge-join-differences (arg)
2157 "Join the selected difference with the following one.
2158 With a prefix argument, join with the preceding one."
2159 (interactive "P")
2160 (let ((n emerge-current-difference))
2161 ;; adjust n to be first difference to join
2162 (if arg
2163 (setq n (1- n)))
2164 ;; n and n+1 are the differences to join
2165 ;; check that they are both differences
2166 (if (or (< n 0) (>= n (1- emerge-number-of-differences)))
2167 (error "Incorrect differences to join"))
2168 ;; remove the flags
2169 (emerge-unselect-difference emerge-current-difference)
2170 ;; decrement total number of differences
2171 (setq emerge-number-of-differences (1- emerge-number-of-differences))
2172 ;; build new differences vector
2173 (let ((i 0)
2174 (new-differences (make-vector emerge-number-of-differences nil)))
2175 (while (< i emerge-number-of-differences)
2176 (aset new-differences i
2177 (cond
2178 ((< i n) (aref emerge-difference-list i))
2179 ((> i n) (aref emerge-difference-list (1+ i)))
2180 (t (let ((prev (aref emerge-difference-list i))
2181 (next (aref emerge-difference-list (1+ i))))
2182 (vector (aref prev 0)
2183 (aref next 1)
2184 (aref prev 2)
2185 (aref next 3)
2186 (aref prev 4)
2187 (aref next 5)
2188 (let ((ps (aref prev 6))
2189 (ns (aref next 6)))
2190 (cond
2191 ((eq ps ns)
2193 ((and (or (eq ps 'B) (eq ps 'prefer-B))
2194 (or (eq ns 'B) (eq ns 'prefer-B)))
2196 (t 'A))))))))
2197 (setq i (1+ i)))
2198 (setq emerge-difference-list new-differences))
2199 ;; set the current difference correctly
2200 (setq emerge-current-difference n)
2201 ;; fix the mode line
2202 (emerge-refresh-mode-line)
2203 ;; reinsert the flags
2204 (emerge-select-difference emerge-current-difference)
2205 (emerge-recenter)))
2207 (defun emerge-split-difference ()
2208 "Split the current difference where the points are in the three windows."
2209 (interactive)
2210 (let ((n emerge-current-difference))
2211 ;; check that this is a valid difference
2212 (emerge-validate-difference)
2213 ;; get the point values and old difference
2214 (let ((A-point (with-current-buffer emerge-A-buffer
2215 (point-marker)))
2216 (B-point (with-current-buffer emerge-B-buffer
2217 (point-marker)))
2218 (merge-point (point-marker))
2219 (old-diff (aref emerge-difference-list n)))
2220 ;; check location of the points, give error if they aren't in the
2221 ;; differences
2222 (if (or (< A-point (aref old-diff 0))
2223 (> A-point (aref old-diff 1)))
2224 (error "Point outside of difference in A buffer"))
2225 (if (or (< B-point (aref old-diff 2))
2226 (> B-point (aref old-diff 3)))
2227 (error "Point outside of difference in B buffer"))
2228 (if (or (< merge-point (aref old-diff 4))
2229 (> merge-point (aref old-diff 5)))
2230 (error "Point outside of difference in merge buffer"))
2231 ;; remove the flags
2232 (emerge-unselect-difference emerge-current-difference)
2233 ;; increment total number of differences
2234 (setq emerge-number-of-differences (1+ emerge-number-of-differences))
2235 ;; build new differences vector
2236 (let ((i 0)
2237 (new-differences (make-vector emerge-number-of-differences nil)))
2238 (while (< i emerge-number-of-differences)
2239 (aset new-differences i
2240 (cond
2241 ((< i n)
2242 (aref emerge-difference-list i))
2243 ((> i (1+ n))
2244 (aref emerge-difference-list (1- i)))
2245 ((= i n)
2246 (vector (aref old-diff 0)
2247 A-point
2248 (aref old-diff 2)
2249 B-point
2250 (aref old-diff 4)
2251 merge-point
2252 (aref old-diff 6)))
2254 (vector (copy-marker A-point)
2255 (aref old-diff 1)
2256 (copy-marker B-point)
2257 (aref old-diff 3)
2258 (copy-marker merge-point)
2259 (aref old-diff 5)
2260 (aref old-diff 6)))))
2261 (setq i (1+ i)))
2262 (setq emerge-difference-list new-differences))
2263 ;; set the current difference correctly
2264 (setq emerge-current-difference n)
2265 ;; fix the mode line
2266 (emerge-refresh-mode-line)
2267 ;; reinsert the flags
2268 (emerge-select-difference emerge-current-difference)
2269 (emerge-recenter))))
2271 (defun emerge-trim-difference ()
2272 "Trim lines off top and bottom of difference that are the same.
2273 If lines are the same in both the A and the B versions, strip them off.
2274 \(This can happen when the A and B versions have common lines that the
2275 ancestor version does not share.)"
2276 (interactive)
2277 ;; make sure we are in a real difference
2278 (emerge-validate-difference)
2279 ;; remove the flags
2280 (emerge-unselect-difference emerge-current-difference)
2281 (let* ((diff (aref emerge-difference-list emerge-current-difference))
2282 (top-a (marker-position (aref diff 0)))
2283 (bottom-a (marker-position (aref diff 1)))
2284 (top-b (marker-position (aref diff 2)))
2285 (bottom-b (marker-position (aref diff 3)))
2286 (top-m (marker-position (aref diff 4)))
2287 (bottom-m (marker-position (aref diff 5)))
2288 size success sa sb sm)
2289 ;; move down the tops of the difference regions as much as possible
2290 ;; Try advancing comparing 1000 chars at a time.
2291 ;; When that fails, go 500 chars at a time, and so on.
2292 (setq size 1000)
2293 (while (> size 0)
2294 (setq success t)
2295 (while success
2296 (setq size (min size (- bottom-a top-a) (- bottom-b top-b)
2297 (- bottom-m top-m)))
2298 (setq sa (with-current-buffer emerge-A-buffer
2299 (buffer-substring top-a
2300 (+ size top-a))))
2301 (setq sb (with-current-buffer emerge-B-buffer
2302 (buffer-substring top-b
2303 (+ size top-b))))
2304 (setq sm (buffer-substring top-m (+ size top-m)))
2305 (setq success (and (> size 0) (equal sa sb) (equal sb sm)))
2306 (if success
2307 (setq top-a (+ top-a size)
2308 top-b (+ top-b size)
2309 top-m (+ top-m size))))
2310 (setq size (/ size 2)))
2311 ;; move up the bottoms of the difference regions as much as possible
2312 ;; Try advancing comparing 1000 chars at a time.
2313 ;; When that fails, go 500 chars at a time, and so on.
2314 (setq size 1000)
2315 (while (> size 0)
2316 (setq success t)
2317 (while success
2318 (setq size (min size (- bottom-a top-a) (- bottom-b top-b)
2319 (- bottom-m top-m)))
2320 (setq sa (with-current-buffer emerge-A-buffer
2321 (buffer-substring (- bottom-a size)
2322 bottom-a)))
2323 (setq sb (with-current-buffer emerge-B-buffer
2324 (buffer-substring (- bottom-b size)
2325 bottom-b)))
2326 (setq sm (buffer-substring (- bottom-m size) bottom-m))
2327 (setq success (and (> size 0) (equal sa sb) (equal sb sm)))
2328 (if success
2329 (setq bottom-a (- bottom-a size)
2330 bottom-b (- bottom-b size)
2331 bottom-m (- bottom-m size))))
2332 (setq size (/ size 2)))
2333 ;; {top,bottom}-{a,b,m} are now set at the new beginnings and ends
2334 ;; of the difference regions. Move them to the beginning of lines, as
2335 ;; appropriate.
2336 (with-current-buffer emerge-A-buffer
2337 (goto-char top-a)
2338 (beginning-of-line)
2339 (aset diff 0 (point-marker))
2340 (goto-char bottom-a)
2341 (beginning-of-line 2)
2342 (aset diff 1 (point-marker)))
2343 (with-current-buffer emerge-B-buffer
2344 (goto-char top-b)
2345 (beginning-of-line)
2346 (aset diff 2 (point-marker))
2347 (goto-char bottom-b)
2348 (beginning-of-line 2)
2349 (aset diff 3 (point-marker)))
2350 (goto-char top-m)
2351 (beginning-of-line)
2352 (aset diff 4 (point-marker))
2353 (goto-char bottom-m)
2354 (beginning-of-line 2)
2355 (aset diff 5 (point-marker))
2356 ;; put the flags back in, recenter the display
2357 (emerge-select-difference emerge-current-difference)
2358 (emerge-recenter)))
2360 ;; FIXME the manual advertised this as working in the A or B buffers,
2361 ;; but it does not, because all the buffer locals are nil there.
2362 ;; It would work to call it from the merge buffer and specify that one
2363 ;; wants to use the value of point in the A or B buffer.
2364 ;; But with the prefix argument already in use, there is no easy way
2365 ;; to have it ask for a buffer.
2366 (defun emerge-find-difference (arg)
2367 "Find the difference containing the current position of the point.
2368 If there is no containing difference and the prefix argument is positive,
2369 it finds the nearest following difference. A negative prefix argument finds
2370 the nearest previous difference."
2371 (interactive "P")
2372 (cond ((eq (current-buffer) emerge-A-buffer)
2373 (emerge-find-difference-A arg))
2374 ((eq (current-buffer) emerge-B-buffer)
2375 (emerge-find-difference-B arg))
2376 (t (emerge-find-difference-merge arg))))
2378 (defun emerge-find-difference-merge (arg)
2379 "Find the difference containing point, in the merge buffer.
2380 If there is no containing difference and the prefix argument is positive,
2381 it finds the nearest following difference. A negative prefix argument finds
2382 the nearest previous difference."
2383 (interactive "P")
2384 ;; search for the point in the merge buffer, using the markers
2385 ;; for the beginning and end of the differences in the merge buffer
2386 (emerge-find-difference1 arg (point) 4 5))
2388 (defun emerge-find-difference-A (arg)
2389 "Find the difference containing point, in the A buffer.
2390 This command must be executed in the merge buffer.
2391 If there is no containing difference and the prefix argument is positive,
2392 it finds the nearest following difference. A negative prefix argument finds
2393 the nearest previous difference."
2394 (interactive "P")
2395 ;; search for the point in the A buffer, using the markers
2396 ;; for the beginning and end of the differences in the A buffer
2397 (emerge-find-difference1 arg
2398 (with-current-buffer emerge-A-buffer (point))
2399 0 1))
2401 (defun emerge-find-difference-B (arg)
2402 "Find the difference containing point, in the B buffer.
2403 This command must be executed in the merge buffer.
2404 If there is no containing difference and the prefix argument is positive,
2405 it finds the nearest following difference. A negative prefix argument finds
2406 the nearest previous difference."
2407 (interactive "P")
2408 ;; search for the point in the B buffer, using the markers
2409 ;; for the beginning and end of the differences in the B buffer
2410 (emerge-find-difference1 arg
2411 (with-current-buffer emerge-B-buffer (point))
2412 2 3))
2414 (defun emerge-find-difference1 (arg location begin end)
2415 (let* ((index
2416 ;; find first difference containing or after the current position
2417 (catch 'search
2418 (let ((n 0))
2419 (while (< n emerge-number-of-differences)
2420 (let ((diff-vector (aref emerge-difference-list n)))
2421 (if (<= location (marker-position (aref diff-vector end)))
2422 (throw 'search n)))
2423 (setq n (1+ n))))
2424 emerge-number-of-differences))
2425 (contains
2426 ;; whether the found difference contains the current position
2427 (and (< index emerge-number-of-differences)
2428 (<= (marker-position (aref (aref emerge-difference-list index)
2429 begin))
2430 location)))
2431 (arg-value
2432 ;; numeric value of prefix argument
2433 (prefix-numeric-value arg)))
2434 (emerge-unselect-and-select-difference
2435 (cond
2436 ;; if the point is in a difference, select it
2437 (contains index)
2438 ;; if the arg is nil and the point is not in a difference, error
2439 ((null arg) (error "No difference contains point"))
2440 ;; if the arg is positive, select the following difference
2441 ((> arg-value 0)
2442 (if (< index emerge-number-of-differences)
2443 index
2444 (error "No difference contains or follows point")))
2445 ;; if the arg is negative, select the preceding difference
2447 (if (> index 0)
2448 (1- index)
2449 (error "No difference contains or precedes point")))))))
2451 (defun emerge-line-numbers ()
2452 "Display the current line numbers.
2453 This function displays the line numbers of the points in the A, B, and
2454 merge buffers."
2455 (interactive)
2456 (let* ((valid-diff
2457 (and (>= emerge-current-difference 0)
2458 (< emerge-current-difference emerge-number-of-differences)))
2459 (emerge-line-diff (and valid-diff
2460 (aref emerge-difference-list
2461 emerge-current-difference)))
2462 (merge-line (emerge-line-number-in-buf 4 5))
2463 (A-line (with-current-buffer emerge-A-buffer
2464 (emerge-line-number-in-buf 0 1)))
2465 (B-line (with-current-buffer emerge-B-buffer
2466 (emerge-line-number-in-buf 2 3))))
2467 (message "At lines: merge = %d, A = %d, B = %d"
2468 merge-line A-line B-line)))
2470 (defvar emerge-line-diff)
2472 (defun emerge-line-number-in-buf (begin-marker end-marker)
2473 ;; FIXME point-min rather than 1? widen?
2474 (let ((temp (1+ (count-lines 1 (line-beginning-position)))))
2475 (if valid-diff
2476 (progn
2477 (if (> (point) (aref emerge-line-diff begin-marker))
2478 (setq temp (- temp emerge-before-flag-lines)))
2479 (if (> (point) (aref emerge-line-diff end-marker))
2480 (setq temp (- temp emerge-after-flag-lines)))))
2481 temp))
2483 (defun emerge-set-combine-template (string &optional localize)
2484 "Set `emerge-combine-versions-template' to STRING.
2485 This value controls how `emerge-combine-versions' combines the two versions.
2486 With prefix argument, `emerge-combine-versions-template' is made local to this
2487 merge buffer. Localization is permanent for any particular merge buffer."
2488 (interactive "s\nP")
2489 (if localize
2490 (make-local-variable 'emerge-combine-versions-template))
2491 (setq emerge-combine-versions-template string)
2492 (message
2493 (if (assq 'emerge-combine-versions-template (buffer-local-variables))
2494 "emerge-set-combine-versions-template set locally"
2495 "emerge-set-combine-versions-template set")))
2497 (defun emerge-set-combine-versions-template (start end &optional localize)
2498 "Copy region into `emerge-combine-versions-template'.
2499 This controls how `emerge-combine-versions' will combine the two versions.
2500 With prefix argument, `emerge-combine-versions-template' is made local to this
2501 merge buffer. Localization is permanent for any particular merge buffer."
2502 (interactive "r\nP")
2503 (if localize
2504 (make-local-variable 'emerge-combine-versions-template))
2505 (setq emerge-combine-versions-template (buffer-substring start end))
2506 (message
2507 (if (assq 'emerge-combine-versions-template (buffer-local-variables))
2508 "emerge-set-combine-versions-template set locally."
2509 "emerge-set-combine-versions-template set.")))
2511 (defun emerge-combine-versions (&optional force)
2512 "Combine versions using the template in `emerge-combine-versions-template'.
2513 Refuses to function if this difference has been edited, i.e., if it is
2514 neither the A nor the B variant.
2515 An argument forces the variant to be selected even if the difference has
2516 been edited."
2517 (interactive "P")
2518 (emerge-combine-versions-internal emerge-combine-versions-template force))
2520 (defun emerge-combine-versions-register (char &optional force)
2521 "Combine the two versions using the template in register REG.
2522 See documentation of the variable `emerge-combine-versions-template'
2523 for how the template is interpreted.
2524 Refuses to function if this difference has been edited, i.e., if it is
2525 neither the A nor the B variant.
2526 An argument forces the variant to be selected even if the difference has
2527 been edited.
2529 Interactively, reads the register using `register-read-with-preview'."
2530 (interactive (list
2531 (register-read-with-preview "Register containing template: ")
2532 current-prefix-arg))
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 ;; When the pointless option emerge-temp-file-prefix goes,
2887 ;; make this function obsolete too, and just use make-temp-file.
2888 (defun emerge-make-temp-file (prefix)
2889 "Make a private temporary file based on `emerge-temp-file-prefix'."
2890 (make-temp-file (concat emerge-temp-file-prefix prefix)))
2892 ;;; Functions that query the user before he can write out the current buffer.
2894 (defun emerge-query-write-file ()
2895 "Ask the user whether to write out an incomplete merge.
2896 If answer is yes, call `write-file' to do so. See `emerge-query-and-call'
2897 for details of the querying process."
2898 (interactive)
2899 (emerge-query-and-call 'write-file))
2901 (defun emerge-query-save-buffer ()
2902 "Ask the user whether to save an incomplete merge.
2903 If answer is yes, call `save-buffer' to do so. See `emerge-query-and-call'
2904 for details of the querying process."
2905 (interactive)
2906 (emerge-query-and-call 'save-buffer))
2908 (defun emerge-query-and-call (command)
2909 "Ask the user whether to save or write out the incomplete merge.
2910 If answer is yes, call COMMAND interactively. During the call, the flags
2911 around the current difference are removed."
2912 (if (yes-or-no-p "Do you really write to write out this unfinished merge? ")
2913 ;; He really wants to do it -- unselect the difference for the duration
2914 (progn
2915 (if (and (>= emerge-current-difference 0)
2916 (< emerge-current-difference emerge-number-of-differences))
2917 (emerge-unselect-difference emerge-current-difference))
2918 ;; call-interactively takes the value of current-prefix-arg as the
2919 ;; prefix argument value to be passed to the command. Thus, we have
2920 ;; to do nothing special to make sure the prefix argument is
2921 ;; transmitted to the command.
2922 (call-interactively command)
2923 (if (and (>= emerge-current-difference 0)
2924 (< emerge-current-difference emerge-number-of-differences))
2925 (progn
2926 (emerge-select-difference emerge-current-difference)
2927 (emerge-recenter))))
2928 ;; He's being smart and not doing it
2929 (message "Not written")))
2931 ;; Make sure the current buffer (for a file) has the same contents as the
2932 ;; file on disk, and attempt to remedy the situation if not.
2933 ;; Signal an error if we can't make them the same, or the user doesn't want
2934 ;; to do what is necessary to make them the same.
2935 (defun emerge-verify-file-buffer ()
2936 ;; First check if the file has been modified since the buffer visited it.
2937 (if (verify-visited-file-modtime (current-buffer))
2938 (if (buffer-modified-p)
2939 ;; If buffer is not obsolete and is modified, offer to save
2940 (if (yes-or-no-p (format "Save file %s? " buffer-file-name))
2941 (save-buffer)
2942 (error "Buffer out of sync for file %s" buffer-file-name))
2943 ;; If buffer is not obsolete and is not modified, do nothing
2944 nil)
2945 (if (buffer-modified-p)
2946 ;; If buffer is obsolete and is modified, give error
2947 (error "Buffer out of sync for file %s" buffer-file-name)
2948 ;; If buffer is obsolete and is not modified, offer to revert
2949 (if (yes-or-no-p (format "Revert file %s? " buffer-file-name))
2950 (revert-buffer t t)
2951 (error "Buffer out of sync for file %s" buffer-file-name)))))
2953 ;; Utilities that might have value outside of Emerge.
2955 ;; Set up the mode in the current buffer to duplicate the mode in another
2956 ;; buffer.
2957 (defun emerge-copy-modes (buffer)
2958 ;; Set the major mode
2959 (funcall (with-current-buffer buffer major-mode)))
2961 ;; Define a key, even if a prefix of it is defined
2962 (defun emerge-force-define-key (keymap key definition)
2963 "Like `define-key', but forcibly creates prefix characters as needed.
2964 If some prefix of KEY has a non-prefix definition, it is redefined."
2965 ;; Find out if a prefix of key is defined
2966 (let ((v (lookup-key keymap key)))
2967 ;; If so, undefine it
2968 (if (integerp v)
2969 (define-key keymap (substring key 0 v) nil)))
2970 ;; Now define the key
2971 (define-key keymap key definition))
2973 ;;;;; Improvements to describe-mode, so that it describes minor modes as well
2974 ;;;;; as the major mode
2975 ;;(defun describe-mode (&optional minor)
2976 ;; "Display documentation of current major mode.
2977 ;;If optional arg MINOR is non-nil (or prefix argument is given if interactive),
2978 ;;display documentation of active minor modes as well.
2979 ;;For this to work correctly for a minor mode, the mode's indicator variable
2980 ;;\(listed in `minor-mode-alist') must also be a function whose documentation
2981 ;;describes the minor mode."
2982 ;; (interactive)
2983 ;; (with-output-to-temp-buffer "*Help*"
2984 ;; (princ mode-name)
2985 ;; (princ " Mode:\n")
2986 ;; (princ (documentation major-mode))
2987 ;; (let ((minor-modes minor-mode-alist)
2988 ;; (locals (buffer-local-variables)))
2989 ;; (while minor-modes
2990 ;; (let* ((minor-mode (car (car minor-modes)))
2991 ;; (indicator (car (cdr (car minor-modes))))
2992 ;; (local-binding (assq minor-mode locals)))
2993 ;; ;; Document a minor mode if it is listed in minor-mode-alist,
2994 ;; ;; bound locally in this buffer, non-nil, and has a function
2995 ;; ;; definition.
2996 ;; (if (and local-binding
2997 ;; (cdr local-binding)
2998 ;; (fboundp minor-mode))
2999 ;; (progn
3000 ;; (princ (format "\n\n\n%s minor mode (indicator%s):\n"
3001 ;; minor-mode indicator))
3002 ;; (princ (documentation minor-mode)))))
3003 ;; (setq minor-modes (cdr minor-modes))))
3004 ;; (with-current-buffer standard-output
3005 ;; (help-mode))
3006 ;; (help-print-return-message)))
3008 ;; This goes with the redefinition of describe-mode.
3009 ;;;; Adjust things so that keyboard macro definitions are documented correctly.
3010 ;;(fset 'defining-kbd-macro (symbol-function 'start-kbd-macro))
3012 ;; substitute-key-definition should work now.
3013 ;;;; Function to shadow a definition in a keymap with definitions in another.
3014 ;;(defun emerge-shadow-key-definition (olddef newdef keymap shadowmap)
3015 ;; "Shadow OLDDEF with NEWDEF for any keys in KEYMAP with entries in SHADOWMAP.
3016 ;;In other words, SHADOWMAP will now shadow all definitions of OLDDEF in KEYMAP
3017 ;;with NEWDEF. Does not affect keys that are already defined in SHADOWMAP,
3018 ;;including those whose definition is OLDDEF."
3019 ;; ;; loop through all keymaps accessible from keymap
3020 ;; (let ((maps (accessible-keymaps keymap)))
3021 ;; (while maps
3022 ;; (let ((prefix (car (car maps)))
3023 ;; (map (cdr (car maps))))
3024 ;; ;; examine a keymap
3025 ;; (if (arrayp map)
3026 ;; ;; array keymap
3027 ;; (let ((len (length map))
3028 ;; (i 0))
3029 ;; (while (< i len)
3030 ;; (if (eq (aref map i) olddef)
3031 ;; ;; set the shadowing definition
3032 ;; (let ((key (concat prefix (char-to-string i))))
3033 ;; (emerge-define-key-if-possible shadowmap key newdef)))
3034 ;; (setq i (1+ i))))
3035 ;; ;; sparse keymap
3036 ;; (while map
3037 ;; (if (eq (cdr-safe (car-safe map)) olddef)
3038 ;; ;; set the shadowing definition
3039 ;; (let ((key
3040 ;; (concat prefix (char-to-string (car (car map))))))
3041 ;; (emerge-define-key-if-possible shadowmap key newdef)))
3042 ;; (setq map (cdr map)))))
3043 ;; (setq maps (cdr maps)))))
3045 ;; Define a key if it (or a prefix) is not already defined in the map.
3046 (defun emerge-define-key-if-possible (keymap key definition)
3047 ;; look up the present definition of the key
3048 (let ((present (lookup-key keymap key)))
3049 (if (integerp present)
3050 ;; if it is "too long", look up the valid prefix
3051 (if (not (lookup-key keymap (substring key 0 present)))
3052 ;; if the prefix isn't defined, define it
3053 (define-key keymap key definition))
3054 ;; if there is no present definition, define it
3055 (if (not present)
3056 (define-key keymap key definition)))))
3058 ;; Ordinary substitute-key-definition should do this now.
3059 ;;(defun emerge-recursively-substitute-key-definition (olddef newdef keymap)
3060 ;; "Like `substitute-key-definition', but act recursively on subkeymaps.
3061 ;;Make sure that subordinate keymaps aren't shared with other keymaps!
3062 ;;\(`copy-keymap' will suffice.)"
3063 ;; ;; Loop through all keymaps accessible from keymap
3064 ;; (let ((maps (accessible-keymaps keymap)))
3065 ;; (while maps
3066 ;; ;; Substitute in this keymap
3067 ;; (substitute-key-definition olddef newdef (cdr (car maps)))
3068 ;; (setq maps (cdr maps)))))
3070 ;; Show the name of the file in the buffer.
3071 (defun emerge-show-file-name ()
3072 "Displays the name of the file loaded into the current buffer.
3073 If the name won't fit on one line, the minibuffer is expanded to hold it,
3074 and the command waits for a keystroke from the user. If the keystroke is
3075 SPC, it is ignored; if it is anything else, it is processed as a command."
3076 (interactive)
3077 (let ((name (buffer-file-name)))
3078 (or name
3079 (setq name "Buffer has no file name."))
3080 (save-window-excursion
3081 (select-window (minibuffer-window))
3082 (unwind-protect
3083 (progn
3084 (erase-buffer)
3085 (insert name)
3086 (while (and (not (pos-visible-in-window-p))
3087 (not (window-full-height-p)))
3088 (enlarge-window 1))
3089 (let* ((echo-keystrokes 0)
3090 (c (read-event)))
3091 (if (not (eq c 32))
3092 (push c unread-command-events))))
3093 (erase-buffer)))))
3095 ;; Improved auto-save file names.
3096 ;; This function fixes many problems with the standard auto-save file names:
3097 ;; Auto-save files for non-file buffers get put in the default directory
3098 ;; for the buffer, whether that makes sense or not.
3099 ;; Auto-save files for file buffers get put in the directory of the file,
3100 ;; regardless of whether we can write into it or not.
3101 ;; Auto-save files for non-file buffers don't use the process id, so if a
3102 ;; user runs more than on Emacs, they can make auto-save files that overwrite
3103 ;; each other.
3104 ;; To use this function, do:
3105 ;; (fset 'make-auto-save-file-name
3106 ;; (symbol-function 'emerge-make-auto-save-file-name))
3107 (defun emerge-make-auto-save-file-name ()
3108 "Return file name to use for auto-saves of current buffer.
3109 Does not consider `auto-save-visited-file-name';
3110 that is checked before calling this function.
3111 You can redefine this for customization.
3112 See also `auto-save-file-name-p'."
3113 (if buffer-file-name
3114 ;; if buffer has a file, try the format <file directory>/#<file name>#
3115 (let ((f (concat (file-name-directory buffer-file-name)
3117 (file-name-nondirectory buffer-file-name)
3118 "#")))
3119 (if (file-writable-p f)
3120 ;; the file is writable, so use it
3122 ;; the file isn't writable, so use the format
3123 ;; ~/#&<file name>&<hash of directory>#
3124 (concat (getenv "HOME")
3125 "/#&"
3126 (file-name-nondirectory buffer-file-name)
3128 (emerge-hash-string-into-string
3129 (file-name-directory buffer-file-name))
3130 "#")))
3131 ;; if buffer has no file, use the format ~/#%<buffer name>%<process id>#
3132 (expand-file-name (concat (getenv "HOME")
3133 "/#%"
3134 ;; quote / into \! and \ into \\
3135 (emerge-unslashify-name (buffer-name))
3137 (make-temp-name "")
3138 "#"))))
3140 ;; Hash a string into five characters more-or-less suitable for use in a file
3141 ;; name. (Allowed characters are ! through ~, except /.)
3142 (defun emerge-hash-string-into-string (s)
3143 (let ((bins (vector 0 0 0 0 0))
3144 (i 0))
3145 (while (< i (length s))
3146 (aset bins (% i 5) (% (+ (* (aref bins (% i 5)) 35)
3147 (aref s i))
3148 65536))
3149 (setq i (1+ i)))
3150 (mapconcat (lambda (b)
3151 (setq b (+ (% b 93) ?!))
3152 (if (>= b ?/)
3153 (setq b (1+ b)))
3154 (char-to-string b))
3155 bins "")))
3157 ;; Quote any /s in a string by replacing them with \!.
3158 ;; Also, replace any \s by \\, to make it one-to-one.
3159 (defun emerge-unslashify-name (s)
3160 (let ((limit 0))
3161 (while (string-match "[/\\]" s limit)
3162 (setq s (concat (substring s 0 (match-beginning 0))
3163 (if (string= (substring s (match-beginning 0)
3164 (match-end 0))
3165 "/")
3166 "\\!"
3167 "\\\\")
3168 (substring s (match-end 0))))
3169 (setq limit (1+ (match-end 0)))))
3172 ;; Metacharacters that have to be protected from the shell when executing
3173 ;; a diff/diff3 command.
3174 (defcustom emerge-metachars
3175 (if (memq system-type '(ms-dos windows-nt))
3176 "[ \t\"<>|?*^&=]"
3177 "[ \t\n!\"#$&'()*;<=>?[\\^`{|~]")
3178 "Characters that must be quoted when used in a shell command line.
3179 More precisely, a [...] regexp to match any one such character."
3180 :type 'regexp
3181 :group 'emerge)
3183 ;; Quote metacharacters (using \) when executing a diff/diff3 command.
3184 (defun emerge-protect-metachars (s)
3185 (if (memq system-type '(ms-dos windows-nt))
3186 (shell-quote-argument s)
3187 (let ((limit 0))
3188 (while (string-match emerge-metachars s limit)
3189 (setq s (concat (substring s 0 (match-beginning 0))
3190 "\\"
3191 (substring s (match-beginning 0))))
3192 (setq limit (1+ (match-end 0)))))
3195 (provide 'emerge)
3197 ;;; emerge.el ends here