]> git.notmuchmail.org Git - notmuch/blob - vim/plugin/notmuch.vim
4a4a2016f4725cc7f2b21a4ca09617eaa0d15fe1
[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 " --- configuration defaults {{{1
23
24 let s:notmuch_defaults = {
25         \ 'g:notmuch_cmd':                           'notmuch'                    ,
26         \
27         \ 'g:notmuch_search_newest_first':           1                            ,
28         \ 'g:notmuch_search_from_column_width':      20                           ,
29         \
30         \ 'g:notmuch_show_fold_signatures':          1                            ,
31         \ 'g:notmuch_show_fold_citations':           1                            ,
32         \ 'g:notmuch_show_fold_bodies':              0                            ,
33         \ 'g:notmuch_show_fold_headers':             1                            ,
34         \
35         \ 'g:notmuch_show_message_begin_regexp':     '^\fmessage{'                ,
36         \ 'g:notmuch_show_message_end_regexp':       '^\fmessage}'                ,
37         \ 'g:notmuch_show_header_begin_regexp':      '^\fheader{'                 ,
38         \ 'g:notmuch_show_header_end_regexp':        '^\fheader}'                 ,
39         \ 'g:notmuch_show_body_begin_regexp':        '^\fbody{'                   ,
40         \ 'g:notmuch_show_body_end_regexp':          '^\fbody}'                   ,
41         \ 'g:notmuch_show_attachment_begin_regexp':  '^\fattachment{'             ,
42         \ 'g:notmuch_show_attachment_end_regexp':    '^\fattachment}'             ,
43         \ 'g:notmuch_show_part_begin_regexp':        '^\fpart{'                   ,
44         \ 'g:notmuch_show_part_end_regexp':          '^\fpart}'                   ,
45         \ 'g:notmuch_show_marker_regexp':            '^\f\\(message\\|header\\|body\\|attachment\\|part\\)[{}].*$',
46         \
47         \ 'g:notmuch_show_message_parse_regexp':     '\(id:[^ ]*\) depth:\([0-9]*\) filename:\(.*\)$',
48         \ 'g:notmuch_show_tags_regexp':              '(\([^)]*\))$'               ,
49         \
50         \ 'g:notmuch_show_signature_regexp':         '^\(-- \?\|_\+\)$'           ,
51         \ 'g:notmuch_show_signature_lines_max':      12                           ,
52         \
53         \ 'g:notmuch_show_citation_regexp':          '^\s*>'                      ,
54         \ }
55
56 " defaults for g:notmuch_initial_search_words
57 " override with: let g:notmuch_initial_search_words = [ ... ]
58 let s:notmuch_initial_search_words_defaults = [
59         \ 'tag:inbox'
60         \ ]
61
62 " defaults for g:notmuch_show_headers
63 " override with: let g:notmuch_show_headers = [ ... ]
64 let s:notmuch_show_headers_defaults = [
65         \ 'Subject',
66         \ 'To',
67         \ 'Cc',
68         \ 'Bcc',
69         \ 'Date'
70         \ ]
71
72 " --- keyboard mapping definitions {{{1
73
74 " --- --- bindings for search screen {{{2
75 let g:notmuch_search_maps = {
76         \ '<Enter>':    ':call <SID>NM_search_show_thread()<CR>',
77         \ 'a':          ':call <SID>NM_search_archive_thread()<CR>',
78         \ 'f':          ':call <SID>NM_search_filter()<CR>',
79         \ 'm':          ':call <SID>NM_new_mail()<CR>',
80         \ 'o':          ':call <SID>NM_search_toggle_order()<CR>',
81         \ 'r':          ':call <SID>NM_search_reply_to_thread()<CR>',
82         \ 's':          ':call <SID>NM_search_prompt()<CR>',
83         \ 'S':          ':call <SID>NM_search_edit()<CR>',
84         \ 't':          ':call <SID>NM_search_filter_by_tag()<CR>',
85         \ 'q':          ':call <SID>NM_kill_buffer()<CR>',
86         \ '+':          ':call <SID>NM_search_add_tags([])<CR>',
87         \ '-':          ':call <SID>NM_search_remove_tags([])<CR>',
88         \ '=':          ':call <SID>NM_search_refresh_view()<CR>',
89         \ }
90
91 " --- --- bindings for show screen {{{2
92 let g:notmuch_show_maps = {
93         \ '<C-P>':      ':call <SID>NM_cmd_show_prev()<CR>',
94         \ '<C-N>':      ':call <SID>NM_cmd_show_next()<CR>',
95         \ 'b':          ':call <SID>NM_cmd_show_fold_toggle(''b'', ''bdy'', !g:notmuch_show_fold_bodies)<CR>',
96         \ 'c':          ':call <SID>NM_cmd_show_fold_toggle(''c'', ''cit'', !g:notmuch_show_fold_citations)<CR>',
97         \ 'h':          ':call <SID>NM_cmd_show_fold_toggle(''h'', ''hdr'', !g:notmuch_show_fold_headers)<CR>',
98         \ 's':          ':call <SID>NM_cmd_show_fold_toggle(''s'', ''sig'', !g:notmuch_show_fold_signatures)<CR>',
99         \ 'q':          ':call <SID>NM_kill_buffer()<CR>',
100         \ }
101
102 " --- implement search screen {{{1
103
104 function! s:NM_cmd_search(words)
105         let cmd = ['search']
106         if g:notmuch_search_newest_first
107                 let cmd = cmd + ['--sort=newest-first']
108         else
109                 let cmd = cmd + ['--sort=oldest-first']
110         endif
111         let data = s:NM_run(cmd + a:words)
112         "let data = substitute(data, '27/27', '25/27', '')
113         "let data = substitute(data, '\[4/4\]', '[0/4]', '')
114         let lines = split(data, "\n")
115         let disp = copy(lines)
116         "call map(disp, 'substitute(v:val, "^thread:\\S* ", "", "")' )
117         call map(disp, 's:NM_cmd_search_fmtline(v:val)')
118
119         call <SID>NM_newBuffer('search', join(disp, "\n"))
120         let b:nm_raw_lines = lines
121         let b:nm_search_words = a:words
122
123         call <SID>NM_cmd_search_mksyntax()
124         call <SID>NM_set_map(g:notmuch_search_maps)
125         setlocal cursorline
126         setlocal nowrap
127 endfunction
128 function! s:NM_cmd_search_fmtline(line)
129         let m = matchlist(a:line, '^\(thread:\S\+\)\s\([^]]\+\]\) \([^;]\+\); \(.*\) (\([^(]*\))$')
130         if !len(m)
131                 return 'ERROR PARSING: ' . a:line
132         endif
133         let max = g:notmuch_search_from_column_width
134         let from = m[3]
135         if strlen(from) >= max
136                 let from = m[3][0:max-4] . '...'
137         endif
138         return printf('%s %-20s | %s (%s)', m[2], from, m[4], m[5])
139 endfunction
140 function! s:NM_cmd_search_mksyntax()
141         syntax clear nmSearchFrom
142         "syntax region nmSearchFrom start='\]\@<=' end='.'me=e+5,he=e+5,re=e+5 oneline contained
143         "syntax match nmSearchFrom /\]\@<=.\{10\}/ oneline contained
144         exec printf('syntax match nmSearchFrom /\(\] \)\@<=.\{%d\}/ oneline contained', g:notmuch_search_from_column_width)
145         "exec printf('syntax region nmSearchFrom start=''\%%%dv'' end=''\%%%dv'' oneline contained', 20, 30)
146 endfunction
147
148 " --- --- search screen action functions {{{2
149
150 function! s:NM_search_show_thread()
151         let id = <SID>NM_search_find_thread_id()
152         if id != ''
153                 call <SID>NM_cmd_show([id])
154         endif
155 endfunction
156
157 function! s:NM_search_prompt()
158         " TODO: input() can support completion
159         let text = input('NotMuch Search: ')
160         if strlen(text)
161                 let tags = split(text)
162         else
163                 let tags = s:notmuch_initial_search_words_defaults
164         endif
165         setlocal bufhidden=delete
166         call <SID>NM_cmd_search(tags)
167 endfunction
168
169 function! s:NM_search_edit()
170         " TODO: input() can support completion
171         let text = input('NotMuch Search: ', join(b:nm_search_words, ' '))
172         if strlen(text)
173                 call <SID>NM_cmd_search(split(text))
174         endif
175 endfunction
176
177 function! s:NM_search_archive_thread()
178         call <SID>NM_add_remove_tags('-', ['inbox'])
179         " TODO: this could be made better and more generic
180         setlocal modifiable
181         s/(\([^)]*\)\<inbox\>\([^)]*\))$/(\1\2)/
182         setlocal nomodifiable
183         norm j
184 endfunction
185
186 function! s:NM_search_filter()
187         call <SID>NM_search_filter_helper('Filter: ', '')
188 endfunction
189
190 function! s:NM_search_filter_by_tag()
191         call <SID>NM_search_filter_helper('Filter Tag(s): ', 'tag:')
192 endfunction
193
194 function! s:NM_search_filter_helper(prompt, prefix)
195         " TODO: input() can support completion
196         let text = input(a:prompt)
197         if !strlen(text)
198                 return
199         endif
200
201         let tags = split(text)
202         map(tags, 'and a:prefix . v:val')
203         let tags = b:nm_search_words + tags
204         echo tags
205
206         let prev_bufnr = bufnr('%')
207         setlocal bufhidden=hide
208         call <SID>NM_cmd_search(tags)
209         setlocal bufhidden=delete
210         let b:nm_prev_bufnr = prev_bufnr
211 endfunction
212
213 function! s:NM_new_mail()
214         echoe 'Not implemented'
215 endfunction
216
217 function! s:NM_search_toggle_order()
218         let g:notmuch_search_newest_first = !g:notmuch_search_newest_first
219         " FIXME: maybe this would be better done w/o reading re-reading the lines
220         "         reversing the b:nm_raw_lines and the buffer lines would be better
221         call <SID>NM_search_refresh_view()
222 endfunction
223
224 function! s:NM_search_reply_to_thread()
225         echoe 'Not implemented'
226 endfunction
227
228 function! s:NM_search_add_tags(tags)
229         call <SID>NM_search_add_remove_tags('Add Tag(s): ', '+', a:tags)
230 endfunction
231
232 function! s:NM_search_remove_tags(tags)
233         call <SID>NM_search_add_remove_tags('Remove Tag(s): ', '-', a:tags)
234 endfunction
235
236 function! s:NM_search_refresh_view()
237         let lno = line('.')
238         setlocal bufhidden=delete
239         call <SID>NM_cmd_search(b:nm_search_words)
240         " FIXME: should find the line of the thread we were on if possible
241         exec printf('norm %dG', lno)
242 endfunction
243
244 " --- --- search screen helper functions {{{2
245
246 function! s:NM_search_find_thread_id()
247         if !exists('b:nm_raw_lines')
248                 echoe 'no b:nm_raw_lines'
249                 return ''
250         else
251                 let line = line('.')
252                 let info = b:nm_raw_lines[line-1]
253                 let what = split(info, '\s\+')[0]
254                 return what
255         endif
256 endfunction
257
258 function! s:NM_search_add_remove_tags(prompt, prefix, intags)
259         if type(a:intags) != type([]) || len(a:intags) == 0
260                 " TODO: input() can support completion
261                 let text = input(a:prompt)
262                 if !strlen(text)
263                         return
264                 endif
265                 call <SID>NM_add_remove_tags(a:prefix, split(text, ' '))
266         else
267                 call <SID>NM_add_remove_tags(a:prefix, a:intags)
268         endif
269         call <SID>NM_search_refresh_view()
270 endfunction
271
272 function! s:NM_add_remove_tags(prefix, tags)
273         let id = <SID>NM_search_find_thread_id()
274         if id == ''
275                 echoe 'Eeek! I couldn''t find the thead id!'
276         endif
277         call map(a:tags, 'a:prefix . v:val')
278         " TODO: handle errors
279         call <SID>NM_run(['tag'] + a:tags + ['--', id])
280 endfunction
281
282 " --- implement show screen {{{1
283
284 function! s:NM_cmd_show(words)
285         let prev_bufnr = bufnr('%')
286         let data = s:NM_run(['show'] + a:words)
287         let lines = split(data, "\n")
288
289         let info = s:NM_cmd_show_parse(lines)
290
291         setlocal bufhidden=hide
292         call <SID>NM_newBuffer('show', join(info['disp'], "\n"))
293         setlocal bufhidden=delete
294         let b:nm_raw_info = info
295         let b:nm_prev_bufnr = prev_bufnr
296
297         call <SID>NM_cmd_show_mkfolds()
298         call <SID>NM_cmd_show_mksyntax()
299         call <SID>NM_set_map(g:notmuch_show_maps)
300         setlocal foldtext=NM_cmd_show_foldtext()
301         setlocal fillchars=
302         setlocal foldcolumn=6
303
304 endfunction
305
306 function! s:NM_kill_buffer()
307         if exists('b:nm_prev_bufnr')
308                 setlocal bufhidden=delete
309                 exec printf(":buffer %d", b:nm_prev_bufnr)
310         else
311                 echo "Nothing to kill."
312         endif
313 endfunction
314
315 function! s:NM_cmd_show_prev()
316         echoe "not implemented"
317 endfunction
318
319 function! s:NM_cmd_show_next()
320         let info = b:nm_raw_info
321         let lnum = line('.')
322         let cnt = 0
323         for msg in info['msgs']
324                 let cnt = cnt + 1
325                 if lnum >= msg['start']
326                         continue
327                 endif
328
329                 exec printf('norm %dG', msg['start'])
330                 norm zz
331                 return
332         endfor
333         norm qj
334         call <SID>NM_search_show_thread()
335 endfunction
336
337 function! s:NM_cmd_show_fold_toggle(key, type, fold)
338         let info = b:nm_raw_info
339         let act = 'open'
340         if a:fold
341                 let act = 'close'
342         endif
343         for fld in info['folds']
344                 if fld[0] == a:type
345                         exec printf('%dfold%s', fld[1], act)
346                 endif
347         endfor
348         exec printf('nnoremap <buffer> %s :call <SID>NM_cmd_show_fold_toggle(''%s'', ''%s'', %d)<CR>', a:key, a:key, a:type, !a:fold)
349 endfunction
350
351
352 " s:NM_cmd_show_parse returns the following dictionary:
353 "    'disp':     lines to display
354 "    'msgs':     message info dicts { start, end, id, depth, filename, descr, header }
355 "    'folds':    fold info arrays [ type, start, end ]
356 "    'foldtext': fold text indexed by start line
357 function! s:NM_cmd_show_parse(inlines)
358         let info = { 'disp': [],       
359                    \ 'msgs': [],       
360                    \ 'folds': [],      
361                    \ 'foldtext': {} }  
362         let msg = {}
363         let hdr = {}
364
365         let in_message = 0
366         let in_header = 0
367         let in_body = 0
368         let in_part = ''
369
370         let body_start = -1
371         let part_start = -1
372
373         let mode_type = ''
374         let mode_start = -1
375
376         let inlnum = 0
377         for line in a:inlines
378                 let inlnum = inlnum + 1
379                 let foldinfo = []
380
381                 if strlen(in_part)
382                         let part_end = 0
383
384                         if match(line, g:notmuch_show_part_end_regexp) != -1
385                                 let part_end = len(info['disp'])
386                         else
387                                 call add(info['disp'], line)
388                         endif
389
390                         if in_part == 'text/plain'
391                                 if !part_end && mode_type == ''
392                                         if match(line, g:notmuch_show_signature_regexp) != -1
393                                                 let mode_type = 'sig'
394                                                 let mode_start = len(info['disp'])
395                                         elseif match(line, g:notmuch_show_citation_regexp) != -1
396                                                 let mode_type = 'cit'
397                                                 let mode_start = len(info['disp'])
398                                         endif
399                                 elseif mode_type == 'cit'
400                                         if part_end || match(line, g:notmuch_show_citation_regexp) == -1
401                                                 let outlnum = len(info['disp'])
402                                                 let foldinfo = [ mode_type, mode_start, outlnum,
403                                                                \ printf('[ %d-line citation.  Press "c" to show. ]', outlnum - mode_start) ]
404                                                 let mode_type = ''
405                                         endif
406                                 elseif mode_type == 'sig'
407                                         let outlnum = len(info['disp'])
408                                         if (outlnum - mode_start) > g:notmuch_show_signature_lines_max
409                                                 let mode_type = ''
410                                         elseif part_end
411                                                 let foldinfo = [ mode_type, mode_start, outlnum,
412                                                                \ printf('[ %d-line signature.  Press "s" to show. ]', outlnum - mode_start) ]
413                                                 let mode_type = ''
414                                         endif
415                                 endif
416                         endif
417
418                         if part_end
419                                 " FIXME: this is a hack for handling two folds being added for one line
420                                 "         we should handle addinga fold in a function
421                                 if len(foldinfo)
422                                         call add(info['folds'], foldinfo[0:2])
423                                         let info['foldtext'][foldinfo[1]] = foldinfo[3]
424                                 endif
425
426                                 let foldinfo = [ 'text', part_start, part_end,
427                                                \ printf('[ %d-line %s.  Press "p" to show. ]', part_end - part_start, in_part) ]
428                                 let in_part = ''
429                                 call add(info['disp'], '')
430                         endif
431
432                 elseif in_body
433                         if !has_key(msg,'body_start')
434                                 let msg['body_start'] = len(info['disp']) + 1
435                         endif
436                         if match(line, g:notmuch_show_body_end_regexp) != -1
437                                 let body_end = len(info['disp'])
438                                 let foldinfo = [ 'bdy', body_start, body_end,
439                                                \ printf('[ BODY %d - %d lines ]', len(info['msgs']), body_end - body_start) ]
440
441                                 let in_body = 0
442
443                         elseif match(line, g:notmuch_show_part_begin_regexp) != -1
444                                 let m = matchlist(line, 'ID: \(\d\+\), Content-type: \(\S\+\)')
445                                 let in_part = 'unknown'
446                                 if len(m)
447                                         let in_part = m[2]
448                                 endif
449                                 call add(info['disp'],
450                                          \ printf('--- %s ---', in_part))
451                                 let part_start = len(info['disp']) + 1
452                         endif
453
454                 elseif in_header
455                         if in_header == 1
456                                 let msg['descr'] = line
457                                 call add(info['disp'], line)
458                                 let in_header = 2
459                                 let msg['hdr_start'] = len(info['disp']) + 1
460
461                         else
462                                 if match(line, g:notmuch_show_header_end_regexp) != -1
463                                         let hdr_start = msg['hdr_start']+1
464                                         let hdr_end = len(info['disp'])
465                                         let foldinfo = [ 'hdr', hdr_start, hdr_end,
466                                                \ printf('[ %d-line headers.  Press "h" to show. ]', hdr_end - hdr_start) ]
467                                         let msg['header'] = hdr
468                                         let in_header = 0
469                                         let hdr = {}
470                                 else
471                                         let m = matchlist(line, '^\(\w\+\):\s*\(.*\)$')
472                                         if len(m)
473                                                 let hdr[m[1]] = m[2]
474                                                 if match(g:notmuch_show_headers, m[1]) != -1
475                                                         call add(info['disp'], line)
476                                                 endif
477                                         endif
478                                 endif
479                         endif
480
481                 elseif in_message
482                         if match(line, g:notmuch_show_message_end_regexp) != -1
483                                 let msg['end'] = len(info['disp'])
484                                 call add(info['disp'], '')
485
486                                 let foldinfo = [ 'msg', msg['start'], msg['end'],
487                                                \ printf('[ MSG %d - %s ]', len(info['msgs']), msg['descr']) ]
488
489                                 call add(info['msgs'], msg)
490                                 let msg = {}
491                                 let in_message = 0
492                                 let in_header = 0
493                                 let in_body = 0
494                                 let in_part = ''
495
496                         elseif match(line, g:notmuch_show_header_begin_regexp) != -1
497                                 let in_header = 1
498                                 continue
499
500                         elseif match(line, g:notmuch_show_body_begin_regexp) != -1
501                                 let body_start = len(info['disp']) + 1
502                                 let in_body = 1
503                                 continue
504                         endif
505
506                 else
507                         if match(line, g:notmuch_show_message_begin_regexp) != -1
508                                 let msg['start'] = len(info['disp']) + 1
509
510                                 let m = matchlist(line, g:notmuch_show_message_parse_regexp)
511                                 if len(m)
512                                         let msg['id'] = m[1]
513                                         let msg['depth'] = m[2]
514                                         let msg['filename'] = m[3]
515                                 endif
516
517                                 let in_message = 1
518                         endif
519                 endif
520
521                 if len(foldinfo)
522                         call add(info['folds'], foldinfo[0:2])
523                         let info['foldtext'][foldinfo[1]] = foldinfo[3]
524                 endif
525         endfor
526         return info
527 endfunction
528
529 function! s:NM_cmd_show_mkfolds()
530         let info = b:nm_raw_info
531
532         for afold in info['folds']
533                 exec printf('%d,%dfold', afold[1], afold[2])
534                 if (afold[0] == 'sig' && g:notmuch_show_fold_signatures)
535                  \ || (afold[0] == 'cit' && g:notmuch_show_fold_citations)
536                  \ || (afold[0] == 'bdy' && g:notmuch_show_fold_bodies)
537                  \ || (afold[0] == 'hdr' && g:notmuch_show_fold_headers)
538                         exec printf('%dfoldclose', afold[1])
539                 else
540                         exec printf('%dfoldopen', afold[1])
541                 endif
542         endfor
543 endfunction
544
545 function! s:NM_cmd_show_mksyntax()
546         let info = b:nm_raw_info
547         let cnt = 0
548         for msg in info['msgs']
549                 let cnt = cnt + 1
550                 let start = msg['start']
551                 let hdr_start = msg['hdr_start']
552                 let body_start = msg['body_start']
553                 let end = msg['end']
554                 exec printf('syntax region nmShowMsg%dDesc start=''\%%%dl'' end=''\%%%dl'' contains=@nmShowMsgDesc', cnt, start, start+1)
555                 exec printf('syntax region nmShowMsg%dHead start=''\%%%dl'' end=''\%%%dl'' contains=@nmShowMsgHead', cnt, hdr_start, body_start)
556                 exec printf('syntax region nmShowMsg%dBody start=''\%%%dl'' end=''\%%%dl'' contains=@nmShowMsgBody', cnt, body_start, end)
557         endfor
558 endfunction
559
560 function! NM_cmd_show_foldtext()
561         let foldtext = b:nm_raw_info['foldtext']
562         return foldtext[v:foldstart]
563 endfunction
564
565
566 " --- notmuch helper functions {{{1
567
568 function! s:NM_newBuffer(ft, content)
569         enew
570         setlocal buftype=nofile readonly modifiable
571         silent put=a:content
572         keepjumps 0d
573         setlocal nomodifiable
574         execute printf('set filetype=notmuch-%s', a:ft)
575         execute printf('set syntax=notmuch-%s', a:ft)
576 endfunction
577
578 function! s:NM_run(args)
579         let cmd = g:notmuch_cmd . ' ' . join(a:args) . '< /dev/null'
580         let out = system(cmd)
581         if v:shell_error
582                 echohl Error
583                 echo substitute(out, '\n*$', '', '')
584                 echohl None
585                 return ''
586         else
587                 return out
588         endif
589 endfunction
590
591 " --- process and set the defaults {{{1
592
593 function! NM_set_defaults(force)
594         for [key, dflt] in items(s:notmuch_defaults)
595                 let cmd = ''
596                 if !a:force && exists(key) && type(dflt) == type(eval(key))
597                         continue
598                 elseif type(dflt) == type(0)
599                         let cmd = printf('let %s = %d', key, dflt)
600                 elseif type(dflt) == type('')
601                         let cmd = printf('let %s = ''%s''', key, dflt)
602                 "elseif type(dflt) == type([])
603                 "        let cmd = printf('let %s = %s', key, string(dflt))
604                 else
605                         echoe printf('E: Unknown type in NM_set_defaults(%d) using [%s,%s]',
606                                                 \ a:force, key, string(dflt))
607                         continue
608                 endif
609                 exec cmd
610         endfor
611 endfunction
612 call NM_set_defaults(0)
613
614 " for some reason NM_set_defaults() didn't work for arrays...
615 if !exists('g:notmuch_show_headers')
616         let g:notmuch_show_headers = s:notmuch_show_headers_defaults
617 endif
618 if !exists('g:notmuch_initial_search_words')
619         let g:notmuch_initial_search_words = s:notmuch_initial_search_words_defaults
620 endif
621
622
623 " --- assign keymaps {{{1
624
625 function! s:NM_set_map(maps)
626         for [key, code] in items(a:maps)
627                 exec printf('nnoremap <buffer> %s %s', key, code)
628         endfor
629 endfunction
630
631 " --- command handler {{{1
632
633 function! NotMuch(args)
634         if !strlen(a:args)
635                 if exists('b:nm_search_words')
636                         let words = b:nm_search_words
637                 else
638                         let words = g:notmuch_initial_search_words
639                 endif
640                 call <SID>NM_cmd_search(words)
641                 return
642         endif
643
644         echo "blarg!"
645
646         let words = split(a:args)
647         " TODO: handle commands passed as arguments
648 endfunction
649 function! CompleteNotMuch(arg_lead, cmd_line, cursor_pos)
650         return []
651 endfunction
652
653
654 " --- glue {{{1
655
656 command! -nargs=* -complete=customlist,CompleteNotMuch NotMuch call NotMuch(<q-args>)
657 cabbrev  notmuch <c-r>=(getcmdtype()==':' && getcmdpos()==1 ? 'NotMuch' : 'notmuch')<CR>
658
659 " --- hacks, only for development :) {{{1
660
661 nnoremap ,nmr :source ~/.vim/plugin/notmuch.vim<CR>:call NotMuch('')<CR>
662
663 " vim: set ft=vim ts=8 sw=8 et foldmethod=marker :