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