diff --git a/lua/autocmd.lua b/lua/autocmd.lua index fd250a4..e3afaf9 100644 --- a/lua/autocmd.lua +++ b/lua/autocmd.lua @@ -54,4 +54,33 @@ vim.api.nvim_create_user_command('WordCount', function() print('Word count: ' .. words) end, { desc = 'Count words in current buffer' }) +vim.api.nvim_create_user_command('HeaderGuard', function() + local filename = vim.fn.expand '%:t' -- filename.ext + local stem = vim.fn.expand '%:t:r' -- filename without extension + local ext = vim.fn.expand '%:e' -- file extension + + if ext ~= 'h' then + print 'HeaderGuard: Not a .h file!' + return + end + + local guard = string.upper(stem) .. '_H' + + -- Insert at top + vim.api.nvim_buf_set_lines(0, 0, 0, false, { + '#ifndef ' .. guard, + '#define ' .. guard, + '', + }) + + -- Insert at bottom + local last_line = vim.api.nvim_buf_line_count(0) + vim.api.nvim_buf_set_lines(0, last_line, last_line, false, { + '', + '#endif // ' .. guard, + }) + + print('Header guard inserted: ' .. guard) +end, { desc = 'Insert header guard for current .h file' }) + -- vim: ts=2 sts=2 sw=2 et