]> code.delx.au - gnu-emacs/blobdiff - lisp/emerge.el
(ibuffer-compressed-file-name-regexp): Avoid loading
[gnu-emacs] / lisp / emerge.el
index bcb15da3aee9046a6e52ece57e66493b94d80488..447e45f14f3eb72e647134fb18b8b13f4f0a97ca 100644 (file)
-;;; emerge.el --- merge diffs inder Emacs control (version 4)
-
-;;; 13 Dec 1991
-
-;; LCD Archive Entry:
-;; emerge|Dale R. Worley|drw@math.mit.edu
-;; |File merge documentation
-;; |91-12-13|version 4|~/packages/emerge.doc.Z
-
-; - Changes from version 3 to version 4
-; 
-; More configuration variables are marked as user options.
-; 
-; Code is included for an improved version of make-auto-save-file-name
-; which eliminates many problems with the default version.  See the
-; documentation of emerge-make-auto-save-file-name to see how to
-; activate it.
-; 
-; Emerge now works with Gnu diff3, which can produce the groups of lines
-; from the various files in the order 1, 2, 3 or 1, 3, 2.
-; 
-; Added x f command to show what files or buffers are being operated on.
-; 
-; The merge buffer now starts read-only, which being in fast mode it
-; should be.
-; 
-; When merging buffers, Emerge writes their contents into temporary
-; files in the directory $TMPDIR (if it is defined), or /tmp by default.
-; 
-; Added x j command to join two differences.
-; 
-; Added x s command to split a difference into two differences.
-; 
-; Added emerge-version variable and function to report the version of Emerge
-; being run.
-; 
-; Added x t command to trim unchanged lines off top and bottom of
-; difference region.
-; 
-; Added x d, x a, and x b commands to locate the differences at or near
-; a given location in one of the buffers.
-; 
-; Emerge no longer tries to copy the minor modes from the A buffer to
-; the merge buffer, only the major mode.
-; 
-; The programs executed to find the differences between versions of the file
-; are no longer controlled by emerge-diff/diff3-command, but rather by:
-;   emerge-diff-program              
-;     Variable: *Name of the program which compares two files.
-;   emerge-diff3-program             
-;     Variable: *Name of the program which compares an ancestor file
-;     (first argument) and two variant files (second and third arguments).
-;   emerge-diff-options              
-;     Variable: *Options to be passed to emerge-diff/diff3-program.
-; 
-; The names of the files are expanded (see expand-file-name) before being
-; passed to emerge-diff/diff3-program, so diff need not invoked under a shell
-; that understands '~', for instance.
-; 
-; If the diff/diff3 program reports errors, the user is notified and the
-; errors are displayed.
-; 
-; The command "0j" can be used to suppress the flags from showing in the buffers.
-; 
-; A discussion of the effect of the merge flags on indentation of code
-; has been added to the documentation.
-; 
-; If kill-fix.el is loaded, Emerge control variables new have their
-; 'preserved' property set, so setting the major mode in the merge
-; buffer doesn't destroy Emerge's state.
-; 
-; Added x c, x C, and x x commands to allow the A and B versions to be
-; combined into #ifdef - #endif forms.
-; 
-; Replaced calls of "ding" to calls of "error" where appropriate.
-; 
-; Added x m command to allow major mode of merge buffer to be changed.
-; 
-; Added x 1 command to shrink the merge window to one line.
-; 
-; Added emerge-startup-hooks to allow customization.
-; 
-; Fixed a bug that is activated when a remote merge request is made when
-; the minibuffer window is selected.
-; 
-; - Changes from version 2 to version 3
-; 
-; The directory into which temporary files are written is now controlled
-; by a user option (emerge-temp-file-prefix).
-; 
-; The A and B versions of the difference can be loaded into the kill
-; ring with the "c a" and "c b" commands.
-; 
-; The A and B versions of the difference can be inserted into the merge
-; buffer with the "i a" and "i b" commands.
-; 
-; The difference region of the merge buffer can be surrounded by the
-; point and mark with the "m" command.
-; 
-; The three windows can be scrolled together with the "^", "v", "<",
-; ">", and "|" commands.
-; 
-; The "s s" and "s a" commands report the state of the option in the
-; echo area.  Similarly, the "f" and "e" commands report what they do in
-; the echo area.
-; 
-; The "q" command has been revamped, and its behavior is now controlled
-; by the manner in which Emerge is started.  In particular, if you wish
-; to write the merge buffer into a file upon exiting, invoke
-; emerge-files[-with-ancestor] with a prefix argument, and it will
-; prompt you for the file name.  Then exiting will write the merge
-; buffer to the file, unless "q" is given a prefix argument.
-; 
-; The "i a" and "i b" commands now work in fast mode.
-; 
-; The modifications that Emerge makes to save-buffer and write-file are
-; described.
-; 
-; Emerge now handles merging narrowed buffers correctly.
-; 
-; Emerge now isn't fooled when the buffer visiting a file is not the
-; same as the file on disk.
-; 
-; - Starting
-; 
-; To start Emerge, you must run one of four commands:
-; 
-;      emerge-files
-;      emerge-files-with-ancestor
-;      emerge-buffers
-;      emerge-buffers-with-ancestor
-; 
-; The "files" versions prompt you for two file names (the "A" and "B"
-; files), the "buffers" versions prompt you for two buffer names (the
-; "A" and "B" buffers).  Emerge then runs a "diff" of the two entities
-; (emerge-buffers writes the buffers into temporary files for input to
-; diff) and digests the output to form a list of the differences between
-; the two files.  Then three buffers are set up: two containing the
-; entities (emerge-files does a find-file (C-x C-f) on the files to get
-; them into buffers), and one, the "merge buffer", which contains the
-; working copy of the merged file that you are constructing.  The three
-; buffers are put up in a nice three-window display, showing the A and B
-; buffers in the upper half and the merge buffer in the lower half.
-; 
-; The versions of the command that say "with-ancestor" ask for a third
-; name, that of an entity which is a common ancestor from which the
-; versions being merged were derived.  These commands use "diff3" to
-; compare all three versions.  If one version of a difference agrees
-; with the ancestor, then it is presumed that the other version is the
-; "correct" version, and is said to be "preferred".
-; 
-; (Note that if you use emerge-files, Emerge attempts to make sure that
-; file on disk and the file in the buffer are the same.  If the file on
-; disk has been changed, Emerge offers to revert the buffer.  If the
-; buffer has been modified, Emerge offers to save the buffer.  If the
-; user declines the offer, or if the file on disk and the buffer have
-; both been modified, Emerge aborts with an error message.  Emerge is
-; careful to widen the buffers containing the files if they have been
-; narrowed.  If you use emerge-buffers, the buffers are not widened --
-; only the visible portion is used.)
-; 
-; During the merge, the A and B buffers are read-only, so you don't
-; damage them.  (This is because the A and B versions of the differences
-; are extracted from these buffers.)  When you quit the merge, the
-; read-only/read-write status and modified flag on the A and B buffers
-; are restored.  In addition, auto-saving of the A and B buffers is
-; suppressed during the merge.  This is because Emerge modifies the A
-; and B buffers to point out the text of the differences, and it would
-; be useless to save these changes.  (Just before suppressing
-; auto-saving, Emerge forces an auto-save.)
-; 
-; If you give a prefix argument to emerge-files or
-; emerge-files-with-ancestor, it prompts you for another file name,
-; which is the file into which the merged file is to be written when you
-; exit Emerge.  The output file name defaults to the A file name.  If
-; you successfully quit Emerge, the merge buffer will be written to the
-; output file, and the buffers for the A, B, and ancestor buffers will
-; be deleted (if they exist and are not modified).  If you abort Emerge,
-; the merge buffer will not be written and the buffers will not be
-; deleted.
-; 
-; You can have any number of merges going at once -- just don't use any
-; one buffer as input to more than one merge at once, since that will
-; cause the read-only/modified/auto-save status save-and-restore to
-; screw up.
-; 
-; Beware that when Emerge starts up, it does a diff or diff3 of the
-; files, which can take many minutes for long files with many
-; differences.  Emacs can't do anything else until diff finishes.
-; 
-; If diff or diff3 produces error messages, Emerge will beep and display
-; the error messages instead of the merge buffer.  There will be a
-; message in the echo area giving the name of the merge buffer.  Note
-; that this is really just an informational message -- you still have
-; switch to the merge buffer and abort the merge to restore the
-; conditions before you ran Emerge.  (Emerge considers any output line
-; that does not match the regexp emerge-diff/diff3-ok-lines to be an
-; error message.)
-; 
-; After the merge has been set up, Emerge runs the hooks in
-; emerge-startup-hooks.
-; 
-; - Merging
-; 
-; Once you have started the merge, you manipulate the merge buffer with
-; special commands issued in the merge buffer.  You may also edit the
-; buffer with ordinary Emacs commands.  Emerge keeps track of each
-; difference between the A and B buffers and the corresponding section
-; of the merge buffer.  Initially, all differences show the A version,
-; except those for which B is preferred (because A agrees with the
-; ancestor), which show the B version.  Emerge always has its attention
-; focused on one particular difference, which is marked off in the three
-; buffers by "vvvvvvvvvvvvvvvvvvvv" above and "^^^^^^^^^^^^^^^^^^^^"
-; below.  The number of the difference is shown in the mode line.
-; 
-; A merge buffer can be in two modes: "fast" mode and "edit" mode.  In
-; fast mode, emerge commands are single characters, and ordinary Emacs
-; commands are disabled.  This makes Emerge operations fast, but
-; prevents you from doing more than selecing the A or the B version of
-; differences.  In edit mode, all emerge commands must be prefixed with
-; C-c, and all (non-conflicting) Emacs commands are available.  This
-; allows editing the merge buffer, but slows down Emerge operations.
-; Edit and fast modes are indicated by "F" and "E" in the minor modes in
-; the mode line.
-; 
-; The Emerge commands are:
-; 
-;      p       go to the previous difference
-;      n       go to the next difference
-;      a       select the A version of this difference
-;      b       select the B version of this difference
-;      j       go to a particular difference (prefix argument
-;              specifies which difference) (0j suppresses display of
-;              the flags)
-;      q       quit - finish the merge*
-;      f       go into fast mode
-;      e       go into edit mode
-;      s a     set/clear auto-advance mode*
-;      s s     set/clear skip-prefers mode*
-;      l       recenter (C-l) all three windows*
-;      - and 0 through 9
-;              prefix numeric arguments
-;      d a     select the A version as the default from here down in
-;              the merge buffer*
-;      d b     select the B version as the default from here down in
-;              the merge buffer*
-;      c a     copy the A version of the difference into the kill
-;              ring
-;      c b     copy the B version of the difference into the kill
-;              ring
-;      i a     insert the A version of the difference at the point
-;      i b     insert the B version of the difference at the point
-;      m       put the point and mark around the difference region
-;      ^       scroll-down (like M-v) the three windows*
-;      v       scroll-up (like C-v) the three windows*
-;      <       scroll-left (like C-x <) the three windows*
-;      >       scroll-right (like C-x >) the three windows*
-;      |       reset horizontal scroll on the three windows*
-;      x 1     shrink the merge window to one line (use C-u l to restore it
-;              to full size)
-;      x a     find the difference containing a location in the A buffer*
-;      x b     find the difference containing a location in the B buffer*
-;      x c     combine the two versions of this difference*
-;      x C     combine the two versions of this difference, using a
-;              register's value as the template*
-;      x d     find the difference containing a location in the merge buffer*
-;      x f     show the files/buffers Emerge is operating on in Help window
-;              (use C-u l to restore windows)
-;      x j     join this difference with the following one
-;              (C-u x j joins this difference with the previous one)
-;      x l     show line numbers of points in A, B, and merge buffers
-;      x m     change major mode of merge buffer*
-;      x s     split this difference into two differences
-;              (first position the point in all three buffers to the places
-;              to split the difference)
-;      x t     trim identical lines off top and bottom of difference
-;              (such lines occur when the A and B versions are
-;              identical but differ from the ancestor version)
-;      x x     set the template for the x c command*
-; 
-; * - more details on these commands are given below
-; 
-; emerge-version is a variable giving the version number of Emerge.  It
-; is also a function which displays emerge-version (when called
-; interactively) or returns it (when called from a program).
-; 
-; - Differences and their states
-; 
-; A difference can have one of seven states:
-; 
-; A:  the difference is showing the A version.
-; 
-; B:  the difference is showing the B version.
-; 
-; default-A and default-B: the difference is showing the A or B state,
-; but has never been selected by the user.  All differences start in the
-; default-A state (and thus the merge buffer is a copy of the A buffer),
-; except those for which one buffer or another is preferred.  When the
-; user selects the difference, it changes to the A or B state.
-; 
-; prefer-A and prefer-B: the difference is showing the A or B state.  In
-; addition, the other buffer (that is, for prefer-A, the B buffer; for
-; prefer-B, the A buffer) agrees with the ancestor buffer.  Thus,
-; presumably, the displayed version is the correct one.  The "a" and "b"
-; commands override these states, and turn them into the A and B states.
-; 
-; combined: the difference is showing a combination of the A and B
-; states that was constructed by the "x c" or "x C" commands.  Since
-; this state is neither the A or B states, the "a" and "b" commands
-; won't alter the difference unless they are given a prefix argument.
-; 
-; The state of the currently selected difference is shown in the mode
-; line of the merge window:
-; 
-;      state           display
-; 
-;      A               A
-;      B               B
-;      prefer-A        A*
-;      prefer-B        B*
-;      combined        comb
-; 
-; - Select default commands (d a and d b)
-; 
-; The d a and d b commands change all default-A's to default-B's (or
-; vice-versa) from the selected difference on down to the end of the
-; file to default-A or default-B, respectively.  (Since a difference
-; that has been selected can not have state default-A or default-B, it
-; will never be affected by d a or d b.  This leads to the unexpected
-; result that d a or d b never affects the difference selected at the
-; moment, but prevents differences that you have already looked at from
-; changing unexpectedly.)
-; 
-; If you work your way down from the top of the file, using d a and d b
-; at judicious points, you can effectivly make the A version the default
-; for some sections of the merge buffer and the B version the default
-; for others.
-; 
-; - Exiting (q)
-; 
-; The quit command finishes the merge session by restoring the state of
-; the A and B buffers and removing the markers around the currently
-; selected difference.  It also disables the Emerge commands in the
-; merge buffer, since executing them later could damage the contents of
-; the various buffers.
-; 
-; The action of "q" depends on how Emerge was started and whether "q"
-; was given a prefix argument.  If there was no prefix argument, it is
-; considered a "successful" finish.  If there was a prefix argument, it
-; is considered an "unsuccessful" finish.  In either case, you are asked
-; to cofirm the exit, and the confirmation message tells which sort of
-; exit you are confirming.
-; 
-; If Emerge was started by some other process, success/failure is
-; reported to the caller.
-; 
-; If Emerge was started with emerge-files or emerge-files-with-ancestor,
-; if a prefix argument was given to that command, then you specified a
-; file into which the merge is to be written.  A successful exit writes
-; the merge into the output file and then kills the A, B, and ancestor
-; buffers (so they aren't lying around to confuse you, since they
-; probably all have similar names).
-; 
-; - Auto-advance mode (s a)
-; 
-; If auto-advance mode is set, the "a" and "b" commands perform an "n"
-; (select next difference) afterward.  When auto-advance mode is set,
-; it is indicated by "A" in the minor modes in the mode line.
-; "s a" with a positive argument sets auto-advance, with a non-positive
-; argument clears it, and with no argument toggles it.
-; 
-; - Skip-prefers mode (s s)
-; 
-; If skip-prefers mode is set, the "n" and "p" commands skip over
-; differences with states prefer-A and prefer-B.  Thus you will only see
-; differences for which one version isn't presumed "correct".  When
-; skip-prefers mode is set, it is indicated by "S" in the minor modes in
-; the mode line.  "s s" with a positive argument sets auto-advance, with
-; a non-positive argument clears it, and with no argument toggles it.
-; 
-; - Recenter (l)
-; 
-; The Emerge "l" command causes the selected difference to be brought
-; into view in the three windows, or at least, whichever of the three
-; merge buffers are visible at the moment.  If a prefix argument is
-; given, then the original three-window display is set up before the
-; difference texts are shown.
-; 
-; - Scrolling the text (^, v, <, >, and |)
-; 
-; Emerge has several commands which scroll all three windows by the same
-; amount, thus allowing you to easily compare the versions of the text.
-; The commands are "^" (scroll-up), "v" (scroll-down), "<"
-; (scroll-left), ">" (scroll-right), and "|" (reset horizontal
-; scrolling).  (Remember that Emacs names scrolling commands by the
-; motion of the text with respect to the window, so C-v is called
-; "scroll-up".)
-; 
-; If these commands (except "|") are given an argument, that is the
-; number of lines or characters by which the windows are scrolled.
-; Otherwise, the amount of motion is computed based on the dimensions of
-; the merge buffer window -- the height of the merge buffer window
-; (minus next-screen-context-lines), or half the width of the merge
-; buffer window.  (The A and B version windows are assumed to be as high
-; as the merge window, but half as wide.)  If the argument is just `C-u
-; -', then the scrolling is half the default amount.
-; 
-; - Finding the difference at or near a location (x d, x a, and x b)
-; 
-; The "x d" command selects the difference containing the current point
-; in the merge buffer.  If there is no difference containing the point,
-; an error is given.  An argument can be given to the command to change
-; this behavior: if the argument is positive (e.g., C-u), the next
-; following difference is selected; if the argument is negative (e.g.,
-; C-u -), the previous difference is selected.
-; 
-; The "x a" and "x b" commands select the difference containing the
-; current point in the A and B buffers, respectively.  Otherwise, they
-; act like the "x d" command.  Note that although the point used in the
-; commands is not the merge buffer point, the commands can only be
-; issued in the merge buffer, because it is the only buffer with the
-; Emerge keymap.
-; 
-; - Combining the two versions (x c, x C, and x x)
-; 
-; Sometimes one wants to combine the two versions of a difference.  For
-; instance, when merging two versions of a program, one wants to make
-; something like this:
-; 
-;      #ifdef NEW
-;              ...new version of code...
-;      #else /* NEW */
-;              ...old version of code...
-;      #endif /* NEW */
-; 
-; The "x c" command will make such a combined version.  (Note that any
-; combined version is not the same as either the A or B versions, and so
-; the "a" and "b" commands will refuse to alter it unless they are given
-; a prefix argument.)  The combination is made under control of a
-; template, which is a character string with the following
-; interpolations:
-; 
-;      %a      the A version of the difference
-;      %b      the B version of the difference
-;      %%      the character '%'
-; 
-; Thus, the template used above is 
-; 
-;      #ifdef NEW\n%b#else /* NEW */\n%a#endif /* NEW */\n
-; 
-; (using \n here to represent newlines).  The template is stored in the
-; variable emerge-combine-versions-template, and its initial value is
-; the one given above.  The template can be set (from the current
-; region) by the "x x" command.  (Be careful to get the newlines in the
-; template in the right places!)  ("x x" was chosen by analogy with "C-x
-; x".)  ("x x" is only available in the merge buffer, of course.
-; Elsewhere, M-x emerge-set-combine-versions-template can be used.)  If
-; "x x" is given a prefix argument, emerge-combine-versions-template is
-; localized in the merge buffer before its value is set, so the "x x"
-; command's effect (and the effect of any later "x x" command in the
-; merge buffer) is only on the merge buffer.
-; 
-; The "x C" command is like "x c", but it prompts for a character
-; which is the register whose value is to be used as the template.
-; This allows one to use multiple templates conveniently.
-; 
-; - Changing the major mode of the edit buffer (x m)
-; 
-; The "x m" command prompts for the name of a major-mode-setting command
-; and executes it.  Ordinarily, major-mode-setting commands change the
-; mode line and local keymap, so the "x m" command then resets the
-; Emerge mode line and the fast or edit mode local keymap, as
-; appropriate.
-; 
-; If you have already changed the major mode of the merge buffer and
-; lost the Emerge keymap, you can use M-x emerge-set-merge-mode to
-; execute this command.
-; 
-; Beware that "x m" accepts any command name, not just
-; major-mode-setting commands.
-; 
-; - Writing the merge buffer manually
-; 
-; Emerge places a wrapper (emerge-query-and-call) on the key bindings of
-; save-buffer (usually "C-x C-s") and write-file (usually "C-x C-w"), in
-; order to protect the user from writing out the merge before it is
-; finished.  Emerge-query-and-call asks the user if he is sure he wants
-; to write out the incomplete merge.  If he answers yes, the buffer is
-; written out.  The flags are suppressed while the write is being done.
-; As a result of this, the displayed portions of the buffers are
-; recentered (equivalent to "l").
-; 
-; - Running Emerge standalone
-; 
-; If you invoke emacs with the following arguments, you can execute
-; Emerge as a standalone program:
-; 
-;      emacs -l emerge -f emerge-files-command file-a file-b file-out
-; 
-;      emacs -l emerge -f emerge-files-with-ancestor-command
-;              file-a file-b file-ancestor file-out
-; 
-; When the user gives the "q" (quit) command, Emerge will write out the
-; merge buffer in file-out and terminate Emacs.  If a prefix argument is
-; given, Emacs will terminate with an unsuccessful return code (1), if
-; not, it will terminate with a successful return code (0).
-; 
-; - Invoking Emerge remotely
-; 
-; If you use the Emacs client/server code that supports remote
-; execution, then you can invoke Emerge remotely by executing one of the
-; Lisp calls:
-; 
-;      (emerge-files-remote "file A" "file B" "output file")
-; 
-;      (emerge-files-with-ancestor-remote "file A" "file B"
-;              "ancestor file" "output file")
-; 
-; Returning a successful/unsuccessful return code is not yet supported
-; by the Emacs client/server code.
-; 
-; Beware that in systems of networked workstations, even though all user
-; directories are shared between all the workstations, the /tmp
-; directory on each workstation is not shared, so writing files into
-; /tmp and then remotely invoking Emerge is not likely to work.
-; 
-; - Effect of merge flags on indenting code
-; 
-; The presence of the flags confuses the indentation code of C and
-; Emacs-Lisp modes.  Starting the flag strings
-; (emerge-{before,after}-flag) with '#' (for C) or ';' (for Lisp)
-; prevents the indentation code from noticing the flags.  Remember to
-; change the flag strings before loading Emerge, or to execute
-; emerge-new-flags after changing them.  But never change the flag
-; strings while a merge is being performed.
-; 
-; - Autoloading
-; 
-; The following autoloads will make all top-level Emerge files
-; autoloading.  Make sure that "emerge" is in a directory on load-path.
-; 
-; (autoload 'emerge-files "emerge"
-;        "Run Emerge on two files."
-;        t)
-; (autoload 'emerge-files-with-ancestor "emerge"
-;        "Run Emerge on two files, giving another file as the ancestor."
-;        t)
-; (autoload 'emerge-buffers "emerge"
-;        "Run Emerge on two buffers."
-;        t)
-; (autoload 'emerge-buffers-with-ancestor "emerge"
-;        "Run Emerge on two buffers, giving another buffer as the ancestor."
-;        t)
-; (autoload 'emerge-files-command "emerge")
-; (autoload 'emerge-files-with-ancestor-command "emerge")
-; (autoload 'emerge-files-remote "emerge")
-; (autoload 'emerge-files-with-ancestor-remote "emerge")
-; 
-; ================================================================
-
-;; Declare that we've got the subsystem loaded
-;; LCD Archive Entry:
-;; emerge|Dale R. Worley|drw@math.mit.edu
-;; |File merge
-;; |91-12-13|version 4|~/packages/emerge.el.Z
+;;; emerge.el --- merge diffs under Emacs control
+
+;;; The author has placed this file in the public domain.
+
+;; This file is part of GNU Emacs.
+
+;; Author: Dale R. Worley <worley@world.std.com>
+;; Version: 5fsf
+;; Keywords: unix, tools
+
+;; This software was created by Dale R. Worley and is
+;; distributed free of charge.  It is placed in the public domain and
+;; permission is granted to anyone to use, duplicate, modify and redistribute
+;; it provided that this notice is attached.
+
+;; Dale R. Worley provides absolutely NO WARRANTY OF ANY KIND
+;; with respect to this software.  The entire risk as to the quality and
+;; performance of this software is with the user.  IN NO EVENT WILL DALE
+;; R. WORLEY BE LIABLE TO ANYONE FOR ANY DAMAGES ARISING OUT THE
+;; USE OF THIS SOFTWARE, INCLUDING, WITHOUT LIMITATION, DAMAGES RESULTING FROM
+;; LOST DATA OR LOST PROFITS, OR FOR ANY SPECIAL, INCIDENTAL OR CONSEQUENTIAL
+;; DAMAGES.
+
+;;; Commentary:
+
+;;; Code:
+
+;;;###autoload
+(defvar menu-bar-emerge-menu (make-sparse-keymap "Emerge"))
+;;;###autoload (fset 'menu-bar-emerge-menu (symbol-value 'menu-bar-emerge-menu))
+
+;;;###autoload (define-key menu-bar-emerge-menu [emerge-merge-directories]
+;;;###autoload   '("Merge Directories..." . emerge-merge-directories))
+;;;###autoload (define-key menu-bar-emerge-menu [emerge-revisions-with-ancestor]
+;;;###autoload   '("Revisions with Ancestor..." . emerge-revisions-with-ancestor))
+;;;###autoload (define-key menu-bar-emerge-menu [emerge-revisions]
+;;;###autoload   '("Revisions..." . emerge-revisions))
+;;;###autoload (define-key menu-bar-emerge-menu [emerge-files-with-ancestor]
+;;;###autoload   '("Files with Ancestor..." . emerge-files-with-ancestor))
+;;;###autoload (define-key menu-bar-emerge-menu [emerge-files]
+;;;###autoload   '("Files..." . emerge-files))
+;;;###autoload (define-key menu-bar-emerge-menu [emerge-buffers-with-ancestor]
+;;;###autoload   '("Buffers with Ancestor..." . emerge-buffers-with-ancestor))
+;;;###autoload (define-key menu-bar-emerge-menu [emerge-buffers]
+;;;###autoload   '("Buffers..." . emerge-buffers))
+
+;; There aren't really global variables, just dynamic bindings
+(defvar A-begin)
+(defvar A-end)
+(defvar B-begin)
+(defvar B-end)
+(defvar diff)
+(defvar diff-vector)
+(defvar merge-begin)
+(defvar merge-end)
+(defvar template)
+(defvar valid-diff)
 
 ;;; Macros
 
 (defmacro emerge-eval-in-buffer (buffer &rest forms)
   "Macro to switch to BUFFER, evaluate FORMS, returns to original buffer.
-Differs from  save-excursion  in that it doesn't save the point and mark."
-  (` (let ((StartBuffer (current-buffer)))
+Differs from `save-excursion' in that it doesn't save the point and mark."
+  `(let ((StartBuffer (current-buffer)))
     (unwind-protect
-       (progn
-         (set-buffer (, buffer))
-         (,@ forms))
-      (set-buffer StartBuffer)))))
-
-(defmacro emerge-defvar-local (var value doc) 
-  "Defines SYMBOL as an advertised variable.  Performs a defvar, then
-executes make-variable-buffer-local on the variable.  Also sets the
-'preserved' property, so that kill-all-local-variables (called by major-mode
-setting commands) won't destroy Emerge control variables."
-  (` (progn
-       (defvar (, var) (, value) (, doc))
-       (make-variable-buffer-local '(, var))
-       (put '(, var) 'preserved t))))
+         (progn
+           (set-buffer ,buffer)
+           ,@forms)
+      (set-buffer StartBuffer))))
+
+(defmacro emerge-defvar-local (var value doc)
+  "Defines SYMBOL as an advertised variable.
+Performs a defvar, then executes `make-variable-buffer-local' on
+the variable.  Also sets the `preserved' property, so that
+`kill-all-local-variables' (called by major-mode setting commands)
+won't destroy Emerge control variables."
+  `(progn
+    (defvar ,var ,value ,doc)
+    (make-variable-buffer-local ',var)
+    (put ',var 'preserved t)))
 
 ;; Add entries to minor-mode-alist so that emerge modes show correctly
-(setq emerge-minor-modes-list '((emerge-mode " Emerge")
-                               (emerge-fast-mode " F")
-                               (emerge-edit-mode " E")
-                               (emerge-auto-advance " A")
-                               (emerge-skip-prefers " S")))
+(defvar emerge-minor-modes-list
+  '((emerge-mode " Emerge")
+    (emerge-fast-mode " F")
+    (emerge-edit-mode " E")
+    (emerge-auto-advance " A")
+    (emerge-skip-prefers " S")))
 (if (not (assq 'emerge-mode minor-mode-alist))
     (setq minor-mode-alist (append emerge-minor-modes-list
                                   minor-mode-alist)))
 
 ;; We need to define this function so describe-mode can describe Emerge mode.
 (defun emerge-mode ()
-  "Emerge mode is used by the Emerge file-merging package.  It is entered only
-through one of the functions:
-       emerge-files
-       emerge-files-with-ancestor
-       emerge-buffers
-       emerge-buffers-with-ancestor
-       emerge-files-command
-       emerge-files-with-ancestor-command
-       emerge-files-remote
-       emerge-files-with-ancestor-remote
+  "Emerge mode is used by the Emerge file-merging package.
+It is entered only through one of the functions:
+       `emerge-files'
+       `emerge-files-with-ancestor'
+       `emerge-buffers'
+       `emerge-buffers-with-ancestor'
+       `emerge-files-command'
+       `emerge-files-with-ancestor-command'
+       `emerge-files-remote'
+       `emerge-files-with-ancestor-remote'
 
 Commands:
 \\{emerge-basic-keymap}
-Commands must be prefixed by \\<emerge-fast-keymap>\\[emerge-basic-keymap] in 'edit' mode, but can be invoked directly
-in 'fast' mode.")
+Commands must be prefixed by \\<emerge-fast-keymap>\\[emerge-basic-keymap] in `edit' mode,
+but can be invoked directly in `fast' mode.")
 
-(defvar emerge-version "4"
+(defvar emerge-version "5fsf"
   "The version of Emerge.")
 
 (defun emerge-version ()
-  "Return string describing the version of Emerge.  When called interactively,
-displays the version."
+  "Return string describing the version of Emerge.
+When called interactively, displays the version."
   (interactive)
   (if (interactive-p)
       (message "Emerge version %s" (emerge-version))
@@ -627,53 +121,165 @@ displays the version."
 
 ;;; Emerge configuration variables
 
+(defgroup emerge nil
+  "Merge diffs under Emacs control."
+  :group 'tools)
+
 ;; Commands that produce difference files
 ;; All that can be configured is the name of the programs to execute
 ;; (emerge-diff-program and emerge-diff3-program) and the options
 ;; to be provided (emerge-diff-options).  The order in which the file names
 ;; are given is fixed.
 ;; The file names are always expanded (see expand-file-name) before being
-;; passed to diff, thus they need not be invoked under a shell that 
-;; understands '~'.
+;; passed to diff, thus they need not be invoked under a shell that
+;; understands `~'.
 ;; The code which processes the diff/diff3 output depends on all the
 ;; finicky details of their output, including the somewhat strange
 ;; way they number lines of a file.
-(defvar emerge-diff-program "diff"
-  "*Name of the program which compares two files.")
-(defvar emerge-diff3-program "diff3"
-  "*Name of the program which compares an ancestor file (first argument)
-and two variant files (second and third arguments).")
-(defvar emerge-diff-options ""
-  "*Options to be passed to emerge-diff/diff3-program.")
-(defvar emerge-match-diff-line (let ((x "\\([0-9]+\\)\\(\\|,\\([0-9]+\\)\\)"))
-                                (concat "^" x "\\([acd]\\)" x "$"))
-  "*Pattern to match lines produced by diff that describe differences (as
-opposed to lines from the source files).")
-(defvar emerge-diff-ok-lines
+(defcustom emerge-diff-program "diff"
+  "*Name of the program which compares two files."
+  :type 'string
+  :group 'emerge)
+(defcustom emerge-diff3-program "diff3"
+  "*Name of the program which compares three files.
+Its arguments are the ancestor file and the two variant files."
+  :type 'string
+  :group 'emerge)
+(defcustom emerge-diff-options ""
+  "*Options to pass to `emerge-diff-program' and `emerge-diff3-program'."
+  :type 'string
+  :group 'emerge)
+(defcustom emerge-match-diff-line
+  (let ((x "\\([0-9]+\\)\\(\\|,\\([0-9]+\\)\\)"))
+    (concat "^" x "\\([acd]\\)" x "$"))
+  "*Pattern to match lines produced by diff that describe differences.
+This is as opposed to lines from the source files."
+  :type 'regexp
+  :group 'emerge)
+(defcustom emerge-diff-ok-lines-regexp
   "^\\([0-9,]+[acd][0-9,]+$\\|[<>] \\|---\\)"
-  "*Regexp that matches normal output lines from  emerge-diff-program .
-Lines that do not match are assumed to be error output.")
-(defvar emerge-diff3-ok-lines
+  "*Regexp that matches normal output lines from `emerge-diff-program'.
+Lines that do not match are assumed to be error messages."
+  :type 'regexp
+  :group 'emerge)
+(defcustom emerge-diff3-ok-lines-regexp
   "^\\([1-3]:\\|====\\|  \\)"
-  "*Regexp that matches normal output lines from  emerge-diff3-program .
-Lines that do not match are assumed to be error output.")
+  "*Regexp that matches normal output lines from `emerge-diff3-program'.
+Lines that do not match are assumed to be error messages."
+  :type 'regexp
+  :group 'emerge)
+
+(defcustom emerge-rcs-ci-program "ci"
+  "*Name of the program that checks in RCS revisions."
+  :type 'string
+  :group 'emerge)
+(defcustom emerge-rcs-co-program "co"
+  "*Name of the program that checks out RCS revisions."
+  :type 'string
+  :group 'emerge)
+
+(defcustom emerge-process-local-variables nil
+  "*Non-nil if Emerge should process local-variables lists in merge buffers.
+\(You can explicitly request processing the local-variables
+by executing `(hack-local-variables)'.)"
+  :type 'boolean
+  :group 'emerge)
+(defcustom emerge-execute-line-deletions nil
+  "*If non-nil: `emerge-execute-line' makes no output if an input was deleted.
+It concludes that an input version has been deleted when an ancestor entry
+is present, only one A or B entry is present, and an output entry is present.
+If nil: In such circumstances, the A or B file that is present will be
+copied to the designated output file."
+  :type 'boolean
+  :group 'emerge)
+
+(defcustom emerge-before-flag "vvvvvvvvvvvvvvvvvvvv\n"
+  "*Flag placed above the highlighted block of code.  Must end with newline.
+Must be set before Emerge is loaded, or  emerge-new-flags  must be run
+after setting."
+  :type 'string
+  :group 'emerge)
+(defcustom emerge-after-flag "^^^^^^^^^^^^^^^^^^^^\n"
+  "*Flag placed below the highlighted block of code.  Must end with newline.
+Must be set before Emerge is loaded, or  emerge-new-flags  must be run
+after setting."
+  :type 'string
+  :group 'emerge)
+
+;; Hook variables
+
+(defcustom emerge-startup-hook nil
+  "*Hook to run in the merge buffer after the merge has been set up."
+  :type 'hook
+  :group 'emerge)
+(defcustom emerge-select-hook nil
+  "*Hook to run after a difference has been selected.
+The variable `n' holds the (internal) number of the difference."
+  :type 'hook
+  :group 'emerge)
+(defcustom emerge-unselect-hook nil
+  "*Hook to run after a difference has been unselected.
+The variable `n' holds the (internal) number of the difference."
+  :type 'hook
+  :group 'emerge)
+
+;; Variables to control the default directories of the arguments to
+;; Emerge commands.
+
+(defcustom emerge-default-last-directories nil
+  "*If nil, default dir for filenames in emerge is `default-directory'.
+If non-nil, filenames complete in the directory of the last argument of the
+same type to an `emerge-files...' command."
+  :type 'boolean
+  :group 'emerge)
+
+(defvar emerge-last-dir-A nil
+  "Last directory for the first file of an `emerge-files...' command.")
+(defvar emerge-last-dir-B nil
+  "Last directory for the second file of an `emerge-files...' command.")
+(defvar emerge-last-dir-ancestor nil
+  "Last directory for the ancestor file of an `emerge-files...' command.")
+(defvar emerge-last-dir-output nil
+  "Last directory for the output file of an `emerge-files...' command.")
+(defvar emerge-last-revision-A nil
+  "Last RCS revision used for first file of an `emerge-revisions...' command.")
+(defvar emerge-last-revision-B nil
+  "Last RCS revision used for second file of an `emerge-revisions...' command.")
+(defvar emerge-last-revision-ancestor nil
+  "Last RCS revision used for ancestor file of an `emerge-revisions...' command.")
+
+(defvar emerge-before-flag-length)
+(defvar emerge-before-flag-lines)
+(defvar emerge-before-flag-match)
+(defvar emerge-after-flag-length)
+(defvar emerge-after-flag-lines)
+(defvar emerge-after-flag-match)
+(defvar emerge-diff-buffer)
+(defvar emerge-diff-error-buffer)
+(defvar emerge-prefix-argument)
+(defvar emerge-file-out)
+(defvar emerge-exit-func)
+(defvar emerge-globalized-difference-list)
+(defvar emerge-globalized-number-of-differences)
 
 ;; The flags used to mark differences in the buffers.
 
 ;; These function definitions need to be up here, because they are used
 ;; during loading.
 (defun emerge-new-flags ()
-  "Function to be called after emerge-{before,after}-flag are changed to
-compute values that depend on the flags."
+  "Function to be called after `emerge-{before,after}-flag'.
+This is called after these functions are changed to compute values that
+depend on the flags."
   (setq emerge-before-flag-length (length emerge-before-flag))
   (setq emerge-before-flag-lines
-       (count-matches-string emerge-before-flag "\n"))
+       (emerge-count-matches-string emerge-before-flag "\n"))
   (setq emerge-before-flag-match (regexp-quote emerge-before-flag))
   (setq emerge-after-flag-length (length emerge-after-flag))
   (setq emerge-after-flag-lines
-       (count-matches-string emerge-after-flag "\n"))
+       (emerge-count-matches-string emerge-after-flag "\n"))
   (setq emerge-after-flag-match (regexp-quote emerge-after-flag)))
-(defun count-matches-string (string regexp)
+
+(defun emerge-count-matches-string (string regexp)
   "Return the number of matches in STRING for REGEXP."
   (let ((i 0)
        (count 0))
@@ -682,62 +288,65 @@ compute values that depend on the flags."
       (setq i (match-end 0)))
     count))
 
-(defvar emerge-before-flag "vvvvvvvvvvvvvvvvvvvv\n"
-  "*Flag placed above the highlighted block of code.  Must end with newline.
-Must be set before Emerge is loaded, or  emerge-new-flags  must be run
-after setting.")
-(defvar emerge-after-flag "^^^^^^^^^^^^^^^^^^^^\n"
-  "*Flag placed below the highlighted block of code.  Must end with newline.
-Must be set before Emerge is loaded, or  emerge-new-flags  must be run
-after setting.")
-
 ;; Calculate dependent variables
 (emerge-new-flags)
 
-(defvar emerge-min-visible-lines 3
+(defcustom emerge-min-visible-lines 3
   "*Number of lines that we want to show above and below the flags when we are
-displaying a difference.")
-
-(defvar emerge-temp-file-prefix
-  (let ((env (getenv "TMPDIR"))
-       d)
-    (setq d (if (and env (> (length env) 0))
-               env
-             "/tmp"))
-    (if (= (aref d (1- (length d))) ?/)
-       (setq d (substring d 0 -1)))
-    (concat d "/emerge"))
-  "*Prefix to put on Emerge temporary file names.
-Do not start with '~/' or '~user-name/'.")
+displaying a difference."
+  :type 'integer
+  :group 'emerge)
 
-(defvar emerge-temp-file-mode 384      ; u=rw only
-  "*Mode for Emerge temporary files.")
-
-(defvar emerge-combine-versions-template
-  "#ifdef NEW\n%b#else /* NEW */\n%a#endif /* NEW */\n"
-  "*Template for  emerge-combine-versions  to combine the two versions.
+(defcustom emerge-temp-file-prefix
+  (expand-file-name "emerge" temporary-file-directory)
+  "*Prefix to put on Emerge temporary file names.
+Do not start with `~/' or `~USERNAME/'."
+  :type 'string
+  :group 'emerge)
+
+(defcustom emerge-temp-file-mode 384   ; u=rw only
+  "*Mode for Emerge temporary files."
+  :type 'integer
+  :group 'emerge)
+
+(defcustom emerge-combine-versions-template
+  "#ifdef NEW\n%b#else /* not NEW */\n%a#endif /* not NEW */\n"
+  "*Template for `emerge-combine-versions' to combine the two versions.
 The template is inserted as a string, with the following interpolations:
        %a      the A version of the difference
        %b      the B version of the difference
-       %%      the character '%'
+       %%      the character `%'
 Don't forget to end the template with a newline.
 Note that this variable can be made local to a particular merge buffer by
-giving a prefix argument to  emerge-set-combine-versions-template .")
+giving a prefix argument to `emerge-set-combine-versions-template'."
+  :type 'string
+  :group 'emerge)
 
 ;; Build keymaps
 
 (defvar emerge-basic-keymap nil
   "Keymap of Emerge commands.
-Directly available in 'fast' mode;
-must be prefixed by \\<emerge-fast-keymap>\\[emerge-basic-keymap] in 'edit' mode.")
+Directly available in `fast' mode;
+must be prefixed by \\<emerge-fast-keymap>\\[emerge-basic-keymap] in `edit' mode.")
 
 (defvar emerge-fast-keymap nil
-  "Local keymap used in Emerge 'fast' mode.
+  "Local keymap used in Emerge `fast' mode.
 Makes Emerge commands directly available.")
 
-(defvar emerge-command-prefix "\C-c"
-  "*Command prefix for Emerge commands in 'edit' mode.
-Must be set before Emerge is loaded.")
+(defvar emerge-options-menu
+  (make-sparse-keymap "Options"))
+
+(defvar emerge-merge-menu
+  (make-sparse-keymap "Merge"))
+
+(defvar emerge-move-menu
+  (make-sparse-keymap "Move"))
+
+(defcustom emerge-command-prefix "\C-c\C-c"
+  "*Command prefix for Emerge commands in `edit' mode.
+Must be set before Emerge is loaded."
+  :type 'string
+  :group 'emerge)
 
 ;; This function sets up the fixed keymaps.  It is executed when the first
 ;; Emerge is done to allow the user maximum time to set up the global keymap.
@@ -751,7 +360,9 @@ Must be set before Emerge is loaded.")
   (define-key emerge-basic-keymap "a" 'emerge-select-A)
   (define-key emerge-basic-keymap "b" 'emerge-select-B)
   (define-key emerge-basic-keymap "j" 'emerge-jump-to-difference)
+  (define-key emerge-basic-keymap "." 'emerge-find-difference)
   (define-key emerge-basic-keymap "q" 'emerge-quit)
+  (define-key emerge-basic-keymap "\C-]" 'emerge-abort)
   (define-key emerge-basic-keymap "f" 'emerge-fast-mode)
   (define-key emerge-basic-keymap "e" 'emerge-edit-mode)
   (define-key emerge-basic-keymap "s" nil)
@@ -775,11 +386,8 @@ Must be set before Emerge is loaded.")
   (define-key emerge-basic-keymap "|" 'emerge-scroll-reset)
   (define-key emerge-basic-keymap "x" nil)
   (define-key emerge-basic-keymap "x1" 'emerge-one-line-window)
-  (define-key emerge-basic-keymap "xa" 'emerge-find-difference-A)
-  (define-key emerge-basic-keymap "xb" 'emerge-find-difference-B)
   (define-key emerge-basic-keymap "xc" 'emerge-combine-versions)
   (define-key emerge-basic-keymap "xC" 'emerge-combine-versions-register)
-  (define-key emerge-basic-keymap "xd" 'emerge-find-difference)
   (define-key emerge-basic-keymap "xf" 'emerge-file-names)
   (define-key emerge-basic-keymap "xj" 'emerge-join-differences)
   (define-key emerge-basic-keymap "xl" 'emerge-line-numbers)
@@ -796,10 +404,84 @@ Must be set before Emerge is loaded.")
   ;; Allow emerge-fast-keymap to be referenced indirectly
   (fset 'emerge-fast-keymap emerge-fast-keymap)
   ;; Suppress write-file and save-buffer
-  (emerge-shadow-key-definition 'write-file 'emerge-query-write-file
-                               (current-global-map) emerge-fast-keymap)
-  (emerge-shadow-key-definition 'save-buffer 'emerge-query-save-buffer
-                               (current-global-map) emerge-fast-keymap))
+  (define-key emerge-fast-keymap [remap write-file] 'emerge-query-write-file)
+  (define-key emerge-fast-keymap [remap save-buffer] 'emerge-query-save-buffer)
+
+  (define-key emerge-basic-keymap [menu-bar] (make-sparse-keymap))
+
+  (define-key emerge-fast-keymap [menu-bar options]
+    (cons "Options" emerge-options-menu))
+  (define-key emerge-fast-keymap [menu-bar merge]
+    (cons "Merge" emerge-merge-menu))
+  (define-key emerge-fast-keymap [menu-bar move]
+    (cons "Move" emerge-move-menu))
+
+  (define-key emerge-move-menu [emerge-scroll-reset]
+    '("Scroll Reset" . emerge-scroll-reset))
+  (define-key emerge-move-menu [emerge-scroll-right]
+    '("Scroll Right" . emerge-scroll-right))
+  (define-key emerge-move-menu [emerge-scroll-left]
+    '("Scroll Left" . emerge-scroll-left))
+  (define-key emerge-move-menu [emerge-scroll-down]
+    '("Scroll Down" . emerge-scroll-down))
+  (define-key emerge-move-menu [emerge-scroll-up]
+    '("Scroll Up" . emerge-scroll-up))
+  (define-key emerge-move-menu [emerge-recenter]
+    '("Recenter" . emerge-recenter))
+  (define-key emerge-move-menu [emerge-mark-difference]
+    '("Mark Difference" . emerge-mark-difference))
+  (define-key emerge-move-menu [emerge-jump-to-difference]
+    '("Jump To Difference" . emerge-jump-to-difference))
+  (define-key emerge-move-menu [emerge-find-difference]
+    '("Find Difference" . emerge-find-difference))
+  (define-key emerge-move-menu [emerge-previous-difference]
+    '("Previous Difference" . emerge-previous-difference))
+  (define-key emerge-move-menu [emerge-next-difference]
+    '("Next Difference" . emerge-next-difference))
+
+
+  (define-key emerge-options-menu [emerge-one-line-window]
+    '("One Line Window" . emerge-one-line-window))
+  (define-key emerge-options-menu [emerge-set-merge-mode]
+    '("Set Merge Mode" . emerge-set-merge-mode))
+  (define-key emerge-options-menu [emerge-set-combine-template]
+    '("Set Combine Template..." . emerge-set-combine-template))
+  (define-key emerge-options-menu [emerge-default-B]
+    '("Default B" . emerge-default-B))
+  (define-key emerge-options-menu [emerge-default-A]
+    '("Default A" . emerge-default-A))
+  (define-key emerge-options-menu [emerge-skip-prefers]
+    '("Skip Prefers" . emerge-skip-prefers))
+  (define-key emerge-options-menu [emerge-auto-advance]
+    '("Auto Advance" . emerge-auto-advance))
+  (define-key emerge-options-menu [emerge-edit-mode]
+    '("Edit Mode" . emerge-edit-mode))
+  (define-key emerge-options-menu [emerge-fast-mode]
+    '("Fast Mode" . emerge-fast-mode))
+
+  (define-key emerge-merge-menu [emerge-abort] '("Abort" . emerge-abort))
+  (define-key emerge-merge-menu [emerge-quit] '("Quit" . emerge-quit))
+  (define-key emerge-merge-menu [emerge-split-difference]
+    '("Split Difference" . emerge-split-difference))
+  (define-key emerge-merge-menu [emerge-join-differences]
+    '("Join Differences" . emerge-join-differences))
+  (define-key emerge-merge-menu [emerge-trim-difference]
+    '("Trim Difference" . emerge-trim-difference))
+  (define-key emerge-merge-menu [emerge-combine-versions]
+    '("Combine Versions" . emerge-combine-versions))
+  (define-key emerge-merge-menu [emerge-copy-as-kill-B]
+    '("Copy B as Kill" . emerge-copy-as-kill-B))
+  (define-key emerge-merge-menu [emerge-copy-as-kill-A]
+    '("Copy A as Kill" . emerge-copy-as-kill-A))
+  (define-key emerge-merge-menu [emerge-insert-B]
+    '("Insert B" . emerge-insert-B))
+  (define-key emerge-merge-menu [emerge-insert-A]
+    '("Insert A" . emerge-insert-A))
+  (define-key emerge-merge-menu [emerge-select-B]
+    '("Select B" . emerge-select-B))
+  (define-key emerge-merge-menu [emerge-select-A]
+    '("Select A" . emerge-select-A)))
+
 
 ;; Variables which control each merge.  They are local to the merge buffer.
 
@@ -856,9 +538,9 @@ the markers.
        default-B       the merge buffer contains the B variant by default,
                        but this difference hasn't been selected yet, so
                        change-default commands can alter it
-       prefer-A        in a three-file merge, the A variant is the prefered
+       prefer-A        in a three-file merge, the A variant is the preferred
                        choice
-       prefer-B        in a three-file merge, the B variant is the prefered
+       prefer-B        in a three-file merge, the B variant is the preferred
                        choice")
 (emerge-defvar-local emerge-current-difference -1
   "The difference that is currently selected.")
@@ -876,30 +558,52 @@ the next difference.")
 (emerge-defvar-local emerge-skip-prefers nil
   "*If non-nil, differences for which there is a preference are automatically
 skipped.")
-(emerge-defvar-local emerge-startup-hooks nil
-  "*Hooks to run in the merge buffer after the merge has been set up.")
-(emerge-defvar-local emerge-quit-hooks nil
+(emerge-defvar-local emerge-quit-hook nil
   "Hooks to run in the merge buffer after the merge has been finished.
-emerge-prefix-argument will be bound to the prefix argument of the emerge-quit
+`emerge-prefix-argument' will hold the prefix argument of the `emerge-quit'
 command.
-This is  not  a user option, since Emerge uses it for its own processing.")
+This is *not* a user option, since Emerge uses it for its own processing.")
 (emerge-defvar-local emerge-output-description nil
-  "Describes output destination of the merge, for the use of
-emerge-file-names.")
+  "Describes output destination of emerge, for `emerge-file-names'.")
 
 ;;; Setup functions for two-file mode.
 
 (defun emerge-files-internal (file-A file-B &optional startup-hooks quit-hooks
-                                    output-file)
+                              output-file)
+  (if (not (file-readable-p file-A))
+      (error "File `%s' does not exist or is not readable" file-A))
+  (if (not (file-readable-p file-B))
+      (error "File `%s' does not exist or is not readable" file-B))
   (let ((buffer-A (find-file-noselect file-A))
        (buffer-B (find-file-noselect file-B)))
+    ;; Record the directories of the files
+    (setq emerge-last-dir-A (file-name-directory file-A))
+    (setq emerge-last-dir-B (file-name-directory file-B))
+    (if output-file
+       (setq emerge-last-dir-output (file-name-directory output-file)))
     ;; Make sure the entire files are seen, and they reflect what is on disk
-    (emerge-eval-in-buffer buffer-A
-                          (widen)
-                          (emerge-verify-file-buffer))
-    (emerge-eval-in-buffer buffer-B
-                          (widen)
-                          (emerge-verify-file-buffer))
+    (emerge-eval-in-buffer
+     buffer-A
+     (widen)
+     (let ((temp (file-local-copy file-A)))
+       (if temp
+          (setq file-A temp
+                startup-hooks
+                (cons `(lambda () (delete-file ,file-A))
+                      startup-hooks))
+           ;; Verify that the file matches the buffer
+           (emerge-verify-file-buffer))))
+    (emerge-eval-in-buffer
+     buffer-B
+     (widen)
+     (let ((temp (file-local-copy file-B)))
+       (if temp
+          (setq file-B temp
+                startup-hooks
+                (cons `(lambda () (delete-file ,file-B))
+                      startup-hooks))
+           ;; Verify that the file matches the buffer
+           (emerge-verify-file-buffer))))
     (emerge-setup buffer-A file-A buffer-B file-B startup-hooks quit-hooks
                  output-file)))
 
@@ -934,11 +638,12 @@ emerge-file-names.")
      (setq emerge-difference-list (emerge-make-diff-list file-A file-B))
      (setq emerge-number-of-differences (length emerge-difference-list))
      (setq emerge-current-difference -1)
-     (setq emerge-quit-hooks quit-hooks)
-     (emerge-remember-buffer-characteristics))
+     (setq emerge-quit-hook quit-hooks)
+     (emerge-remember-buffer-characteristics)
+     (emerge-handle-local-variables))
     (emerge-setup-windows buffer-A buffer-B merge-buffer t)
     (emerge-eval-in-buffer merge-buffer
-                          (run-hooks 'startup-hooks 'emerge-startup-hooks)
+                          (run-hooks 'startup-hooks 'emerge-startup-hook)
                           (setq buffer-read-only t))))
 
 ;; Generate the Emerge difference list between two files
@@ -949,9 +654,11 @@ emerge-file-names.")
    (erase-buffer)
    (shell-command
     (format "%s %s %s %s"
-           emerge-diff-program emerge-diff-options file-A file-B)
+           emerge-diff-program emerge-diff-options
+           (emerge-protect-metachars file-A)
+           (emerge-protect-metachars file-B))
     t))
-  (emerge-prepare-error-list emerge-diff-ok-lines)
+  (emerge-prepare-error-list emerge-diff-ok-lines-regexp)
   (emerge-convert-diffs-to-markers
    emerge-A-buffer emerge-B-buffer emerge-merge-buffer
    (emerge-extract-diffs emerge-diff-buffer)))
@@ -962,20 +669,20 @@ emerge-file-names.")
      diff-buffer
      (goto-char (point-min))
      (while (re-search-forward emerge-match-diff-line nil t)
-       (let* ((a-begin (string-to-int (buffer-substring (match-beginning 1)
-                                                       (match-end 1))))
+       (let* ((a-begin (string-to-number (buffer-substring (match-beginning 1)
+                                                           (match-end 1))))
              (a-end  (let ((b (match-beginning 3))
                            (e (match-end 3)))
                        (if b
-                           (string-to-int (buffer-substring b e))
+                           (string-to-number (buffer-substring b e))
                          a-begin)))
              (diff-type (buffer-substring (match-beginning 4) (match-end 4)))
-             (b-begin (string-to-int (buffer-substring (match-beginning 5)
-                                                       (match-end 5))))
+             (b-begin (string-to-number (buffer-substring (match-beginning 5)
+                                                           (match-end 5))))
              (b-end (let ((b (match-beginning 7))
                           (e (match-end 7)))
                       (if b
-                          (string-to-int (buffer-substring b e))
+                          (string-to-number (buffer-substring b e))
                         b-begin))))
         ;; fix the beginning and end numbers, because diff is somewhat
         ;; strange about how it numbers lines
@@ -1011,21 +718,57 @@ emerge-file-names.")
 ;;; Top-level and setup functions for three-file mode.
 
 (defun emerge-files-with-ancestor-internal (file-A file-B file-ancestor
-                                         &optional startup-hooks quit-hooks
-                                         output-file)
+                                           &optional startup-hooks quit-hooks
+                                           output-file)
+  (if (not (file-readable-p file-A))
+      (error "File `%s' does not exist or is not readable" file-A))
+  (if (not (file-readable-p file-B))
+      (error "File `%s' does not exist or is not readable" file-B))
+  (if (not (file-readable-p file-ancestor))
+      (error "File `%s' does not exist or is not readable" file-ancestor))
   (let ((buffer-A (find-file-noselect file-A))
        (buffer-B (find-file-noselect file-B))
        (buffer-ancestor (find-file-noselect file-ancestor)))
+    ;; Record the directories of the files
+    (setq emerge-last-dir-A (file-name-directory file-A))
+    (setq emerge-last-dir-B (file-name-directory file-B))
+    (setq emerge-last-dir-ancestor (file-name-directory file-ancestor))
+    (if output-file
+       (setq emerge-last-dir-output (file-name-directory output-file)))
     ;; Make sure the entire files are seen, and they reflect what is on disk
-    (emerge-eval-in-buffer buffer-A
-                          (widen)
-                          (emerge-verify-file-buffer))
-    (emerge-eval-in-buffer buffer-B
-                          (widen)
-                          (emerge-verify-file-buffer))
-    (emerge-eval-in-buffer buffer-ancestor
-                          (widen)
-                          (emerge-verify-file-buffer))
+    (emerge-eval-in-buffer
+     buffer-A
+     (widen)
+     (let ((temp (file-local-copy file-A)))
+       (if temp
+          (setq file-A temp
+                startup-hooks
+                (cons `(lambda () (delete-file ,file-A))
+                      startup-hooks))
+           ;; Verify that the file matches the buffer
+           (emerge-verify-file-buffer))))
+    (emerge-eval-in-buffer
+     buffer-B
+     (widen)
+     (let ((temp (file-local-copy file-B)))
+       (if temp
+          (setq file-B temp
+                startup-hooks
+                (cons `(lambda () (delete-file ,file-B))
+                      startup-hooks))
+           ;; Verify that the file matches the buffer
+           (emerge-verify-file-buffer))))
+    (emerge-eval-in-buffer
+     buffer-ancestor
+     (widen)
+     (let ((temp (file-local-copy file-ancestor)))
+       (if temp
+          (setq file-ancestor temp
+                startup-hooks
+                (cons `(lambda () (delete-file ,file-ancestor))
+                      startup-hooks))
+           ;; Verify that the file matches the buffer
+           (emerge-verify-file-buffer))))
     (emerge-setup-with-ancestor buffer-A file-A buffer-B file-B
                                buffer-ancestor file-ancestor
                                startup-hooks quit-hooks output-file)))
@@ -1065,12 +808,13 @@ emerge-file-names.")
           (emerge-make-diff3-list file-A file-B file-ancestor))
      (setq emerge-number-of-differences (length emerge-difference-list))
      (setq emerge-current-difference -1)
-     (setq emerge-quit-hooks quit-hooks)
+     (setq emerge-quit-hook quit-hooks)
      (emerge-remember-buffer-characteristics)
-     (emerge-select-prefer-Bs))
+     (emerge-select-prefer-Bs)
+     (emerge-handle-local-variables))
     (emerge-setup-windows buffer-A buffer-B merge-buffer t)
     (emerge-eval-in-buffer merge-buffer
-                          (run-hooks 'startup-hooks 'emerge-startup-hooks)
+                          (run-hooks 'startup-hooks 'emerge-startup-hook)
                           (setq buffer-read-only t))))
 
 ;; Generate the Emerge difference list between two files with an ancestor
@@ -1082,9 +826,11 @@ emerge-file-names.")
    (shell-command
     (format "%s %s %s %s %s"
            emerge-diff3-program emerge-diff-options
-           file-ancestor file-A file-B)
+           (emerge-protect-metachars file-A)
+           (emerge-protect-metachars file-ancestor)
+           (emerge-protect-metachars file-B))
     t))
-  (emerge-prepare-error-list emerge-diff3-ok-lines)
+  (emerge-prepare-error-list emerge-diff3-ok-lines-regexp)
   (emerge-convert-diffs-to-markers
    emerge-A-buffer emerge-B-buffer emerge-merge-buffer
    (emerge-extract-diffs3 emerge-diff-buffer)))
@@ -1098,14 +844,17 @@ emerge-file-names.")
        (beginning-of-line 2)
        (let ((agreement (buffer-substring (match-beginning 1) (match-end 1))))
         ;; if the A and B files are the same, ignore the difference
-        (if (not (string-equal agreement "1"))
+        (if (not (string-equal agreement "2"))
             (setq list
-                  (cons 
-                   (let ((group-2 (emerge-get-diff3-group "2"))
-                         (group-3 (emerge-get-diff3-group "3")))
-                     (vector (car group-2) (car (cdr group-2))
+                  (cons
+                   (let (group-1 group-3 pos)
+                     (setq pos (point))
+                     (setq group-1 (emerge-get-diff3-group "1"))
+                     (goto-char pos)
+                     (setq group-3 (emerge-get-diff3-group "3"))
+                     (vector (car group-1) (car (cdr group-1))
                              (car group-3) (car (cdr group-3))
-                             (cond ((string-equal agreement "2") 'prefer-A)
+                             (cond ((string-equal agreement "1") 'prefer-A)
                                    ((string-equal agreement "3") 'prefer-B)
                                    (t 'default-A))))
                    list))))))
@@ -1125,16 +874,16 @@ emerge-file-names.")
        ;; it is a "c" group
        (if (match-beginning 2)
            ;; it has two numbers
-           (list (string-to-int
+           (list (string-to-number
                   (buffer-substring (match-beginning 1) (match-end 1)))
-                 (1+ (string-to-int
+                 (1+ (string-to-number
                       (buffer-substring (match-beginning 3) (match-end 3)))))
          ;; it has one number
-         (let ((x (string-to-int
+         (let ((x (string-to-number
                    (buffer-substring (match-beginning 1) (match-end 1)))))
            (list x (1+ x))))
       ;; it is an "a" group
-      (let ((x (1+ (string-to-int
+      (let ((x (1+ (string-to-number
                    (buffer-substring (match-beginning 1) (match-end 1))))))
        (list x x)))))
 
@@ -1147,18 +896,17 @@ emerge-file-names.")
   (interactive
    (let (f)
      (list current-prefix-arg
-          (setq f (read-file-name "File A to merge: " nil nil 'confirm))
-          (read-file-name "File B to merge: " nil nil 'confirm)
+          (setq f (emerge-read-file-name "File A to merge" emerge-last-dir-A
+                                         nil nil t))
+          (emerge-read-file-name "File B to merge" emerge-last-dir-B nil f t)
           (and current-prefix-arg
-               (read-file-name
-                (format "Output file: (default %s) " f)
-                nil f nil)))))
+               (emerge-read-file-name "Output file" emerge-last-dir-output
+                                      f f nil)))))
+  (if file-out
+      (add-hook 'quit-hooks `(lambda () (emerge-files-exit ,file-out))))
   (emerge-files-internal
    file-A file-B startup-hooks
-   (if arg
-       (cons (` (lambda () (emerge-files-exit (, file-out))))
-            quit-hooks)
-     quit-hooks)
+   quit-hooks
    file-out))
 
 ;;;###autoload
@@ -1168,19 +916,19 @@ emerge-file-names.")
   (interactive
    (let (f)
      (list current-prefix-arg
-          (setq f (read-file-name "File A to merge: " nil nil 'confirm))
-          (read-file-name "File B to merge: " nil nil 'confirm)
-          (read-file-name "Ancestor file: " nil nil 'confirm)
+          (setq f (emerge-read-file-name "File A to merge" emerge-last-dir-A
+                                         nil nil t))
+          (emerge-read-file-name "File B to merge" emerge-last-dir-B nil f t)
+          (emerge-read-file-name "Ancestor file" emerge-last-dir-ancestor
+                                 nil f t)
           (and current-prefix-arg
-               (read-file-name
-                (format "Output file: (default %s) " f)
-                nil f nil)))))
+               (emerge-read-file-name "Output file" emerge-last-dir-output
+                                      f f nil)))))
+  (if file-out
+      (add-hook 'quit-hooks `(lambda () (emerge-files-exit ,file-out))))
   (emerge-files-with-ancestor-internal
    file-A file-B file-ancestor startup-hooks
-   (if arg
-       (cons (` (lambda () (emerge-files-exit (, file-out))))
-            quit-hooks)
-     quit-hooks)
+   quit-hooks
    file-out))
 
 ;; Write the merge buffer out in place of the file the A buffer is visiting.
@@ -1205,17 +953,17 @@ emerge-file-names.")
      (write-region (point-min) (point-max) emerge-file-B nil 'no-message))
     (emerge-setup (get-buffer buffer-A) emerge-file-A
                  (get-buffer buffer-B) emerge-file-B
-                 (cons (function (lambda ()
-                                   (delete-file emerge-file-A)
-                                   (delete-file emerge-file-B)))
+                 (cons `(lambda ()
+                          (delete-file ,emerge-file-A)
+                          (delete-file ,emerge-file-B))
                        startup-hooks)
                  quit-hooks
                  nil)))
 
 ;;;###autoload
 (defun emerge-buffers-with-ancestor (buffer-A buffer-B buffer-ancestor
-                                             &optional startup-hooks
-                                             quit-hooks)
+                                     &optional startup-hooks
+                                     quit-hooks)
   "Run Emerge on two buffers, giving another buffer as the ancestor."
   (interactive
    "bBuffer A to merge: \nbBuffer B to merge: \nbAncestor buffer: ")
@@ -1236,11 +984,11 @@ emerge-file-names.")
                                (get-buffer buffer-B) emerge-file-B
                                (get-buffer buffer-ancestor)
                                emerge-file-ancestor
-                               (cons (function (lambda ()
-                                                 (delete-file emerge-file-A)
-                                                 (delete-file emerge-file-B)
-                                                 (delete-file
-                                                  emerge-file-ancestor)))
+                               (cons `(lambda ()
+                                        (delete-file ,emerge-file-A)
+                                        (delete-file ,emerge-file-B)
+                                        (delete-file
+                                         ,emerge-file-ancestor))
                                      startup-hooks)
                                quit-hooks
                                nil)))
@@ -1255,7 +1003,7 @@ emerge-file-names.")
     (setq command-line-args-left (nthcdr 3 command-line-args-left))
     (emerge-files-internal
      file-a file-b nil
-     (list (` (lambda () (emerge-command-exit (, file-out))))))))
+     (list `(lambda () (emerge-command-exit ,file-out))))))
 
 ;;;###autoload
 (defun emerge-files-with-ancestor-command ()
@@ -1269,16 +1017,16 @@ emerge-file-names.")
          (setq file-anc (nth 1 command-line-args-left))
          (setq file-out (nth 4 command-line-args-left))
          (setq command-line-args-left (nthcdr 5 command-line-args-left)))
-      ;; arguments are "file-a file-b ancestor file-out"
-      (setq file-a (nth 0 command-line-args-left))
-      (setq file-b (nth 1 command-line-args-left))
-      (setq file-anc (nth 2 command-line-args-left))
-      (setq file-out (nth 3 command-line-args-left))
-      (setq command-line-args-left (nthcdr 4 command-line-args-left)))
+        ;; arguments are "file-a file-b ancestor file-out"
+        (setq file-a (nth 0 command-line-args-left))
+        (setq file-b (nth 1 command-line-args-left))
+        (setq file-anc (nth 2 command-line-args-left))
+        (setq file-out (nth 3 command-line-args-left))
+        (setq command-line-args-left (nthcdr 4 command-line-args-left)))
     (emerge-files-with-ancestor-internal
      file-a file-b file-anc nil
-     (list (` (lambda () (emerge-command-exit (, file-out))))))))
-      
+     (list `(lambda () (emerge-command-exit ,file-out))))))
+
 (defun emerge-command-exit (file-out)
   (emerge-write-and-delete file-out)
   (kill-emacs (if emerge-prefix-argument 1 0)))
@@ -1290,7 +1038,7 @@ emerge-file-names.")
   (setq emerge-file-out file-out)
   (emerge-files-internal
    file-a file-b nil
-   (list (` (lambda () (emerge-remote-exit (, file-out) '(, exit-func)))))
+   (list `(lambda () (emerge-remote-exit ,file-out ',emerge-exit-func)))
    file-out)
   (throw 'client-wait nil))
 
@@ -1299,14 +1047,329 @@ emerge-file-names.")
   (setq emerge-file-out file-out)
   (emerge-files-with-ancestor-internal
    file-a file-b file-anc nil
-   (list (` (lambda () (emerge-remote-exit (, file-out) '(, exit-func)))))
+   (list `(lambda () (emerge-remote-exit ,file-out ',emerge-exit-func)))
    file-out)
   (throw 'client-wait nil))
 
-(defun emerge-remote-exit (file-out exit-func)
+(defun emerge-remote-exit (file-out emerge-exit-func)
   (emerge-write-and-delete file-out)
   (kill-buffer emerge-merge-buffer)
-  (funcall exit-func (if emerge-prefix-argument 1 0)))
+  (funcall emerge-exit-func (if emerge-prefix-argument 1 0)))
+
+;;; Functions to start Emerge on RCS versions
+
+;;;###autoload
+(defun emerge-revisions (arg file revision-A revision-B
+                        &optional startup-hooks quit-hooks)
+  "Emerge two RCS revisions of a file."
+  (interactive
+   (list current-prefix-arg
+        (read-file-name "File to merge: " nil nil 'confirm)
+        (read-string "Revision A to merge: " emerge-last-revision-A)
+        (read-string "Revision B to merge: " emerge-last-revision-B)))
+  (setq emerge-last-revision-A revision-A
+       emerge-last-revision-B revision-B)
+  (emerge-revisions-internal
+   file revision-A revision-B startup-hooks
+   (if arg
+       (cons `(lambda ()
+               (shell-command
+                ,(format "%s %s" emerge-rcs-ci-program file)))
+            quit-hooks)
+       quit-hooks)))
+
+;;;###autoload
+(defun emerge-revisions-with-ancestor (arg file revision-A
+                                       revision-B ancestor
+                                       &optional
+                                       startup-hooks quit-hooks)
+  "Emerge two RCS revisions of a file, with another revision as ancestor."
+  (interactive
+   (list current-prefix-arg
+        (read-file-name "File to merge: " nil nil 'confirm)
+        (read-string "Revision A to merge: " emerge-last-revision-A)
+        (read-string "Revision B to merge: " emerge-last-revision-B)
+        (read-string "Ancestor: " emerge-last-revision-ancestor)))
+  (setq emerge-last-revision-A revision-A
+       emerge-last-revision-B revision-B
+       emerge-last-revision-ancestor ancestor)
+  (emerge-revision-with-ancestor-internal
+   file revision-A revision-B ancestor startup-hooks
+   (if arg
+       (let ((cmd ))
+        (cons `(lambda ()
+                 (shell-command
+                  ,(format "%s %s" emerge-rcs-ci-program file)))
+              quit-hooks))
+       quit-hooks)))
+
+(defun emerge-revisions-internal (file revision-A revision-B &optional
+                                  startup-hooks quit-hooks output-file)
+  (let ((buffer-A (get-buffer-create (format "%s,%s" file revision-A)))
+       (buffer-B (get-buffer-create (format "%s,%s" file revision-B)))
+       (emerge-file-A (emerge-make-temp-file "A"))
+       (emerge-file-B (emerge-make-temp-file "B")))
+    ;; Get the revisions into buffers
+    (emerge-eval-in-buffer
+     buffer-A
+     (erase-buffer)
+     (shell-command
+      (format "%s -q -p%s %s" emerge-rcs-co-program revision-A file)
+      t)
+     (write-region (point-min) (point-max) emerge-file-A nil 'no-message)
+     (set-buffer-modified-p nil))
+    (emerge-eval-in-buffer
+     buffer-B
+     (erase-buffer)
+     (shell-command
+      (format "%s -q -p%s %s" emerge-rcs-co-program revision-B file)
+      t)
+     (write-region (point-min) (point-max) emerge-file-B nil 'no-message)
+     (set-buffer-modified-p nil))
+    ;; Do the merge
+    (emerge-setup buffer-A emerge-file-A
+                 buffer-B emerge-file-B
+                 (cons `(lambda ()
+                          (delete-file ,emerge-file-A)
+                          (delete-file ,emerge-file-B))
+                       startup-hooks)
+                 (cons `(lambda () (emerge-files-exit ,file))
+                       quit-hooks)
+                 nil)))
+
+(defun emerge-revision-with-ancestor-internal (file revision-A revision-B
+                                               ancestor
+                                               &optional startup-hooks
+                                               quit-hooks output-file)
+  (let ((buffer-A (get-buffer-create (format "%s,%s" file revision-A)))
+       (buffer-B (get-buffer-create (format "%s,%s" file revision-B)))
+       (buffer-ancestor (get-buffer-create (format "%s,%s" file ancestor)))
+       (emerge-file-A (emerge-make-temp-file "A"))
+       (emerge-file-B (emerge-make-temp-file "B"))
+       (emerge-ancestor (emerge-make-temp-file "ancestor")))
+    ;; Get the revisions into buffers
+    (emerge-eval-in-buffer
+     buffer-A
+     (erase-buffer)
+     (shell-command
+      (format "%s -q -p%s %s" emerge-rcs-co-program
+             revision-A file)
+      t)
+     (write-region (point-min) (point-max) emerge-file-A nil 'no-message)
+     (set-buffer-modified-p nil))
+    (emerge-eval-in-buffer
+     buffer-B
+     (erase-buffer)
+     (shell-command
+      (format "%s -q -p%s %s" emerge-rcs-co-program revision-B file)
+      t)
+     (write-region (point-min) (point-max) emerge-file-B nil 'no-message)
+     (set-buffer-modified-p nil))
+    (emerge-eval-in-buffer
+     buffer-ancestor
+     (erase-buffer)
+     (shell-command
+      (format "%s -q -p%s %s" emerge-rcs-co-program ancestor file)
+      t)
+     (write-region (point-min) (point-max) emerge-ancestor nil 'no-message)
+     (set-buffer-modified-p nil))
+    ;; Do the merge
+    (emerge-setup-with-ancestor
+     buffer-A emerge-file-A buffer-B emerge-file-B
+     buffer-ancestor emerge-ancestor
+     (cons `(lambda ()
+             (delete-file ,emerge-file-A)
+             (delete-file ,emerge-file-B)
+             (delete-file ,emerge-ancestor))
+          startup-hooks)
+     (cons `(lambda () (emerge-files-exit ,file))
+          quit-hooks)
+     output-file)))
+
+;;; Function to start Emerge based on a line in a file
+
+(defun emerge-execute-line ()
+  "Run Emerge using files named in current text line.
+Looks in that line for whitespace-separated entries of these forms:
+       a=file1
+       b=file2
+       ancestor=file3
+       output=file4
+to specify the files to use in Emerge.
+
+In addition, if only one of `a=file' or `b=file' is present, and `output=file'
+is present:
+If `emerge-execute-line-deletions' is non-nil and `ancestor=file' is present,
+it is assumed that the file in question has been deleted, and it is
+not copied to the output file.
+Otherwise, the A or B file present is copied to the output file."
+  (interactive)
+  (let (file-A file-B file-ancestor file-out
+              (case-fold-search t))
+    ;; Stop if at end of buffer (even though we might be in a line, if
+    ;; the line does not end with newline)
+    (if (eobp)
+       (error "At end of buffer"))
+    ;; Go to the beginning of the line
+    (beginning-of-line)
+    ;; Skip any initial whitespace
+    (if (looking-at "[ \t]*")
+       (goto-char (match-end 0)))
+    ;; Process the entire line
+    (while (not (eolp))
+      ;; Get the next entry
+      (if (looking-at "\\([a-z]+\\)=\\([^ \t\n]+\\)[ \t]*")
+         ;; Break apart the tab (before =) and the filename (after =)
+         (let ((tag (downcase
+                     (buffer-substring (match-beginning 1) (match-end 1))))
+               (file (buffer-substring (match-beginning 2) (match-end 2))))
+           ;; Move point after the entry
+           (goto-char (match-end 0))
+           ;; Store the filename in the right variable
+           (cond
+              ((string-equal tag "a")
+               (if file-A
+                   (error "This line has two `A' entries"))
+               (setq file-A file))
+              ((string-equal tag "b")
+               (if file-B
+                   (error "This line has two `B' entries"))
+               (setq file-B file))
+              ((or (string-equal tag "anc") (string-equal tag "ancestor"))
+               (if file-ancestor
+                   (error "This line has two `ancestor' entries"))
+               (setq file-ancestor file))
+              ((or (string-equal tag "out") (string-equal tag "output"))
+               (if file-out
+                   (error "This line has two `output' entries"))
+               (setq file-out file))
+              (t
+               (error "Unrecognized entry"))))
+          ;; If the match on the entry pattern failed
+          (error "Unparsable entry")))
+    ;; Make sure that file-A and file-B are present
+    (if (not (or (and file-A file-B) file-out))
+       (error "Must have both `A' and `B' entries"))
+    (if (not (or file-A file-B))
+       (error "Must have `A' or `B' entry"))
+    ;; Go to the beginning of the next line, so next execution will use
+    ;; next line in buffer.
+    (beginning-of-line 2)
+    ;; Execute the correct command
+    (cond
+      ;; Merge of two files with ancestor
+      ((and file-A file-B file-ancestor)
+       (message "Merging %s and %s..." file-A file-B)
+       (emerge-files-with-ancestor (not (not file-out)) file-A file-B
+                                   file-ancestor file-out
+                                   nil
+                                   ;; When done, return to this buffer.
+                                   (list
+                                    `(lambda ()
+                                      (switch-to-buffer ,(current-buffer))
+                                      (message "Merge done.")))))
+      ;; Merge of two files without ancestor
+      ((and file-A file-B)
+       (message "Merging %s and %s..." file-A file-B)
+       (emerge-files (not (not file-out)) file-A file-B file-out
+                     nil
+                     ;; When done, return to this buffer.
+                     (list
+                      `(lambda ()
+                        (switch-to-buffer ,(current-buffer))
+                        (message "Merge done.")))))
+      ;; There is an output file (or there would have been an error above),
+      ;; but only one input file.
+      ;; The file appears to have been deleted in one version; do nothing.
+      ((and file-ancestor emerge-execute-line-deletions)
+       (message "No action."))
+      ;; The file should be copied from the version that contains it
+      (t (let ((input-file (or file-A file-B)))
+           (message "Copying...")
+           (copy-file input-file file-out)
+           (message "%s copied to %s." input-file file-out))))))
+
+;;; Sample function for creating information for emerge-execute-line
+
+(defcustom emerge-merge-directories-filename-regexp "[^.]"
+  "Regexp describing files to be processed by `emerge-merge-directories'."
+  :type 'regexp
+  :group 'emerge)
+
+;;;###autoload
+(defun emerge-merge-directories (a-dir b-dir ancestor-dir output-dir)
+  (interactive
+   (list
+    (read-file-name "A directory: " nil nil 'confirm)
+    (read-file-name "B directory: " nil nil 'confirm)
+    (read-file-name "Ancestor directory (null for none): " nil nil 'confirm)
+    (read-file-name "Output directory (null for none): " nil nil 'confirm)))
+  ;; Check that we're not on a line
+  (if (not (and (bolp) (eolp)))
+      (error "There is text on this line"))
+  ;; Turn null strings into nil to indicate directories not used.
+  (if (and ancestor-dir (string-equal ancestor-dir ""))
+      (setq ancestor-dir nil))
+  (if (and output-dir (string-equal output-dir ""))
+      (setq output-dir nil))
+  ;; Canonicalize the directory names
+  (setq a-dir (expand-file-name a-dir))
+  (if (not (string-equal (substring a-dir -1) "/"))
+      (setq a-dir (concat a-dir "/")))
+  (setq b-dir (expand-file-name b-dir))
+  (if (not (string-equal (substring b-dir -1) "/"))
+      (setq b-dir (concat b-dir "/")))
+  (if ancestor-dir
+      (progn
+       (setq ancestor-dir (expand-file-name ancestor-dir))
+       (if (not (string-equal (substring ancestor-dir -1) "/"))
+           (setq ancestor-dir (concat ancestor-dir "/")))))
+  (if output-dir
+      (progn
+       (setq output-dir (expand-file-name output-dir))
+       (if (not (string-equal (substring output-dir -1) "/"))
+           (setq output-dir (concat output-dir "/")))))
+  ;; Set the mark to where we start
+  (push-mark)
+  ;; Find out what files are in the directories.
+  (let* ((a-dir-files
+         (directory-files a-dir nil emerge-merge-directories-filename-regexp))
+        (b-dir-files
+         (directory-files b-dir nil emerge-merge-directories-filename-regexp))
+        (ancestor-dir-files
+         (and ancestor-dir
+              (directory-files ancestor-dir nil
+                               emerge-merge-directories-filename-regexp)))
+        (all-files (sort (nconc (copy-sequence a-dir-files)
+                                (copy-sequence b-dir-files)
+                                (copy-sequence ancestor-dir-files))
+                         (function string-lessp))))
+    ;; Remove duplicates from all-files.
+    (let ((p all-files))
+      (while p
+       (if (and (cdr p) (string-equal (car p) (car (cdr p))))
+           (setcdr p (cdr (cdr p)))
+         (setq p (cdr p)))))
+    ;; Generate the control lines for the various files.
+    (while all-files
+      (let ((f (car all-files)))
+       (setq all-files (cdr all-files))
+       (if (and a-dir-files (string-equal (car a-dir-files) f))
+           (progn
+             (insert "A=" a-dir f "\t")
+             (setq a-dir-files (cdr a-dir-files))))
+       (if (and b-dir-files (string-equal (car b-dir-files) f))
+           (progn
+             (insert "B=" b-dir f "\t")
+             (setq b-dir-files (cdr b-dir-files))))
+       (if (and ancestor-dir-files (string-equal (car ancestor-dir-files) f))
+           (progn
+             (insert "ancestor=" ancestor-dir f "\t")
+             (setq ancestor-dir-files (cdr ancestor-dir-files))))
+       (if output-dir
+           (insert "output=" output-dir f "\t"))
+       (backward-delete-char 1)
+       (insert "\n")))))
 
 ;;; Common setup routines
 
@@ -1354,26 +1417,35 @@ emerge-file-names.")
   ;; Install the Emerge commands
   (emerge-force-define-key emerge-edit-keymap emerge-command-prefix
                           'emerge-basic-keymap)
+  (define-key emerge-edit-keymap [menu-bar] (make-sparse-keymap))
+
+  ;; Create the additional menu bar items.
+  (define-key emerge-edit-keymap [menu-bar options]
+    (cons "Options" emerge-options-menu))
+  (define-key emerge-edit-keymap [menu-bar merge]
+    (cons "Merge" emerge-merge-menu))
+  (define-key emerge-edit-keymap [menu-bar move]
+    (cons "Move" emerge-move-menu))
+
   ;; Suppress write-file and save-buffer
-  (emerge-recursively-substitute-key-definition 'write-file
-                                               'emerge-query-write-file
-                                               emerge-edit-keymap)
-  (emerge-recursively-substitute-key-definition 'save-buffer
-                                               'emerge-query-save-buffer
-                                               emerge-edit-keymap)
-  (emerge-shadow-key-definition 'write-file 'emerge-query-write-file
-                               (current-global-map) emerge-edit-keymap)
-  (emerge-shadow-key-definition 'save-buffer 'emerge-query-save-buffer
-                               (current-global-map) emerge-edit-keymap)
+  (substitute-key-definition 'write-file
+                            'emerge-query-write-file
+                            emerge-edit-keymap)
+  (substitute-key-definition 'save-buffer
+                            'emerge-query-save-buffer
+                            emerge-edit-keymap)
+  (define-key emerge-edit-keymap [remap write-file] 'emerge-query-write-file)
+  (define-key emerge-edit-keymap [remap save-buffer] 'emerge-query-save-buffer)
   (use-local-map emerge-fast-keymap)
   (setq emerge-edit-mode nil)
   (setq emerge-fast-mode t))
 
 (defun emerge-remember-buffer-characteristics ()
-  "Must be called in the merge buffer.  Remembers certain properties of the
-buffers being merged (read-only, modified, auto-save), and saves them in
-buffer local variables.  Sets the buffers read-only and turns off auto-save.
-These characteristics are restored by emerge-restore-buffer-characteristics."
+  "Record certain properties of the buffers being merged.
+Must be called in the merge buffer.  Remembers read-only, modified,
+auto-save, and saves them in buffer local variables.  Sets the buffers
+read-only and turns off `auto-save-mode'.
+These characteristics are restored by `emerge-restore-buffer-characteristics'."
   ;; force auto-save, because we will turn off auto-saving in buffers for the
   ;; duration
   (do-auto-save)
@@ -1394,8 +1466,7 @@ These characteristics are restored by emerge-restore-buffer-characteristics."
                                     emerge-merging-values)))))
 
 (defun emerge-restore-buffer-characteristics ()
-  "Restores the characteristics remembered by
-emerge-remember-buffer-characteristics."
+  "Restore characteristics saved by `emerge-remember-buffer-characteristics'."
   (let ((A-values emerge-A-buffer-values)
        (B-values emerge-B-buffer-values))
     (emerge-eval-in-buffer emerge-A-buffer
@@ -1405,6 +1476,12 @@ emerge-remember-buffer-characteristics."
                           (emerge-restore-variables emerge-saved-variables
                                                     B-values))))
 
+;; Move to line DESIRED-LINE assuming we are at line CURRENT-LINE.
+;; Return DESIRED-LINE.
+(defun emerge-goto-line (desired-line current-line)
+  (forward-line (- desired-line current-line))
+  desired-line)
+
 (defun emerge-convert-diffs-to-markers (A-buffer
                                        B-buffer
                                        merge-buffer
@@ -1412,23 +1489,21 @@ emerge-remember-buffer-characteristics."
   (let* (marker-list
         (A-point-min (emerge-eval-in-buffer A-buffer (point-min)))
         (offset (1- A-point-min))
-        (A-hidden-lines (emerge-eval-in-buffer
-                         A-buffer
-                         (save-restriction
-                           (widen)
-                           (count-lines 1 A-point-min))))
         (B-point-min (emerge-eval-in-buffer B-buffer (point-min)))
-        (B-hidden-lines (emerge-eval-in-buffer
-                         B-buffer
-                         (save-restriction
-                           (widen)
-                           (count-lines 1 B-point-min)))))
+        ;; Record current line number in each buffer
+        ;; so we don't have to count from the beginning.
+        (a-line 1)
+        (b-line 1))
+    (emerge-eval-in-buffer A-buffer (goto-char (point-min)))
+    (emerge-eval-in-buffer B-buffer (goto-char (point-min)))
     (while lineno-list
       (let* ((list-element (car lineno-list))
             a-begin-marker
             a-end-marker
             b-begin-marker
             b-end-marker
+            merge-begin-marker
+            merge-end-marker
             (a-begin (aref list-element 0))
             (a-end (aref list-element 1))
             (b-begin (aref list-element 2))
@@ -1437,15 +1512,15 @@ emerge-remember-buffer-characteristics."
        ;; place markers at the appropriate places in the buffers
        (emerge-eval-in-buffer
         A-buffer
-        (goto-line (+ a-begin A-hidden-lines))
+        (setq a-line (emerge-goto-line a-begin a-line))
         (setq a-begin-marker (point-marker))
-        (goto-line (+ a-end A-hidden-lines))
+        (setq a-line (emerge-goto-line a-end a-line))
         (setq a-end-marker (point-marker)))
        (emerge-eval-in-buffer
         B-buffer
-        (goto-line (+ b-begin B-hidden-lines))
+        (setq b-line (emerge-goto-line b-begin b-line))
         (setq b-begin-marker (point-marker))
-        (goto-line (+ b-end B-hidden-lines))
+        (setq b-line (emerge-goto-line b-end b-line))
         (setq b-end-marker (point-marker)))
        (setq merge-begin-marker (set-marker
                                  (make-marker)
@@ -1468,7 +1543,7 @@ emerge-remember-buffer-characteristics."
     ;; fast access
     (setq emerge-difference-list (apply 'vector (nreverse marker-list)))))
 
-;; If we have an ancestor, select all B variants that we prefer 
+;; If we have an ancestor, select all B variants that we prefer
 (defun emerge-select-prefer-Bs ()
   (let ((n 0))
     (while (< n emerge-number-of-differences)
@@ -1480,6 +1555,15 @@ emerge-remember-buffer-characteristics."
       (setq n (1+ n))))
   (emerge-unselect-and-select-difference -1))
 
+;; Process the local-variables list at the end of the merged file, if
+;; requested.
+(defun emerge-handle-local-variables ()
+  (if emerge-process-local-variables
+      (condition-case err
+         (hack-local-variables)
+       (error (message "Local-variables error in merge buffer: %s"
+                       (prin1-to-string err))))))
+
 ;;; Common exit routines
 
 (defun emerge-write-and-delete (file-out)
@@ -1494,14 +1578,15 @@ emerge-remember-buffer-characteristics."
           (not (buffer-modified-p emerge-ancestor-buffer)))
       (kill-buffer emerge-ancestor-buffer))
   ;; Write merge buffer to file
-  (write-file file-out))
+  (and file-out
+       (write-file file-out)))
 
 ;;; Commands
 
 (defun emerge-recenter (&optional arg)
-  "Bring the highlighted region of all three merge buffers into view,
-if they are in windows.  If an ARGUMENT is given, the default three-window
-display is reestablished."
+  "Bring the highlighted region of all three merge buffers into view.
+This brings the buffers into view if they are in windows.
+With an argument, reestablish the default three-window display."
   (interactive "P")
   ;; If there is an argument, rebuild the window structure
   (if arg
@@ -1513,8 +1598,8 @@ display is reestablished."
       (let* ((merge-buffer emerge-merge-buffer)
             (buffer-A emerge-A-buffer)
             (buffer-B emerge-B-buffer)
-            (window-A (get-buffer-window buffer-A))
-            (window-B (get-buffer-window buffer-B))
+            (window-A (get-buffer-window buffer-A 'visible))
+            (window-B (get-buffer-window buffer-B 'visible))
             (merge-window (get-buffer-window merge-buffer))
             (diff-vector
              (aref emerge-difference-list emerge-current-difference)))
@@ -1558,8 +1643,8 @@ display is reestablished."
   (let* ((merge-buffer emerge-merge-buffer)
         (buffer-A emerge-A-buffer)
         (buffer-B emerge-B-buffer)
-        (window-A (get-buffer-window buffer-A))
-        (window-B (get-buffer-window buffer-B))
+        (window-A (get-buffer-window buffer-A 'visible))
+        (window-B (get-buffer-window buffer-B 'visible))
         (merge-window (get-buffer-window merge-buffer)))
     (if window-A (progn
                   (select-window window-A)
@@ -1577,12 +1662,12 @@ display is reestablished."
 
 (defun emerge-scroll-up (&optional arg)
   "Scroll up all three merge buffers, if they are in windows.
-If an ARGUMENT is given, that is how many lines are scrolled, else nearly
-the size of the merge window.  `C-u -' alone as argument scrolls half the
-size of the merge window."
+With argument N, scroll N lines; otherwise scroll by nearly
+the height of the merge window.
+`C-u -' alone as argument scrolls half the height of the merge window."
   (interactive "P")
   (emerge-operate-on-windows
-   'scroll-up 
+   'scroll-up
    ;; calculate argument to scroll-up
    ;; if there is an explicit argument
    (if (and arg (not (equal arg '-)))
@@ -1604,9 +1689,9 @@ size of the merge window."
 
 (defun emerge-scroll-down (&optional arg)
   "Scroll down all three merge buffers, if they are in windows.
-If an ARGUMENT is given, that is how many lines are scrolled, else nearly
-the size of the merge window.  `C-u -' alone as argument scrolls half the
-size of the merge window."
+With argument N, scroll N lines; otherwise scroll by nearly
+the height of the merge window.
+`C-u -' alone as argument scrolls half the height of the merge window."
   (interactive "P")
   (emerge-operate-on-windows
    'scroll-down
@@ -1631,7 +1716,7 @@ size of the merge window."
 
 (defun emerge-scroll-left (&optional arg)
   "Scroll left all three merge buffers, if they are in windows.
-If an ARGUMENT is given, that is how many columns are scrolled, else nearly
+If an argument is given, that is how many columns are scrolled, else nearly
 the width of the A and B windows.  `C-u -' alone as argument scrolls half the
 width of the A and B windows."
   (interactive "P")
@@ -1659,7 +1744,7 @@ width of the A and B windows."
 
 (defun emerge-scroll-right (&optional arg)
   "Scroll right all three merge buffers, if they are in windows.
-If an ARGUMENT is given, that is how many columns are scrolled, else nearly
+If an argument is given, that is how many columns are scrolled, else nearly
 the width of the A and B windows.  `C-u -' alone as argument scrolls half the
 width of the A and B windows."
   (interactive "P")
@@ -1686,8 +1771,9 @@ width of the A and B windows."
             default-amount)))))))
 
 (defun emerge-scroll-reset ()
-  "Reset horizontal scrolling of all three merge buffers to the left margin,
-if they are in windows."
+  "Reset horizontal scrolling in Emerge.
+This resets the horizontal scrolling of all three merge buffers
+to the left margin, if they are in windows."
   (interactive)
   (emerge-operate-on-windows
    (function (lambda (x) (set-window-hscroll (selected-window) 0)))
@@ -1697,7 +1783,7 @@ if they are in windows."
 ;; If there are min-lines lines above and below the region, then don't do
 ;; anything.
 ;; If not, recenter the region to make it so.
-;; If that isn't possible, remove context lines balancedly from top and botton
+;; If that isn't possible, remove context lines balancedly from top and bottom
 ;; so the entire region shows.
 ;; If that isn't possible, show the top of the region.
 ;; BEG must be at the beginning of a line.
@@ -1716,8 +1802,7 @@ if they are in windows."
       ;; Meanwhile positioning it correctly in case it doesn't fit
       (progn
        (set-window-start (selected-window) beg)
-       (setq fits (pos-visible-in-window-p end))
-       (if fits
+       (if (pos-visible-in-window-p end)
            ;; Determine the number of lines that the region occupies
            (let ((lines 0))
              (while (> end (progn
@@ -1769,9 +1854,15 @@ if they are in windows."
        (emerge-unselect-and-select-difference difference-number)
       (error "Bad difference number"))))
 
+(defun emerge-abort ()
+  "Abort the Emerge session."
+  (interactive)
+  (emerge-quit t))
+
 (defun emerge-quit (arg)
-  "Finish an Emerge session.  Prefix ARGUMENT means to abort rather than
-successfully finish.  The difference depends on how the merge was started,
+  "Finish the Emerge session and exit Emerge.
+Prefix argument means to abort rather than successfully finish.
+The difference depends on how the merge was started,
 but usually means to not write over one of the original files, or to signal
 to some process which invoked Emerge a failure code.
 
@@ -1816,13 +1907,14 @@ buffer after this will cause serious problems."
   ;; restore mode line
   (kill-local-variable 'mode-line-buffer-identification)
   (let ((emerge-prefix-argument arg))
-    (run-hooks 'emerge-quit-hooks)))
+    (run-hooks 'emerge-quit-hook)))
 
 (defun emerge-select-A (&optional force)
-  "Select the A variant of this difference.  Refuses to function if this
-difference has been edited, i.e., if it is neither the A nor the B variant.
-An ARGUMENT forces the variant to be selected even if the difference has
-been edited."
+  "Select the A variant of this difference.
+Refuses to function if this difference has been edited, i.e., if it
+is neither the A nor the B variant.
+A prefix argument forces the variant to be selected
+even if the difference has been edited."
   (interactive "P")
   (let ((operate
         (function (lambda ()
@@ -1847,10 +1939,11 @@ been edited."
    (emerge-refresh-mode-line)))
 
 (defun emerge-select-B (&optional force)
-  "Select the B variant of this difference.  Refuses to function if this
-difference has been edited, i.e., if it is neither the A nor the B variant.
-An ARGUMENT forces the variant to be selected even if the difference has
-been edited."
+  "Select the B variant of this difference.
+Refuses to function if this difference has been edited, i.e., if it
+is neither the A nor the B variant.
+A prefix argument forces the variant to be selected
+even if the difference has been edited."
   (interactive "P")
   (let ((operate
         (function (lambda ()
@@ -1875,7 +1968,8 @@ been edited."
    (emerge-refresh-mode-line)))
 
 (defun emerge-default-A ()
-  "Selects the A variant for all differences from here down in the buffer
+  "Make the A variant the default from here down.
+This selects the A variant for all differences from here down in the buffer
 which are still defaulted, i.e., which the user has not selected and for
 which there is no preference."
   (interactive)
@@ -1890,13 +1984,14 @@ which there is no preference."
                (emerge-select-A)
                (aset diff-vector 6 'default-A))))
        (setq n (1+ n))
-       (if (= (* (/ n 10) 10) n)
+       (if (zerop (% n 10))
            (message "Setting default to A...%d" n)))
       (emerge-unselect-and-select-difference selected-difference)))
-  (message "Default A set"))
+  (message "Default choice is now A"))
 
 (defun emerge-default-B ()
-  "Selects the B variant for all differences from here down in the buffer
+  "Make the B variant the default from here down.
+This selects the B variant for all differences from here down in the buffer
 which are still defaulted, i.e., which the user has not selected and for
 which there is no preference."
   (interactive)
@@ -1911,14 +2006,15 @@ which there is no preference."
                (emerge-select-B)
                (aset diff-vector 6 'default-B))))
        (setq n (1+ n))
-       (if (= (* (/ n 10) 10) n)
+       (if (zerop (% n 10))
            (message "Setting default to B...%d" n)))
       (emerge-unselect-and-select-difference selected-difference)))
-  (message "Default B set"))
+  (message "Default choice is now B"))
 
 (defun emerge-fast-mode ()
-  "Set fast mode, in which ordinary Emacs commands are disabled, and Emerge
-commands are need not be prefixed with \\<emerge-fast-keymap>\\[emerge-basic-keymap]."
+  "Set fast mode, for Emerge.
+In this mode ordinary Emacs commands are disabled, and Emerge commands
+need not be prefixed with \\<emerge-fast-keymap>\\[emerge-basic-keymap]."
   (interactive)
   (setq buffer-read-only t)
   (use-local-map emerge-fast-keymap)
@@ -1926,12 +2022,12 @@ commands are need not be prefixed with \\<emerge-fast-keymap>\\[emerge-basic-key
   (setq emerge-fast-mode t)
   (setq emerge-edit-mode nil)
   (message "Fast mode set")
-  ;; force mode line redisplay
-  (set-buffer-modified-p (buffer-modified-p)))
+  (force-mode-line-update))
 
 (defun emerge-edit-mode ()
-  "Set edit mode, in which ordinary Emacs commands are available, and Emerge
-commands must be prefixed with \\<emerge-fast-keymap>\\[emerge-basic-keymap]."
+  "Set edit mode, for Emerge.
+In this mode ordinary Emacs commands are available, and Emerge commands
+must be prefixed with \\<emerge-fast-keymap>\\[emerge-basic-keymap]."
   (interactive)
   (setq buffer-read-only nil)
   (use-local-map emerge-edit-keymap)
@@ -1939,31 +2035,29 @@ commands must be prefixed with \\<emerge-fast-keymap>\\[emerge-basic-keymap]."
   (setq emerge-fast-mode nil)
   (setq emerge-edit-mode t)
   (message "Edit mode set")
-  ;; force mode line redisplay
-  (set-buffer-modified-p (buffer-modified-p)))
+  (force-mode-line-update))
 
 (defun emerge-auto-advance (arg)
-  "Toggle auto-advance mode, which causes  emerge-select-A  and
- emerge-select-B  to automatically advance to the next difference.  (See
-emerge-auto-advance.)  
-If a positive ARGUMENT is given, it turns on auto-advance mode.
-If a negative ARGUMENT is given, it turns off auto-advance mode."
+  "Toggle Auto-Advance mode, for Emerge.
+This mode causes `emerge-select-A' and `emerge-select-B' to automatically
+advance to the next difference.
+With a positive argument, turn on Auto-Advance mode.
+With a negative argument, turn off Auto-Advance mode."
   (interactive "P")
   (setq emerge-auto-advance (if (null arg)
                                (not emerge-auto-advance)
                              (> (prefix-numeric-value arg) 0)))
-  (message (if emerge-skip-prefers
+  (message (if emerge-auto-advance
               "Auto-advance set"
             "Auto-advance cleared"))
-  ;; force mode line redisplay
-  (set-buffer-modified-p (buffer-modified-p)))
+  (force-mode-line-update))
 
 (defun emerge-skip-prefers (arg)
-  "Toggle skip-prefers mode, which causes  emerge-next-difference  and
- emerge-previous-difference  to automatically skip over differences for which
-there is a preference.  (See emerge-skip-prefers.)  
-If a positive ARGUMENT is given, it turns on skip-prefers mode.
-If a negative ARGUMENT is given, it turns off skip-prefers mode."
+  "Toggle Skip-Prefers mode, for Emerge.
+This mode causes `emerge-next-difference' and `emerge-previous-difference'
+to automatically skip over differences for which there is a preference.
+With a positive argument, turn on Skip-Prefers mode.
+With a negative argument, turn off Skip-Prefers mode."
   (interactive "P")
   (setq emerge-skip-prefers (if (null arg)
                                (not emerge-skip-prefers)
@@ -1971,8 +2065,7 @@ If a negative ARGUMENT is given, it turns off skip-prefers mode."
   (message (if emerge-skip-prefers
               "Skip-prefers set"
             "Skip-prefers cleared"))
-  ;; force mode line redisplay
-  (set-buffer-modified-p (buffer-modified-p)))
+  (force-mode-line-update))
 
 (defun emerge-copy-as-kill-A ()
   "Put the A variant of this difference in the kill ring."
@@ -2056,10 +2149,10 @@ With prefix argument, puts mark before, point after."
 
 (defun emerge-file-names ()
   "Show the names of the buffers or files being operated on by Emerge.
-Use ^U L to reset the windows afterward."
+Use C-u l to reset the windows afterward."
   (interactive)
   (delete-other-windows)
-  (let ((temp-buffer-show-hook
+  (let ((temp-buffer-show-function
         (function (lambda (buf)
                     (split-window-vertically)
                     (switch-to-buffer buf)
@@ -2093,11 +2186,14 @@ Use ^U L to reset the windows afterward."
                                       (princ "Ancestor buffer is: ")
                                       (princ (buffer-name))))
                                   (princ "\n")))
-      (princ emerge-output-description))))
+      (princ emerge-output-description)
+      (save-excursion
+       (set-buffer standard-output)
+       (help-mode)))))
 
 (defun emerge-join-differences (arg)
-  "Join the selected difference with the following one.  With a prefix
-argument, join with the preceeding one."
+  "Join the selected difference with the following one.
+With a prefix argument, join with the preceding one."
   (interactive "P")
   (let ((n emerge-current-difference))
     ;; adjust n to be first difference to join
@@ -2211,9 +2307,10 @@ argument, join with the preceeding one."
       (emerge-recenter))))
 
 (defun emerge-trim-difference ()
-  "Trim lines off the top and bottom of a difference that are the same in
-both the A and B versions.  (This can happen when the A and B versions
-have common lines that the ancestor version does not share.)"
+  "Trim lines off top and bottom of difference that are the same.
+If lines are the same in both the A and the B versions, strip them off.
+\(This can happen when the A and B versions have common lines that the
+ancestor version does not share.)"
   (interactive)
   ;; make sure we are in a real difference
   (emerge-validate-difference)
@@ -2302,6 +2399,18 @@ have common lines that the ancestor version does not share.)"
   "Find the difference containing the current position of the point.
 If there is no containing difference and the prefix argument is positive,
 it finds the nearest following difference.  A negative prefix argument finds
+the nearest previous difference."
+  (interactive "P")
+  (cond ((eq (current-buffer) emerge-A-buffer)
+        (emerge-find-difference-A arg))
+       ((eq (current-buffer) emerge-B-buffer)
+        (emerge-find-difference-B arg))
+       (t (emerge-find-difference-merge arg))))
+
+(defun emerge-find-difference-merge (arg)
+  "Find the difference containing point, in the merge buffer.
+If there is no containing difference and the prefix argument is positive,
+it finds the nearest following difference.  A negative prefix argument finds
 the nearest previous difference."
   (interactive "P")
   ;; search for the point in the merge buffer, using the markers
@@ -2309,8 +2418,8 @@ the nearest previous difference."
   (emerge-find-difference1 arg (point) 4 5))
 
 (defun emerge-find-difference-A (arg)
-  "Find the difference containing the current position of the point in the
-A buffer.  (Nonetheless, this command must be executed in the merge buffer.)
+  "Find the difference containing point, in the A buffer.
+This command must be executed in the merge buffer.
 If there is no containing difference and the prefix argument is positive,
 it finds the nearest following difference.  A negative prefix argument finds
 the nearest previous difference."
@@ -2322,8 +2431,8 @@ the nearest previous difference."
                           0 1))
 
 (defun emerge-find-difference-B (arg)
-  "Find the difference containing the current position of the point in the
-B buffer.  (Nonetheless, this command must be executed in the merge buffer.)
+  "Find the difference containing point, in the B buffer.
+This command must be executed in the merge buffer.
 If there is no containing difference and the prefix argument is positive,
 it finds the nearest following difference.  A negative prefix argument finds
 the nearest previous difference."
@@ -2365,14 +2474,15 @@ the nearest previous difference."
        (if (< index emerge-number-of-differences)
           index
         (error "No difference contains or follows point")))
-      ;; if the arg is negative, select the preceeding difference
+      ;; if the arg is negative, select the preceding difference
       (t
        (if (> index 0)
           (1- index)
-        (error "No difference contains or preceeds point")))))))
+        (error "No difference contains or precedes point")))))))
 
 (defun emerge-line-numbers ()
-  "Display the current line numbers of the points in the A, B, and
+  "Display the current line numbers.
+This function displays the line numbers of the points in the A, B, and
 merge buffers."
   (interactive)
   (let* ((valid-diff
@@ -2401,10 +2511,24 @@ merge buffers."
              (setq temp (- temp emerge-after-flag-lines)))))
     temp))
 
+(defun emerge-set-combine-template (string &optional localize)
+  "Set `emerge-combine-versions-template' to STRING.
+This value controls how `emerge-combine-versions' combines the two versions.
+With prefix argument, `emerge-combine-versions-template' is made local to this
+merge buffer.  Localization is permanent for any particular merge buffer."
+  (interactive "s\nP")
+  (if localize
+      (make-local-variable 'emerge-combine-versions-template))
+  (setq emerge-combine-versions-template string)
+  (message
+   (if (assq 'emerge-combine-versions-template (buffer-local-variables))
+       "emerge-set-combine-versions-template set locally"
+     "emerge-set-combine-versions-template set")))
+
 (defun emerge-set-combine-versions-template (start end &optional localize)
-  "Copy region into  emerge-combine-versions-template  which controls how
-emerge-combine-versions  will combine the two versions.
-With prefix argument,  emerge-combine-versions  is made local to this
+  "Copy region into `emerge-combine-versions-template'.
+This controls how `emerge-combine-versions' will combine the two versions.
+With prefix argument, `emerge-combine-versions-template' is made local to this
 merge buffer.  Localization is permanent for any particular merge buffer."
   (interactive "r\nP")
   (if localize
@@ -2416,22 +2540,21 @@ merge buffer.  Localization is permanent for any particular merge buffer."
      "emerge-set-combine-versions-template set.")))
 
 (defun emerge-combine-versions (&optional force)
-  "Combine the two versions using the template in
-emerge-combine-versions-template.
+  "Combine versions using the template in `emerge-combine-versions-template'.
 Refuses to function if this difference has been edited, i.e., if it is
 neither the A nor the B variant.
-An ARGUMENT forces the variant to be selected even if the difference has
+An argument forces the variant to be selected even if the difference has
 been edited."
   (interactive "P")
   (emerge-combine-versions-internal emerge-combine-versions-template force))
 
 (defun emerge-combine-versions-register (char &optional force)
   "Combine the two versions using the template in register REG.
-See documentation of the variable  emerge-combine-versions-template
+See documentation of the variable `emerge-combine-versions-template'
 for how the template is interpreted.
 Refuses to function if this difference has been edited, i.e., if it is
 neither the A nor the B variant.
-An ARGUMENT forces the variant to be selected even if the difference has
+An argument forces the variant to be selected even if the difference has
 been edited."
   (interactive "cRegister containing template: \nP")
   (let ((template (get-register char)))
@@ -2460,18 +2583,18 @@ been edited."
         (if (= c ?%)
             (progn
               (setq i (1+ i))
-              (setq c 
+              (setq c
                     (condition-case nil
                         (aref template i)
                       (error ?%)))
               (cond ((= c ?a)
                      (insert-buffer-substring emerge-A-buffer A-begin A-end))
-                    ((= c ?b) 
+                    ((= c ?b)
                      (insert-buffer-substring emerge-B-buffer B-begin B-end))
-                    ((= c ?%) 
-                     (insert ?%)
-                     (t
-                      (insert c)))))
+                    ((= c ?%)
+                     (insert ?%))
+                    (t
+                     (insert c))))
           (insert c)))
        (setq i (1+ i))))
    (goto-char merge-begin)
@@ -2479,8 +2602,9 @@ been edited."
    (emerge-refresh-mode-line)))
 
 (defun emerge-set-merge-mode (mode)
-  "Set the major mode in a merge buffer.  Overrides any change that the mode
-might make to the mode line or local keymap.  Leaves merge in fast mode."
+  "Set the major mode in a merge buffer.
+Overrides any change that the mode might make to the mode line or local
+keymap.  Leaves merge in fast mode."
   (interactive
    (list (intern (completing-read "New major mode for merge buffer: "
                                  obarray 'commandp t nil))))
@@ -2500,34 +2624,72 @@ might make to the mode line or local keymap.  Leaves merge in fast mode."
 ;; Select a difference by placing the visual flags around the appropriate
 ;; group of lines in the A, B, and merge buffers
 (defun emerge-select-difference (n)
-  (let ((diff-vector (aref emerge-difference-list n)))
-    (emerge-place-flags-in-buffer emerge-A-buffer
-                                 (aref diff-vector 0) (aref diff-vector 1))
-    (emerge-place-flags-in-buffer emerge-B-buffer
-                                 (aref diff-vector 2) (aref diff-vector 3))
-    (emerge-place-flags-in-buffer emerge-merge-buffer
-                                 (aref diff-vector 4) (aref diff-vector 5))))
-
-(defun emerge-place-flags-in-buffer (buffer before after)
-  (if (eq buffer emerge-merge-buffer)
-      (emerge-place-flags-in-buffer1 buffer before after)
-    (emerge-eval-in-buffer
-     buffer
-     (emerge-place-flags-in-buffer1 buffer before after))))
-
-(defun emerge-place-flags-in-buffer1 (buffer before after)
+  (let ((emerge-globalized-difference-list emerge-difference-list)
+       (emerge-globalized-number-of-differences emerge-number-of-differences))
+    (emerge-place-flags-in-buffer emerge-A-buffer n 0 1)
+    (emerge-place-flags-in-buffer emerge-B-buffer n 2 3)
+    (emerge-place-flags-in-buffer nil n 4 5))
+  (run-hooks 'emerge-select-hook))
+
+(defun emerge-place-flags-in-buffer (buffer difference before-index
+                                           after-index)
+  (if buffer
+      (emerge-eval-in-buffer
+       buffer
+       (emerge-place-flags-in-buffer1 difference before-index after-index))
+    (emerge-place-flags-in-buffer1 difference before-index after-index)))
+
+(defun emerge-place-flags-in-buffer1 (difference before-index after-index)
   (let ((buffer-read-only nil))
-    ;; insert the flags
-    (goto-char before)
-    (insert-before-markers emerge-before-flag)
-    (goto-char after)
-    (insert emerge-after-flag)
-    ;; put the markers into the flags, so alterations above or below won't move
-    ;; them
-    ;; before marker is one char before the end of the before flag
-    ;; after marker is one char after the beginning of the after flag
-    (set-marker before (1- before))
-    (set-marker after (1+ after))))
+    ;; insert the flag before the difference
+    (let ((before (aref (aref emerge-globalized-difference-list difference)
+                       before-index))
+         here)
+      (goto-char before)
+      ;; insert the flag itself
+      (insert-before-markers emerge-before-flag)
+      (setq here (point))
+      ;; Put the marker(s) referring to this position 1 character before the
+      ;; end of the flag, so it won't be damaged by the user.
+      ;; This gets a bit tricky, as there could be a number of markers
+      ;; that have to be moved.
+      (set-marker before (1- before))
+      (let ((n (1- difference)) after-marker before-marker diff-list)
+       (while (and
+               (>= n 0)
+               (progn
+                 (setq diff-list (aref emerge-globalized-difference-list n)
+                       after-marker (aref diff-list after-index))
+                 (= after-marker here)))
+         (set-marker after-marker (1- after-marker))
+         (setq before-marker (aref diff-list before-index))
+         (if (= before-marker here)
+             (setq before-marker (1- before-marker)))
+         (setq n (1- n)))))
+    ;; insert the flag after the difference
+    (let* ((after (aref (aref emerge-globalized-difference-list difference)
+                       after-index))
+          (here (marker-position after)))
+      (goto-char here)
+      ;; insert the flag itself
+      (insert emerge-after-flag)
+      ;; Put the marker(s) referring to this position 1 character after the
+      ;; beginning of the flag, so it won't be damaged by the user.
+      ;; This gets a bit tricky, as there could be a number of markers
+      ;; that have to be moved.
+      (set-marker after (1+ after))
+      (let ((n (1+ difference)) before-marker after-marker diff-list)
+       (while (and
+               (< n emerge-globalized-number-of-differences)
+               (progn
+                 (setq diff-list (aref emerge-globalized-difference-list n)
+                       before-marker (aref diff-list before-index))
+                 (= before-marker here)))
+         (set-marker before-marker (1+ before-marker))
+         (setq after-marker (aref diff-list after-index))
+         (if (= after-marker here)
+             (setq after-marker (1+ after-marker)))
+         (setq n (1+ n)))))))
 
 ;; Unselect a difference by removing the visual flags in the buffers.
 (defun emerge-unselect-difference (n)
@@ -2537,30 +2699,28 @@ might make to the mode line or local keymap.  Leaves merge in fast mode."
     (emerge-remove-flags-in-buffer emerge-B-buffer
                                   (aref diff-vector 2) (aref diff-vector 3))
     (emerge-remove-flags-in-buffer emerge-merge-buffer
-                                  (aref diff-vector 4) (aref diff-vector 5))))
+                                  (aref diff-vector 4) (aref diff-vector 5)))
+  (run-hooks 'emerge-unselect-hook))
 
 (defun emerge-remove-flags-in-buffer (buffer before after)
   (emerge-eval-in-buffer
    buffer
    (let ((buffer-read-only nil))
-     ;; put the markers at the beginning of the flags
-     (set-marker before (- before (1- emerge-before-flag-length)))
-     (set-marker after (1- after))
-     ;; remove the flags
-     (goto-char before)
+     ;; remove the flags, if they're there
+     (goto-char (- before (1- emerge-before-flag-length)))
      (if (looking-at emerge-before-flag-match)
         (delete-char emerge-before-flag-length)
        ;; the flag isn't there
        (ding)
-       (message "Trouble removing flag."))
-     (goto-char after)
+       (message "Trouble removing flag"))
+     (goto-char (1- after))
      (if (looking-at emerge-after-flag-match)
         (delete-char emerge-after-flag-length)
        ;; the flag isn't there
        (ding)
-       (message "Trouble removing flag.")))))
+       (message "Trouble removing flag")))))
 
-;; Select a difference, removing an flags that exist now.
+;; Select a difference, removing any flags that exist now.
 (defun emerge-unselect-and-select-difference (n &optional suppress-display)
   (if (and (>= emerge-current-difference 0)
           (< emerge-current-difference emerge-number-of-differences))
@@ -2610,7 +2770,57 @@ might make to the mode line or local keymap.  Leaves merge in fast mode."
            (funcall b-version)
          (if (or force (= merge-begin merge-end))
              (funcall neither-version)
-           (error "This difference region has been edited.")))))))
+           (error "This difference region has been edited")))))))
+
+;; Read a file name, handling all of the various defaulting rules.
+
+(defun emerge-read-file-name (prompt alternative-default-dir default-file
+                             A-file must-match)
+  ;; `prompt' should not have trailing ": ", so that it can be modified
+  ;; according to context.
+  ;; If alternative-default-dir is non-nil, it should be used as the default
+  ;; directory instead if default-directory, if emerge-default-last-directories
+  ;; is set.
+  ;; If default-file is set, it should be used as the default value.
+  ;; If A-file is set, and its directory is different from
+  ;; alternative-default-dir, and if emerge-default-last-directories is set,
+  ;; the default file should be the last part of A-file in the default
+  ;; directory.  (Overriding default-file.)
+  (cond
+   ;; If this is not the A-file argument (shown by non-nil A-file), and
+   ;; if emerge-default-last-directories is set, and
+   ;; the default directory exists but is not the same as the directory of the
+   ;; A-file,
+   ;; then make the default file have the same name as the A-file, but in
+   ;; the default directory.
+   ((and emerge-default-last-directories
+        A-file
+        alternative-default-dir
+        (not (string-equal alternative-default-dir
+                           (file-name-directory A-file))))
+    (read-file-name (format "%s (default %s): "
+                           prompt (file-name-nondirectory A-file))
+                   alternative-default-dir
+                   (concat alternative-default-dir
+                           (file-name-nondirectory A-file))
+                   (and must-match 'confirm)))
+   ;; If there is a default file, use it.
+   (default-file
+     (read-file-name (format "%s (default %s): " prompt default-file)
+                    ;; If emerge-default-last-directories is set, use the
+                    ;; directory from the same argument of the last call of
+                    ;; Emerge as the default for this argument.
+                    (and emerge-default-last-directories
+                         alternative-default-dir)
+                    default-file (and must-match 'confirm)))
+   (t
+    (read-file-name (concat prompt ": ")
+                   ;; If emerge-default-last-directories is set, use the
+                   ;; directory from the same argument of the last call of
+                   ;; Emerge as the default for this argument.
+                   (and emerge-default-last-directories
+                        alternative-default-dir)
+                   nil (and must-match 'confirm)))))
 
 ;; Revise the mode line to display which difference we have selected
 
@@ -2631,8 +2841,7 @@ might make to the mode line or local keymap.  Leaves merge in fast mode."
                                       (prefer-B . " - B*")
                                       (combined . " - comb"))))
                        ""))))
-  ;; Force mode-line redisplay
-  (set-buffer-modified-p (buffer-modified-p)))
+  (force-mode-line-update))
 
 ;; compare two regions in two buffers for containing the same text
 (defun emerge-compare-buffers (buffer-x x-begin x-end buffer-y y-begin y-end)
@@ -2643,7 +2852,7 @@ might make to the mode line or local keymap.  Leaves merge in fast mode."
       (while (< x-begin x-end)
        ;; bite off and compare no more than 1000 characters at a time
        (let* ((compare-length (min (- x-end x-begin) 1000))
-              (x-string (emerge-eval-in-buffer 
+              (x-string (emerge-eval-in-buffer
                          buffer-x
                          (buffer-substring x-begin
                                            (+ x-begin compare-length))))
@@ -2658,7 +2867,7 @@ might make to the mode line or local keymap.  Leaves merge in fast mode."
       t)))
 
 ;; Construct a unique buffer name.
-;; The first one tried is prefixsuffix, then prefix<2>suffix, 
+;; The first one tried is prefixsuffix, then prefix<2>suffix,
 ;; prefix<3>suffix, etc.
 (defun emerge-unique-buffer-name (prefix suffix)
   (if (null (get-buffer (concat prefix suffix)))
@@ -2680,7 +2889,7 @@ might make to the mode line or local keymap.  Leaves merge in fast mode."
 ;; a list of variables.  The argument is a list of symbols (the names of
 ;; the variables).  A list element can also be a list of two functions,
 ;; the first of which (when called with no arguments) gets the value, and
-;; the second (when called with a value as an argment) sets the value.
+;; the second (when called with a value as an argument) sets the value.
 ;; A "function" is anything that funcall can handle as an argument.
 
 (defun emerge-save-variables (vars)
@@ -2702,31 +2911,33 @@ might make to the mode line or local keymap.  Leaves merge in fast mode."
 ;; Make a temporary file that only we have access to.
 ;; PREFIX is appended to emerge-temp-file-prefix to make the filename prefix.
 (defun emerge-make-temp-file (prefix)
-  (let ((f (make-temp-name (concat emerge-temp-file-prefix prefix))))
-    ;; create the file
-    (write-region (point-min) (point-min) f nil 'no-message)
-    (set-file-modes f emerge-temp-file-mode)
+  (let (f (old-modes (default-file-modes)))
+    (unwind-protect
+       (progn
+         (set-default-file-modes emerge-temp-file-mode)
+         (setq f (make-temp-file (concat emerge-temp-file-prefix prefix))))
+      (set-default-file-modes old-modes))
     f))
 
 ;;; Functions that query the user before he can write out the current buffer.
 
 (defun emerge-query-write-file ()
-  "Query the user if he really wants to write out the incomplete merge.
-If he says yes, call  write-file  to do so.  See  emerge-query-and-call
+  "Ask the user whether to write out an incomplete merge.
+If answer is yes, call `write-file' to do so.  See `emerge-query-and-call'
 for details of the querying process."
   (interactive)
   (emerge-query-and-call 'write-file))
 
 (defun emerge-query-save-buffer ()
-  "Query the user if he really wants to write out the incomplete merge.
-If he says yes, call  save-buffer  to do so.  See  emerge-query-and-call
+  "Ask the user whether to save an incomplete merge.
+If answer is yes, call `save-buffer' to do so.  See `emerge-query-and-call'
 for details of the querying process."
   (interactive)
   (emerge-query-and-call 'save-buffer))
 
 (defun emerge-query-and-call (command)
-  "Query the user if he really wants to write out the incomplete merge.
-If he says yes, call COMMAND interactively.  During the call, the flags
+  "Ask the user whether to save or write out the incomplete merge.
+If answer is yes, call COMMAND interactively.  During the call, the flags
 around the current difference are removed."
   (if (yes-or-no-p "Do you really write to write out this unfinished merge? ")
       ;; He really wants to do it -- unselect the difference for the duration
@@ -2779,8 +2990,8 @@ around the current difference are removed."
 
 ;; Define a key, even if a prefix of it is defined
 (defun emerge-force-define-key (keymap key definition)
-  "Like define-key, but is not stopped if a prefix of KEY is a defined 
-command."
+  "Like `define-key', but forcibly creates prefix characters as needed.
+If some prefix of KEY has a non-prefix definition, it is redefined."
   ;; Find out if a prefix of key is defined
   (let ((v (lookup-key keymap key)))
     ;; If so, undefine it
@@ -2789,73 +3000,78 @@ command."
   ;; Now define the key
   (define-key keymap key definition))
 
-;;; Improvements to describe-mode, so that it describes minor modes as well
-;;; as the major mode
-(defun describe-mode (&optional minor)
-  "Display documentation of current major mode.
-If optional MINOR is non-nil (or prefix argument is given if interactive),
-display documentation of acive minor modes as well.
-For this to work correctly for a minor mode, the mode's indicator variable
-(listed in minor-mode-alist) must also be a function whose documentation
-describes the minor mode."
-  (interactive)
-  (with-output-to-temp-buffer "*Help*"
-    (princ mode-name)
-    (princ " Mode:\n")
-    (princ (documentation major-mode))
-    (let ((minor-modes minor-mode-alist)
-         (locals (buffer-local-variables)))
-      (while minor-modes
-       (let* ((minor-mode (car (car minor-modes)))
-              (indicator (car (cdr (car minor-modes))))
-              (local-binding (assq minor-mode locals)))
-         ;; Document a minor mode if it is listed in minor-mode-alist,
-         ;; bound locally in this buffer, non-nil, and has a function
-         ;; definition.
-         (if (and local-binding
-                  (cdr local-binding)
-                  (fboundp minor-mode))
-             (progn
-               (princ (format "\n\n\n%s minor mode (indicator%s):\n"
-                              minor-mode indicator))
-               (princ (documentation minor-mode)))))
-       (setq minor-modes (cdr minor-modes))))
-    (print-help-return-message)))
-
-;; Adjust things so that keyboard macro definitions are documented correctly.
-(fset 'defining-kbd-macro (symbol-function 'start-kbd-macro))
-
-;; Function to shadow a definition in a keymap with definitions in another.
-(defun emerge-shadow-key-definition (olddef newdef keymap shadowmap)
-  "Shadow OLDDEF with NEWDEF for any keys in KEYMAP with entries in SHADOWMAP.
-In other words, SHADOWMAP will now shadow all definitions of OLDDEF in KEYMAP
-with NEWDEF.  Does not affect keys that are already defined in SHADOWMAP,
-including those whose definition is OLDDEF."
-  ;; loop through all keymaps accessible from keymap
-  (let ((maps (accessible-keymaps keymap)))
-    (while maps
-      (let ((prefix (car (car maps)))
-           (map (cdr (car maps))))
-       ;; examine a keymap
-       (if (arrayp map)
-           ;; array keymap
-           (let ((len (length map))
-                 (i 0))
-             (while (< i len)
-               (if (eq (aref map i) olddef)
-                   ;; set the shadowing definition
-                   (let ((key (concat prefix (char-to-string i))))
-                     (emerge-define-key-if-possible shadowmap key newdef)))
-               (setq i (1+ i))))
-         ;; sparse keymap
-         (while map
-           (if (eq (cdr-safe (car-safe map)) olddef)
-               ;; set the shadowing definition
-               (let ((key
-                      (concat prefix (char-to-string (car (car map))))))
-                     (emerge-define-key-if-possible shadowmap key newdef)))
-           (setq map (cdr map)))))
-      (setq maps (cdr maps)))))
+;;;;; Improvements to describe-mode, so that it describes minor modes as well
+;;;;; as the major mode
+;;(defun describe-mode (&optional minor)
+;;  "Display documentation of current major mode.
+;;If optional arg MINOR is non-nil (or prefix argument is given if interactive),
+;;display documentation of active minor modes as well.
+;;For this to work correctly for a minor mode, the mode's indicator variable
+;;\(listed in `minor-mode-alist') must also be a function whose documentation
+;;describes the minor mode."
+;;  (interactive)
+;;  (with-output-to-temp-buffer "*Help*"
+;;    (princ mode-name)
+;;    (princ " Mode:\n")
+;;    (princ (documentation major-mode))
+;;    (let ((minor-modes minor-mode-alist)
+;;       (locals (buffer-local-variables)))
+;;      (while minor-modes
+;;     (let* ((minor-mode (car (car minor-modes)))
+;;            (indicator (car (cdr (car minor-modes))))
+;;            (local-binding (assq minor-mode locals)))
+;;       ;; Document a minor mode if it is listed in minor-mode-alist,
+;;       ;; bound locally in this buffer, non-nil, and has a function
+;;       ;; definition.
+;;       (if (and local-binding
+;;                (cdr local-binding)
+;;                (fboundp minor-mode))
+;;           (progn
+;;             (princ (format "\n\n\n%s minor mode (indicator%s):\n"
+;;                            minor-mode indicator))
+;;             (princ (documentation minor-mode)))))
+;;     (setq minor-modes (cdr minor-modes))))
+;;    (save-excursion
+;;      (set-buffer standard-output)
+;;      (help-mode))
+;;    (print-help-return-message)))
+
+;; This goes with the redefinition of describe-mode.
+;;;; Adjust things so that keyboard macro definitions are documented correctly.
+;;(fset 'defining-kbd-macro (symbol-function 'start-kbd-macro))
+
+;; substitute-key-definition should work now.
+;;;; Function to shadow a definition in a keymap with definitions in another.
+;;(defun emerge-shadow-key-definition (olddef newdef keymap shadowmap)
+;;  "Shadow OLDDEF with NEWDEF for any keys in KEYMAP with entries in SHADOWMAP.
+;;In other words, SHADOWMAP will now shadow all definitions of OLDDEF in KEYMAP
+;;with NEWDEF.  Does not affect keys that are already defined in SHADOWMAP,
+;;including those whose definition is OLDDEF."
+;;  ;; loop through all keymaps accessible from keymap
+;;  (let ((maps (accessible-keymaps keymap)))
+;;    (while maps
+;;      (let ((prefix (car (car maps)))
+;;         (map (cdr (car maps))))
+;;     ;; examine a keymap
+;;     (if (arrayp map)
+;;         ;; array keymap
+;;         (let ((len (length map))
+;;               (i 0))
+;;           (while (< i len)
+;;             (if (eq (aref map i) olddef)
+;;                 ;; set the shadowing definition
+;;                 (let ((key (concat prefix (char-to-string i))))
+;;                   (emerge-define-key-if-possible shadowmap key newdef)))
+;;             (setq i (1+ i))))
+;;       ;; sparse keymap
+;;       (while map
+;;         (if (eq (cdr-safe (car-safe map)) olddef)
+;;             ;; set the shadowing definition
+;;             (let ((key
+;;                    (concat prefix (char-to-string (car (car map))))))
+;;                   (emerge-define-key-if-possible shadowmap key newdef)))
+;;         (setq map (cdr map)))))
+;;      (setq maps (cdr maps)))))
 
 ;; Define a key if it (or a prefix) is not already defined in the map.
 (defun emerge-define-key-if-possible (keymap key definition)
@@ -2870,16 +3086,17 @@ including those whose definition is OLDDEF."
       (if (not present)
          (define-key keymap key definition)))))
 
-(defun emerge-recursively-substitute-key-definition (olddef newdef keymap)
-  "Like substitute-key-definition, but examines and substitutes in all
-keymaps accessible from KEYMAP.  Make sure that subordinate keymaps aren't
-shared with other keymaps!  (copy-keymap will suffice.)"
-  ;; Loop through all keymaps accessible from keymap
-  (let ((maps (accessible-keymaps keymap)))
-    (while maps
-      ;; Substitute in this keymap
-      (substitute-key-definition olddef newdef (cdr (car maps)))
-      (setq maps (cdr maps)))))
+;; Ordinary substitute-key-definition should do this now.
+;;(defun emerge-recursively-substitute-key-definition (olddef newdef keymap)
+;;  "Like `substitute-key-definition', but act recursively on subkeymaps.
+;;Make sure that subordinate keymaps aren't shared with other keymaps!
+;;\(`copy-keymap' will suffice.)"
+;;  ;; Loop through all keymaps accessible from keymap
+;;  (let ((maps (accessible-keymaps keymap)))
+;;    (while maps
+;;      ;; Substitute in this keymap
+;;      (substitute-key-definition olddef newdef (cdr (car maps)))
+;;      (setq maps (cdr maps)))))
 
 ;; Show the name of the file in the buffer.
 (defun emerge-show-file-name ()
@@ -2893,16 +3110,19 @@ SPC, it is ignored; if it is anything else, it is processed as a command."
        (setq name "Buffer has no file name."))
     (save-window-excursion
       (select-window (minibuffer-window))
-      (erase-buffer)
-      (insert name)
-      (if (not (pos-visible-in-window-p))
-         (let ((echo-keystrokes 0))
-           (while (and (not (pos-visible-in-window-p))
-                       (> (1- (screen-height)) (window-height)))
-             (enlarge-window 1))
-           (let ((c (read-char)))
-             (if (/= c 32)
-                 (setq unread-command-char c))))))))
+      (unwind-protect
+         (progn
+           (erase-buffer)
+           (insert name)
+           (if (not (pos-visible-in-window-p))
+               (while (and (not (pos-visible-in-window-p))
+                           (> (1- (frame-height)) (window-height)))
+                 (enlarge-window 1)))
+           (let* ((echo-keystrokes 0)
+                  (c (read-event)))
+             (if (not (eq c 32))
+                 (setq unread-command-events (list c)))))
+       (erase-buffer)))))
 
 ;; Improved auto-save file names.
 ;; This function fixes many problems with the standard auto-save file names:
@@ -2918,10 +3138,10 @@ SPC, it is ignored; if it is anything else, it is processed as a command."
 ;;           (symbol-function 'emerge-make-auto-save-file-name))
 (defun emerge-make-auto-save-file-name ()
   "Return file name to use for auto-saves of current buffer.
-Does not consider auto-save-visited-file-name; that is checked
-before calling this function.
+Does not consider `auto-save-visited-file-name';
+that is checked before calling this function.
 You can redefine this for customization.
-See also auto-save-file-name-p."
+See also `auto-save-file-name-p'."
   (if buffer-file-name
       ;; if buffer has a file, try the format <file directory>/#<file name>#
       (let ((f (concat (file-name-directory buffer-file-name)
@@ -2937,21 +3157,21 @@ See also auto-save-file-name-p."
                  "/#&"
                  (file-name-nondirectory buffer-file-name)
                  "&"
-                 (hash-string-into-string
+                 (emerge-hash-string-into-string
                   (file-name-directory buffer-file-name))
                  "#")))
     ;; if buffer has no file, use the format ~/#%<buffer name>%<process id>#
     (expand-file-name (concat (getenv "HOME")
                              "/#%"
                              ;; quote / into \! and \ into \\
-                             (unslashify-name (buffer-name))
+                             (emerge-unslashify-name (buffer-name))
                              "%"
                              (make-temp-name "")
                              "#"))))
 
 ;; Hash a string into five characters more-or-less suitable for use in a file
 ;; name.  (Allowed characters are ! through ~, except /.)
-(defun hash-string-into-string (s)
+(defun emerge-hash-string-into-string (s)
   (let ((bins (vector 0 0 0 0 0))
        (i 0))
     (while (< i (length s))
@@ -2968,7 +3188,7 @@ See also auto-save-file-name-p."
 
 ;; Quote any /s in a string by replacing them with \!.
 ;; Also, replace any \s by \\, to make it one-to-one.
-(defun unslashify-name (s)
+(defun emerge-unslashify-name (s)
   (let ((limit 0))
     (while (string-match "[/\\]" s limit)
       (setq s (concat (substring s 0 (match-beginning 0))
@@ -2981,6 +3201,25 @@ See also auto-save-file-name-p."
       (setq limit (1+ (match-end 0)))))
   s)
 
+;; Metacharacters that have to be protected from the shell when executing
+;; a diff/diff3 command.
+(defcustom emerge-metachars "[ \t\n!\"#$&'()*;<=>?[\\^`{|~]"
+  "Characters that must be quoted with \\ when used in a shell command line.
+More precisely, a [...] regexp to match any one such character."
+  :type 'regexp
+  :group 'emerge)
+
+;; Quote metacharacters (using \) when executing a diff/diff3 command.
+(defun emerge-protect-metachars (s)
+  (let ((limit 0))
+    (while (string-match emerge-metachars s limit)
+      (setq s (concat (substring s 0 (match-beginning 0))
+                     "\\"
+                     (substring s (match-beginning 0))))
+      (setq limit (1+ (match-end 0)))))
+  s)
+
 (provide 'emerge)
 
+;;; arch-tag: a575f092-6e44-400e-b8a2-4124e9377585
 ;;; emerge.el ends here