C - stuck on Multiple completions #wh6

I’m stuck on Multiple completions #wh6

I’ve followed an earlier note to use readline instead of building from scratch and it worked up to this point by hooking custom functions.
This stage requires a specific format of completions, which uses a different separator than the default.

The closest thing I found is rl_completion_display_matches_hook which does the job, but loses the prompt in the process and any extra readline function I call to remedy that gives me a segfault.

Now I wonder if readline is the tool for this job? I must be missing something.

Hey @horse-966, sorry for the delayed response!

You likely don’t need to use rl_completion_display_matches_hook here. readline will automatically adjust the display based on the number of matches.


You’ll need to trigger the bell (return NULL from the completion() function) when there are multiple matches (the first time), instead of returning rl_completion_matches(text, generator) directly.

Let me know if you’d like further clarification!

No worries, thanks for getting back to me.

Not sure I follow, to be honest (haven’t used readline before). Do you mean to skip rl_complete_matches call, generate and output the complition list in completion() and then return NULL? I see this triggers the bell and passes the bell part of the test, Thanks

Great progress!

Hint: the rl_completion_matches function returns an array of matches, or NULL if there are none.

You’ll likely want to do something like this:

char **completion(const char *text, int start, int end) {
    char **matches = rl_completion_matches(text, generator);
    ...
}

Let me know if you’d like further clarification.

I noticed that after I replied :stuck_out_tongue: So I guess restoring the prompt is the last obstacle (rl_restore_prompt() is only a partial solution), but I’ll take another look at the docs, thanks for a quick reply!

1 Like

Got stuck on this one - it works as expected locally, I can type after completion and there’s a space between that and the completed command.I wonder why that space is not detected by the tester.
Actually, it’s visible in the tester if I type something extra after the space. It’s almost as if the prompt is trimmed of trailing whitespace in the tester, but that’s highly unlikely.

Example with added a after the space

I’m not sure the highlighted parts are strictly necessary. What issues are you seeing without them?

I could be wrong, but as long as the completion() function returns the correct matches (or NULL), readline should take care of the output formatting automatically.

Wow, I never tried simply returning matches for whatever reason…Talk about overcomplicating a problem. You’re right, it just works. Well, at least I learned a few other things about readline :rofl: Thanks!

1 Like