all repos — dot @ master

Shell, editor and terminal configuration, and the tools that go with them

config/nvim/init.lua (view raw)

 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
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
------------------------------------------------------
---- Runtime -----------------------------------------
------------------------------------------------------

-- Ensure XDG environment variables are set
vim.env.XDG_CONFIG_HOME = vim.env.XDG_CONFIG_HOME or vim.env.HOME .. "/.config"
vim.env.XDG_CACHE_HOME = vim.env.XDG_CACHE_HOME or vim.env.HOME .. "/.cache"
vim.env.XDG_DATA_HOME = vim.env.XDG_DATA_HOME or vim.env.HOME .. "/.local/share"

-- Ensure the Neovim cache directory exists
local nvim_cache_dir = vim.env.XDG_CACHE_HOME .. "/nvim"
if vim.fn.isdirectory(nvim_cache_dir) == 0 then
	vim.fn.mkdir(nvim_cache_dir, "p")
end

-- Ensure necessary paths are in $PATH
local paths = { "/usr/local/bin", "/opt/brew/bin", "/opt/homebrew/bin" }
for _, path in ipairs(paths) do
	if not string.match(vim.env.PATH, path) then
		vim.env.PATH = path .. ":" .. vim.env.PATH
	end
end

-- Add node_modules/.bin/ to PATH for Node.js projects
if not string.match(vim.env.PATH, "node_modules/%.bin") then
	vim.env.PATH = vim.env.PATH .. ":node_modules/.bin/"
end

-- Speed up startup by explicitly setting the Python interpreter
local python_paths = {
	"/opt/brew/bin/python3",
	"/opt/homebrew/bin/python3",
	"/usr/local/bin/python3",
	"/usr/bin/python3",
}
for _, path in ipairs(python_paths) do
	if vim.fn.executable(path) == 1 then
		vim.g.python3_host_prog = path
		break
	end
end

-- Disable unused language providers (no Ruby/Perl remote plugins here).
-- Skips provider probing at startup and quiets :checkhealth.
vim.g.loaded_ruby_provider = 0
vim.g.loaded_perl_provider = 0

------------------------------------------------------
---- Basics ------------------------------------------
------------------------------------------------------

-- colorscheme is set from
-- ~/.local/share/nvim/colors.lua

-- Set leader key
vim.g.mapleader = " "

-- Sensible defaults
vim.opt.encoding = "utf-8"
vim.opt.mouse = "a" -- Use mouse in terminal
vim.opt.showcmd = true
vim.opt.ruler = true -- Show cursor position
vim.opt.number = true -- Show line numbers
-- vim.opt.cursorline = true -- Highlight current line
vim.opt.showmatch = true -- Highlight matching brackets/braces
vim.opt.matchtime = 5 -- Duration to show matching brace (1/10 sec)
-- vim.opt.shortmess:append("I") -- Disable intro message / splash screen
vim.opt.splitbelow = true -- New horizontal splits go below
vim.opt.splitright = true -- New vertical splits go right
vim.opt.backspace = { "indent", "eol", "start" } -- Backspace works across lines
vim.opt.hidden = true -- Allow changing buffers without saving
vim.opt.errorbells = false
vim.opt.autoread = true -- Auto read when file changes elsewhere
vim.opt.autowrite = true -- Auto write before :next, :make, etc.
vim.opt.cmdheight = 2 -- More space for messages
vim.opt.scrolloff = 5 -- Start scrolling when within 5 lines near top/bottom
vim.opt.laststatus = 2 -- Always display the status line
vim.opt.termguicolors = true -- Use 24-bit colors in the terminal
vim.opt.updatetime = 200 -- Snappier CursorHold + gitsigns (default 4000)
vim.opt.timeoutlen = 300 -- Faster which-key / mapped-sequence timeout (default 1000)
vim.o.winborder = "rounded" -- Consistent border on all floating windows (0.11+)
vim.opt.splitkeep = "screen" -- Keep text visually stable when opening/closing splits
-- Unified sign/number/fold/git gutter (snacks.statuscolumn, enabled in ui.lua)
vim.o.statuscolumn = [[%!v:lua.require'snacks.statuscolumn'.get()]]

