From 32e575aa75792c63f710f0bdc3e2fb5aa8ea75ad Mon Sep 17 00:00:00 2001 From: Zhou Fang Date: Sat, 15 Feb 2025 16:27:01 +0900 Subject: [PATCH] feat(keymaps): enable toggling in quickfix list and location list (#5608) ## Description This PR addresses issue #5503 by modifying the behavior of `xq` and `xl` so that they toggle the quickfix and location lists rather than simply opening them. To prevent the full Lua error stack from being printed when no list exists prior to using `copen` or `lopen`, I've wrapped the commands in `pcall` and used `vim.notify` to alert the user in the same manner as before. ## Related Issue(s) #5503 ## Screenshots https://github.com/user-attachments/assets/b22b1861-e6e5-4d8a-967e-f760cca15719 ## Checklist - [x] I've read the [CONTRIBUTING](https://github.com/LazyVim/LazyVim/blob/main/CONTRIBUTING.md) guidelines. --- lua/lazyvim/config/keymaps.lua | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/lua/lazyvim/config/keymaps.lua b/lua/lazyvim/config/keymaps.lua index 3e3829d1..031e7054 100644 --- a/lua/lazyvim/config/keymaps.lua +++ b/lua/lazyvim/config/keymaps.lua @@ -94,8 +94,21 @@ map("n", "l", "Lazy", { desc = "Lazy" }) -- new file map("n", "fn", "enew", { desc = "New File" }) -map("n", "xl", "lopen", { desc = "Location List" }) -map("n", "xq", "copen", { desc = "Quickfix List" }) +-- location list +map("n", "xl", function() + local success, err = pcall(vim.fn.getloclist(0, { winid = 0 }).winid ~= 0 and vim.cmd.lclose or vim.cmd.lopen) + if not success and err then + vim.notify(err, vim.log.levels.ERROR) + end +end, { desc = "Location List" }) + +-- quickfix list +map("n", "xq", function() + local success, err = pcall(vim.fn.getqflist({ winid = 0 }).winid ~= 0 and vim.cmd.cclose or vim.cmd.copen) + if not success and err then + vim.notify(err, vim.log.levels.ERROR) + end +end, { desc = "Quickfix List" }) map("n", "[q", vim.cmd.cprev, { desc = "Previous Quickfix" }) map("n", "]q", vim.cmd.cnext, { desc = "Next Quickfix" })