szermatt / emacs-bash-completion

Add programmable bash completion to Emacs shell-mode
GNU General Public License v2.0
279 stars 33 forks source link

complete -C not acting right #51

Open perlancar opened 2 years ago

perlancar commented 2 years ago

I have CLI scripts that complete themselves (complete -C SCRIPTNAME SCRIPTNAME) written in Perl. When doing completion, bash-completion.el seems to call a script multiple times and without COMP_LINE and COMP_POINT set, making completion fail. In the example below, I am using the remove-pdf-password script, available from CPAN (https://metacpan.org/pod/remove-pdf-password).

I have enabled logging in my script to see environment varible. When in bash:

% remove-pdf-password <tab>

will log this line:

[Sat Jul 24 08:14:45 2021] [prog /home/s1/perl5/perlbrew/perls/perl-5.30.2/bin/remove-pdf-password] [pid 8508] [uid 1000] COMP_LINE=<remove-pdf-password -> (21 char(s)) COMP_POINT=<21>

When in M-x shell, doing the same causes this to be logged instead:

[Sat Jul 24 08:15:01 2021] [prog /home/s1/perl5/perlbrew/perls/perl-5.30.2/bin/remove-pdf-password] [pid 8747] [uid 1000] COMP_LINE=<> (0 char(s)) COMP_POINT=<>
[Sat Jul 24 08:15:01 2021] [prog /home/s1/perl5/perlbrew/perls/perl-5.30.2/bin/remove-pdf-password] [pid 8748] [uid 1000] COMP_LINE=<> (0 char(s)) COMP_POINT=<>
[Sat Jul 24 08:15:01 2021] [prog /home/s1/perl5/perlbrew/perls/perl-5.30.2/bin/remove-pdf-password] [pid 8749] [uid 1000] COMP_LINE=<> (0 char(s)) COMP_POINT=<>

Relevant lines from output of M-x bash-completion-debug:

commandline: <<EOF
__emacs_complete_pre_command;  echo -n "^[^[pwd=${PWD}^[^[";__EMACS_COMPLETE_WRAPPER='COMP_LINE='\''remove-pdf-password '\''; COMP_POINT=$(( 1 + ${#COMP_LINE} )); COMP_CWORD=1; COMP_WORDS=( remove-pdf-password '\'''\'' ); remove-pdf-password remove-pdf-password '\'''\'' remove-pdf-password' compgen -F __emacs_complete_wrapper -- '' 2>/dev/null
EOF

status: 1
process: #<process shell>
output-buffer: <<EOF
^[^[pwd=/zpool_host_mnt/mnt/home/s1^[^[remove-pdf-password: ERROR 400: Argument 'files' fails validation: @[1]: Length must be at least 1
EOF

bash-major-version: 4
emacs-version: "25.2.2"
completion-ignore-case: t
context: [cl-struct-completion "remove-pdf-password "
                      ("remove-pdf-password" "")
                      1 "" 1797 "" nil
                      ("-C" "remove-pdf-password")
                      "\"'@><=;|&(:" nil]

complete-p: ((nil "-F" "_shcompgen_loader")
 ("shcompgen" "-C" "shcompgen")
 ("renwd-cd" "-C" "renwd")
 ("cdpm" "-F" "_cdpm")
 ("remove-pdf-password" "-C" "remove-pdf-password")
 ("cds" "-F" "_cds"))