NYAOS 3000でMercurialのサブコマンドを補完する

6/12: 2010-06-12 - メモ:wantoraに最新版があります。

nyaos.completeを使ってMercurialのサブコマンドを補完するスクリプトを作ってみた。

nyaos.completeでは補完対象の文字列は取得できるが、補完対象の文字列の前にある文字列が取得できないので、nyaos.keyhookを併用してそれを実現している。

-- MIT License (http://d.hatena.ne.jp/wantora/20101212/1292141801)

local command_text = nil
local hgcomplete = nil

local function get_hgcomplete()
	local hgcmds = {}
	for line in nyaos.eval('hg help'):gmatch('[^\n]+') do
		local name = line:match('^ ([^%s]+)')
		if #hgcmds > 0 and (not name) then break end
		if name                       then table.insert(hgcmds, name) end
	end
	return hgcmds
end

function nyaos.keyhook(t)
	command_text = t.text
	return nil
end

function nyaos.complete(basestring, pos)
	if command_text:sub(1, pos):gsub('^hg%.%w*', 'hg'):match('^hg%s*$') then
		if not hgcomplete then
			hgcomplete = get_hgcomplete()
		end
		return hgcomplete
	end
	
	return nyaos.default_complete(basestring, pos)
end