]> git.madduck.net Git - etc/vim.git/blob - .vim/plugin/lbdbq.vim

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

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.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

fix cmdline complation
[etc/vim.git] / .vim / plugin / lbdbq.vim
1 " Name: lbdbq.vim
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
7 " Version: 0.3
8
9 if exists("loaded_lbdbq")
10     finish
11 endif
12 let loaded_lbdbq = 1
13
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 . "'")
18   let results = []
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]] ]
22   endfor
23   return results
24 endfunction
25
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\+>$')
31 endfunction
32
33 function! LbdbTrim(s)
34   return substitute(a:s, '^\s*\(.\{-}\)\s*$', '\1', '')
35 endfunction
36
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)
43     return qstring
44   else  " try to expand (short) contact
45     let contacts = LbdbQuery(qstring)
46     let contact = []
47     if empty(contacts)  " no matching (long) contact found
48       return qstring
49     elseif len(contacts) > 1  " multiple matches: disambiguate
50       echo "Choose a recipient for '" . qstring . "':"
51       let choices = []
52       let counter = 0
53       for contact in contacts
54         let choices += [ printf("%2d. %s <%s>", counter, contact[0], contact[1]) ]
55         let counter += 1
56       endfor
57       let contact = contacts[inputlist(choices)]
58     else  " len(contacts) == 1, i.e. one single match
59       let contact = contacts[0]
60     endif
61     return printf("\"%s\" <%s>", escape(contact[0], '"'), contact[1])
62   endif
63 endfunction
64
65 " as above but support input strings composed by comma separated (short)
66 " contacts
67 function! LbdbExpandContacts(raw)
68   let raw = LbdbTrim(a:raw)
69   let qstrings = split(raw, '\s*,\s*')
70   let exp_strings = []
71   for qstring in qstrings
72     let exp_strings += [ LbdbExpandContact(qstring) ]
73   endfor
74   return join(exp_strings, ', ')
75 endfunction
76
77 " expand all (short) contacts on a given recipient line, asking interactively
78 " if disambiguation is needed.
79 " E.g.:
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)
91   else
92     return a:recpt_line
93   endif
94   if a:recpt_line =~ ',\s*$'
95     let exp_line .= ','
96   endif
97   return exp_line
98 endfunction
99
100 function! LbdbExpandCurLine()
101   call setline(line('.'), LbdbExpandRcptLine(getline('.')))
102 endfunction
103
104 function! LbdbExpandVisual()
105   if visualmode() ==# 'v'
106     normal gvy
107     let raw = getreg('"')
108     let expanded = ''
109     if raw =~ ","
110       let expanded = LbdbExpandContacts(raw)
111     else
112       let expanded = LbdbExpandContact(raw)
113     endif
114     call setreg('"', expanded)
115     normal gvP
116   elseif visualmode() ==# 'V'
117     call LbdbExpandCurLine()
118   end
119 endfunction
120
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
124