summaryrefslogtreecommitdiff
path: root/.config/nvim/lua/core/autocmds.lua
blob: 898aeb747a896b37a3139b16272b1d71d38046e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
-- Core autocommands
-- Centralized place for all autocommands

--------------------------------
-- FILE DETECTION & RELOADING
--------------------------------

-- Auto-reload files when changed externally
local file_change_detect = vim.api.nvim_create_augroup("FileChangeDetect", { clear = true })
vim.api.nvim_create_autocmd({"FocusGained", "BufEnter", "BufWinEnter", "WinEnter", "CursorHold", "CursorHoldI"}, {
    group = file_change_detect,
    callback = function()
        vim.cmd("checktime")
    end,
})

vim.api.nvim_create_autocmd("FileChangedShellPost", {
    group = file_change_detect,
    callback = function()
        vim.notify("File changed on disk. Buffer reloaded.", vim.log.levels.WARN)
        vim.cmd("checktime")
    end,
})

--------------------------------
-- TERMINAL BEHAVIOR
--------------------------------

-- Terminal settings and keymaps
local terminal_settings = vim.api.nvim_create_augroup("TerminalSettings", { clear = true })
vim.api.nvim_create_autocmd({"TermOpen", "BufEnter"}, {
    group = terminal_settings,
    callback = function()
        if vim.opt.buftype:get() == "terminal" then
            vim.cmd("startinsert")
            
            -- Terminal keymaps
            local opts = { noremap = true, silent = true, buffer = true }
            vim.keymap.set("t", "<C-d>", [[<C-\><C-n><cmd>ToggleTerm<CR>]], opts)
            vim.keymap.set("t", "<C-q>", [[<C-\><C-n><cmd>ToggleTerm<CR>]], opts)
            vim.keymap.set("t", "jk", [[<C-\><C-n>]], opts)
            vim.keymap.set("t", "<C-h>", [[<C-\><C-n><C-W>h]], opts)
            vim.keymap.set("t", "<C-j>", [[<C-\><C-n><C-W>j]], opts)
            vim.keymap.set("t", "<C-k>", [[<C-\><C-n><C-W>k]], opts)
            vim.keymap.set("t", "<C-l>", [[<C-\><C-n><C-W>l]], opts)
        end
    end,
})

--------------------------------
-- CUSTOM FILETYPES
--------------------------------

-- Custom filetype detection
local filetype_group = vim.api.nvim_create_augroup("CustomFileTypes", { clear = true })

-- Set up .seq files to use codev syntax
vim.api.nvim_create_autocmd({"BufRead", "BufNewFile"}, {
    group = filetype_group,
    pattern = "*.seq",
    callback = function()
        vim.bo.filetype = "seq"
        vim.bo.syntax = "codev"
        vim.bo.commentstring = "!%s"
    end,
})

-- Set up .lis files to use codelis syntax
vim.api.nvim_create_autocmd({"BufRead", "BufNewFile"}, {
    group = filetype_group,
    pattern = "*.lis",
    callback = function()
        vim.bo.filetype = "lis"
        vim.bo.syntax = "codelis"
    end,
})

--------------------------------
-- CUSTOM COMMANDS
--------------------------------

-- Python docstring helper
vim.api.nvim_create_user_command("PythonDocstring", function()
    local docstring = [["""

# Information:

# Arguments:

# Returns:

"""]]
    -- Insert the docstring at the cursor position
    local line = vim.api.nvim_win_get_cursor(0)[1]
    vim.api.nvim_buf_set_lines(0, line-1, line-1, false, vim.split(docstring, "\n"))
    -- Position cursor at the right spot
    vim.api.nvim_win_set_cursor(0, {line+1, 0})
end, {})
Back to https://optics-design.com