aboutsummaryrefslogtreecommitdiffstats
path: root/after/ftplugin/markdown.lua
blob: 160b6cc3599f64d98041f2172c1018a2b08c4130 (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
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
local function format()
	local view = vim.fn.winsaveview()
	local buf_str = table.concat(vim.api.nvim_buf_get_lines(0, 0, -1, false), "\n") .. "\n"
	local tempname = vim.fn.tempname()
	local tempfile = assert(io.open(tempname, "w"), "Could not open temporary file")
	tempfile:write(buf_str)
	tempfile:close()

	local r_check = vim.system({
		"mdformat",
		"--number",
		"--extensions",
		"tables",
		"--extensions",
		"frontmatter",
		"--wrap",
		tostring(vim.bo.textwidth),
		"--check",
		tempname,
	}):wait()
	if r_check.code == 0 then
		return
	elseif r_check.code ~= 1 then
		vim.notify("mdformat failed (" .. r_check.code .. "):\n" .. r_check.stderr)
	end
	local r_format = vim.system({
		"mdformat",
		"--number",
		"--extensions",
		"tables",
		"--extensions",
		"frontmatter",
		"--wrap",
		tostring(vim.bo.textwidth),
		tempname,
	}):wait()
	if r_format.code ~= 0 then
		vim.notify("mdformat failed (" .. r_format.code .. "):\n" .. r_format.stderr)
	end
	local formatted_lines = {}
	for line in io.lines(tempname) do
		formatted_lines[#formatted_lines + 1] = line
	end
	os.remove(tempname)
	vim.api.nvim_buf_set_lines(0, 0, -1, false, formatted_lines)

	vim.fn.winrestview(view)
end

vim.opt_local.tabstop = 2 -- CommonMark expects two spaces for indentation
vim.opt_local.shiftwidth = 0
vim.opt_local.softtabstop = -1
vim.opt_local.expandtab = true -- Change tabs to spaces
-- Nothing in the gutter, except sign columns if necessary
vim.opt_local.number = false
vim.opt_local.relativenumber = false
vim.opt_local.signcolumn = "auto"
vim.opt_local.foldcolumn = "0"
--
vim.opt_local.textwidth = 120 -- gw wraps at this value
vim.opt_local.foldlevel = 1
-- Replace concealed text with a character from `'listchars'`
-- With `'foldmethod'` set to `expr` and `'foldlevel'` set to 2 fenced code blocks are concealed and completely invisible
vim.opt_local.complete = {
	"o", -- 'omnifunc'
	".", -- current buffer
}
-- Visible link labels
vim.api.nvim_set_hl(0, "@markup.link.label.markdown_inline", { underline = true, update = true })

vim.b.autoformat = true
vim.b.format_func = format

local function check_checklist_item()
	local line = vim.api.nvim_get_current_line()
	if line:match("^%s*%- %[ %] ") == nil then
		return
	end
	local edited_line = line:gsub("%[ %]", "[X]", 1)
	edited_line = edited_line:gsub("^(%s*%- %[X%] )(.*)", "%1~%2~", 1)
	local start = vim.api.nvim_win_get_cursor(0)[1] - 1
	vim.api.nvim_buf_set_lines(0, start, start + 1, false, { edited_line })
end

local function uncheck_checklist_item()
	local line = vim.api.nvim_get_current_line()
	if line:match("^%s*%- %[X%] ") == nil then
		return
	end
	local edited_line = line:gsub("%[X%]", "[ ]", 1)
	edited_line = edited_line:gsub("~", "", 2)
	local start = vim.api.nvim_win_get_cursor(0)[1] - 1
	vim.api.nvim_buf_set_lines(0, start, start + 1, false, { edited_line })
end

vim.api.nvim_buf_create_user_command(0, "MarkdownListCheck", check_checklist_item, { desc = "Check checklist item" })
vim.api.nvim_buf_create_user_command(
	0,
	"MarkdownListUncheck",
	uncheck_checklist_item,
	{ desc = "Uncheck checklist item" }
)