From e64c9e5b1db135eeb850a7e459f0785f0da40e4f Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Tue, 16 Mar 2010 13:10:35 -0500 Subject: update snipmate --- vim/after/plugin/snipMate.vim | 17 +++- vim/autoload/snipMate.vim | 203 +++++++++++++++++++++++++----------------- vim/doc/snipMate.txt | 25 ++++-- vim/plugin/snipMate.vim | 50 ++++++++--- vim/snippets/c.snippets | 24 ++++- vim/snippets/cpp.snippets | 4 +- vim/snippets/html.snippets | 2 +- vim/snippets/java.snippets | 16 ++-- vim/snippets/objc.snippets | 47 ++++++---- vim/snippets/perl.snippets | 20 +++-- vim/snippets/php.snippets | 4 +- vim/snippets/python.snippets | 11 ++- vim/snippets/sh.snippets | 2 +- vim/snippets/vim.snippets | 2 - vim/syntax/snippet.vim | 2 +- 15 files changed, 279 insertions(+), 150 deletions(-) diff --git a/vim/after/plugin/snipMate.vim b/vim/after/plugin/snipMate.vim index 1d995ef..03e79ae 100644 --- a/vim/after/plugin/snipMate.vim +++ b/vim/after/plugin/snipMate.vim @@ -1,24 +1,35 @@ " These are the mappings for snipMate.vim. Putting it here ensures that it " will be mapped after other plugins such as supertab.vim. -if exists('s:did_snips_mappings') || &cp || version < 700 +if !exists('loaded_snips') || exists('s:did_snips_mappings') finish endif let s:did_snips_mappings = 1 ino =TriggerSnippet() snor i=TriggerSnippet() +ino =BackwardsSnippet() +snor i=BackwardsSnippet() ino =ShowAvailableSnips() + +" The default mappings for these are annoying & sometimes break snipMate. +" You can change them back if you want, I've put them here for convenience. snor b -snor ' b' snor a snor bi +snor ' b' +snor ` b` +snor % b% +snor U bU +snor ^ b^ +snor \ b\ +snor b " By default load snippets in snippets_dir if empty(snippets_dir) finish endif -call GetSnippets(snippets_dir, '_') " Get global snippets +call GetSnippets(snippets_dir, '_') " Get global snippets au FileType * if &ft != 'help' | call GetSnippets(snippets_dir, &ft) | endif " vim:noet:sw=4:ts=4:ft=vim diff --git a/vim/autoload/snipMate.vim b/vim/autoload/snipMate.vim index e0219e3..dcd28f6 100644 --- a/vim/autoload/snipMate.vim +++ b/vim/autoload/snipMate.vim @@ -5,19 +5,28 @@ fun! Filename(...) endf fun s:RemoveSnippet() - unl g:snipPos s:curPos s:snipLen s:endSnip s:endSnipLine s:prevLen + unl! g:snipPos s:curPos s:snipLen s:endCol s:endLine s:prevLen + \ s:lastBuf s:oldWord + if exists('s:update') + unl s:startCol s:origWordLen s:update + if exists('s:oldVars') | unl s:oldVars s:oldEndCol | endif + endif + aug! snipMateAutocmds endf fun snipMate#expandSnip(snip, col) let lnum = line('.') | let col = a:col let snippet = s:ProcessSnippet(a:snip) + " Avoid error if eval evaluates to nothing if snippet == '' | return '' | endif + " Expand snippet onto current position with the tab stops removed let snipLines = split(substitute(snippet, '$\d\+\|${\d\+.\{-}}', '', 'g'), "\n", 1) let line = getline(lnum) let afterCursor = strpart(line, col - 1) + " Keep text after the cursor if afterCursor != "\t" && afterCursor != ' ' let line = strpart(line, 0, col - 1) let snipLines[-1] .= afterCursor @@ -34,14 +43,21 @@ fun snipMate#expandSnip(snip, col) " Autoindent snippet according to previous indentation let indent = matchend(line, '^.\{-}\ze\(\S\|$\)') + 1 call append(lnum, map(snipLines[1:], "'".strpart(line, 0, indent - 1)."'.v:val")) + + " Open any folds snippet expands into if &fen | sil! exe lnum.','.(lnum + len(snipLines) - 1).'foldopen' | endif let [g:snipPos, s:snipLen] = s:BuildTabStops(snippet, lnum, col - indent, indent) if s:snipLen - let s:curPos = 0 - let s:endSnip = g:snipPos[s:curPos][1] - let s:endSnipLine = g:snipPos[s:curPos][0] + aug snipMateAutocmds + au CursorMovedI * call s:UpdateChangedSnip(0) + au InsertEnter * call s:UpdateChangedSnip(1) + aug END + let s:lastBuf = bufnr(0) " Only expand snippet while in current buffer + let s:curPos = 0 + let s:endCol = g:snipPos[s:curPos][1] + let s:endLine = g:snipPos[s:curPos][0] call cursor(g:snipPos[s:curPos][0], g:snipPos[s:curPos][1]) let s:prevLen = [line('$'), col('$')] @@ -56,6 +72,7 @@ fun snipMate#expandSnip(snip, col) return '' endf +" Prepare snippet to be processed by s:BuildTabStops fun s:ProcessSnippet(snip) let snippet = a:snip " Evaluate eval (`...`) expressions. @@ -81,7 +98,7 @@ fun s:ProcessSnippet(snip) while stridx(snippet, '${'.i) != -1 let s = matchstr(snippet, '${'.i.':\zs.\{-}\ze}') if s != '' - let snippet = substitute(snippet, '$'.i, '&'.s, 'g') + let snippet = substitute(snippet, '$'.i, s.'&', 'g') endif let i += 1 endw @@ -92,6 +109,7 @@ fun s:ProcessSnippet(snip) return snippet endf +" Counts occurences of haystack in needle fun s:Count(haystack, needle) let counter = 0 let index = stridx(a:haystack, a:needle) @@ -102,8 +120,7 @@ fun s:Count(haystack, needle) return counter endf -" This function builds a list of a list of each tab stop in the -" snippet containing: +" Builds a list of a list of each tab stop in the snippet containing: " 1.) The tab stop's line number. " 2.) The tab stop's column number " (by getting the length of the string between the last "\n" and the @@ -120,28 +137,28 @@ fun s:BuildTabStops(snip, lnum, col, indent) let withoutVars = substitute(a:snip, '$\d\+', '', 'g') while stridx(a:snip, '${'.i) != -1 let beforeTabStop = matchstr(withoutVars, '^.*\ze${'.i.'\D') - let withoutOthers = substitute(withoutVars, '${'.i.'\@!\d\+.\{-}}', '', 'g') - let snipPos += [[a:lnum + s:Count(beforeTabStop, "\n"), - \ a:indent + len(matchstr(withoutOthers, - \ "^.*\\(\n\\|^\\)\\zs.*\\ze${".i.'\D')), -1]] - if snipPos[i-1][0] == a:lnum - let snipPos[i-1][1] += a:col - endif + let withoutOthers = substitute(withoutVars, '${\('.i.'\D\)\@!\d\+.\{-}}', '', 'g') + + let j = i - 1 + call add(snipPos, [0, 0, -1]) + let snipPos[j][0] = a:lnum + s:Count(beforeTabStop, "\n") + let snipPos[j][1] = a:indent + len(matchstr(withoutOthers, '.*\(\n\|^\)\zs.*\ze${'.i.'\D')) + if snipPos[j][0] == a:lnum | let snipPos[j][1] += a:col | endif " Get all $# matches in another list, if ${#:name} is given if stridx(withoutVars, '${'.i.':') != -1 - let j = i - 1 let snipPos[j][2] = len(matchstr(withoutVars, '${'.i.':\zs.\{-}\ze}')) - let snipPos[j] += [[]] + let dots = repeat('.', snipPos[j][2]) + call add(snipPos[j], []) let withoutOthers = substitute(a:snip, '${\d\+.\{-}}\|$'.i.'\@!\d\+', '', 'g') - while match(withoutOthers, '$'.i.'\D') != -1 - let beforeMark = matchstr(withoutOthers, '^.\{-}\ze$'.i.'\D') - let linecount = a:lnum + s:Count(beforeMark, "\n") - let snipPos[j][3] += [[linecount, - \ a:indent + (linecount > a:lnum - \ ? len(matchstr(beforeMark, "^.*\n\\zs.*")) - \ : a:col + len(beforeMark))]] - let withoutOthers = substitute(withoutOthers, '$'.i.'\ze\D', '', '') + while match(withoutOthers, '$'.i.'\(\D\|$\)') != -1 + let beforeMark = matchstr(withoutOthers, '^.\{-}\ze'.dots.'$'.i.'\(\D\|$\)') + call add(snipPos[j][3], [0, 0]) + let snipPos[j][3][-1][0] = a:lnum + s:Count(beforeMark, "\n") + let snipPos[j][3][-1][1] = a:indent + (snipPos[j][3][-1][0] > a:lnum + \ ? len(matchstr(beforeMark, '.*\n\zs.*')) + \ : a:col + len(beforeMark)) + let withoutOthers = substitute(withoutOthers, '$'.i.'\ze\(\D\|$\)', '', '') endw endif let i += 1 @@ -149,45 +166,65 @@ fun s:BuildTabStops(snip, lnum, col, indent) return [snipPos, i - 1] endf -fun snipMate#jumpTabStop() +fun snipMate#jumpTabStop(backwards) + let leftPlaceholder = exists('s:origWordLen') + \ && s:origWordLen != g:snipPos[s:curPos][2] + if leftPlaceholder && exists('s:oldEndCol') + let startPlaceholder = s:oldEndCol + 1 + endif + if exists('s:update') call s:UpdatePlaceholderTabStops() else call s:UpdateTabStops() endif - let s:curPos += 1 + " Don't reselect placeholder if it has been modified + if leftPlaceholder && g:snipPos[s:curPos][2] != -1 + if exists('startPlaceholder') + let g:snipPos[s:curPos][1] = startPlaceholder + else + let g:snipPos[s:curPos][1] = col('.') + let g:snipPos[s:curPos][2] = 0 + endif + endif + + let s:curPos += a:backwards ? -1 : 1 + " Loop over the snippet when going backwards from the beginning + if s:curPos < 0 | let s:curPos = s:snipLen - 1 | endif + if s:curPos == s:snipLen - let sMode = s:endSnip == g:snipPos[s:curPos-1][1]+g:snipPos[s:curPos-1][2] + let sMode = s:endCol == g:snipPos[s:curPos-1][1]+g:snipPos[s:curPos-1][2] call s:RemoveSnippet() return sMode ? "\" : TriggerSnippet() endif call cursor(g:snipPos[s:curPos][0], g:snipPos[s:curPos][1]) - let s:endSnipLine = g:snipPos[s:curPos][0] - let s:endSnip = g:snipPos[s:curPos][1] - let s:prevLen = [line('$'), col('$')] + let s:endLine = g:snipPos[s:curPos][0] + let s:endCol = g:snipPos[s:curPos][1] + let s:prevLen = [line('$'), col('$')] return g:snipPos[s:curPos][2] == -1 ? '' : s:SelectWord() endf fun s:UpdatePlaceholderTabStops() let changeLen = s:origWordLen - g:snipPos[s:curPos][2] - unl s:startSnip s:origWordLen s:update - if !exists('s:origPos') | return | endif + unl s:startCol s:origWordLen s:update + if !exists('s:oldVars') | return | endif " Update tab stops in snippet if text has been added via "$#" " (e.g., in "${1:foo}bar$1${2}"). if changeLen != 0 let curLine = line('.') - for pos in g:snipPos[s:curPos + 1:] - let changed = pos[0] == curLine && pos[1] > s:origSnipPos + for pos in g:snipPos + if pos == g:snipPos[s:curPos] | continue | endif + let changed = pos[0] == curLine && pos[1] > s:oldEndCol let changedVars = 0 let endPlaceholder = pos[2] - 1 + pos[1] " Subtract changeLen from each tab stop that was after any of " the current tab stop's placeholders. - for [lnum, col] in s:origPos + for [lnum, col] in s:oldVars if lnum > pos[0] | break | endif if pos[0] == lnum if pos[1] > col || (pos[2] == -1 && pos[1] == col) @@ -204,8 +241,8 @@ fun s:UpdatePlaceholderTabStops() if pos[2] == -1 | continue | endif " Do the same to any placeholders in the other tab stops. for nPos in pos[3] - let changed = nPos[0] == curLine && nPos[1] > s:origSnipPos - for [lnum, col] in s:origPos + let changed = nPos[0] == curLine && nPos[1] > s:oldEndCol + for [lnum, col] in s:oldVars if lnum > nPos[0] | break | endif if nPos[0] == lnum && nPos[1] > col let changed += 1 @@ -215,23 +252,23 @@ fun s:UpdatePlaceholderTabStops() endfor endfor endif - unl s:endSnip s:origPos s:origSnipPos + unl s:endCol s:oldVars s:oldEndCol endf fun s:UpdateTabStops() - let changeLine = s:endSnipLine - g:snipPos[s:curPos][0] - let changeCol = s:endSnip - g:snipPos[s:curPos][1] + let changeLine = s:endLine - g:snipPos[s:curPos][0] + let changeCol = s:endCol - g:snipPos[s:curPos][1] if exists('s:origWordLen') let changeCol -= s:origWordLen unl s:origWordLen endif let lnum = g:snipPos[s:curPos][0] - let col = g:snipPos[s:curPos][1] + let col = g:snipPos[s:curPos][1] " Update the line number of all proceeding tab stops if has " been inserted. if changeLine != 0 let changeLine -= 1 - for pos in g:snipPos[s:curPos + 1:] + for pos in g:snipPos if pos[0] >= lnum if pos[0] == lnum | let pos[1] += changeCol | endif let pos[0] += changeLine @@ -247,7 +284,7 @@ fun s:UpdateTabStops() elseif changeCol != 0 " Update the column of all proceeding tab stops if text has " been inserted/deleted in the current line. - for pos in g:snipPos[s:curPos + 1:] + for pos in g:snipPos if pos[1] >= col && pos[0] == lnum let pos[1] += changeCol endif @@ -264,13 +301,13 @@ endf fun s:SelectWord() let s:origWordLen = g:snipPos[s:curPos][2] - let s:oldWord = strpart(getline('.'), g:snipPos[s:curPos][1] - 1, - \ s:origWordLen) + let s:oldWord = strpart(getline('.'), g:snipPos[s:curPos][1] - 1, + \ s:origWordLen) let s:prevLen[1] -= s:origWordLen if !empty(g:snipPos[s:curPos][3]) - let s:update = 1 - let s:endSnip = -1 - let s:startSnip = g:snipPos[s:curPos][1] - 1 + let s:update = 1 + let s:endCol = -1 + let s:startCol = g:snipPos[s:curPos][1] - 1 endif if !s:origWordLen | return '' | endif let l = col('.') != 1 ? 'l' : '' @@ -289,53 +326,57 @@ endf " " It also automatically quits the snippet if the cursor is moved out of it " while in insert mode. -au CursorMovedI * call s:UpdateChangedSnip(0) -au InsertEnter * call s:UpdateChangedSnip(1) fun s:UpdateChangedSnip(entering) - if exists('s:update') " If modifying a placeholder - if !exists('s:origPos') && s:curPos + 1 < s:snipLen + if exists('g:snipPos') && bufnr(0) != s:lastBuf + call s:RemoveSnippet() + elseif exists('s:update') " If modifying a placeholder + if !exists('s:oldVars') && s:curPos + 1 < s:snipLen " Save the old snippet & word length before it's updated - " s:startSnip must be saved too, in case text is added + " s:startCol must be saved too, in case text is added " before the snippet (e.g. in "foo$1${2}bar${1:foo}"). - let s:origSnipPos = s:startSnip - let s:origPos = deepcopy(g:snipPos[s:curPos][3]) + let s:oldEndCol = s:startCol + let s:oldVars = deepcopy(g:snipPos[s:curPos][3]) endif let col = col('.') - 1 - if s:endSnip != -1 + if s:endCol != -1 let changeLen = col('$') - s:prevLen[1] - let s:endSnip += changeLen + let s:endCol += changeLen else " When being updated the first time, after leaving select mode if a:entering | return | endif - let s:endSnip = col - 1 + let s:endCol = col - 1 endif " If the cursor moves outside the snippet, quit it - if line('.') != g:snipPos[s:curPos][0] || col < s:startSnip || - \ col - 1 > s:endSnip - unl! s:startSnip s:origWordLen s:origPos s:update + if line('.') != g:snipPos[s:curPos][0] || col < s:startCol || + \ col - 1 > s:endCol + unl! s:startCol s:origWordLen s:oldVars s:update return s:RemoveSnippet() endif call s:UpdateVars() let s:prevLen[1] = col('$') elseif exists('g:snipPos') - let col = col('.') - let lnum = line('.') + if !a:entering && g:snipPos[s:curPos][2] != -1 + let g:snipPos[s:curPos][2] = -2 + endif + + let col = col('.') + let lnum = line('.') let changeLine = line('$') - s:prevLen[0] - if lnum == s:endSnipLine - let s:endSnip += col('$') - s:prevLen[1] + if lnum == s:endLine + let s:endCol += col('$') - s:prevLen[1] let s:prevLen = [line('$'), col('$')] endif if changeLine != 0 - let s:endSnipLine += changeLine - let s:endSnip = col + let s:endLine += changeLine + let s:endCol = col endif " Delete snippet if cursor moves out of it in insert mode - if (lnum == s:endSnipLine && (col > s:endSnip || col < g:snipPos[s:curPos][1])) - \ || lnum > s:endSnipLine || lnum < g:snipPos[s:curPos][0] + if (lnum == s:endLine && (col > s:endCol || col < g:snipPos[s:curPos][1])) + \ || lnum > s:endLine || lnum < g:snipPos[s:curPos][0] call s:RemoveSnippet() endif endif @@ -344,25 +385,25 @@ endf " This updates the variables in a snippet when a placeholder has been edited. " (e.g., each "$1" in "${1:foo} $1bar $1bar") fun s:UpdateVars() - let newWordLen = s:endSnip - s:startSnip + 1 - let newWord = strpart(getline('.'), s:startSnip, newWordLen) + let newWordLen = s:endCol - s:startCol + 1 + let newWord = strpart(getline('.'), s:startCol, newWordLen) if newWord == s:oldWord || empty(g:snipPos[s:curPos][3]) return endif - let changeLen = g:snipPos[s:curPos][2] - newWordLen - let curLine = line('.') - let startCol = col('.') - let oldStartSnip = s:startSnip + let changeLen = g:snipPos[s:curPos][2] - newWordLen + let curLine = line('.') + let startCol = col('.') + let oldStartSnip = s:startCol let updateTabStops = changeLen != 0 - let i = 0 + let i = 0 for [lnum, col] in g:snipPos[s:curPos][3] if updateTabStops - let start = s:startSnip + let start = s:startCol if lnum == curLine && col <= start - let s:startSnip -= changeLen - let s:endSnip -= changeLen + let s:startCol -= changeLen + let s:endCol -= changeLen endif for nPos in g:snipPos[s:curPos][3][(i):] " This list is in ascending order, so quit if we've gone too far. @@ -382,8 +423,8 @@ fun s:UpdateVars() call setline(lnum, substitute(getline(lnum), '\%'.col.'c\V'. \ escape(s:oldWord, '\'), escape(newWord, '\&'), '')) endfor - if oldStartSnip != s:startSnip - call cursor(0, startCol + s:startSnip - oldStartSnip) + if oldStartSnip != s:startCol + call cursor(0, startCol + s:startCol - oldStartSnip) endif let s:oldWord = newWord diff --git a/vim/doc/snipMate.txt b/vim/doc/snipMate.txt index 1d86877..704d44a 100644 --- a/vim/doc/snipMate.txt +++ b/vim/doc/snipMate.txt @@ -1,7 +1,7 @@ *snipMate.txt* Plugin for using TextMate-style snippets in Vim. snipMate *snippet* *snippets* *snipMate* -Last Change: May 8, 2009 +Last Change: July 13, 2009 |snipMate-description| Description |snipMate-syntax| Snippet syntax @@ -33,14 +33,19 @@ you type "for" in insert mode, it will expand a typical for loop in C: > To go to the next item in the loop, simply over to it; if there is repeated code, such as the "i" variable in this example, you can simply start typing once it's highlighted and all the matches specified in the -snippet will be updated. +snippet will be updated. To go in reverse, use . ============================================================================== SYNTAX *snippet-syntax* Snippets can be defined in two ways. They can be in their own file, named after their trigger in 'snippets//.snippet', or they can be -defined together in a 'snippets/.snippets' file. +defined together in a 'snippets/.snippets' file. Note that dotted +'filetype' syntax is supported -- e.g., you can use > + + :set ft=html.eruby + +to activate snippets for both HTML and eRuby for the current file. The syntax for snippets in *.snippets files is the following: > @@ -221,14 +226,14 @@ spaces. If 'softtabstop' is not set, 'shiftwidth' is used instead. snipMate does not come with a setting to customize the trigger key, but you can remap it easily in the two lines it's defined in the 'after' directory under 'plugin/snipMate.vim'. For instance, to change the trigger key -to shift-tab, just change this: > +to CTRL-J, just change this: > ino =TriggerSnippet() snor i=TriggerSnippet() to this: > - ino =TriggerSnippet() - snor i=TriggerSnippet() + ino =TriggerSnippet() + snor i=TriggerSnippet() ============================================================================== FEATURES *snipMate-features* @@ -243,15 +248,15 @@ snipMate.vim has the following features among others: - Snippets can have multiple matches. - Snippets can be out of order. For instance, in a do...while loop, the condition can be added before the code. - - (New) File-based snippets are supported. - - (New) Triggers after non-word delimiters are expanded, e.g. "foo" + - [New] File-based snippets are supported. + - [New] Triggers after non-word delimiters are expanded, e.g. "foo" in "bar.foo". + - [New] can now be used to jump tab stops in reverse order. ============================================================================== DISADVANTAGES *snipMate-disadvantages* snipMate.vim currently has the following disadvantages to TextMate's snippets: - - There is no way to go back a tab stop, like shift-tab in TextMate. - There is no $0; the order of tab stops must be explicitly stated. - Placeholders within placeholders are not possible. E.g.: > @@ -276,4 +281,6 @@ To contact the author (Michael Sanders), please email: I greatly appreciate any suggestions or improvements offered for the script. +============================================================================== + vim:tw=78:ts=8:ft=help:norl: diff --git a/vim/plugin/snipMate.vim b/vim/plugin/snipMate.vim index fe0c8a1..3efee2a 100644 --- a/vim/plugin/snipMate.vim +++ b/vim/plugin/snipMate.vim @@ -1,7 +1,7 @@ " File: snipMate.vim " Author: Michael Sanders -" Version: 0.81 -" Last Modified: May 15, 2009 +" Last Updated: July 13, 2009 +" Version: 0.83 " Description: snipMate.vim implements some of TextMate's snippets features in " Vim. A snippet is a piece of often-typed text that you can " insert into your document using a trigger word followed by a "". @@ -17,7 +17,7 @@ let loaded_snips = 1 if !exists('snips_author') | let snips_author = 'Me' | endif au BufRead,BufNewFile *.snippets\= set ft=snippet -au FileType snippet setl noet +au FileType snippet setl noet fdm=indent let s:snippets = {} | let s:multi_snips = {} @@ -139,7 +139,7 @@ fun! TriggerSnippet() call feedkeys("\") | return '' endif - if exists('g:snipPos') | return snipMate#jumpTabStop() | endif + if exists('g:snipPos') | return snipMate#jumpTabStop(0) | endif let word = matchstr(getline('.'), '\S\+\%'.col('.').'c') for scope in [bufnr('%')] + split(&ft, '\.') + ['_'] @@ -148,7 +148,7 @@ fun! TriggerSnippet() " the snippet. if snippet != '' let col = col('.') - len(trigger) - sil exe 's/\V'.escape(trigger, '/').'\%#//' + sil exe 's/\V'.escape(trigger, '/.').'\%#//' return snipMate#expandSnip(snippet, col) endif endfor @@ -160,6 +160,23 @@ fun! TriggerSnippet() return "\" endf +fun! BackwardsSnippet() + if exists('g:snipPos') | return snipMate#jumpTabStop(1) | endif + + if exists('g:SuperTabMappingForward') + if g:SuperTabMappingBackward == "" + let SuperTabKey = "\" + elseif g:SuperTabMappingForward == "" + let SuperTabKey = "\" + endif + endif + if exists('SuperTabKey') + call feedkeys(SuperTabKey) + return '' + endif + return "\" +endf + " Check if word under cursor is snippet trigger; if it isn't, try checking if " the text after non-word characters is (e.g. check for "foo" in "bar.foo") fun s:GetSnippet(word, scope) @@ -175,6 +192,9 @@ fun s:GetSnippet(word, scope) let word = substitute(word, '.\{-}\W', '', '') endif endw + if word == '' && a:word != '.' && stridx(a:word, '.') != -1 + let [word, snippet] = s:GetSnippet('.', a:scope) + endif return [word, snippet] endf @@ -190,17 +210,19 @@ fun s:ChooseSnippet(scope, trigger) return num == -1 ? '' : s:multi_snips[a:scope][a:trigger][num][1] endf -fun ShowAvailableSnips() - let word = matchstr(getline('.'), '\S\+\%'.col('.').'c') +fun! ShowAvailableSnips() + let line = getline('.') + let col = col('.') + let word = matchstr(getline('.'), '\S\+\%'.col.'c') let words = [word] if stridx(word, '.') let words += split(word, '\.', 1) endif - let matchpos = 0 + let matchlen = 0 let matches = [] for scope in [bufnr('%')] + split(&ft, '\.') + ['_'] - let triggers = exists('s:snippets["'.scope.'"]') ? keys(s:snippets[scope]) : [] - if exists('s:multi_snips["'.scope.'"]') + let triggers = has_key(s:snippets, scope) ? keys(s:snippets[scope]) : [] + if has_key(s:multi_snips, scope) let triggers += keys(s:multi_snips[scope]) endif for trigger in triggers @@ -210,12 +232,16 @@ fun ShowAvailableSnips() elseif trigger =~ '^'.word let matches += [trigger] let len = len(word) - if len > matchpos | let matchpos = len | endif + if len > matchlen | let matchlen = len | endif endif endfor endfor endfor - call complete(col('.') - matchpos, matches) + + " This is to avoid a bug with Vim when using complete(col - matchlen, matches) + " (Issue#46 on the Google Code snipMate issue tracker). + call setline(line('.'), substitute(line, repeat('.', matchlen).'\%'.col.'c', '', '')) + call complete(col, matches) return '' endf " vim:noet:sw=4:ts=4:ft=vim diff --git a/vim/snippets/c.snippets b/vim/snippets/c.snippets index dd16668..8824329 100644 --- a/vim/snippets/c.snippets +++ b/vim/snippets/c.snippets @@ -18,6 +18,14 @@ snippet Def #endif${3} snippet def #define +snippet ifdef + #ifdef ${1:FOO} + ${2:#define } + #endif +snippet #if + #if ${1:FOO} + ${2} + #endif # Header Include-Guard snippet once #ifndef ${1:`toupper(Filename('', 'UNTITLED').'_H')`} @@ -64,9 +72,12 @@ snippet fun { ${4:/* code */} } +# Function Declaration +snippet fund + ${1:void} ${2:function_name}(${3});${4} # Typedef snippet td - typedef ${1:int} ${2:MyCustomType}; + typedef ${1:int} ${2:MyCustomType};${3} # Struct snippet st struct ${1:`Filename('$1_t', 'name')`} { @@ -74,9 +85,14 @@ snippet st }${3: /* optional variable list */};${4} # Typedef struct snippet tds - typedef struct ${2:$1 }{ + typedef struct ${2:_$1 }{ ${3:/* data */} } ${1:`Filename('$1_t', 'name')`}; +# Typdef enum +snippet tde + typedef enum { + ${1:/* data */} + } ${2:foo}; # printf # unfortunately version this isn't as nice as TextMates's, given the lack of a # dynamic `...` @@ -85,3 +101,7 @@ snippet pr # fprintf (again, this isn't as nice as TextMate's version, but it works) snippet fpr fprintf(${1:stderr}, "${2:%s}\n"${3});${4} +snippet . + [${1}]${2} +snippet un + unsigned diff --git a/vim/snippets/cpp.snippets b/vim/snippets/cpp.snippets index 4a3b7c7..65fe5e1 100644 --- a/vim/snippets/cpp.snippets +++ b/vim/snippets/cpp.snippets @@ -24,7 +24,7 @@ snippet cl public: $1(${2:arguments}); virtual ~$1(); - + private: ${3:/* data */} }; @@ -33,5 +33,5 @@ snippet forv for (std::vector<${1:int}>::const_iterator ${2:it} = ${3:vec}.begin(); $2 != $3.end(); ++$2) { - ${4:/* code */} + ${4:/* code */} } diff --git a/vim/snippets/html.snippets b/vim/snippets/html.snippets index 5abc731..aefb9db 100644 --- a/vim/snippets/html.snippets +++ b/vim/snippets/html.snippets @@ -115,7 +115,7 @@ snippet head ${2} snippet title - ${1:`substitute(Filename("", "Page Title"), "^.", "\u&", "")`}${2} + ${1:`substitute(Filename('', 'Page Title'), '^.', '\u&', '')`}${2} snippet script