I'm trying to convert the following conditional mapping into lua code
:nnoremap <expr> <CR> ((&buftype is# "quickfix") ? "<CR>" : ":\:nohlsearch<cr>\n")
This mappigng maps Enter to a :nohlsearch command, but only if we are not in the quickfix buffer. There, you need the Enter for selecting lines.
The lua code that I came with is:
vim.keymap.set("n", "<CR>", function()
if vim.bo.buftype == "quickfix" then
print("<CR>")
vim.api.nvim_input("<CR>")
else
print("nohlsearch")
vim.cmd(":nohlsearch")
end
end, {desc = "call :nohlsearch to disable highlighting (but only if in file editing)"} )
The branching, the vim.cmd, and the print() parts work as expected, but the vim.api.nvim_input("<CR>") part doesn't work (in quickfix I can't use Enter to select a line). It's not equivalent to the <CR> in the vimscript mapping.
Also, there has to be a more idiomatic way to write this mapping.
I started this conversion because I need to make the condition more complex.