diff --git a/nvim/init.lua b/nvim/init.lua index f9129a8..bda7b89 100644 --- a/nvim/init.lua +++ b/nvim/init.lua @@ -102,141 +102,81 @@ vim.lsp.config("pyright", { local notify = require("notify") -vim.keymap.set("n", "cd", function() - local buf = vim.api.nvim_create_buf(false, true) +vim.defer_fn(function() + local cmake = require("config.cmake").find_cmake() + if cmake and cmake ~= "cmake" then + --vim.notify("CMake found at: " .. cmake, vim.log.levels.INFO, { title = "Build System" }) + end +end, 1000) - local width = math.floor(vim.o.columns * 0.8) - local height = math.floor(vim.o.lines * 0.5) +-- 1) resolve cmake once +local function find_cmake() + local p = vim.fn.exepath("cmake") + if p ~= "" then return p end + for _, f in ipairs({"/opt/homebrew/bin/cmake","/usr/local/bin/cmake","/usr/bin/cmake"}) do + if vim.fn.filereadable(f) == 1 then return f end + end + return "cmake" -- last-ditch; may still fail +end +local CMAKE = find_cmake() + +-- 2) tiny float-term helper +local function float_term(cmd, opts) + opts = opts or {} + local buf = vim.api.nvim_create_buf(false, true) + local width = math.floor(vim.o.columns * (opts.width_ratio or 0.8)) + local height = math.floor(vim.o.lines * (opts.height_ratio or 0.5)) local row = math.floor((vim.o.lines - height) / 2) local col = math.floor((vim.o.columns - width) / 2) - - local opts = { - style = "minimal", - relative = "editor", - width = width, - height = height, - row = row, - col = col, - border = "rounded", - } - - vim.api.nvim_open_win(buf, true, opts) - - vim.fn.termopen("mkdir -p build && cd build && cmake -DCMAKE_BUILD_TYPE=Debug -DCMAKE_EXPORT_COMPILE_COMMANDS=ON ..", { - on_stdout = function(_, _, _) - local win = vim.api.nvim_get_current_win() - vim.api.nvim_win_set_cursor(win, {vim.api.nvim_buf_line_count(0), 0}) - end, - on_stderr = function(_, _, _) - local win = vim.api.nvim_get_current_win() - vim.api.nvim_win_set_cursor(win, {vim.api.nvim_buf_line_count(0), 0}) - end, + vim.api.nvim_open_win(buf, true, { + style = "minimal", relative = "editor", + width = width, height = height, row = row, col = col, + border = opts.border or "rounded", }) - vim.api.nvim_buf_set_keymap(buf, "n", "q", "bd!", { silent = true, noremap = true }) -end, { desc = "Configure CMake Debug Build in floating terminal" }) - -vim.keymap.set("n", "cr", function() - local buf = vim.api.nvim_create_buf(false, true) - - local width = math.floor(vim.o.columns * 0.8) - local height = math.floor(vim.o.lines * 0.5) - local row = math.floor((vim.o.lines - height) / 2) - local col = math.floor((vim.o.columns - width) / 2) - - local opts = { - style = "minimal", - relative = "editor", - width = width, - height = height, - row = row, - col = col, - border = "rounded", - } - - vim.api.nvim_open_win(buf, true, opts) - - vim.fn.termopen("mkdir -p build && cd build && cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_EXPORT_COMPILE_COMMANDS=ON ..", { - on_stdout = function(_, _, _) - local win = vim.api.nvim_get_current_win() - vim.api.nvim_win_set_cursor(win, {vim.api.nvim_buf_line_count(0), 0}) - end, - on_stderr = function(_, _, _) - local win = vim.api.nvim_get_current_win() - vim.api.nvim_win_set_cursor(win, {vim.api.nvim_buf_line_count(0), 0}) - end, - }) - - vim.api.nvim_buf_set_keymap(buf, "n", "q", "bd!", { silent = true, noremap = true }) -end, { desc = "Configure CMake Release Build in floating terminal" }) - -vim.keymap.set("n", "cp", function() - local buf = vim.api.nvim_create_buf(false, true) - - local width = math.floor(vim.o.columns * 0.8) - local height = math.floor(vim.o.lines * 0.5) - local row = math.floor((vim.o.lines - height) / 2) - local col = math.floor((vim.o.columns - width) / 2) - - local opts = { - style = "minimal", - relative = "editor", - width = width, - height = height, - row = row, - col = col, - border = "rounded", - } - - vim.api.nvim_open_win(buf, true, opts) - - vim.fn.termopen("mkdir -p build && cd build && cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_EXPORT_COMPILE_COMMANDS=ON ..", { - on_stdout = function(_, _, _) - local win = vim.api.nvim_get_current_win() - vim.api.nvim_win_set_cursor(win, {vim.api.nvim_buf_line_count(0), 0}) - end, - on_stderr = function(_, _, _) - local win = vim.api.nvim_get_current_win() - vim.api.nvim_win_set_cursor(win, {vim.api.nvim_buf_line_count(0), 0}) - end, - }) - - vim.api.nvim_buf_set_keymap(buf, "n", "q", "bd!", { silent = true, noremap = true }) -end, { desc = "Configure CMake RelWithDebugInfo Build in floating terminal" }) - -vim.api.nvim_create_user_command("Build", function(opts) - local target = opts.args - if target == "" then - print("Usage: :Build ") - return + local function scroll_bottom() + local win = vim.api.nvim_get_current_win() + vim.api.nvim_win_set_cursor(win, {vim.api.nvim_buf_line_count(0), 0}) end - local buf = vim.api.nvim_create_buf(false, true) - local win = vim.api.nvim_open_win(buf, true, { - relative = 'editor', - width = math.floor(vim.o.columns * 0.8), - height = math.floor(vim.o.lines * 0.8), - row = math.floor(vim.o.lines * 0.1), - col = math.floor(vim.o.columns * 0.1), - style = 'minimal', - border = 'single', - }) - - vim.fn.termopen("cmake --build build --parallel 20 --target " .. target, { - on_stdout = function(_, _, _) - local win = vim.api.nvim_get_current_win() - vim.api.nvim_win_set_cursor(win, {vim.api.nvim_buf_line_count(0), 0}) - end, - on_stderr = function(_, _, _) - local win = vim.api.nvim_get_current_win() - vim.api.nvim_win_set_cursor(win, {vim.api.nvim_buf_line_count(0), 0}) + local job = vim.fn.termopen(cmd, { + cwd = opts.cwd, -- <= important + on_stdout = function(...) scroll_bottom() end, + on_stderr = function(...) scroll_bottom() end, + on_exit = function(_, code, _) + vim.schedule(function() + vim.notify(("build exited %d"):format(code), code == 0 and vim.log.levels.INFO or vim.log.levels.ERROR) + end) end, + env = opts.env, -- e.g. env = { PATH = os.getenv("PATH") } }) - vim.api.nvim_buf_set_keymap(buf, "n", "q", "bd!", { silent = true, noremap = true }) - end, { - nargs = 1, -}) + vim.keymap.set("n", "q", "bd!", { buffer = buf, silent = true }) + return job +end + +-- 3) configure without shell; mkdir via Lua; use cwd + argv form +local function configure_cmake(build_type) + vim.fn.mkdir("build", "p") + float_term( + { CMAKE, "-DCMAKE_BUILD_TYPE=" .. build_type, "-DCMAKE_EXPORT_COMPILE_COMMANDS=ON", ".." }, + { cwd = "build" } + ) +end + +vim.keymap.set("n", "cd", function() configure_cmake("Debug") end, { desc = "CMake Debug" }) +vim.keymap.set("n", "cr", function() configure_cmake("Release") end, { desc = "CMake Release" }) +vim.keymap.set("n", "cp", function() configure_cmake("RelWithDebInfo") end, { desc = "CMake RelWithDebInfo" }) + +-- 4) build target (no shell, proper argv, cwd) +vim.api.nvim_create_user_command("Build", function(opts) + local t = opts.args + if t == "" then print("Usage: :Build "); return end + float_term( + { CMAKE, "--build", "build", "--parallel", "20", "--target", t }, + { height_ratio = 0.8, width_ratio = 0.8, border = "single" } + ) +end, { nargs = 1 }) vim.api.nvim_create_autocmd("BufWritePre", { group = vim.api.nvim_create_augroup("AutoFormatOnSave", { clear = true }), diff --git a/nvim/lazy-lock.json b/nvim/lazy-lock.json index 9c8db87..024db5b 100644 --- a/nvim/lazy-lock.json +++ b/nvim/lazy-lock.json @@ -1,21 +1,22 @@ { - "LuaSnip": { "branch": "master", "commit": "73813308abc2eaeff2bc0d3f2f79270c491be9d7" }, + "LuaSnip": { "branch": "master", "commit": "21e9fecfc07fb2cd707b6c7c3fa148550a34d053" }, "cmp-nvim-lsp": { "branch": "main", "commit": "bd5a7d6db125d4654b50eeae9f5217f24bb22fd3" }, "cmp_luasnip": { "branch": "master", "commit": "98d9cb5c2c38532bd9bdb481067b20fea8f32e90" }, "lazy.nvim": { "branch": "main", "commit": "1ea3c4085785f460fb0e46d2fe1ee895f5f9e7c1" }, - "lualine.nvim": { "branch": "master", "commit": "b8c23159c0161f4b89196f74ee3a6d02cdc3a955" }, + "lualine.nvim": { "branch": "master", "commit": "e533fac71bc361768f90004af695cd1f1aa1900a" }, "monochrome.nvim": { "branch": "main", "commit": "2de78d9688ea4a177bcd9be554ab9192337d35ff" }, - "nvim-autopairs": { "branch": "master", "commit": "23320e75953ac82e559c610bec5a90d9c6dfa743" }, + "nvim-autopairs": { "branch": "master", "commit": "7a2c97cccd60abc559344042fefb1d5a85b3e33b" }, "nvim-cmp": { "branch": "main", "commit": "b5311ab3ed9c846b585c0c15b7559be131ec4be9" }, - "nvim-lspconfig": { "branch": "master", "commit": "623bcf08d5f9ff4ee3ce2686fa1f1947a045b1a5" }, + "nvim-lspconfig": { "branch": "master", "commit": "ac98db2f9f06a56498ec890a96928774eae412c3" }, "nvim-notify": { "branch": "master", "commit": "8701bece920b38ea289b457f902e2ad184131a5d" }, "nvim-tree.lua": { "branch": "master", "commit": "e397756d2a79d74314ea4cd3efc41300e91c0ff0" }, "nvim-treesitter": { "branch": "master", "commit": "42fc28ba918343ebfd5565147a42a26580579482" }, "nvim-web-devicons": { "branch": "master", "commit": "b8221e42cf7287c4dcde81f232f58d7b947c210d" }, "oxocarbon.nvim": { "branch": "main", "commit": "9f85f6090322f39b11ae04a343d4eb9d12a86897" }, "plenary.nvim": { "branch": "master", "commit": "b9fd5226c2f76c951fc8ed5923d85e4de065e509" }, + "smear-cursor.nvim": { "branch": "main", "commit": "abfa5835920b1d76c0e24e1465a618ad914be90a" }, "telescope.nvim": { "branch": "master", "commit": "a0bbec21143c7bc5f8bb02e0005fa0b982edc026" }, "tokyodark.nvim": { "branch": "master", "commit": "cc70a2fb809d5376f2bd8e5836f9bb3f5fb8ef43" }, - "tokyonight.nvim": { "branch": "main", "commit": "00d92e6009671493fceeb0e4baf644f5b983e6e4" }, + "tokyonight.nvim": { "branch": "main", "commit": "d14614cbfc63b6037bfccd48bb982d2ad2003352" }, "transparent.nvim": { "branch": "main", "commit": "8ac59883de84e9cd1850ea25cf087031c5ba7d54" } } diff --git a/nvim/lua/config/cmake.lua b/nvim/lua/config/cmake.lua new file mode 100644 index 0000000..29b2e3e --- /dev/null +++ b/nvim/lua/config/cmake.lua @@ -0,0 +1,41 @@ +local M = {} + +function M.find_cmake() + if vim.g.cmake_path and vim.fn.filereadable(vim.g.cmake_path) == 1 then + return vim.g.cmake_path + end + + local path = vim.fn.exepath("cmake") + if path ~= "" and vim.fn.filereadable(path) == 1 then + vim.g.cmake_path = path + return path + end + + local fallback_paths = { + "/opt/homebrew/bin/cmake", + "/usr/local/bin/cmake", + "/usr/bin/cmake", + } + for _, p in ipairs(fallback_paths) do + if vim.fn.filereadable(p) == 1 then + vim.g.cmake_path = p + return p + end + end + + local handle = io.popen("which cmake 2>/dev/null") + if handle then + local result = handle:read("*a"):gsub("%s+", "") + handle:close() + if result ~= "" and vim.fn.filereadable(result) == 1 then + vim.g.cmake_path = result + return result + end + end + + vim.notify("cmake not found in PATH or common locations", vim.log.levels.ERROR) + return "cmake" -- fallback, so termopen still runs even if broken +end + +return M + diff --git a/nvim/lua/plugins/base.lua b/nvim/lua/plugins/base.lua index 1856c96..73ac15e 100644 --- a/nvim/lua/plugins/base.lua +++ b/nvim/lua/plugins/base.lua @@ -1,4 +1,9 @@ return { + { + "sphamba/smear-cursor.nvim", + opts = {}, + }, + { "xiyaowong/transparent.nvim", lazy = false,