aboutsummaryrefslogtreecommitdiff
path: root/vim/autoload/mail/header.vim
diff options
context:
space:
mode:
authorTom Ryder <tom@sanctum.geek.nz>2020-01-01 16:08:21 +1300
committerTom Ryder <tom@sanctum.geek.nz>2020-01-01 16:09:11 +1300
commit80227fc6263ca864227de8b9d8d9f93f19ad173a (patch)
treef0183af0e7fc5a73cc2427653ac7fd67a3848a2e /vim/autoload/mail/header.vim
parentAdd trailing commas (diff)
downloaddotfiles-80227fc6263ca864227de8b9d8d9f93f19ad173a.tar.gz
dotfiles-80227fc6263ca864227de8b9d8d9f93f19ad173a.zip
Reimplement mail header importance flagging
Just for fun, write something a little more comprehensive to read in the entire mail header as a data structure, in order to add or set header fields correctly. I don't think this is totally RFC-compliant yet; I'll need to check.
Diffstat (limited to 'vim/autoload/mail/header.vim')
-rw-r--r--vim/autoload/mail/header.vim54
1 files changed, 54 insertions, 0 deletions
diff --git a/vim/autoload/mail/header.vim b/vim/autoload/mail/header.vim
new file mode 100644
index 00000000..8495a721
--- /dev/null
+++ b/vim/autoload/mail/header.vim
@@ -0,0 +1,54 @@
+function! mail#header#Read() abort
+ let fields = []
+ for lnum in range(1, line('$'))
+ let line = getline(lnum)
+ let matchlist = matchlist(
+ \ line,
+ \ '^\([a-zA-Z0-9-]\+\):\s*\(\_.*\)',
+ \)
+ if !empty(matchlist)
+ let field = {
+ \ 'name': matchlist[1],
+ \ 'body': matchlist[2],
+ \}
+ call add(fields, field)
+ elseif line =~ '^\s' && exists('field')
+ let field['body'] .= "\n" . line
+ elseif line ==# ''
+ break
+ else
+ throw 'Parse error'
+ endif
+ endfor
+ let header = {
+ \'fields': fields,
+ \}
+ return header
+endfunction
+
+function! mail#header#String(header) abort
+ let fields = copy(a:header['fields'])
+ return join(
+ \ map(
+ \ copy(a:header['fields']),
+ \ 'v:val[''name''] . '': '' . v:val[''body''] . "\n"'),
+ \ '',
+ \)
+endfunction
+
+function! mail#header#Write(header) abort
+ let start = 1
+ for lnum in range(1, line('$'))
+ if getline(lnum) ==# ''
+ break
+ endif
+ let end = lnum
+ endfor
+ let curpos = getpos('.')
+ if exists('end')
+ let range = join([start, end], ',')
+ execute join(['silent', range, 'delete'])
+ endif
+ silent 0 put =mail#header#String(a:header)
+ call setpos('.', curpos)
+endfunction