# HG changeset patch
# User Matt Mackall <mpm@selenic.com>
# Date 1202088545 21600
#      Sun Feb 03 19:29:05 2008 -0600
# Node ID 3c9dbb743d2091943950a1f564e899038ad41dac
# Parent  3c33032d8906f751ca2b3dd8c0f937dea5a717f4
merge: add registry look up bits to tool search

diff --git a/mercurial/filemerge.py b/mercurial/filemerge.py
--- a/mercurial/filemerge.py
+++ b/mercurial/filemerge.py
@@ -16,6 +16,13 @@
     return ui.configbool("merge-tools", tool + "." + part, default)
 
 def _findtool(ui, tool):
+    k = _toolstr(ui, tool, "regkey")
+    if k:
+        p = util.lookup_reg(k, _toolstr(ui, tool, "regname"))
+        if p:
+            p = util.find_exe(p + _toolstr(ui, tool, "regappend"))
+            if p:
+                return p
     return util.find_exe(_toolstr(ui, tool, "executable", tool))
 
 def _picktool(repo, ui, path, binary, symlink):
diff --git a/mercurial/util.py b/mercurial/util.py
--- a/mercurial/util.py
+++ b/mercurial/util.py
@@ -1086,6 +1086,9 @@
 else:
     nulldev = '/dev/null'
 
+    def lookup_reg(key, name=None, scope=None):
+        return None
+
     def rcfiles(path):
         rcs = [os.path.join(path, 'hgrc')]
         rcdir = os.path.join(path, 'hgrc.d')
diff --git a/mercurial/util_win32.py b/mercurial/util_win32.py
--- a/mercurial/util_win32.py
+++ b/mercurial/util_win32.py
@@ -187,6 +187,37 @@
         return details[0] != winerror.ERROR_INVALID_PARAMETER
     return True
 
+def lookup_reg(key, valname=None, scope=None):
+    ''' Look up a key/value name in the Windows registry.
+
+    valname: value name. If unspecified, the default value for the key
+    is used.
+    scope: optionally specify scope for registry lookup, this can be
+    a sequence of scopes to look up in order. Default (CURRENT_USER,
+    LOCAL_MACHINE).
+    '''
+    try:
+        from _winreg import HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE, \
+            QueryValueEx, OpenKey
+    except ImportError:
+        return None
+
+    def query_val(scope, key):
+        try:
+            keyhandle = OpenKey(scope, key)
+            return QueryValueEx(keyhandle, valname)[0]
+        except EnvironmentError:
+            return None
+
+    if scope is None:
+        scope = (HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE)
+    elif not isinstance(scope, (list, tuple)):
+        scope = (scope,)
+    for s in scope:
+        val = query_val(s, key, valname)
+        if val is not None:
+            return val
+
 def system_rcpath_win32():
     '''return default os-specific hgrc search path'''
     proc = win32api.GetCurrentProcess()