summaryrefslogtreecommitdiff
path: root/.config/nvim/lua/autocmds.lua
diff options
context:
space:
mode:
authorKyren223 <kyren223@proton.me>2025-08-01 18:12:27 +0300
committerKyren223 <kyren223@proton.me>2025-08-01 18:12:27 +0300
commitdaed0a2277ca1b2f9a23c089fd19c60024b270db (patch)
tree33228daa4ef13821e5112203ceb9f9868e11f427 /.config/nvim/lua/autocmds.lua
parent6d5a01d6b0bae1d687eec916d86d31f2fff1cc46 (diff)
C file templates
Diffstat (limited to '.config/nvim/lua/autocmds.lua')
-rw-r--r--.config/nvim/lua/autocmds.lua56
1 files changed, 56 insertions, 0 deletions
diff --git a/.config/nvim/lua/autocmds.lua b/.config/nvim/lua/autocmds.lua
index 9b54e17..f69b7cb 100644
--- a/.config/nvim/lua/autocmds.lua
+++ b/.config/nvim/lua/autocmds.lua
@@ -122,3 +122,59 @@ vim.api.nvim_create_autocmd('BufWritePre', {
vim.lsp.buf.format({ async = false })
end,
})
+
+-- NOTE: template for C/C++ files
+vim.api.nvim_create_autocmd({ 'BufNewFile', 'BufEnter' }, {
+ pattern = { '*.c', '*.h', '*.cpp' },
+ callback = function()
+ local buf = vim.api.nvim_get_current_buf()
+ local line_count = vim.api.nvim_buf_line_count(buf)
+ local is_empty = line_count == 1 and vim.api.nvim_buf_get_lines(buf, 0, 1, false)[1] == ''
+ if not is_empty then
+ return
+ end
+
+ local ext = vim.fn.expand('%:e')
+ local cwd = vim.fn.getcwd()
+ local project_name = vim.fn.fnamemodify(cwd, ':t')
+ local copyright = nil
+ local lines = nil
+
+ if project_name == 'krypton' then
+ copyright = {
+ '// Copyright (c) Kyren223',
+ '// Licensed under the MIT license (https://opensource.org/license/mit/)',
+ '',
+ }
+
+ local c_template = {
+ '#include "base.h"',
+ '',
+ }
+
+ lines = {
+ c = c_template,
+ h = {
+ '#ifndef ' .. string.upper(vim.fn.expand('%:t:r')) .. '_H',
+ '#define ' .. string.upper(vim.fn.expand('%:t:r')) .. '_H',
+ '',
+ '#include "' .. vim.fn.expand('%:t:r') .. '.meta.h"',
+ '',
+ '#endif',
+ },
+ cpp = c_template,
+ }
+ end
+
+ if not lines then
+ return
+ end
+ local content = lines[ext]
+
+ if copyright then
+ content = vim.list_extend(copyright, content)
+ end
+
+ vim.api.nvim_buf_set_lines(0, 0, -1, false, content)
+ end,
+})