-- Bash-like autocomplete for commands
vim.opt.wildmode = { "list:longest", "list:full" } -- First tab completes longest, second cycles
vim.opt.wildmenu = true

-- Indentation
vim.opt.expandtab = true
vim.opt.tabstop = 4
vim.opt.softtabstop = 4
vim.opt.shiftwidth = 4
vim.opt.autoindent = true

-- Invisibles
vim.opt.listchars = { tab = "▸ ", eol = "¬", nbsp = "☠", trail = "⍁" }
vim.opt.list = false

-- Wrapping
vim.opt.wrap = true
vim.opt.linebreak = true
vim.opt.showbreak = "…"
vim.opt.formatoptions:remove("t")
-- vim.opt.textwidth = 80
-- vim.opt.colorcolumn = "80"

-- Autocomplete
vim.opt.completeopt = { "longest", "preview", "menu" }
vim.opt.dictionary:remove("/usr/share/dict/words")
vim.opt.dictionary:append("/usr/share/dict/words")
vim.opt.thesaurus:remove("/usr/share/dict/mthesaur")
vim.opt.thesaurus:append("/usr/share/dict/mthesaur")

-- Searching
vim.opt.ignorecase = true
vim.opt.smartcase = true
vim.opt.incsearch = true
vim.opt.hlsearch = true

-- Folding
vim.opt.foldmethod = "expr"
vim.opt.foldexpr = "v:lua.vim.treesitter.foldexpr()"
vim.opt.foldtext = "v:lua.vim.treesitter.foldtext()"
vim.opt.foldnestmax = 4
vim.opt.foldminlines = 3
vim.opt.foldlevel = 5
vim.opt.foldtext =
	[[substitute(getline(v:foldstart),'\\t',repeat('\ ',&tabstop),'g').'…'.trim(getline(v:foldend)) . ' (#' . (v:foldend - v:foldstart + 1) . ' lines)']]
vim.opt.fillchars = "fold: "

-- Swap and Undo Directories
local swapdir = os.getenv("XDG_CACHE_HOME") .. "/nvim/swap//"
if vim.fn.isdirectory(swapdir) == 0 then
	vim.fn.mkdir(swapdir, "p")
end
vim.opt.swapfile = true
vim.opt.directory = swapdir

local undodir = os.getenv("XDG_CACHE_HOME") .. "/nvim/undo//"
if vim.fn.isdirectory(undodir) == 0 then
	vim.fn.mkdir(undodir, "p")
end
vim.opt.undofile = true
vim.opt.undodir = undodir

-- File Search Path
vim.opt.path = ".,,**"

-- Disable netrw, we're using nvim-tree instead
vim.g.loaded_netrw = 1
vim.g.loaded_netrwPlugin = 1

-- Set SHELL command
if vim.fn.executable("/usr/local/bin/zsh") == 1 then
	vim.opt.shell = "/usr/local/bin/zsh -l"
elseif vim.fn.executable("/usr/bin/zsh") == 1 then
	vim.opt.shell = "/usr/bin/zsh -l"
end

-- Terminal settings
vim.g.terminal_scrollback_buffer_size = 10000 -- Store reasonable amount of terminal history

-- Built-in :grep command
if vim.fn.executable("rg") == 1 then
	vim.opt.grepprg = "rg --vimgrep --smart-case --no-heading"
	vim.opt.grepformat = "%f:%l:%c:%m,%f:%l:%m"
elseif vim.fn.executable("ag") == 1 then
	vim.opt.grepprg = "ag --vimgrep --smart-case --noheading"
	vim.opt.grepformat = "%f:%l:%c:%m,%f:%l:%m" -- this has not been tested
end

------------------------------------------------------
---- Neovide (GUI) -----------------------------------
------------------------------------------------------

