All patches and comments are welcome. Please squash your changes to logical
commits before using git-format-patch and git-send-email to
patches@git.madduck.net.
If you'd read over the Git project's submission guidelines and adhered to them,
I'd be especially grateful.
2 " Summary: functions and mode mappings for querying lbdb from Vim
3 " Copyright: Copyright (C) 2007 Stefano Zacchiroli <zack@bononia.it>
4 " License: GNU GPL version 3 or above
5 " Maintainer: Stefano Zacchiroli <zack@bononia.it>
6 " URL: http://www.vim.org/scripts/script.php?script_id=1757
9 if exists("loaded_lbdbq")
14 " queries lbdb with a query string and return a list of pairs:
15 " [['full name', 'email'], ['full name', 'email'], ...]
16 function! LbdbQuery(qstring)
17 let output = system("lbdbq '" . a:qstring . "'")
19 for line in split(output, "\n")[1:] " skip first line (lbdbq summary)
20 let fields = split(line, "\t")
21 let results += [ [fields[1], fields[0]] ]
26 " check if a potential query string has already been expanded in a complete
27 " recipient. E.g.: 'Stefano Zacchiroli <zack@bononia.it>' is a complete
28 " recipient, 'stefano zacchiroli' and 'stefano' are not
29 function! LbdbIsExpanded(qstring)
30 return (a:qstring =~ '^\S\+@\S\+$\|<\S\+@\S\+>$')
34 return substitute(a:s, '^\s*\(.\{-}\)\s*$', '\1', '')
37 " expand a (short) contact given as a query string, asking interactively if
38 " disambiguation is needed
39 " E.g.: 'stefano zacchiroli' -> 'Stefano Zacchiroli <zack@bononia.it>'
40 function! LbdbExpandContact(qstring)
41 let qstring = LbdbTrim(a:qstring)
42 if LbdbIsExpanded(qstring)
44 else " try to expand (short) contact
45 let contacts = LbdbQuery(qstring)
47 if empty(contacts) " no matching (long) contact found
49 elseif len(contacts) > 1 " multiple matches: disambiguate
50 echo "Choose a recipient for '" . qstring . "':"
53 for contact in contacts
54 let choices += [ printf("%2d. %s <%s>", counter, contact[0], contact[1]) ]
57 let contact = contacts[inputlist(choices)]
58 else " len(contacts) == 1, i.e. one single match
59 let contact = contacts[0]
61 return printf("\"%s\" <%s>", escape(contact[0], '"'), contact[1])
65 " as above but support input strings composed by comma separated (short)
67 function! LbdbExpandContacts(raw)
68 let raw = LbdbTrim(a:raw)
69 let qstrings = split(raw, '\s*,\s*')
71 for qstring in qstrings
72 let exp_strings += [ LbdbExpandContact(qstring) ]
74 return join(exp_strings, ', ')
77 " expand all (short) contacts on a given recipient line, asking interactively
78 " if disambiguation is needed.
80 " 'To: stefano zacchiroli, bram'
81 " -> 'To: Stefano Zacchiroli <zack@bononia.it>, Bram Moolenaar <Bram@moolenaar.net>
82 function! LbdbExpandRcptLine(recpt_line)
83 if a:recpt_line =~ '^\w\+:' " line is the *beginning* of a RFC822 field
84 let raw = substitute(a:recpt_line, '^\w\+:\s*', '', '')
85 let recpt_kind = substitute(a:recpt_line, '^\(\w\+\):\s*.*$', '\1', '')
86 let exp_line = recpt_kind . ': ' . LbdbExpandContacts(raw)
87 elseif a:recpt_line =~ '^\s\+' " line is the *continuation* of a RFC822 field
88 let raw = substitute(a:recpt_line, '^\s\+', '', '')
89 let lpadding = substitute(a:recpt_line, '\S.*$', '', '')
90 let exp_line = lpadding . LbdbExpandContacts(raw)
94 if a:recpt_line =~ ',\s*$'
100 function! LbdbExpandCurLine()
101 call setline(line('.'), LbdbExpandRcptLine(getline('.')))
104 function! LbdbExpandVisual()
105 if visualmode() ==# 'v'
107 let raw = getreg('"')
110 let expanded = LbdbExpandContacts(raw)
112 let expanded = LbdbExpandContact(raw)
114 call setreg('"', expanded)
116 elseif visualmode() ==# 'V'
117 call LbdbExpandCurLine()
121 nmap <silent> <LocalLeader>lb :call LbdbExpandCurLine()<RETURN>
122 vmap <silent> <LocalLeader>lb :call LbdbExpandVisual()<RETURN>
123 imap <silent> <LocalLeader>lb <ESC>:call LbdbExpandCurLine()<RETURN>A