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

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