Refactor the logic to actually work at some level.
1 files changed, 66 insertions(+), 6 deletions(-)

M hgcomplete.lua
M hgcomplete.lua +66 -6
@@ 7,13 7,73 @@ if command ~= "hg" then
   return 1
 end
 
-param = arg[2]
-if param == nil or #arg == 3 then
-  winapi.setenv("TABCOMPLETIONRESULT", "add rm commit push pull revert init")
-  return 0
-elseif string.sub(param,1,1) == "c" then
-  winapi.setenv("TABCOMPLETIONRESULT", "commit")
+function string:split(sep)
+        local sep, fields = sep or ":", {}
+        local pattern = string.format("([^%s]+)", sep)
+        self:gsub(pattern, function(c) fields[#fields+1] = c end)
+        return fields
+end
+
+-- print("SCRIPT PARAMETERS:")
+-- print("amount of parameters: " .. #arg)
+-- for a,b in pairs(arg)
+-- do
+--   print(a .. ":" .. b)
+-- end
+
+possible_params = { "add", "clone", "commit", "push", "pull", "revert", "rm", "init" }
+
+argument_table = string.split(arg[#arg], " ")
+
+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, " "))
   return 0
 end
 
+if #arg == 3 then
+	cursor_pos = arg[2]
+elseif #arg == 4 then
+	cursor_pos = arg[3]
+else
+  cursor_pos = 0
+end
+
+-- argument_table = justWords(all_arguments)
+
+possibilities = {}
+
+-- print("arguments:")
+-- for a,b in pairs(argument_table)
+-- do
+--   print(a .. ":" .. b)
+-- end
+
+if #arg == 4 and #argument_table == 2 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
+	
+	if #possibilities > 0 then
+	  winapi.setenv("TABCOMPLETIONRESULT", table.concat(possibilities, " "))
+	  return 0
+	end
+elseif #arg == 3 and #argument_table >= 2 then
+  if argument_table[2] == "commit" then
+    winapi.setenv("TABCOMPLETIONRESULT", "--edit --addremove --amend --interactive --message")
+    return 0
+  end
+end
+
 return 1
+