Your hunch is right. This line from your completion function
let substr = matchstr(substr, "[^ \t]*$") " word till cursor
grabs all the non-whitespace characters in front of the cursor; m/$variab
in your example.
Because it contains a slash, the file completion is then triggered, which doesn't consider the $variable
, naturally.
To fix this, you could switch to matching only keyword characters (\k*$
, what the default completion considers), but that might negatively affect the file completion case.