diff --git a/autoload/snipMate.vim b/autoload/snipMate.vim index f2bb5fa9..b8f65318 100644 --- a/autoload/snipMate.vim +++ b/autoload/snipMate.vim @@ -545,5 +545,42 @@ fun! snipMate#GetSnippets(scopes, trigger) return result endf +" adds leading tab +" and replaces leading spaces by tabs +" see ftplugin/snippet.vim +fun! snipMate#RetabSnip() range + let leadingTab = expand('%:e') == 'snippets' + + let lines = getline(a:firstline, a:lastline) + + " remove leading "\t" + let allIndented = 1 + for l in lines + if l[0] != '\t' | let allIndented = 0 | endif + endfor + + " retab + if allIndented + call map(lines, 'v:val[1:]') + endif + + let leadingSp = filter(map(copy(lines),'matchstr(v:val,"^\\s*") '),'v:val !=""') + if !empty(leadingSp) + " lines containing leading spaces found + let smallestInd = len(sort(leadingSp)[-1]) + let ind = input('retab, spaces per tab: ', smallestInd) + for i in range(0, len(lines)-1) + let ml = matchlist(lines[i], '^\(\s*\)\(.*\)') + let lines[i] = repeat("\t", len(ml[1]) / ind) + \ . repeat( " ", len(ml[1]) % ind) + \ . ml[2] + endfor + endif + " readd tab + let tab = leadingTab ? "\t" : "" + for i in range(0,len(lines)-1) + call setline(a:firstline + i, tab.lines[i]) + endfor +endf " vim:noet:sw=4:ts=4:ft=vim diff --git a/doc/snipMate.txt b/doc/snipMate.txt index f807a1a4..5bce5edf 100644 --- a/doc/snipMate.txt +++ b/doc/snipMate.txt @@ -57,8 +57,9 @@ Note that the first hard tab after the snippet trigger is required, and not expanded in the actual snippet. The syntax for *.snippet files is the same, only without the trigger declaration and starting indentation. -Also note that snippets must be defined using hard tabs. They can be expanded -to spaces later if desired (see |snipMate-indenting|). +Also note that indentation within snippets must be defined using hard tabs. +They can be expanded to spaces later if desired (see |snipMate-indenting|). +You can retab a snippet by visually selecting the lines, then press . "#" is used as a line-comment character in *.snippets files; however, they can only be used outside of a snippet declaration. E.g.: > diff --git a/ftplugin/snippet.vim b/ftplugin/snippet.vim new file mode 100644 index 00000000..d2d79321 --- /dev/null +++ b/ftplugin/snippet.vim @@ -0,0 +1,2 @@ +command -buffer -range=% RetabSnip ,call snipMate#RetabSnip() +vnoremap :RetabSnip