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

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