]> git.madduck.net Git - etc/lazyvim.git/blob - .config/lazyvim/after/ftplugin/mail.lua

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

b3d4005a731debe4d57fc1d4cad2000054a8f180
[etc/lazyvim.git] / .config / lazyvim / after / ftplugin / mail.lua
1 local i, _ = string.find(vim.bo.filetype, "markdown")
2 if not i then
3   vim.opt_local.filetype = "mail.markdown"
4   -- this should trigger a reload of the ftplugin
5   return
6 end
7
8 -- There are no diagnostics for mail, but we do not want the
9 -- Markdown standards imposed on mail header and signature…
10 vim.diagnostic.enable(false)
11 vim.b.autoformat = false
12
13 -- disable Treesitter format expression for Mail
14 vim.opt_local.formatexpr = ""
15
16 vim.opt_local.formatoptions:remove("o")
17 vim.opt_local.formatoptions:remove("r")
18 vim.opt_local.formatoptions:remove("l")
19
20 vim.keymap.set("n", "<leader>m", "", { buffer = true, desc = "mail functions" })
21 vim.keymap.set("n", "<leader>ms", "", { buffer = true, desc = "subject manipulation" })
22
23 vim.keymap.set(
24   "n",
25   "<leader>msn",
26   ":1,/^$/s,\\v(Subject:)\\s*((Re|AW):\\s*)*((.|\\_s\\s+)+),\\1  (was: \\4),e<CR>:set nohls<CR>Whi",
27   -- <CR><cmd>set nohls<CR>f li",
28   { buffer = true, desc = "make a new subject" }
29 )
30 vim.keymap.set(
31   "n",
32   "<leader>msd",
33   '1G/\\v^Subject:(.|\\_s\\s+)+was:/e<CR>:set nohls<CR>"_dab',
34   { buffer = true, desc = "remove old subjects" }
35 )
36
37 vim.keymap.set("n", "<leader>p", "", { buffer = true, desc = "mailplate profiles" })
38
39 vim.keymap.set(
40   "n",
41   "<leader>pp",
42   ":w<CR>:%!mailplate --auto --keep-unknown 2>/dev/null<CR>",
43   { buffer = true, desc = "Automatically determine mailplate profile" }
44 )
45
46 local function profile_keymap(key, profile)
47   vim.keymap.set(
48     "n",
49     "<leader>p<" .. key .. ">",
50     ":w<CR>:%!mailplate --keep-unknown " .. profile .. "<CR>",
51     { buffer = true, desc = "Switch to mailplate profile '" .. profile .. "'" }
52   )
53 end
54
55 profile_keymap("F1", "official")
56 profile_keymap("F2", "pobox")
57 profile_keymap("F3", "tahi")
58 profile_keymap("F4", "toni")
59 profile_keymap("F5", "kbkg")
60 profile_keymap("F6", "krafftwerk")
61 profile_keymap("F7", "siby")
62 profile_keymap("F8", "debian")
63 profile_keymap("F9", "uniwh")
64 profile_keymap("F10", "mtfk")
65 profile_keymap("F11", "sudetia")
66 profile_keymap("F12", "default")
67
68 local function write_mail_backup()
69   local tmpdir = vim.fn.expand(os.getenv("TMPDIR") or "/tmp") .. "/mail-backups"
70   vim.fn.mkdir(tmpdir, "p", "0o700")
71   local filename = os.date("%Y-%m-%d-%H%M%S") .. ".msg"
72   local file = io.open(tmpdir .. "/" .. filename, "a")
73   if file ~= nil then
74     local lines = vim.api.nvim_buf_get_lines(0, 1, -1, false) or {}
75     local content = table.concat(lines, "\n")
76     file:write(content)
77     file:close()
78     vim.notify("Saved a backup to " .. filename, vim.log.levels.INFO)
79   end
80 end
81
82 vim.api.nvim_create_autocmd({ "BufWrite" }, {
83   callback = write_mail_backup,
84   buffer = 0,
85 })
86
87 vim.cmd.runtime("greeting_abbrevs.vim")
88
89 local function mail_area_detect()
90   local ts = vim.treesitter
91   if not ts then
92     return
93   end
94   local node = vim.treesitter.get_node()
95   if not node then
96     return
97   end
98   if node:type():find("^body") ~= nil then
99     vim.opt_local.formatoptions:append("a")
100     vim.opt_local.formatoptions:append("w")
101     vim.opt_local.formatoptions:append("n")
102     vim.opt_local.formatoptions:append("t")
103     vim.opt_local.formatoptions:append("c")
104     vim.w.in_body = true
105   else
106     vim.opt_local.formatoptions:remove("a")
107     vim.opt_local.formatoptions:remove("w")
108     vim.opt_local.formatoptions:remove("n")
109     vim.opt_local.formatoptions:remove("t")
110     vim.opt_local.formatoptions:remove("c")
111     vim.w.in_body = false
112   end
113 end
114
115 local cmdgroup = vim.api.nvim_create_augroup("mailarea", { clear = true })
116 vim.api.nvim_create_autocmd({ "CursorMoved", "CursorMovedI" }, {
117   buffer = 0,
118   group = cmdgroup,
119   callback = mail_area_detect,
120 })
121 mail_area_detect()