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