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

formatlistpat so that dates are not interpreted as lists
[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 -- Do not conceal anything (Markdown) in emails going out
18 vim.opt_local.conceallevel = 0
19 -- }}}
20
21 -- {{{ formatting
22 vim.opt_local.formatoptions:remove("o") -- would insert current comment leader for o/O
23 vim.opt_local.formatoptions:remove("r") -- would insert current comment leader for <CR>
24 vim.opt_local.formatoptions:remove("l") -- would not break lines that were long before insert
25
26 vim.opt_local.commentstring = "> %s" -- commenting means quoting in mails
27 -- }}}
28
29 -- {{{ options
30 vim.opt.number = false
31 vim.opt.relativenumber = false
32 vim.opt.signcolumn = "no"
33 -- }}}
34
35 -- {{{ autocmds
36 vim.api.nvim_create_autocmd({ "BufWrite" }, {
37   buffer = 0,
38   group = vim.api.nvim_create_augroup("squashemptylines", { clear = true }),
39   callback = function()
40     local lines = vim.api.nvim_buf_get_lines(0, 0, -1, false)
41     local ret = {}
42     local count = 0
43     local squash = false
44     for _, line in ipairs(lines) do
45       if line == "" then
46         if not squash then
47           squash = true
48           table.insert(ret, line)
49         else
50           count = count + 1
51         end
52       else
53         squash = false
54         table.insert(ret, line)
55       end
56     end
57     if count > 0 then
58       vim.notify("Squashed " .. count .. " empty line(s)")
59     end
60     vim.api.nvim_buf_set_lines(0, 0, -1, false, ret)
61   end,
62 })
63 -- }}}
64
65 -- {{{ keymaps
66 vim.keymap.set("n", "<leader>m", "", { buffer = true, desc = "mail functions" })
67 vim.keymap.set("n", "<leader>ms", "", { buffer = true, desc = "subject manipulation" })
68
69 vim.keymap.set(
70   "n",
71   "<leader>msn",
72   ":1,/^$/s,\\v(Subject:)\\s*((Re|AW):\\s*)*((.|\\_s\\s+)+),\\1  (was: \\4),e<CR>:set nohls<CR>Whi",
73   -- <CR><cmd>set nohls<CR>f li",
74   { buffer = true, desc = "make a new subject" }
75 )
76 vim.keymap.set(
77   "n",
78   "<leader>msd",
79   '1G/\\v^Subject:(.|\\_s\\s+)+was:/e<CR>:set nohls<CR>"_dab',
80   { buffer = true, desc = "remove old subjects" }
81 )
82
83 -- {{{ profiles
84
85 vim.keymap.set("n", "<leader>p", "", { buffer = true, desc = "mailplate profiles" })
86
87 vim.keymap.set(
88   "n",
89   "<leader>pp",
90   ":w<CR>:%!mailplate --auto --keep-unknown 2>/dev/null<CR>",
91   { buffer = true, desc = "Automatically determine mailplate profile" }
92 )
93
94 local function profile_keymap(key, profile)
95   vim.keymap.set(
96     "n",
97     "<leader>p<" .. key .. ">",
98     ":w<CR>:%!mailplate --keep-unknown " .. profile .. "<CR>",
99     { buffer = true, desc = "Switch to mailplate profile '" .. profile .. "'" }
100   )
101 end
102
103 profile_keymap("F1", "main")
104 profile_keymap("F2", "pobox")
105 profile_keymap("F3", "tahi")
106 profile_keymap("F4", "toni")
107 profile_keymap("F5", "kbkg")
108 profile_keymap("F6", "krafftwerk")
109 profile_keymap("F7", "siby")
110 profile_keymap("F8", "debian")
111 profile_keymap("F9", "uniwh")
112 profile_keymap("F10", "mtfk")
113 profile_keymap("F12", "default")
114
115 -- }}} profiles
116 -- }}}
117
118 -- {{{ write mail backups
119
120 local function write_mail_backup()
121   local tmpdir = vim.fn.expand(os.getenv("TMPDIR") or "/tmp") .. "/mail-backups"
122   vim.fn.mkdir(tmpdir, "p", "0o700")
123   local filename = os.date("%Y-%m-%d-%H%M%S") .. ".msg"
124   local file = io.open(tmpdir .. "/" .. filename, "a")
125   if file ~= nil then
126     local lines = vim.api.nvim_buf_get_lines(0, 1, -1, false) or {}
127     local content = table.concat(lines, "\n")
128     file:write(content)
129     file:close()
130     vim.notify("Saved a backup to " .. filename, vim.log.levels.INFO)
131   end
132 end
133
134 vim.api.nvim_create_autocmd({ "BufWrite" }, {
135   callback = write_mail_backup,
136   buffer = 0,
137 })
138
139 -- }}}
140
141 -- {{{ mail area detect
142
143 local function mail_area_detect()
144   local ts = vim.treesitter
145   if not ts then
146     return
147   end
148   local node = vim.treesitter.get_node()
149   if not node then
150     return
151   end
152   if node:type():find("^body") ~= nil then
153     vim.opt_local.formatoptions:append("a") -- turn on auto-reflow
154     vim.opt_local.formatoptions:append("w") -- trailing whitespace for format=flowed
155     vim.opt_local.formatoptions:append("n") -- numbered lists
156     vim.opt_local.formatoptions:append("t") -- autowrap with textwidth
157     vim.opt_local.formatoptions:append("c") -- autowrap and insert quote (comment) leader
158     vim.w.in_body = true
159   else
160     vim.opt_local.formatoptions:remove("a")
161     vim.opt_local.formatoptions:remove("w")
162     vim.opt_local.formatoptions:remove("n")
163     vim.opt_local.formatoptions:remove("t")
164     vim.opt_local.formatoptions:remove("c")
165     vim.w.in_body = false
166   end
167 end
168
169 local cmdgroup = vim.api.nvim_create_augroup("mailarea", { clear = true })
170 vim.api.nvim_create_autocmd({ "CursorMoved", "CursorMovedI" }, {
171   buffer = 0,
172   group = cmdgroup,
173   callback = mail_area_detect,
174 })
175 mail_area_detect()
176
177 vim.cmd.runtime("greeting_abbrevs.vim")
178
179 -- }}}
180
181 -- vim:foldmethod=marker:foldlevel=0