diff --git a/autoload/neosnippet.vim b/autoload/neosnippet.vim index ec959b0..1420143 100644 --- a/autoload/neosnippet.vim +++ b/autoload/neosnippet.vim @@ -1,7 +1,7 @@ "============================================================================= " FILE: neosnippet.vim " AUTHOR: Shougo Matsushita -" Last Modified: 18 Nov 2013. +" Last Modified: 19 Nov 2013. " License: MIT license {{{ " Permission is hereby granted, free of charge, to any person obtaining " a copy of this software and associated documentation files (the @@ -45,20 +45,6 @@ let s:neosnippet_options = [ \] "}}} -function! neosnippet#initialize() "{{{ - let s:is_initialized = 1 - - call s:initialize_script_variables() - call s:initialize_others() - call s:initialize_cache() -endfunction"}}} - -function! neosnippet#_check_initialize() "{{{ - if !exists('s:is_initialized') - call neosnippet#initialize() - endif -endfunction"}}} - " For echodoc. "{{{ let s:doc_dict = { \ 'name' : 'neosnippet', @@ -106,7 +92,7 @@ function! neosnippet#caching() "{{{ endfunction"}}} function! neosnippet#recaching() "{{{ - let s:snippets = {} + call neosnippet#variables#set_snippets({}) endfunction"}}} function! s:set_snippet_dict(snippet_dict, snippets, dup_check, snippets_file) "{{{ @@ -178,7 +164,7 @@ function! neosnippet#edit_snippets(args) "{{{ return endif - call neosnippet#_check_initialize() + call neosnippet#init#check() let [args, options] = neosnippet#util#parse_options( \ a:args, s:neosnippet_options) @@ -242,7 +228,7 @@ function! s:initialize_options(options) "{{{ endfunction"}}} function! neosnippet#make_cache(filetype) "{{{ - call neosnippet#_check_initialize() + call neosnippet#init#check() let filetype = a:filetype == '' ? \ &filetype : a:filetype @@ -250,7 +236,8 @@ function! neosnippet#make_cache(filetype) "{{{ let filetype = 'nothing' endif - if has_key(s:snippets, filetype) + let snippets = neosnippet#variables#get_snippets() + if has_key(snippets, filetype) return endif @@ -267,11 +254,12 @@ function! neosnippet#make_cache(filetype) "{{{ call s:parse_snippets_file(snippet, snippets_file) endfor - let s:snippets[filetype] = snippet + let snippets = neosnippet#variables#get_snippets() + let snippets[filetype] = snippet endfunction"}}} function! neosnippet#source_file(filename) "{{{ - call neosnippet#_check_initialize() + call neosnippet#init#check() let neosnippet = neosnippet#get_current_neosnippet() call s:parse_snippets_file(neosnippet.snippets, a:filename) @@ -455,31 +443,34 @@ endfunction"}}} function! neosnippet#jump(cur_text, col) "{{{ call s:skip_next_auto_completion() + let expand_stack = neosnippet#variables#get_expand_stack() + " Get patterns and count. - if empty(s:snippets_expand_stack) + if empty(expand_stack) return s:search_outof_range(a:col) endif - let expand_info = s:snippets_expand_stack[-1] + let expand_info = expand_stack[-1] " Search patterns. - let [begin, end] = s:get_snippet_range( + let [begin, end] = neosnippet#_get_snippet_range( \ expand_info.begin_line, \ expand_info.begin_patterns, \ expand_info.end_line, \ expand_info.end_patterns) - if s:search_snippet_range(begin, end, expand_info.holder_cnt) + if neosnippet#_search_snippet_range(begin, end, expand_info.holder_cnt) " Next count. let expand_info.holder_cnt += 1 return 1 endif " Search placeholder 0. - if s:search_snippet_range(begin, end, 0) + if neosnippet#_search_snippet_range(begin, end, 0) return 1 endif " Not found. - let s:snippets_expand_stack = s:snippets_expand_stack[: -2] + let expand_stack = neosnippet#variables#get_expand_stack() + let expand_stack = expand_stack[: -2] return s:search_outof_range(a:col) endfunction"}}} @@ -546,6 +537,8 @@ function! neosnippet#expand(cur_text, col, trigger_name) "{{{ let &l:foldmethod = 'manual' endif + let expand_stack = neosnippet#variables#get_expand_stack() + try call setline('.', snippet_lines[0]) if len(snippet_lines) > 1 @@ -560,7 +553,7 @@ function! neosnippet#expand(cur_text, col, trigger_name) "{{{ \ [getline(begin_line - 1)] : [] let end_patterns = (end_line < line('$')) ? \ [getline(end_line + 1)] : [] - call add(s:snippets_expand_stack, { + call add(expand_stack, { \ 'begin_line' : begin_line, \ 'begin_patterns' : begin_patterns, \ 'end_line' : end_line, @@ -684,7 +677,7 @@ function! neosnippet#register_oneshot_snippet() "{{{ echo 'Registered trigger : ' . trigger endfunction"}}} -function! s:get_snippet_range(begin_line, begin_patterns, end_line, end_patterns) "{{{ +function! neosnippet#_get_snippet_range(begin_line, begin_patterns, end_line, end_patterns) "{{{ let pos = getpos('.') call cursor(a:begin_line, 0) @@ -718,7 +711,7 @@ function! s:get_snippet_range(begin_line, begin_patterns, end_line, end_patterns call setpos('.', pos) return [begin, end] endfunction"}}} -function! s:search_snippet_range(start, end, cnt, ...) "{{{ +function! neosnippet#_search_snippet_range(start, end, cnt, ...) "{{{ let is_select = get(a:000, 0, 1) call s:substitute_placeholder_marker(a:start, a:end, a:cnt) @@ -970,13 +963,14 @@ function! neosnippet#get_current_neosnippet() "{{{ return b:neosnippet endfunction"}}} function! neosnippet#get_snippets() "{{{ - call neosnippet#_check_initialize() + call neosnippet#init#check() let neosnippet = neosnippet#get_current_neosnippet() let snippets = copy(neosnippet.snippets) for filetype in s:get_sources_filetypes(neosnippet#get_filetype()) call neosnippet#make_cache(filetype) - call extend(snippets, s:snippets[filetype], 'keep') + call extend(snippets, + \ neosnippet#variables#get_snippets()[filetype], 'keep') endfor let cur_text = neosnippet#util#get_cur_text() @@ -993,26 +987,20 @@ function! neosnippet#get_snippets() "{{{ return snippets endfunction"}}} function! neosnippet#get_snippets_directory() "{{{ - call neosnippet#_check_initialize() - - let snippets_dir = copy(s:snippets_dir) + let snippets_dir = copy(neosnippet#variables#get_snippets_dir()) if !get(g:neosnippet#disable_runtime_snippets, \ neosnippet#get_filetype(), \ get(g:neosnippet#disable_runtime_snippets, '_', 0)) - let snippets_dir += s:runtime_dir + let snippets_dir += neosnippet#variables#get_runtime_dir() endif return snippets_dir endfunction"}}} function! neosnippet#get_user_snippets_directory() "{{{ - call neosnippet#_check_initialize() - - return copy(s:snippets_dir) + return copy(neosnippet#variables#get_snippets_dir()) endfunction"}}} function! neosnippet#get_runtime_snippets_directory() "{{{ - call neosnippet#_check_initialize() - - return copy(s:runtime_dir) + return copy(neosnippet#variables#get_runtime_dir()) endfunction"}}} function! neosnippet#get_filetype() "{{{ if !exists('s:exists_context_filetype') @@ -1206,50 +1194,6 @@ function! s:skip_next_auto_completion() "{{{ let neosnippet.trigger = 0 endfunction"}}} -function! s:on_insert_leave() "{{{ - " Get patterns and count. - if empty(s:snippets_expand_stack) - \ || neosnippet#get_current_neosnippet().trigger - return - endif - - let expand_info = s:snippets_expand_stack[-1] - - if expand_info.begin_line != expand_info.end_line - return - endif - - " Search patterns. - let [begin, end] = s: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 s: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 s:search_snippet_range(begin, end, 0) - let found = 1 - endif - finally - if found - stopinsert - endif - - call setpos('.', pos) - endtry -endfunction"}}} - if g:neosnippet#enable_snipmate_compatibility " For snipMate function. function! Filename(...) @@ -1264,84 +1208,6 @@ if g:neosnippet#enable_snipmate_compatibility endfunction endif -function! s:initialize_script_variables() "{{{ - " Initialize. - let s:snippets_expand_stack = [] - let s:snippets = {} - let s:runtime_dir = [] - - " Set runtime dir. - let s:runtime_dir += split(globpath(&runtimepath, - \ 'autoload/neosnippet/snippets'), '\n') - if g:neosnippet#enable_snipmate_compatibility - " Load snipMate snippet directories. - let s:runtime_dir += split(globpath(&runtimepath, - \ 'snippets'), '\n') - endif - let s:runtime_dir += (exists('g:snippets_dir') ? - \ split(g:snippets_dir, '\s*,\s*') - \ : split(globpath(&runtimepath, 'snippets'), '\n')) - call map(s:runtime_dir, 'substitute(v:val, "[\\\\/]$", "", "")') - - " Set snippets_dir. - let s:snippets_dir = [] - for dir in neosnippet#util#option2list(g:neosnippet#snippets_directory) - let dir = neosnippet#util#expand(dir) - if !isdirectory(dir) && !neosnippet#util#is_sudo() - call mkdir(dir, 'p') - endif - call add(s:snippets_dir, dir) - endfor - call map(s:snippets_dir, 'substitute(v:val, "[\\\\/]$", "", "")') -endfunction"}}} -function! s:initialize_cache() "{{{ - " Caching _ snippets. - call neosnippet#make_cache('_') - - " Initialize check. - call neosnippet#caching() -endfunction"}}} -function! s:initialize_others() "{{{ - augroup neosnippet "{{{ - autocmd! - " Set caching event. - autocmd FileType * call neosnippet#caching() - " Recaching events - autocmd BufWritePost *.snip,*.snippets - \ call neosnippet#recaching() - autocmd BufEnter * - \ call neosnippet#clear_select_mode_mappings() - autocmd InsertLeave * call s:on_insert_leave() - augroup END"}}} - - augroup neosnippet - autocmd BufNewFile,BufRead,Syntax * - \ execute 'syntax match neosnippetExpandSnippets' - \ "'".neosnippet#get_placeholder_marker_pattern(). '\|' - \ .neosnippet#get_sync_placeholder_marker_pattern().'\|' - \ .neosnippet#get_mirror_placeholder_marker_pattern()."'" - \ 'containedin=ALL oneline' - if has('conceal') - autocmd BufNewFile,BufRead,Syntax * - \ syntax region neosnippetConcealExpandSnippets - \ matchgroup=neosnippetExpandSnippets - \ start='<`\d\+:\=\|<{\d\+:\=\|<|' - \ end='`>\|}>\||>' - \ containedin=ALL - \ concealends oneline - endif - augroup END - doautocmd neosnippet BufRead - - hi def link neosnippetExpandSnippets Special - - if get(g:, 'loaded_echodoc', 0) - call echodoc#register('snippets_complete', s:doc_dict) - endif - - call neosnippet#clear_select_mode_mappings() -endfunction"}}} - let &cpo = s:save_cpo unlet s:save_cpo diff --git a/autoload/neosnippet/init.vim b/autoload/neosnippet/init.vim new file mode 100644 index 0000000..d437b2a --- /dev/null +++ b/autoload/neosnippet/init.vim @@ -0,0 +1,167 @@ +"============================================================================= +" FILE: init.vim +" AUTHOR: Shougo Matsushita +" Last Modified: 19 Nov 2013. +" 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 + +function! neosnippet#init#_initialize() "{{{ + let s:is_initialized = 1 + + call s:initialize_script_variables() + call s:initialize_others() + call s:initialize_cache() +endfunction"}}} + +function! neosnippet#init#check() "{{{ + if !exists('s:is_initialized') + call neosnippet#init#_initialize() + endif +endfunction"}}} + +function! s:initialize_script_variables() "{{{ + " Set runtime dir. + let runtime_dir = neosnippet#variables#get_runtime_dir() + let runtime_dir += split(globpath(&runtimepath, + \ 'autoload/neosnippet/snippets'), '\n') + if g:neosnippet#enable_snipmate_compatibility + " Load snipMate snippet directories. + let runtime_dir += split(globpath(&runtimepath, + \ 'snippets'), '\n') + endif + let runtime_dir += (exists('g:snippets_dir') ? + \ split(g:snippets_dir, '\s*,\s*') + \ : split(globpath(&runtimepath, 'snippets'), '\n')) + call map(runtime_dir, 'substitute(v:val, "[\\\\/]$", "", "")') + + " Set snippets_dir. + let snippets_dir = neosnippet#variables#get_snippets_dir() + for dir in neosnippet#util#option2list(g:neosnippet#snippets_directory) + let dir = neosnippet#util#expand(dir) + if !isdirectory(dir) && !neosnippet#util#is_sudo() + call mkdir(dir, 'p') + endif + call add(snippets_dir, dir) + endfor + call map(snippets_dir, 'substitute(v:val, "[\\\\/]$", "", "")') +endfunction"}}} +function! s:initialize_cache() "{{{ + " Caching _ snippets. + call neosnippet#make_cache('_') + + " Initialize check. + call neosnippet#caching() +endfunction"}}} +function! s:initialize_others() "{{{ + augroup neosnippet "{{{ + autocmd! + " Set caching event. + autocmd FileType * call neosnippet#caching() + " Recaching events + autocmd BufWritePost *.snip,*.snippets + \ call neosnippet#recaching() + autocmd BufEnter * + \ call neosnippet#clear_select_mode_mappings() + autocmd InsertLeave * call s:on_insert_leave() + augroup END"}}} + + augroup neosnippet + autocmd BufNewFile,BufRead,Syntax * + \ execute 'syntax match neosnippetExpandSnippets' + \ "'".neosnippet#get_placeholder_marker_pattern(). '\|' + \ .neosnippet#get_sync_placeholder_marker_pattern().'\|' + \ .neosnippet#get_mirror_placeholder_marker_pattern()."'" + \ 'containedin=ALL oneline' + if has('conceal') + autocmd BufNewFile,BufRead,Syntax * + \ syntax region neosnippetConcealExpandSnippets + \ matchgroup=neosnippetExpandSnippets + \ start='<`\d\+:\=\|<{\d\+:\=\|<|' + \ end='`>\|}>\||>' + \ containedin=ALL + \ concealends oneline + endif + augroup END + doautocmd neosnippet BufRead + + hi def link neosnippetExpandSnippets Special + + if get(g:, 'loaded_echodoc', 0) + call echodoc#register('snippets_complete', s:doc_dict) + endif + + call neosnippet#clear_select_mode_mappings() +endfunction"}}} + +function! s:on_insert_leave() "{{{ + let expand_stack = neosnippet#variables#get_expand_stack() + + " Get patterns and count. + if empty(expand_stack) + \ || neosnippet#get_current_neosnippet().trigger + return + endif + + let expand_info = expand_stack[-1] + + if expand_info.begin_line != expand_info.end_line + return + endif + + " Search patterns. + let [begin, end] = neosnippet#_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#_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#_search_snippet_range(begin, end, 0) + let found = 1 + endif + finally + if found + stopinsert + endif + + call setpos('.', pos) + endtry +endfunction"}}} + +let &cpo = s:save_cpo +unlet s:save_cpo + +" vim: foldmethod=marker diff --git a/autoload/neosnippet/mappings.vim b/autoload/neosnippet/mappings.vim index 921f147..adeb0af 100644 --- a/autoload/neosnippet/mappings.vim +++ b/autoload/neosnippet/mappings.vim @@ -1,7 +1,7 @@ "============================================================================= " FILE: mappings.vim " AUTHOR: Shougo Matsushita -" Last Modified: 18 Nov 2013. +" Last Modified: 19 Nov 2013. " License: MIT license {{{ " Permission is hereby granted, free of charge, to any person obtaining " a copy of this software and associated documentation files (the @@ -85,7 +85,7 @@ function! s:SID_PREFIX() "{{{ endfunction"}}} function! s:trigger(function) "{{{ - call neosnippet#_check_initialize() + call neosnippet#init#check() let cur_text = neosnippet#util#get_cur_text() diff --git a/plugin/neosnippet.vim b/plugin/neosnippet.vim index 2303c0b..77cc284 100644 --- a/plugin/neosnippet.vim +++ b/plugin/neosnippet.vim @@ -1,7 +1,7 @@ "============================================================================= " FILE: neosnippet.vim " AUTHOR: Shougo Matsushita -" Last Modified: 18 Nov 2013. +" Last Modified: 19 Nov 2013. " License: MIT license {{{ " Permission is hereby granted, free of charge, to any person obtaining " a copy of this software and associated documentation files (the @@ -70,7 +70,7 @@ inoremap (neosnippet_start_unite_snippet) "}}} augroup neosnippet "{{{ - autocmd InsertEnter * call neosnippet#initialize() + autocmd InsertEnter * call neosnippet#init#_initialize() augroup END"}}} " Commands. "{{{