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 let filename = expand('%:t:r')
3 if filename == '' | return a:0 == 2 ? a:2 : '' | endif
4 return !a:0 || a:1 == '' ? filename : substitute(a:1, '$1', filename, 'g')
8 unl! g:snipPos s:curPos s:snipLen s:endCol s:endLine s:prevLen
11 unl s:startCol s:origWordLen s:update
12 if exists('s:oldVars') | unl s:oldVars s:oldEndCol | endif
17 fun snipMate#expandSnip(snip, col)
18 let lnum = line('.') | let col = a:col
20 let snippet = s:ProcessSnippet(a:snip)
21 " Avoid error if eval evaluates to nothing
22 if snippet == '' | return '' | endif
24 " Expand snippet onto current position with the tab stops removed
25 let snipLines = split(substitute(snippet, '$\d\+\|${\d\+.\{-}}', '', 'g'), "\n", 1)
27 let line = getline(lnum)
28 let afterCursor = strpart(line, col - 1)
29 " Keep text after the cursor
30 if afterCursor != "\t" && afterCursor != ' '
31 let line = strpart(line, 0, col - 1)
32 let snipLines[-1] .= afterCursor
35 " For some reason the cursor needs to move one right after this
36 if line != '' && col == 1 && &ve != 'all' && &ve != 'onemore'
41 call setline(lnum, line.snipLines[0])
43 " Autoindent snippet according to previous indentation
44 let indent = matchend(line, '^.\{-}\ze\(\S\|$\)') + 1
45 call append(lnum, map(snipLines[1:], "'".strpart(line, 0, indent - 1)."'.v:val"))
47 " Open any folds snippet expands into
48 if &fen | sil! exe lnum.','.(lnum + len(snipLines) - 1).'foldopen' | endif
50 let [g:snipPos, s:snipLen] = s:BuildTabStops(snippet, lnum, col - indent, indent)
54 au CursorMovedI * call s:UpdateChangedSnip(0)
55 au InsertEnter * call s:UpdateChangedSnip(1)
57 let s:lastBuf = bufnr(0) " Only expand snippet while in current buffer
59 let s:endCol = g:snipPos[s:curPos][1]
60 let s:endLine = g:snipPos[s:curPos][0]
62 call cursor(g:snipPos[s:curPos][0], g:snipPos[s:curPos][1])
63 let s:prevLen = [line('$'), col('$')]
64 if g:snipPos[s:curPos][2] != -1 | return s:SelectWord() | endif
66 unl g:snipPos s:snipLen
67 " Place cursor at end of snippet if no tab stop is given
68 let newlines = len(snipLines) - 1
69 call cursor(lnum + newlines, indent + len(snipLines[-1]) - len(afterCursor)
70 \ + (newlines ? 0: col - 1))
75 " Prepare snippet to be processed by s:BuildTabStops
76 fun s:ProcessSnippet(snip)
78 " Evaluate eval (`...`) expressions.
79 " Using a loop here instead of a regex fixes a bug with nested "\=".
80 if stridx(snippet, '`') != -1
81 while match(snippet, '`.\{-}`') != -1
82 let snippet = substitute(snippet, '`.\{-}`',
83 \ substitute(eval(matchstr(snippet, '`\zs.\{-}\ze`')),
84 \ "\n\\%$", '', ''), '')
86 let snippet = substitute(snippet, "\r", "\n", 'g')
89 " Place all text after a colon in a tab stop after the tab stop
90 " (e.g. "${#:foo}" becomes "${:foo}foo").
91 " This helps tell the position of the tab stops later.
92 let snippet = substitute(snippet, '${\d\+:\(.\{-}\)}', '&\1', 'g')
94 " Update the a:snip so that all the $# become the text after
95 " the colon in their associated ${#}.
96 " (e.g. "${1:foo}" turns all "$1"'s into "foo")
98 while stridx(snippet, '${'.i) != -1
99 let s = matchstr(snippet, '${'.i.':\zs.\{-}\ze}')
101 let snippet = substitute(snippet, '$'.i, s.'&', 'g')
106 if &et " Expand tabs to spaces if 'expandtab' is set.
107 return substitute(snippet, '\t', repeat(' ', &sts ? &sts : &sw), 'g')
112 " Counts occurences of haystack in needle
113 fun s:Count(haystack, needle)
115 let index = stridx(a:haystack, a:needle)
117 let index = stridx(a:haystack, a:needle, index+1)
123 " Builds a list of a list of each tab stop in the snippet containing:
124 " 1.) The tab stop's line number.
125 " 2.) The tab stop's column number
126 " (by getting the length of the string between the last "\n" and the
128 " 3.) The length of the text after the colon for the current tab stop
129 " (e.g. "${1:foo}" would return 3). If there is no text, -1 is returned.
130 " 4.) If the "${#:}" construct is given, another list containing all
131 " the matches of "$#", to be replaced with the placeholder. This list is
132 " composed the same way as the parent; the first item is the line number,
133 " and the second is the column.
134 fun s:BuildTabStops(snip, lnum, col, indent)
137 let withoutVars = substitute(a:snip, '$\d\+', '', 'g')
138 while stridx(a:snip, '${'.i) != -1
139 let beforeTabStop = matchstr(withoutVars, '^.*\ze${'.i.'\D')
140 let withoutOthers = substitute(withoutVars, '${\('.i.'\D\)\@!\d\+.\{-}}', '', 'g')
143 call add(snipPos, [0, 0, -1])
144 let snipPos[j][0] = a:lnum + s:Count(beforeTabStop, "\n")
145 let snipPos[j][1] = a:indent + len(matchstr(withoutOthers, '.*\(\n\|^\)\zs.*\ze${'.i.'\D'))
146 if snipPos[j][0] == a:lnum | let snipPos[j][1] += a:col | endif
148 " Get all $# matches in another list, if ${#:name} is given
149 if stridx(withoutVars, '${'.i.':') != -1
150 let snipPos[j][2] = len(matchstr(withoutVars, '${'.i.':\zs.\{-}\ze}'))
151 let dots = repeat('.', snipPos[j][2])
152 call add(snipPos[j], [])
153 let withoutOthers = substitute(a:snip, '${\d\+.\{-}}\|$'.i.'\@!\d\+', '', 'g')
154 while match(withoutOthers, '$'.i.'\(\D\|$\)') != -1
155 let beforeMark = matchstr(withoutOthers, '^.\{-}\ze'.dots.'$'.i.'\(\D\|$\)')
156 call add(snipPos[j][3], [0, 0])
157 let snipPos[j][3][-1][0] = a:lnum + s:Count(beforeMark, "\n")
158 let snipPos[j][3][-1][1] = a:indent + (snipPos[j][3][-1][0] > a:lnum
159 \ ? len(matchstr(beforeMark, '.*\n\zs.*'))
160 \ : a:col + len(beforeMark))
161 let withoutOthers = substitute(withoutOthers, '$'.i.'\ze\(\D\|$\)', '', '')
166 return [snipPos, i - 1]
169 fun snipMate#jumpTabStop(backwards)
170 let leftPlaceholder = exists('s:origWordLen')
171 \ && s:origWordLen != g:snipPos[s:curPos][2]
172 if leftPlaceholder && exists('s:oldEndCol')
173 let startPlaceholder = s:oldEndCol + 1
176 if exists('s:update')
177 call s:UpdatePlaceholderTabStops()
179 call s:UpdateTabStops()
182 " Don't reselect placeholder if it has been modified
183 if leftPlaceholder && g:snipPos[s:curPos][2] != -1
184 if exists('startPlaceholder')
185 let g:snipPos[s:curPos][1] = startPlaceholder
187 let g:snipPos[s:curPos][1] = col('.')
188 let g:snipPos[s:curPos][2] = 0
192 let s:curPos += a:backwards ? -1 : 1
193 " Loop over the snippet when going backwards from the beginning
194 if s:curPos < 0 | let s:curPos = s:snipLen - 1 | endif
196 if s:curPos == s:snipLen
197 let sMode = s:endCol == g:snipPos[s:curPos-1][1]+g:snipPos[s:curPos-1][2]
198 call s:RemoveSnippet()
199 return sMode ? "\<tab>" : TriggerSnippet()
202 call cursor(g:snipPos[s:curPos][0], g:snipPos[s:curPos][1])
204 let s:endLine = g:snipPos[s:curPos][0]
205 let s:endCol = g:snipPos[s:curPos][1]
206 let s:prevLen = [line('$'), col('$')]
208 return g:snipPos[s:curPos][2] == -1 ? '' : s:SelectWord()
211 fun s:UpdatePlaceholderTabStops()
212 let changeLen = s:origWordLen - g:snipPos[s:curPos][2]
213 unl s:startCol s:origWordLen s:update
214 if !exists('s:oldVars') | return | endif
215 " Update tab stops in snippet if text has been added via "$#"
216 " (e.g., in "${1:foo}bar$1${2}").
218 let curLine = line('.')
221 if pos == g:snipPos[s:curPos] | continue | endif
222 let changed = pos[0] == curLine && pos[1] > s:oldEndCol
224 let endPlaceholder = pos[2] - 1 + pos[1]
225 " Subtract changeLen from each tab stop that was after any of
226 " the current tab stop's placeholders.
227 for [lnum, col] in s:oldVars
228 if lnum > pos[0] | break | endif
230 if pos[1] > col || (pos[2] == -1 && pos[1] == col)
232 elseif col < endPlaceholder
237 let pos[1] -= changeLen * changed
238 let pos[2] -= changeLen * changedVars " Parse variables within placeholders
239 " e.g., "${1:foo} ${2:$1bar}"
241 if pos[2] == -1 | continue | endif
242 " Do the same to any placeholders in the other tab stops.
244 let changed = nPos[0] == curLine && nPos[1] > s:oldEndCol
245 for [lnum, col] in s:oldVars
246 if lnum > nPos[0] | break | endif
247 if nPos[0] == lnum && nPos[1] > col
251 let nPos[1] -= changeLen * changed
255 unl s:endCol s:oldVars s:oldEndCol
258 fun s:UpdateTabStops()
259 let changeLine = s:endLine - g:snipPos[s:curPos][0]
260 let changeCol = s:endCol - g:snipPos[s:curPos][1]
261 if exists('s:origWordLen')
262 let changeCol -= s:origWordLen
265 let lnum = g:snipPos[s:curPos][0]
266 let col = g:snipPos[s:curPos][1]
267 " Update the line number of all proceeding tab stops if <cr> has
273 if pos[0] == lnum | let pos[1] += changeCol | endif
274 let pos[0] += changeLine
276 if pos[2] == -1 | continue | endif
279 if nPos[0] == lnum | let nPos[1] += changeCol | endif
280 let nPos[0] += changeLine
284 elseif changeCol != 0
285 " Update the column of all proceeding tab stops if text has
286 " been inserted/deleted in the current line.
288 if pos[1] >= col && pos[0] == lnum
289 let pos[1] += changeCol
291 if pos[2] == -1 | continue | endif
293 if nPos[0] > lnum | break | endif
294 if nPos[0] == lnum && nPos[1] >= col
295 let nPos[1] += changeCol
303 let s:origWordLen = g:snipPos[s:curPos][2]
304 let s:oldWord = strpart(getline('.'), g:snipPos[s:curPos][1] - 1,
306 let s:prevLen[1] -= s:origWordLen
307 if !empty(g:snipPos[s:curPos][3])
310 let s:startCol = g:snipPos[s:curPos][1] - 1
312 if !s:origWordLen | return '' | endif
313 let l = col('.') != 1 ? 'l' : ''
314 if &sel == 'exclusive'
315 return "\<esc>".l.'v'.s:origWordLen."l\<c-g>"
317 return s:origWordLen == 1 ? "\<esc>".l.'gh'
318 \ : "\<esc>".l.'v'.(s:origWordLen - 1)."l\<c-g>"
321 " This updates the snippet as you type when text needs to be inserted
322 " into multiple places (e.g. in "${1:default text}foo$1bar$1",
323 " "default text" would be highlighted, and if the user types something,
324 " UpdateChangedSnip() would be called so that the text after "foo" & "bar"
325 " are updated accordingly)
327 " It also automatically quits the snippet if the cursor is moved out of it
328 " while in insert mode.
329 fun s:UpdateChangedSnip(entering)
330 if exists('g:snipPos') && bufnr(0) != s:lastBuf
331 call s:RemoveSnippet()
332 elseif exists('s:update') " If modifying a placeholder
333 if !exists('s:oldVars') && s:curPos + 1 < s:snipLen
334 " Save the old snippet & word length before it's updated
335 " s:startCol must be saved too, in case text is added
336 " before the snippet (e.g. in "foo$1${2}bar${1:foo}").
337 let s:oldEndCol = s:startCol
338 let s:oldVars = deepcopy(g:snipPos[s:curPos][3])
340 let col = col('.') - 1
343 let changeLen = col('$') - s:prevLen[1]
344 let s:endCol += changeLen
345 else " When being updated the first time, after leaving select mode
346 if a:entering | return | endif
347 let s:endCol = col - 1
350 " If the cursor moves outside the snippet, quit it
351 if line('.') != g:snipPos[s:curPos][0] || col < s:startCol ||
353 unl! s:startCol s:origWordLen s:oldVars s:update
354 return s:RemoveSnippet()
358 let s:prevLen[1] = col('$')
359 elseif exists('g:snipPos')
360 if !a:entering && g:snipPos[s:curPos][2] != -1
361 let g:snipPos[s:curPos][2] = -2
366 let changeLine = line('$') - s:prevLen[0]
369 let s:endCol += col('$') - s:prevLen[1]
370 let s:prevLen = [line('$'), col('$')]
373 let s:endLine += changeLine
377 " Delete snippet if cursor moves out of it in insert mode
378 if (lnum == s:endLine && (col > s:endCol || col < g:snipPos[s:curPos][1]))
379 \ || lnum > s:endLine || lnum < g:snipPos[s:curPos][0]
380 call s:RemoveSnippet()
385 " This updates the variables in a snippet when a placeholder has been edited.
386 " (e.g., each "$1" in "${1:foo} $1bar $1bar")
388 let newWordLen = s:endCol - s:startCol + 1
389 let newWord = strpart(getline('.'), s:startCol, newWordLen)
390 if newWord == s:oldWord || empty(g:snipPos[s:curPos][3])
394 let changeLen = g:snipPos[s:curPos][2] - newWordLen
395 let curLine = line('.')
396 let startCol = col('.')
397 let oldStartSnip = s:startCol
398 let updateTabStops = changeLen != 0
401 for [lnum, col] in g:snipPos[s:curPos][3]
403 let start = s:startCol
404 if lnum == curLine && col <= start
405 let s:startCol -= changeLen
406 let s:endCol -= changeLen
408 for nPos in g:snipPos[s:curPos][3][(i):]
409 " This list is in ascending order, so quit if we've gone too far.
410 if nPos[0] > lnum | break | endif
411 if nPos[0] == lnum && nPos[1] > col
412 let nPos[1] -= changeLen
415 if lnum == curLine && col > start
417 let g:snipPos[s:curPos][3][i][1] = col
422 " "Very nomagic" is used here to allow special characters.
423 call setline(lnum, substitute(getline(lnum), '\%'.col.'c\V'.
424 \ escape(s:oldWord, '\'), escape(newWord, '\&'), ''))
426 if oldStartSnip != s:startCol
427 call cursor(0, startCol + s:startCol - oldStartSnip)
430 let s:oldWord = newWord
431 let g:snipPos[s:curPos][2] = newWordLen
433 " vim:noet:sw=4:ts=4:ft=vim