if vim.g.neovide then
	-- Put anything you want to happen only in Neovide here
	-- vim.o.guifont = "LiterationMono Nerd Font Mono:14"
	-- vim.o.guifont = "SauceCodePro NF:h16"
	vim.o.guifont = "FantasqueSansM Nerd Font Mono:h16"
	-- vim.o.guifont = "Agave Nerd Font Mono:h16"
	-- vim.o.guifont = "SFMono Nerd Font:h16"
	-- vim.o.guifont = "FiraCode Nerd Font:h16"

	vim.g.neovide_opacity = 0.8
	vim.g.transparency = 1

	vim.g.neovide_scale_factor = 1.0
	local change_scale_factor = function(delta)
		vim.g.neovide_scale_factor = vim.g.neovide_scale_factor * delta
	end
	vim.keymap.set("n", "<C-=>", function()
		change_scale_factor(1.05)
	end)
	vim.keymap.set("n", "<C-->", function()
		change_scale_factor(1 / 1.05)
	end)
	vim.keymap.set("n", "<C-0>", function()
		change_scale_factor(1)
	end)

	vim.g.neovide_window_blurred = true
	vim.g.neovide_show_border = false
	vim.g.neovide_input_macos_option_key_is_meta = "only_left"

	-- vim.g.neovide_cursor_animation_length = 0.150
	vim.g.neovide_cursor_animation_length = 0.025
	vim.g.neovide_cursor_short_animation_length = 0.5
	vim.g.neovide_cursor_trail_size = 0.2
	vim.g.neovide_cursor_vfx_mode = "pixiedust"

	vim.keymap.set("n", "<D-n>", function()
		vim.fn.jobstart("neovide --fork", { detach = true })
	end) -- Open new instance
	vim.keymap.set("n", "<D-s>", ":w<CR>") -- Save
	vim.keymap.set("v", "<D-c>", '"+y') -- Copy
	vim.keymap.set("n", "<D-v>", '"+P') -- Paste normal mode
	vim.keymap.set("v", "<D-v>", '"+P') -- Paste visual mode
	vim.keymap.set("c", "<D-v>", "<C-R>+") -- Paste command mode
	vim.keymap.set("i", "<D-v>", "<C-R>+") -- Paste insert mode

	local dirs = { "/Volumes/Sense/src", "~/src" }
	for _, dir in ipairs(dirs) do
		if vim.fn.isdirectory(dir) == 1 then
			vim.cmd("cd " .. dir)
			break
		end
	end
end

------------------------------------------------------
---- lazy.nvim ---------------------------------------
------------------------------------------------------

-- this will load ~/.config/nvim/lua/config/lazy.lua
-- Guarded so re-sourcing init.lua (via <Leader><) does not call lazy.setup()
-- again; only the options/keymaps/autocmds below get re-applied.
if not vim.g.lazy_bootstrapped then
	vim.g.lazy_bootstrapped = true
	require("config.lazy")
end

------------------------------------------------------
---- Caps Lock key -----------------------------------
------------------------------------------------------

-- Caps Lock sends F20 (hammerspoon), which reaches Neovim as F22 through tmux.
-- Neovim types the name of a function key nothing maps, so holding it filled
-- the buffer with <F22>. Wispr Flow reads the key before the terminal does, so
-- nothing is lost by dropping it here. Terminal mode too, or holding it in a
-- :terminal would send the key on to the shell running there.
for _, key in ipairs({ "<F20>", "<F22>" }) do
	vim.keymap.set({ "n", "x", "s", "o", "i", "c", "t" }, key, "<Nop>")
end

------------------------------------------------------
---- Terminal ----------------------------------------
------------------------------------------------------

-- Terminal lives under <Leader>; so <Leader>t is free for tests (neotest)
vim.keymap.set("n", "<Leader>;v", ":vsplit term://$SHELL<CR>i", { desc = "Terminal in vertical split" })
vim.keymap.set("n", "<Leader>;s", ":belowright split term://$SHELL<CR>i", { desc = "Terminal in horizontal split" })
-- Toggleable, reusable terminal (snacks) — hide/show without re-spawning
vim.keymap.set("n", "<Leader>;t", function()
	Snacks.terminal.toggle()
end, { desc = "Toggle terminal" })
vim.keymap.set("t", "<Esc>", "<C-\\><C-n>", { desc = "Exit terminal mode" })

