aboutsummaryrefslogtreecommitdiffstats
path: root/plugin/50-ftdetect.lua
blob: bd7529352724658673191bfa81a798488b623673 (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
--[[ 50-ftdetect.lua — set 'filetype' for cases the built-in detection misses.

Detects bash scripts (a `.bash` extension or a `#!/usr/bin/bash` shebang) and the `claude` CLI
running in a terminal (matched by the command in the `term://<cwd>//<pid>:<command>` buffer name),
the latter tagged `claude-cli` so its terminal can carry Claude-specific ftplugin settings.
]]

-- Set filetype `bash` for a `.bash` file or one with a `#!/usr/bin/bash` shebang.
local function detect_bash()
	if
		string.find(vim.api.nvim_buf_get_name(0), "%.bash$")
		or string.find(vim.api.nvim_buf_get_lines(0, 0, 1, false)[1] or "", "^#!/usr/bin/bash")
	then
		vim.bo.filetype = "bash"
	end
end

--[[ Set filetype `claude-cli` on a terminal buffer running the `claude` CLI.
Detected from the command in the terminal buffer name (`term://<cwd>//<pid>:<command>`) when its
executable's basename is `claude`. ]]
local function detect_claude_cli(args)
	local cmd = vim.api.nvim_buf_get_name(args.buf):match("//%d+:(.*)$")
	local exe = cmd and cmd:match("^%S+")
	if exe and vim.fn.fnamemodify(exe, ":t") == "claude" then
		vim.bo[args.buf].filetype = "claude-cli"
	end
end

vim.api.nvim_create_autocmd({ "BufRead", "BufNewFile" }, {
	desc = "Detect a bash file",
	group = vim.g.dotfiles_augroup,
	pattern = "*",
	callback = detect_bash,
})
vim.api.nvim_create_autocmd("TermOpen", {
	desc = "Detect the Claude CLI terminal",
	group = vim.g.dotfiles_augroup,
	callback = detect_claude_cli,
})