summaryrefslogtreecommitdiffstats
path: root/lua/conf/auto.lua
blob: 0ab1a86d7cee9f955e3b687b6f494b8a28b49559 (plain) (blame)
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
local function auto(event, opts)
  a.nvim_create_autocmd(event, opts)
end

local function augroup(name, opts)
  opts = opts or {}
  opts['clear'] = true
  a.nvim_create_augroup(name, opts)
end

local winchange = augroup('winchange')
local bufcheck = augroup('bufcheck')
local toggles = augroup('toggles')

auto({ "FocusGained", "TermClose", "TermLeave" }, {
  group = bufcheck,
  desc = 'Update contents of file.',
  command = "checktime",
})

auto("VimResized", {
  group = winchange,
  desc = 'Resize splits when window is resized.',
  callback = function()
    local current_tab = vim.fn.tabpagenr()
    vim.cmd("tabdo wincmd =")
    vim.cmd("tabnext " .. current_tab)
  end,
})

auto('TextYankPost', {
  group = bufcheck,
  pattern = '*',
  desc = 'Highlight on yank.',
  callback = function()
    vim.highlight.on_yank{ timeout = 250 }
  end
})

auto('BufRead', {
  pattern = '*',
  group = bufcheck,
  desc = 'Return to the last place the buffer was closed in.',
  callback = function() vim.cmd([[call setpos(".", getpos("'\""))]]) end
})

auto('FileType', {
  pattern = { 'gitcommit', 'markdown' },
  desc = 'Spell checking and wrapping in commit buffers and markdown files.',
  callback = function()
    vim.opt_local.wrap = true
    vim.opt_local.spell = true
  end
})

auto('BufWritePre', {
  pattern = '*',
  group = bufcheck,
  desc = 'Basically mkdir -p.',
  callback = function(ctx)
    if ctx.match:match("^%w%w+://") then return end
    local dir = vim.fn.fnamemodify(ctx.file, ':p:h')
    vim.fn.mkdir(dir, 'p')
  end
})

auto('WinLeave', {
	desc = 'Unset cursorline',
  group = toggles,
	callback = function() vim.opt.cursorline = false end
})

auto('WinEnter', {
	desc = 'Set cursorline',
  group = toggles,
	callback = function()
    if vim.bo.filetype ~= 'alpha' then
      vim.opt.cursorline = true
    end
  end
})

auto('ColorScheme', {
  desc = 'Update statusline on colorscheme change',
  group = winchange,
  callback = function() require('el').reset_windows() end
})