Commits

scop  committed 7f9c07a

Move new vc package from unsupported/scop/vc to main packages tree <200708181923.19296.scop@xemacs.org>

  • Participants
  • Parent commits afc5ecb

Comments (0)

Files changed (15)

+2007-08-21  Ville Skyttä  <scop@xemacs.org>
+
+	* vc-rcs.el (vc-rcs-annotate-command): Use `substring' if
+	`substring-no-properties' is not available, thanks to Adrian Aichner.
+
+2007-08-18  Ville Skyttä  <scop@xemacs.org>
+
+	* Makefile (VERSION): 0.94.
+
+	* mercurial.el: Sync with upstream.
+
+2007-07-16  Ville Skyttä  <scop@xemacs.org>
+
+	* mercurial.el: Sync with upstream.
+	* vc-git.el: Ditto.
+
+2007-07-08  Ville Skyttä  <scop@xemacs.org>
+
+	* Makefile (VERSION): 0.93.
+
+	* Makefile (REQUIRES): Add gnus for `parse-time' in vc-cvs.el.
+
+	* vc.el (vc-dired-purge): Use `dired-do-kill-file-lines'
+	instead of `dired-do-kill-lines'.
+	(vc-directory): Don't try to load dired-aux.
+
+2007-07-07  Ville Skyttä  <scop@xemacs.org>
+
+	* smerge-mode.el: Removed, requires too much porting work to be
+	feasible at the moment.
+	* Makefile (ELCS): Remove smerge-mode.elc.
+	* package-info.in (provides): Remove smerge-mode.
+
+	* vc.el: Bring back old `vc-resolve-conflicts'.
+	* vc-arch.el (vc-arch-find-file-hook): Make smerge-mode optional.
+
+	* Makefile (REQUIRES): Add elib and edebug (pulled in by ediff).
+
+	* vc-arch.el (vc-arch-add-tagline): Call `comment-normalize-vars'
+	only if it's available.
+
+	* vc-svn.el (vc-svn-repository-hostname): Adapt upstream fix
+	for finding out the URL from files created by Subversion >= 1.4.
+
+2007-06-24  Ville Skyttä  <scop@xemacs.org>
+
+	* Makefile (VERSION): 0.92.
+
+	* vc.el, mercurial.el: Sync with upstream.
+
+	* mercurial.el, vc-hooks.el, vc-xemacs.el: Pull in mercurial.el
+	on demand only, like other vc-* modes.
+
+2007-06-16  Ville Skyttä  <scop@xemacs.org>
+
+	* vc-arch.el: Sync with upstream.
+	* vc-svn.el: Ditto.
+	* vc.el: Ditto.
+
+2007-06-06  Ville Skyttä  <scop@xemacs.org>
+
+	* vc-hooks.el: Sync with upstream.
+	* log-edit.el: Ditto.
+
+2007-05-21  Ville Skyttä  <scop@xemacs.org>
+
+	* Makefile (VERSION): 0.91.
+	
+	* mercurial.el: New, from Mercurial itself.
+	* Makefile (ELCS): Add mercurial.el.
+	* package-info.in (provides): Add mercurial.
+	* vc-hooks.el: Add `hg-find-file-hook' to `find-file-hook'.
+
+2007-05-14  Ville Skyttä  <scop@xemacs.org>
+
+	* *.el: Sync with upstream.
+
+2007-02-25  Ville Skyttä  <scop@xemacs.org>
+
+	* vc-svn.el: Sync with upstream.
+
+2007-02-11  Ville Skyttä  <scop@xemacs.org>
+
+	* vc-svn.el: Sync with upstream.
+
+2007-02-03  Ville Skyttä  <scop@xemacs.org>
+
+	* Makefile (VERSION): 0.90.
+
+	* vc-hooks.el (vc-registered): Call vc-BACKEND-registered-lite
+	if vc-BACKEND-registered is not bound to work around XEmacs 21.4
+	byte compiler or bytecode interpreter issues when the real
+	-registered defun is redefined and called by the fully autoloaded
+	version.
+
+	* vc-arch.el (vc-arch-registered-lite):
+	* vc-cvs.el (vc-cvs-registered-lite):
+	* vc-mcvs.el (vc-mcvs-registered-lite):
+	* vc-svn.el (vc-svn-registered-lite):
+	Rename fully autoloaded vc-BACKEND-registered to
+	vc-BACKEND-registered-lite.
+
+	* vc-git.el (vc-git-registered-lite): New, fully autoloaded version.
+
+	* log-edit.el: Sync with upstream.
+
+2007-01-28  Ville Skyttä  <scop@xemacs.org>
+
+	* *.el: Sync with upstream.
+
+2007-01-18  Ville Skyttä  <scop@xemacs.org>
+
+	* log-edit.el: Sync with upstream.
+	* vc-arch.el: Ditto.
+	* vc-git.el: Ditto.
+	* vc-hooks.el: Ditto.
+	* vc-svn.el: Ditto.
+
+2006-11-11  Ville Skyttä  <scop@xemacs.org>
+
+	* vc-svn.el: Sync with upstream.
+
+2006-10-25  Ville Skyttä  <scop@xemacs.org>
+
+	* vc.el: Sync with upstream.
+	* vc-svn.el: Ditto.
+
+2006-10-15  Ville Skyttä  <scop@xemacs.org>
+
+	* vc-git.el: Sync with upstream.
+	* vc-hooks.el: Ditto.
+
+2006-08-29  Ville Skyttä  <scop@xemacs.org>
+
+	* smerge-mode.el (smerge-mine): Comment out unsupported
+	min-colors properties, thanks to Stephen J. Turnbull for the catch.
+	(smerge-base): Ditto.
+
+2006-08-16  Ville Skyttä  <scop@xemacs.org>
+
+	* smerge-mode.el: New from GNU Emacs CVS.  Needs work.
+
+2006-08-15  Ville Skyttä  <scop@xemacs.org>
+
+	* vc-git.el (vc-git-registered): Avoid failures if `git' is not
+	installed, thanks to Adrian Aichner for the catch.
+
+2006-08-05  Ville Skyttä  <scop@xemacs.org>
+
+	* vc.el: Sync with upstream.
+
+	* vc-git.el: Changes applied upstream.
+
+2006-07-06  Ville Skyttä  <scop@xemacs.org>
+
+	* vc.el: Sync with upstream.
+
+2006-07-04  Ville Skyttä  <scop@xemacs.org>
+
+	* Makefile (VERSION): 0.05.
+
+	* vc-git.el: New, from git upstream.
+	(vc-git-print-log): Add optional BUFFER arg, use it.
+	(vc-git-diff): Ditto.
+	* vc-hooks.el (vc-handled-backends): Add GIT.
+	(vc-mode-line): Ditto.
+	* package-info.in (provides): Add vc-git.
+	* Makefile (ELCS): Add vc-git.elc.
+
+	* vc.el (vc-coding-system-for-diff): We don't have
+	`find-operation-coding-system'.
+
+	* vc.el (vc-revert-buffer): Don't try to delete the revert
+	confirmation diff window for now as it breaks for some
+	reason, and prevents the actual revert from happening.
+
+	* vc.el (vc-restore-buffer-context): Test for mark before using.
+
+	* vc-xemacs.el: Comment update.
+
+2006-06-28  Ville Skyttä  <scop@xemacs.org>
+
+	* vc.el: Sync with upstream.
+
+2006-06-12  Ville Skyttä  <scop@xemacs.org>
+
+	* Makefile (VERSION): 0.04.
+
+	* vc.el: Sync with upstream.
+
+2006-05-01  Ville Skyttä  <scop@xemacs.org>
+
+	* vc.el (vc-default-show-log-entry): Workaround for (at least so
+	far) missing log-view stuff.
+
+2006-04-29  Ville Skyttä  <scop@xemacs.org>
+
+	* vc.el (vc-annotate-warp-version): Use `forward-line' instead
+	of `previous-line'.
+
+2006-04-22  Ville Skyttä  <scop@xemacs.org>
+
+	* Makefile (VERSION): 0.03.
+
+	* vc.el (vc-annotate): `goto-line' in XEmacs takes only 1 arg,
+	emulate GNU Emacs' 2-arg one.
+	(vc-annotate-warp-version): Ditto.
+
+	* vc-cvs.el (vc-cvs-parse-root): Fix :local: root parsing.
+
+2006-04-21  Ville Skyttä  <scop@xemacs.org>
+
+	* Makefile (VERSION): 0.02.
+
+	* vc-hooks.el: Bind C-x C-q to `vc-toggle-read-only' like in the
+	old vc package, thanks to Vegard Vesterheim.
+
+2006-04-16  Ville Skyttä  <scop@xemacs.org>
+
+	* vc.el: Sync with upstream.
+
+	* vc-xemacs.el (line-number-at-pos): Sync compat implementation
+	with XEmacs 21.5.
+
+2006-02-11  Ville Skyttä  <scop@xemacs.org>
+
+	* *.el: Sync with upstream.
+	* Makefile (REQUIRES): Add tramp.
+
+2005-12-31  Ville Skyttä  <scop@xemacs.org>
+
+	* vc.el: Sync with upstream.
+
+2005-12-26  Ville Skyttä  <scop@xemacs.org>
+
+	* vc.el, vc-mcvs.el, vc-svn.el: Sync with upstream.
+
+2005-12-05  Ville Skyttä  <scop@xemacs.org>
+
+	* log-edit.el: Sync with upstream.
+
+	* log-edit.el: Use pcl-cvs-util (already in the packages tree).
+	* Makefile (REQUIRES): Add pcl-cvs.
+	* Makefile (ELCS): Remove pcvs-util.elc.
+	* package-info-in (provides): Remove pcvs-util.
+	* pcvs-util.el: Remove.
+	* vc-xemacs.el (compare-strings): Remove, only used by pcvs-util.
+
+2005-11-28  Ville Skyttä  <scop@xemacs.org>
+
+	* log-edit.el: Sync with upstream.
+
+2005-11-15  Ville Skyttä  <scop@xemacs.org>
+
+	* vc-svn.el: Sync with upstream.
+
+	* vc-xemacs.el (compare-strings): New, borrowed from ECB.
+
+2005-11-10  Oscar Figueiredo  <oscar@xemacs.org>
+
+	* vc-xemacs.el (subst-char-in-string): Do not use `replace-in-string'
+	because of the regexp semantic on its second parameter.
+
+2005-11-10  Ville Skyttä  <scop@xemacs.org>
+
+	* vc-xemacs.el: Update FSF's address.
+
+2005-11-02  Ville Skyttä  <scop@xemacs.org>
+
+	* vc.el, vc-cvs.el, vc-sccs.el: Sync with upstream.
+
+2005-10-08  Ville Skyttä  <scop@xemacs.org>
+
+	* log-edit.el, vc.el, vc-mcvs.el: Sync with upstream.
+
+2005-09-11  Ville Skyttä  <scop@xemacs.org>
+
+	* vc.el: Sync with upstream.
+
+2005-08-16  Ville Skyttä  <scop@xemacs.org>
+
+	* *.el: Sync with upstream.
+
+2005-07-24  Ville Skyttä  <scop@xemacs.org>
+
+	* *.el: Sync with upstream.
+
+2005-06-21  Ville Skyttä  <scop@xemacs.org>
+
+	* log-edit.el, vc-arch.el, vc-cvs.el, vc.el: Sync with upstream.
+
+2005-04-30  Ville Skyttä  <scop@xemacs.org>
+
+	* vc.el: Sync with upstream.
+
+2005-04-09  Ville Skyttä  <scop@xemacs.org>
+
+	* pcvs-util.el, vc.el, vc-cvs.el, vc-hooks.el: Sync with upstream.
+
+2005-03-06  Ville Skyttä  <scop@xemacs.org>
+
+	* vc.el: Sync with upstream.
+
+2005-02-11  Ville Skyttä  <scop@xemacs.org>
+
+	* vc*.el: Sync with upstream.
+
+2005-01-19  Ville Skyttä  <scop@xemacs.org>
+
+	* vc*.el: Sync with upstream.
+
+2005-01-06  Ville Skyttä  <scop@xemacs.org>
+
+	* vc-arch.el, vc-rcs.el, vc-svn.el: Sync with upstream.
+
+2004-12-19  Ville Skyttä  <scop@xemacs.org>
+
+	* vc-cvs.el: Port the `vc-cvs-program' defcustom from the old
+	XEmacs vc package, and use it.  Originally by Christian Schmitt.
+
+2004-12-18  Ville Skyttä  <scop@xemacs.org>
+
+	* vc.el: Sync with upstream.
+
+2004-12-15  Ville Skyttä  <scop@xemacs.org>
+
+	* vc-svn.el: Sync with upstream.  Adds svn annotate support,
+	and includes my repository-hostname fix.
+
+2004-12-13  Ville Skyttä  <scop@xemacs.org>
+
+	* log-edit.el: New, from upstream CVS; temporarily (?) here so
+	that editing commit messages could actually work.
+	* pcvs-util.el: Ditto.
+
+2004-12-12  Ville Skyttä  <scop@xemacs.org>
+
+	* vc-cvs.el (vc-cvs-parse-root): XEmacs wants 2+ args to `setq'.
+
+	* vc-svn.el (vc-svn-repository-hostname): Make it work also with
+	Subversion >= 0.33.0, simplify regexp.
+
+2004-12-06  Ville Skyttä  <scop@xemacs.org>
+
+	* vc.el, vc-cvs.el, vc-hooks.el, vc-rcs.el: Sync with upstream.
+
+2004-11-10  Ville Skyttä  <scop@xemacs.org>
+
+	* vc.el, vc-cvs.el, vc-mcvs.el: Sync with upstream.
+
+2004-11-01  Ville Skyttä  <scop@xemacs.org>
+
+	* vc.el, vc-arch.el, vc-hooks.el: Sync with upstream.
+
+2004-10-13  Ville Skyttä  <scop@xemacs.org>
+
+	* vc.el: Sync with upstream.
+
+2004-09-15  Ville Skyttä  <scop@xemacs.org>
+
+	* vc*.el: Sync with upstream.
+
+2004-08-25  Ville Skyttä  <scop@xemacs.org>
+
+	* vc-svn.el: Sync with upstream.
+
+2004-08-15  Ville Skyttä  <scop@xemacs.org>
+
+	* *: Move most of the XEmacs compatibility stuff into vc-xemacs.el,
+	add some TODOs and fix a few incompatibilities.
+
+2004-07-30  Ville Skyttä  <scop@xemacs.org>
+
+	* vc-arch.el: Use replace-in-string instead of subst-char-in-string
+	and replace-regexp-in-string.
+
+2004-07-03  Ville Skyttä  <scop@xemacs.org>
+
+	* vc-arch.el, vc-rcs.el: Sync with upstream.
+
+2004-06-05  Ville Skyttä  <scop@xemacs.org>
+
+	* vc-svn.el: Sync with upstream.
+
+2004-06-01  Ville Skyttä  <scop@xemacs.org>
+
+	* vc-arch.el: Sync with upstream.
+
+2004-05-29  Ville Skyttä  <scop@xemacs.org>
+
+	* vc-arch.el, vc-mcvs.el: Sync with upstream.
+
+2004-05-24  Ville Skyttä  <scop@xemacs.org>
+
+	* Initial revision of vc port from GNU Emacs.
+
 2005-06-05  Norbert Koch  <viteno@xemacs.org>
 
 	* Makefile (VERSION): XEmacs package 1.41 released.
-# Makefile for XEmacs base lisp code
+# Makefile for VC lisp code
 
 # This file is part of XEmacs.
 
 MAINTAINER = XEmacs Development Team <xemacs-beta@xemacs.org>
 PACKAGE = vc
 PKG_TYPE = regular
-REQUIRES = dired xemacs-base vc mail-lib ediff
+REQUIRES = dired xemacs-base vc mail-lib ediff sh-script pcl-cvs tramp \
+	prog-modes elib edebug gnus
 CATEGORY = standard
 
-ELCS = vc-hooks.elc vc.elc
+ELCS = vc-hooks.elc vc.elc vc-arch.elc vc-cvs.elc vc-git.elc vc-mcvs.elc \
+	vc-rcs.elc vc-sccs.elc vc-svn.elc mercurial.elc vc-xemacs.elc \
+	log-edit.elc
 
 include ../../XEmacs.rules
+;;; log-edit.el --- Major mode for editing CVS commit messages
+
+;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004,
+;;   2005, 2006, 2007 Free Software Foundation, Inc.
+
+;; Author: Stefan Monnier <monnier@iro.umontreal.ca>
+;; Keywords: pcl-cvs cvs commit log
+
+;; This file is part of GNU Emacs.
+
+;; GNU Emacs is free software; you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation; either version 2, or (at your option)
+;; any later version.
+
+;; GNU Emacs is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+;; GNU General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with GNU Emacs; see the file COPYING.  If not, write to the
+;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+;; Boston, MA 02110-1301, USA.
+
+;;; Commentary:
+
+;; Todo:
+
+;; - Move in VC's code
+;; - Add compatibility for VC's hook variables
+
+;;; Code:
+
+(eval-when-compile (require 'cl))
+(require 'add-log)			; for all the ChangeLog goodies
+;; XEmacs change: pcvs-util -> pcl-cvs-util
+(require 'pcl-cvs-util)
+(require 'ring)
+
+;;;;
+;;;; Global Variables
+;;;;
+
+(defgroup log-edit nil
+  "Major mode for editing RCS and CVS commit messages."
+  :group 'pcl-cvs
+  :group 'vc				; It's used by VC.
+  :version "21.1"
+  :prefix "log-edit-")
+
+;; compiler pacifiers
+(defvar cvs-buffer)
+
+
+;; The main keymap
+
+(easy-mmode-defmap log-edit-mode-map
+  `(("\C-c\C-c" . log-edit-done)
+    ("\C-c\C-a" . log-edit-insert-changelog)
+    ("\C-c\C-f" . log-edit-show-files)
+    ("\M-n"	. log-edit-next-comment)
+    ("\M-p"	. log-edit-previous-comment)
+    ("\M-r"	. log-edit-comment-search-backward)
+    ("\M-s"	. log-edit-comment-search-forward)
+    ("\C-c?"	. log-edit-mode-help))
+  "Keymap for the `log-edit-mode' (to edit version control log messages)."
+  :group 'log-edit)
+
+;; Compatibility with old names.  Should we bother ?
+(defvar vc-log-mode-map log-edit-mode-map)
+(defvar vc-log-entry-mode vc-log-mode-map)
+
+(easy-menu-define log-edit-menu log-edit-mode-map
+  "Menu used for `log-edit-mode'."
+  '("Log-Edit"
+    ["Done" log-edit-done
+     :help "Exit log-edit and proceed with the actual action."]
+    "--"
+    ["Insert ChangeLog" log-edit-insert-changelog]
+    ["Add to ChangeLog" log-edit-add-to-changelog]
+    "--"
+    ["List files" log-edit-show-files
+     :help "Show the list of relevant files."]
+    "--"
+    ["Previous comment"		log-edit-previous-comment]
+    ["Next comment"		log-edit-next-comment]
+    ["Search comment forward"	log-edit-comment-search-forward]
+    ["Search comment backward"	log-edit-comment-search-backward]))
+
+(defcustom log-edit-confirm 'changed
+  "*If non-nil, `log-edit-done' will request confirmation.
+If 'changed, only request confirmation if the list of files has
+  changed since the beginning of the log-edit session."
+  :group 'log-edit
+  :type '(choice (const changed) (const t) (const nil)))
+
+(defcustom log-edit-keep-buffer nil
+  "*If non-nil, don't hide the buffer after `log-edit-done'."
+  :group 'log-edit
+  :type 'boolean)
+
+(defvar cvs-commit-buffer-require-final-newline t)
+(make-obsolete-variable 'cvs-commit-buffer-require-final-newline
+                        'log-edit-require-final-newline)
+
+(defcustom log-edit-require-final-newline
+  cvs-commit-buffer-require-final-newline
+  "*Enforce a newline at the end of commit log messages.
+Enforce it silently if t, query if non-nil and don't do anything if nil."
+  :group 'log-edit
+  :type '(choice (const ask) (const t) (const nil)))
+
+(defcustom log-edit-setup-invert nil
+  "*Non-nil means `log-edit' should invert the meaning of its SETUP arg.
+If SETUP is 'force, this variable has no effect."
+  :group 'log-edit
+  :type 'boolean)
+
+(defcustom log-edit-hook '(log-edit-insert-cvs-template
+			   log-edit-insert-changelog)
+  "*Hook run at the end of `log-edit'."
+  :group 'log-edit
+  :type '(hook :options (log-edit-insert-changelog
+                         log-edit-insert-cvs-rcstemplate
+                         log-edit-insert-cvs-template
+			 log-edit-insert-filenames)))
+
+(defcustom log-edit-mode-hook (if (boundp 'vc-log-mode-hook) vc-log-mode-hook)
+  "*Hook run when entering `log-edit-mode'."
+  :group 'log-edit
+  :type 'hook)
+
+(defcustom log-edit-done-hook nil
+  "*Hook run before doing the actual commit.
+This hook can be used to cleanup the message, enforce various
+conventions, or to allow recording the message in some other database,
+such as a bug-tracking system.  The list of files about to be committed
+can be obtained from `log-edit-files'."
+  :group 'log-edit
+  :type '(hook :options (log-edit-set-common-indentation
+			 log-edit-add-to-changelog)))
+
+(defvar cvs-changelog-full-paragraphs t)
+(make-obsolete-variable 'cvs-changelog-full-paragraphs
+                        'log-edit-changelog-full-paragraphs)
+
+(defvar log-edit-changelog-full-paragraphs cvs-changelog-full-paragraphs
+  "*If non-nil, include full ChangeLog paragraphs in the log.
+This may be set in the ``local variables'' section of a ChangeLog, to
+indicate the policy for that ChangeLog.
+
+A ChangeLog paragraph is a bunch of log text containing no blank lines;
+a paragraph usually describes a set of changes with a single purpose,
+but perhaps spanning several functions in several files.  Changes in
+different paragraphs are unrelated.
+
+You could argue that the log entry for a file should contain the
+full ChangeLog paragraph mentioning the change to the file, even though
+it may mention other files, because that gives you the full context you
+need to understand the change.  This is the behavior you get when this
+variable is set to t.
+
+On the other hand, you could argue that the log entry for a change
+should contain only the text for the changes which occurred in that
+file, because the log is per-file.  This is the behavior you get
+when this variable is set to nil.")
+
+;;;; Internal global or buffer-local vars
+
+(defconst log-edit-files-buf "*log-edit-files*")
+(defvar log-edit-initial-files nil)
+(defvar log-edit-callback nil)
+(defvar log-edit-listfun nil)
+(defvar log-edit-parent-buffer nil)
+
+;;; Originally taken from VC-Log mode
+
+(defconst log-edit-maximum-comment-ring-size 32
+  "Maximum number of saved comments in the comment ring.")
+(defvar log-edit-comment-ring (make-ring log-edit-maximum-comment-ring-size))
+(defvar log-edit-comment-ring-index nil)
+(defvar log-edit-last-comment-match "")
+
+(defun log-edit-new-comment-index (stride len)
+  "Return the comment index STRIDE elements from the current one.
+LEN is the length of `log-edit-comment-ring'."
+  (mod (cond
+	(log-edit-comment-ring-index (+ log-edit-comment-ring-index stride))
+	;; Initialize the index on the first use of this command
+	;; so that the first M-p gets index 0, and the first M-n gets
+	;; index -1.
+	((> stride 0) (1- stride))
+	(t stride))
+       len))
+
+(defun log-edit-previous-comment (arg)
+  "Cycle backwards through comment history.
+With a numeric prefix ARG, go back ARG comments."
+  (interactive "*p")
+  (let ((len (ring-length log-edit-comment-ring)))
+    (if (<= len 0)
+	(progn (message "Empty comment ring") (ding))
+      ;; Don't use `erase-buffer' because we don't want to `widen'.
+      (delete-region (point-min) (point-max))
+      (setq log-edit-comment-ring-index (log-edit-new-comment-index arg len))
+      (message "Comment %d" (1+ log-edit-comment-ring-index))
+      (insert (ring-ref log-edit-comment-ring log-edit-comment-ring-index)))))
+
+(defun log-edit-next-comment (arg)
+  "Cycle forwards through comment history.
+With a numeric prefix ARG, go forward ARG comments."
+  (interactive "*p")
+  (log-edit-previous-comment (- arg)))
+
+(defun log-edit-comment-search-backward (str &optional stride)
+  "Search backwards through comment history for substring match of STR.
+If the optional argument STRIDE is present, that is a step-width to use
+when going through the comment ring."
+  ;; Why substring rather than regexp ?   -sm
+  (interactive
+   (list (read-string "Comment substring: " nil nil log-edit-last-comment-match)))
+  (unless stride (setq stride 1))
+  (if (string= str "")
+      (setq str log-edit-last-comment-match)
+    (setq log-edit-last-comment-match str))
+  (let* ((str (regexp-quote str))
+	 (len (ring-length log-edit-comment-ring))
+	 (n (log-edit-new-comment-index stride len)))
+    (while (progn (when (or (>= n len) (< n 0)) (error "Not found"))
+		  (not (string-match str (ring-ref log-edit-comment-ring n))))
+      (setq n (+ n stride)))
+    (setq log-edit-comment-ring-index n)
+    (log-edit-previous-comment 0)))
+
+(defun log-edit-comment-search-forward (str)
+  "Search forwards through comment history for a substring match of STR."
+  (interactive
+   (list (read-string "Comment substring: " nil nil log-edit-last-comment-match)))
+  (log-edit-comment-search-backward str -1))
+
+(defun log-edit-comment-to-change-log (&optional whoami file-name)
+  "Enter last VC comment into the change log for the current file.
+WHOAMI (interactive prefix) non-nil means prompt for user name
+and site.  FILE-NAME is the name of the change log; if nil, use
+`change-log-default-name'.
+
+This may be useful as a `log-edit-checkin-hook' to update change logs
+automatically."
+  (interactive (if current-prefix-arg
+		   (list current-prefix-arg
+			 (prompt-for-change-log-name))))
+  (let (;; Extract the comment first so we get any error before doing anything.
+	(comment (ring-ref log-edit-comment-ring 0))
+	;; Don't let add-change-log-entry insert a defun name.
+	(add-log-current-defun-function 'ignore)
+	end)
+    ;; Call add-log to do half the work.
+    (add-change-log-entry whoami file-name t t)
+    ;; Insert the VC comment, leaving point before it.
+    (setq end (save-excursion (insert comment) (point-marker)))
+    (if (looking-at "\\s *\\s(")
+	;; It starts with an open-paren, as in "(foo): Frobbed."
+	;; So remove the ": " add-log inserted.
+	(delete-char -2))
+    ;; Canonicalize the white space between the file name and comment.
+    (just-one-space)
+    ;; Indent rest of the text the same way add-log indented the first line.
+    (let ((indentation (current-indentation)))
+      (save-excursion
+	(while (< (point) end)
+	  (forward-line 1)
+	  (indent-to indentation))
+	(setq end (point))))
+    ;; Fill the inserted text, preserving open-parens at bol.
+    (let ((paragraph-start (concat paragraph-start "\\|\\s *\\s(")))
+      (beginning-of-line)
+      (fill-region (point) end))
+    ;; Canonicalize the white space at the end of the entry so it is
+    ;; separated from the next entry by a single blank line.
+    (skip-syntax-forward " " end)
+    (delete-char (- (skip-syntax-backward " ")))
+    (or (eobp) (looking-at "\n\n")
+	(insert "\n"))))
+
+;; Compatibility with old names.
+;; XEmacs change: only 2 args to `define-obsolete-*-alias'
+(define-obsolete-variable-alias 'vc-comment-ring 'log-edit-comment-ring)
+(define-obsolete-variable-alias 'vc-comment-ring-index 'log-edit-comment-ring-index)
+(define-obsolete-function-alias 'vc-previous-comment 'log-edit-previous-comment)
+(define-obsolete-function-alias 'vc-next-comment 'log-edit-next-comment)
+(define-obsolete-function-alias 'vc-comment-search-reverse 'log-edit-comment-search-backward)
+(define-obsolete-function-alias 'vc-comment-search-forward 'log-edit-comment-search-forward)
+(define-obsolete-function-alias 'vc-comment-to-change-log 'log-edit-comment-to-change-log)
+
+;;;
+;;; Actual code
+;;;
+
+(defvar log-edit-font-lock-keywords
+  '(("\\`\\(Summary:\\)\\(.*\\)"
+     (1 font-lock-keyword-face)
+     (2 font-lock-function-name-face))))
+
+;;;###autoload
+(defun log-edit (callback &optional setup listfun buffer &rest ignore)
+  "Setup a buffer to enter a log message.
+\\<log-edit-mode-map>The buffer will be put in `log-edit-mode'.
+If SETUP is non-nil, the buffer is then erased and `log-edit-hook' is run.
+Mark and point will be set around the entire contents of the
+buffer so that it is easy to kill the contents of the buffer with \\[kill-region].
+Once you're done editing the message, pressing \\[log-edit-done] will call
+`log-edit-done' which will end up calling CALLBACK to do the actual commit.
+LISTFUN if non-nil is a function of no arguments returning the list of files
+  that are concerned by the current operation (using relative names).
+If BUFFER is non-nil `log-edit' will jump to that buffer, use it to edit the
+  log message and go back to the current buffer when done.  Otherwise, it
+  uses the current buffer."
+  (let ((parent (current-buffer)))
+    (if buffer (pop-to-buffer buffer))
+    (when (and log-edit-setup-invert (not (eq setup 'force)))
+      (setq setup (not setup)))
+    (when setup (erase-buffer))
+    (log-edit-mode)
+    (set (make-local-variable 'log-edit-callback) callback)
+    (set (make-local-variable 'log-edit-listfun) listfun)
+    (if buffer (set (make-local-variable 'log-edit-parent-buffer) parent))
+    (set (make-local-variable 'log-edit-initial-files) (log-edit-files))
+    (when setup (run-hooks 'log-edit-hook))
+    (goto-char (point-min)) (push-mark (point-max))
+    (message "%s" (substitute-command-keys
+	      "Press \\[log-edit-done] when you are done editing."))))
+
+(define-derived-mode log-edit-mode text-mode "Log-Edit"
+  "Major mode for editing version-control log messages.
+When done editing the log entry, just type \\[log-edit-done] which
+will trigger the actual commit of the file(s).
+Several other handy support commands are provided of course and
+the package from which this is used might also provide additional
+commands (under C-x v for VC, for example).
+
+\\{log-edit-mode-map}"
+  (set (make-local-variable 'font-lock-defaults)
+       '(log-edit-font-lock-keywords t))
+  (make-local-variable 'log-edit-comment-ring-index))
+
+(defun log-edit-hide-buf (&optional buf where)
+  (when (setq buf (get-buffer (or buf log-edit-files-buf)))
+    (let ((win (get-buffer-window buf where)))
+      (if win (ignore-errors (delete-window win))))
+    (bury-buffer buf)))
+
+(defun log-edit-done ()
+  "Finish editing the log message and commit the files.
+If you want to abort the commit, simply delete the buffer."
+  (interactive)
+  ;; Get rid of trailing empty lines
+  (goto-char (point-max))
+  (skip-syntax-backward " ")
+  (when (equal (char-after) ?\n) (forward-char 1))
+  (delete-region (point) (point-max))
+  ;; Check for final newline
+  (if (and (> (point-max) (point-min))
+	   (/= (char-before (point-max)) ?\n)
+	   (or (eq log-edit-require-final-newline t)
+	       (and log-edit-require-final-newline
+		    (y-or-n-p
+		     (format "Buffer %s does not end in newline.  Add one? "
+			     (buffer-name))))))
+      (save-excursion
+	(goto-char (point-max))
+	(insert ?\n)))
+  (let ((comment (buffer-string)))
+    (when (or (ring-empty-p log-edit-comment-ring)
+	      (not (equal comment (ring-ref log-edit-comment-ring 0))))
+      (ring-insert log-edit-comment-ring comment)))
+  (let ((win (get-buffer-window log-edit-files-buf)))
+    (if (and log-edit-confirm
+	     (not (and (eq log-edit-confirm 'changed)
+		       (equal (log-edit-files) log-edit-initial-files)))
+	     (progn
+	       (log-edit-show-files)
+	       (not (y-or-n-p "Really commit? "))))
+	(progn (when (not win) (log-edit-hide-buf))
+	       (message "Oh, well!  Later maybe?"))
+      (run-hooks 'log-edit-done-hook)
+      (log-edit-hide-buf)
+      (unless (or log-edit-keep-buffer (not log-edit-parent-buffer))
+	(cvs-bury-buffer (current-buffer) log-edit-parent-buffer))
+      (call-interactively log-edit-callback))))
+
+(defun log-edit-files ()
+  "Return the list of files that are about to be committed."
+  (ignore-errors (funcall log-edit-listfun)))
+
+(defun log-edit-mode-help ()
+  "Provide help for the `log-edit-mode-map'."
+  (interactive)
+  (if (eq last-command 'log-edit-mode-help)
+      (describe-function major-mode)
+    (message "%s"
+     (substitute-command-keys
+      "Type `\\[log-edit-done]' to finish commit.  Try `\\[describe-function] log-edit-done' for more help."))))
+
+(defcustom log-edit-common-indent 0
+  "Minimum indentation to use in `log-edit-set-common-indentation'."
+  :group 'log-edit
+  :type 'integer)
+
+(defun log-edit-set-common-indentation ()
+  "(Un)Indent the current buffer rigidly to `log-edit-common-indent'."
+  (save-excursion
+    (let ((common (point-max)))
+      (goto-char (point-min))
+      (while (< (point) (point-max))
+        (if (not (looking-at "^[ \t]*$"))
+            (setq common (min common (current-indentation))))
+        (forward-line 1))
+      (indent-rigidly (point-min) (point-max)
+		      (- log-edit-common-indent common)))))
+
+(defun log-edit-show-files ()
+  "Show the list of files to be committed."
+  (interactive)
+  (let* ((files (log-edit-files))
+	 (buf (get-buffer-create log-edit-files-buf)))
+    (with-current-buffer buf
+      (log-edit-hide-buf buf 'all)
+      (setq buffer-read-only nil)
+      (erase-buffer)
+      (cvs-insert-strings files)
+      (setq buffer-read-only t)
+      (goto-char (point-min))
+      (save-selected-window
+	(cvs-pop-to-buffer-same-frame buf)
+	(shrink-window-if-larger-than-buffer)
+	(selected-window)))))
+
+(defun log-edit-insert-cvs-template ()
+  "Insert the template specified by the CVS administrator, if any.
+This simply uses the local CVS/Template file."
+  (interactive)
+  (when (or (interactive-p) (= (point-min) (point-max)))
+    (when (file-readable-p "CVS/Template")
+      (insert-file-contents "CVS/Template"))))
+
+(defun log-edit-insert-cvs-rcstemplate ()
+  "Insert the rcstemplate from the CVS repository.
+This contacts the repository to get the rcstemplate file and
+can thus take some time."
+  (interactive)
+  (when (or (interactive-p) (= (point-min) (point-max)))
+    (when (file-readable-p "CVS/Root")
+      ;; Ignore the stderr stuff, even if it's an error.
+      (call-process "cvs" nil '(t nil) nil
+                    "checkout" "-p" "CVSROOT/rcstemplate"))))
+
+(defun log-edit-insert-filenames ()
+  "Insert the list of files that are to be committed."
+  (interactive)
+  (insert "Affected files:  \n"
+          (mapconcat 'identity (log-edit-files) "  \n")))
+
+(defun log-edit-add-to-changelog ()
+  "Insert this log message into the appropriate ChangeLog file."
+  (interactive)
+  ;; Yuck!
+  (unless (string= (buffer-string) (ring-ref log-edit-comment-ring 0))
+    (ring-insert log-edit-comment-ring (buffer-string)))
+  (dolist (f (log-edit-files))
+    (let ((buffer-file-name (expand-file-name f)))
+      (save-excursion
+	(log-edit-comment-to-change-log)))))
+
+(defvar log-edit-changelog-use-first nil)
+(defun log-edit-insert-changelog (&optional use-first)
+  "Insert a log message by looking at the ChangeLog.
+The idea is to write your ChangeLog entries first, and then use this
+command to commit your changes.
+
+To select default log text, we:
+- find the ChangeLog entries for the files to be checked in,
+- verify that the top entry in the ChangeLog is on the current date
+  and by the current user; if not, we don't provide any default text,
+- search the ChangeLog entry for paragraphs containing the names of
+  the files we're checking in, and finally
+- use those paragraphs as the log text.
+
+If the optional prefix arg USE-FIRST is given (via \\[universal-argument]),
+or if the command is repeated a second time in a row, use the first log entry
+regardless of user name or time."
+  (interactive "P")
+  (let ((log-edit-changelog-use-first
+	 (or use-first (eq last-command 'log-edit-insert-changelog))))
+    (log-edit-insert-changelog-entries (log-edit-files)))
+  (log-edit-set-common-indentation)
+  (goto-char (point-min))
+  (when (looking-at "\\*\\s-+")
+    (forward-line 1)
+    (when (not (re-search-forward "^\\*\\s-+" nil t))
+      (goto-char (point-min))
+      (skip-chars-forward "^():")
+      (skip-chars-forward ": ")
+      (delete-region (point-min) (point)))))
+
+;;;;
+;;;; functions for getting commit message from ChangeLog a file...
+;;;; Courtesy Jim Blandy
+;;;;
+
+(defun log-edit-narrow-changelog ()
+  "Narrow to the top page of the current buffer, a ChangeLog file.
+Actually, the narrowed region doesn't include the date line.
+A \"page\" in a ChangeLog file is the area between two dates."
+  (or (eq major-mode 'change-log-mode)
+      (error "log-edit-narrow-changelog: current buffer isn't a ChangeLog"))
+
+  (goto-char (point-min))
+
+  ;; Skip date line and subsequent blank lines.
+  (forward-line 1)
+  (if (looking-at "[ \t\n]*\n")
+      (goto-char (match-end 0)))
+
+  (let ((start (point)))
+    (forward-page 1)
+    (narrow-to-region start (point))
+    (goto-char (point-min))))
+
+(defun log-edit-changelog-paragraph ()
+  "Return the bounds of the ChangeLog paragraph containing point.
+If we are between paragraphs, return the previous paragraph."
+  (save-excursion
+    (beginning-of-line)
+    (if (looking-at "^[ \t]*$")
+        (skip-chars-backward " \t\n" (point-min)))
+    (list (progn
+            (if (re-search-backward "^[ \t]*\n" nil 'or-to-limit)
+                (goto-char (match-end 0)))
+            (point))
+          (if (re-search-forward "^[ \t\n]*$" nil t)
+              (match-beginning 0)
+            (point-max)))))
+
+(defun log-edit-changelog-subparagraph ()
+  "Return the bounds of the ChangeLog subparagraph containing point.
+A subparagraph is a block of non-blank lines beginning with an asterisk.
+If we are between sub-paragraphs, return the previous subparagraph."
+  (save-excursion
+    (end-of-line)
+    (if (search-backward "*" nil t)
+        (list (progn (beginning-of-line) (point))
+              (progn
+                (forward-line 1)
+                (if (re-search-forward "^[ \t]*[\n*]" nil t)
+                    (match-beginning 0)
+                  (point-max))))
+      (list (point) (point)))))
+
+(defun log-edit-changelog-entry ()
+  "Return the bounds of the ChangeLog entry containing point.
+The variable `log-edit-changelog-full-paragraphs' decides whether an
+\"entry\" is a paragraph or a subparagraph; see its documentation string
+for more details."
+  (if log-edit-changelog-full-paragraphs
+      (log-edit-changelog-paragraph)
+    (log-edit-changelog-subparagraph)))
+
+(defvar user-full-name)
+(defvar user-mail-address)
+(defun log-edit-changelog-ours-p ()
+  "See if ChangeLog entry at point is for the current user, today.
+Return non-nil iff it is."
+  ;; Code adapted from add-change-log-entry.
+  (let ((name (or (and (boundp 'add-log-full-name) add-log-full-name)
+		  (and (fboundp 'user-full-name) (user-full-name))
+		  (and (boundp 'user-full-name) user-full-name)))
+        (mail (or (and (boundp 'add-log-mailing-address) add-log-mailing-address)
+		  ;;(and (fboundp 'user-mail-address) (user-mail-address))
+		  (and (boundp 'user-mail-address) user-mail-address)))
+	(time (or (and (boundp 'add-log-time-format)
+		       (functionp add-log-time-format)
+		       (funcall add-log-time-format))
+		  (format-time-string "%Y-%m-%d"))))
+    (looking-at (if log-edit-changelog-use-first
+                    "[^ \t]"
+                  (regexp-quote (format "%s  %s  <%s>" time name mail))))))
+
+(defun log-edit-changelog-entries (file)
+  "Return the ChangeLog entries for FILE, and the ChangeLog they came from.
+The return value looks like this:
+  (LOGBUFFER (ENTRYSTART . ENTRYEND) ...)
+where LOGBUFFER is the name of the ChangeLog buffer, and each
+\(ENTRYSTART . ENTRYEND\) pair is a buffer region."
+  (save-excursion
+    (let ((changelog-file-name
+	   (let ((default-directory
+		   (file-name-directory (expand-file-name file)))
+		 (visiting-buffer (find-buffer-visiting file)))
+	     ;; If there is a buffer visiting FILE, and it has a local
+	     ;; value for `change-log-default-name', use that.
+	     (if (and visiting-buffer
+		      (local-variable-p 'change-log-default-name
+					visiting-buffer))
+		 (save-excursion
+		   (set-buffer visiting-buffer)
+		   change-log-default-name)
+	       ;; `find-change-log' uses `change-log-default-name' if set
+	       ;; and sets it before exiting, so we need to work around
+	       ;; that memoizing which is undesired here
+	       (setq change-log-default-name nil)
+	       (find-change-log)))))
+      (set-buffer (find-file-noselect changelog-file-name))
+      (unless (eq major-mode 'change-log-mode) (change-log-mode))
+      (goto-char (point-min))
+      (if (looking-at "\\s-*\n") (goto-char (match-end 0)))
+      (if (not (log-edit-changelog-ours-p))
+	  (list (current-buffer))
+	(save-restriction
+	  (log-edit-narrow-changelog)
+	  (goto-char (point-min))
+
+	  ;; Search for the name of FILE relative to the ChangeLog.  If that
+	  ;; doesn't occur anywhere, they're not using full relative
+	  ;; filenames in the ChangeLog, so just look for FILE; we'll accept
+	  ;; some false positives.
+	  (let ((pattern (file-relative-name
+			  file (file-name-directory changelog-file-name))))
+	    (if (or (string= pattern "")
+		    (not (save-excursion
+			   (search-forward pattern nil t))))
+		(setq pattern (file-name-nondirectory file)))
+
+            ;; XEmacs change: [:alnum:] -> A-Za-z0-9 (duh)
+            (setq pattern (concat "\\(^\\|[^A-Za-z0-9]\\)"
+                                  pattern
+                                  "\\($\\|[^A-Za-z0-9]\\)"))
+
+	    (let (texts)
+	      (while (re-search-forward pattern nil t)
+		(let ((entry (log-edit-changelog-entry)))
+		  (push entry texts)
+		  (goto-char (elt entry 1))))
+
+	      (cons (current-buffer) texts))))))))
+
+(defun log-edit-changelog-insert-entries (buffer regions)
+  "Insert those regions in BUFFER specified in REGIONS.
+Sort REGIONS front-to-back first."
+  (let ((regions (sort regions 'car-less-than-car))
+        (last))
+    (dolist (region regions)
+      (when (and last (< last (car region))) (newline))
+      (setq last (elt region 1))
+      (apply 'insert-buffer-substring buffer region))))
+
+(defun log-edit-insert-changelog-entries (files)
+  "Given a list of files FILES, insert the ChangeLog entries for them."
+  (let ((buffer-entries nil))
+
+    ;; Add each buffer to buffer-entries, and associate it with the list
+    ;; of entries we want from that file.
+    (dolist (file files)
+      (let* ((entries (log-edit-changelog-entries file))
+             (pair (assq (car entries) buffer-entries)))
+        (if pair
+            (setcdr pair (cvs-union (cdr pair) (cdr entries)))
+          (push entries buffer-entries))))
+
+    ;; Now map over each buffer in buffer-entries, sort the entries for
+    ;; each buffer, and extract them as strings.
+    (dolist (buffer-entry buffer-entries)
+      (log-edit-changelog-insert-entries (car buffer-entry) (cdr buffer-entry))
+      (when (cdr buffer-entry) (newline)))))
+
+(provide 'log-edit)
+
+;; arch-tag: 8089b39c-983b-4e83-93cd-ed0a64c7fdcc
+;;; log-edit.el ends here

File mercurial.el

+;;; mercurial.el --- Emacs support for the Mercurial distributed SCM
+
+;; Copyright (C) 2005, 2006 Bryan O'Sullivan
+
+;; Author: Bryan O'Sullivan <bos@serpentine.com>
+
+;; mercurial.el is free software; you can redistribute it and/or
+;; modify it under the terms of version 2 of the GNU General Public
+;; License as published by the Free Software Foundation.
+
+;; mercurial.el is distributed in the hope that it will be useful, but
+;; WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+;; General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with mercurial.el, GNU Emacs, or XEmacs; see the file COPYING
+;; (`C-h C-l').  If not, write to the Free Software Foundation, Inc.,
+;; 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+
+;;; Commentary:
+
+;; mercurial.el builds upon Emacs's VC mode to provide flexible
+;; integration with the Mercurial distributed SCM tool.
+
+;; To get going as quickly as possible, load mercurial.el into Emacs and
+;; type `C-c h h'; this runs hg-help-overview, which prints a helpful
+;; usage overview.
+
+;; Much of the inspiration for mercurial.el comes from Rajesh
+;; Vaidheeswarran's excellent p4.el, which does an admirably thorough
+;; job for the commercial Perforce SCM product.  In fact, substantial
+;; chunks of code are adapted from p4.el.
+
+;; This code has been developed under XEmacs 21.5, and may not work as
+;; well under GNU Emacs (albeit tested under 21.4).  Patches to
+;; enhance the portability of this code, fix bugs, and add features
+;; are most welcome.  You can clone a Mercurial repository for this
+;; package from http://www.serpentine.com/hg/hg-emacs
+
+;; Please send problem reports and suggestions to bos@serpentine.com.
+
+
+;;; Code:
+
+(eval-when-compile (require 'cl))
+(require 'diff-mode)
+(require 'easymenu)
+(require 'executable)
+(require 'vc)
+
+(defmacro hg-feature-cond (&rest clauses)
+  "Test CLAUSES for feature at compile time.
+Each clause is (FEATURE BODY...)."
+  (dolist (x clauses)
+    (let ((feature (car x))
+	  (body (cdr x)))
+      (when (or (eq feature t)
+		(featurep feature))
+	(return (cons 'progn body))))))
+
+
+;;; XEmacs has view-less, while GNU Emacs has view.  Joy.
+
+(hg-feature-cond
+ (xemacs (require 'view-less))
+ (t (require 'view)))
+
+
+;;; Variables accessible through the custom system.
+
+(defgroup mercurial nil
+  "Mercurial distributed SCM."
+  :group 'tools)
+
+(defcustom hg-binary
+    (or (executable-find "hg")
+	(dolist (path '("~/bin/hg" "/usr/bin/hg" "/usr/local/bin/hg"))
+	  (when (file-executable-p path)
+	    (return path))))
+  "The path to Mercurial's hg executable."
+  :type '(file :must-match t)
+  :group 'mercurial)
+
+(defcustom hg-mode-hook nil
+  "Hook run when a buffer enters hg-mode."
+  :type 'sexp
+  :group 'mercurial)
+
+(defcustom hg-commit-mode-hook nil
+  "Hook run when a buffer is created to prepare a commit."
+  :type 'sexp
+  :group 'mercurial)
+
+(defcustom hg-pre-commit-hook nil
+  "Hook run before a commit is performed.
+If you want to prevent the commit from proceeding, raise an error."
+  :type 'sexp
+  :group 'mercurial)
+
+(defcustom hg-log-mode-hook nil
+  "Hook run after a buffer is filled with log information."
+  :type 'sexp
+  :group 'mercurial)
+
+(defcustom hg-global-prefix "\C-ch"
+  "The global prefix for Mercurial keymap bindings."
+  :type 'sexp
+  :group 'mercurial)
+
+(defcustom hg-commit-allow-empty-message nil
+  "Whether to allow changes to be committed with empty descriptions."
+  :type 'boolean
+  :group 'mercurial)
+
+(defcustom hg-commit-allow-empty-file-list nil
+  "Whether to allow changes to be committed without any modified files."
+  :type 'boolean
+  :group 'mercurial)
+
+(defcustom hg-rev-completion-limit 100
+  "The maximum number of revisions that hg-read-rev will offer to complete.
+This affects memory usage and performance when prompting for revisions
+in a repository with a lot of history."
+  :type 'integer
+  :group 'mercurial)
+
+(defcustom hg-log-limit 50
+  "The maximum number of revisions that hg-log will display."
+  :type 'integer
+  :group 'mercurial)
+
+(defcustom hg-update-modeline t
+  "Whether to update the modeline with the status of a file after every save.
+Set this to nil on platforms with poor process management, such as Windows."
+  :type 'boolean
+  :group 'mercurial)
+
+(defcustom hg-incoming-repository "default"
+  "The repository from which changes are pulled from by default.
+This should be a symbolic repository name, since it is used for all
+repository-related commands."
+  :type 'string
+  :group 'mercurial)
+
+(defcustom hg-outgoing-repository "default-push"
+  "The repository to which changes are pushed to by default.
+This should be a symbolic repository name, since it is used for all
+repository-related commands."
+  :type 'string
+  :group 'mercurial)
+
+
+;;; Other variables.
+
+(defvar hg-mode nil
+  "Is this file managed by Mercurial?")
+(make-variable-buffer-local 'hg-mode)
+(put 'hg-mode 'permanent-local t)
+
+(defvar hg-status nil)
+(make-variable-buffer-local 'hg-status)
+(put 'hg-status 'permanent-local t)
+
+(defvar hg-prev-buffer nil)
+(make-variable-buffer-local 'hg-prev-buffer)
+(put 'hg-prev-buffer 'permanent-local t)
+
+(defvar hg-root nil)
+(make-variable-buffer-local 'hg-root)
+(put 'hg-root 'permanent-local t)
+
+(defvar hg-view-mode nil)
+(make-variable-buffer-local 'hg-view-mode)
+(put 'hg-view-mode 'permanent-local t)
+
+(defvar hg-view-file-name nil)
+(make-variable-buffer-local 'hg-view-file-name)
+(put 'hg-view-file-name 'permanent-local t)
+
+(defvar hg-output-buffer-name "*Hg*"
+  "The name to use for Mercurial output buffers.")
+
+(defvar hg-file-history nil)
+(defvar hg-repo-history nil)
+(defvar hg-rev-history nil)
+(defvar hg-repo-completion-table nil)	; shut up warnings
+
+
+;;; Random constants.
+
+(defconst hg-commit-message-start
+  "--- Enter your commit message.  Type `C-c C-c' to commit. ---\n")
+
+(defconst hg-commit-message-end
+  "--- Files in bold will be committed.  Click to toggle selection. ---\n")
+
+(defconst hg-state-alist
+  '((?M . modified)
+    (?A . added)
+    (?R . removed)
+    (?! . deleted)
+    (?C . normal)
+    (?I . ignored)
+    (?? . nil)))
+
+;;; hg-mode keymap.
+
+(defvar hg-prefix-map
+  (let ((map (make-sparse-keymap)))
+    (hg-feature-cond (xemacs (set-keymap-name map 'hg-prefix-map))) ; XEmacs
+    (set-keymap-parent map vc-prefix-map)
+    (define-key map "=" 'hg-diff)
+    (define-key map "c" 'hg-undo)
+    (define-key map "g" 'hg-annotate)
+    (define-key map "i" 'hg-add)
+    (define-key map "l" 'hg-log)
+    (define-key map "n" 'hg-commit-start)
+    ;; (define-key map "r" 'hg-update)
+    (define-key map "u" 'hg-revert-buffer)
+    (define-key map "~" 'hg-version-other-window)
+    map)
+  "This keymap overrides some default vc-mode bindings.")
+
+(defvar hg-mode-map
+  (let ((map (make-sparse-keymap)))
+    (define-key map "\C-xv" hg-prefix-map)
+    map))
+
+(add-minor-mode 'hg-mode 'hg-mode hg-mode-map)
+
+
+;;; Global keymap.
+
+(defvar hg-global-map
+  (let ((map (make-sparse-keymap)))
+    (define-key map "," 'hg-incoming)
+    (define-key map "." 'hg-outgoing)
+    (define-key map "<" 'hg-pull)
+    (define-key map "=" 'hg-diff-repo)
+    (define-key map ">" 'hg-push)
+    (define-key map "?" 'hg-help-overview)
+    (define-key map "A" 'hg-addremove)
+    (define-key map "U" 'hg-revert)
+    (define-key map "a" 'hg-add)
+    (define-key map "c" 'hg-commit-start)
+    (define-key map "f" 'hg-forget)
+    (define-key map "h" 'hg-help-overview)
+    (define-key map "i" 'hg-init)
+    (define-key map "l" 'hg-log-repo)
+    (define-key map "r" 'hg-root)
+    (define-key map "s" 'hg-status)
+    (define-key map "u" 'hg-update)
+    map))
+
+(global-set-key hg-global-prefix hg-global-map)
+
+;;; View mode keymap.
+
+(defvar hg-view-mode-map
+  (let ((map (make-sparse-keymap)))
+    (hg-feature-cond (xemacs (set-keymap-name map 'hg-view-mode-map))) ; XEmacs
+    (define-key map (hg-feature-cond (xemacs [button2])
+				     (t [mouse-2]))
+      'hg-buffer-mouse-clicked)
+    map))
+
+(add-minor-mode 'hg-view-mode "" hg-view-mode-map)
+
+
+;;; Commit mode keymaps.
+
+(defvar hg-commit-mode-map
+  (let ((map (make-sparse-keymap)))
+    (define-key map "\C-c\C-c" 'hg-commit-finish)
+    (define-key map "\C-c\C-k" 'hg-commit-kill)
+    (define-key map "\C-xv=" 'hg-diff-repo)
+    map))
+
+(defvar hg-commit-mode-file-map
+  (let ((map (make-sparse-keymap)))
+    (define-key map (hg-feature-cond (xemacs [button2])
+				     (t [mouse-2]))
+      'hg-commit-mouse-clicked)
+    (define-key map " " 'hg-commit-toggle-file)
+    (define-key map "\r" 'hg-commit-toggle-file)
+    map))
+
+
+;;; Convenience functions.
+
+(defsubst hg-binary ()
+  (if hg-binary
+      hg-binary
+    (error "No `hg' executable found!")))
+
+(defsubst hg-replace-in-string (str regexp newtext &optional literal)
+  "Replace all matches in STR for REGEXP with NEWTEXT string.
+Return the new string.  Optional LITERAL non-nil means do a literal
+replacement.
+
+This function bridges yet another pointless impedance gap between
+XEmacs and GNU Emacs."
+  (hg-feature-cond
+   (xemacs (replace-in-string str regexp newtext literal))
+   (t (replace-regexp-in-string regexp newtext str nil literal))))
+
+(defsubst hg-strip (str)
+  "Strip leading and trailing blank lines from a string."
+  (hg-replace-in-string (hg-replace-in-string str "[\r\n][ \t\r\n]*\\'" "")
+			"\\`[ \t\r\n]*[\r\n]" ""))
+
+(defsubst hg-chomp (str)
+  "Strip trailing newlines from a string."
+  (hg-replace-in-string str "[\r\n]+\\'" ""))
+
+(defun hg-run-command (command &rest args)
+  "Run the shell command COMMAND, returning (EXIT-CODE . COMMAND-OUTPUT).
+The list ARGS contains a list of arguments to pass to the command."
+  (let* (exit-code
+	 (output
+	  (with-output-to-string
+	    (with-current-buffer
+		standard-output
+	      (setq exit-code
+		    (apply 'call-process command nil t nil args))))))
+    (cons exit-code output)))
+
+(defun hg-run (command &rest args)
+  "Run the Mercurial command COMMAND, returning (EXIT-CODE . COMMAND-OUTPUT)."
+  (apply 'hg-run-command (hg-binary) command args))
+
+(defun hg-run0 (command &rest args)
+  "Run the Mercurial command COMMAND, returning its output.
+If the command does not exit with a zero status code, raise an error."
+  (let ((res (apply 'hg-run-command (hg-binary) command args)))
+    (if (not (eq (car res) 0))
+	(error "Mercurial command failed %s - exit code %s"
+	       (cons command args)
+	       (car res))
+      (cdr res))))
+
+(defmacro hg-do-across-repo (path &rest body)
+  (let ((root-name (make-symbol "root-"))
+	(buf-name (make-symbol "buf-")))
+    `(let ((,root-name (hg-root ,path)))
+       (save-excursion
+	 (dolist (,buf-name (buffer-list))
+	   (set-buffer ,buf-name)
+	   (when (and hg-status (equal (hg-root buffer-file-name) ,root-name))
+	     ,@body))))))
+
+(put 'hg-do-across-repo 'lisp-indent-function 1)
+
+(defun hg-sync-buffers (path)
+  "Sync buffers visiting PATH with their on-disk copies.
+If PATH is not being visited, but is under the repository root, sync
+all buffers visiting files in the repository."
+  (let ((buf (find-buffer-visiting path)))
+    (if buf
+	(with-current-buffer buf
+	  (vc-buffer-sync))
+      (hg-do-across-repo path
+	(vc-buffer-sync)))))
+
+(defun hg-buffer-commands (pnt)
+  "Use the properties of a character to do something sensible."
+  (interactive "d")
+  (let ((rev (get-char-property pnt 'rev))
+	(file (get-char-property pnt 'file)))
+    (cond
+     (file
+      (find-file-other-window file))
+     (rev
+      (hg-diff hg-view-file-name rev rev))
+     ((message "I don't know how to do that yet")))))
+
+(defsubst hg-event-point (event)
+  "Return the character position of the mouse event EVENT."
+  (hg-feature-cond (xemacs (event-point event))
+		   (t (posn-point (event-start event)))))
+
+(defsubst hg-event-window (event)
+  "Return the window over which mouse event EVENT occurred."
+  (hg-feature-cond (xemacs (event-window event))
+		   (t (posn-window (event-start event)))))
+
+(defun hg-buffer-mouse-clicked (event)
+  "Translate the mouse clicks in a HG log buffer to character events.
+These are then handed off to `hg-buffer-commands'.
+
+Handle frickin' frackin' gratuitous event-related incompatibilities."
+  (interactive "e")
+  (select-window (hg-event-window event))
+  (hg-buffer-commands (hg-event-point event)))
+
+(defsubst hg-abbrev-file-name (file)
+  "Portable wrapper around abbreviate-file-name."
+  (hg-feature-cond (xemacs (abbreviate-file-name file t))
+		   (t (abbreviate-file-name file))))
+
+(defun hg-read-file-name (&optional prompt default)
+  "Read a file or directory name, or a pattern, to use with a command."
+  (save-excursion
+    (while hg-prev-buffer
+      (set-buffer hg-prev-buffer))
+    (let ((path (or default
+                    (buffer-file-name)
+                    (expand-file-name default-directory))))
+      (if (or (not path) current-prefix-arg)
+          (expand-file-name
+           (eval (list* 'read-file-name
+                        (format "File, directory or pattern%s: "
+                                (or prompt ""))
+                        (and path (file-name-directory path))
+                        nil nil
+                        (and path (file-name-nondirectory path))
+                        (hg-feature-cond
+			 (xemacs (cons (quote 'hg-file-history) nil))
+			 (t nil)))))
+        path))))
+
+(defun hg-read-number (&optional prompt default)
+  "Read a integer value."
+  (save-excursion
+    (if (or (not default) current-prefix-arg)
+        (string-to-number
+         (eval (list* 'read-string
+                      (or prompt "")
+                      (if default (cons (format "%d" default) nil) nil))))
+      default)))
+
+(defun hg-read-config ()
+  "Return an alist of (key . value) pairs of Mercurial config data.
+Each key is of the form (section . name)."
+  (let (items)
+    (dolist (line (split-string (hg-chomp (hg-run0 "debugconfig")) "\n") items)
+      (string-match "^\\([^=]*\\)=\\(.*\\)" line)
+      (let* ((left (substring line (match-beginning 1) (match-end 1)))
+	     (right (substring line (match-beginning 2) (match-end 2)))
+	     (key (split-string left "\\."))
+	     (value (hg-replace-in-string right "\\\\n" "\n" t)))
+	(setq items (cons (cons (cons (car key) (cadr key)) value) items))))))
+
+(defun hg-config-section (section config)
+  "Return an alist of (name . value) pairs for SECTION of CONFIG."
+  (let (items)
+    (dolist (item config items)
+      (when (equal (caar item) section)
+	(setq items (cons (cons (cdar item) (cdr item)) items))))))
+
+(defun hg-string-starts-with (sub str)
+  "Indicate whether string STR starts with the substring or character SUB."
+  (if (not (stringp sub))
+      (and (> (length str) 0) (equal (elt str 0) sub))
+    (let ((sub-len (length sub)))
+      (and (<= sub-len (length str))
+	   (string= sub (substring str 0 sub-len))))))
+
+(defun hg-complete-repo (string predicate all)
+  "Attempt to complete a repository name.
+We complete on either symbolic names from Mercurial's config or real
+directory names from the file system.  We do not penalise URLs."
+  (or (if all
+	  (all-completions string hg-repo-completion-table predicate)
+	(try-completion string hg-repo-completion-table predicate))
+      (let* ((str (expand-file-name string))
+	     (dir (file-name-directory str))
+	     (file (file-name-nondirectory str)))
+	(if all
+	    (let (completions)
+	      (dolist (name (delete "./" (file-name-all-completions file dir))
+			    completions)
+		(let ((path (concat dir name)))
+		  (when (file-directory-p path)
+		    (setq completions (cons name completions))))))
+	  (let ((comp (file-name-completion file dir)))
+	    (if comp
+		(hg-abbrev-file-name (concat dir comp))))))))
+
+(defun hg-read-repo-name (&optional prompt initial-contents default)
+  "Read the location of a repository."
+  (save-excursion
+    (while hg-prev-buffer
+      (set-buffer hg-prev-buffer))
+    (let (hg-repo-completion-table)
+      (if current-prefix-arg
+	  (progn
+	    (dolist (path (hg-config-section "paths" (hg-read-config)))
+	      (setq hg-repo-completion-table
+		    (cons (cons (car path) t) hg-repo-completion-table))
+	      (unless (hg-string-starts-with (hg-feature-cond
+					      (xemacs directory-sep-char)
+					      (t ?/))
+					     (cdr path))
+		(setq hg-repo-completion-table
+		      (cons (cons (cdr path) t) hg-repo-completion-table))))
+	    (completing-read (format "Repository%s: " (or prompt ""))
+			     'hg-complete-repo
+			     nil
+			     nil
+			     initial-contents
+			     'hg-repo-history
+			     default))
+	default))))
+
+(defun hg-read-rev (&optional prompt default)
+  "Read a revision or tag, offering completions."
+  (save-excursion
+    (while hg-prev-buffer
+      (set-buffer hg-prev-buffer))
+    (let ((rev (or default "tip")))
+      (if current-prefix-arg
+	  (let ((revs (split-string
+		       (hg-chomp
+			(hg-run0 "-q" "log" "-l"
+				 (format "%d" hg-rev-completion-limit)))
+		       "[\n:]")))
+	    (dolist (line (split-string (hg-chomp (hg-run0 "tags")) "\n"))
+	      (setq revs (cons (car (split-string line "\\s-")) revs)))
+	    (completing-read (format "Revision%s (%s): "
+				     (or prompt "")
+				     (or default "tip"))
+			     (map 'list 'cons revs revs)
+			     nil
+			     nil
+			     nil
+			     'hg-rev-history
+			     (or default "tip")))
+	rev))))
+
+(defun hg-parents-for-mode-line (root)
+  "Format the parents of the working directory for the mode line."
+  (let ((parents (split-string (hg-chomp
+				(hg-run0 "--cwd" root "parents" "--template"
+					 "{rev}\n")) "\n")))
+    (mapconcat 'identity parents "+")))
+
+(defun hg-buffers-visiting-repo (&optional path)
+  "Return a list of buffers visiting the repository containing PATH."
+  (let ((root-name (hg-root (or path (buffer-file-name))))
+	bufs)
+    (save-excursion
+      (dolist (buf (buffer-list) bufs)
+	(set-buffer buf)
+	(let ((name (buffer-file-name)))
+	  (when (and hg-status name (equal (hg-root name) root-name))
+	    (setq bufs (cons buf bufs))))))))
+
+(defun hg-update-mode-lines (path)
+  "Update the mode lines of all buffers visiting the same repository as PATH."
+  (let* ((root (hg-root path))
+	 (parents (hg-parents-for-mode-line root)))
+    (save-excursion
+      (dolist (info (hg-path-status
+		     root
+		     (mapcar
+		      (function
+		       (lambda (buf)
+			 (substring (buffer-file-name buf) (length root))))
+		      (hg-buffers-visiting-repo root))))
+	(let* ((name (car info))
+	       (status (cdr info))
+	       (buf (find-buffer-visiting (concat root name))))
+	  (when buf
+	    (set-buffer buf)
+	    (hg-mode-line-internal status parents)))))))
+
+
+;;; View mode bits.
+
+(defun hg-exit-view-mode (buf)
+  "Exit from hg-view-mode.
+We delete the current window if entering hg-view-mode split the
+current frame."
+  (when (and (eq buf (current-buffer))
+	     (> (length (window-list)) 1))
+    (delete-window))
+  (when (buffer-live-p buf)
+    (kill-buffer buf)))
+
+(defun hg-view-mode (prev-buffer &optional file-name)
+  (goto-char (point-min))
+  (set-buffer-modified-p nil)
+  (toggle-read-only t)
+  (hg-feature-cond (xemacs (view-minor-mode prev-buffer 'hg-exit-view-mode))
+		   (t (view-mode-enter nil 'hg-exit-view-mode)))
+  (setq hg-view-mode t)
+  (setq truncate-lines t)
+  (when file-name
+    (setq hg-view-file-name
+	  (hg-abbrev-file-name file-name))))
+
+(defun hg-file-status (file)
+  "Return status of FILE, or nil if FILE does not exist or is unmanaged."
+  (let* ((s (hg-run "status" file))
+	 (exit (car s))
+	 (output (cdr s)))
+    (if (= exit 0)
+	(let ((state (and (>= (length output) 2)
+			  (= (aref output 1) ? )
+			  (assq (aref output 0) hg-state-alist))))
+	  (if state
+	      (cdr state)
+	    'normal)))))
+
+(defun hg-path-status (root paths)
+  "Return status of PATHS in repo ROOT as an alist.
+Each entry is a pair (FILE-NAME . STATUS)."
+  (let ((s (apply 'hg-run "--cwd" root "status" "-marduc" paths))
+	result)
+    (dolist (entry (split-string (hg-chomp (cdr s)) "\n") (nreverse result))
+      (let (state name)
+	(cond ((= (aref entry 1) ? )
+	       (setq state (assq (aref entry 0) hg-state-alist)
+		     name (substring entry 2)))
+	      ((string-match "\\(.*\\): " entry)
+	       (setq name (match-string 1 entry))))
+	(setq result (cons (cons name state) result))))))
+
+(defmacro hg-view-output (args &rest body)
+  "Execute BODY in a clean buffer, then quickly display that buffer.
+If the buffer contains one line, its contents are displayed in the
+minibuffer.  Otherwise, the buffer is displayed in view-mode.
+ARGS is of the form (BUFFER-NAME &optional FILE), where BUFFER-NAME is
+the name of the buffer to create, and FILE is the name of the file
+being viewed."
+  (let ((prev-buf (make-symbol "prev-buf-"))
+	(v-b-name (car args))
+	(v-m-rest (cdr args)))
+    `(let ((view-buf-name ,v-b-name)
+	   (,prev-buf (current-buffer)))
+       (get-buffer-create view-buf-name)
+       (kill-buffer view-buf-name)
+       (get-buffer-create view-buf-name)
+       (set-buffer view-buf-name)
+       (save-excursion
+	 ,@body)
+       (case (count-lines (point-min) (point-max))
+	 ((0)
+	  (kill-buffer view-buf-name)
+	  (message "(No output)"))
+	 ((1)
+	  (let ((msg (hg-chomp (buffer-substring (point-min) (point-max)))))
+	    (kill-buffer view-buf-name)
+	    (message "%s" msg)))
+	 (t
+	  (pop-to-buffer view-buf-name)
+	  (setq hg-prev-buffer ,prev-buf)
+	  (hg-view-mode ,prev-buf ,@v-m-rest))))))
+
+(put 'hg-view-output 'lisp-indent-function 1)
+
+;;; Context save and restore across revert and other operations.
+
+(defun hg-position-context (pos)
+  "Return information to help find the given position again."
+  (let* ((end (min (point-max) (+ pos 98))))
+    (list pos
+	  (buffer-substring (max (point-min) (- pos 2)) end)
+	  (- end pos))))
+
+(defun hg-buffer-context ()
+  "Return information to help restore a user's editing context.
+This is useful across reverts and merges, where a context is likely
+to have moved a little, but not really changed."
+  (let ((point-context (hg-position-context (point)))
+	(mark-context (let ((mark (mark-marker)))
+			(and mark (hg-position-context mark)))))
+    (list point-context mark-context)))
+
+(defun hg-find-context (ctx)
+  "Attempt to find a context in the given buffer.
+Always returns a valid, hopefully sane, position."
+  (let ((pos (nth 0 ctx))
+	(str (nth 1 ctx))
+	(fixup (nth 2 ctx)))
+    (save-excursion
+      (goto-char (max (point-min) (- pos 15000)))
+      (if (and (not (equal str ""))
+	       (search-forward str nil t))
+	  (- (point) fixup)
+	(max pos (point-min))))))
+
+(defun hg-restore-context (ctx)
+  "Attempt to restore the user's editing context."
+  (let ((point-context (nth 0 ctx))
+	(mark-context (nth 1 ctx)))
+    (goto-char (hg-find-context point-context))
+    (when mark-context
+      (set-mark (hg-find-context mark-context)))))
+
+
+;;; Hooks.
+
+(defun hg-mode-line-internal (status parents)
+  (setq hg-status status
+	hg-mode (and status (concat " Hg:"
+				    parents
+				    (cdr (assq status
+					       '((normal . "")
+						 (removed . "r")
+						 (added . "a")
+						 (deleted . "!")
+						 (modified . "m"))))))))
+
+(defun hg-mode-line (&optional force)
+  "Update the modeline with the current status of a file.
+An update occurs if optional argument FORCE is non-nil,
+hg-update-modeline is non-nil, or we have not yet checked the state of
+the file."
+  (let ((root (hg-root)))
+    (when (and root (or force hg-update-modeline (not hg-mode)))
+      (let ((status (hg-file-status buffer-file-name))
+	    (parents (hg-parents-for-mode-line root)))
+	(hg-mode-line-internal status parents)
+	status))))
+
+(defun hg-mode (&optional toggle)
+  "Minor mode for Mercurial distributed SCM integration.
+
+The Mercurial mode user interface is based on that of VC mode, so if
+you're already familiar with VC, the same keybindings and functions
+will generally work.
+
+Below is a list of many common SCM tasks.  In the list, `G/L\'
+indicates whether a key binding is global (G) to a repository or
+local (L) to a file.  Many commands take a prefix argument.
+
+SCM Task                              G/L  Key Binding  Command Name
+--------                              ---  -----------  ------------
+Help overview (what you are reading)  G    C-c h h      hg-help-overview
+
+Tell Mercurial to manage a file       G    C-c h a      hg-add
+Commit changes to current file only   L    C-x v n      hg-commit-start
+Undo changes to file since commit     L    C-x v u      hg-revert-buffer
+
+Diff file vs last checkin             L    C-x v =      hg-diff
+
+View file change history              L    C-x v l      hg-log
+View annotated file                   L    C-x v a      hg-annotate
+
+Diff repo vs last checkin             G    C-c h =      hg-diff-repo
+View status of files in repo          G    C-c h s      hg-status
+Commit all changes                    G    C-c h c      hg-commit-start
+
+Undo all changes since last commit    G    C-c h U      hg-revert
+View repo change history              G    C-c h l      hg-log-repo
+
+See changes that can be pulled        G    C-c h ,      hg-incoming
+Pull changes                          G    C-c h <      hg-pull
+Update working directory after pull   G    C-c h u      hg-update
+See changes that can be pushed        G    C-c h .      hg-outgoing
+Push changes                          G    C-c h >      hg-push"
+  (unless vc-make-backup-files
+    (set (make-local-variable 'backup-inhibited) t))
+  (run-hooks 'hg-mode-hook))
+
+(defun hg-find-file-hook ()
+  (ignore-errors
+    (when (hg-mode-line)
+      (hg-mode))))
+
+(add-hook 'find-file-hooks 'hg-find-file-hook)
+
+(defun hg-after-save-hook ()
+  (ignore-errors
+    (let ((old-status hg-status))
+      (hg-mode-line)
+      (if (and (not old-status) hg-status)
+	  (hg-mode)))))
+
+(add-hook 'after-save-hook 'hg-after-save-hook)
+
+
+;;; User interface functions.
+
+(defun hg-help-overview ()
+  "This is an overview of the Mercurial SCM mode for Emacs.
+
+You can find the source code, license (GPL v2), and credits for this
+code by typing `M-x find-library mercurial RET'."
+  (interactive)
+  (hg-view-output ("Mercurial Help Overview")
+    (insert (documentation 'hg-help-overview))
+    (let ((pos (point)))
+      (insert (documentation 'hg-mode))
+      (goto-char pos)
+      (end-of-line 1)
+      (delete-region pos (point)))
+    (let ((hg-root-dir (hg-root)))
+      (if (not hg-root-dir)
+	  (error "error: %s: directory is not part of a Mercurial repository."
+		 default-directory)
+	(cd hg-root-dir)))))
+
+(defun hg-fix-paths ()
+  "Fix paths reported by some Mercurial commands."
+  (save-excursion
+    (goto-char (point-min))
+    (while (re-search-forward " \\.\\.." nil t)
+      (replace-match " " nil nil))))
+
+(defun hg-add (path)
+  "Add PATH to the Mercurial repository on the next commit.
+With a prefix argument, prompt for the path to add."
+  (interactive (list (hg-read-file-name " to add")))
+  (let ((buf (current-buffer))
+	(update (equal buffer-file-name path)))
+    (hg-view-output (hg-output-buffer-name)
+      (apply 'call-process (hg-binary) nil t nil (list "add" path))
+      (hg-fix-paths)
+      (goto-char (point-min))
+      (cd (hg-root path)))
+    (when update
+      (unless vc-make-backup-files
+	(set (make-local-variable 'backup-inhibited) t))
+      (with-current-buffer buf
+	(hg-mode-line)))))
+
+(defun hg-addremove ()
+  (interactive)
+  (error "not implemented"))
+
+(defun hg-annotate ()
+  (interactive)
+  (error "not implemented"))
+
+(defun hg-commit-toggle-file (pos)
+  "Toggle whether or not the file at POS will be committed."
+  (interactive "d")
+  (save-excursion
+    (goto-char pos)
+    (let ((face (get-text-property pos 'face))
+	  (inhibit-read-only t)
+	  bol)
+      (beginning-of-line)
+      (setq bol (+ (point) 4))
+      (end-of-line)
+      (if (eq face 'bold)
+	  (progn
+	    (remove-text-properties bol (point) '(face nil))
+	    (message "%s will not be committed"
+		     (buffer-substring bol (point))))
+	(add-text-properties bol (point) '(face bold))
+	(message "%s will be committed"
+		 (buffer-substring bol (point)))))))
+
+(defun hg-commit-mouse-clicked (event)
+  "Toggle whether or not the file at POS will be committed."
+  (interactive "@e")
+  (hg-commit-toggle-file (hg-event-point event)))
+
+(defun hg-commit-kill ()
+  "Kill the commit currently being prepared."
+  (interactive)
+  (when (or (not (buffer-modified-p)) (y-or-n-p "Really kill this commit? "))
+    (let ((buf hg-prev-buffer))
+      (kill-buffer nil)
+      (switch-to-buffer buf))))
+
+(defun hg-commit-finish ()
+  "Finish preparing a commit, and perform the actual commit.
+The hook hg-pre-commit-hook is run before anything else is done.  If
+the commit message is empty and hg-commit-allow-empty-message is nil,
+an error is raised.  If the list of files to commit is empty and
+hg-commit-allow-empty-file-list is nil, an error is raised."
+  (interactive)
+  (let ((root hg-root))
+    (save-excursion
+      (run-hooks 'hg-pre-commit-hook)
+      (goto-char (point-min))
+      (search-forward hg-commit-message-start)
+      (let (message files)
+	(let ((start (point)))
+	  (goto-char (point-max))
+	  (search-backward hg-commit-message-end)
+	  (setq message (hg-strip (buffer-substring start (point)))))
+	(when (and (= (length message) 0)
+		   (not hg-commit-allow-empty-message))
+	  (error "Cannot proceed - commit message is empty"))
+	(forward-line 1)
+	(beginning-of-line)
+	(while (< (point) (point-max))
+	  (let ((pos (+ (point) 4)))
+	    (end-of-line)
+	    (when (eq (get-text-property pos 'face) 'bold)
+	      (end-of-line)
+	      (setq files (cons (buffer-substring pos (point)) files))))
+	  (forward-line 1))
+	(when (and (= (length files) 0)
+		   (not hg-commit-allow-empty-file-list))
+	  (error "Cannot proceed - no files to commit"))
+	(setq message (concat message "\n"))
+	(apply 'hg-run0 "--cwd" hg-root "commit" "-m" message files))
+      (let ((buf hg-prev-buffer))
+	(kill-buffer nil)
+	(switch-to-buffer buf))
+      (hg-update-mode-lines root))))
+
+(defun hg-commit-mode ()
+  "Mode for describing a commit of changes to a Mercurial repository.
+This involves two actions: describing the changes with a commit
+message, and choosing the files to commit.
+
+To describe the commit, simply type some text in the designated area.
+
+By default, all modified, added and removed files are selected for
+committing.  Files that will be committed are displayed in bold face\;
+those that will not are displayed in normal face.
+
+To toggle whether a file will be committed, move the cursor over a
+particular file and hit space or return.  Alternatively, middle click
+on the file.
+
+Key bindings
+------------
+\\[hg-commit-finish]		proceed with commit
+\\[hg-commit-kill]		kill commit
+
+\\[hg-diff-repo]		view diff of pending changes"
+  (interactive)
+  (use-local-map hg-commit-mode-map)
+  (set-syntax-table text-mode-syntax-table)
+  (setq local-abbrev-table text-mode-abbrev-table
+	major-mode 'hg-commit-mode
+	mode-name "Hg-Commit")
+  (set-buffer-modified-p nil)
+  (setq buffer-undo-list nil)
+  (run-hooks 'text-mode-hook 'hg-commit-mode-hook))
+
+(defun hg-commit-start ()
+  "Prepare a commit of changes to the repository containing the current file."
+  (interactive)
+  (while hg-prev-buffer
+    (set-buffer hg-prev-buffer))
+  (let ((root (hg-root))
+	(prev-buffer (current-buffer))
+	modified-files)
+    (unless root
+      (error "Cannot commit outside a repository!"))
+    (hg-sync-buffers root)
+    (setq modified-files (hg-chomp (hg-run0 "--cwd" root "status" "-arm")))
+    (when (and (= (length modified-files) 0)
+	       (not hg-commit-allow-empty-file-list))
+      (error "No pending changes to commit"))
+    (let* ((buf-name (format "*Mercurial: Commit %s*" root)))
+      (pop-to-buffer (get-buffer-create buf-name))
+      (when (= (point-min) (point-max))
+	(set (make-local-variable 'hg-root) root)
+	(setq hg-prev-buffer prev-buffer)
+	(insert "\n")
+	(let ((bol (point)))
+	  (insert hg-commit-message-end)
+	  (add-text-properties bol (point) '(face bold-italic)))
+	(let ((file-area (point)))
+	  (insert modified-files)
+	  (goto-char file-area)
+	  (while (< (point) (point-max))
+	    (let ((bol (point)))
+	      (forward-char 1)
+	      (insert "  ")
+	      (end-of-line)
+	      (add-text-properties (+ bol 4) (point)
+				   '(face bold mouse-face highlight)))
+	    (forward-line 1))
+	  (goto-char file-area)
+	  (add-text-properties (point) (point-max)
+			       `(keymap ,hg-commit-mode-file-map))
+	  (goto-char (point-min))
+	  (insert hg-commit-message-start)
+	  (add-text-properties (point-min) (point) '(face bold-italic))
+	  (insert "\n\n")
+	  (forward-line -1)
+	  (save-excursion
+	    (goto-char (point-max))
+	    (search-backward hg-commit-message-end)
+	    (add-text-properties (match-beginning 0) (point-max)
+				 '(read-only t))
+	    (goto-char (point-min))
+	    (search-forward hg-commit-message-start)
+	    (add-text-properties (match-beginning 0) (match-end 0)
+				 '(read-only t)))
+	  (hg-commit-mode)
+          (cd root))))))
+
+(defun hg-diff (path &optional rev1 rev2)
+  "Show the differences between REV1 and REV2 of PATH.
+When called interactively, the default behaviour is to treat REV1 as
+the \"parent\" revision, REV2 as the current edited version of the file, and
+PATH as the file edited in the current buffer.
+With a prefix argument, prompt for all of these."
+  (interactive (list (hg-read-file-name " to diff")
+                     (let ((rev1 (hg-read-rev " to start with" 'parent)))
+		       (and (not (eq rev1 'parent)) rev1))
+		     (let ((rev2 (hg-read-rev " to end with" 'working-dir)))
+		       (and (not (eq rev2 'working-dir)) rev2))))
+  (hg-sync-buffers path)
+  (let ((a-path (hg-abbrev-file-name path))
+        ;; none revision is specified explicitly
+        (none (and (not rev1) (not rev2)))
+        ;; only one revision is specified explicitly
+        (one (or (and (or (equal rev1 rev2) (not rev2)) rev1)
+                 (and (not rev1) rev2)))
+	diff)
+    (hg-view-output ((cond
+		      (none
+		       (format "Mercurial: Diff against parent of %s" a-path))
+		      (one
+		       (format "Mercurial: Diff of rev %s of %s" one a-path))
+		      (t
+		       (format "Mercurial: Diff from rev %s to %s of %s"
+			       rev1 rev2 a-path))))
+      (cond
+       (none
+        (call-process (hg-binary) nil t nil "diff" path))
+       (one
+        (call-process (hg-binary) nil t nil "diff" "-r" one path))
+       (t
+        (call-process (hg-binary) nil t nil "diff" "-r" rev1 "-r" rev2 path)))
+      (diff-mode)
+      (setq diff (not (= (point-min) (point-max))))
+      (font-lock-fontify-buffer)
+      (cd (hg-root path)))
+    diff))
+
+(defun hg-diff-repo (path &optional rev1 rev2)
+  "Show the differences between REV1 and REV2 of repository containing PATH.
+When called interactively, the default behaviour is to treat REV1 as
+the \"parent\" revision, REV2 as the current edited version of the file, and
+PATH as the `hg-root' of the current buffer.
+With a prefix argument, prompt for all of these."
+  (interactive (list (hg-read-file-name " to diff")
+                     (let ((rev1 (hg-read-rev " to start with" 'parent)))
+		       (and (not (eq rev1 'parent)) rev1))
+		     (let ((rev2 (hg-read-rev " to end with" 'working-dir)))
+		       (and (not (eq rev2 'working-dir)) rev2))))
+  (hg-diff (hg-root path) rev1 rev2))
+
+(defun hg-forget (path)
+  "Lose track of PATH, which has been added, but not yet committed.
+This will prevent the file from being incorporated into the Mercurial
+repository on the next commit.
+With a prefix argument, prompt for the path to forget."
+  (interactive (list (hg-read-file-name " to forget")))
+  (let ((buf (current-buffer))
+	(update (equal buffer-file-name path)))
+    (hg-view-output (hg-output-buffer-name)
+      (apply 'call-process (hg-binary) nil t nil (list "forget" path))
+      ;; "hg forget" shows pathes relative NOT TO ROOT BUT TO REPOSITORY
+      (hg-fix-paths)
+      (goto-char (point-min))
+      (cd (hg-root path)))
+    (when update
+      (with-current-buffer buf
+        (when (local-variable-p 'backup-inhibited)
+          (kill-local-variable 'backup-inhibited))
+	(hg-mode-line)))))
+
+(defun hg-incoming (&optional repo)
+  "Display changesets present in REPO that are not present locally."
+  (interactive (list (hg-read-repo-name " where changes would come from")))
+  (hg-view-output ((format "Mercurial: Incoming from %s to %s"
+			   (hg-abbrev-file-name (hg-root))
+			   (hg-abbrev-file-name
+			    (or repo hg-incoming-repository))))
+    (call-process (hg-binary) nil t nil "incoming"
+		  (or repo hg-incoming-repository))
+    (hg-log-mode)
+    (cd (hg-root))))
+
+(defun hg-init ()
+  (interactive)
+  (error "not implemented"))
+
+(defun hg-log-mode ()
+  "Mode for viewing a Mercurial change log."
+  (goto-char (point-min))
+  (when (looking-at "^searching for changes.*$")
+    (delete-region (match-beginning 0) (match-end 0)))
+  (run-hooks 'hg-log-mode-hook))
+
+(defun hg-log (path &optional rev1 rev2 log-limit)
+  "Display the revision history of PATH.
+History is displayed between REV1 and REV2.
+Number of displayed changesets is limited to LOG-LIMIT.
+REV1 defaults to the tip, while REV2 defaults to 0.
+LOG-LIMIT defaults to `hg-log-limit'.
+With a prefix argument, prompt for each parameter."
+  (interactive (list (hg-read-file-name " to log")
+                     (hg-read-rev " to start with"
+                                  "tip")
+                     (hg-read-rev " to end with"
+				  "0")
+                     (hg-read-number "Output limited to: "
+                                     hg-log-limit)))
+  (let ((a-path (hg-abbrev-file-name path))
+        (r1 (or rev1 "tip"))
+        (r2 (or rev2 "0"))
+        (limit (format "%d" (or log-limit hg-log-limit))))
+    (hg-view-output ((if (equal r1 r2)
+                         (format "Mercurial: Log of rev %s of %s" rev1 a-path)
+                       (format
+                        "Mercurial: at most %s log(s) from rev %s to %s of %s"
+                        limit r1 r2 a-path)))
+      (eval (list* 'call-process (hg-binary) nil t nil
+                   "log"
+                   "-r" (format "%s:%s" r1 r2)
+                   "-l" limit
+                   (if (> (length path) (length (hg-root path)))
+                       (cons path nil)
+                     nil)))
+      (hg-log-mode)
+      (cd (hg-root path)))))
+
+(defun hg-log-repo (path &optional rev1 rev2 log-limit)
+  "Display the revision history of the repository containing PATH.
+History is displayed between REV1 and REV2.
+Number of displayed changesets is limited to LOG-LIMIT,
+REV1 defaults to the tip, while REV2 defaults to 0.
+LOG-LIMIT defaults to `hg-log-limit'.
+With a prefix argument, prompt for each parameter."