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

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