|
@@ -0,0 +1,242 @@
|
|
1
|
+" Query Command Complete
|
|
2
|
+" ======================
|
|
3
|
+"
|
|
4
|
+" Vim plugin to suggest completions with the results of an external
|
|
5
|
+" query command.
|
|
6
|
+"
|
|
7
|
+" The original intention is to use it as a mutt query_command wrapper
|
|
8
|
+" to complete addresses in the mail headers, but it can be adapted
|
|
9
|
+" to any other kind of functionality by modifying the exposed setting
|
|
10
|
+" parameters.
|
|
11
|
+"
|
|
12
|
+" Last Change: 2013 Jun 16
|
|
13
|
+" Author: Caio Romão (http://caioromao.com)
|
|
14
|
+" License: This file is placed in the public domain
|
|
15
|
+" Contributors:
|
|
16
|
+" Brian Henderson https://github.com/bhenderson
|
|
17
|
+" Mark Stillwell https://github.com/marklee77
|
|
18
|
+" Rui Abreu Ferreira http://ruiabreu.org
|
|
19
|
+"
|
|
20
|
+" Setup:
|
|
21
|
+" This plugin exports the completion function QueryCommandComplete,
|
|
22
|
+" which can be set as the complete function (or omni function) for
|
|
23
|
+" any filetype. If you have a working mutt setup with query_command
|
|
24
|
+" configured, the plugin works out of the box.
|
|
25
|
+"
|
|
26
|
+" Example:
|
|
27
|
+" let g:qcc_query_command='abook'
|
|
28
|
+" au BufRead /tmp/mutt* setlocal omnifunc=QueryCommandComplete
|
|
29
|
+"
|
|
30
|
+" Settings:
|
|
31
|
+" Note: Overriding settings on a buffer-basis is supported. So
|
|
32
|
+" b:qcc_query_command takes precedence over g:qcc_query_command
|
|
33
|
+"
|
|
34
|
+" g:qcc_query_command
|
|
35
|
+" External command that queries for contacts
|
|
36
|
+" If empty, QueryCommandComplete tries to guess what command to
|
|
37
|
+" run by executing `mutt -Q query_command`.
|
|
38
|
+"
|
|
39
|
+" g:qcc_line_separator
|
|
40
|
+" Separator for each entry in the result from the query
|
|
41
|
+" default: '\n'
|
|
42
|
+"
|
|
43
|
+" g:qcc_field_separator
|
|
44
|
+" Separator for the fields of an entry from the result
|
|
45
|
+" default: '\t'
|
|
46
|
+"
|
|
47
|
+" g:qcc_pattern
|
|
48
|
+" Pattern used to match against the current line to decide
|
|
49
|
+" whether to call the query command
|
|
50
|
+" default: '^\(To\|Cc\|Bcc\|From\|Reply-To\):'
|
|
51
|
+"
|
|
52
|
+" g:qcc_multiline
|
|
53
|
+" Whether to try matching g:qcc_pattern against the current
|
|
54
|
+" and any previous line
|
|
55
|
+" default: 0
|
|
56
|
+"
|
|
57
|
+" g:qcc_multiline_pattern
|
|
58
|
+" Pattern to match against the current line when deciding
|
|
59
|
+" wether to keep looking for a line that matches g:qcc_pattern
|
|
60
|
+" This provides finer control over the recursion, which
|
|
61
|
+" is useful if calling the completion on really big files.
|
|
62
|
+" default: '.*'
|
|
63
|
+"
|
|
64
|
+" g:qcc_format_word
|
|
65
|
+" Format string to be used when building the word field
|
|
66
|
+" of the completion (i.e.: the final result, what gets fed into
|
|
67
|
+" the current line when you select an option)
|
|
68
|
+" default: '${1} <${0}>' (as in: FirstName <email@domain.com>)
|
|
69
|
+"
|
|
70
|
+" g:qcc_format_abbr
|
|
71
|
+" Format string to be used when building the abbreviation
|
|
72
|
+" for the completion menu (i.e.: the first row in the completion
|
|
73
|
+" menu).
|
|
74
|
+" default: '${1}'
|
|
75
|
+"
|
|
76
|
+" g:qcc_format_menu
|
|
77
|
+" Format string for the optional second column of the completion
|
|
78
|
+" menu.
|
|
79
|
+" default: '${2}'
|
|
80
|
+"
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+if exists("g:loaded_QueryCommandComplete") || &cp
|
|
84
|
+ finish
|
|
85
|
+endif
|
|
86
|
+
|
|
87
|
+function! s:GetSetting(name)
|
|
88
|
+ let global_option = 'g:qcc_' . a:name
|
|
89
|
+ let local_option = 'b:qcc_' . a:name
|
|
90
|
+
|
|
91
|
+ let result = ''
|
|
92
|
+ if exists(local_option)
|
|
93
|
+ let result = {local_option}
|
|
94
|
+ elseif exists(global_option)
|
|
95
|
+ let result = {global_option}
|
|
96
|
+ endif
|
|
97
|
+
|
|
98
|
+ return result
|
|
99
|
+endfunction
|
|
100
|
+
|
|
101
|
+let g:loaded_QueryCommandComplete = 1
|
|
102
|
+let s:save_cpo = &cpo
|
|
103
|
+set cpo&vim
|
|
104
|
+
|
|
105
|
+function! s:DefaultIfUnset(name, default)
|
|
106
|
+ if !exists(a:name)
|
|
107
|
+ let {a:name} = a:default
|
|
108
|
+ endif
|
|
109
|
+endfunction
|
|
110
|
+
|
|
111
|
+call s:DefaultIfUnset('g:qcc_line_separator', '\n')
|
|
112
|
+call s:DefaultIfUnset('g:qcc_field_separator', '\t')
|
|
113
|
+call s:DefaultIfUnset('g:qcc_pattern', '^\(To\|Cc\|Bcc\|From\|Reply-To\):')
|
|
114
|
+call s:DefaultIfUnset('g:qcc_multiline', 0)
|
|
115
|
+call s:DefaultIfUnset('g:qcc_multiline_pattern', '.*')
|
|
116
|
+call s:DefaultIfUnset('g:qcc_format_word', '${1} <${0}>')
|
|
117
|
+call s:DefaultIfUnset('g:qcc_format_abbr', '${1}')
|
|
118
|
+call s:DefaultIfUnset('g:qcc_format_menu', '${2}')
|
|
119
|
+
|
|
120
|
+" Given a format string where the placeholders are in the format
|
|
121
|
+" '${index}' and index is a valid index the the given 'fields'
|
|
122
|
+" argument, this function returns a string with all placeholders
|
|
123
|
+" replaced by the corresponding data in the fields list.
|
|
124
|
+" FIXME I can't help but think there's a standard way to do this
|
|
125
|
+" but I failed finding it. Please call me a dumbass if you
|
|
126
|
+" know The Easy Way.
|
|
127
|
+function! s:ApplyFieldsToFormatString(fields, format)
|
|
128
|
+ let result = a:format
|
|
129
|
+
|
|
130
|
+ while 1
|
|
131
|
+ let placeholder = matchstr(result, '${[0-9]}')
|
|
132
|
+
|
|
133
|
+ if (empty(placeholder))
|
|
134
|
+ break
|
|
135
|
+ endif
|
|
136
|
+
|
|
137
|
+ let index = matchstr(placeholder, '[0-9]')
|
|
138
|
+
|
|
139
|
+ " If ${NUMBER} is not a valid index in a:fields,
|
|
140
|
+ " use '' as a fallback.
|
|
141
|
+ " FIXME Decide whether to warn/err/whatever here
|
|
142
|
+ let content = ''
|
|
143
|
+ if (len(a:fields) > index)
|
|
144
|
+ let content = a:fields[index]
|
|
145
|
+ endif
|
|
146
|
+
|
|
147
|
+ let result = substitute(result, placeholder, content, 'g')
|
|
148
|
+
|
|
149
|
+ endwhile
|
|
150
|
+
|
|
151
|
+ return result
|
|
152
|
+endfunction
|
|
153
|
+
|
|
154
|
+function! s:MakeCompletionEntry(fields)
|
|
155
|
+ let entry = {}
|
|
156
|
+ let entry.word = s:ApplyFieldsToFormatString(a:fields, s:GetSetting('format_word'))
|
|
157
|
+ let entry.abbr = s:ApplyFieldsToFormatString(a:fields, s:GetSetting('format_abbr'))
|
|
158
|
+ let entry.menu = s:ApplyFieldsToFormatString(a:fields, s:GetSetting('format_menu'))
|
|
159
|
+ let entry.icase = 1
|
|
160
|
+ return entry
|
|
161
|
+endfunction
|
|
162
|
+
|
|
163
|
+function! s:FindStartingIndex()
|
|
164
|
+ let cur_line = getline('.')
|
|
165
|
+
|
|
166
|
+ " locate the start of the word
|
|
167
|
+ let start = col('.') - 1
|
|
168
|
+ while start > 0 && cur_line[start - 1] =~ '[^:,]'
|
|
169
|
+ let start -= 1
|
|
170
|
+ endwhile
|
|
171
|
+
|
|
172
|
+ " lstrip()
|
|
173
|
+ while cur_line[start] =~ '[ ]'
|
|
174
|
+ let start += 1
|
|
175
|
+ endwhile
|
|
176
|
+
|
|
177
|
+ return start
|
|
178
|
+endfunction
|
|
179
|
+
|
|
180
|
+function! s:GenerateCompletions(findstart, base)
|
|
181
|
+ if a:findstart
|
|
182
|
+ return s:FindStartingIndex()
|
|
183
|
+ endif
|
|
184
|
+
|
|
185
|
+ let results = []
|
|
186
|
+ let cmd = s:GetSetting('query_command')
|
|
187
|
+ if cmd !~ '%s'
|
|
188
|
+ let cmd .= ' %s'
|
|
189
|
+ endif
|
|
190
|
+ let cmd = substitute(cmd, '%s', shellescape(a:base), '')
|
|
191
|
+ let lines = split(system(cmd), g:qcc_line_separator)
|
|
192
|
+
|
|
193
|
+ for my_line in lines
|
|
194
|
+ let fields = split(my_line, g:qcc_field_separator)
|
|
195
|
+
|
|
196
|
+ let entry = s:MakeCompletionEntry(fields)
|
|
197
|
+
|
|
198
|
+ call add(results, entry)
|
|
199
|
+ endfor
|
|
200
|
+
|
|
201
|
+ return results
|
|
202
|
+endfunction
|
|
203
|
+
|
|
204
|
+function! s:ShouldGenerateCompletions(line_number)
|
|
205
|
+ let current_line = getline(a:line_number)
|
|
206
|
+
|
|
207
|
+ if current_line =~ g:qcc_pattern
|
|
208
|
+ return 1
|
|
209
|
+ endif
|
|
210
|
+
|
|
211
|
+ if ! g:qcc_multiline || a:line_number <= 1 || current_line !~ g:qcc_multiline_pattern
|
|
212
|
+ return 0
|
|
213
|
+ endif
|
|
214
|
+
|
|
215
|
+ return s:ShouldGenerateCompletions(a:line_number - 1)
|
|
216
|
+endfunction
|
|
217
|
+
|
|
218
|
+function! s:CheckSettings()
|
|
219
|
+ " Try to use mutt's query_command by default if nothing is set
|
|
220
|
+ if empty(s:GetSetting('query_command'))
|
|
221
|
+ let s:querycmd = system('mutt -Q query_command 2>/dev/null')
|
|
222
|
+ let s:querycmd = substitute(s:querycmd, '^query_command="\(.*\)"\n', '\1','')
|
|
223
|
+
|
|
224
|
+ if len(s:querycmd)
|
|
225
|
+ let g:qcc_query_command = s:querycmd
|
|
226
|
+ let g:qcc_multiline = 1
|
|
227
|
+ autocmd FileType mail setlocal omnifunc=QueryCommandComplete
|
|
228
|
+ else
|
|
229
|
+ echoerr "QueryCommandComplete: g:qcc_query_command not set!"
|
|
230
|
+ return 0
|
|
231
|
+ endif
|
|
232
|
+ endif
|
|
233
|
+ return 1
|
|
234
|
+endfunction
|
|
235
|
+
|
|
236
|
+function! QueryCommandComplete(findstart, base)
|
|
237
|
+ if s:CheckSettings() && s:ShouldGenerateCompletions(line('.'))
|
|
238
|
+ return s:GenerateCompletions(a:findstart, a:base)
|
|
239
|
+ endif
|
|
240
|
+endfunction
|
|
241
|
+
|
|
242
|
+let &cpo = s:save_cpo
|