Eli Zaretskii <eliz@gnu.org> writes:
>> Why isn't the compiler aware of the foo-var variable?
>
> Because lexical-binding is turned on?
yes it is but I miss the deep understanding to fully understand.
Lexical binding isn't the issue - the variable is global in scope.
The issue is that when you explicitly run the byte-compiler in batch mode, the "defvar" _expression_ is only compiled, not evaluated, while the defmacro is evaluated, and the application of the macro function is evaluated during compilation.
Hence you need "eval-when-compile" per Tomas's response.
The reason it works in interactive mode is that the defvar is evaluated, not compiled, and the macro function is evaluated in the run-time environment.
Lynn