1
0
mirror of https://github.com/gryf/snipmate.vim.git synced 2026-01-31 04:55:46 +01:00

added support for buffer-only snippets

This commit is contained in:
Michael Sanders
2009-03-15 18:52:29 -04:00
parent 150a65594b
commit 5527548d63
3 changed files with 42 additions and 37 deletions

View File

@@ -7,6 +7,8 @@ let snippet_filetype = 'vim'
" snippets for making snippets :) " snippets for making snippets :)
exe 'Snipp snip exe "Snipp ${1:trigger}"${2}' exe 'Snipp snip exe "Snipp ${1:trigger}"${2}'
exe "Snipp snipp exe 'Snipp ${1:trigger}'${2}" exe "Snipp snipp exe 'Snipp ${1:trigger}'${2}"
exe 'Snipp bsnip exe "BufferSnip ${1:trigger}"${2}'
exe "Snipp bsnipp exe 'BufferSnip ${1:trigger}'${2}"
exe 'Snipp gsnip exe "GlobalSnip ${1:trigger}"${2}' exe 'Snipp gsnip exe "GlobalSnip ${1:trigger}"${2}'
exe "Snipp gsnipp exe 'GlobalSnip ${1:trigger}'${2}" exe "Snipp gsnipp exe 'GlobalSnip ${1:trigger}'${2}"
exe "Snipp guard if !exists('g:loaded_snips') || exists('s:did_". exe "Snipp guard if !exists('g:loaded_snips') || exists('s:did_".

View File

@@ -1,7 +1,7 @@
*snipMate.txt* Plugin for using TextMate-style snippets in Vim. *snipMate.txt* Plugin for using TextMate-style snippets in Vim.
snipMate *snippet* *snippets* *snipMate* snipMate *snippet* *snippets* *snipMate*
Last Change: March 8, 2009 Last Change: March 15, 2009
|snipMate-description| Description |snipMate-description| Description
|snipMate-usage| Usage |snipMate-usage| Usage
@@ -43,10 +43,10 @@ There are currently two ways to make snippets: file-based and command-based.
File-based snippets are simply *.snippet files named after the trigger of File-based snippets are simply *.snippet files named after the trigger of
the snippet placed in the directory of the filetype the snippet placed in the directory of the filetype
(<filetype>/<trigger>.snippet); command-based snippets are snippets defined (<filetype>/<trigger>.snippet); command-based snippets are snippets defined
using the |Snipp| and |GlobalSnip| commands. File-based snippets have the using the |Snipp| , |BufferSnip|, and |GlobalSnip| commands. File-based
advantage of being easier to read, but do not support special characters in snippets have the advantage of being easier to read, but do not support
snippet triggers, while command-based snippets are obviously convenient for special characters in snippet triggers, while command-based snippets are
short snippets but can quickly get unreadable. obviously convenient for short snippets but can quickly get unreadable.
*command-snippets* *command-snippets*
------------------------------------------------------------------------------ ------------------------------------------------------------------------------
@@ -69,33 +69,35 @@ snipMate: >
This ensures dotted filetypes (see 'filetype') are dealt with correctly. This ensures dotted filetypes (see 'filetype') are dealt with correctly.
*Snipp* *GlobalSnip* *Snipp* *BufferSnip* *GlobalSnip*
Snipp and GlobalSnip Commands~ Snipp, BufferSnip, and GlobalSnip Commands~
Snippets are added via the "Snipp" and "GlobalSnip" commands. The syntax for Snippets are added via the "Snipp" and "GlobalSnip" commands. The syntax for
these are "Snipp <trigger> <text>"; e.g.: > these are "Snipp <trigger> <text>"; e.g.: >
exe "Snipp trigger The cursor will be placed at the end of this sentence." exe "Snipp trigger The cursor will be placed at the end of this sentence."
exe "GlobalSnip another_trigger foo" exe "GlobalSnip another_trigger foo"
exe "BufferSnip bar This snippet only works for the current buffer."
"Snipp" creates snippets local to the buffer, while "GlobalSnip" creates "Snipp" creates snippets for the current filetype, "GlobalSnip" creates global
global snippets. "Snipp" is used instead of "Snip" to avoid conflicts with the snippets, and "BufferSnip" creates snippets for the current buffer. "Snipp"
imaps.vim vim script that uses that command name. is used instead of "Snip" to avoid conflicts with the imaps.vim vim script
that uses that command name.
These commands are conveniently bound to snippets themselves; "snip" and These commands are conveniently bound to snippets themselves; "snip", "bsnip",
"gsnip", respectively. So to expand a Snipp command with double quotes, and "gsnip", respectively (in vim files). So to expand a Snipp command with
just type snip<tab>. Single quote Snipp and GlobalSnip commands are bound double quotes, just type snip<tab>. Single quote Snipp and GlobalSnip
to the snippets "snipp" and "gsnipp". See |literal-string| for the commands are bound to the snippets "snipp", "bsnipp" and "gsnipp". See
difference between single and double quotes. |literal-string| for the difference between single and double quotes.
*multi_snip* *Snipp!* *GlobalSnip!* *multi_snip* *Snipp!* *BufferSnip!* *GlobalSnip!*
To specify that a snippet can have multiple matches, use the Snipp or To specify that a snippet can have multiple matches, use the Snipp,
GlobalSnip command followed by a bang (!). The syntax for these are BufferSnip, or GlobalSnip command followed by a bang (!). The syntax for these
'Snipp! <trigger> "<name>" <text>'. (Note that the name must be are 'Snipp! <trigger> "<name>" <text>'. (Note that the name must be enclosed
enclosed in double quotes). E.g.: > in double quotes). E.g.: >
exe 'Snip! trigger "Snippet name #1" expand_this_text' exe 'Snipp! trigger "Snippet name #1" expand_this_text'
exe 'Snip! trigger "Snippet name #2" expand_THIS_text!' exe 'Snipp! trigger "Snippet name #2" expand_THIS_text!'
In this example, when "trigger<tab>" is typed, a numbered menu containing all In this example, when "trigger<tab>" is typed, a numbered menu containing all
of the names for the "trigger" will be shown; when the user presses the of the names for the "trigger" will be shown; when the user presses the

