@@ 1,15 1,33 @@
-- Mercurial (hg) tab completion for Take Command
--
--- Install the script with command:
--- tabcomplete /l C:\Work\lua\tabcomplete\hgcomplete.lua
+-- Install the helper .btm script with command:
+-- tabcomplete /l %CD%\hgcomplete.btm
+
+require "os"
+require "io"
-require "winapi"
+logging = 0
+
+function LOG(message)
+ if logging ~= 0 then
+ io.stderr:write("LOG: " .. message .. "\n")
+ end
+end
command = arg[1]
if command ~= "hg" then
return 1
end
+LOG("")
+LOG(command)
+
+LOG("all arguments:")
+for a,b in pairs(arg)
+do
+ LOG(a .. ":" .. b)
+end
+
function string:split(sep)
local sep, fields = sep or ":", {}
local pattern = string.format("([^%s]+)", sep)
@@ 17,9 35,23 @@ function string:split(sep)
return fields
end
+function table_merge(a,b)
+ new_table = {}
+ for _, v in pairs(a)
+ do
+ table.insert(new_table, v)
+ end
+ for _, v in pairs(b)
+ do
+ table.insert(new_table, v)
+ end
+
+ return new_table
+end
+
function changed_files()
local files = {}
- local p = io.popen('hg status -n -m')
+ local p = io.popen('hg status -q -n -m')
for f in p:lines() do
table.insert(files, f)
end
@@ 35,18 67,34 @@ function unknown_files()
return files
end
--- print("")
--- print("SCRIPT PARAMETERS:")
--- print("amount of parameters: " .. #arg)
--- for a,b in pairs(arg)
--- do
--- print(a .. ":" .. b)
--- end
+function find_possibilities(given, choices)
+ found = {}
+
+ if choices == nil then
+ return found
+ end
-possible_params = {
- "add", "branch",
+ for i,p in pairs(choices)
+ do
+ if string.sub(p, 1, string.len(given)) == given then
+ table.insert(found, p)
+ end
+ end
+
+ return found
+end
+
+hg_subcommands = {
+ "add",
+ "branch",
+ "cat",
+ "checkout",
+ "churn",
"clone",
"commit",
+ "config",
+ "continue",
+ "copy",
"export",
"fold",
"heads",
@@ 56,75 104,83 @@ possible_params = {
"pull",
"revert",
"rm",
- "init" }
+ "init"
+}
+
+hg_commit_arguments = {
+ "--edit",
+ "--addremove",
+ "--amend",
+ "--interactive",
+ "--message"
+}
arg_copy = arg[#arg]
+-- If the 4th argument is surrounded by quotes, remove them
if string.sub(arg_copy, 1, 1) == "\"" and string.sub(arg_copy, string.len(arg_copy)) == "\"" then
arg_copy = string.sub(arg_copy, 2, #arg_copy - 1)
end
+
+-- Split all arguments by space
argument_table = string.split(arg_copy, " ")
+LOG("all arguments:")
+for a,b in pairs(argument_table)
+do
+ LOG(a .. ":" .. b)
+end
+
+-- Only 'hg' command given?
if #argument_table < 2 then
- -- print "all arguments:"
- -- for a,b in pairs(argument_table)
- -- do
- -- print(a .. ":" .. b)
- -- end
- winapi.setenv("TABCOMPLETIONRESULT", table.concat(possible_params, " "))
+ LOG("Only main command given")
+ LOG(table.concat(hg_subcommands, " "))
+ -- winapi.setenv("TABCOMPLETIONRESULT", table.concat(hg_subcommands, " "))
+ print(table.concat(hg_subcommands, " "))
return 0
end
-if #arg == 3 then
- cursor_pos = arg[2]
-elseif #arg == 4 then
- cursor_pos = arg[3]
-else
- cursor_pos = 0
-end
+hg_command = argument_table[2]
-- argument_table = justWords(all_arguments)
possibilities = {}
--- print("arguments:" .. #argument_table)
--- for a,b in pairs(argument_table)
--- do
--- print(a .. ":" .. b .."|")
--- end
-
-if #arg == 4 and #argument_table == 2 and string.sub(arg_copy, string.len(arg_copy)) ~= " " then
- for i,p in pairs(possible_params)
- do
- if string.sub(p, 1, string.len(argument_table[2])) == argument_table[2] then
- table.insert(possibilities, p)
--- else
--- print("argument_table[2] len = " .. string.len(argument_table[2]))
--- print("no match: " .. p .. "|" .. string.sub(p, 1, string.len(argument_table[2])) .. "|" .. argument_table[2])
- end
- end
+-- We got 'hg' and part of subcommand?
+if #arg >= 4 and #argument_table == 2 and string.sub(arg_copy, string.len(arg_copy)) ~= " " then
+ -- Search for possible subcommands
+ possibilities = find_possibilities(hg_command, hg_subcommands)
if #possibilities > 0 then
- winapi.setenv("TABCOMPLETIONRESULT", table.concat(possibilities, " "))
+ LOG("SUGGESTION(1):")
+ LOG(table.concat(possibilities, " "))
+ print(table.concat(possibilities, " "))
return 0
- elseif argument_table[2] == "\"" then
- winapi.setenv("TABCOMPLETIONRESULT", table.concat(possible_params, " "))
- -- else
- -- print("Y2:" .. argument_table[2] .. "|" .. string.len(argument_table[2]))
+ elseif hg_command == "\"" then
+ LOG("SUGGESTION(2):")
+ LOG(table.concat(hg_subcommands, " "))
+ print(table.concat(hg_subcommands, " "))
end
+
+-- We got complete subcommand and are looking for its parameters?
elseif #arg >= 3 and #argument_table >= 2 then
- if argument_table[2] == "commit" then
- commit_args = "--edit --addremove --amend --interactive --message"
- result = commit_args
+ if hg_command == "commit" then
+ LOG("COMMIT")
c = changed_files()
- if #c > 0 then
- result = result .. " " .. table.concat(c, " ")
+ choices = table_merge(c, hg_commit_arguments)
+ LOG(table.concat(choices, " "))
+ possibilities = find_possibilities(argument_table[3], choices)
+ LOG("P: " .. argument_table[3] .. " | ".. table.concat(possibilities, " "))
+ if #possibilities > 0 then
+ LOG(table.concat(possibilities, " "))
+ print(table.concat(possibilities, " "))
+ return 0
end
- winapi.setenv("TABCOMPLETIONRESULT", result)
- return 0
- elseif argument_table[2] == "add" then
+ return 1
+ elseif hg_command == "add" then
u = unknown_files()
if #u > 0 then
- winapi.setenv("TABCOMPLETIONRESULT", table.concat(u, " "))
+ LOG(table.concat(u, " "))
+ print(table.concat(u, " "))
return 0
else
return 1