summaryrefslogtreecommitdiffstats
path: root/lua/inbox/view.lua
blob: 5c40d5f6bf9bc96a37b5e563d500ef9bc7b322ad (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
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
local config = require("inbox.config")
local indexers = require("inbox.indexers")
local utils = require("inbox.utils")

local M = {}

---@param path string
---@param buf_options table<string, any> | nil
---@return integer bufnr
function M.create_buffer(path, buf_options)
	local bufnr = vim.api.nvim_create_buf(true, false)

	for k, v in pairs(buf_options or {}) do
		vim.api.nvim_buf_set_option(bufnr, k, v)
	end

	local bufname = string.format("maildir://%s", path)

	vim.api.nvim_buf_set_name(bufnr, bufname)

	return bufnr
end

function M.render_buffer(bufnr)
	local maildir, id = utils.parse_scheme(bufnr)

	if maildir == nil then
		vim.notify(("Buffer is not a valid maildir buffer: %s"):format(bufnr), vim.log.levels.ERROR)
	elseif id ~= nil and id ~= "" then
		M.render_entry(bufnr, id)
	else
		M.render_inbox(bufnr, maildir)
	end
end

---@param bufnr integer
---@param lines string[]
---@param start integer?
---@param end_ integer?
function M.set_buffer_content(bufnr, lines, start, end_)
	if start == nil then
		start = 0
	end

	if end_ == nil then
		end_ = -1
	end

	vim.bo[bufnr].modifiable = true
	vim.api.nvim_buf_set_lines(bufnr, start, end_, true, lines)
	vim.bo[bufnr].modifiable = false
	vim.bo[bufnr].modified = false
end

---@param maildir string
---@return integer bufnr
function M.initialize_inbox(maildir)
	local buf_options = vim.tbl_extend("keep", config.buf_options, {
		buftype = "acwrite",
		syntax = "inbox",
		filetype = "inbox",
	})

	local bufnr = M.create_buffer(maildir, buf_options)

	-- vim.api.nvim_clear_autocmds({ buffer = bufnr, group = "Inbox" })

	vim.keymap.set("n", "<Enter>", require("inbox").open_entry, { buffer = bufnr })

	M.render_inbox(bufnr, maildir)

	return bufnr
end

---@param bufnr integer
function M.render_inbox(bufnr, maildir)
	local indexer = indexers.get_indexer()
	indexer.index(maildir, function(ids, entries, signs)
		vim.b[bufnr].inbox_ids = ids
		local lines, highlights = utils.render_table(entries, config.columns)

		local winid = vim.api.nvim_get_current_win()
		for k, v in pairs(config.win_options) do
			vim.api.nvim_set_option_value(k, v, { scope = "local", win = winid })
		end

		M.set_buffer_content(bufnr, lines)
		utils.set_highlights(bufnr, highlights)
		utils.set_signs(bufnr, signs)

		vim.api.nvim_set_option_value(
			"winbar",
			utils.render_row({ "Flags", "Date", "From", "Subject" }, {
				vim.fn.getwininfo(winid)[1].textoff,
				unpack(config.columns),
			}),
			{ scope = "local", win = winid }
		)
	end)
end

---@param maildir string
---@param id string
---@param content_type string
---@return integer? bufnr
function M.initialize_entry(maildir, id, content_type)
	local bufname = ("%s:%s"):format(maildir, id)
	local bufnr = M.create_buffer(bufname, {
		syntax = "mail",
		filetype = "mail",
	})

	vim.api.nvim_clear_autocmds({ buffer = bufnr, group = require("inbox").augroup })

	vim.api.nvim_create_autocmd("BufDelete", {
		group = "Inbox",
		buffer = bufnr,
		callback = function()
			require("inbox").buffers[id] = nil
		end,
	})

	vim.keymap.set("n", "q", function()
		vim.api.nvim_buf_delete(bufnr, {})
	end, { buffer = bufnr })

	vim.keymap.set("n", "<C-h>", require("inbox").toggle_headers, { buffer = bufnr })
	vim.keymap.set("n", "<C-n>", function()
		local k
		if vim.b[bufnr].inbox_content_type then
			k = next(vim.b[bufnr].inbox_parts, vim.b[bufnr].inbox_content_type)
		else
			k = next(vim.b[bufnr].inbox_parts)
		end
		M.render_entry(bufnr, id, k)
	end, { buffer = bufnr })

	vim.b[bufnr].inbox_id = id
	vim.b[bufnr].header_filter = config.headers

	M.render_headers(bufnr, id)
	M.render_entry(bufnr, id, content_type)

	return bufnr
end

function M.render_headers(bufnr, id)
	local indexer = indexers.get_indexer()
	local entry = indexer.get_entry(id)

	if entry == nil then
		return
	end

	local lines = {}

	for _, name in pairs(config.headers) do
		if entry.headers[name] ~= nil then
			table.insert(lines, ("%s: %s"):format(name, entry.headers[name]))
		end
	end

	if vim.b[bufnr].show_all_headers then
		for name, value in pairs(entry.headers) do
			if not vim.tbl_contains(config.headers, name) then
				table.insert(lines, ("%s: %s"):format(name, value))
			end
		end
	end

	table.insert(lines, "")

	local cursor_pos = vim.fn.getpos(".")

	if vim.b[bufnr].header_count ~= nil then
		M.set_buffer_content(bufnr, {}, 0, vim.b[bufnr].header_count)
	end

	M.set_buffer_content(bufnr, lines, 0, 0)

	vim.b[bufnr].header_count = #lines

	vim.fn.setpos(".", cursor_pos)
end

function M.render_entry(bufnr, id, content_type)
	local Job = require("plenary.job")
	local indexer = indexers.get_indexer()
	local entry = indexer.get_entry(id)

	if entry == nil then
		utils.error("Failed to get entry", { id = id })
		return
	end

	if content_type == nil then
		content_type = next(entry.parts)
	end

	local part = entry.parts[content_type]

	if part == nil then
		utils.error("Failed to find message part for entry", { id = id, ["content-type"] = content_type })
		return
	end

	vim.b[bufnr].inbox_parts = entry.parts
	vim.b[bufnr].inbox_content_type = content_type

	if vim.tbl_isempty(config.filters[content_type] or {}) then
		local lines = vim.split(part.content, "\n")
		M.set_buffer_content(bufnr, lines, vim.b[bufnr].header_count)
	else
		local job

		for i, filter in pairs(config.filters[content_type]) do
			if i == 1 then
				job = Job:new({
					command = filter.command,
					args = filter.args,
					writer = part.content,
				})
			elseif i < #config.filters[content_type] then
				job = Job:new({
					command = filter.command,
					args = filter.args,
					writer = job,
				})
			else
				job = Job:new({
					command = filter.command,
					args = filter.args,
					writer = job,
					on_exit = vim.schedule_wrap(function(self)
						vim.print(self)
						M.set_buffer_content(bufnr, self:result(), vim.b[bufnr].header_count)
					end),
				})
			end
		end

		job:start()
	end
end

return M