View File

@@ -16,6 +16,7 @@ let loaded_snips = 1
if !exists('snips_author') | let snips_author = 'Me' | endif if !exists('snips_author') | let snips_author = 'Me' | endif
com! -nargs=+ -bang Snipp call s:MakeSnippet(<q-args>, snippet_filetype, <bang>0) com! -nargs=+ -bang Snipp call s:MakeSnippet(<q-args>, snippet_filetype, <bang>0)
com! -nargs=+ -bang BufferSnip call s:MakeSnippet(<q-args>, bufnr('%'), <bang>0)
com! -nargs=+ -bang GlobalSnip call s:MakeSnippet(<q-args>, '_', <bang>0) com! -nargs=+ -bang GlobalSnip call s:MakeSnippet(<q-args>, '_', <bang>0)
let s:snippets = {} | let s:multi_snips = {} let s:snippets = {} | let s:multi_snips = {}
@@ -26,7 +27,7 @@ fun! Filename(...)
return !a:0 || a:1 == '' ? filename : substitute(a:1, '$1', filename, 'g') return !a:0 || a:1 == '' ? filename : substitute(a:1, '$1', filename, 'g')
endf endf
fun s:MakeSnippet(text, ft, multisnip) fun s:MakeSnippet(text, scope, multisnip)
let space = stridx(a:text, ' ') let space = stridx(a:text, ' ')
let trigger = strpart(a:text, 0, space) let trigger = strpart(a:text, 0, space)
if a:multisnip if a:multisnip
@@ -38,14 +39,14 @@ fun s:MakeSnippet(text, ft, multisnip)
else else
let var = 's:snippets' let var = 's:snippets'
endif endif
if !has_key({var}, a:ft) | let {var}[a:ft] = {} | endif if !has_key({var}, a:scope) | let {var}[a:scope] = {} | endif
let end = strpart(a:text, space + 1) let end = strpart(a:text, space + 1)
if end == '' || space == '' || (a:multisnip && name == '') if end == '' || space == '' || (a:multisnip && name == '')
echom 'Error in snipMate.vim: Snippet '.a:text.' is undefined.' echom 'Error in snipMate.vim: Snippet '.a:text.' is undefined.'
elseif !has_key({var}[a:ft], trigger) elseif !has_key({var}[a:scope], trigger)
let {var}[a:ft][trigger] = a:multisnip ? [[name, end]] : end let {var}[a:scope][trigger] = a:multisnip ? [[name, end]] : end
elseif a:multisnip | let {var}[a:ft][trigger] += [[name, end]] elseif a:multisnip | let {var}[a:scope][trigger] += [[name, end]]
else else
echom 'Warning in snipMate.vim: Snippet '.strpart(a:text, 0, stridx(a:text, ' ')) echom 'Warning in snipMate.vim: Snippet '.strpart(a:text, 0, stridx(a:text, ' '))
\ .' is already defined. See :h multi_snip for help on snippets' \ .' is already defined. See :h multi_snip for help on snippets'
@@ -90,16 +91,16 @@ fun s:RemoveSnippet()
unl s:snipPos s:curPos s:snipLen s:endSnip s:endSnipLine s:prevLen unl s:snipPos s:curPos s:snipLen s:endSnip s:endSnipLine s:prevLen
endf endf
fun s:ChooseSnippet(ft, trigger) fun s:ChooseSnippet(scope, trigger)
let snippet = [] let snippet = []
let i = 1 let i = 1
for snip in s:multi_snips[a:ft][a:trigger] for snip in s:multi_snips[a:scope][a:trigger]
let snippet += [i.'. '.snip[0]] let snippet += [i.'. '.snip[0]]
let i += 1 let i += 1
endfor endfor
if i == 2 | return s:multi_snips[a:ft][a:trigger][0][1] | endif if i == 2 | return s:multi_snips[a:scope][a:trigger][0][1] | endif
let num = inputlist(snippet) - 1 let num = inputlist(snippet) - 1
return num < i-1 ? s:multi_snips[a:ft][a:trigger][num][1] : '' return num < i-1 ? s:multi_snips[a:scope][a:trigger][num][1] : ''
endf endf
fun! TriggerSnippet() fun! TriggerSnippet()
@@ -117,7 +118,7 @@ fun! TriggerSnippet()
endif endif
let word = matchstr(getline('.'), '\S\+\%'.col('.').'c') let word = matchstr(getline('.'), '\S\+\%'.col('.').'c')
for filetype in split(&ft, '\.') + ['_'] " Deal with dotted file-types for filetype in [bufnr('%')] + split(&ft, '\.') + ['_']
let trigger = s:GetSnippet(word, filetype) let trigger = s:GetSnippet(word, filetype)
if exists('s:snippet') | break | endif if exists('s:snippet') | break | endif
endfor endfor
@@ -144,13 +145,13 @@ endf
" Check if word under cursor is snippet trigger; if it isn't, try checking if " 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") " the text after non-word characters is (e.g. check for "foo" in "bar.foo")
fun s:GetSnippet(word, ft) fun s:GetSnippet(scope, ft)
let word = a:word let word = a:word
wh !exists('s:snippet') wh !exists('s:snippet')
if exists('s:snippets["'.a:ft.'"]["'.word.'"]') if exists('s:snippets["'.a:scope.'"]["'.word.'"]')
let s:snippet = s:snippets[a:ft][word] let s:snippet = s:snippets[a:scope][word]
elseif exists('s:multi_snips["'.a:ft.'"]["'.word.'"]') elseif exists('s:multi_snips["'.a:scope.'"]["'.word.'"]')
let s:snippet = s:ChooseSnippet(a:ft, word) let s:snippet = s:ChooseSnippet(a:scope, word)
else else
if match(word, '\W') == -1 | break | endif if match(word, '\W') == -1 | break | endif
let word = substitute(word, '.\{-}\W', '', '') let word = substitute(word, '.\{-}\W', '', '')