Using an expression mapping is the easiest way to do this:
nnoremap <expr> <CR> &buftype ==# 'quickfix' ? "\<CR>" : 'za'
This defines a mapping that checks whether you are in the quickfix window, and then performs either a <CR>
or za
accordingly.
See:
:h :map-expr
:h 'buftype'
:h expr1
:h string
Mapping to a function
You can also define a function that carries out a regular enter when invoked from the quickfix window, and does 'za' otherwise. This isn't efficient for what you're trying to do, but I'm leaving it here because it's useful info for if you want to map to something more complicated than a normal mode command:
function! FoldingEnter()
if &buftype ==# 'quickfix'
execute "normal! \<CR>"
else
normal! za
endif
endfunction
nnoremap <CR> :call FoldingEnter()<CR>