summaryrefslogtreecommitdiffstats
path: root/lua/core/auto.lua
blob: d833aa20a1a9f4effaca62134eb7b1abe0e1cb50 (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
local function auto(event, opts)
  a.nvim_create_autocmd(event, opts)
end

a.nvim_create_augroup('bufcheck', {clear = true})

auto('TextYankPost', { -- highlight yanks
  group = 'bufcheck',
  pattern = '*',
  callback = function()
    vim.highlight.on_yank{ timeout = 250 }
  end
})

auto('FileType', { -- start git messages in insert mode
  group = 'bufcheck',
  pattern = { 'gitcommit', 'gitrebase', },
  command = 'startinsert | 1'
})

auto('FileType', { -- show git cached diff
  group = 'bufcheck',
  pattern = { 'gitcommit', 'gitrebase', },
  callback = function()
    local function lower(a, b)
      if a > b then
        return b
      else
        return a
      end
    end

    local function tablelength(T)
      local count = 0
      for _ in pairs(T) do count = count + 1 end
      return count
    end

    local diff = io.popen('git diff --cached', "r")
    local diffl = {}
    for i in diff:lines() do
      table.insert(diffl, i)
    end
    diff:close()

    if next(diffl) == nil then
      vim.notify('No diff to show :(', vim.log.levels.INFO, {
        title = 'Neovim Config' })
      return
    end

    local buf = a.nvim_create_buf(true, false)
    a.nvim_buf_set_lines(buf, 0, -1, false, diffl)
    a.nvim_buf_set_name(buf, 'Git Commit Diff')
    a.nvim_buf_set_option(buf, 'ft', 'diff')
    a.nvim_buf_set_option(buf, 'readonly', true)
    a.nvim_buf_set_option(buf, 'modifiable', false)
    a.nvim_buf_set_option(buf, 'modified', false)
    local win = a.nvim_open_win(buf, false, {
      row = 1,
      col = 80,
      relative = 'win',
      width = 80,
      height = lower(vim.o.lines - vim.o.cmdheight - 4, tablelength(diffl)),
      border = 'shadow',
      style = 'minimal',
    })
  end,
})


auto('BufRead', { -- return to last place
  pattern = '*',
  command = [[call setpos(".", getpos("'\""))]]
})

auto('TermOpen', { -- start terminal in insert mode
  group    = 'bufcheck',
  pattern  = '*',
  callback = function()
    vim.cmd('startinsert | set winfixheight')
    o.winfixheight = true
    o.cmdheight = 0
  end
})

auto('TermClose', { -- close terminal buffers after shell dies
  group    = 'bufcheck',
  pattern  = 'term://*',
  callback = function()
    vim.cmd('call nvim_input("<CR>")')
    o.cmdheight = 1
  end
})

auto('InsertEnter', { -- toggle things when entering insert mode
  group = 'bufcheck',
  callback = function()
    o.colorcolumn = { 80 }
  end
})

auto('InsertLeave', { -- toggle things when exiting insert mode
  group = 'bufcheck',
  callback = function()
    o.colorcolumn = { 0 }
  end
})

auto('BufWritePre', { -- make dirs when they don't exist
  pattern = '*',
  group = vim.api.nvim_create_augroup('auto_create_dir', { clear = true }),
  callback = function(ctx)
    local dir = vim.fn.fnamemodify(ctx.file, ':p:h')
    vim.fn.mkdir(dir, 'p')
  end
})