2

I come from bash and am looking into tcsh's tab completion feature.

1) Is it correct that tcsh cannot delegate completion to a shell function (the way bash does with 'complete -F _func cmd')?

2) When delegating completion to an external program, tcsh only provides COMMAND_LINE, unlike bash which provides COMP_LINE and COMP_POINT. This means that tcsh cannot properly do this:

% cmd --foo -<tab> --bar --baz

where I want the program doing the completion to give a list of unmentioned options. Because tcsh will only give COMMAND_LINE as 'cmd --foo -' whereas bash would give the whole line but give clue about the cursor position in COMP_POINT.

0
2
  1. tcsh doesn't have functions at all; so that's a negative. The general strategy is to create a separate script, and call that as an external program (for some things, like alias, a source will also work).

  2. Well, this is not really a question; I can confirm that your observations are correct :-) Adding COMP_POINT might not be difficult though; in fact, here's patch (which is NOT tested very well):

    diff --git i/tc.const.c w/tc.const.c
    index c472acc..1a69fee 100644
    --- i/tc.const.c
    +++ w/tc.const.c
    @@ -469,6 +469,8 @@ Char STRPROGRAM_ENVIRONMENT[] = { 'P', 'R', 'O', 'G', 'R', 'A', 'M',
     #endif /* _OSD_POSIX */
     Char STRCOMMAND_LINE[] = { 'C', 'O', 'M', 'M', 'A', 'N', 'D', '_', 'L', 'I',
                                'N', 'E', '\0' };
    +Char STRCOMP_POINT[]   = { 'C', 'O', 'M', 'P', '_', 'P', 'O', 'I', 'N', 'T',
    +                          '\0' };
    
     #ifdef WARP
     Char STRwarp[]         = { 'w', 'a', 'r', 'p', '\0' };
    diff --git i/tw.comp.c w/tw.comp.c
    index 03da837..101962f 100644
    --- i/tw.comp.c
    +++ w/tw.comp.c
    @@ -635,9 +635,12 @@ tw_complete(const Char *line, Char **word, Char **pat, int looking, eChar *suf)
            default:
                abort();                   /* Cannot happen */
            }
    +
            tsetenv(STRCOMMAND_LINE, line);
    +       tsetenv(STRCOMP_POINT, str2short(xasprintf("%d", (char) CursorH)));
            res = tw_result(com, pat);
            Unsetenv(STRCOMMAND_LINE);
    +       Unsetenv(STRCOMP_POINT);
            cleanup_until(buf);
            return res;
         }
    

You probably want to take this up on the tcsh mailinglist, though.

4
  • Thanks for the answer. It is my impression that programmable tab completion is rather new in tcsh (I read on another question that passing COMMAND_LINE is only recently added). I hope something like COMP_POINT will be added too. Also, didn't know that tcsh doesn't have the concept of shell functions, thanks. Nov 25 '14 at 3:02
  • @GerryLufwansa Actually, completion has been present in tcsh since the late 70's or early 80's (though the current syntax is from the early 90's). It was probably the first UNIX shell to implement it long before bash or zsh even existed. It's a bit dodgy at some points not because it's very new, but because it's very old! You don't have functions because tcsh uses an ad-hoc parser (vs. a "proper" lexer/tokenizer), which is quirky at times, and difficult to modify. No one has bothered to either rewrite it, or extend it so that functions are supported (so far...) Nov 25 '14 at 12:26
  • @GerryLufwansa Also, when I have more time, I'll make a patch proper with some of the other environment variables that bash has. It seems quite useful, and it's on my TODO-list :-) Nov 25 '14 at 12:27
  • By "programmable tab completion" I was referring to the way a user can provide custom completion answers via shell function or external command. By not providing something like COMMAND_LINE to an external command, it seems to me that no one or few people are actually doing "real" completion with tcsh. But perhaps there has not been such need or focus in the tcsh community, and the 'complete' command is already regarded as enough. Nov 25 '14 at 21:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.