summaryrefslogtreecommitdiffstats
path: root/lua/conf/autos.lua
blob: c00745bac6dab45aeae61c51c55773d002e17a38 (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
local misc = require("core.misc")
local auto, augroup = misc.auto, misc.augroup

-- auto commands which interact with bufferes without modifying them
local bufcheck = augroup("bufcheck")
-- auto commands which modify things on the filesystem
local fsmod = augroup("fsmod")

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

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

auto("BufRead", {
  group = bufcheck,
  desc = "Return to the last place the buffer was closed in.",
  callback = function()
    vim.fn.setpos(".", vim.fn.getpos("'\""))
    vim.cmd("norm! zz")
  end
})

auto("BufWritePre", {
  group = fsmod,
  desc = "remove trailing spaces on file save",
  callback = function()
    local pos = vim.api.nvim_win_get_cursor(0)
    vim.cmd([[%s/\s\+$//e]])
    vim.api.nvim_win_set_cursor(0, pos)
  end
})

auto("BufWritePre", {
  group = fsmod,
  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
})