Luigit
repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by admin

neovim/lua/bugabinga/options/behaviour.lua

Raw
local auto = require 'std.auto'
local ignored = require 'std.ignored'
local dbg = require 'std.dbg'

auto 'highlight_yanked_text' {
  description = 'briefly highlight yanked text',
  events = 'TextYankPost',
  pattern = '*',
  command = function()
    vim.hl.on_yank {
      higroup = 'TextYankPost',
      timeout = 169,
    }
  end,
}

auto 'disable_columns_in_special_buffers' {
  description = 'Hide columns in buffers, that do not show source code.',
  events = { 'FileType' },
  pattern = ignored.filetypes,
  command = function(data)
    vim.opt_local.colorcolumn = {}
    vim.opt_local.signcolumn = 'no'
    vim.opt_local.foldcolumn = '0'
    vim.opt_local.number = false
  end,
}

local restore_cursor_handler = function(ev)
	-- BufReadPost fires on every read of this buffer, not just the first one.
	-- Reloading a file that changed on disk ( `:checktime`, 'autoread' ) reads
	-- it again with READ_NEW and therefore triggers this too.
	-- Restoring the last known position is a once per buffer action, so latch
	-- it. Buffer local variables survive a reload, the buffer object stays the
	-- same and only its lines are replaced.
	if vim.b[ev.buf].cursor_restored then return end
	vim.b[ev.buf].cursor_restored = true

	if dbg.get() then
		vim.notify(string.format('RCP stage1: buf=%d cursor_line=%d mark_line=%d',
			ev.buf, vim.fn.line('.'), vim.fn.line([['"]])), vim.log.levels.DEBUG)
	end

	local ft = vim.bo[ev.buf].filetype
	local bt = vim.bo[ev.buf].buftype

	if ignored.buftypes_kv[bt] then return end
	if ignored.filetypes_kv[ft] then return end
	for _, pattern in ipairs(ignored.filetypes_patterns) do
		if ft:find(pattern) then return end
	end

	local cursor_line = vim.fn.line('.')
	local mark_line = vim.fn.line([['"]])
	if cursor_line == mark_line then return end

	local col = vim.fn.col([['"]]) - 1
	if mark_line > 0 and mark_line <= vim.fn.line('$') then
		if dbg.get() then
			vim.notify(string.format('RCP: creating stage2 for line=%d col=%d', mark_line, col), vim.log.levels.DEBUG)
		end
		auto 'restore_cursor_stage2' {
			events = 'BufWinEnter',
			buffer = ev.buf,
			once = true,
			command = function()
				if dbg.get() then
					vim.notify(string.format('RCP stage2: restoring to %d,%d', mark_line, col), vim.log.levels.DEBUG)
				end
				vim.api.nvim_win_set_cursor(0, { mark_line, col })
				vim.cmd.normal { 'zvzz', bang = true }
			end,
		}
	end
end

auto 'restore_cursor_position' {
	description = 'Restore cursor position when opening a file',
	events = 'BufReadPost',
	command = restore_cursor_handler,
}

auto 'handle_usr1_signal' {
	description = 'Reloads the current theme, when USR1 signal is received',
	events = { 'Signal' },
	pattern = 'SIGUSR1',
	command = function()
		dbg.print 'received signal USR1! Attempting to reload colorscheme.'
		vim.cmd.colorscheme 'nugu'
	end,
}

return {
	restore_cursor_handler = restore_cursor_handler,
}