thg / tortoisehg / util /

import shlex
import os
from mercurial import util, match

def _toolstr(ui, tool, part, default=""):
    return ui.config("editor-tools", tool + "." + part, default)

def _findtool(ui, tool):
    for kn in ("regkey", "regkeyalt"):
        k = _toolstr(ui, tool, kn)
        if not k:
        p = util.lookupreg(k, _toolstr(ui, tool, "regname"))
        if p:
            p = util.findexe(p + _toolstr(ui, tool, "regappend"))
            if p:
                return p
    exe = _toolstr(ui, tool, "executable", tool)
    return util.findexe(util.expandpath(exe))

def _findeditor(repo, files):
    '''returns tuple of editor name and editor path.

    tools matched by pattern are returned as (name, toolpath)
    tools detected by search are returned as (name, toolpath)
    tortoisehg.editor is returned as         (None, tortoisehg.editor)
    HGEDITOR or ui.editor are returned as    (None, ui.editor)

    So first return value is an [editor-tool] name or None and
    second return value is a toolpath or user configured command line
    ui = repo.ui

    # first check for tool specified by file patterns.  The first file pattern
    # which matches one of the files being edited selects the editor
    for pat, tool in ui.configitems("editor-patterns"):
        mf = match.match(repo.root, '', files)
        toolpath = _findtool(ui, tool)
        if mf(path) and toolpath:
            return (tool, util.shellquote(toolpath))

    # then editor-tools
    tools = {}
    for k, v in ui.configitems("editor-tools"):
        t = k.split('.')[0]
        if t not in tools:
            tools[t] = int(_toolstr(ui, t, "priority", "0"))
    names = tools.keys()
    tools = sorted([(-p, t) for t, p in tools.items()])
    editor = ui.config('tortoisehg', 'editor')
    if editor:
        if editor not in names:
            # if tortoisehg.editor does not match an editor-tools entry, take
            # the value directly
            return (None, editor)
        # else select this editor as highest priority (may still use another if
        # it is not found on this machine)
        tools.insert(0, (None, editor))
    for p, t in tools:
        toolpath = _findtool(ui, t)
        if toolpath:
            return (t, util.shellquote(toolpath))

    # fallback to potential CLI editor
    editor = os.environ.get('HGEDITOR') or repo.ui.config('ui', 'editor') \
             or os.environ.get('EDITOR', 'vi')
    return (None, editor)

def detecteditor(repo, files):
    'returns tuple of editor tool path and arguments'
    name, pathorconfig = _findeditor(repo, files)
    if name is None:
        cmds = shlex.split(pathorconfig)
        toolpath = cmds[0]
        args = ' '.join(cmds[1:])
        toolpath = pathorconfig
        args = _toolstr(repo.ui, name, "args")
    return toolpath, args