------------------------------------------------------
---- Running the current file ------------------------
------------------------------------------------------

-- Run a command in a terminal, optionally in a split: split_mode = "v" | "h" | nil
local function run_cur(cmd, split_mode)
	if split_mode == "v" then
		vim.cmd("vsplit")
	elseif split_mode == "h" then
		vim.cmd("split")
	end
	vim.cmd("terminal " .. cmd)
end

-- Determine the interpreter and run the current file
local function run_cur_self(split_mode)
	vim.cmd("write") -- Save the file

	local filetype = vim.bo.filetype
	local interpreter

	if vim.fn.filereadable("run.sh") == 1 then
		interpreter = "bash run.sh"
	elseif vim.fn.filereadable("sh/run.sh") == 1 then
		interpreter = "bash sh/run.sh"
	elseif filetype == "sh" then
		interpreter = "bash"
	elseif filetype:match("javascript") then
		interpreter = "node"
	elseif filetype:match("scala") then
		interpreter = "scala"
	elseif filetype == "python" then
		interpreter = "pyexec"
	elseif filetype:match("lua") then
		interpreter = "lua"
	elseif filetype == "make" then
		run_cur("make", split_mode)
	-- or, if the file is executable, run it directly
	elseif vim.fn.executable(vim.fn.expand("%")) == 1 then
		interpreter = ""
	else
		print("No interpreter set for filetype=" .. filetype)
		return
	end

	-- Use absolute path and shell-escape the filename
	local file = vim.fn.shellescape(vim.fn.expand("%:p"))
	run_cur(interpreter .. " " .. file, split_mode)
end

vim.keymap.set("n", "<Leader>nn", function()
	run_cur_self(nil)
end, { desc = "Run current file" })
vim.keymap.set("n", "<Leader>nv", function()
	run_cur_self("v")
end, { desc = "Run current file (vertical split)" })
vim.keymap.set("n", "<Leader>ns", function()
	run_cur_self("h")
end, { desc = "Run current file (horizontal split)" })

------------------------------------------------------
---- Diagnostics -------------------------------------
------------------------------------------------------

vim.diagnostic.config({
	virtual_lines = { current_line = true }, -- full diagnostic inline, cursor line only
	virtual_text = false,
	signs = false,
	underline = true,
	update_in_insert = false,
	severity_sort = true,
	float = {
		border = "rounded",
		source = true, -- always show the source (was the deprecated "always")
		header = "",
		prefix = "",
	},
})
local function mapDiagnosticsToggle(key, diagnostic_option)
	vim.keymap.set("n", key, function()
		local current_config = vim.diagnostic.config() or {}
		local new_config = not current_config[diagnostic_option]
		vim.diagnostic.config({ [diagnostic_option] = new_config })
	end, { desc = "Toggle diagnostic " .. diagnostic_option })
end
mapDiagnosticsToggle("<Leader>dl", "virtual_lines")
mapDiagnosticsToggle("<Leader>dt", "virtual_text")
mapDiagnosticsToggle("<Leader>ds", "signs")
mapDiagnosticsToggle("<Leader>du", "underline")
vim.keymap.set("n", "<Leader>dd", function()
	vim.diagnostic.enable(not vim.diagnostic.is_enabled())
end, { desc = "Toggle diagnostics" })
vim.keymap.set("n", "<Leader>dq", function()
	vim.diagnostic.setqflist()
end, { desc = "Show all diagnostics in quickfix" })
vim.keymap.set("n", "<Leader>dh", function()
	vim.lsp.inlay_hint.enable(not vim.lsp.inlay_hint.is_enabled({}))
end, { desc = "Toggle inlay hints" })

------------------------------------------------------
---- Productivity ------------------------------------
------------------------------------------------------

