diff --git a/home/user/.config/nvim/lua/base/mappings/run-scripts.lua b/home/user/.config/nvim/lua/base/mappings/run-scripts.lua index 62c6ebf..e36aab0 100644 --- a/home/user/.config/nvim/lua/base/mappings/run-scripts.lua +++ b/home/user/.config/nvim/lua/base/mappings/run-scripts.lua @@ -1,86 +1,32 @@ -local function autocmd(func) - local create_autocmd = vim.api.nvim_create_autocmd - create_autocmd("BufEnter", - { pattern = '*', callback = func} +local function map_filetype(filetype, key, cmd) + local function format_string() + local run_script_string = [[:tabnew %% | :terminal %s :set nocursorline number norelativenumber G]] + local cmd_string = string.format(run_script_string, cmd) + + local map = vim.keymap.set + local opts = { noremap = true, silent = true } + map('n', key, cmd_string, opts) + end + + + vim.api.nvim_create_autocmd("FileType", + { pattern = filetype, callback = format_string} ) end -local function set_keymap_base(key, cmd) - local map = vim.keymap.set - local opts = { noremap = true, silent = true } - local keymap_keys = string.format([[r%s]], key) - map("n", keymap_keys, cmd, opts) -end +-- map_filename('manpage', 'rr', 'man -P cat -l %') -- fix (by filenamej) -local function set_keymap_format_file(cmd) - local cmd_string = string.format([[:!%s %% ]], cmd) - set_keymap_base("f", cmd_string) -end +map_filetype('*', 'rs', '$(head -1 % | cut -c 3-) %') -local function set_keymap_run_script_base(key, cmd) - local run_script_string = [[:tabnew %% :terminal %s %% :set nocursorline number norelativenumber G ]] - local cmd_string = string.format(run_script_string, cmd) - set_keymap_base(key, cmd_string) -end +map_filetype('python', 'rr', 'python3 %') +map_filetype('python', 'rt', 'pytest %') -local function set_keymap_run_script(cmd) - set_keymap_run_script_base("r", cmd) -end +map_filetype('go', 'rr', 'go run') +-- map_filetype('go', 'rf', 'go fmt') -- fix (without open terminal) -local function set_keymap_test_file_base(key, cmd) - local run_script_string = [[:tabnew %% :terminal %s %% :set nocursorline number norelativenumber G ]] - local cmd_string = string.format(run_script_string, cmd) - set_keymap_base(key, cmd_string) -end +map_filetype('rust', 'rr', 'cargo run') +-- map_filetype('rust', 'rf', 'cargo fmt -p') -- fix (without open terminal) -local function set_keymap_test_file(cmd) - set_keymap_test_file_base("t", cmd) -end - -local function set_keymap_run_script_by_shebang() - set_keymap_run_script_base("s", [[$(head -1 % | cut -c 3-) %]]) -end - -local function create_function_autocmd_by_filetype(set_keymap_func, ft, cmd) - return function() - if vim.bo.filetype == ft then - set_keymap_func(cmd) - end - end -end - -local function create_function_autocmd_by_filename(set_keymap_func, fn, cmd) - return function() - if vim.fn.expand('%:t') == fn then - set_keymap_func(cmd) - end - end -end - -local function autocmd_run_script_by_filetype(ft, cmd) - autocmd(create_function_autocmd_by_filetype(set_keymap_run_script, ft, cmd)) -end - -local function autocmd_format_file_by_filetype(ft, cmd) - autocmd(create_function_autocmd_by_filetype(set_keymap_format_file, ft, cmd)) -end - -local function autocmd_run_tests_by_filetype(ft, cmd) - autocmd(create_function_autocmd_by_filetype(set_keymap_test_file, ft, cmd)) -end - - -autocmd_run_script_by_filetype('python', 'python3') -autocmd_run_script_by_filetype('go', 'go run') -autocmd_run_script_by_filetype('rust', 'cargo run') - -autocmd_run_tests_by_filetype('python', 'pytest') - -autocmd(create_function_autocmd_by_filename(set_keymap_run_script, 'manpage', 'man -P cat -l')) - -autocmd_format_file_by_filetype('rust', 'cargo fmt -p') -autocmd_format_file_by_filetype('go', 'go fmt') - -autocmd(set_keymap_run_script_by_shebang) +map_filetype('c', 'rr', 'gcc % && ./a.out') \ No newline at end of file