]> git.notmuchmail.org Git - notmuch/blob - vim/plugin/notmuch.vim
added syntax files for search and show screens
[notmuch] / vim / plugin / notmuch.vim
1 " notmuch.vim plugin --- run notmuch within vim
2 "
3 " Copyright © Carl Worth
4 "
5 " This file is part of Notmuch.
6 "
7 " Notmuch is free software: you can redistribute it and/or modify it
8 " under the terms of the GNU General Public License as published by
9 " the Free Software Foundation, either version 3 of the License, or
10 " (at your option) any later version.
11 "
12 " Notmuch is distributed in the hope that it will be useful, but
13 " WITHOUT ANY WARRANTY; without even the implied warranty of
14 " MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 " General Public License for more details.
16 "
17 " You should have received a copy of the GNU General Public License
18 " along with Notmuch.  If not, see <http://www.gnu.org/licenses/>.
19 "
20 " Authors: Bart Trojanowski <bart@jukie.net>
21
22 " --- defaults
23
24 if !exists('g:notmuch_cmd')
25         let g:notmuch_cmd = 'notmuch'
26 endif
27
28 " --- implement search screen
29
30 function! s:NM_cmd_search(words)
31         let data = s:NM_run(['search'] + a:words)
32         "let data = substitute(data, '27/27', '25/27', '')
33         "let data = substitute(data, '\[4/4\]', '[0/4]', '')
34         let lines = split(data, "\n")
35         let disp = copy(lines)
36         call map(disp, 'substitute(v:val, "^thread:\\S* ", "", "")' )
37
38         call s:NM_newBuffer('search', join(disp, "\n"))
39         let b:nm_raw_data = lines
40
41         nnoremap <buffer> <Enter> :call <SID>NM_search_display()<CR>
42         setlocal cursorline
43         setlocal nowrap
44 endfunction
45
46 function! s:NM_search_display()
47         let line = line('.')
48         if !exists('b:nm_raw_data')
49                 echo 'no b:nm_raw_data'
50         else
51                 let info = b:nm_raw_data[line]
52                 let what = split(info, '\s\+')[0]
53                 call s:NM_cmd_show([what])
54         endif
55 endfunction
56
57
58 " --- implement show screen
59
60 function! s:NM_cmd_show(words)
61         let bufnr = bufnr('%')
62         let data = s:NM_run(['show'] + a:words)
63
64         call s:NM_newBuffer('show', data)
65         setlocal bufhidden=delete
66         let b:nm_raw_data = data
67
68         exec printf("nnoremap <buffer> q :b %d<CR>", bufnr)
69 endfunction
70
71
72 " --- helper function
73
74 function! s:NM_newBuffer(ft, content)
75         enew
76         setlocal buftype=nofile readonly modifiable
77         silent put=a:content
78         keepjumps 0d
79         setlocal nomodifiable
80         execute printf('set filetype=notmuch-%s', a:ft)
81         execute printf('set syntax=notmuch-%s', a:ft)
82 endfunction
83
84 function! s:NM_run(args)
85         let cmd = g:notmuch_cmd . ' ' . join(a:args) . '< /dev/null'
86         let out = system(cmd)
87         if v:shell_error
88                 echohl Error
89                 echo substitute(out, '\n*$', '', '')
90                 echohl None
91                 return ''
92         else
93                 return out
94         endif
95 endfunction
96
97
98 " --- command handler
99
100 function! NotMuch(args)
101         if !strlen(a:args)
102                 call s:NM_cmd_search(['tag:inbox'])
103                 return
104         endif
105
106         echo "blarg!"
107
108         let words = split(a:args)
109         " TODO: handle commands passed as arguments
110 endfunction
111 function! CompleteNotMuch(arg_lead, cmd_line, cursor_pos)
112         return []
113 endfunction
114
115
116 " --- glue
117
118 command! -nargs=* -complete=customlist,CompleteNotMuch NotMuch call NotMuch(<q-args>)
119 cabbrev  notmuch <c-r>=(getcmdtype()==':' && getcmdpos()==1 ? 'NotMuch' : 'notmuch')<CR>
120
121 " --- hacks, only for development :)
122
123 nnoremap ,nmr :source ~/.vim/plugin/notmuch.vim<CR>:call NotMuch('')<CR>