+" --- external mail handling helpers {{{1
+
+function! s:NM_new_mail()
+ call <SID>NM_cmd_compose([], [])
+endfunction
+
+" --- tag manipulation helpers {{{1
+
+" used to combine an array of words with prefixes and separators
+" example:
+" NM_combine_tags('tag:', ['one', 'two', 'three'], 'OR', '()')
+" -> ['(', 'tag:one', 'OR', 'tag:two', 'OR', 'tag:three', ')']
+function! s:NM_combine_tags(word_prefix, words, separator, brackets)
+ let res = []
+ for word in a:words
+ if len(res) && strlen(a:separator)
+ call add(res, a:separator)
+ endif
+ call add(res, a:word_prefix . word)
+ endfor
+ if len(res) > 1 && strlen(a:brackets)
+ if strlen(a:brackets) != 2
+ throw 'Eeek! brackets arg to NM_combine_tags must be 2 chars'
+ endif
+ call insert(res, a:brackets[0])
+ call add(res, a:brackets[1])
+ endif
+ return res
+endfunction
+
+" --- other helpers {{{1
+
+function! s:NM_get_search_words()
+ if !exists('b:nm_search_words')
+ throw 'Eeek! no b:nm_search_words'
+ endif
+ return b:nm_search_words
+endfunction
+
+function! s:NM_kill_this_buffer()
+ if exists('b:nm_prev_bufnr')
+ let prev_bufnr = b:nm_prev_bufnr
+ bdelete!
+ exec printf("buffer %d", prev_bufnr)
+ else
+ echo "This is the last buffer; use :q<CR> to quit."
+ endif
+endfunction
+
+function! s:NM_search_expand(arg)
+ let word = expand(a:arg)
+ let prev_bufnr = bufnr('%')
+ setlocal bufhidden=hide
+ call <SID>NM_cmd_search([word])
+ setlocal bufhidden=delete
+ let b:nm_prev_bufnr = prev_bufnr
+endfunction
+
+function! s:NM_tag(filter, tags)
+ let filter = len(a:filter) ? a:filter : [<SID>NM_search_thread_id()]
+ if !len(filter)
+ throw 'Eeek! I couldn''t find the thead id!'
+ endif
+ let args = ['tag']
+ call extend(args, a:tags)
+ call add(args, '--')
+ call extend(args, filter)
+ " TODO: handle errors
+ call <SID>NM_run(args)
+endfunction
+
+" --- process and set the defaults {{{1
+
+function! NM_set_defaults(force)
+ for [key, dflt] in items(s:notmuch_defaults)
+ let cmd = ''
+ if !a:force && exists(key) && type(dflt) == type(eval(key))
+ continue
+ elseif type(dflt) == type(0)
+ let cmd = printf('let %s = %d', key, dflt)
+ elseif type(dflt) == type('')
+ let cmd = printf('let %s = ''%s''', key, dflt)
+ " FIXME: not sure why this didn't work when dflt is an array
+ "elseif type(dflt) == type([])
+ " let cmd = printf('let %s = %s', key, string(dflt))
+ else
+ echoe printf('E: Unknown type in NM_set_defaults(%d) using [%s,%s]',
+ \ a:force, key, string(dflt))
+ continue
+ endif
+ exec cmd
+ endfor
+endfunction
+call NM_set_defaults(0)
+
+" for some reason NM_set_defaults() didn't work for arrays...
+if !exists('g:notmuch_show_headers')
+ let g:notmuch_show_headers = s:notmuch_show_headers_defaults
+endif
+if !exists('g:notmuch_initial_search_words')
+ let g:notmuch_initial_search_words = s:notmuch_initial_search_words_defaults
+endif
+if !exists('g:notmuch_folders')
+ let g:notmuch_folders = s:notmuch_folders_defaults
+endif
+
+if !exists('g:notmuch_signature')
+ let g:notmuch_signature = s:notmuch_signature_defaults
+endif
+if !exists('g:notmuch_compose_headers')
+ let g:notmuch_compose_headers = s:notmuch_compose_headers_defaults
+endif
+
+" --- assign keymaps {{{1
+
+function! s:NM_set_map(type, maps)
+ nmapclear
+ for [key, code] in items(a:maps)
+ exec printf('%snoremap <buffer> %s %s', a:type, key, code)
+ endfor
+ " --- this is a hack for development :)
+ nnoremap ,nmr :runtime! plugin/notmuch.vim<CR>
+endfunction