vim.keymap.set("n", "<Leader>,", ":e $MYVIMRC<CR>", { noremap = true, silent = true })
vim.keymap.set("n", "<Leader><", function()
	-- Re-applies options/keymaps/autocmds. The lazy bootstrap is guarded above,
	-- so this does not re-run lazy.setup(). For plugin changes use :Lazy reload
	-- <plugin> or restart.
	dofile(vim.env.MYVIMRC)
	vim.notify("Core config reloaded", vim.log.levels.INFO)
end, { desc = "Reload core config (options/keymaps/autocmds)" })
vim.keymap.set("n", "<Leader>w", ":update<CR>", { desc = "Save file if modified" })
vim.keymap.set("n", "<Leader>W", ":write<CR>", { desc = "Save file" })
vim.keymap.set("n", "<Leader>\\", ":nohl<CR>", { desc = "Clear search highlights" })

-- Quickfix navigation
vim.keymap.set("n", "]q", ":cnext<CR>", { desc = "Quickfix next" })
vim.keymap.set("n", "[q", ":cprevious<CR>", { desc = "Quickfix previous" })
vim.keymap.set("n", "[[", function()
	local qf_exists = false
	for _, win in pairs(vim.fn.getwininfo()) do
		if win["quickfix"] == 1 then
			qf_exists = true
			break
		end
	end
	if qf_exists then
		vim.cmd("cclose")
	else
		vim.cmd("copen")
	end
end, { desc = "Quickfix toggle" })
vim.keymap.set("n", "[]", ":copen<CR>", { desc = "Quickfix open" })
vim.keymap.set("n", "][", ":cclose<CR>", { desc = "Quickfix close" })

-- Location list navigation
vim.keymap.set("n", "]l", ":lnext<CR>", { noremap = true, silent = true, desc = "Location list next" })
vim.keymap.set("n", "[l", ":lprevious<CR>", { noremap = true, silent = true, desc = "Location list previous" })

-- Common typing mistakes
vim.cmd([[cnoreabbrev E e]])
vim.cmd([[inoreabbrev requrie require]])
vim.cmd([[inoreabbrev lenght length]])

-- Copy current filename with line number
vim.keymap.set(
	"n",
	"<Leader><Leader>l",
	":let @+ = expand('%') . ':' . line('.')<CR>",
	{ noremap = true, silent = true }
)

-- %% expands to dirname of current file
vim.cmd([[cnoremap <expr> %% getcmdtype() == ':' ? expand('%:p:h').'/' : '%%']])

-- Redraw screen
vim.keymap.set("n", "<C-n>", "<C-l>", { noremap = true, silent = true })

-- Disable distracting features in current buffer
vim.keymap.set("n", "<Leader>C", function()
	-- Disable gitsigns
	require("gitsigns").setup({ signcolumn = false })

	-- Disable snacks.indent for current buffer
	vim.b.snacks_indent = false

	-- Disable line numbers and listchars
	vim.opt_local.number = false
	vim.opt_local.list = false

	vim.opt_local.showtabline = 0 -- Hide bufferline
	-- disable diagnostics

	vim.cmd("nohlsearch") -- cclear search highlight
	vim.diagnostic.enable(false)

	vim.opt_local.cursorline = false -- Disable cursorline

	require("lualine").hide()

	vim.notify("Disabled distractions in current buffer", vim.log.levels.INFO)
end, { desc = "Disable distractions" })

------------------------------------------------------
---- Autocommands ------------------------------------
------------------------------------------------------

local api = vim.api

-- Primary autocmd group. clear=true means re-sourcing the config replaces these
-- handlers instead of stacking duplicates.
local szAutoGroup = api.nvim_create_augroup("szAutoGroup", { clear = true })

-- Trigger autoread when files change on disk
api.nvim_create_autocmd({ "FocusGained", "BufEnter", "CursorHold", "CursorHoldI" }, {
	group = szAutoGroup,
	pattern = "*",
	command = "if mode() !~ '\\v(c|r.?|!|t)' && getcmdwintype() == '' | checktime | endif",
})
-- Notification after file change
api.nvim_create_autocmd("FileChangedShellPost", {
	group = szAutoGroup,
	pattern = "*",
	callback = function()
		vim.notify("File changed on disk. Buffer reloaded.", vim.log.levels.WARN)
	end,
})

