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