neosnippet.vim/autoload/neosnippet/commands.vim

217 lines
6.2 KiB
VimL
Raw Normal View History

2013-11-19 07:19:33 +00:00
"=============================================================================
" FILE: commands.vim
" AUTHOR: Shougo Matsushita <Shougo.Matsu@gmail.com>
2014-01-01 01:22:03 +00:00
" Last Modified: 01 Jan 2014.
2013-11-19 07:19:33 +00:00
" License: MIT license {{{
" Permission is hereby granted, free of charge, to any person obtaining
" a copy of this software and associated documentation files (the
" "Software"), to deal in the Software without restriction, including
" without limitation the rights to use, copy, modify, merge, publish,
" distribute, sublicense, and/or sell copies of the Software, and to
" permit persons to whom the Software is furnished to do so, subject to
" the following conditions:
"
" The above copyright notice and this permission notice shall be included
" in all copies or substantial portions of the Software.
"
" THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
" OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
" MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
" IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
" CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
" TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
" SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
" }}}
"=============================================================================
let s:save_cpo = &cpo
set cpo&vim
" Variables "{{{
let s:edit_options = [
\ '-runtime',
\ '-vertical', '-horizontal', '-direction=', '-split',
\]
"}}}
function! neosnippet#commands#_edit(args) "{{{
if neosnippet#util#is_sudo()
call neosnippet#util#print_error(
\ '"sudo vim" is detected. This feature is disabled.')
return
endif
call neosnippet#init#check()
let [args, options] = neosnippet#util#parse_options(
\ a:args, s:edit_options)
let filetype = get(args, 0, '')
if filetype == ''
2013-11-21 09:40:40 +00:00
let filetype = neosnippet#helpers#get_filetype()
2013-11-19 07:19:33 +00:00
endif
let options = s:initialize_options(options)
let snippet_dir = (options.runtime ?
\ get(neosnippet#get_runtime_snippets_directory(), 0, '') :
\ get(neosnippet#get_user_snippets_directory(), -1, ''))
if snippet_dir == ''
call neosnippet#util#print_error('Snippet directory is not found.')
return
endif
" Edit snippet file.
let filename = snippet_dir .'/'.filetype
if isdirectory(filename)
" Edit in snippet directory.
let filename .= '/'.filetype
endif
if filename !~ '\.snip*$'
let filename .= '.snip'
endif
if options.split
" Split window.
execute options.direction
\ (options.vertical ? 'vsplit' : 'split')
endif
try
edit `=filename`
catch /^Vim\%((\a\+)\)\=:E749/
endtry
endfunction"}}}
function! neosnippet#commands#_make_cache(filetype) "{{{
call neosnippet#init#check()
let filetype = a:filetype == '' ?
\ &filetype : a:filetype
if filetype ==# ''
let filetype = 'nothing'
endif
2013-11-21 09:20:36 +00:00
let snippets = neosnippet#variables#snippets()
2013-11-19 07:19:33 +00:00
if has_key(snippets, filetype)
return
endif
2014-01-01 01:22:03 +00:00
let path = join(neosnippet#helpers#get_snippets_directory(), ',')
let snippets_files = []
for glob in [filetype.'.snip*', filetype.'_*.snip*',
\ filetype . '/**/*.snip*']
let snippets_files += split(globpath(path, glob), '\n')
2013-11-19 07:19:33 +00:00
endfor
2014-01-01 01:22:03 +00:00
let snippet = {}
call map(reverse(snippets_files),
\ "neosnippet#parser#_parse(snippet, v:val)")
2013-11-21 09:20:36 +00:00
let snippets = neosnippet#variables#snippets()
2013-11-19 07:19:33 +00:00
let snippets[filetype] = snippet
endfunction"}}}
function! neosnippet#commands#_source(filename) "{{{
call neosnippet#init#check()
2013-11-21 09:20:36 +00:00
let neosnippet = neosnippet#variables#current_neosnippet()
call neosnippet#parser#_parse(neosnippet.snippets, a:filename)
2013-11-19 07:19:33 +00:00
endfunction"}}}
function! neosnippet#commands#_clear_markers() "{{{
let expand_stack = neosnippet#variables#expand_stack()
" Get patterns and count.
if empty(expand_stack)
\ || neosnippet#variables#current_neosnippet().trigger
return
endif
let expand_info = expand_stack[-1]
" Search patterns.
let [begin, end] = neosnippet#view#_get_snippet_range(
\ expand_info.begin_line,
\ expand_info.begin_patterns,
\ expand_info.end_line,
\ expand_info.end_patterns)
let pos = getpos('.')
" Found snippet.
let found = 0
try
while neosnippet#view#_search_snippet_range(
\ begin, end, expand_info.holder_cnt, 0)
" Next count.
let expand_info.holder_cnt += 1
let found = 1
endwhile
" Search placeholder 0.
if neosnippet#view#_search_snippet_range(begin, end, 0)
let found = 1
endif
finally
if found
stopinsert
endif
call setpos('.', pos)
endtry
endfunction"}}}
2013-11-21 09:40:40 +00:00
" Complete helpers.
2013-11-19 07:19:33 +00:00
function! neosnippet#commands#_edit_complete(arglead, cmdline, cursorpos) "{{{
return filter(s:edit_options +
2013-11-21 09:40:40 +00:00
\ neosnippet#commands#_filetype_complete(a:arglead, a:cmdline, a:cursorpos),
2013-11-19 07:19:33 +00:00
\ 'stridx(v:val, a:arglead) == 0')
endfunction"}}}
2013-11-21 09:40:40 +00:00
function! neosnippet#commands#_filetype_complete(arglead, cmdline, cursorpos) "{{{
" Dup check.
let ret = {}
for item in map(
\ split(globpath(&runtimepath, 'syntax/*.vim'), '\n') +
\ split(globpath(&runtimepath, 'indent/*.vim'), '\n') +
\ split(globpath(&runtimepath, 'ftplugin/*.vim'), '\n')
\ , 'fnamemodify(v:val, ":t:r")')
if !has_key(ret, item) && item =~ '^'.a:arglead
let ret[item] = 1
endif
endfor
return sort(keys(ret))
endfunction"}}}
function! neosnippet#commands#_complete_target_snippets(arglead, cmdline, cursorpos) "{{{
return map(filter(values(neosnippet#helpers#get_snippets()),
\ "stridx(v:val.word, a:arglead) == 0
\ && v:val.snip =~# neosnippet#get_placeholder_target_marker_pattern()"), 'v:val.word')
endfunction"}}}
2013-11-19 07:19:33 +00:00
function! s:initialize_options(options) "{{{
let default_options = {
\ 'runtime' : 0,
\ 'vertical' : 0,
\ 'direction' : 'below',
\ 'split' : 0,
\ }
let options = extend(default_options, a:options)
" Complex initializer.
if has_key(options, 'horizontal')
" Disable vertically.
let options.vertical = 0
endif
return options
endfunction"}}}
2013-11-19 07:19:33 +00:00
let &cpo = s:save_cpo
unlet s:save_cpo
" vim: foldmethod=marker