Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: [Pkg-zsh-devel] Bug#679824: zsh: Buggy perl completion with -e [origin: vincent@xxxxxxxxxx]

On 2014/06/28, at 23:20, Axel Beckert <abe@xxxxxxxxxxxxxxx> wrote:
> $ perl -pi -e 's/foo/bar/' [TAB]
> zsh wants to complete on Perl scripts instead of normal files.
> AFAIK, after -e '...', completion should be done on arbitrary
> arguments (e.g. like after an unknown command).

I first thought that just replacing the line 19 of _perl
    '(   -E)*-e+[run one line of program]:one line of program' \
    '(1   -E)*-e+[run one line of program]:one line of program' \
would be enough, but it didn't work. For example

    perl -e 's/foo/bar/' <TAB>

will offer all the command names (external commands etc.), instead of
the file names in the current directory.

This is because the '::' in line 44
    '*::args: _normal'
will clear the array $words, and the completion system thinks that
it is completing at the beginning of the command line and will try
to complete a command name.

But this '::' can't be simply replaced by ':' (infinite recursion
will happen).

A possible fix would be to modify the completion of the 1st argument
if -e '...' is already on the command line (see the patch bellow).
But detecting -e '...' (or -E '...') is rather complicated, because
perl allows command lines like

    perl -pes/foo/bar/ arg ...
    perl -CE script_file arg ...

I'm not sure the following patch is 100% OK (I'm not familiar with
most of the perl command line options).

Another possibility would be to modify line 19 (and 20) as above,
and modify line 44 to
    '*:args: _files'
I guess this may reduce the possibility of user customization of
the completion, but I have no idea how serious it is.

diff --git a/Completion/Unix/Command/_perl b/Completion/Unix/Command/_perl
index b00baa6..1939cb0 100644
--- a/Completion/Unix/Command/_perl
+++ b/Completion/Unix/Command/_perl
@@ -40,10 +40,21 @@ _perl () {
     "(-w    -X)-W[enable all warnings (ignores 'no warnings')]" \
     "(-w -W   )-X[disable all warnings (ignores 'use warnings')]" \
     '-x-[strip off text before #!perl line and perhaps cd to directory]:directory to cd to:_files -/' \
-    '1:Perl script:_files -/ -g "*.(p[ml]|PL|t)(-.)"' \
+    '1:script or args:_script_or_args' \
     '*::args: _normal'
+_script_or_args () {
+  local expl
+  if (( $words[(I)-(e|E)*] )); then
+    _description args expl 'file'
+    _files "$expl[@]"
+  else
+    _description script expl 'Perl script'
+    _files "$expl[@]" -/ -g "*.(p[ml]|PL|t)(-.)"
+  fi
 _perl_m_opt () {
   compset -P '-'

Messages sorted by: Reverse Date, Date, Thread, Author