zsh-workers
 help / color / mirror / code / Atom feed
* [PATCH] Completion: Add _lua
@ 2018-07-23  6:50 dana
  2018-07-23 11:40 ` dana
  0 siblings, 1 reply; 2+ messages in thread
From: dana @ 2018-07-23  6:50 UTC (permalink / raw)
  To: Zsh hackers list

Add completion for the Lua command-line interpreter.

_lua_libraries is maybe a little excessive, tbh, but i found it useful — i don't
think there's any 'official' way to do what it does.

dana


diff --git a/Completion/Unix/Command/_lua b/Completion/Unix/Command/_lua
new file mode 100644
index 000000000..e11cd226c
--- /dev/null
+++ b/Completion/Unix/Command/_lua
@@ -0,0 +1,48 @@
+#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
+  local -a tmp tmp2 pre
+
+  # Lua will tell us its effective search paths in the error message
+  tmp=( ${(f)"$( _call_program libraries $words[1] -l '"%%FAKE%%"' 2>&1 )"} )
+
+  [[ $tmp == *'%%FAKE%%'* ]] && {
+    tmp=( ${(@M)tmp:#*no file*'%%FAKE%%'*} )
+    tmp=( ${(@)tmp##[[:space:]]#no file[[:space:]\'\"]##} )
+    tmp=( ${(@)tmp%%[[:space:]\'\"]##} )
+
+    for p in $tmp; do
+      pre+=( ${(b)p%%'%%FAKE%%'*} )
+      # Don't recurse infinitely into the current directory; we'll just trust
+      # that all other paths are sensible
+      if [[ $p == './%%FAKE%%'* ]]; then
+        tmp2+=( ${~${${(b)p}/'%%FAKE%%'/'*'}}(#qN) )
+      else
+        tmp2+=( ${~${${(b)p}/'%%FAKE%%'/'**/*'}}(#qN) )
+      fi
+    done
+
+    tmp=( $tmp2 )
+    tmp=( ${(@)tmp%%(.lua|/init.lua|.so)} )
+    tmp=( ${(@)tmp##(${~${(j<|>)pre}})} )
+    tmp=( ${(@u)${(@)tmp//\//.}} )
+  }
+
+  _wanted -x libraries expl 'Lua library' compadd -a "$@" - tmp
+}
+
+# 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' \
+  '*:script argument'


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] Completion: Add _lua
  2018-07-23  6:50 [PATCH] Completion: Add _lua dana
@ 2018-07-23 11:40 ` dana
  0 siblings, 0 replies; 2+ messages in thread
From: dana @ 2018-07-23 11:40 UTC (permalink / raw)
  To: Zsh hackers list

On 23 Jul 2018, at 01:50, dana <dana@dana.is> wrote:
>_lua_libraries is maybe a little excessive, tbh, but i found it useful — i don't
>think there's any 'official' way to do what it does.

Eric showed me his own _lua function, which uses an embarrassingly less silly
method of building the library matches for this. His also handles script
arguments more like other interpreters' functions do. I've incorporated those
enhancements here.

dana


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 "$@"


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2018-07-23 11:40 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-07-23  6:50 [PATCH] Completion: Add _lua dana
2018-07-23 11:40 ` dana

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).