summaryrefslogtreecommitdiff
path: root/dot_config/nvim/ftplugin/markdown_quote.lua
blob: 3a57c201f1cde0e4420999104e0a0de63e857c53 (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
68
69
70
71
72
73
74
75
76
local api = vim.api
local fn  = vim.fn

local buffer = 0 -- operate on current buffer

local function location(opts)
	-- if called from a command, we don't have event info
	if not opts then
		opts = {
			line1 = fn.line 'v',
			line2 = fn.line '.',
		}
	end

	if opts.line1 > opts.line2 then
		opts.line1, opts.line2 = opts.line2, opts.line1
	end

	-- nvim api lines are zero-based end-exclusive
	opts.line1 = opts.line1 - 1
	return opts
end

local function mdquote(opts)
	opts = location(opts)

	-- 1. get all of the matching lines
	local lines = api.nvim_buf_get_lines(buffer, opts.line1, opts.line2, true)

	-- 2. edit them appropriately
	for i, line in ipairs(lines) do
		-- prepend a ">" and a " ",
		-- unless the first character was already ">" or whitespace
		-- so prepend " " first unless the above is true
		if line:match '^$' then
		elseif line:match '^[%s>]' then
			lines[i] = '>' .. line
		else
			lines[i] = '> ' .. line
		end
	end

	-- 3. insert them back
	api.nvim_buf_set_lines(buffer, opts.line1, opts.line2, true, lines)
end

local function mddequote(opts)
	opts = location(opts)

	-- see mdquote for the steps
	local lines = api.nvim_buf_get_lines(buffer, opts.line1, opts.line2, true)

	for i, line in ipairs(lines) do
		-- if the line starts with a "> ", remove both
		-- elseif the line starts with a ">", remove that
		-- TODO: I don't handle \t-based quoting
		if line:match '^> ' then
			lines[i] = line:sub(3)
		elseif line:match '^>' then
			lines[i] = line:sub(2)
		end
	end

	api.nvim_buf_set_lines(buffer, opts.line1, opts.line2, true, lines)
end

api.nvim_buf_create_user_command(buffer, 'MdQuote', mdquote, { range = true })
api.nvim_buf_create_user_command(buffer, 'MdDeQuote', mddequote, { range = true })
vim.keymap.set({'n', 'x'}, '>', mdquote, {
	buffer = true,
	desc = 'increase quote level of selection',
})
vim.keymap.set({'n', 'x'}, '<', mddequote, {
	buffer = true,
	desc = 'decrease quote level of selection',
})