about summary refs log tree commit diff
diff options
context:
space:
mode:
authordana <dana@dana.is>2018-07-25 12:10:11 -0500
committerdana <dana@dana.is>2018-07-25 12:10:11 -0500
commit79153bec16073c6e28e6f22e260c79f98ad32e66 (patch)
tree2c703ba6a42756d4870fe72ec67fb26b8ed62e1c
parentc5d9c9cbd3c69fb31e55ad373e99c244eeb1ec67 (diff)
downloadzsh-79153bec16073c6e28e6f22e260c79f98ad32e66.tar.gz
zsh-79153bec16073c6e28e6f22e260c79f98ad32e66.tar.xz
zsh-79153bec16073c6e28e6f22e260c79f98ad32e66.zip
43203: Add completion for lua
-rw-r--r--ChangeLog2
-rw-r--r--Completion/Unix/Command/_lua63
2 files changed, 65 insertions, 0 deletions
diff --git a/ChangeLog b/ChangeLog
index b796c9313..207d42946 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,7 @@
 2018-07-25  dana  <dana@dana.is>
 
+	* 43203: Completion/Unix/Command/_lua: Add completion for lua
+
 	* Simon Ruderich: 43200: Completion/Linux/Command/_iptables: Add
 	completion for ip6tables*
 
diff --git a/Completion/Unix/Command/_lua b/Completion/Unix/Command/_lua
new file mode 100644
index 000000000..7254d3819
--- /dev/null
+++ b/Completion/Unix/Command/_lua
@@ -0,0 +1,63 @@
+#compdef lua -P lua[0-9.-]##
+
+# Complete lua library names. We go out of our way here to support sub-modules
+# (of the format foo.bar.baz), even though the way `lua -l` handles those isn't
+# very nice, because it might be useful for informational purposes
+(( $+functions[_lua_libraries] )) ||
+_lua_libraries() {
+  local p pre
+  local -a tmp tmp2
+
+  # Get Lua's library search path
+  tmp=( ${(s<;>)${(@f)"$(
+    _call_program library-path $words[1] -e '"print(package.path)"'
+  )"}} )
+  # Support C modules, which aren't explicitly included in the above
+  tmp+=( ${(@)${(@M)tmp:#*'?.lua'}/%.lua/.so} )
+
+  for p in ${(@u)tmp}; do
+    # Don't recurse infinitely into the current directory; we'll just trust
+    # that all other paths are sensible
+    if [[ $p == './?'* ]]; then
+      tmp=( ${~${${(b)p}/'\?'/'*'}}(#qN) )
+    else
+      tmp=( ${~${${(b)p}/'\?'/'**/*'}}(#qN) )
+    fi
+    tmp2+=( ${(@)tmp##${p%%'?'*}} )
+  done
+
+  tmp=( ${(@)tmp2%%(/init.lua|.lua|.so)} )
+  tmp=( ${(@u)${(@)tmp//\//.}} )
+
+  _wanted -x libraries expl 'Lua library' compadd -a "$@" - tmp
+}
+
+_lua() {
+  local ret=1
+  local -a context expl line state state_descr
+  local -A opt_args
+
+  # Stacking not supported, no arguments are exclusive except `-`
+  _arguments -S -A '-*' : \
+    '*-e+[execute specified command string]:command string' \
+    '-E[ignore environment variables]' \
+    '-i[enter interactive mode]' \
+    '*-l+[specify library or module to require]: :_lua_libraries' \
+    '-v[display version information]' \
+    '(1 -)-[stop argument parsing and execute script on stdin]' \
+    '1:Lua script:_files' \
+    '*:: :->arg' \
+  && ret=0
+
+  [[ $state == arg ]] &&
+  if [[ $line[1] == - ]]; then
+    _description files expl 'script argument'
+    _files "${(@)expl}" && ret=0
+  else
+    _normal && ret=0
+  fi
+
+  return ret
+}
+
+_lua "$@"