summaryrefslogtreecommitdiff
path: root/lua/config/04-autocmds.lua
blob: aff6d2742256ac34835a566f826f0b47fc871ce7 (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
-- Autocommands migrated from the original init.vim.
-- Plugin-specific autocommands (LSP, windownav, etc.) stay in their modules.

local api = vim.api

local init_group = api.nvim_create_augroup('InitVim', { clear = true })

-- Turn search highlighting off when leaving Insert mode; mappings for n/N
-- in 01-mappings.lua re-enable highlighting when navigating results.
api.nvim_create_autocmd('InsertLeave', {
  group = init_group,
  callback = function()
    vim.opt.hlsearch = false
  end,
  desc = 'Clear search highlighting after insert mode',
})

-- Highlight matches whenever entering the / or ? search command line.
api.nvim_create_autocmd('CmdlineEnter', {
  group = init_group,
  pattern = { '/', '?' },
  callback = function()
    vim.opt.hlsearch = true
  end,
  desc = 'Enable search highlighting on search',
})

vim.api.nvim_create_autocmd('TextYankPost', {
  group = vim.api.nvim_create_augroup('HighlightYank', { clear = true }),
  callback = function()
    vim.hl.hl_op({ higroup = 'IncSearch', timeout = 100 })
  end,
})

vim.api.nvim_create_autocmd('TextPutPost', {
  group = vim.api.nvim_create_augroup('HighlightPut', { clear = true }),
  callback = function()
    vim.hl.hl_op({ higroup = 'IncSearch', timeout = 100 })
  end,
})

api.nvim_create_autocmd({ 'FocusGained', 'BufEnter' }, {
  group = init_group,
  command = 'checktime',
  desc = 'Check for changes to files on disk',
})

-- The original UseTerminal group only opened a terminal if the entered buffer
-- *itself* was named after a directory. Guard that explicitly: the new
-- open_terminal_on_directory() helper defaults to the file's parent directory,
-- which would otherwise open a terminal whenever you entered any file.
local terminal_group = api.nvim_create_augroup('UseTerminal', { clear = true })
api.nvim_create_autocmd({ 'BufNewFile', 'BufEnter' }, {
  group = terminal_group,
  callback = function(args)
    local dirname = api.nvim_buf_get_name(args.buf)
    if dirname ~= '' and vim.fn.isdirectory(dirname) == 1 then
      -- Defined by 03-commands.lua. Resolve at callback time so our
      -- numbered config loader may source this file independently.
      if type(_G.open_terminal_on_directory) == 'function' then
        _G.open_terminal_on_directory(dirname)
      end
    end
  end,
  desc = 'Open a terminal when editing a directory buffer',
})