]> 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:

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