-- Terminal buffers: no line numbers, soft wrap
api.nvim_create_autocmd("TermOpen", {
	group = szAutoGroup,
	pattern = "*",
	callback = function()
		vim.opt_local.number = false
		vim.opt_local.relativenumber = false
		vim.opt_local.wrap = true
	end,
})

-- Project-specific keybindings
local function project_has_file(filename)
	return vim.fn.filereadable(vim.fn.getcwd() .. "/" .. filename) == 1
end

local function setup_project_keybindings()
	-- Clear any existing project keybindings
	pcall(vim.keymap.del, "n", "<Leader>cT")

	-- TypeScript compilation: <Leader>cT (requires tsconfig.json)
	if project_has_file("tsconfig.json") then
		vim.keymap.set("n", "<Leader>cT", function()
			vim.opt.makeprg = "npx tsc --noEmit"
			vim.opt.errorformat = "%f(%l,%c): %trror TS%n: %m,%f(%l,%c): %tarning TS%n: %m"
			vim.cmd("make!")
			vim.cmd("copen")
		end, { desc = "TypeScript compile to quickfix" })
	end
end

api.nvim_create_autocmd("VimEnter", {
	group = szAutoGroup,
	callback = setup_project_keybindings,
})
api.nvim_create_autocmd("DirChanged", {
	group = szAutoGroup,
	callback = setup_project_keybindings,
})

-- Filetype detection & settings
api.nvim_create_autocmd({ "BufRead", "BufNewFile", "BufEnter" }, {
	group = szAutoGroup,
	pattern = "*/.kube/config",
	command = "setlocal nowrap filetype=yaml",
})
api.nvim_create_autocmd({ "BufRead", "BufNewFile", "BufEnter" }, {
	group = szAutoGroup,
	pattern = "*gitconfig*,*git/config*",
	command = "setlocal filetype=gitconfig",
})
api.nvim_create_autocmd("FileType", {
	group = szAutoGroup,
	pattern = "matlab",
	command = "setlocal commentstring=%\\ %s",
})
api.nvim_create_autocmd("FileType", {
	group = szAutoGroup,
	pattern = "tf",
	command = "setlocal commentstring=#\\ %s",
})
api.nvim_create_autocmd("FileType", {
	group = szAutoGroup,
	pattern = "tla",
	command = "setlocal commentstring=\\\\*\\ %s",
})
api.nvim_create_autocmd("FileType", {
	group = szAutoGroup,
	pattern = "sql",
	command = "setlocal commentstring=--\\ %s",
})
api.nvim_create_autocmd({ "BufRead", "BufNewFile" }, {
	group = szAutoGroup,
	pattern = "*Dockerfile*",
	command = "set filetype=Dockerfile",
})
api.nvim_create_autocmd({ "BufRead", "BufNewFile" }, {
	group = szAutoGroup,
	pattern = "*Jenkinsfile*",
	command = "set filetype=groovy",
})
api.nvim_create_autocmd({ "BufRead", "BufNewFile" }, {
	group = szAutoGroup,
	pattern = "*.keymap",
	command = "set filetype=dts | setlocal commentstring=//\\ %s",
})
api.nvim_create_autocmd({ "BufRead", "BufNewFile" }, {
	group = szAutoGroup,
	pattern = "*.mjml",
	command = "set filetype=html",
})

-- Allow comments in JSON files
api.nvim_create_autocmd("FileType", {
	group = szAutoGroup,
	pattern = "json",
	command = [[syntax match Comment +\/\/.\+$+]],
})

-- Insert console.log() in JS/TS files
api.nvim_create_autocmd("FileType", {
	group = szAutoGroup,
	pattern = { "javascript", "typescript", "javascriptreact", "typescriptreact" },
	command = "inoremap <C-l> console.log()<Esc>i",
})

