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

8513a6d1ee1b406417af53386b617099f65fcfc1
[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", "official")
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("F11", "sudetia")
105 profile_keymap("F12", "default")
106
107 -- }}} profiles
108 -- }}}
109
110 -- {{{ write mail backups
111
112 local function write_mail_backup()
113   local tmpdir = vim.fn.expand(os.getenv("TMPDIR") or "/tmp") .. "/mail-backups"
114   vim.fn.mkdir(tmpdir, "p", "0o700")
115   local filename = os.date("%Y-%m-%d-%H%M%S") .. ".msg"
116   local file = io.open(tmpdir .. "/" .. filename, "a")
117   if file ~= nil then
118     local lines = vim.api.nvim_buf_get_lines(0, 1, -1, false) or {}
119     local content = table.concat(lines, "\n")
120     file:write(content)
121     file:close()
122     vim.notify("Saved a backup to " .. filename, vim.log.levels.INFO)
123   end
124 end
125
126 vim.api.nvim_create_autocmd({ "BufWrite" }, {
127   callback = write_mail_backup,
128   buffer = 0,
129 })
130
131 -- }}}
132
133 -- {{{ mail area detect
134
135 local function mail_area_detect()
136   local ts = vim.treesitter
137   if not ts then
138     return
139   end
140   local node = vim.treesitter.get_node()
141   if not node then
142     return
143   end
144   if node:type():find("^body") ~= nil then
145     vim.opt_local.formatoptions:append("a") -- turn on auto-reflow
146     vim.opt_local.formatoptions:append("w") -- trailing whitespace for format=flowed
147     vim.opt_local.formatoptions:append("n") -- numbered lists
148     vim.opt_local.formatoptions:append("t") -- autowrap with textwidth
149     vim.opt_local.formatoptions:append("c") -- autowrap and insert quote (comment) leader
150     vim.w.in_body = true
151   else
152     vim.opt_local.formatoptions:remove("a")
153     vim.opt_local.formatoptions:remove("w")
154     vim.opt_local.formatoptions:remove("n")
155     vim.opt_local.formatoptions:remove("t")
156     vim.opt_local.formatoptions:remove("c")
157     vim.w.in_body = false
158   end
159 end
160
161 local cmdgroup = vim.api.nvim_create_augroup("mailarea", { clear = true })
162 vim.api.nvim_create_autocmd({ "CursorMoved", "CursorMovedI" }, {
163   buffer = 0,
164   group = cmdgroup,
165   callback = mail_area_detect,
166 })
167 mail_area_detect()
168
169 vim.cmd.runtime("greeting_abbrevs.vim")
170
171 -- }}}
172
173 -- vim:foldmethod=marker:foldlevel=0