aboutsummaryrefslogtreecommitdiffhomepage
path: root/vim/plugin/notmuch.vim
blob: 5fe438e78cf1eece676d160201c473738b2e9c2a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
" notmuch.vim plugin --- run notmuch within vim
"
" Copyright © Carl Worth
"
" This file is part of Notmuch.
"
" Notmuch is free software: you can redistribute it and/or modify it
" under the terms of the GNU General Public License as published by
" the Free Software Foundation, either version 3 of the License, or
" (at your option) any later version.
"
" Notmuch is distributed in the hope that it will be useful, but
" WITHOUT ANY WARRANTY; without even the implied warranty of
" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
" General Public License for more details.
"
" You should have received a copy of the GNU General Public License
" along with Notmuch.  If not, see <http://www.gnu.org/licenses/>.
"
" Authors: Bart Trojanowski <bart@jukie.net>

" --- defaults

if !exists('g:notmuch_cmd')
        let g:notmuch_cmd = 'notmuch'
endif

if !exists('g:notmuch_search_reverse')
        let g:notmuch_search_reverse = 1
endif

" --- used to match output of notmuch

let s:notmuch_show_message_begin_regexp    = '^message{'
let s:notmuch_show_message_end_regexp      = '^message}'
let s:notmuch_show_header_begin_regexp     = '^header{'
let s:notmuch_show_header_end_regexp       = '^header}'
let s:notmuch_show_body_begin_regexp       = '^body{'
let s:notmuch_show_body_end_regexp         = '^body}'
let s:notmuch_show_attachment_begin_regexp = '^attachment{'
let s:notmuch_show_attachment_end_regexp   = '^attachment}'
let s:notmuch_show_part_begin_regexp       = '^part{'
let s:notmuch_show_part_end_regexp         = '^part}'
let s:notmuch_show_marker_regexp           = '^\\(message\\|header\\|body\\|attachment\\|part\\)[{}].*$'

let s:notmuch_show_id_regexp               = '\(id:[^ ]*\)'
let s:notmuch_show_depth_regexp            = ' depth:\([0-9]*\) '
let s:notmuch_show_filename_regexp         = 'filename:\(.*\)$'
let s:notmuch_show_tags_regexp             = '(\([^)]*\))$'

let s:notmuch_show_signature_regexp        = '^\(-- \?\|_\+\)$'
let s:notmuch_show_signature_lines_max     = 12

let s:notmuch_show_citation_regexp         = '^\s*>'

" --- implement search screen

function! s:NM_cmd_search(words)
        let cmd = ['search']
        if g:notmuch_search_reverse
                let cmd = cmd + ['--reverse']
        endif
        let data = s:NM_run(cmd + a:words)
        "let data = substitute(data, '27/27', '25/27', '')
        "let data = substitute(data, '\[4/4\]', '[0/4]', '')
        let lines = split(data, "\n")
        let disp = copy(lines)
        call map(disp, 'substitute(v:val, "^thread:\\S* ", "", "")' )

        call s:NM_newBuffer('search', join(disp, "\n"))
        let b:nm_raw_data = lines

        nnoremap <buffer> <Enter> :call <SID>NM_search_display()<CR>
        nnoremap <buffer> s       :call <SID>NM_cmd_search(split(input('NotMuch Search:')))<CR>
        setlocal cursorline
        setlocal nowrap
endfunction

function! s:NM_search_display()
        if !exists('b:nm_raw_data')
                echo 'no b:nm_raw_data'
        else
                let line = line('.')
                let info = b:nm_raw_data[line-1]
                let what = split(info, '\s\+')[0]
                call s:NM_cmd_show([what])
        endif
endfunction


" --- implement show screen

function! s:NM_cmd_show(words)
        let bufnr = bufnr('%')
        let data = s:NM_run(['show'] + a:words)

        call s:NM_newBuffer('show', data)
        setlocal bufhidden=delete
        let b:nm_raw_data = data

        call s:NM_cmd_show_mkfolds()

        exec printf("nnoremap <buffer> q :b %d<CR>", bufnr)
endfunction

function! s:NM_cmd_show_mkfolds()
        let modetype = ''
        let modeline = -1
        let lnum = 1
        while lnum <= line('$')
                let line = getline(lnum)
                if modetype == ''
                        if match(line, s:notmuch_show_signature_regexp) != -1
                                let modetype = 'sig'
                                let modeline = lnum
                        elseif match(line, s:notmuch_show_citation_regexp) != -1
                                let modetype = 'cit'
                                let modeline = lnum
                        endif
                elseif modetype == 'cit'
                        if match(line, s:notmuch_show_citation_regexp) == -1
                                exec printf('%d,%dfold', modeline, lnum)
                                let modetype = ''
                        endif
                elseif modetype == 'sig'
                        if (lnum - modeline) > s:notmuch_show_signature_lines_max
                                let modetype = ''
                        elseif match(line, s:notmuch_show_part_end_regexp) != -1
                                exec printf('%d,%dfold', modeline, lnum)
                                let modetype = ''
                        endif
                endif

                let lnum = lnum + 1
        endwhile
endfunction


" --- helper functions

function! s:NM_newBuffer(ft, content)
        enew
        setlocal buftype=nofile readonly modifiable
        silent put=a:content
        keepjumps 0d
        setlocal nomodifiable
        execute printf('set filetype=notmuch-%s', a:ft)
        execute printf('set syntax=notmuch-%s', a:ft)
endfunction

function! s:NM_run(args)
        let cmd = g:notmuch_cmd . ' ' . join(a:args) . '< /dev/null'
        let out = system(cmd)
        if v:shell_error
                echohl Error
                echo substitute(out, '\n*$', '', '')
                echohl None
                return ''
        else
                return out
        endif
endfunction


" --- command handler

function! NotMuch(args)
        if !strlen(a:args)
                call s:NM_cmd_search(['tag:inbox'])
                return
        endif

        echo "blarg!"

        let words = split(a:args)
        " TODO: handle commands passed as arguments
endfunction
function! CompleteNotMuch(arg_lead, cmd_line, cursor_pos)
        return []
endfunction


" --- glue

command! -nargs=* -complete=customlist,CompleteNotMuch NotMuch call NotMuch(<q-args>)
cabbrev  notmuch <c-r>=(getcmdtype()==':' && getcmdpos()==1 ? 'NotMuch' : 'notmuch')<CR>

" --- hacks, only for development :)

nnoremap ,nmr :source ~/.vim/plugin/notmuch.vim<CR>:call NotMuch('')<CR>