Skip to content

Commit 3b2e059

Browse files
committed
refactor: consolidate C file settings and commands into a single configuration
1 parent ae42921 commit 3b2e059

File tree

2 files changed

+95
-99
lines changed

2 files changed

+95
-99
lines changed

after/ftplugin/c.lua

Lines changed: 95 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,101 @@
1+
-- Import vim module
12
local set = vim.opt_local
23

3-
set.shiftwidth = 4
4-
set.tabstop = 4
5-
vim.g.sleuth_automatic = 0
4+
-- Indentation settings
5+
set.expandtab = true -- Convert tabs to spaces
6+
set.shiftwidth = 4 -- Indent size for autoindent
7+
set.tabstop = 4 -- How wide tabs appear
8+
set.softtabstop = 4 -- How many spaces Tab key inserts/removes
9+
10+
-- Comment string for commentary plugins
611
set.commentstring = '/* %s */'
712

13+
-- Disable vim-sleuth for C files
14+
vim.g.sleuth_automatic = 0
15+
816
-- Define a custom command ':IntMain' that inserts int main() {} template
917
vim.api.nvim_create_user_command('IntMain', function()
10-
local current_line = vim.api.nvim_win_get_cursor(0)[1]
11-
local lines = {
12-
'#include <stdio.h>',
13-
'',
14-
'int main ()',
15-
'{',
16-
' ',
17-
' printf();',
18-
' return 0;',
19-
'}',
20-
}
21-
vim.api.nvim_buf_set_lines(0, current_line - 1, current_line - 1, false, lines)
22-
-- Position cursor inside the function body
23-
vim.api.nvim_win_set_cursor(0, { current_line + 1, 4 })
24-
end, {})
25-
26-
vim.api.nvim_create_user_command('Libft', function()
27-
local current_line = vim.api.nvim_win_get_cursor(0)[1]
28-
vim.api.nvim_buf_set_lines(0, current_line - 1, current_line - 1, false, {
29-
'#include "libft.h"',
30-
})
31-
end, {})
32-
33-
-- Force these settings after any other scripts might have changed them
34-
vim.api.nvim_create_autocmd("FileType", {
35-
pattern = "c",
36-
callback = function()
37-
vim.opt_local.tabstop = 4
38-
vim.opt_local.shiftwidth = 4
39-
vim.opt_local.softtabstop = 4
40-
vim.opt_local.expandtab = true
41-
end
42-
})
18+
local current_line = vim.api.nvim_win_get_cursor(0)[1]
19+
local lines = {
20+
'#include <stdio.h>',
21+
'',
22+
'int main(void)',
23+
'{',
24+
' ',
25+
' printf("Hello, world!\\n");',
26+
' return 0;',
27+
'}',
28+
}
29+
vim.api.nvim_buf_set_lines(0, current_line - 1, current_line - 1, false, lines)
30+
-- Position cursor inside the function body
31+
vim.api.nvim_win_set_cursor(0, { current_line + 4, 4 })
32+
end, {})
33+
34+
-- Define a custom command ':Libft' that inserts libft.h include
35+
vim.api.nvim_create_user_command('Libft', function()
36+
local current_line = vim.api.nvim_win_get_cursor(0)[1]
37+
vim.api.nvim_buf_set_lines(0, current_line - 1, current_line - 1, false, {
38+
'#include "libft.h"',
39+
})
40+
end, {})
41+
42+
-- Define a custom command ':CommentRestOfFile' that comments out rest of file
43+
vim.api.nvim_create_user_command('CommentRestOfFile', function()
44+
local current_line = vim.api.nvim_win_get_cursor(0)[1]
45+
local last_line = vim.api.nvim_buf_line_count(0)
46+
47+
-- Get all lines from current to end of file
48+
local lines = vim.api.nvim_buf_get_lines(0, current_line - 1, last_line, false)
49+
50+
-- Prefix each line with //
51+
for i = 1, #lines do
52+
lines[i] = '//' .. lines[i]
53+
end
54+
55+
-- Set the modified lines back in the buffer
56+
vim.api.nvim_buf_set_lines(0, current_line - 1, last_line, false, lines)
57+
end, {})
58+
59+
-- Define a custom command ':UncommentRestOfFile' that removes comments from rest of file
60+
vim.api.nvim_create_user_command('UncommentRestOfFile', function ()
61+
local current_line = vim.api.nvim_win_get_cursor(0)[1]
62+
local last_line = vim.api.nvim_buf_line_count(0)
63+
64+
-- Get all lines from current to end of file
65+
local lines = vim.api.nvim_buf_get_lines(0, current_line - 1, last_line, false)
66+
67+
-- Remove leading // if present
68+
for i = 1, #lines do
69+
lines[i] = lines[i]:gsub("^%s*//", "")
70+
end
71+
72+
-- Set the modified lines back in the buffer
73+
vim.api.nvim_buf_set_lines(0, current_line - 1, last_line, false, lines)
74+
end, {})
75+
76+
-- -- Debugging command to check indentation settings
77+
-- vim.api.nvim_create_user_command('CheckIndent', function()
78+
-- print("expandtab: " .. tostring(vim.bo.expandtab))
79+
-- print("tabstop: " .. vim.bo.tabstop)
80+
-- print("shiftwidth: " .. vim.bo.shiftwidth)
81+
-- print("softtabstop: " .. vim.bo.softtabstop)
82+
-- end, {})
83+
84+
-- Force settings to apply after all other scripts have loaded
85+
vim.api.nvim_create_autocmd("BufEnter", {
86+
pattern = "*.c,*.h",
87+
callback = function()
88+
vim.opt_local.expandtab = true
89+
vim.opt_local.tabstop = 4
90+
vim.opt_local.shiftwidth = 4
91+
vim.opt_local.softtabstop = 4
92+
end
93+
})
94+
95+
-- Add any C-specific key mappings you want here
96+
-- Example: Map F5 to compile and run the current file
97+
vim.keymap.set('n', '<F5>', function()
98+
local filename = vim.fn.expand('%:r')
99+
vim.cmd('write')
100+
vim.cmd('belowright split | terminal gcc -Wall -Wextra -Werror % -o ' .. filename .. ' && ./' .. filename)
101+
end, { buffer = true, desc = 'Compile and run C file' })

ftplugin/c.lua

Lines changed: 0 additions & 63 deletions
This file was deleted.

0 commit comments

Comments
 (0)