aboutsummaryrefslogtreecommitdiffhomepage
path: root/.config/nvim/lua/init/03-autocmds.lua
blob: b8e53ec94d169d938b98eeac8deb824c57c06d2c (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
local F = vim.fn
local O = vim.o
local vimrc = require('vimrc')
local my_env = require('vimrc.my_env')
local A = vimrc.autocmd


vimrc.create_augroup_for_vimrc()


-- Auto-resize windows when Vim is resized.
A('VimResized', {
   command = 'wincmd =',
})


-- Calculate 'numberwidth' to fit file size.
-- Note: extra 2 is the room of left and right spaces.
A({'BufEnter', 'WinEnter', 'BufWinEnter'}, {
   callback = function()
      vim.wo.numberwidth = #tostring(F.line('$')) + 2
   end,
})


-- Jump to the last cursor position when you open a file.
A('BufRead', {
   callback = function()
      if 0 < F.line("'\"") and F.line("'\"") <= F.line('$') and
         not O.filetype:match('commit') and not O.filetype:match('rebase')
      then
         vim.cmd('normal g`"')
      end
   end,
})


-- Lua version of https://github.com/mopp/autodirmake.vim
-- License: NYSL
A('BufWritePre', {
   callback = function()
      -- Don't create directory for ephemeral buffers.
      -- See :help 'bufhidden'
      local bufhidden = vim.bo.bufhidden
      if bufhidden == 'unload' or bufhidden == 'delete' or bufhidden == 'wipe' then
         return
      end

      local dir = F.expand('<afile>:p:h')
      if F.isdirectory(dir) ~= 0 then
         return
      end
      vimrc.echo(('"%s" does not exist. Create? [y/N] '):format(dir), 'Question')
      local answer = vimrc.getchar()
      if answer ~= 'y' and answer ~= 'Y' then
         return
      end
      F.mkdir(dir, 'p')
   end,
})


A('BufEnter', {
   desc = 'Set up *scratch* buffer',
   pattern = my_env.scratch_dir .. '/*/*.*',
   callback = function()
      vim.b._scratch_ = true
      if F.exists(':AutosaveEnable') == 2 then
         vim.cmd('AutosaveEnable')
      end
   end,
})


vimrc.register_filetype_autocmds_for_indentation()

-- TODO: move this elsewhere
vim.filetype.add({
   extension = {
      dj = 'djot',
      saty = 'satysfi',
      satyg = 'satysfi',
      satyh = 'satysfi',
   },
   filename = {
      ['.clinerules'] = 'markdown',
   },
})

-- TODO: move this elsewhere
vim.diagnostic.config({
   virtual_text = true,
   virtual_lines = {
      current_line = true,
   },
})