summaryrefslogtreecommitdiffstats
path: root/lua/inbox/view.lua
blob: a72f3f2621cf23f9973ef6179f46d7a53a673c06 (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
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
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 start integer
---@param end_ integer
---@param strict_indexing boolean
---@param lines string[]?
---@param escape_ascii boolean?
function M.buf_set_lines(bufnr, start, end_, strict_indexing, lines, escape_ascii)
	local buf_writer
	if escape_ascii and pcall(require, "baleia") then
		local baleia = require("baleia").setup()
		buf_writer = baleia.buf_set_lines
	else
		buf_writer = vim.api.nvim_buf_set_lines
	end

	vim.bo[bufnr].modifiable = true
	buf_writer(bufnr, start, end_, strict_indexing, lines or {})
	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.buf_set_lines(bufnr, 0, -1, true, 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)

	vim.api.nvim_create_autocmd({ "BufModifiedSet", "BufWinEnter" }, {
		group = "Inbox",
		buffer = bufnr,
		callback = function()
			for _, winid in pairs(vim.fn.win_findbuf(bufnr)) do
				local winbar = string.format(
					"%sPart: %s",
					string.rep(" ", vim.fn.getwininfo(winid)[1].textoff),
					vim.b[bufnr].inbox_content_type
				)

				vim.api.nvim_set_option_value("winbar", winbar, {
					scope = "local",
					win = winid,
				})
			end
		end,
	})

	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 = {}

	local headers = {}
	if vim.b[bufnr].show_all_headers then
		headers = entry.headers
	else
		for _, name in pairs(config.headers) do
			if entry.headers[name] ~= nil then
				headers[name] = entry.headers[name]
			end
		end
	end

	for name, value in pairs(headers) do
		local i, line = next(value)
		table.insert(lines, ("%s: %s"):format(name, line))
		i, line = next(value, i)
		while i ~= nil do
			table.insert(lines, line)
			i, line = next(value, i)
		end
	end

	table.insert(lines, "")

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

	M.buf_set_lines(bufnr, 0, vim.b[bufnr].header_lines or 0, true, {})
	M.buf_set_lines(bufnr, 0, 0, true, lines)

	vim.b[bufnr].header_lines = #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.buf_set_lines(bufnr, vim.b[bufnr].header_lines or 0, -1, true, lines)
	else
		local job

		for i, filter in pairs(config.filters[content_type] or {}) do
			for key, value in pairs(filter) do
				if type(value) == "function" then
					filter[key] = value(bufnr)
				else
					filter[key] = value
				end
			end

			local opts = {
				command = filter.command,
				args = filter.args,
			}

			if job == nil then
				opts.writer = part.content
			else
				opts.writer = job
			end

			if i == #config.filters[content_type] then
				opts.on_exit = vim.schedule_wrap(function(self)
					M.buf_set_lines(bufnr, vim.b[bufnr].header_lines or 0, -1, true, self:result(), true)
				end)
			end

			job = Job:new(opts)
		end

		job:start()
	end
end

return M