-- Restore cursor to file position from the previous editing session
api.nvim_create_autocmd("BufReadPost", {
	group = szAutoGroup,
	callback = function(args)
		local mark = api.nvim_buf_get_mark(args.buf, '"')
		local line_count = api.nvim_buf_line_count(args.buf)
		if mark[1] > 0 and mark[1] <= line_count then
			api.nvim_win_set_cursor(0, mark)
			-- defer centering slightly so it's applied after render
			vim.schedule(function()
				vim.cmd("normal! zz")
			end)
		end
	end,
})

-- QuickFix window behavior
api.nvim_create_autocmd("QuickFixCmdPost", {
	group = szAutoGroup,
	pattern = "*grep*",
	command = "cwindow",
})

-- Only allow a single tab when starting Neovim
api.nvim_create_autocmd("VimEnter", {
	group = szAutoGroup,
	pattern = "*",
	command = "silent! tabonly",
})

-- Open help in a vertical split
api.nvim_create_autocmd("FileType", {
	group = szAutoGroup,
	pattern = "help",
	command = "wincmd L",
})

-- Auto resize splits when the terminal window is resized
api.nvim_create_autocmd("VimResized", {
	group = szAutoGroup,
	command = "wincmd =",
})

-- Highlight yanked text
api.nvim_create_autocmd("TextYankPost", {
	group = api.nvim_create_augroup("highlight_yank", { clear = true }),
	pattern = "*",
	desc = "highlight selection on yank",
	callback = function()
		vim.hl.on_yank({ timeout = 200, visual = true })
	end,
})

-- Swapfile recovery
api.nvim_create_autocmd("SwapExists", {
	group = szAutoGroup,
	pattern = "*",
	command = "let b:swapchoice = '?' | let b:swapname = v:swapname",
})
api.nvim_create_autocmd("BufReadPost", {
	group = szAutoGroup,
	pattern = "*",
	command = "let b:swapchoice_likely = (&l:ro ? 'o' : 'r')",
})
api.nvim_create_autocmd("BufEnter", {
	group = szAutoGroup,
	pattern = "*",
	command = "let b:swapchoice_likely = (&l:ro ? 'o' : 'e')",
})
api.nvim_create_autocmd("BufWinEnter", {
	group = szAutoGroup,
	pattern = "*",
	command = [[
        if exists('b:swapchoice') && exists('b:swapchoice_likely') |
            let b:swapchoice = b:swapchoice_likely |
            unlet b:swapchoice_likely |
        endif
    ]],
})

-- Open a diff against the original file version
local function diff_orig()
	vim.cmd("vert new")
	vim.opt_local.buftype = "nofile"
	vim.cmd("r #")
	vim.cmd("0d_")
	vim.cmd("diffthis")
	vim.cmd("wincmd p")
	vim.cmd("diffthis")
end
-- Handle swap file recovery
local function handle_recover()
	vim.fn.system(
		"diff - " .. vim.fn.shellescape(vim.fn.expand("%:p")),
		table.concat(vim.fn.getline(1, "$") --[[@as string[] ]], "\n") .. "\n"
	)
	if vim.v.shell_error ~= 0 then
		diff_orig()
	else
		vim.fn.delete(vim.b.swapname)
		vim.notify("No differences; deleting the old swap file.", vim.log.levels.WARN)
		api.nvim_feedkeys(" ", "n", false)
	end
end
api.nvim_create_autocmd("BufWinEnter", {
	group = szAutoGroup,
	pattern = "*",
	callback = function()
		if vim.b.swapchoice == "r" then
			handle_recover()
		end
	end,
})

-- Show cursorline only in the active window
local active_cursorline = api.nvim_create_augroup("active_cursorline", { clear = true })
api.nvim_create_autocmd({ "WinEnter", "BufEnter" }, {
	group = active_cursorline,
	callback = function()
		vim.opt_local.cursorline = true
	end,
})
api.nvim_create_autocmd({ "WinLeave", "BufLeave" }, {
	group = active_cursorline,
	callback = function()
		vim.opt_local.cursorline = false
	end,
})

-- LSP reference highlighting under the cursor is handled by snacks.words
-- (configured in lua/plugins/ui.lua), which uses CursorHold rather than firing
-- an LSP request on every cursor move.