Followup to last change in browse-url.el
[emacs.git] / lisp / vc / emerge.el
blob3f945bbb2bf18622f8305907c19bdbd1eecc9313
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 (shell-quote-argument emerge-diff-program)
625 emerge-diff-options
626 (shell-quote-argument file-A)
627 (shell-quote-argument file-B))
629 (emerge-prepare-error-list emerge-diff-ok-lines-regexp)
630 (emerge-convert-diffs-to-markers
631 emerge-A-buffer emerge-B-buffer emerge-merge-buffer
632 (emerge-extract-diffs emerge-diff-buffer)))
634 (defun emerge-extract-diffs (diff-buffer)
635 (let (list)
636 (with-current-buffer
637 diff-buffer
638 (goto-char (point-min))
639 (while (re-search-forward emerge-match-diff-line nil t)
640 (let* ((a-begin (string-to-number (buffer-substring (match-beginning 1)
641 (match-end 1))))
642 (a-end (let ((b (match-beginning 3))
643 (e (match-end 3)))
644 (if b
645 (string-to-number (buffer-substring b e))
646 a-begin)))
647 (diff-type (buffer-substring (match-beginning 4) (match-end 4)))
648 (b-begin (string-to-number (buffer-substring (match-beginning 5)
649 (match-end 5))))
650 (b-end (let ((b (match-beginning 7))
651 (e (match-end 7)))
652 (if b
653 (string-to-number (buffer-substring b e))
654 b-begin))))
655 ;; fix the beginning and end numbers, because diff is somewhat
656 ;; strange about how it numbers lines
657 (if (string-equal diff-type "a")
658 (progn
659 (setq b-end (1+ b-end))
660 (setq a-begin (1+ a-begin))
661 (setq a-end a-begin))
662 (if (string-equal diff-type "d")
663 (progn
664 (setq a-end (1+ a-end))
665 (setq b-begin (1+ b-begin))
666 (setq b-end b-begin))
667 ;; (string-equal diff-type "c")
668 (progn
669 (setq a-end (1+ a-end))
670 (setq b-end (1+ b-end)))))
671 (setq list (cons (vector a-begin a-end
672 b-begin b-end
673 'default-A)
674 list)))))
675 (nreverse list)))
677 ;; Set up buffer of diff/diff3 error messages.
678 (defun emerge-prepare-error-list (ok-regexp)
679 (setq emerge-diff-error-buffer (get-buffer-create "*emerge-diff-errors*"))
680 (with-current-buffer
681 emerge-diff-error-buffer
682 (erase-buffer)
683 (save-excursion (insert-buffer-substring emerge-diff-buffer))
684 (delete-matching-lines ok-regexp)))
686 ;;; Top-level and setup functions for three-file mode.
688 (defun emerge-files-with-ancestor-internal (file-A file-B file-ancestor
689 &optional startup-hooks quit-hooks
690 output-file)
691 (if (not (file-readable-p file-A))
692 (error "File `%s' does not exist or is not readable" file-A))
693 (if (not (file-readable-p file-B))
694 (error "File `%s' does not exist or is not readable" file-B))
695 (if (not (file-readable-p file-ancestor))
696 (error "File `%s' does not exist or is not readable" file-ancestor))
697 (let ((buffer-A (find-file-noselect file-A))
698 (buffer-B (find-file-noselect file-B))
699 (buffer-ancestor (find-file-noselect file-ancestor)))
700 ;; Record the directories of the files
701 (setq emerge-last-dir-A (file-name-directory file-A))
702 (setq emerge-last-dir-B (file-name-directory file-B))
703 (setq emerge-last-dir-ancestor (file-name-directory file-ancestor))
704 (if output-file
705 (setq emerge-last-dir-output (file-name-directory output-file)))
706 ;; Make sure the entire files are seen, and they reflect what is on disk
707 (with-current-buffer
708 buffer-A
709 (widen)
710 (let ((temp (file-local-copy file-A)))
711 (if temp
712 (setq file-A temp
713 startup-hooks
714 (cons `(lambda () (delete-file ,file-A))
715 startup-hooks))
716 ;; Verify that the file matches the buffer
717 (emerge-verify-file-buffer))))
718 (with-current-buffer
719 buffer-B
720 (widen)
721 (let ((temp (file-local-copy file-B)))
722 (if temp
723 (setq file-B temp
724 startup-hooks
725 (cons `(lambda () (delete-file ,file-B))
726 startup-hooks))
727 ;; Verify that the file matches the buffer
728 (emerge-verify-file-buffer))))
729 (with-current-buffer
730 buffer-ancestor
731 (widen)
732 (let ((temp (file-local-copy file-ancestor)))
733 (if temp
734 (setq file-ancestor temp
735 startup-hooks
736 (cons `(lambda () (delete-file ,file-ancestor))
737 startup-hooks))
738 ;; Verify that the file matches the buffer
739 (emerge-verify-file-buffer))))
740 (emerge-setup-with-ancestor buffer-A file-A buffer-B file-B
741 buffer-ancestor file-ancestor
742 startup-hooks quit-hooks output-file)))
744 ;; Start up Emerge on two files with an ancestor
745 (defun emerge-setup-with-ancestor (buffer-A file-A buffer-B file-B
746 buffer-ancestor file-ancestor
747 &optional startup-hooks quit-hooks
748 output-file)
749 (setq file-A (expand-file-name file-A))
750 (setq file-B (expand-file-name file-B))
751 (setq file-ancestor (expand-file-name file-ancestor))
752 (setq output-file (and output-file (expand-file-name output-file)))
753 (let* ((merge-buffer-name (emerge-unique-buffer-name "*merge" "*"))
754 ;; create the merge buffer from buffer A, so it inherits buffer A's
755 ;; default directory, etc.
756 (merge-buffer (with-current-buffer
757 buffer-A
758 (get-buffer-create merge-buffer-name))))
759 (with-current-buffer
760 merge-buffer
761 (emerge-copy-modes buffer-A)
762 (setq buffer-read-only nil)
763 (auto-save-mode 1)
764 (setq emerge-mode t)
765 (setq emerge-A-buffer buffer-A)
766 (setq emerge-B-buffer buffer-B)
767 (setq emerge-ancestor-buffer buffer-ancestor)
768 (setq emerge-merge-buffer merge-buffer)
769 (setq emerge-output-description
770 (if output-file
771 (concat "Output to file: " output-file)
772 (concat "Output to buffer: " (buffer-name merge-buffer))))
773 (save-excursion (insert-buffer-substring emerge-A-buffer))
774 (emerge-set-keys)
775 (setq emerge-difference-list
776 (emerge-make-diff3-list file-A file-B file-ancestor))
777 (setq emerge-number-of-differences (length emerge-difference-list))
778 (setq emerge-current-difference -1)
779 (setq emerge-quit-hook quit-hooks)
780 (emerge-remember-buffer-characteristics)
781 (emerge-select-prefer-Bs)
782 (emerge-handle-local-variables))
783 (emerge-setup-windows buffer-A buffer-B merge-buffer t)
784 (with-current-buffer merge-buffer
785 (run-hooks 'startup-hooks 'emerge-startup-hook)
786 (setq buffer-read-only t))))
788 ;; Generate the Emerge difference list between two files with an ancestor
789 (defun emerge-make-diff3-list (file-A file-B file-ancestor)
790 (setq emerge-diff-buffer (get-buffer-create "*emerge-diff*"))
791 (with-current-buffer
792 emerge-diff-buffer
793 (erase-buffer)
794 (shell-command
795 (format "%s %s %s %s %s"
796 (shell-quote-argument emerge-diff3-program)
797 emerge-diff-options
798 (shell-quote-argument file-A)
799 (shell-quote-argument file-ancestor)
800 (shell-quote-argument file-B))
802 (emerge-prepare-error-list emerge-diff3-ok-lines-regexp)
803 (emerge-convert-diffs-to-markers
804 emerge-A-buffer emerge-B-buffer emerge-merge-buffer
805 (emerge-extract-diffs3 emerge-diff-buffer)))
807 (defun emerge-extract-diffs3 (diff-buffer)
808 (let (list)
809 (with-current-buffer
810 diff-buffer
811 (while (re-search-forward "^====\\(.?\\)$" nil t)
812 ;; leave point after matched line
813 (beginning-of-line 2)
814 (let ((agreement (buffer-substring (match-beginning 1) (match-end 1))))
815 ;; if the A and B files are the same, ignore the difference
816 (if (not (string-equal agreement "2"))
817 (setq list
818 (cons
819 (let (group-1 group-3 pos)
820 (setq pos (point))
821 (setq group-1 (emerge-get-diff3-group "1"))
822 (goto-char pos)
823 (setq group-3 (emerge-get-diff3-group "3"))
824 (vector (car group-1) (car (cdr group-1))
825 (car group-3) (car (cdr group-3))
826 (cond ((string-equal agreement "1") 'prefer-A)
827 ((string-equal agreement "3") 'prefer-B)
828 (t 'default-A))))
829 list))))))
830 (nreverse list)))
832 (defun emerge-get-diff3-group (file)
833 ;; This save-excursion allows emerge-get-diff3-group to be called for the
834 ;; various groups of lines (1, 2, 3) in any order, and for the lines to
835 ;; appear in any order. The reason this is necessary is that Gnu diff3
836 ;; can produce the groups in the order 1, 2, 3 or 1, 3, 2.
837 (save-excursion
838 (re-search-forward
839 (concat "^" file ":\\([0-9]+\\)\\(,\\([0-9]+\\)\\)?\\([ac]\\)$"))
840 (beginning-of-line 2)
841 ;; treatment depends on whether it is an "a" group or a "c" group
842 (if (string-equal (buffer-substring (match-beginning 4) (match-end 4)) "c")
843 ;; it is a "c" group
844 (if (match-beginning 2)
845 ;; it has two numbers
846 (list (string-to-number
847 (buffer-substring (match-beginning 1) (match-end 1)))
848 (1+ (string-to-number
849 (buffer-substring (match-beginning 3) (match-end 3)))))
850 ;; it has one number
851 (let ((x (string-to-number
852 (buffer-substring (match-beginning 1) (match-end 1)))))
853 (list x (1+ x))))
854 ;; it is an "a" group
855 (let ((x (1+ (string-to-number
856 (buffer-substring (match-beginning 1) (match-end 1))))))
857 (list x x)))))
859 ;;; Functions to start Emerge on files
861 ;;;###autoload
862 (defun emerge-files (_arg file-A file-B file-out &optional startup-hooks
863 quit-hooks)
864 "Run Emerge on two files."
865 (interactive
866 (let (f)
867 (list current-prefix-arg
868 (setq f (emerge-read-file-name "File A to merge" emerge-last-dir-A
869 nil nil t))
870 (emerge-read-file-name "File B to merge" emerge-last-dir-B nil f t)
871 (and current-prefix-arg
872 (emerge-read-file-name "Output file" emerge-last-dir-output
873 f f nil)))))
874 (if file-out
875 (add-hook 'quit-hooks `(lambda () (emerge-files-exit ,file-out))))
876 (emerge-files-internal
877 file-A file-B startup-hooks
878 quit-hooks
879 file-out))
881 ;;;###autoload
882 (defun emerge-files-with-ancestor (_arg file-A file-B file-ancestor file-out
883 &optional startup-hooks quit-hooks)
884 "Run Emerge on two files, giving another file as the ancestor."
885 (interactive
886 (let (f)
887 (list current-prefix-arg
888 (setq f (emerge-read-file-name "File A to merge" emerge-last-dir-A
889 nil nil t))
890 (emerge-read-file-name "File B to merge" emerge-last-dir-B nil f t)
891 (emerge-read-file-name "Ancestor file" emerge-last-dir-ancestor
892 nil f t)
893 (and current-prefix-arg
894 (emerge-read-file-name "Output file" emerge-last-dir-output
895 f f nil)))))
896 (if file-out
897 (add-hook 'quit-hooks `(lambda () (emerge-files-exit ,file-out))))
898 (emerge-files-with-ancestor-internal
899 file-A file-B file-ancestor startup-hooks
900 quit-hooks
901 file-out))
903 ;; Write the merge buffer out in place of the file the A buffer is visiting.
904 (defun emerge-files-exit (file-out)
905 ;; if merge was successful was given, save to disk
906 (if (not emerge-prefix-argument)
907 (emerge-write-and-delete file-out)))
909 ;;; Functions to start Emerge on buffers
911 ;;;###autoload
912 (defun emerge-buffers (buffer-A buffer-B &optional startup-hooks quit-hooks)
913 "Run Emerge on two buffers."
914 (interactive "bBuffer A to merge: \nbBuffer B to merge: ")
915 (let ((emerge-file-A (emerge-make-temp-file "A"))
916 (emerge-file-B (emerge-make-temp-file "B")))
917 (with-current-buffer
918 buffer-A
919 (write-region (point-min) (point-max) emerge-file-A nil 'no-message))
920 (with-current-buffer
921 buffer-B
922 (write-region (point-min) (point-max) emerge-file-B nil 'no-message))
923 (emerge-setup (get-buffer buffer-A) emerge-file-A
924 (get-buffer buffer-B) emerge-file-B
925 (cons `(lambda ()
926 (delete-file ,emerge-file-A)
927 (delete-file ,emerge-file-B))
928 startup-hooks)
929 quit-hooks
930 nil)))
932 ;;;###autoload
933 (defun emerge-buffers-with-ancestor (buffer-A buffer-B buffer-ancestor
934 &optional startup-hooks
935 quit-hooks)
936 "Run Emerge on two buffers, giving another buffer as the ancestor."
937 (interactive
938 "bBuffer A to merge: \nbBuffer B to merge: \nbAncestor buffer: ")
939 (let ((emerge-file-A (emerge-make-temp-file "A"))
940 (emerge-file-B (emerge-make-temp-file "B"))
941 (emerge-file-ancestor (emerge-make-temp-file "anc")))
942 (with-current-buffer
943 buffer-A
944 (write-region (point-min) (point-max) emerge-file-A nil 'no-message))
945 (with-current-buffer
946 buffer-B
947 (write-region (point-min) (point-max) emerge-file-B nil 'no-message))
948 (with-current-buffer
949 buffer-ancestor
950 (write-region (point-min) (point-max) emerge-file-ancestor nil
951 'no-message))
952 (emerge-setup-with-ancestor (get-buffer buffer-A) emerge-file-A
953 (get-buffer buffer-B) emerge-file-B
954 (get-buffer buffer-ancestor)
955 emerge-file-ancestor
956 (cons `(lambda ()
957 (delete-file ,emerge-file-A)
958 (delete-file ,emerge-file-B)
959 (delete-file
960 ,emerge-file-ancestor))
961 startup-hooks)
962 quit-hooks
963 nil)))
965 ;;; Functions to start Emerge from the command line
967 ;;;###autoload
968 (defun emerge-files-command ()
969 (let ((file-a (nth 0 command-line-args-left))
970 (file-b (nth 1 command-line-args-left))
971 (file-out (nth 2 command-line-args-left)))
972 (setq command-line-args-left (nthcdr 3 command-line-args-left))
973 (emerge-files-internal
974 file-a file-b nil
975 (list `(lambda () (emerge-command-exit ,file-out))))))
977 ;;;###autoload
978 (defun emerge-files-with-ancestor-command ()
979 (let (file-a file-b file-anc file-out)
980 ;; check for a -a flag, for filemerge compatibility
981 (if (string= (car command-line-args-left) "-a")
982 ;; arguments are "-a ancestor file-a file-b file-out"
983 (progn
984 (setq file-a (nth 2 command-line-args-left))
985 (setq file-b (nth 3 command-line-args-left))
986 (setq file-anc (nth 1 command-line-args-left))
987 (setq file-out (nth 4 command-line-args-left))
988 (setq command-line-args-left (nthcdr 5 command-line-args-left)))
989 ;; arguments are "file-a file-b ancestor file-out"
990 (setq file-a (nth 0 command-line-args-left))
991 (setq file-b (nth 1 command-line-args-left))
992 (setq file-anc (nth 2 command-line-args-left))
993 (setq file-out (nth 3 command-line-args-left))
994 (setq command-line-args-left (nthcdr 4 command-line-args-left)))
995 (emerge-files-with-ancestor-internal
996 file-a file-b file-anc nil
997 (list `(lambda () (emerge-command-exit ,file-out))))))
999 (defun emerge-command-exit (file-out)
1000 (emerge-write-and-delete file-out)
1001 (kill-emacs (if emerge-prefix-argument 1 0)))
1003 ;;; Functions to start Emerge via remote request
1005 ;;;###autoload
1006 (defun emerge-files-remote (file-a file-b file-out)
1007 (setq emerge-file-out file-out)
1008 (emerge-files-internal
1009 file-a file-b nil
1010 (list `(lambda () (emerge-remote-exit ,file-out ',emerge-exit-func)))
1011 file-out)
1012 (throw 'client-wait nil))
1014 ;;;###autoload
1015 (defun emerge-files-with-ancestor-remote (file-a file-b file-anc file-out)
1016 (setq emerge-file-out file-out)
1017 (emerge-files-with-ancestor-internal
1018 file-a file-b file-anc nil
1019 (list `(lambda () (emerge-remote-exit ,file-out ',emerge-exit-func)))
1020 file-out)
1021 (throw 'client-wait nil))
1023 (defun emerge-remote-exit (file-out emerge-exit-func)
1024 (emerge-write-and-delete file-out)
1025 (kill-buffer emerge-merge-buffer)
1026 (funcall emerge-exit-func (if emerge-prefix-argument 1 0)))
1028 ;;; Functions to start Emerge on RCS versions
1030 ;;;###autoload
1031 (defun emerge-revisions (arg file revision-A revision-B
1032 &optional startup-hooks quit-hooks)
1033 "Emerge two RCS revisions of a file."
1034 (interactive
1035 (list current-prefix-arg
1036 (read-file-name "File to merge: " nil nil 'confirm)
1037 (read-string "Revision A to merge: " emerge-last-revision-A)
1038 (read-string "Revision B to merge: " emerge-last-revision-B)))
1039 (setq emerge-last-revision-A revision-A
1040 emerge-last-revision-B revision-B)
1041 (emerge-revisions-internal
1042 file revision-A revision-B startup-hooks
1043 (if arg
1044 (cons `(lambda ()
1045 (shell-command
1046 ,(format "%s %s" emerge-rcs-ci-program file)))
1047 quit-hooks)
1048 quit-hooks)))
1050 ;;;###autoload
1051 (defun emerge-revisions-with-ancestor (arg file revision-A
1052 revision-B ancestor
1053 &optional
1054 startup-hooks quit-hooks)
1055 "Emerge two RCS revisions of a file, with another revision as ancestor."
1056 (interactive
1057 (list current-prefix-arg
1058 (read-file-name "File to merge: " nil nil 'confirm)
1059 (read-string "Revision A to merge: " emerge-last-revision-A)
1060 (read-string "Revision B to merge: " emerge-last-revision-B)
1061 (read-string "Ancestor: " emerge-last-revision-ancestor)))
1062 (setq emerge-last-revision-A revision-A
1063 emerge-last-revision-B revision-B
1064 emerge-last-revision-ancestor ancestor)
1065 (emerge-revision-with-ancestor-internal
1066 file revision-A revision-B ancestor startup-hooks
1067 (if arg
1068 (let ((cmd ))
1069 (cons `(lambda ()
1070 (shell-command
1071 ,(format "%s %s" emerge-rcs-ci-program file)))
1072 quit-hooks))
1073 quit-hooks)))
1075 (defun emerge-revisions-internal (file revision-A revision-B &optional
1076 startup-hooks quit-hooks _output-file)
1077 (let ((buffer-A (get-buffer-create (format "%s,%s" file revision-A)))
1078 (buffer-B (get-buffer-create (format "%s,%s" file revision-B)))
1079 (emerge-file-A (emerge-make-temp-file "A"))
1080 (emerge-file-B (emerge-make-temp-file "B")))
1081 ;; Get the revisions into buffers
1082 (with-current-buffer
1083 buffer-A
1084 (erase-buffer)
1085 (shell-command
1086 (format "%s -q -p%s %s" emerge-rcs-co-program revision-A file)
1088 (write-region (point-min) (point-max) emerge-file-A nil 'no-message)
1089 (set-buffer-modified-p nil))
1090 (with-current-buffer
1091 buffer-B
1092 (erase-buffer)
1093 (shell-command
1094 (format "%s -q -p%s %s" emerge-rcs-co-program revision-B file)
1096 (write-region (point-min) (point-max) emerge-file-B nil 'no-message)
1097 (set-buffer-modified-p nil))
1098 ;; Do the merge
1099 (emerge-setup buffer-A emerge-file-A
1100 buffer-B emerge-file-B
1101 (cons `(lambda ()
1102 (delete-file ,emerge-file-A)
1103 (delete-file ,emerge-file-B))
1104 startup-hooks)
1105 (cons `(lambda () (emerge-files-exit ,file))
1106 quit-hooks)
1107 nil)))
1109 (defun emerge-revision-with-ancestor-internal (file revision-A revision-B
1110 ancestor
1111 &optional startup-hooks
1112 quit-hooks output-file)
1113 (let ((buffer-A (get-buffer-create (format "%s,%s" file revision-A)))
1114 (buffer-B (get-buffer-create (format "%s,%s" file revision-B)))
1115 (buffer-ancestor (get-buffer-create (format "%s,%s" file ancestor)))
1116 (emerge-file-A (emerge-make-temp-file "A"))
1117 (emerge-file-B (emerge-make-temp-file "B"))
1118 (emerge-ancestor (emerge-make-temp-file "ancestor")))
1119 ;; Get the revisions into buffers
1120 (with-current-buffer
1121 buffer-A
1122 (erase-buffer)
1123 (shell-command
1124 (format "%s -q -p%s %s" emerge-rcs-co-program
1125 revision-A file)
1127 (write-region (point-min) (point-max) emerge-file-A nil 'no-message)
1128 (set-buffer-modified-p nil))
1129 (with-current-buffer
1130 buffer-B
1131 (erase-buffer)
1132 (shell-command
1133 (format "%s -q -p%s %s" emerge-rcs-co-program revision-B file)
1135 (write-region (point-min) (point-max) emerge-file-B nil 'no-message)
1136 (set-buffer-modified-p nil))
1137 (with-current-buffer
1138 buffer-ancestor
1139 (erase-buffer)
1140 (shell-command
1141 (format "%s -q -p%s %s" emerge-rcs-co-program ancestor file)
1143 (write-region (point-min) (point-max) emerge-ancestor nil 'no-message)
1144 (set-buffer-modified-p nil))
1145 ;; Do the merge
1146 (emerge-setup-with-ancestor
1147 buffer-A emerge-file-A buffer-B emerge-file-B
1148 buffer-ancestor emerge-ancestor
1149 (cons `(lambda ()
1150 (delete-file ,emerge-file-A)
1151 (delete-file ,emerge-file-B)
1152 (delete-file ,emerge-ancestor))
1153 startup-hooks)
1154 (cons `(lambda () (emerge-files-exit ,file))
1155 quit-hooks)
1156 output-file)))
1158 ;;; Function to start Emerge based on a line in a file
1160 (defun emerge-execute-line ()
1161 "Run Emerge using files named in current text line.
1162 Looks in that line for whitespace-separated entries of these forms:
1163 a=file1
1164 b=file2
1165 ancestor=file3
1166 output=file4
1167 to specify the files to use in Emerge.
1169 In addition, if only one of `a=file' or `b=file' is present, and `output=file'
1170 is present:
1171 If `emerge-execute-line-deletions' is non-nil and `ancestor=file' is present,
1172 it is assumed that the file in question has been deleted, and it is
1173 not copied to the output file.
1174 Otherwise, the A or B file present is copied to the output file."
1175 (interactive)
1176 (let (file-A file-B file-ancestor file-out
1177 (case-fold-search t))
1178 ;; Stop if at end of buffer (even though we might be in a line, if
1179 ;; the line does not end with newline)
1180 (if (eobp)
1181 (error "At end of buffer"))
1182 ;; Go to the beginning of the line
1183 (beginning-of-line)
1184 ;; Skip any initial whitespace
1185 (if (looking-at "[ \t]*")
1186 (goto-char (match-end 0)))
1187 ;; Process the entire line
1188 (while (not (eolp))
1189 ;; Get the next entry
1190 (if (looking-at "\\([a-z]+\\)=\\([^ \t\n]+\\)[ \t]*")
1191 ;; Break apart the tab (before =) and the filename (after =)
1192 (let ((tag (downcase
1193 (buffer-substring (match-beginning 1) (match-end 1))))
1194 (file (buffer-substring (match-beginning 2) (match-end 2))))
1195 ;; Move point after the entry
1196 (goto-char (match-end 0))
1197 ;; Store the filename in the right variable
1198 (cond
1199 ((string-equal tag "a")
1200 (if file-A
1201 (error "This line has two `A' entries"))
1202 (setq file-A file))
1203 ((string-equal tag "b")
1204 (if file-B
1205 (error "This line has two `B' entries"))
1206 (setq file-B file))
1207 ((or (string-equal tag "anc") (string-equal tag "ancestor"))
1208 (if file-ancestor
1209 (error "This line has two `ancestor' entries"))
1210 (setq file-ancestor file))
1211 ((or (string-equal tag "out") (string-equal tag "output"))
1212 (if file-out
1213 (error "This line has two `output' entries"))
1214 (setq file-out file))
1216 (error "Unrecognized entry"))))
1217 ;; If the match on the entry pattern failed
1218 (error "Unparsable entry")))
1219 ;; Make sure that file-A and file-B are present
1220 (if (not (or (and file-A file-B) file-out))
1221 (error "Must have both `A' and `B' entries"))
1222 (if (not (or file-A file-B))
1223 (error "Must have `A' or `B' entry"))
1224 ;; Go to the beginning of the next line, so next execution will use
1225 ;; next line in buffer.
1226 (beginning-of-line 2)
1227 ;; Execute the correct command
1228 (cond
1229 ;; Merge of two files with ancestor
1230 ((and file-A file-B file-ancestor)
1231 (message "Merging %s and %s..." file-A file-B)
1232 (emerge-files-with-ancestor (not (not file-out)) file-A file-B
1233 file-ancestor file-out
1235 ;; When done, return to this buffer.
1236 (list
1237 `(lambda ()
1238 (switch-to-buffer ,(current-buffer))
1239 (message "Merge done.")))))
1240 ;; Merge of two files without ancestor
1241 ((and file-A file-B)
1242 (message "Merging %s and %s..." file-A file-B)
1243 (emerge-files (not (not file-out)) file-A file-B file-out
1245 ;; When done, return to this buffer.
1246 (list
1247 `(lambda ()
1248 (switch-to-buffer ,(current-buffer))
1249 (message "Merge done.")))))
1250 ;; There is an output file (or there would have been an error above),
1251 ;; but only one input file.
1252 ;; The file appears to have been deleted in one version; do nothing.
1253 ((and file-ancestor emerge-execute-line-deletions)
1254 (message "No action."))
1255 ;; The file should be copied from the version that contains it
1256 (t (let ((input-file (or file-A file-B)))
1257 (message "Copying...")
1258 (copy-file input-file file-out)
1259 (message "%s copied to %s." input-file file-out))))))
1261 ;;; Sample function for creating information for emerge-execute-line
1263 (defcustom emerge-merge-directories-filename-regexp "[^.]"
1264 "Regexp describing files to be processed by `emerge-merge-directories'."
1265 :type 'regexp
1266 :group 'emerge)
1268 ;;;###autoload
1269 (defun emerge-merge-directories (a-dir b-dir ancestor-dir output-dir)
1270 (interactive
1271 (list
1272 (read-directory-name "A directory: " nil nil 'confirm)
1273 (read-directory-name "B directory: " nil nil 'confirm)
1274 (read-directory-name "Ancestor directory (null for none): " nil nil 'confirm)
1275 (read-directory-name "Output directory (null for none): " nil nil 'confirm)))
1276 ;; Check that we're not on a line
1277 (if (not (and (bolp) (eolp)))
1278 (error "There is text on this line"))
1279 ;; Turn null strings into nil to indicate directories not used.
1280 (if (and ancestor-dir (string-equal ancestor-dir ""))
1281 (setq ancestor-dir nil))
1282 (if (and output-dir (string-equal output-dir ""))
1283 (setq output-dir nil))
1284 ;; Canonicalize the directory names
1285 (setq a-dir (expand-file-name a-dir))
1286 (if (not (string-equal (substring a-dir -1) "/"))
1287 (setq a-dir (concat a-dir "/")))
1288 (setq b-dir (expand-file-name b-dir))
1289 (if (not (string-equal (substring b-dir -1) "/"))
1290 (setq b-dir (concat b-dir "/")))
1291 (if ancestor-dir
1292 (progn
1293 (setq ancestor-dir (expand-file-name ancestor-dir))
1294 (if (not (string-equal (substring ancestor-dir -1) "/"))
1295 (setq ancestor-dir (concat ancestor-dir "/")))))
1296 (if output-dir
1297 (progn
1298 (setq output-dir (expand-file-name output-dir))
1299 (if (not (string-equal (substring output-dir -1) "/"))
1300 (setq output-dir (concat output-dir "/")))))
1301 ;; Set the mark to where we start
1302 (push-mark)
1303 ;; Find out what files are in the directories.
1304 (let* ((a-dir-files
1305 (directory-files a-dir nil emerge-merge-directories-filename-regexp))
1306 (b-dir-files
1307 (directory-files b-dir nil emerge-merge-directories-filename-regexp))
1308 (ancestor-dir-files
1309 (and ancestor-dir
1310 (directory-files ancestor-dir nil
1311 emerge-merge-directories-filename-regexp)))
1312 (all-files (sort (nconc (copy-sequence a-dir-files)
1313 (copy-sequence b-dir-files)
1314 (copy-sequence ancestor-dir-files))
1315 (function string-lessp))))
1316 ;; Remove duplicates from all-files.
1317 (let ((p all-files))
1318 (while p
1319 (if (and (cdr p) (string-equal (car p) (car (cdr p))))
1320 (setcdr p (cdr (cdr p)))
1321 (setq p (cdr p)))))
1322 ;; Generate the control lines for the various files.
1323 (while all-files
1324 (let ((f (car all-files)))
1325 (setq all-files (cdr all-files))
1326 (if (and a-dir-files (string-equal (car a-dir-files) f))
1327 (progn
1328 (insert "A=" a-dir f "\t")
1329 (setq a-dir-files (cdr a-dir-files))))
1330 (if (and b-dir-files (string-equal (car b-dir-files) f))
1331 (progn
1332 (insert "B=" b-dir f "\t")
1333 (setq b-dir-files (cdr b-dir-files))))
1334 (if (and ancestor-dir-files (string-equal (car ancestor-dir-files) f))
1335 (progn
1336 (insert "ancestor=" ancestor-dir f "\t")
1337 (setq ancestor-dir-files (cdr ancestor-dir-files))))
1338 (if output-dir
1339 (insert "output=" output-dir f "\t"))
1340 (backward-delete-char 1)
1341 (insert "\n")))))
1343 ;;; Common setup routines
1345 ;; Set up the window configuration. If POS is given, set the points to
1346 ;; the beginnings of the buffers.
1347 (defun emerge-setup-windows (buffer-A buffer-B merge-buffer &optional pos)
1348 ;; Make sure we are not in the minibuffer window when we try to delete
1349 ;; all other windows.
1350 (if (eq (selected-window) (minibuffer-window))
1351 (other-window 1))
1352 (delete-other-windows)
1353 (switch-to-buffer merge-buffer)
1354 (emerge-refresh-mode-line)
1355 (split-window-below)
1356 (split-window-right)
1357 (switch-to-buffer buffer-A)
1358 (if pos
1359 (goto-char (point-min)))
1360 (other-window 1)
1361 (switch-to-buffer buffer-B)
1362 (if pos
1363 (goto-char (point-min)))
1364 (other-window 1)
1365 (if pos
1366 (goto-char (point-min)))
1367 ;; If diff/diff3 reports errors, display them rather than the merge buffer.
1368 (if (/= 0 (with-current-buffer emerge-diff-error-buffer (buffer-size)))
1369 (progn
1370 (ding)
1371 (message "Errors found in diff/diff3 output. Merge buffer is %s."
1372 (buffer-name emerge-merge-buffer))
1373 (switch-to-buffer emerge-diff-error-buffer))))
1375 ;; Set up the keymap in the merge buffer
1376 (defun emerge-set-keys ()
1377 ;; Set up fixed keymaps if necessary
1378 (if (not emerge-basic-keymap)
1379 (emerge-setup-fixed-keymaps))
1380 ;; Save the old local map
1381 (setq emerge-old-keymap (current-local-map))
1382 ;; Construct the edit keymap
1383 (setq emerge-edit-keymap (if emerge-old-keymap
1384 (copy-keymap emerge-old-keymap)
1385 (make-sparse-keymap)))
1386 ;; Install the Emerge commands
1387 (emerge-force-define-key emerge-edit-keymap emerge-command-prefix
1388 'emerge-basic-keymap)
1389 (define-key emerge-edit-keymap [menu-bar] (make-sparse-keymap))
1391 ;; Create the additional menu bar items.
1392 (define-key emerge-edit-keymap [menu-bar emerge-options]
1393 (cons "Merge-Options" emerge-options-menu))
1394 (define-key emerge-edit-keymap [menu-bar merge]
1395 (cons "Merge" emerge-merge-menu))
1396 (define-key emerge-edit-keymap [menu-bar move]
1397 (cons "Move" emerge-move-menu))
1399 ;; Suppress write-file and save-buffer
1400 (substitute-key-definition 'write-file
1401 'emerge-query-write-file
1402 emerge-edit-keymap)
1403 (substitute-key-definition 'save-buffer
1404 'emerge-query-save-buffer
1405 emerge-edit-keymap)
1406 (define-key emerge-edit-keymap [remap write-file] 'emerge-query-write-file)
1407 (define-key emerge-edit-keymap [remap save-buffer] 'emerge-query-save-buffer)
1408 (use-local-map emerge-fast-keymap)
1409 (setq emerge-edit-mode nil)
1410 (setq emerge-fast-mode t))
1412 (defun emerge-remember-buffer-characteristics ()
1413 "Record certain properties of the buffers being merged.
1414 Must be called in the merge buffer. Remembers read-only, modified,
1415 auto-save, and saves them in buffer local variables. Sets the buffers
1416 read-only and turns off `auto-save-mode'.
1417 These characteristics are restored by `emerge-restore-buffer-characteristics'."
1418 ;; force auto-save, because we will turn off auto-saving in buffers for the
1419 ;; duration
1420 (do-auto-save)
1421 ;; remember and alter buffer characteristics
1422 (setq emerge-A-buffer-values
1423 (with-current-buffer
1424 emerge-A-buffer
1425 (prog1
1426 (emerge-save-variables emerge-saved-variables)
1427 (emerge-restore-variables emerge-saved-variables
1428 emerge-merging-values))))
1429 (setq emerge-B-buffer-values
1430 (with-current-buffer
1431 emerge-B-buffer
1432 (prog1
1433 (emerge-save-variables emerge-saved-variables)
1434 (emerge-restore-variables emerge-saved-variables
1435 emerge-merging-values)))))
1437 (defun emerge-restore-buffer-characteristics ()
1438 "Restore characteristics saved by `emerge-remember-buffer-characteristics'."
1439 (let ((A-values emerge-A-buffer-values)
1440 (B-values emerge-B-buffer-values))
1441 (with-current-buffer emerge-A-buffer
1442 (emerge-restore-variables emerge-saved-variables
1443 A-values))
1444 (with-current-buffer emerge-B-buffer
1445 (emerge-restore-variables emerge-saved-variables
1446 B-values))))
1448 ;; Move to line DESIRED-LINE assuming we are at line CURRENT-LINE.
1449 ;; Return DESIRED-LINE.
1450 (defun emerge-goto-line (desired-line current-line)
1451 (forward-line (- desired-line current-line))
1452 desired-line)
1454 (defun emerge-convert-diffs-to-markers (A-buffer
1455 B-buffer
1456 merge-buffer
1457 lineno-list)
1458 (let* (marker-list
1459 (A-point-min (with-current-buffer A-buffer (point-min)))
1460 (offset (1- A-point-min))
1461 (B-point-min (with-current-buffer B-buffer (point-min)))
1462 ;; Record current line number in each buffer
1463 ;; so we don't have to count from the beginning.
1464 (a-line 1)
1465 (b-line 1))
1466 (with-current-buffer A-buffer (goto-char (point-min)))
1467 (with-current-buffer B-buffer (goto-char (point-min)))
1468 (while lineno-list
1469 (let* ((list-element (car lineno-list))
1470 a-begin-marker
1471 a-end-marker
1472 b-begin-marker
1473 b-end-marker
1474 merge-begin-marker
1475 merge-end-marker
1476 (a-begin (aref list-element 0))
1477 (a-end (aref list-element 1))
1478 (b-begin (aref list-element 2))
1479 (b-end (aref list-element 3))
1480 (state (aref list-element 4)))
1481 ;; place markers at the appropriate places in the buffers
1482 (with-current-buffer
1483 A-buffer
1484 (setq a-line (emerge-goto-line a-begin a-line))
1485 (setq a-begin-marker (point-marker))
1486 (setq a-line (emerge-goto-line a-end a-line))
1487 (setq a-end-marker (point-marker)))
1488 (with-current-buffer
1489 B-buffer
1490 (setq b-line (emerge-goto-line b-begin b-line))
1491 (setq b-begin-marker (point-marker))
1492 (setq b-line (emerge-goto-line b-end b-line))
1493 (setq b-end-marker (point-marker)))
1494 (setq merge-begin-marker (set-marker
1495 (make-marker)
1496 (- (marker-position a-begin-marker)
1497 offset)
1498 merge-buffer))
1499 (setq merge-end-marker (set-marker
1500 (make-marker)
1501 (- (marker-position a-end-marker)
1502 offset)
1503 merge-buffer))
1504 ;; record all the markers for this difference
1505 (setq marker-list (cons (vector a-begin-marker a-end-marker
1506 b-begin-marker b-end-marker
1507 merge-begin-marker merge-end-marker
1508 state)
1509 marker-list)))
1510 (setq lineno-list (cdr lineno-list)))
1511 ;; convert the list of difference information into a vector for
1512 ;; fast access
1513 (setq emerge-difference-list (apply 'vector (nreverse marker-list)))))
1515 ;; If we have an ancestor, select all B variants that we prefer
1516 (defun emerge-select-prefer-Bs ()
1517 (let ((n 0))
1518 (while (< n emerge-number-of-differences)
1519 (if (eq (aref (aref emerge-difference-list n) 6) 'prefer-B)
1520 (progn
1521 (emerge-unselect-and-select-difference n t)
1522 (emerge-select-B)
1523 (aset (aref emerge-difference-list n) 6 'prefer-B)))
1524 (setq n (1+ n))))
1525 (emerge-unselect-and-select-difference -1))
1527 ;; Process the local-variables list at the end of the merged file, if
1528 ;; requested.
1529 (defun emerge-handle-local-variables ()
1530 (if emerge-process-local-variables
1531 (condition-case err
1532 (hack-local-variables)
1533 (error (message "Local-variables error in merge buffer: %s"
1534 (prin1-to-string err))))))
1536 ;;; Common exit routines
1538 (defun emerge-write-and-delete (file-out)
1539 ;; clear screen format
1540 (delete-other-windows)
1541 ;; delete A, B, and ancestor buffers, if they haven't been changed
1542 (if (not (buffer-modified-p emerge-A-buffer))
1543 (kill-buffer emerge-A-buffer))
1544 (if (not (buffer-modified-p emerge-B-buffer))
1545 (kill-buffer emerge-B-buffer))
1546 (if (and emerge-ancestor-buffer
1547 (not (buffer-modified-p emerge-ancestor-buffer)))
1548 (kill-buffer emerge-ancestor-buffer))
1549 ;; Write merge buffer to file
1550 (and file-out
1551 (write-file file-out)))
1553 ;;; Commands
1555 (defun emerge-recenter (&optional arg)
1556 "Bring the highlighted region of all three merge buffers into view.
1557 This brings the buffers into view if they are in windows.
1558 With an argument, reestablish the default three-window display."
1559 (interactive "P")
1560 ;; If there is an argument, rebuild the window structure
1561 (if arg
1562 (emerge-setup-windows emerge-A-buffer emerge-B-buffer
1563 emerge-merge-buffer))
1564 ;; Redisplay whatever buffers are showing, if there is a selected difference
1565 (if (and (>= emerge-current-difference 0)
1566 (< emerge-current-difference emerge-number-of-differences))
1567 (let* ((merge-buffer emerge-merge-buffer)
1568 (buffer-A emerge-A-buffer)
1569 (buffer-B emerge-B-buffer)
1570 (window-A (get-buffer-window buffer-A 'visible))
1571 (window-B (get-buffer-window buffer-B 'visible))
1572 (merge-window (get-buffer-window merge-buffer))
1573 (diff-vector
1574 (aref emerge-difference-list emerge-current-difference)))
1575 (if window-A (progn
1576 (select-window window-A)
1577 (emerge-position-region
1578 (- (aref diff-vector 0)
1579 (1- emerge-before-flag-length))
1580 (+ (aref diff-vector 1)
1581 (1- emerge-after-flag-length))
1582 (1+ (aref diff-vector 0)))))
1583 (if window-B (progn
1584 (select-window window-B)
1585 (emerge-position-region
1586 (- (aref diff-vector 2)
1587 (1- emerge-before-flag-length))
1588 (+ (aref diff-vector 3)
1589 (1- emerge-after-flag-length))
1590 (1+ (aref diff-vector 2)))))
1591 (if merge-window (progn
1592 (select-window merge-window)
1593 (emerge-position-region
1594 (- (aref diff-vector 4)
1595 (1- emerge-before-flag-length))
1596 (+ (aref diff-vector 5)
1597 (1- emerge-after-flag-length))
1598 (1+ (aref diff-vector 4))))))))
1600 ;;; Window scrolling operations
1601 ;; These operations are designed to scroll all three windows the same amount,
1602 ;; so as to keep the text in them aligned.
1604 ;; Perform some operation on all three windows (if they are showing).
1605 ;; Catches all errors on the operation in the A and B windows, but not
1606 ;; in the merge window. Usually, errors come from scrolling off the
1607 ;; beginning or end of the buffer, and this gives a nice error message:
1608 ;; End of buffer is reported in the merge buffer, but if the scroll was
1609 ;; possible in the A or B windows, it is performed there before the error
1610 ;; is reported.
1611 (defun emerge-operate-on-windows (operation arg)
1612 (let* ((merge-buffer emerge-merge-buffer)
1613 (buffer-A emerge-A-buffer)
1614 (buffer-B emerge-B-buffer)
1615 (window-A (get-buffer-window buffer-A 'visible))
1616 (window-B (get-buffer-window buffer-B 'visible))
1617 (merge-window (get-buffer-window merge-buffer)))
1618 (if window-A (progn
1619 (select-window window-A)
1620 (condition-case nil
1621 (funcall operation arg)
1622 (error))))
1623 (if window-B (progn
1624 (select-window window-B)
1625 (condition-case nil
1626 (funcall operation arg)
1627 (error))))
1628 (if merge-window (progn
1629 (select-window merge-window)
1630 (funcall operation arg)))))
1632 (defun emerge-scroll-up (&optional arg)
1633 "Scroll up all three merge buffers, if they are in windows.
1634 With argument N, scroll N lines; otherwise scroll by nearly
1635 the height of the merge window.
1636 `C-u -' alone as argument scrolls half the height of the merge window."
1637 (interactive "P")
1638 (emerge-operate-on-windows
1639 'scroll-up
1640 ;; calculate argument to scroll-up
1641 ;; if there is an explicit argument
1642 (if (and arg (not (equal arg '-)))
1643 ;; use it
1644 (prefix-numeric-value arg)
1645 ;; if not, see if we can determine a default amount (the window height)
1646 (let ((merge-window (get-buffer-window emerge-merge-buffer)))
1647 (if (null merge-window)
1648 ;; no window, use nil
1650 (let ((default-amount
1651 (- (window-height merge-window) 1 next-screen-context-lines)))
1652 ;; the window was found
1653 (if arg
1654 ;; C-u as argument means half of default amount
1655 (/ default-amount 2)
1656 ;; no argument means default amount
1657 default-amount)))))))
1659 (defun emerge-scroll-down (&optional arg)
1660 "Scroll down all three merge buffers, if they are in windows.
1661 With argument N, scroll N lines; otherwise scroll by nearly
1662 the height of the merge window.
1663 `C-u -' alone as argument scrolls half the height of the merge window."
1664 (interactive "P")
1665 (emerge-operate-on-windows
1666 'scroll-down
1667 ;; calculate argument to scroll-down
1668 ;; if there is an explicit argument
1669 (if (and arg (not (equal arg '-)))
1670 ;; use it
1671 (prefix-numeric-value arg)
1672 ;; if not, see if we can determine a default amount (the window height)
1673 (let ((merge-window (get-buffer-window emerge-merge-buffer)))
1674 (if (null merge-window)
1675 ;; no window, use nil
1677 (let ((default-amount
1678 (- (window-height merge-window) 1 next-screen-context-lines)))
1679 ;; the window was found
1680 (if arg
1681 ;; C-u as argument means half of default amount
1682 (/ default-amount 2)
1683 ;; no argument means default amount
1684 default-amount)))))))
1686 (defun emerge-scroll-left (&optional arg)
1687 "Scroll left all three merge buffers, if they are in windows.
1688 If an argument is given, that is how many columns are scrolled, else nearly
1689 the width of the A and B windows. `C-u -' alone as argument scrolls half the
1690 width of the A and B windows."
1691 (interactive "P")
1692 (emerge-operate-on-windows
1693 'scroll-left
1694 ;; calculate argument to scroll-left
1695 ;; if there is an explicit argument
1696 (if (and arg (not (equal arg '-)))
1697 ;; use it
1698 (prefix-numeric-value arg)
1699 ;; if not, see if we can determine a default amount
1700 ;; (half the window width)
1701 (let ((merge-window (get-buffer-window emerge-merge-buffer)))
1702 (if (null merge-window)
1703 ;; no window, use nil
1705 (let ((default-amount
1706 (- (/ (window-width merge-window) 2) 3)))
1707 ;; the window was found
1708 (if arg
1709 ;; C-u as argument means half of default amount
1710 (/ default-amount 2)
1711 ;; no argument means default amount
1712 default-amount)))))))
1714 (defun emerge-scroll-right (&optional arg)
1715 "Scroll right all three merge buffers, if they are in windows.
1716 If an argument is given, that is how many columns are scrolled, else nearly
1717 the width of the A and B windows. `C-u -' alone as argument scrolls half the
1718 width of the A and B windows."
1719 (interactive "P")
1720 (emerge-operate-on-windows
1721 'scroll-right
1722 ;; calculate argument to scroll-right
1723 ;; if there is an explicit argument
1724 (if (and arg (not (equal arg '-)))
1725 ;; use it
1726 (prefix-numeric-value arg)
1727 ;; if not, see if we can determine a default amount
1728 ;; (half the window width)
1729 (let ((merge-window (get-buffer-window emerge-merge-buffer)))
1730 (if (null merge-window)
1731 ;; no window, use nil
1733 (let ((default-amount
1734 (- (/ (window-width merge-window) 2) 3)))
1735 ;; the window was found
1736 (if arg
1737 ;; C-u as argument means half of default amount
1738 (/ default-amount 2)
1739 ;; no argument means default amount
1740 default-amount)))))))
1742 (defun emerge-scroll-reset ()
1743 "Reset horizontal scrolling in Emerge.
1744 This resets the horizontal scrolling of all three merge buffers
1745 to the left margin, if they are in windows."
1746 (interactive)
1747 (emerge-operate-on-windows
1748 (lambda (x) (set-window-hscroll (selected-window) 0))
1749 nil))
1751 ;; Attempt to show the region nicely.
1752 ;; If there are min-lines lines above and below the region, then don't do
1753 ;; anything.
1754 ;; If not, recenter the region to make it so.
1755 ;; If that isn't possible, remove context lines evenly from top and bottom
1756 ;; so the entire region shows.
1757 ;; If that isn't possible, show the top of the region.
1758 ;; BEG must be at the beginning of a line.
1759 (defun emerge-position-region (beg end pos)
1760 ;; First test whether the entire region is visible with
1761 ;; emerge-min-visible-lines above and below it
1762 (if (not (and (<= (progn
1763 (move-to-window-line emerge-min-visible-lines)
1764 (point))
1765 beg)
1766 (<= end (progn
1767 (move-to-window-line
1768 (- (1+ emerge-min-visible-lines)))
1769 (point)))))
1770 ;; We failed that test, see if it fits at all
1771 ;; Meanwhile positioning it correctly in case it doesn't fit
1772 (progn
1773 (set-window-start (selected-window) beg)
1774 (if (pos-visible-in-window-p end)
1775 ;; Determine the number of lines that the region occupies
1776 (let ((lines 0))
1777 (while (> end (progn
1778 (move-to-window-line lines)
1779 (point)))
1780 (setq lines (1+ lines)))
1781 ;; And position the beginning on the right line
1782 (goto-char beg)
1783 (recenter (/ (1+ (- (1- (window-height))
1784 lines))
1785 2))))))
1786 (goto-char pos))
1788 (defun emerge-next-difference ()
1789 "Advance to the next difference."
1790 (interactive)
1791 (if (< emerge-current-difference emerge-number-of-differences)
1792 (let ((n (1+ emerge-current-difference)))
1793 (while (and emerge-skip-prefers
1794 (< n emerge-number-of-differences)
1795 (memq (aref (aref emerge-difference-list n) 6)
1796 '(prefer-A prefer-B)))
1797 (setq n (1+ n)))
1798 (let ((buffer-read-only nil))
1799 (emerge-unselect-and-select-difference n)))
1800 (error "At end")))
1802 (defun emerge-previous-difference ()
1803 "Go to the previous difference."
1804 (interactive)
1805 (if (> emerge-current-difference -1)
1806 (let ((n (1- emerge-current-difference)))
1807 (while (and emerge-skip-prefers
1808 (> n -1)
1809 (memq (aref (aref emerge-difference-list n) 6)
1810 '(prefer-A prefer-B)))
1811 (setq n (1- n)))
1812 (let ((buffer-read-only nil))
1813 (emerge-unselect-and-select-difference n)))
1814 (error "At beginning")))
1816 (defun emerge-jump-to-difference (difference-number)
1817 "Go to the N-th difference."
1818 (interactive "p")
1819 (let ((buffer-read-only nil))
1820 (setq difference-number (1- difference-number))
1821 (if (and (>= difference-number -1)
1822 (< difference-number (1+ emerge-number-of-differences)))
1823 (emerge-unselect-and-select-difference difference-number)
1824 (error "Bad difference number"))))
1826 (defun emerge-abort ()
1827 "Abort the Emerge session."
1828 (interactive)
1829 (emerge-quit t))
1831 (defun emerge-quit (arg)
1832 "Finish the Emerge session and exit Emerge.
1833 Prefix argument means to abort rather than successfully finish.
1834 The difference depends on how the merge was started,
1835 but usually means to not write over one of the original files, or to signal
1836 to some process which invoked Emerge a failure code.
1838 Unselects the selected difference, if any, restores the read-only and modified
1839 flags of the merged file buffers, restores the local keymap of the merge
1840 buffer, and sets off various emerge flags. Using Emerge commands in this
1841 buffer after this will cause serious problems."
1842 (interactive "P")
1843 (if (prog1
1844 (y-or-n-p
1845 (if (not arg)
1846 "Do you really want to successfully finish this merge? "
1847 "Do you really want to abort this merge? "))
1848 (message ""))
1849 (emerge-really-quit arg)))
1851 ;; Perform the quit operations.
1852 (defun emerge-really-quit (arg)
1853 (setq buffer-read-only nil)
1854 (emerge-unselect-and-select-difference -1)
1855 (emerge-restore-buffer-characteristics)
1856 ;; null out the difference markers so they don't slow down future editing
1857 ;; operations
1858 (mapc (lambda (d)
1859 (set-marker (aref d 0) nil)
1860 (set-marker (aref d 1) nil)
1861 (set-marker (aref d 2) nil)
1862 (set-marker (aref d 3) nil)
1863 (set-marker (aref d 4) nil)
1864 (set-marker (aref d 5) nil))
1865 emerge-difference-list)
1866 ;; allow them to be garbage collected
1867 (setq emerge-difference-list nil)
1868 ;; restore the local map
1869 (use-local-map emerge-old-keymap)
1870 ;; turn off all the emerge modes
1871 (setq emerge-mode nil)
1872 (setq emerge-fast-mode nil)
1873 (setq emerge-edit-mode nil)
1874 (setq emerge-auto-advance nil)
1875 (setq emerge-skip-prefers nil)
1876 ;; restore mode line
1877 (kill-local-variable 'mode-line-buffer-identification)
1878 (let ((emerge-prefix-argument arg))
1879 (run-hooks 'emerge-quit-hook)))
1881 (defun emerge-select-A (&optional force)
1882 "Select the A variant of this difference.
1883 Refuses to function if this difference has been edited, i.e., if it
1884 is neither the A nor the B variant.
1885 A prefix argument forces the variant to be selected
1886 even if the difference has been edited."
1887 (interactive "P")
1888 (let ((operate
1889 (lambda ()
1890 (emerge-select-A-edit merge-begin merge-end A-begin A-end)
1891 (if emerge-auto-advance
1892 (emerge-next-difference))))
1893 (operate-no-change
1894 (lambda () (if emerge-auto-advance
1895 (emerge-next-difference)))))
1896 (emerge-select-version force operate-no-change operate operate)))
1898 ;; Actually select the A variant
1899 (defun emerge-select-A-edit (merge-begin merge-end A-begin A-end)
1900 (with-current-buffer
1901 emerge-merge-buffer
1902 (delete-region merge-begin merge-end)
1903 (goto-char merge-begin)
1904 (insert-buffer-substring emerge-A-buffer A-begin A-end)
1905 (goto-char merge-begin)
1906 (aset diff-vector 6 'A)
1907 (emerge-refresh-mode-line)))
1909 (defun emerge-select-B (&optional force)
1910 "Select the B variant of this difference.
1911 Refuses to function if this difference has been edited, i.e., if it
1912 is neither the A nor the B variant.
1913 A prefix argument forces the variant to be selected
1914 even if the difference has been edited."
1915 (interactive "P")
1916 (let ((operate
1917 (lambda ()
1918 (emerge-select-B-edit merge-begin merge-end B-begin B-end)
1919 (if emerge-auto-advance
1920 (emerge-next-difference))))
1921 (operate-no-change
1922 (lambda () (if emerge-auto-advance
1923 (emerge-next-difference)))))
1924 (emerge-select-version force operate operate-no-change operate)))
1926 ;; Actually select the B variant
1927 (defun emerge-select-B-edit (merge-begin merge-end B-begin B-end)
1928 (with-current-buffer
1929 emerge-merge-buffer
1930 (delete-region merge-begin merge-end)
1931 (goto-char merge-begin)
1932 (insert-buffer-substring emerge-B-buffer B-begin B-end)
1933 (goto-char merge-begin)
1934 (aset diff-vector 6 'B)
1935 (emerge-refresh-mode-line)))
1937 (defun emerge-default-A ()
1938 "Make the A variant the default from here down.
1939 This selects the A variant for all differences from here down in the buffer
1940 which are still defaulted, i.e., which the user has not selected and for
1941 which there is no preference."
1942 (interactive)
1943 (let ((buffer-read-only nil))
1944 (let ((selected-difference emerge-current-difference)
1945 (n (max emerge-current-difference 0)))
1946 (while (< n emerge-number-of-differences)
1947 (let ((diff-vector (aref emerge-difference-list n)))
1948 (if (eq (aref diff-vector 6) 'default-B)
1949 (progn
1950 (emerge-unselect-and-select-difference n t)
1951 (emerge-select-A)
1952 (aset diff-vector 6 'default-A))))
1953 (setq n (1+ n))
1954 (if (zerop (% n 10))
1955 (message "Setting default to A...%d" n)))
1956 (emerge-unselect-and-select-difference selected-difference)))
1957 (message "Default choice is now A"))
1959 (defun emerge-default-B ()
1960 "Make the B variant the default from here down.
1961 This selects the B variant for all differences from here down in the buffer
1962 which are still defaulted, i.e., which the user has not selected and for
1963 which there is no preference."
1964 (interactive)
1965 (let ((buffer-read-only nil))
1966 (let ((selected-difference emerge-current-difference)
1967 (n (max emerge-current-difference 0)))
1968 (while (< n emerge-number-of-differences)
1969 (let ((diff-vector (aref emerge-difference-list n)))
1970 (if (eq (aref diff-vector 6) 'default-A)
1971 (progn
1972 (emerge-unselect-and-select-difference n t)
1973 (emerge-select-B)
1974 (aset diff-vector 6 'default-B))))
1975 (setq n (1+ n))
1976 (if (zerop (% n 10))
1977 (message "Setting default to B...%d" n)))
1978 (emerge-unselect-and-select-difference selected-difference)))
1979 (message "Default choice is now B"))
1981 (defun emerge-fast-mode ()
1982 "Set fast mode, for Emerge.
1983 In this mode ordinary Emacs commands are disabled, and Emerge commands
1984 need not be prefixed with \\<emerge-fast-keymap>\\[emerge-basic-keymap]."
1985 (interactive)
1986 (setq buffer-read-only t)
1987 (use-local-map emerge-fast-keymap)
1988 (setq emerge-mode t)
1989 (setq emerge-fast-mode t)
1990 (setq emerge-edit-mode nil)
1991 (message "Fast mode set")
1992 (force-mode-line-update))
1994 (defun emerge-edit-mode ()
1995 "Set edit mode, for Emerge.
1996 In this mode ordinary Emacs commands are available, and Emerge commands
1997 must be prefixed with \\<emerge-fast-keymap>\\[emerge-basic-keymap]."
1998 (interactive)
1999 (setq buffer-read-only nil)
2000 (use-local-map emerge-edit-keymap)
2001 (setq emerge-mode t)
2002 (setq emerge-fast-mode nil)
2003 (setq emerge-edit-mode t)
2004 (message "Edit mode set")
2005 (force-mode-line-update))
2007 (defun emerge-auto-advance (arg)
2008 "Toggle Auto-Advance mode, for Emerge.
2009 This mode causes `emerge-select-A' and `emerge-select-B' to automatically
2010 advance to the next difference.
2011 With a positive argument, turn on Auto-Advance mode.
2012 With a negative argument, turn off Auto-Advance mode."
2013 (interactive "P")
2014 (setq emerge-auto-advance (if (null arg)
2015 (not emerge-auto-advance)
2016 (> (prefix-numeric-value arg) 0)))
2017 (message (if emerge-auto-advance
2018 "Auto-advance set"
2019 "Auto-advance cleared"))
2020 (force-mode-line-update))
2022 (defun emerge-skip-prefers (arg)
2023 "Toggle Skip-Prefers mode, for Emerge.
2024 This mode causes `emerge-next-difference' and `emerge-previous-difference'
2025 to automatically skip over differences for which there is a preference.
2026 With a positive argument, turn on Skip-Prefers mode.
2027 With a negative argument, turn off Skip-Prefers mode."
2028 (interactive "P")
2029 (setq emerge-skip-prefers (if (null arg)
2030 (not emerge-skip-prefers)
2031 (> (prefix-numeric-value arg) 0)))
2032 (message (if emerge-skip-prefers
2033 "Skip-prefers set"
2034 "Skip-prefers cleared"))
2035 (force-mode-line-update))
2037 (defun emerge-copy-as-kill-A ()
2038 "Put the A variant of this difference in the kill ring."
2039 (interactive)
2040 (emerge-validate-difference)
2041 (let* ((diff-vector
2042 (aref emerge-difference-list emerge-current-difference))
2043 (A-begin (1+ (aref diff-vector 0)))
2044 (A-end (1- (aref diff-vector 1)))
2045 ;; so further kills don't append
2046 this-command)
2047 (with-current-buffer emerge-A-buffer
2048 (copy-region-as-kill A-begin A-end))))
2050 (defun emerge-copy-as-kill-B ()
2051 "Put the B variant of this difference in the kill ring."
2052 (interactive)
2053 (emerge-validate-difference)
2054 (let* ((diff-vector
2055 (aref emerge-difference-list emerge-current-difference))
2056 (B-begin (1+ (aref diff-vector 2)))
2057 (B-end (1- (aref diff-vector 3)))
2058 ;; so further kills don't append
2059 this-command)
2060 (with-current-buffer emerge-B-buffer
2061 (copy-region-as-kill B-begin B-end))))
2063 (defun emerge-insert-A (arg)
2064 "Insert the A variant of this difference at the point.
2065 Leaves point after text, mark before.
2066 With prefix argument, puts point before, mark after."
2067 (interactive "P")
2068 (emerge-validate-difference)
2069 (let* ((diff-vector
2070 (aref emerge-difference-list emerge-current-difference))
2071 (A-begin (1+ (aref diff-vector 0)))
2072 (A-end (1- (aref diff-vector 1)))
2073 (opoint (point))
2074 (buffer-read-only nil))
2075 (insert-buffer-substring emerge-A-buffer A-begin A-end)
2076 (if (not arg)
2077 (set-mark opoint)
2078 (set-mark (point))
2079 (goto-char opoint))))
2081 (defun emerge-insert-B (arg)
2082 "Insert the B variant of this difference at the point.
2083 Leaves point after text, mark before.
2084 With prefix argument, puts point before, mark after."
2085 (interactive "P")
2086 (emerge-validate-difference)
2087 (let* ((diff-vector
2088 (aref emerge-difference-list emerge-current-difference))
2089 (B-begin (1+ (aref diff-vector 2)))
2090 (B-end (1- (aref diff-vector 3)))
2091 (opoint (point))
2092 (buffer-read-only nil))
2093 (insert-buffer-substring emerge-B-buffer B-begin B-end)
2094 (if (not arg)
2095 (set-mark opoint)
2096 (set-mark (point))
2097 (goto-char opoint))))
2099 (defun emerge-mark-difference (arg)
2100 "Leaves the point before this difference and the mark after it.
2101 With prefix argument, puts mark before, point after."
2102 (interactive "P")
2103 (emerge-validate-difference)
2104 (let* ((diff-vector
2105 (aref emerge-difference-list emerge-current-difference))
2106 (merge-begin (1+ (aref diff-vector 4)))
2107 (merge-end (1- (aref diff-vector 5))))
2108 (if (not arg)
2109 (progn
2110 (goto-char merge-begin)
2111 (set-mark merge-end))
2112 (goto-char merge-end)
2113 (set-mark merge-begin))))
2115 (defun emerge-file-names ()
2116 "Show the names of the buffers or files being operated on by Emerge.
2117 Use C-u l to reset the windows afterward."
2118 (interactive)
2119 (delete-other-windows)
2120 (let ((temp-buffer-show-function
2121 (lambda (buf)
2122 (split-window-below)
2123 (switch-to-buffer buf)
2124 (other-window 1))))
2125 (with-output-to-temp-buffer "*Help*"
2126 (with-current-buffer emerge-A-buffer
2127 (if buffer-file-name
2128 (progn
2129 (princ "File A is: ")
2130 (princ buffer-file-name))
2131 (progn
2132 (princ "Buffer A is: ")
2133 (princ (buffer-name))))
2134 (princ "\n"))
2135 (with-current-buffer emerge-B-buffer
2136 (if buffer-file-name
2137 (progn
2138 (princ "File B is: ")
2139 (princ buffer-file-name))
2140 (progn
2141 (princ "Buffer B is: ")
2142 (princ (buffer-name))))
2143 (princ "\n"))
2144 (if emerge-ancestor-buffer
2145 (with-current-buffer emerge-ancestor-buffer
2146 (if buffer-file-name
2147 (progn
2148 (princ "Ancestor file is: ")
2149 (princ buffer-file-name))
2150 (progn
2151 (princ "Ancestor buffer is: ")
2152 (princ (buffer-name))))
2153 (princ "\n")))
2154 (princ emerge-output-description)
2155 (with-current-buffer standard-output
2156 (help-mode)))))
2158 (defun emerge-join-differences (arg)
2159 "Join the selected difference with the following one.
2160 With a prefix argument, join with the preceding one."
2161 (interactive "P")
2162 (let ((n emerge-current-difference))
2163 ;; adjust n to be first difference to join
2164 (if arg
2165 (setq n (1- n)))
2166 ;; n and n+1 are the differences to join
2167 ;; check that they are both differences
2168 (if (or (< n 0) (>= n (1- emerge-number-of-differences)))
2169 (error "Incorrect differences to join"))
2170 ;; remove the flags
2171 (emerge-unselect-difference emerge-current-difference)
2172 ;; decrement total number of differences
2173 (setq emerge-number-of-differences (1- emerge-number-of-differences))
2174 ;; build new differences vector
2175 (let ((i 0)
2176 (new-differences (make-vector emerge-number-of-differences nil)))
2177 (while (< i emerge-number-of-differences)
2178 (aset new-differences i
2179 (cond
2180 ((< i n) (aref emerge-difference-list i))
2181 ((> i n) (aref emerge-difference-list (1+ i)))
2182 (t (let ((prev (aref emerge-difference-list i))
2183 (next (aref emerge-difference-list (1+ i))))
2184 (vector (aref prev 0)
2185 (aref next 1)
2186 (aref prev 2)
2187 (aref next 3)
2188 (aref prev 4)
2189 (aref next 5)
2190 (let ((ps (aref prev 6))
2191 (ns (aref next 6)))
2192 (cond
2193 ((eq ps ns)
2195 ((and (or (eq ps 'B) (eq ps 'prefer-B))
2196 (or (eq ns 'B) (eq ns 'prefer-B)))
2198 (t 'A))))))))
2199 (setq i (1+ i)))
2200 (setq emerge-difference-list new-differences))
2201 ;; set the current difference correctly
2202 (setq emerge-current-difference n)
2203 ;; fix the mode line
2204 (emerge-refresh-mode-line)
2205 ;; reinsert the flags
2206 (emerge-select-difference emerge-current-difference)
2207 (emerge-recenter)))
2209 (defun emerge-split-difference ()
2210 "Split the current difference where the points are in the three windows."
2211 (interactive)
2212 (let ((n emerge-current-difference))
2213 ;; check that this is a valid difference
2214 (emerge-validate-difference)
2215 ;; get the point values and old difference
2216 (let ((A-point (with-current-buffer emerge-A-buffer
2217 (point-marker)))
2218 (B-point (with-current-buffer emerge-B-buffer
2219 (point-marker)))
2220 (merge-point (point-marker))
2221 (old-diff (aref emerge-difference-list n)))
2222 ;; check location of the points, give error if they aren't in the
2223 ;; differences
2224 (if (or (< A-point (aref old-diff 0))
2225 (> A-point (aref old-diff 1)))
2226 (error "Point outside of difference in A buffer"))
2227 (if (or (< B-point (aref old-diff 2))
2228 (> B-point (aref old-diff 3)))
2229 (error "Point outside of difference in B buffer"))
2230 (if (or (< merge-point (aref old-diff 4))
2231 (> merge-point (aref old-diff 5)))
2232 (error "Point outside of difference in merge buffer"))
2233 ;; remove the flags
2234 (emerge-unselect-difference emerge-current-difference)
2235 ;; increment total number of differences
2236 (setq emerge-number-of-differences (1+ emerge-number-of-differences))
2237 ;; build new differences vector
2238 (let ((i 0)
2239 (new-differences (make-vector emerge-number-of-differences nil)))
2240 (while (< i emerge-number-of-differences)
2241 (aset new-differences i
2242 (cond
2243 ((< i n)
2244 (aref emerge-difference-list i))
2245 ((> i (1+ n))
2246 (aref emerge-difference-list (1- i)))
2247 ((= i n)
2248 (vector (aref old-diff 0)
2249 A-point
2250 (aref old-diff 2)
2251 B-point
2252 (aref old-diff 4)
2253 merge-point
2254 (aref old-diff 6)))
2256 (vector (copy-marker A-point)
2257 (aref old-diff 1)
2258 (copy-marker B-point)
2259 (aref old-diff 3)
2260 (copy-marker merge-point)
2261 (aref old-diff 5)
2262 (aref old-diff 6)))))
2263 (setq i (1+ i)))
2264 (setq emerge-difference-list new-differences))
2265 ;; set the current difference correctly
2266 (setq emerge-current-difference n)
2267 ;; fix the mode line
2268 (emerge-refresh-mode-line)
2269 ;; reinsert the flags
2270 (emerge-select-difference emerge-current-difference)
2271 (emerge-recenter))))
2273 (defun emerge-trim-difference ()
2274 "Trim lines off top and bottom of difference that are the same.
2275 If lines are the same in both the A and the B versions, strip them off.
2276 \(This can happen when the A and B versions have common lines that the
2277 ancestor version does not share.)"
2278 (interactive)
2279 ;; make sure we are in a real difference
2280 (emerge-validate-difference)
2281 ;; remove the flags
2282 (emerge-unselect-difference emerge-current-difference)
2283 (let* ((diff (aref emerge-difference-list emerge-current-difference))
2284 (top-a (marker-position (aref diff 0)))
2285 (bottom-a (marker-position (aref diff 1)))
2286 (top-b (marker-position (aref diff 2)))
2287 (bottom-b (marker-position (aref diff 3)))
2288 (top-m (marker-position (aref diff 4)))
2289 (bottom-m (marker-position (aref diff 5)))
2290 size success sa sb sm)
2291 ;; move down the tops of the difference regions as much as possible
2292 ;; Try advancing comparing 1000 chars at a time.
2293 ;; When that fails, go 500 chars at a time, and so on.
2294 (setq size 1000)
2295 (while (> size 0)
2296 (setq success t)
2297 (while success
2298 (setq size (min size (- bottom-a top-a) (- bottom-b top-b)
2299 (- bottom-m top-m)))
2300 (setq sa (with-current-buffer emerge-A-buffer
2301 (buffer-substring top-a
2302 (+ size top-a))))
2303 (setq sb (with-current-buffer emerge-B-buffer
2304 (buffer-substring top-b
2305 (+ size top-b))))
2306 (setq sm (buffer-substring top-m (+ size top-m)))
2307 (setq success (and (> size 0) (equal sa sb) (equal sb sm)))
2308 (if success
2309 (setq top-a (+ top-a size)
2310 top-b (+ top-b size)
2311 top-m (+ top-m size))))
2312 (setq size (/ size 2)))
2313 ;; move up the bottoms of the difference regions as much as possible
2314 ;; Try advancing comparing 1000 chars at a time.
2315 ;; When that fails, go 500 chars at a time, and so on.
2316 (setq size 1000)
2317 (while (> size 0)
2318 (setq success t)
2319 (while success
2320 (setq size (min size (- bottom-a top-a) (- bottom-b top-b)
2321 (- bottom-m top-m)))
2322 (setq sa (with-current-buffer emerge-A-buffer
2323 (buffer-substring (- bottom-a size)
2324 bottom-a)))
2325 (setq sb (with-current-buffer emerge-B-buffer
2326 (buffer-substring (- bottom-b size)
2327 bottom-b)))
2328 (setq sm (buffer-substring (- bottom-m size) bottom-m))
2329 (setq success (and (> size 0) (equal sa sb) (equal sb sm)))
2330 (if success
2331 (setq bottom-a (- bottom-a size)
2332 bottom-b (- bottom-b size)
2333 bottom-m (- bottom-m size))))
2334 (setq size (/ size 2)))
2335 ;; {top,bottom}-{a,b,m} are now set at the new beginnings and ends
2336 ;; of the difference regions. Move them to the beginning of lines, as
2337 ;; appropriate.
2338 (with-current-buffer emerge-A-buffer
2339 (goto-char top-a)
2340 (beginning-of-line)
2341 (aset diff 0 (point-marker))
2342 (goto-char bottom-a)
2343 (beginning-of-line 2)
2344 (aset diff 1 (point-marker)))
2345 (with-current-buffer emerge-B-buffer
2346 (goto-char top-b)
2347 (beginning-of-line)
2348 (aset diff 2 (point-marker))
2349 (goto-char bottom-b)
2350 (beginning-of-line 2)
2351 (aset diff 3 (point-marker)))
2352 (goto-char top-m)
2353 (beginning-of-line)
2354 (aset diff 4 (point-marker))
2355 (goto-char bottom-m)
2356 (beginning-of-line 2)
2357 (aset diff 5 (point-marker))
2358 ;; put the flags back in, recenter the display
2359 (emerge-select-difference emerge-current-difference)
2360 (emerge-recenter)))
2362 ;; FIXME the manual advertised this as working in the A or B buffers,
2363 ;; but it does not, because all the buffer locals are nil there.
2364 ;; It would work to call it from the merge buffer and specify that one
2365 ;; wants to use the value of point in the A or B buffer.
2366 ;; But with the prefix argument already in use, there is no easy way
2367 ;; to have it ask for a buffer.
2368 (defun emerge-find-difference (arg)
2369 "Find the difference containing the current position of the point.
2370 If there is no containing difference and the prefix argument is positive,
2371 it finds the nearest following difference. A negative prefix argument finds
2372 the nearest previous difference."
2373 (interactive "P")
2374 (cond ((eq (current-buffer) emerge-A-buffer)
2375 (emerge-find-difference-A arg))
2376 ((eq (current-buffer) emerge-B-buffer)
2377 (emerge-find-difference-B arg))
2378 (t (emerge-find-difference-merge arg))))
2380 (defun emerge-find-difference-merge (arg)
2381 "Find the difference containing point, in the merge buffer.
2382 If there is no containing difference and the prefix argument is positive,
2383 it finds the nearest following difference. A negative prefix argument finds
2384 the nearest previous difference."
2385 (interactive "P")
2386 ;; search for the point in the merge buffer, using the markers
2387 ;; for the beginning and end of the differences in the merge buffer
2388 (emerge-find-difference1 arg (point) 4 5))
2390 (defun emerge-find-difference-A (arg)
2391 "Find the difference containing point, in the A buffer.
2392 This command must be executed in the merge buffer.
2393 If there is no containing difference and the prefix argument is positive,
2394 it finds the nearest following difference. A negative prefix argument finds
2395 the nearest previous difference."
2396 (interactive "P")
2397 ;; search for the point in the A buffer, using the markers
2398 ;; for the beginning and end of the differences in the A buffer
2399 (emerge-find-difference1 arg
2400 (with-current-buffer emerge-A-buffer (point))
2401 0 1))
2403 (defun emerge-find-difference-B (arg)
2404 "Find the difference containing point, in the B buffer.
2405 This command must be executed in the merge buffer.
2406 If there is no containing difference and the prefix argument is positive,
2407 it finds the nearest following difference. A negative prefix argument finds
2408 the nearest previous difference."
2409 (interactive "P")
2410 ;; search for the point in the B buffer, using the markers
2411 ;; for the beginning and end of the differences in the B buffer
2412 (emerge-find-difference1 arg
2413 (with-current-buffer emerge-B-buffer (point))
2414 2 3))
2416 (defun emerge-find-difference1 (arg location begin end)
2417 (let* ((index
2418 ;; find first difference containing or after the current position
2419 (catch 'search
2420 (let ((n 0))
2421 (while (< n emerge-number-of-differences)
2422 (let ((diff-vector (aref emerge-difference-list n)))
2423 (if (<= location (marker-position (aref diff-vector end)))
2424 (throw 'search n)))
2425 (setq n (1+ n))))
2426 emerge-number-of-differences))
2427 (contains
2428 ;; whether the found difference contains the current position
2429 (and (< index emerge-number-of-differences)
2430 (<= (marker-position (aref (aref emerge-difference-list index)
2431 begin))
2432 location)))
2433 (arg-value
2434 ;; numeric value of prefix argument
2435 (prefix-numeric-value arg)))
2436 (emerge-unselect-and-select-difference
2437 (cond
2438 ;; if the point is in a difference, select it
2439 (contains index)
2440 ;; if the arg is nil and the point is not in a difference, error
2441 ((null arg) (error "No difference contains point"))
2442 ;; if the arg is positive, select the following difference
2443 ((> arg-value 0)
2444 (if (< index emerge-number-of-differences)
2445 index
2446 (error "No difference contains or follows point")))
2447 ;; if the arg is negative, select the preceding difference
2449 (if (> index 0)
2450 (1- index)
2451 (error "No difference contains or precedes point")))))))
2453 (defun emerge-line-numbers ()
2454 "Display the current line numbers.
2455 This function displays the line numbers of the points in the A, B, and
2456 merge buffers."
2457 (interactive)
2458 (let* ((valid-diff
2459 (and (>= emerge-current-difference 0)
2460 (< emerge-current-difference emerge-number-of-differences)))
2461 (emerge-line-diff (and valid-diff
2462 (aref emerge-difference-list
2463 emerge-current-difference)))
2464 (merge-line (emerge-line-number-in-buf 4 5))
2465 (A-line (with-current-buffer emerge-A-buffer
2466 (emerge-line-number-in-buf 0 1)))
2467 (B-line (with-current-buffer emerge-B-buffer
2468 (emerge-line-number-in-buf 2 3))))
2469 (message "At lines: merge = %d, A = %d, B = %d"
2470 merge-line A-line B-line)))
2472 (defvar emerge-line-diff)
2474 (defun emerge-line-number-in-buf (begin-marker end-marker)
2475 ;; FIXME point-min rather than 1? widen?
2476 (let ((temp (1+ (count-lines 1 (line-beginning-position)))))
2477 (if valid-diff
2478 (progn
2479 (if (> (point) (aref emerge-line-diff begin-marker))
2480 (setq temp (- temp emerge-before-flag-lines)))
2481 (if (> (point) (aref emerge-line-diff end-marker))
2482 (setq temp (- temp emerge-after-flag-lines)))))
2483 temp))
2485 (defun emerge-set-combine-template (string &optional localize)
2486 "Set `emerge-combine-versions-template' to STRING.
2487 This value controls how `emerge-combine-versions' combines the two versions.
2488 With prefix argument, `emerge-combine-versions-template' is made local to this
2489 merge buffer. Localization is permanent for any particular merge buffer."
2490 (interactive "s\nP")
2491 (if localize
2492 (make-local-variable 'emerge-combine-versions-template))
2493 (setq emerge-combine-versions-template string)
2494 (message
2495 (if (assq 'emerge-combine-versions-template (buffer-local-variables))
2496 "emerge-set-combine-versions-template set locally"
2497 "emerge-set-combine-versions-template set")))
2499 (defun emerge-set-combine-versions-template (start end &optional localize)
2500 "Copy region into `emerge-combine-versions-template'.
2501 This controls how `emerge-combine-versions' will combine the two versions.
2502 With prefix argument, `emerge-combine-versions-template' is made local to this
2503 merge buffer. Localization is permanent for any particular merge buffer."
2504 (interactive "r\nP")
2505 (if localize
2506 (make-local-variable 'emerge-combine-versions-template))
2507 (setq emerge-combine-versions-template (buffer-substring start end))
2508 (message
2509 (if (assq 'emerge-combine-versions-template (buffer-local-variables))
2510 "emerge-set-combine-versions-template set locally."
2511 "emerge-set-combine-versions-template set.")))
2513 (defun emerge-combine-versions (&optional force)
2514 "Combine versions using the template in `emerge-combine-versions-template'.
2515 Refuses to function if this difference has been edited, i.e., if it is
2516 neither the A nor the B variant.
2517 An argument forces the variant to be selected even if the difference has
2518 been edited."
2519 (interactive "P")
2520 (emerge-combine-versions-internal emerge-combine-versions-template force))
2522 (defun emerge-combine-versions-register (char &optional force)
2523 "Combine the two versions using the template in register REG.
2524 See documentation of the variable `emerge-combine-versions-template'
2525 for how the template is interpreted.
2526 Refuses to function if this difference has been edited, i.e., if it is
2527 neither the A nor the B variant.
2528 An argument forces the variant to be selected even if the difference has
2529 been edited.
2531 Interactively, reads the register using `register-read-with-preview'."
2532 (interactive (list
2533 (register-read-with-preview "Register containing template: ")
2534 current-prefix-arg))
2535 (let ((template (get-register char)))
2536 (if (not (stringp template))
2537 (error "Register does not contain text"))
2538 (emerge-combine-versions-internal template force)))
2540 (defun emerge-combine-versions-internal (emerge-combine-template force)
2541 (let ((operate
2542 (lambda ()
2543 (emerge-combine-versions-edit merge-begin merge-end
2544 A-begin A-end B-begin B-end)
2545 (if emerge-auto-advance
2546 (emerge-next-difference)))))
2547 (emerge-select-version force operate operate operate)))
2549 (defvar emerge-combine-template)
2551 (defun emerge-combine-versions-edit (merge-begin merge-end
2552 A-begin A-end B-begin B-end)
2553 (with-current-buffer
2554 emerge-merge-buffer
2555 (delete-region merge-begin merge-end)
2556 (goto-char merge-begin)
2557 (let ((i 0))
2558 (while (< i (length emerge-combine-template))
2559 (let ((c (aref emerge-combine-template i)))
2560 (if (= c ?%)
2561 (progn
2562 (setq i (1+ i))
2563 (setq c
2564 (condition-case nil
2565 (aref emerge-combine-template i)
2566 (error ?%)))
2567 (cond ((= c ?a)
2568 (insert-buffer-substring emerge-A-buffer A-begin A-end))
2569 ((= c ?b)
2570 (insert-buffer-substring emerge-B-buffer B-begin B-end))
2571 ((= c ?%)
2572 (insert ?%))
2574 (insert c))))
2575 (insert c)))
2576 (setq i (1+ i))))
2577 (goto-char merge-begin)
2578 (aset diff-vector 6 'combined)
2579 (emerge-refresh-mode-line)))
2581 (defun emerge-set-merge-mode (mode)
2582 "Set the major mode in a merge buffer.
2583 Overrides any change that the mode might make to the mode line or local
2584 keymap. Leaves merge in fast mode."
2585 (interactive
2586 (list (intern (completing-read "New major mode for merge buffer: "
2587 obarray 'commandp t nil))))
2588 (funcall mode)
2589 (emerge-refresh-mode-line)
2590 (if emerge-fast-mode
2591 (emerge-fast-mode)
2592 (emerge-edit-mode)))
2594 (defun emerge-one-line-window ()
2595 (interactive)
2596 (let ((window-min-height 1))
2597 (shrink-window (- (window-height) 2))))
2599 ;;; Support routines
2601 ;; Select a difference by placing the visual flags around the appropriate
2602 ;; group of lines in the A, B, and merge buffers
2603 (defun emerge-select-difference (n)
2604 (let ((emerge-globalized-difference-list emerge-difference-list)
2605 (emerge-globalized-number-of-differences emerge-number-of-differences))
2606 (emerge-place-flags-in-buffer emerge-A-buffer n 0 1)
2607 (emerge-place-flags-in-buffer emerge-B-buffer n 2 3)
2608 (emerge-place-flags-in-buffer nil n 4 5))
2609 (run-hooks 'emerge-select-hook))
2611 (defun emerge-place-flags-in-buffer (buffer difference before-index
2612 after-index)
2613 (if buffer
2614 (with-current-buffer
2615 buffer
2616 (emerge-place-flags-in-buffer1 difference before-index after-index))
2617 (emerge-place-flags-in-buffer1 difference before-index after-index)))
2619 (defun emerge-place-flags-in-buffer1 (difference before-index after-index)
2620 (let ((buffer-read-only nil))
2621 ;; insert the flag before the difference
2622 (let ((before (aref (aref emerge-globalized-difference-list difference)
2623 before-index))
2624 here)
2625 (goto-char before)
2626 ;; insert the flag itself
2627 (insert-before-markers emerge-before-flag)
2628 (setq here (point))
2629 ;; Put the marker(s) referring to this position 1 character before the
2630 ;; end of the flag, so it won't be damaged by the user.
2631 ;; This gets a bit tricky, as there could be a number of markers
2632 ;; that have to be moved.
2633 (set-marker before (1- before))
2634 (let ((n (1- difference)) after-marker before-marker diff-list)
2635 (while (and
2636 (>= n 0)
2637 (progn
2638 (setq diff-list (aref emerge-globalized-difference-list n)
2639 after-marker (aref diff-list after-index))
2640 (= after-marker here)))
2641 (set-marker after-marker (1- after-marker))
2642 (setq before-marker (aref diff-list before-index))
2643 (if (= before-marker here)
2644 (setq before-marker (1- before-marker)))
2645 (setq n (1- n)))))
2646 ;; insert the flag after the difference
2647 (let* ((after (aref (aref emerge-globalized-difference-list difference)
2648 after-index))
2649 (here (marker-position after)))
2650 (goto-char here)
2651 ;; insert the flag itself
2652 (insert emerge-after-flag)
2653 ;; Put the marker(s) referring to this position 1 character after the
2654 ;; beginning of the flag, so it won't be damaged by the user.
2655 ;; This gets a bit tricky, as there could be a number of markers
2656 ;; that have to be moved.
2657 (set-marker after (1+ after))
2658 (let ((n (1+ difference)) before-marker after-marker diff-list)
2659 (while (and
2660 (< n emerge-globalized-number-of-differences)
2661 (progn
2662 (setq diff-list (aref emerge-globalized-difference-list n)
2663 before-marker (aref diff-list before-index))
2664 (= before-marker here)))
2665 (set-marker before-marker (1+ before-marker))
2666 (setq after-marker (aref diff-list after-index))
2667 (if (= after-marker here)
2668 (setq after-marker (1+ after-marker)))
2669 (setq n (1+ n)))))))
2671 ;; Unselect a difference by removing the visual flags in the buffers.
2672 (defun emerge-unselect-difference (n)
2673 (let ((diff-vector (aref emerge-difference-list n)))
2674 (emerge-remove-flags-in-buffer emerge-A-buffer
2675 (aref diff-vector 0) (aref diff-vector 1))
2676 (emerge-remove-flags-in-buffer emerge-B-buffer
2677 (aref diff-vector 2) (aref diff-vector 3))
2678 (emerge-remove-flags-in-buffer emerge-merge-buffer
2679 (aref diff-vector 4) (aref diff-vector 5)))
2680 (run-hooks 'emerge-unselect-hook))
2682 (defun emerge-remove-flags-in-buffer (buffer before after)
2683 (with-current-buffer
2684 buffer
2685 (let ((buffer-read-only nil))
2686 ;; remove the flags, if they're there
2687 (goto-char (- before (1- emerge-before-flag-length)))
2688 (if (looking-at emerge-before-flag-match)
2689 (delete-char emerge-before-flag-length)
2690 ;; the flag isn't there
2691 (ding)
2692 (message "Trouble removing flag"))
2693 (goto-char (1- after))
2694 (if (looking-at emerge-after-flag-match)
2695 (delete-char emerge-after-flag-length)
2696 ;; the flag isn't there
2697 (ding)
2698 (message "Trouble removing flag")))))
2700 ;; Select a difference, removing any flags that exist now.
2701 (defun emerge-unselect-and-select-difference (n &optional suppress-display)
2702 (if (and (>= emerge-current-difference 0)
2703 (< emerge-current-difference emerge-number-of-differences))
2704 (emerge-unselect-difference emerge-current-difference))
2705 (if (and (>= n 0) (< n emerge-number-of-differences))
2706 (progn
2707 (emerge-select-difference n)
2708 (let* ((diff-vector (aref emerge-difference-list n))
2709 (selection-type (aref diff-vector 6)))
2710 (if (eq selection-type 'default-A)
2711 (aset diff-vector 6 'A)
2712 (if (eq selection-type 'default-B)
2713 (aset diff-vector 6 'B))))))
2714 (setq emerge-current-difference n)
2715 (if (not suppress-display)
2716 (progn
2717 (emerge-recenter)
2718 (emerge-refresh-mode-line))))
2720 ;; Perform tests to see whether user should be allowed to select a version
2721 ;; of this difference:
2722 ;; a valid difference has been selected; and
2723 ;; the difference text in the merge buffer is:
2724 ;; the A version (execute a-version), or
2725 ;; the B version (execute b-version), or
2726 ;; empty (execute neither-version), or
2727 ;; argument FORCE is true (execute neither-version)
2728 ;; Otherwise, signal an error.
2729 (defun emerge-select-version (force a-version b-version neither-version)
2730 (emerge-validate-difference)
2731 (let ((buffer-read-only nil))
2732 (let* ((diff-vector
2733 (aref emerge-difference-list emerge-current-difference))
2734 (A-begin (1+ (aref diff-vector 0)))
2735 (A-end (1- (aref diff-vector 1)))
2736 (B-begin (1+ (aref diff-vector 2)))
2737 (B-end (1- (aref diff-vector 3)))
2738 (merge-begin (1+ (aref diff-vector 4)))
2739 (merge-end (1- (aref diff-vector 5))))
2740 (if (emerge-compare-buffers emerge-A-buffer A-begin A-end
2741 emerge-merge-buffer merge-begin
2742 merge-end)
2743 (funcall a-version)
2744 (if (emerge-compare-buffers emerge-B-buffer B-begin B-end
2745 emerge-merge-buffer merge-begin
2746 merge-end)
2747 (funcall b-version)
2748 (if (or force (= merge-begin merge-end))
2749 (funcall neither-version)
2750 (error "This difference region has been edited")))))))
2752 ;; Read a file name, handling all of the various defaulting rules.
2754 (defun emerge-read-file-name (prompt alternative-default-dir default-file
2755 A-file must-match)
2756 ;; `prompt' should not have trailing ": ", so that it can be modified
2757 ;; according to context.
2758 ;; If alternative-default-dir is non-nil, it should be used as the default
2759 ;; directory instead if default-directory, if emerge-default-last-directories
2760 ;; is set.
2761 ;; If default-file is set, it should be used as the default value.
2762 ;; If A-file is set, and its directory is different from
2763 ;; alternative-default-dir, and if emerge-default-last-directories is set,
2764 ;; the default file should be the last part of A-file in the default
2765 ;; directory. (Overriding default-file.)
2766 (cond
2767 ;; If this is not the A-file argument (shown by non-nil A-file), and
2768 ;; if emerge-default-last-directories is set, and
2769 ;; the default directory exists but is not the same as the directory of the
2770 ;; A-file,
2771 ;; then make the default file have the same name as the A-file, but in
2772 ;; the default directory.
2773 ((and emerge-default-last-directories
2774 A-file
2775 alternative-default-dir
2776 (not (string-equal alternative-default-dir
2777 (file-name-directory A-file))))
2778 (read-file-name (format "%s (default %s): "
2779 prompt (file-name-nondirectory A-file))
2780 alternative-default-dir
2781 (concat alternative-default-dir
2782 (file-name-nondirectory A-file))
2783 (and must-match 'confirm)))
2784 ;; If there is a default file, use it.
2785 (default-file
2786 (read-file-name (format "%s (default %s): " prompt default-file)
2787 ;; If emerge-default-last-directories is set, use the
2788 ;; directory from the same argument of the last call of
2789 ;; Emerge as the default for this argument.
2790 (and emerge-default-last-directories
2791 alternative-default-dir)
2792 default-file (and must-match 'confirm)))
2794 (read-file-name (concat prompt ": ")
2795 ;; If emerge-default-last-directories is set, use the
2796 ;; directory from the same argument of the last call of
2797 ;; Emerge as the default for this argument.
2798 (and emerge-default-last-directories
2799 alternative-default-dir)
2800 nil (and must-match 'confirm)))))
2802 ;; Revise the mode line to display which difference we have selected
2804 (defun emerge-refresh-mode-line ()
2805 (setq mode-line-buffer-identification
2806 (list (format "Emerge: %%b diff %d of %d%s"
2807 (1+ emerge-current-difference)
2808 emerge-number-of-differences
2809 (if (and (>= emerge-current-difference 0)
2810 (< emerge-current-difference
2811 emerge-number-of-differences))
2812 (cdr (assq (aref (aref emerge-difference-list
2813 emerge-current-difference)
2815 '((A . " - A")
2816 (B . " - B")
2817 (prefer-A . " - A*")
2818 (prefer-B . " - B*")
2819 (combined . " - comb"))))
2820 ""))))
2821 (force-mode-line-update))
2823 ;; compare two regions in two buffers for containing the same text
2824 (defun emerge-compare-buffers (buffer-x x-begin x-end buffer-y y-begin y-end)
2825 ;; first check that the two regions are the same length
2826 (if (not (and (= (- x-end x-begin) (- y-end y-begin))))
2828 (catch 'exit
2829 (while (< x-begin x-end)
2830 ;; bite off and compare no more than 1000 characters at a time
2831 (let* ((compare-length (min (- x-end x-begin) 1000))
2832 (x-string (with-current-buffer
2833 buffer-x
2834 (buffer-substring x-begin
2835 (+ x-begin compare-length))))
2836 (y-string (with-current-buffer
2837 buffer-y
2838 (buffer-substring y-begin
2839 (+ y-begin compare-length)))))
2840 (if (not (string-equal x-string y-string))
2841 (throw 'exit nil)
2842 (setq x-begin (+ x-begin compare-length))
2843 (setq y-begin (+ y-begin compare-length)))))
2844 t)))
2846 ;; Construct a unique buffer name.
2847 ;; The first one tried is prefixsuffix, then prefix<2>suffix,
2848 ;; prefix<3>suffix, etc.
2849 (defun emerge-unique-buffer-name (prefix suffix)
2850 (if (null (get-buffer (concat prefix suffix)))
2851 (concat prefix suffix)
2852 (let ((n 2))
2853 (while (get-buffer (format "%s<%d>%s" prefix n suffix))
2854 (setq n (1+ n)))
2855 (format "%s<%d>%s" prefix n suffix))))
2857 ;; Verify that we have a difference selected.
2858 (defun emerge-validate-difference ()
2859 (if (not (and (>= emerge-current-difference 0)
2860 (< emerge-current-difference emerge-number-of-differences)))
2861 (error "No difference selected")))
2863 ;;; Functions for saving and restoring a batch of variables
2865 ;; These functions save (get the values of) and restore (set the values of)
2866 ;; a list of variables. The argument is a list of symbols (the names of
2867 ;; the variables). A list element can also be a list of two functions,
2868 ;; the first of which (when called with no arguments) gets the value, and
2869 ;; the second (when called with a value as an argument) sets the value.
2870 ;; A "function" is anything that funcall can handle as an argument.
2872 (defun emerge-save-variables (vars)
2873 (mapcar (lambda (v) (if (symbolp v)
2874 (symbol-value v)
2875 (funcall (car v))))
2876 vars))
2878 (defun emerge-restore-variables (vars values)
2879 (while vars
2880 (let ((var (car vars))
2881 (value (car values)))
2882 (if (symbolp var)
2883 (set var value)
2884 (funcall (car (cdr var)) value)))
2885 (setq vars (cdr vars))
2886 (setq values (cdr values))))
2888 ;; When the pointless option emerge-temp-file-prefix goes,
2889 ;; make this function obsolete too, and just use make-temp-file.
2890 (defun emerge-make-temp-file (prefix)
2891 "Make a private temporary file based on `emerge-temp-file-prefix'."
2892 (make-temp-file (concat emerge-temp-file-prefix prefix)))
2894 ;;; Functions that query the user before he can write out the current buffer.
2896 (defun emerge-query-write-file ()
2897 "Ask the user whether to write out an incomplete merge.
2898 If answer is yes, call `write-file' to do so. See `emerge-query-and-call'
2899 for details of the querying process."
2900 (interactive)
2901 (emerge-query-and-call 'write-file))
2903 (defun emerge-query-save-buffer ()
2904 "Ask the user whether to save an incomplete merge.
2905 If answer is yes, call `save-buffer' to do so. See `emerge-query-and-call'
2906 for details of the querying process."
2907 (interactive)
2908 (emerge-query-and-call 'save-buffer))
2910 (defun emerge-query-and-call (command)
2911 "Ask the user whether to save or write out the incomplete merge.
2912 If answer is yes, call COMMAND interactively. During the call, the flags
2913 around the current difference are removed."
2914 (if (yes-or-no-p "Do you really write to write out this unfinished merge? ")
2915 ;; He really wants to do it -- unselect the difference for the duration
2916 (progn
2917 (if (and (>= emerge-current-difference 0)
2918 (< emerge-current-difference emerge-number-of-differences))
2919 (emerge-unselect-difference emerge-current-difference))
2920 ;; call-interactively takes the value of current-prefix-arg as the
2921 ;; prefix argument value to be passed to the command. Thus, we have
2922 ;; to do nothing special to make sure the prefix argument is
2923 ;; transmitted to the command.
2924 (call-interactively command)
2925 (if (and (>= emerge-current-difference 0)
2926 (< emerge-current-difference emerge-number-of-differences))
2927 (progn
2928 (emerge-select-difference emerge-current-difference)
2929 (emerge-recenter))))
2930 ;; He's being smart and not doing it
2931 (message "Not written")))
2933 ;; Make sure the current buffer (for a file) has the same contents as the
2934 ;; file on disk, and attempt to remedy the situation if not.
2935 ;; Signal an error if we can't make them the same, or the user doesn't want
2936 ;; to do what is necessary to make them the same.
2937 (defun emerge-verify-file-buffer ()
2938 ;; First check if the file has been modified since the buffer visited it.
2939 (if (verify-visited-file-modtime (current-buffer))
2940 (if (buffer-modified-p)
2941 ;; If buffer is not obsolete and is modified, offer to save
2942 (if (yes-or-no-p (format "Save file %s? " buffer-file-name))
2943 (save-buffer)
2944 (error "Buffer out of sync for file %s" buffer-file-name))
2945 ;; If buffer is not obsolete and is not modified, do nothing
2946 nil)
2947 (if (buffer-modified-p)
2948 ;; If buffer is obsolete and is modified, give error
2949 (error "Buffer out of sync for file %s" buffer-file-name)
2950 ;; If buffer is obsolete and is not modified, offer to revert
2951 (if (yes-or-no-p (format "Revert file %s? " buffer-file-name))
2952 (revert-buffer t t)
2953 (error "Buffer out of sync for file %s" buffer-file-name)))))
2955 ;; Utilities that might have value outside of Emerge.
2957 ;; Set up the mode in the current buffer to duplicate the mode in another
2958 ;; buffer.
2959 (defun emerge-copy-modes (buffer)
2960 ;; Set the major mode
2961 (funcall (with-current-buffer buffer major-mode)))
2963 ;; Define a key, even if a prefix of it is defined
2964 (defun emerge-force-define-key (keymap key definition)
2965 "Like `define-key', but forcibly creates prefix characters as needed.
2966 If some prefix of KEY has a non-prefix definition, it is redefined."
2967 ;; Find out if a prefix of key is defined
2968 (let ((v (lookup-key keymap key)))
2969 ;; If so, undefine it
2970 (if (integerp v)
2971 (define-key keymap (substring key 0 v) nil)))
2972 ;; Now define the key
2973 (define-key keymap key definition))
2975 ;;;;; Improvements to describe-mode, so that it describes minor modes as well
2976 ;;;;; as the major mode
2977 ;;(defun describe-mode (&optional minor)
2978 ;; "Display documentation of current major mode.
2979 ;;If optional arg MINOR is non-nil (or prefix argument is given if interactive),
2980 ;;display documentation of active minor modes as well.
2981 ;;For this to work correctly for a minor mode, the mode's indicator variable
2982 ;;\(listed in `minor-mode-alist') must also be a function whose documentation
2983 ;;describes the minor mode."
2984 ;; (interactive)
2985 ;; (with-output-to-temp-buffer "*Help*"
2986 ;; (princ mode-name)
2987 ;; (princ " Mode:\n")
2988 ;; (princ (documentation major-mode))
2989 ;; (let ((minor-modes minor-mode-alist)
2990 ;; (locals (buffer-local-variables)))
2991 ;; (while minor-modes
2992 ;; (let* ((minor-mode (car (car minor-modes)))
2993 ;; (indicator (car (cdr (car minor-modes))))
2994 ;; (local-binding (assq minor-mode locals)))
2995 ;; ;; Document a minor mode if it is listed in minor-mode-alist,
2996 ;; ;; bound locally in this buffer, non-nil, and has a function
2997 ;; ;; definition.
2998 ;; (if (and local-binding
2999 ;; (cdr local-binding)
3000 ;; (fboundp minor-mode))
3001 ;; (progn
3002 ;; (princ (format "\n\n\n%s minor mode (indicator%s):\n"
3003 ;; minor-mode indicator))
3004 ;; (princ (documentation minor-mode)))))
3005 ;; (setq minor-modes (cdr minor-modes))))
3006 ;; (with-current-buffer standard-output
3007 ;; (help-mode))
3008 ;; (help-print-return-message)))
3010 ;; This goes with the redefinition of describe-mode.
3011 ;;;; Adjust things so that keyboard macro definitions are documented correctly.
3012 ;;(fset 'defining-kbd-macro (symbol-function 'start-kbd-macro))
3014 ;; substitute-key-definition should work now.
3015 ;;;; Function to shadow a definition in a keymap with definitions in another.
3016 ;;(defun emerge-shadow-key-definition (olddef newdef keymap shadowmap)
3017 ;; "Shadow OLDDEF with NEWDEF for any keys in KEYMAP with entries in SHADOWMAP.
3018 ;;In other words, SHADOWMAP will now shadow all definitions of OLDDEF in KEYMAP
3019 ;;with NEWDEF. Does not affect keys that are already defined in SHADOWMAP,
3020 ;;including those whose definition is OLDDEF."
3021 ;; ;; loop through all keymaps accessible from keymap
3022 ;; (let ((maps (accessible-keymaps keymap)))
3023 ;; (while maps
3024 ;; (let ((prefix (car (car maps)))
3025 ;; (map (cdr (car maps))))
3026 ;; ;; examine a keymap
3027 ;; (if (arrayp map)
3028 ;; ;; array keymap
3029 ;; (let ((len (length map))
3030 ;; (i 0))
3031 ;; (while (< i len)
3032 ;; (if (eq (aref map i) olddef)
3033 ;; ;; set the shadowing definition
3034 ;; (let ((key (concat prefix (char-to-string i))))
3035 ;; (emerge-define-key-if-possible shadowmap key newdef)))
3036 ;; (setq i (1+ i))))
3037 ;; ;; sparse keymap
3038 ;; (while map
3039 ;; (if (eq (cdr-safe (car-safe map)) olddef)
3040 ;; ;; set the shadowing definition
3041 ;; (let ((key
3042 ;; (concat prefix (char-to-string (car (car map))))))
3043 ;; (emerge-define-key-if-possible shadowmap key newdef)))
3044 ;; (setq map (cdr map)))))
3045 ;; (setq maps (cdr maps)))))
3047 ;; Define a key if it (or a prefix) is not already defined in the map.
3048 (defun emerge-define-key-if-possible (keymap key definition)
3049 ;; look up the present definition of the key
3050 (let ((present (lookup-key keymap key)))
3051 (if (integerp present)
3052 ;; if it is "too long", look up the valid prefix
3053 (if (not (lookup-key keymap (substring key 0 present)))
3054 ;; if the prefix isn't defined, define it
3055 (define-key keymap key definition))
3056 ;; if there is no present definition, define it
3057 (if (not present)
3058 (define-key keymap key definition)))))
3060 ;; Ordinary substitute-key-definition should do this now.
3061 ;;(defun emerge-recursively-substitute-key-definition (olddef newdef keymap)
3062 ;; "Like `substitute-key-definition', but act recursively on subkeymaps.
3063 ;;Make sure that subordinate keymaps aren't shared with other keymaps!
3064 ;;\(`copy-keymap' will suffice.)"
3065 ;; ;; Loop through all keymaps accessible from keymap
3066 ;; (let ((maps (accessible-keymaps keymap)))
3067 ;; (while maps
3068 ;; ;; Substitute in this keymap
3069 ;; (substitute-key-definition olddef newdef (cdr (car maps)))
3070 ;; (setq maps (cdr maps)))))
3072 ;; Show the name of the file in the buffer.
3073 (defun emerge-show-file-name ()
3074 "Displays the name of the file loaded into the current buffer.
3075 If the name won't fit on one line, the minibuffer is expanded to hold it,
3076 and the command waits for a keystroke from the user. If the keystroke is
3077 SPC, it is ignored; if it is anything else, it is processed as a command."
3078 (interactive)
3079 (let ((name (buffer-file-name)))
3080 (or name
3081 (setq name "Buffer has no file name."))
3082 (save-window-excursion
3083 (select-window (minibuffer-window))
3084 (unwind-protect
3085 (progn
3086 (erase-buffer)
3087 (insert name)
3088 (while (and (not (pos-visible-in-window-p))
3089 (not (window-full-height-p)))
3090 (enlarge-window 1))
3091 (let* ((echo-keystrokes 0)
3092 (c (read-event)))
3093 (if (not (eq c 32))
3094 (push c unread-command-events))))
3095 (erase-buffer)))))
3097 ;; Improved auto-save file names.
3098 ;; This function fixes many problems with the standard auto-save file names:
3099 ;; Auto-save files for non-file buffers get put in the default directory
3100 ;; for the buffer, whether that makes sense or not.
3101 ;; Auto-save files for file buffers get put in the directory of the file,
3102 ;; regardless of whether we can write into it or not.
3103 ;; Auto-save files for non-file buffers don't use the process id, so if a
3104 ;; user runs more than on Emacs, they can make auto-save files that overwrite
3105 ;; each other.
3106 ;; To use this function, do:
3107 ;; (fset 'make-auto-save-file-name
3108 ;; (symbol-function 'emerge-make-auto-save-file-name))
3109 (defun emerge-make-auto-save-file-name ()
3110 "Return file name to use for auto-saves of current buffer.
3111 Does not consider `auto-save-visited-file-name';
3112 that is checked before calling this function.
3113 You can redefine this for customization.
3114 See also `auto-save-file-name-p'."
3115 (if buffer-file-name
3116 ;; if buffer has a file, try the format <file directory>/#<file name>#
3117 (let ((f (concat (file-name-directory buffer-file-name)
3119 (file-name-nondirectory buffer-file-name)
3120 "#")))
3121 (if (file-writable-p f)
3122 ;; the file is writable, so use it
3124 ;; the file isn't writable, so use the format
3125 ;; ~/#&<file name>&<hash of directory>#
3126 (concat (getenv "HOME")
3127 "/#&"
3128 (file-name-nondirectory buffer-file-name)
3130 (emerge-hash-string-into-string
3131 (file-name-directory buffer-file-name))
3132 "#")))
3133 ;; if buffer has no file, use the format ~/#%<buffer name>%<process id>#
3134 (expand-file-name (concat (getenv "HOME")
3135 "/#%"
3136 ;; quote / into \! and \ into \\
3137 (emerge-unslashify-name (buffer-name))
3139 (make-temp-name "")
3140 "#"))))
3142 ;; Hash a string into five characters more-or-less suitable for use in a file
3143 ;; name. (Allowed characters are ! through ~, except /.)
3144 (defun emerge-hash-string-into-string (s)
3145 (let ((bins (vector 0 0 0 0 0))
3146 (i 0))
3147 (while (< i (length s))
3148 (aset bins (% i 5) (% (+ (* (aref bins (% i 5)) 35)
3149 (aref s i))
3150 65536))
3151 (setq i (1+ i)))
3152 (mapconcat (lambda (b)
3153 (setq b (+ (% b 93) ?!))
3154 (if (>= b ?/)
3155 (setq b (1+ b)))
3156 (char-to-string b))
3157 bins "")))
3159 ;; Quote any /s in a string by replacing them with \!.
3160 ;; Also, replace any \s by \\, to make it one-to-one.
3161 (defun emerge-unslashify-name (s)
3162 (let ((limit 0))
3163 (while (string-match "[/\\]" s limit)
3164 (setq s (concat (substring s 0 (match-beginning 0))
3165 (if (string= (substring s (match-beginning 0)
3166 (match-end 0))
3167 "/")
3168 "\\!"
3169 "\\\\")
3170 (substring s (match-end 0))))
3171 (setq limit (1+ (match-end 0)))))
3174 (defcustom emerge-metachars nil
3175 "No longer used. Emerge now uses `shell-quote-argument'."
3176 :type '(choice (const nil) regexp)
3177 :group 'emerge)
3178 (make-obsolete-variable 'emerge-metachars nil "26.1")
3180 (provide 'emerge)
3182 ;;; emerge.el ends here