From f2ec2db91cf18fc7ce1cdf0e15139f99ef7dc5c3 Mon Sep 17 00:00:00 2001 From: klein panic Date: Wed, 29 Jan 2025 16:49:22 -0500 Subject: [PATCH] fixed structure, removed backups, added compilation features for C, C++, and ASM --- nvim/.gitignore | 1 + nvim/init.lua.bak | 73 ----------- nvim/lua/compilation.lua | 137 +++++++++++++++------ nvim/pack/nvim/start/cmp-bufffer | 1 - nvim/pack/nvim/start/cmp-cmdline | 1 - nvim/pack/nvim/start/cmp-nvim-lsp | 1 - nvim/pack/nvim/start/cmp-path | 1 - nvim/pack/nvim/start/cmp_luasnip | 1 - nvim/pack/nvim/start/comment | 1 - nvim/pack/nvim/start/gitsigns | 1 - nvim/pack/nvim/start/gruvbox | 1 - nvim/pack/nvim/start/lualine | 1 - nvim/pack/nvim/start/luasnip | 1 - nvim/pack/nvim/start/markdown-preview.nvim | 1 - nvim/pack/nvim/start/nvim-autopairs | 1 - nvim/pack/nvim/start/nvim-cmp | 1 - nvim/pack/nvim/start/nvim-lspconfig | 1 - nvim/pack/nvim/start/nvim-treesitter | 1 - nvim/pack/nvim/start/nvim-web-devicons | 1 - nvim/pack/nvim/start/plenary | 1 - nvim/pack/nvim/start/telescope | 1 - nvim/pack/nvim/start/vimwiki | 1 - nvim/pack/nvim/start/vimwiki-markdown | 1 - nvim/pack/nvim/start/which-key | 1 - 24 files changed, 100 insertions(+), 132 deletions(-) delete mode 100644 nvim/init.lua.bak delete mode 160000 nvim/pack/nvim/start/cmp-bufffer delete mode 160000 nvim/pack/nvim/start/cmp-cmdline delete mode 160000 nvim/pack/nvim/start/cmp-nvim-lsp delete mode 160000 nvim/pack/nvim/start/cmp-path delete mode 160000 nvim/pack/nvim/start/cmp_luasnip delete mode 160000 nvim/pack/nvim/start/comment delete mode 160000 nvim/pack/nvim/start/gitsigns delete mode 160000 nvim/pack/nvim/start/gruvbox delete mode 160000 nvim/pack/nvim/start/lualine delete mode 160000 nvim/pack/nvim/start/luasnip delete mode 160000 nvim/pack/nvim/start/markdown-preview.nvim delete mode 160000 nvim/pack/nvim/start/nvim-autopairs delete mode 160000 nvim/pack/nvim/start/nvim-cmp delete mode 160000 nvim/pack/nvim/start/nvim-lspconfig delete mode 160000 nvim/pack/nvim/start/nvim-treesitter delete mode 160000 nvim/pack/nvim/start/nvim-web-devicons delete mode 160000 nvim/pack/nvim/start/plenary delete mode 160000 nvim/pack/nvim/start/telescope delete mode 160000 nvim/pack/nvim/start/vimwiki delete mode 160000 nvim/pack/nvim/start/vimwiki-markdown delete mode 160000 nvim/pack/nvim/start/which-key diff --git a/nvim/.gitignore b/nvim/.gitignore index 06199ea..3f12821 100644 --- a/nvim/.gitignore +++ b/nvim/.gitignore @@ -1 +1,2 @@ pack/ +backups/ diff --git a/nvim/init.lua.bak b/nvim/init.lua.bak deleted file mode 100644 index e515c2e..0000000 --- a/nvim/init.lua.bak +++ /dev/null @@ -1,73 +0,0 @@ --- General -vim.o.number = true - --- gruvbox -vim.o.background = "dark" -vim.cmd([[colorscheme gruvbox]]) - - --- treesitter -require'nvim-treesitter.configs'.setup{ - highlight = { - enable = true, - }, - indent = { - enable = true, - } -} - - --- vimwiki -vim.cmd([[ - set nocompatible - filetype plugin on - syntax on - ]]) -vim.g.vimwiki_list = { - { - path = '~/vimwiki/', - syntax = 'markdown', - ext = '.md' - } -} - - --- telescope -local builtin = require('telescope.builtin') -vim.keymap.set('n', 'ff', builtin.find_files, { desc = 'Telescope find files' }) -vim.keymap.set('n', 'fg', builtin.live_grep, { desc = 'Telescope live grep' }) -vim.keymap.set('n', 'fb', builtin.buffers, { desc = 'Telescope buffers' }) -vim.keymap.set('n', 'fh', builtin.help_tags, { desc = 'Telescope help tags' }) - - --- lualine -require('lualine').setup() - - --- comment -require('Comment').setup() - - --- autopairs -require('nvim-autopairs').setup() - - --- cmp -local cmp = require'cmp' -cmp.setup({ - snippet = { - expand = function(args) - require('luasnip').lsp_expand(args.body) - end - } -}) -cmp.setup.cmdline(':', { - mapping = cmp.mapping.preset.cmdline(), - sources = cmp.config.sources({ - { name = 'path' } - }, { - { name = 'cmdline' } - }), - matching = { disallow_symbol_nonprefix_matching = false } -}) --- TODO complete https://github.com/hrsh7th/nvim-cmp diff --git a/nvim/lua/compilation.lua b/nvim/lua/compilation.lua index 6741ebd..42a0ba2 100644 --- a/nvim/lua/compilation.lua +++ b/nvim/lua/compilation.lua @@ -1,6 +1,12 @@ -- File: ~/.config/nvim/lua/compilation.lua local M = {} +local exec_terminal_buf = nil -- Module-level variable to track the exec terminal buffer + +-- Utility function for non-blocking notifications +local function notify(msg, hl) + vim.api.nvim_echo({{msg, hl}}, true, {}) +end -- Function to compile and run the current file function M.compile_and_run() @@ -9,15 +15,15 @@ function M.compile_and_run() -- Check if the current buffer is a directory if vim.fn.isdirectory(file) == 1 then - vim.notify("Cannot compile a directory. Please open a source file.", vim.log.levels.ERROR) + notify("Cannot compile a directory. Please open a source file.", "ErrorMsg") return end - -- Determine the filetype (c, cpp, etc.) + -- Determine the filetype (c, cpp, asm, etc.) local filetype = vim.bo.filetype local compiler = '' local flags = '' - + -- Set compiler and flags based on filetype if filetype == 'c' then compiler = 'gcc' @@ -25,70 +31,125 @@ function M.compile_and_run() elseif filetype == 'cpp' then compiler = 'g++' flags = '-lm -O3' + elseif filetype == 'asm' then + compiler = 'nasm' + flags = '-f elf64' else - vim.notify("Unsupported file type: " .. filetype, vim.log.levels.ERROR) + notify("Unsupported file type: " .. filetype, "ErrorMsg") return end -- Get the base name of the file without extension local output_name = vim.fn.expand('%:t:r') -- %:t:r extracts the filename without extension - -- Construct the compile command - local compile_cmd = string.format('%s %s "%s" -o "%s"', compiler, flags, file, output_name) - - -- Notify the user about the compilation process - vim.notify("Compiling...", vim.log.levels.INFO) - - -- Execute the compile command - local compile_output = vim.fn.system(compile_cmd) - local compile_exit = vim.v.shell_error + if filetype == 'asm' then + -- Assemble the asm file to object file + local assemble_cmd = string.format('%s %s "%s" -o "%s.o"', compiler, flags, file, output_name) + + -- Notify the user about the assembly process + notify("Assembling...", "MoreMsg") + + -- Execute the assemble command + local assemble_output = vim.fn.system(assemble_cmd) + local assemble_exit = vim.v.shell_error - -- Check if compilation was successful - if compile_exit ~= 0 then - vim.notify("Compilation failed:\n" .. compile_output, vim.log.levels.ERROR) - -- Populate quickfix list with errors - vim.fn.setqflist({}, ' ') - for line in compile_output:gmatch("[^\r\n]+") do - if line:match("error") or line:match("warning") then - vim.fn.setqflist({}, 'a', { lines = { line } }) + -- Check if assembly was successful + if assemble_exit ~= 0 then + notify("Assembly failed:\n" .. assemble_output, "ErrorMsg") + -- Populate quickfix list with errors and warnings + vim.fn.setqflist({}, ' ') + for line in assemble_output:gmatch("[^\r\n]+") do + if line:match("error") or line:match("warning") then + vim.fn.setqflist({}, 'a', { lines = { line } }) + end end + vim.cmd('copen') -- Open the quickfix list to show errors + return + end + + -- Link the object file to create executable + local link_cmd = string.format('ld "%s.o" -o "%s"', output_name, output_name) + + -- Notify the user about the linking process + notify("Linking...", "MoreMsg") + + -- Execute the link command + local link_output = vim.fn.system(link_cmd) + local link_exit = vim.v.shell_error + + -- Check if linking was successful + if link_exit ~= 0 then + notify("Linking failed:\n" .. link_output, "ErrorMsg") + -- Populate quickfix list with errors and warnings + vim.fn.setqflist({}, ' ') + for line in link_output:gmatch("[^\r\n]+") do + if line:match("error") or line:match("warning") then + vim.fn.setqflist({}, 'a', { lines = { line } }) + end + end + vim.cmd('copen') -- Open the quickfix list to show errors + return + else + notify("Compilation successful!", "MoreMsg") + end + + else + -- For C and C++, compile and link in one step + local compile_cmd = string.format('%s %s "%s" -o "%s"', compiler, flags, file, output_name) + + -- Notify the user about the compilation process + notify("Compiling...", "MoreMsg") + + -- Execute the compile command + local compile_output = vim.fn.system(compile_cmd) + local compile_exit = vim.v.shell_error + + -- Check if compilation was successful + if compile_exit ~= 0 then + notify("Compilation failed:\n" .. compile_output, "ErrorMsg") + -- Populate quickfix list with errors and warnings + vim.fn.setqflist({}, ' ') + for line in compile_output:gmatch("[^\r\n]+") do + if line:match("error") or line:match("warning") then + vim.fn.setqflist({}, 'a', { lines = { line } }) + end + end + vim.cmd('copen') -- Open the quickfix list to show errors + return + else + notify("Compilation successful!", "MoreMsg") end - vim.cmd('copen') -- Open the quickfix list to show errors - return end -- Construct the run command local run_cmd = string.format('./%s', output_name) -- Notify the user about the execution process - vim.notify("Running executable...", vim.log.levels.INFO) + notify("Running executable...", "MoreMsg") - -- Function to check if a terminal is already open - local function is_terminal_open() + -- Function to find an existing terminal window for execution + local function find_exec_terminal() for _, win in ipairs(vim.api.nvim_list_wins()) do local buf = vim.api.nvim_win_get_buf(win) if vim.api.nvim_buf_get_option(buf, 'buftype') == 'terminal' then - return true + return win end end - return false + return nil end -- Run the executable in a terminal split at the bottom - if not is_terminal_open() then - vim.cmd('botright split') -- Open split at the bottom + local exec_win = find_exec_terminal() + + if not exec_win then + -- Open a new terminal split at the bottom + vim.cmd('botright split') vim.cmd('resize 15') -- Optional: Adjust the size of the split vim.cmd('terminal ' .. run_cmd) -- Run the executable else -- If a terminal is already open, send the run command to it - for _, win in ipairs(vim.api.nvim_list_wins()) do - local buf = vim.api.nvim_win_get_buf(win) - if vim.api.nvim_buf_get_option(buf, 'buftype') == 'terminal' then - vim.api.nvim_set_current_win(win) - vim.api.nvim_feedkeys(run_cmd .. '\n', 'n', true) - break - end - end + vim.api.nvim_set_current_win(exec_win) + vim.api.nvim_chan_send(vim.b.terminal_job_id, run_cmd .. '\n') end end diff --git a/nvim/pack/nvim/start/cmp-bufffer b/nvim/pack/nvim/start/cmp-bufffer deleted file mode 160000 index 3022dbc..0000000 --- a/nvim/pack/nvim/start/cmp-bufffer +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 3022dbc9166796b644a841a02de8dd1cc1d311fa diff --git a/nvim/pack/nvim/start/cmp-cmdline b/nvim/pack/nvim/start/cmp-cmdline deleted file mode 160000 index d250c63..0000000 --- a/nvim/pack/nvim/start/cmp-cmdline +++ /dev/null @@ -1 +0,0 @@ -Subproject commit d250c63aa13ead745e3a40f61fdd3470efde3923 diff --git a/nvim/pack/nvim/start/cmp-nvim-lsp b/nvim/pack/nvim/start/cmp-nvim-lsp deleted file mode 160000 index 99290b3..0000000 --- a/nvim/pack/nvim/start/cmp-nvim-lsp +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 99290b3ec1322070bcfb9e846450a46f6efa50f0 diff --git a/nvim/pack/nvim/start/cmp-path b/nvim/pack/nvim/start/cmp-path deleted file mode 160000 index 91ff86c..0000000 --- a/nvim/pack/nvim/start/cmp-path +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 91ff86cd9c29299a64f968ebb45846c485725f23 diff --git a/nvim/pack/nvim/start/cmp_luasnip b/nvim/pack/nvim/start/cmp_luasnip deleted file mode 160000 index 98d9cb5..0000000 --- a/nvim/pack/nvim/start/cmp_luasnip +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 98d9cb5c2c38532bd9bdb481067b20fea8f32e90 diff --git a/nvim/pack/nvim/start/comment b/nvim/pack/nvim/start/comment deleted file mode 160000 index e30b7f2..0000000 --- a/nvim/pack/nvim/start/comment +++ /dev/null @@ -1 +0,0 @@ -Subproject commit e30b7f2008e52442154b66f7c519bfd2f1e32acb diff --git a/nvim/pack/nvim/start/gitsigns b/nvim/pack/nvim/start/gitsigns deleted file mode 160000 index 3ec5fbd..0000000 --- a/nvim/pack/nvim/start/gitsigns +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 3ec5fbd9202ae3908551c98c4b6c3c05ff7c8e96 diff --git a/nvim/pack/nvim/start/gruvbox b/nvim/pack/nvim/start/gruvbox deleted file mode 160000 index 68c3460..0000000 --- a/nvim/pack/nvim/start/gruvbox +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 68c3460a5d1d1a362318960035c9f3466d5011f5 diff --git a/nvim/pack/nvim/start/lualine b/nvim/pack/nvim/start/lualine deleted file mode 160000 index 2a5bae9..0000000 --- a/nvim/pack/nvim/start/lualine +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 2a5bae925481f999263d6f5ed8361baef8df4f83 diff --git a/nvim/pack/nvim/start/luasnip b/nvim/pack/nvim/start/luasnip deleted file mode 160000 index c9b9a22..0000000 --- a/nvim/pack/nvim/start/luasnip +++ /dev/null @@ -1 +0,0 @@ -Subproject commit c9b9a22904c97d0eb69ccb9bab76037838326817 diff --git a/nvim/pack/nvim/start/markdown-preview.nvim b/nvim/pack/nvim/start/markdown-preview.nvim deleted file mode 160000 index a923f5f..0000000 --- a/nvim/pack/nvim/start/markdown-preview.nvim +++ /dev/null @@ -1 +0,0 @@ -Subproject commit a923f5fc5ba36a3b17e289dc35dc17f66d0548ee diff --git a/nvim/pack/nvim/start/nvim-autopairs b/nvim/pack/nvim/start/nvim-autopairs deleted file mode 160000 index 3d02855..0000000 --- a/nvim/pack/nvim/start/nvim-autopairs +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 3d02855468f94bf435db41b661b58ec4f48a06b7 diff --git a/nvim/pack/nvim/start/nvim-cmp b/nvim/pack/nvim/start/nvim-cmp deleted file mode 160000 index 1250990..0000000 --- a/nvim/pack/nvim/start/nvim-cmp +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 12509903a5723a876abd65953109f926f4634c30 diff --git a/nvim/pack/nvim/start/nvim-lspconfig b/nvim/pack/nvim/start/nvim-lspconfig deleted file mode 160000 index b4d65bc..0000000 --- a/nvim/pack/nvim/start/nvim-lspconfig +++ /dev/null @@ -1 +0,0 @@ -Subproject commit b4d65bce97795438ab6e1974b3672c17a4865e3c diff --git a/nvim/pack/nvim/start/nvim-treesitter b/nvim/pack/nvim/start/nvim-treesitter deleted file mode 160000 index 6587a58..0000000 --- a/nvim/pack/nvim/start/nvim-treesitter +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 6587a5886873cce8698a47477224c30578b33a24 diff --git a/nvim/pack/nvim/start/nvim-web-devicons b/nvim/pack/nvim/start/nvim-web-devicons deleted file mode 160000 index aafa5c1..0000000 --- a/nvim/pack/nvim/start/nvim-web-devicons +++ /dev/null @@ -1 +0,0 @@ -Subproject commit aafa5c187a15701a7299a392b907ec15d9a7075f diff --git a/nvim/pack/nvim/start/plenary b/nvim/pack/nvim/start/plenary deleted file mode 160000 index 3707cdb..0000000 --- a/nvim/pack/nvim/start/plenary +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 3707cdb1e43f5cea73afb6037e6494e7ce847a66 diff --git a/nvim/pack/nvim/start/telescope b/nvim/pack/nvim/start/telescope deleted file mode 160000 index 415af52..0000000 --- a/nvim/pack/nvim/start/telescope +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 415af52339215926d705cccc08145f3782c4d132 diff --git a/nvim/pack/nvim/start/vimwiki b/nvim/pack/nvim/start/vimwiki deleted file mode 160000 index 7279261..0000000 --- a/nvim/pack/nvim/start/vimwiki +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 72792615e739d0eb54a9c8f7e0a46a6e2407c9e8 diff --git a/nvim/pack/nvim/start/vimwiki-markdown b/nvim/pack/nvim/start/vimwiki-markdown deleted file mode 160000 index 1b6007d..0000000 --- a/nvim/pack/nvim/start/vimwiki-markdown +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 1b6007d528912c06de3008c134f04462a84beb61 diff --git a/nvim/pack/nvim/start/which-key b/nvim/pack/nvim/start/which-key deleted file mode 160000 index 6cebd86..0000000 --- a/nvim/pack/nvim/start/which-key +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 6cebd86917df559a88de0f806b2989799c6e6423