Improve a little ('add' support)
1 files changed, 66 insertions(+), 25 deletions(-)

M hgcomplete.lua
M hgcomplete.lua +66 -25
@@ 1,4 1,7 @@ 
--- TABCOMPLETIONRESULT
+-- Mercurial (hg) tab completion for Take Command
+--
+-- Install the script with command:
+-- tabcomplete /l C:\Work\lua\tabcomplete\hgcomplete.lua
 
 require "winapi"
 

          
@@ 8,10 11,28 @@ if command ~= "hg" then
 end
 
 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
+  local sep, fields = sep or ":", {}
+  local pattern = string.format("([^%s]+)", sep)
+  self:gsub(pattern, function(c) fields[#fields+1] = c end)
+  return fields
+end
+
+function changed_files()
+  local files = {}
+  local p = io.popen('hg status -n -m')
+  for f in p:lines() do
+    table.insert(files, f)
+  end
+  return files
+end
+
+function unknown_files()
+  local files = {}
+  local p = io.popen('hg status -n --unknown')
+  for f in p:lines() do
+    table.insert(files, f)
+  end
+  return files
 end
 
 -- print("SCRIPT PARAMETERS:")

          
@@ 23,6 44,8 @@ end
 
 possible_params = { "add", "clone", "commit", "push", "pull", "revert", "rm", "init" }
 
+-- print("arg: " .. arg[#arg])
+
 argument_table = string.split(arg[#arg], " ")
 
 if #argument_table < 2 then

          
@@ 36,9 59,9 @@ if #argument_table < 2 then
 end
 
 if #arg == 3 then
-	cursor_pos = arg[2]
+  cursor_pos = arg[2]
 elseif #arg == 4 then
-	cursor_pos = arg[3]
+  cursor_pos = arg[3]
 else
   cursor_pos = 0
 end

          
@@ 47,31 70,49 @@ end
 
 possibilities = {}
 
--- print("arguments:")
+-- print("arguments:" .. #argument_table)
 -- for a,b in pairs(argument_table)
 -- do
---   print(a .. ":" .. b)
+--   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
+  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
+  elseif argument_table[2] == "\"" then
+    winapi.setenv("TABCOMPLETIONRESULT", table.concat(possible_params, " "))
+  -- else
+  --   print("Y2:" .. argument_table[2] .. "|" .. string.len(argument_table[2]))
+  end
+elseif #arg >= 3 and #argument_table >= 2 then
   if argument_table[2] == "commit" then
-    winapi.setenv("TABCOMPLETIONRESULT", "--edit --addremove --amend --interactive --message")
+    commit_args = "--edit --addremove --amend --interactive --message"
+    result = commit_args
+    c = changed_files()
+    if #c > 0 then
+      result = result .. " " .. table.concat(c, " ")
+    end
+    winapi.setenv("TABCOMPLETIONRESULT", result)
     return 0
+  elseif argument_table[2] == "add" then
+    u = unknown_files()
+    if #u > 0 then
+      winapi.setenv("TABCOMPLETIONRESULT", table.concat(u, " "))
+      return 0
+    else
+      return 1
+    end
   end
 end