Skip to content

Instantly share code, notes, and snippets.

@cavebatsofware
Created October 1, 2025 15:36
Show Gist options
  • Save cavebatsofware/045a75dcec88d25680cfcada96195807 to your computer and use it in GitHub Desktop.
Save cavebatsofware/045a75dcec88d25680cfcada96195807 to your computer and use it in GitHub Desktop.
" Mbox Email Domain Search Functions
" Author: cavebatsofware for mbox email searching
" Usage: :call SearchMboxDomain('example.com', 'top') or :call SearchMboxDomain('example.com', 'bottom')
function! SearchMboxDomain(domain, direction)
" Validate input parameters
if a:domain == ''
echo "Error: Domain cannot be empty"
return
endif
if a:direction != 'top' && a:direction != 'bottom'
echo "Error: Direction must be 'top' or 'bottom'"
return
endif
" Save current cursor position
let l:save_cursor = getpos('.')
" Move to start or end based on direction
if a:direction == 'top'
normal! gg
let l:search_flags = 'W'
echo "Searching from top for emails from: " . a:domain
else
normal! G
let l:search_flags = 'bW'
echo "Searching from bottom for emails from: " . a:domain
endif
" Create search pattern for From header with domain
" This matches "From: " followed by any characters, then @ and the domain
let l:pattern = '^From:.*@' . escape(a:domain, '.')
" Perform the search
let l:found = search(l:pattern, l:search_flags)
if l:found == 0
echo "No emails found from domain: " . a:domain
" Restore cursor position if nothing found
call setpos('.', l:save_cursor)
else
echo "Found email from: " . a:domain . " (line " . line('.') . ")"
" Highlight the current line
normal! V
endif
endfunction
function! SearchMboxDomainNext(domain)
" Search for next occurrence of the domain (forward)
let l:pattern = '^From:.*@' . escape(a:domain, '.')
let l:found = search(l:pattern, 'W')
if l:found == 0
echo "No more emails found from domain: " . a:domain
else
echo "Next email from: " . a:domain . " (line " . line('.') . ")"
normal! V
endif
endfunction
function! SearchMboxDomainPrev(domain)
" Search for previous occurrence of the domain (backward)
let l:pattern = '^From:.*@' . escape(a:domain, '.')
let l:found = search(l:pattern, 'bW')
if l:found == 0
echo "No previous emails found from domain: " . a:domain
else
echo "Previous email from: " . a:domain . " (line " . line('.') . ")"
normal! V
endif
endfunction
function! ShowEmailHeader()
" Show the full header of the current email
" Find the start of current email (From line)
let l:current_line = line('.')
" Search backward for the start of this email
let l:from_line = search('^From ', 'bcnW')
if l:from_line == 0
echo "Not currently in an email"
return
endif
" Search forward for the end of headers (empty line)
let l:header_end = search('^$', 'nW')
if l:header_end == 0
let l:header_end = line('$')
endif
" Display the header range
execute l:from_line . ',' . (l:header_end - 1) . 'print'
endfunction
" Convenience commands
command! -nargs=1 SearchDomainTop call SearchMboxDomain(<q-args>, 'top')
command! -nargs=1 SearchDomainBottom call SearchMboxDomain(<q-args>, 'bottom')
command! -nargs=1 SearchDomainNext call SearchMboxDomainNext(<q-args>)
command! -nargs=1 SearchDomainPrev call SearchMboxDomainPrev(<q-args>)
command! ShowHeader call ShowEmailHeader()
" Key mappings (optional - uncomment to use)
" nnoremap <leader>st :SearchDomainTop
" nnoremap <leader>sb :SearchDomainBottom
" nnoremap <leader>sn :SearchDomainNext
" nnoremap <leader>sp :SearchDomainPrev
" nnoremap <leader>sh :ShowHeader<CR>
" Example usage:
" :call SearchMboxDomain('gmail.com', 'top')
" :call SearchMboxDomain('company.com', 'bottom')
" :SearchDomainTop example.com
" :SearchDomainBottom example.com
" :SearchDomainNext example.com
" :SearchDomainPrev example.com
"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment