From 3ff291843375f4f29e3224b768a421632e99ef9b Mon Sep 17 00:00:00 2001 From: Shougo Matsushita Date: Mon, 18 Nov 2013 20:14:01 +0900 Subject: [PATCH 1/4] - Splitted mappings file. --- autoload/neosnippet.vim | 174 +++++++++---------------------- autoload/neosnippet/mappings.vim | 129 +++++++++++++++++++++++ plugin/neosnippet.vim | 18 ++-- 3 files changed, 187 insertions(+), 134 deletions(-) create mode 100644 autoload/neosnippet/mappings.vim diff --git a/autoload/neosnippet.vim b/autoload/neosnippet.vim index 2930514..ec959b0 100644 --- a/autoload/neosnippet.vim +++ b/autoload/neosnippet.vim @@ -53,7 +53,7 @@ function! neosnippet#initialize() "{{{ call s:initialize_cache() endfunction"}}} -function! s:check_initialize() "{{{ +function! neosnippet#_check_initialize() "{{{ if !exists('s:is_initialized') call neosnippet#initialize() endif @@ -72,7 +72,7 @@ function! s:doc_dict.search(cur_text) "{{{ let snippets = neosnippet#get_snippets() - let cur_word = s:get_cursor_snippet(snippets, a:cur_text) + let cur_word = neosnippet#get_cursor_snippet(snippets, a:cur_text) if cur_word == '' return [] endif @@ -92,13 +92,13 @@ function! neosnippet#expandable_or_jumpable() "{{{ endfunction"}}} function! neosnippet#expandable() "{{{ " Check snippet trigger. - return s:get_cursor_snippet( + return neosnippet#get_cursor_snippet( \ neosnippet#get_snippets(), neosnippet#util#get_cur_text()) != '' endfunction"}}} function! neosnippet#jumpable() "{{{ " Found snippet placeholder. - return search(s:get_placeholder_marker_pattern(). '\|' - \ .s:get_sync_placeholder_marker_pattern(), 'nw') > 0 + return search(neosnippet#get_placeholder_marker_pattern(). '\|' + \ .neosnippet#get_sync_placeholder_marker_pattern(), 'nw') > 0 endfunction"}}} function! neosnippet#caching() "{{{ @@ -133,7 +133,7 @@ endfunction"}}} function! s:initialize_snippet(dict, path, line, pattern, name) "{{{ let a:dict.word = substitute(a:dict.word, '\n\+$', '', '') if a:dict.word !~ - \ s:get_placeholder_marker_substitute_pattern() + \ neosnippet#get_placeholder_marker_substitute_pattern() " Add placeholder. let a:dict.word .= '${0}' endif @@ -143,8 +143,8 @@ function! s:initialize_snippet(dict, path, line, pattern, name) "{{{ if !has_key(a:dict, 'abbr') || a:dict.abbr == '' " Set default abbr. let abbr = substitute(a:dict.word, - \ s:get_placeholder_marker_pattern(). '\|'. - \ s:get_mirror_placeholder_marker_pattern(). + \ neosnippet#get_placeholder_marker_pattern(). '\|'. + \ neosnippet#get_mirror_placeholder_marker_pattern(). \ '\|\s\+\|\n\|TARGET', ' ', 'g') let a:dict.abbr = a:dict.name else @@ -178,7 +178,7 @@ function! neosnippet#edit_snippets(args) "{{{ return endif - call s:check_initialize() + call neosnippet#_check_initialize() let [args, options] = neosnippet#util#parse_options( \ a:args, s:neosnippet_options) @@ -242,7 +242,7 @@ function! s:initialize_options(options) "{{{ endfunction"}}} function! neosnippet#make_cache(filetype) "{{{ - call s:check_initialize() + call neosnippet#_check_initialize() let filetype = a:filetype == '' ? \ &filetype : a:filetype @@ -271,7 +271,7 @@ function! neosnippet#make_cache(filetype) "{{{ endfunction"}}} function! neosnippet#source_file(filename) "{{{ - call s:check_initialize() + call neosnippet#_check_initialize() let neosnippet = neosnippet#get_current_neosnippet() call s:parse_snippets_file(neosnippet.snippets, a:filename) @@ -434,7 +434,7 @@ function! s:is_beginning_of_line(cur_text) "{{{ return prev_word_end <= 0 endfunction"}}} -function! s:get_cursor_snippet(snippets, cur_text) "{{{ +function! neosnippet#get_cursor_snippet(snippets, cur_text) "{{{ let cur_word = matchstr(a:cur_text, '\S\+$') if cur_word != '' && has_key(a:snippets, cur_word) return cur_word @@ -452,14 +452,6 @@ function! s:get_cursor_snippet(snippets, cur_text) "{{{ return cur_word endfunction"}}} -function! s:snippets_expand(cur_text, col) "{{{ - let cur_word = s:get_cursor_snippet( - \ neosnippet#get_snippets(), - \ a:cur_text) - - call neosnippet#expand( - \ a:cur_text, a:col, cur_word) -endfunction"}}} function! neosnippet#jump(cur_text, col) "{{{ call s:skip_next_auto_completion() @@ -491,31 +483,6 @@ function! neosnippet#jump(cur_text, col) "{{{ return s:search_outof_range(a:col) endfunction"}}} -function! s:snippets_expand_or_jump(cur_text, col) "{{{ - let cur_word = s:get_cursor_snippet( - \ neosnippet#get_snippets(), a:cur_text) - - if cur_word != '' - " Found snippet trigger. - call neosnippet#expand( - \ a:cur_text, a:col, cur_word) - else - call neosnippet#jump(a:cur_text, a:col) - endif -endfunction"}}} -function! s:snippets_jump_or_expand(cur_text, col) "{{{ - let cur_word = s:get_cursor_snippet( - \ neosnippet#get_snippets(), a:cur_text) - if search(s:get_placeholder_marker_pattern(). '\|' - \ .s:get_sync_placeholder_marker_pattern(), 'nw') > 0 - " Found snippet placeholder. - call neosnippet#jump(a:cur_text, a:col) - else - call neosnippet#expand( - \ a:cur_text, a:col, cur_word) - endif -endfunction"}}} - function! neosnippet#expand(cur_text, col, trigger_name) "{{{ call s:skip_next_auto_completion() @@ -548,14 +515,14 @@ function! neosnippet#expand(cur_text, col, trigger_name) "{{{ " Substitute markers. let snip_word = substitute(snip_word, - \ '\\\@', 'g') let snip_word = substitute(snip_word, - \ '\\\@', 'g') let snip_word = substitute(snip_word, - \ '\\'.s:get_mirror_placeholder_marker_substitute_pattern().'\|'. - \ '\\'.s:get_placeholder_marker_substitute_pattern(), + \ '\\'.neosnippet#get_mirror_placeholder_marker_substitute_pattern().'\|'. + \ '\\'.neosnippet#get_placeholder_marker_substitute_pattern(), \ '\=submatch(0)[1:]', 'g') " Insert snippets. @@ -601,7 +568,7 @@ function! neosnippet#expand(cur_text, col, trigger_name) "{{{ \ 'holder_cnt' : 1, \ }) - if snip_word =~ s:get_placeholder_marker_pattern() + if snip_word =~ neosnippet#get_placeholder_marker_pattern() call neosnippet#jump(a:cur_text, a:col) endif finally @@ -756,7 +723,7 @@ function! s:search_snippet_range(start, end, cnt, ...) "{{{ call s:substitute_placeholder_marker(a:start, a:end, a:cnt) " Search marker pattern. - let pattern = substitute(s:get_placeholder_marker_pattern(), + let pattern = substitute(neosnippet#get_placeholder_marker_pattern(), \ '\\d\\+', a:cnt, '') for line in filter(range(a:start, a:end), @@ -770,7 +737,7 @@ endfunction"}}} function! s:search_outof_range(col) "{{{ call s:substitute_placeholder_marker(1, 0, 0) - let pattern = s:get_placeholder_marker_pattern() + let pattern = neosnippet#get_placeholder_marker_pattern() if search(pattern, 'w') > 0 call s:expand_placeholder(line('.'), 0, '\\d\\+', line('.')) return 1 @@ -795,14 +762,14 @@ endfunction"}}} function! s:expand_placeholder(start, end, holder_cnt, line, ...) "{{{ let is_select = get(a:000, 0, 1) - let pattern = substitute(s:get_placeholder_marker_pattern(), + let pattern = substitute(neosnippet#get_placeholder_marker_pattern(), \ '\\d\\+', a:holder_cnt, '') let current_line = getline(a:line) let match = match(current_line, pattern) let neosnippet = neosnippet#get_current_neosnippet() let default_pattern = substitute( - \ s:get_placeholder_marker_default_pattern(), + \ neosnippet#get_placeholder_marker_default_pattern(), \ '\\d\\+', a:holder_cnt, '') let default = substitute( \ matchstr(current_line, default_pattern), @@ -821,10 +788,10 @@ function! s:expand_placeholder(start, end, holder_cnt, line, ...) "{{{ " Substitute marker. let default = substitute(default, - \ s:get_placeholder_marker_substitute_pattern(), + \ neosnippet#get_placeholder_marker_substitute_pattern(), \ '<`\1`>', 'g') let default = substitute(default, - \ s:get_mirror_placeholder_marker_substitute_pattern(), + \ neosnippet#get_mirror_placeholder_marker_substitute_pattern(), \ '<|\1|>', 'g') " len() cannot use for multibyte. @@ -836,7 +803,7 @@ function! s:expand_placeholder(start, end, holder_cnt, line, ...) "{{{ let cnt = s:search_sync_placeholder(a:start, a:end, a:holder_cnt) if cnt >= 0 - let pattern = substitute(s:get_placeholder_marker_pattern(), + let pattern = substitute(neosnippet#get_placeholder_marker_pattern(), \ '\\d\\+', cnt, '') call setline(a:line, substitute(current_line, pattern, \ '<{'.cnt.':'.escape(default, '\').'}>', '')) @@ -915,15 +882,15 @@ function! s:search_sync_placeholder(start, end, number) "{{{ if a:end == 0 " Search in current buffer. let cnt = matchstr(getline('.'), - \ substitute(s:get_placeholder_marker_pattern(), + \ substitute(neosnippet#get_placeholder_marker_pattern(), \ '\\d\\+', '\\zs\\d\\+\\ze', '')) return search(substitute( - \ s:get_mirror_placeholder_marker_pattern(), + \ neosnippet#get_mirror_placeholder_marker_pattern(), \ '\\d\\+', cnt, ''), 'nw') > 0 ? cnt : -1 endif let pattern = substitute( - \ s:get_mirror_placeholder_marker_pattern(), + \ neosnippet#get_mirror_placeholder_marker_pattern(), \ '\\d\\+', a:number, '') for line in filter(range(a:start, a:end), \ 'getline(v:val) =~ pattern') @@ -935,35 +902,35 @@ endfunction"}}} function! s:substitute_placeholder_marker(start, end, snippet_holder_cnt) "{{{ if a:snippet_holder_cnt > 0 let cnt = a:snippet_holder_cnt-1 - let sync_marker = substitute(s:get_sync_placeholder_marker_pattern(), + let sync_marker = substitute(neosnippet#get_sync_placeholder_marker_pattern(), \ '\\d\\+', cnt, '') let mirror_marker = substitute( - \ s:get_mirror_placeholder_marker_pattern(), + \ neosnippet#get_mirror_placeholder_marker_pattern(), \ '\\d\\+', cnt, '') let line = a:start for line in range(a:start, a:end) if getline(line) =~ sync_marker let sub = escape(matchstr(getline(line), - \ substitute(s:get_sync_placeholder_marker_default_pattern(), + \ substitute(neosnippet#get_sync_placeholder_marker_default_pattern(), \ '\\d\\+', cnt, '')), '/\') silent execute printf('%d,%ds/\m' . mirror_marker . '/%s/' \ . (&gdefault ? '' : 'g'), a:start, a:end, sub) call setline(line, substitute(getline(line), sync_marker, sub, '')) endif endfor - elseif search(s:get_sync_placeholder_marker_pattern(), 'wb') > 0 + elseif search(neosnippet#get_sync_placeholder_marker_pattern(), 'wb') > 0 let sub = escape(matchstr(getline('.'), - \ s:get_sync_placeholder_marker_default_pattern()), '/\') + \ neosnippet#get_sync_placeholder_marker_default_pattern()), '/\') let cnt = matchstr(getline('.'), - \ substitute(s:get_sync_placeholder_marker_pattern(), + \ substitute(neosnippet#get_sync_placeholder_marker_pattern(), \ '\\d\\+', '\\zs\\d\\+\\ze', '')) let mirror_marker = substitute( - \ s:get_mirror_placeholder_marker_pattern(), + \ neosnippet#get_mirror_placeholder_marker_pattern(), \ '\\d\\+', cnt, '') silent execute printf('%%s/\m' . mirror_marker . '/%s/' \ . (&gdefault ? 'g' : ''), sub) - let sync_marker = substitute(s:get_sync_placeholder_marker_pattern(), + let sync_marker = substitute(neosnippet#get_sync_placeholder_marker_pattern(), \ '\\d\\+', cnt, '') call setline('.', substitute(getline('.'), sync_marker, sub, '')) endif @@ -1003,7 +970,7 @@ function! neosnippet#get_current_neosnippet() "{{{ return b:neosnippet endfunction"}}} function! neosnippet#get_snippets() "{{{ - call s:check_initialize() + call neosnippet#_check_initialize() let neosnippet = neosnippet#get_current_neosnippet() let snippets = copy(neosnippet.snippets) @@ -1026,7 +993,7 @@ function! neosnippet#get_snippets() "{{{ return snippets endfunction"}}} function! neosnippet#get_snippets_directory() "{{{ - call s:check_initialize() + call neosnippet#_check_initialize() let snippets_dir = copy(s:snippets_dir) if !get(g:neosnippet#disable_runtime_snippets, @@ -1038,12 +1005,12 @@ function! neosnippet#get_snippets_directory() "{{{ return snippets_dir endfunction"}}} function! neosnippet#get_user_snippets_directory() "{{{ - call s:check_initialize() + call neosnippet#_check_initialize() return copy(s:snippets_dir) endfunction"}}} function! neosnippet#get_runtime_snippets_directory() "{{{ - call s:check_initialize() + call neosnippet#_check_initialize() return copy(s:runtime_dir) endfunction"}}} @@ -1106,57 +1073,28 @@ endfunction"}}} function! neosnippet#get_placeholder_target_marker_pattern() "{{{ return '\${\d\+:TARGET\%(:.\{-}\)\?\\\@' endfunction"}}} -function! s:get_placeholder_marker_substitute_pattern() "{{{ +function! neosnippet#get_placeholder_marker_substitute_pattern() "{{{ return '\${\(\d\+\%(:.\{-}\)\?\\\@' endfunction"}}} -function! s:get_sync_placeholder_marker_pattern() "{{{ +function! neosnippet#get_sync_placeholder_marker_pattern() "{{{ return '<{\d\+\%(:.\{-}\)\?\\\@' endfunction"}}} -function! s:get_sync_placeholder_marker_default_pattern() "{{{ +function! neosnippet#get_sync_placeholder_marker_default_pattern() "{{{ return '<{\d\+:\zs.\{-}\ze\\\@' endfunction"}}} -function! s:get_mirror_placeholder_marker_pattern() "{{{ +function! neosnippet#get_mirror_placeholder_marker_pattern() "{{{ return '<|\d\+|>' endfunction"}}} -function! s:get_mirror_placeholder_marker_substitute_pattern() "{{{ +function! neosnippet#get_mirror_placeholder_marker_substitute_pattern() "{{{ return '\$\(\d\+\)' endfunction"}}} -function! s:SID_PREFIX() "{{{ - return matchstr(expand(''), '\d\+_') -endfunction"}}} - -function! s:trigger(function) "{{{ - call s:check_initialize() - - let cur_text = neosnippet#util#get_cur_text() - - let col = col('.') - let expr = '' - if mode() !=# 'i' - " Fix column. - let col += 2 - endif - - " Get selected text. - let neosnippet = neosnippet#get_current_neosnippet() - let neosnippet.trigger = 1 - if mode() ==# 's' && neosnippet.selected_text =~ '^#:' - let expr .= "a\" - endif - - let expr .= printf("\:call %s(%s,%d)\", - \ a:function, string(cur_text), col) - - return expr -endfunction"}}} - function! neosnippet#get_selected_text(type, ...) "{{{ let sel_save = &selection let &selection = 'inclusive' @@ -1326,20 +1264,6 @@ if g:neosnippet#enable_snipmate_compatibility endfunction endif -" Plugin key-mappings. -function! neosnippet#expand_or_jump_impl() - return s:trigger(s:SID_PREFIX().'snippets_expand_or_jump') -endfunction -function! neosnippet#jump_or_expand_impl() - return s:trigger(s:SID_PREFIX().'snippets_jump_or_expand') -endfunction -function! neosnippet#expand_impl() - return s:trigger(s:SID_PREFIX().'snippets_expand') -endfunction -function! neosnippet#jump_impl() - return s:trigger('neosnippet#jump') -endfunction - function! s:initialize_script_variables() "{{{ " Initialize. let s:snippets_expand_stack = [] @@ -1393,9 +1317,9 @@ function! s:initialize_others() "{{{ augroup neosnippet autocmd BufNewFile,BufRead,Syntax * \ execute 'syntax match neosnippetExpandSnippets' - \ "'".s:get_placeholder_marker_pattern(). '\|' - \ .s:get_sync_placeholder_marker_pattern().'\|' - \ .s:get_mirror_placeholder_marker_pattern()."'" + \ "'".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 * diff --git a/autoload/neosnippet/mappings.vim b/autoload/neosnippet/mappings.vim new file mode 100644 index 0000000..921f147 --- /dev/null +++ b/autoload/neosnippet/mappings.vim @@ -0,0 +1,129 @@ +"============================================================================= +" FILE: mappings.vim +" AUTHOR: Shougo Matsushita +" Last Modified: 18 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! s:snippets_expand(cur_text, col) "{{{ + let cur_word = neosnippet#get_cursor_snippet( + \ neosnippet#get_snippets(), + \ a:cur_text) + + call neosnippet#expand( + \ a:cur_text, a:col, cur_word) +endfunction"}}} + +function! neosnippet#mappings#_get_cursor_snippet(snippets, cur_text) "{{{ + let cur_word = matchstr(a:cur_text, '\S\+$') + if cur_word != '' && has_key(a:snippets, cur_word) + return cur_word + endif + + while cur_word != '' + if has_key(a:snippets, cur_word) && + \ a:snippets[cur_word].options.word + return cur_word + endif + + let cur_word = substitute(cur_word, '^\%(\w\+\|\W\)', '', '') + endwhile + + return cur_word +endfunction"}}} + +function! s:snippets_expand_or_jump(cur_text, col) "{{{ + let cur_word = neosnippet#get_cursor_snippet( + \ neosnippet#get_snippets(), a:cur_text) + + if cur_word != '' + " Found snippet trigger. + call neosnippet#expand( + \ a:cur_text, a:col, cur_word) + else + call neosnippet#jump(a:cur_text, a:col) + endif +endfunction"}}} + +function! s:snippets_jump_or_expand(cur_text, col) "{{{ + let cur_word = neosnippet#get_cursor_snippet( + \ neosnippet#get_snippets(), a:cur_text) + if search(neosnippet#get_placeholder_marker_pattern(). '\|' + \ .neosnippet#get_sync_placeholder_marker_pattern(), 'nw') > 0 + " Found snippet placeholder. + call neosnippet#jump(a:cur_text, a:col) + else + call neosnippet#expand( + \ a:cur_text, a:col, cur_word) + endif +endfunction"}}} + +function! s:SID_PREFIX() "{{{ + return matchstr(expand(''), '\d\+_') +endfunction"}}} + +function! s:trigger(function) "{{{ + call neosnippet#_check_initialize() + + let cur_text = neosnippet#util#get_cur_text() + + let col = col('.') + let expr = '' + if mode() !=# 'i' + " Fix column. + let col += 2 + endif + + " Get selected text. + let neosnippet = neosnippet#get_current_neosnippet() + let neosnippet.trigger = 1 + if mode() ==# 's' && neosnippet.selected_text =~ '^#:' + let expr .= "a\" + endif + + let expr .= printf("\:call %s(%s,%d)\", + \ a:function, string(cur_text), col) + + return expr +endfunction"}}} + +" Plugin key-mappings. +function! neosnippet#mappings#expand_or_jump_impl() + return s:trigger(s:SID_PREFIX().'snippets_expand_or_jump') +endfunction +function! neosnippet#mappings#jump_or_expand_impl() + return s:trigger(s:SID_PREFIX().'snippets_jump_or_expand') +endfunction +function! neosnippet#mappings#expand_impl() + return s:trigger(s:SID_PREFIX().'snippets_expand') +endfunction +function! neosnippet#mappings#jump_impl() + return s:trigger('neosnippet#jump') +endfunction + +let &cpo = s:save_cpo +unlet s:save_cpo + +" vim: foldmethod=marker diff --git a/plugin/neosnippet.vim b/plugin/neosnippet.vim index 9a66c76..2303c0b 100644 --- a/plugin/neosnippet.vim +++ b/plugin/neosnippet.vim @@ -1,7 +1,7 @@ "============================================================================= " FILE: neosnippet.vim " AUTHOR: Shougo Matsushita -" Last Modified: 04 Jun 2013. +" Last Modified: 18 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 @@ -39,21 +39,21 @@ set cpo&vim " Plugin key-mappings. "{{{ inoremap (neosnippet_expand_or_jump) - \ neosnippet#expand_or_jump_impl() + \ neosnippet#mappings#expand_or_jump_impl() inoremap (neosnippet_jump_or_expand) - \ neosnippet#jump_or_expand_impl() + \ neosnippet#mappings#jump_or_expand_impl() inoremap (neosnippet_expand) - \ neosnippet#expand_impl() + \ neosnippet#mappings#expand_impl() inoremap (neosnippet_jump) - \ neosnippet#jump_impl() + \ neosnippet#mappings#jump_impl() snoremap (neosnippet_expand_or_jump) - \ neosnippet#expand_or_jump_impl() + \ neosnippet#mappings#expand_or_jump_impl() snoremap (neosnippet_jump_or_expand) - \ neosnippet#jump_or_expand_impl() + \ neosnippet#mappings#jump_or_expand_impl() snoremap (neosnippet_expand) - \ neosnippet#expand_impl() + \ neosnippet#mappings#expand_impl() snoremap (neosnippet_jump) - \ neosnippet#jump_impl() + \ neosnippet#mappings#jump_impl() xnoremap (neosnippet_get_selected_text) \ :call neosnippet#get_selected_text(visualmode(), 1) From 3f4e7a6ea9e71c8aa4ee912dc431cd1f3008cf56 Mon Sep 17 00:00:00 2001 From: Shougo Matsushita Date: Tue, 19 Nov 2013 16:04:32 +0900 Subject: [PATCH 2/4] - Improved initializer. --- autoload/neosnippet.vim | 194 +++++-------------------------- autoload/neosnippet/init.vim | 167 ++++++++++++++++++++++++++ autoload/neosnippet/mappings.vim | 4 +- plugin/neosnippet.vim | 4 +- 4 files changed, 201 insertions(+), 168 deletions(-) create mode 100644 autoload/neosnippet/init.vim 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. "{{{ From c3ca7a9ae2088c50452c73e5a9ba2a6a4d058cea Mon Sep 17 00:00:00 2001 From: Shougo Matsushita Date: Tue, 19 Nov 2013 16:07:04 +0900 Subject: [PATCH 3/4] - Improved clear mappings. --- autoload/neosnippet.vim | 26 ------------------- autoload/neosnippet/init.vim | 4 +-- autoload/neosnippet/mappings.vim | 44 +++++++++++++++++++++++++------- doc/neosnippet.txt | 2 +- 4 files changed, 38 insertions(+), 38 deletions(-) diff --git a/autoload/neosnippet.vim b/autoload/neosnippet.vim index 1420143..f5909ef 100644 --- a/autoload/neosnippet.vim +++ b/autoload/neosnippet.vim @@ -1155,32 +1155,6 @@ function! neosnippet#substitute_selected_text(type, text) "{{{ endtry endfunction"}}} -function! neosnippet#clear_select_mode_mappings() "{{{ - if !g:neosnippet#disable_select_mode_mappings - return - endif - - redir => mappings - silent! smap - redir END - - for line in map(filter(split(mappings, '\n'), - \ "v:val !~# '^s'"), - \ "substitute(v:val, '', '', 'g')") - let map = matchstr(line, '^\a*\s*\zs\S\+') - let map = substitute(map, '', '', 'g') - - silent! execute 'sunmap' map - silent! execute 'sunmap ' map - endfor - - " Define default select mode mappings. - snoremap a - snoremap a - snoremap a - snoremap a -endfunction"}}} - function! s:skip_next_auto_completion() "{{{ " Skip next auto completion. if exists('*neocomplcache#skip_next_complete') diff --git a/autoload/neosnippet/init.vim b/autoload/neosnippet/init.vim index d437b2a..3e30f0b 100644 --- a/autoload/neosnippet/init.vim +++ b/autoload/neosnippet/init.vim @@ -83,7 +83,7 @@ function! s:initialize_others() "{{{ autocmd BufWritePost *.snip,*.snippets \ call neosnippet#recaching() autocmd BufEnter * - \ call neosnippet#clear_select_mode_mappings() + \ call neosnippet#mappings#_clear_select_mode_mappings() autocmd InsertLeave * call s:on_insert_leave() augroup END"}}} @@ -112,7 +112,7 @@ function! s:initialize_others() "{{{ call echodoc#register('snippets_complete', s:doc_dict) endif - call neosnippet#clear_select_mode_mappings() + call neosnippet#mappings#_clear_select_mode_mappings() endfunction"}}} function! s:on_insert_leave() "{{{ diff --git a/autoload/neosnippet/mappings.vim b/autoload/neosnippet/mappings.vim index adeb0af..85fdbf0 100644 --- a/autoload/neosnippet/mappings.vim +++ b/autoload/neosnippet/mappings.vim @@ -27,15 +27,6 @@ let s:save_cpo = &cpo set cpo&vim -function! s:snippets_expand(cur_text, col) "{{{ - let cur_word = neosnippet#get_cursor_snippet( - \ neosnippet#get_snippets(), - \ a:cur_text) - - call neosnippet#expand( - \ a:cur_text, a:col, cur_word) -endfunction"}}} - function! neosnippet#mappings#_get_cursor_snippet(snippets, cur_text) "{{{ let cur_word = matchstr(a:cur_text, '\S\+$') if cur_word != '' && has_key(a:snippets, cur_word) @@ -54,6 +45,41 @@ function! neosnippet#mappings#_get_cursor_snippet(snippets, cur_text) "{{{ return cur_word endfunction"}}} +function! neosnippet#mappings#_clear_select_mode_mappings() "{{{ + if !g:neosnippet#disable_select_mode_mappings + return + endif + + redir => mappings + silent! smap + redir END + + for line in map(filter(split(mappings, '\n'), + \ "v:val !~# '^s'"), + \ "substitute(v:val, '', '', 'g')") + let map = matchstr(line, '^\a*\s*\zs\S\+') + let map = substitute(map, '', '', 'g') + + silent! execute 'sunmap' map + silent! execute 'sunmap ' map + endfor + + " Define default select mode mappings. + snoremap a + snoremap a + snoremap a + snoremap a +endfunction"}}} + +function! s:snippets_expand(cur_text, col) "{{{ + let cur_word = neosnippet#get_cursor_snippet( + \ neosnippet#get_snippets(), + \ a:cur_text) + + call neosnippet#expand( + \ a:cur_text, a:col, cur_word) +endfunction"}}} + function! s:snippets_expand_or_jump(cur_text, col) "{{{ let cur_word = neosnippet#get_cursor_snippet( \ neosnippet#get_snippets(), a:cur_text) diff --git a/doc/neosnippet.txt b/doc/neosnippet.txt index d686553..c714441 100755 --- a/doc/neosnippet.txt +++ b/doc/neosnippet.txt @@ -1,7 +1,7 @@ *neosnippet.txt* The neo-snippet plugin contains snippet source -Version: 3.1 +Version: 4.0 Author: Shougo License: MIT license {{{ Permission is hereby granted, free of charge, to any person obtaining From bee4d730921d5dbe67eec23d141fe55feac7e76b Mon Sep 17 00:00:00 2001 From: Shougo Matsushita Date: Tue, 19 Nov 2013 16:19:33 +0900 Subject: [PATCH 4/4] - Added commands. --- autoload/neosnippet.vim | 107 +--------------- autoload/neosnippet/commands.vim | 135 +++++++++++++++++++++ autoload/neosnippet/init.vim | 4 +- autoload/unite/sources/neosnippet_file.vim | 8 +- plugin/neosnippet.vim | 8 +- 5 files changed, 149 insertions(+), 113 deletions(-) create mode 100644 autoload/neosnippet/commands.vim diff --git a/autoload/neosnippet.vim b/autoload/neosnippet.vim index f5909ef..0b49f07 100644 --- a/autoload/neosnippet.vim +++ b/autoload/neosnippet.vim @@ -38,13 +38,6 @@ call neosnippet#util#set_default( \ 'g:neosnippet#enable_snipmate_compatibility', 0) "}}} -" Variables "{{{ -let s:neosnippet_options = [ - \ '-runtime', - \ '-vertical', '-horizontal', '-direction=', '-split', - \] -"}}} - " For echodoc. "{{{ let s:doc_dict = { \ 'name' : 'neosnippet', @@ -88,7 +81,7 @@ function! neosnippet#jumpable() "{{{ endfunction"}}} function! neosnippet#caching() "{{{ - call neosnippet#make_cache(&filetype) + call neosnippet#commands#_make_cache(&filetype) endfunction"}}} function! neosnippet#recaching() "{{{ @@ -157,56 +150,6 @@ function! s:initialize_snippet_options() "{{{ return { 'head' : 0, 'word' : 0, 'indent' : 0 } endfunction"}}} -function! neosnippet#edit_snippets(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:neosnippet_options) - - let filetype = get(args, 0, '') - if filetype == '' - let filetype = neosnippet#get_filetype() - 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! s:initialize_options(options) "{{{ let default_options = { @@ -227,45 +170,7 @@ function! s:initialize_options(options) "{{{ return options endfunction"}}} -function! neosnippet#make_cache(filetype) "{{{ - call neosnippet#init#check() - - let filetype = a:filetype == '' ? - \ &filetype : a:filetype - if filetype ==# '' - let filetype = 'nothing' - endif - - let snippets = neosnippet#variables#get_snippets() - if has_key(snippets, filetype) - return - endif - - let snippets_dir = neosnippet#get_snippets_directory() - let snippet = {} - let snippets_files = - \ split(globpath(join(snippets_dir, ','), - \ filetype . '.snip*'), '\n') - \ + split(globpath(join(snippets_dir, ','), - \ filetype . '_*.snip*'), '\n') - \ + split(globpath(join(snippets_dir, ','), - \ filetype . '/**/*.snip*'), '\n') - for snippets_file in reverse(snippets_files) - call s:parse_snippets_file(snippet, snippets_file) - endfor - - let snippets = neosnippet#variables#get_snippets() - let snippets[filetype] = snippet -endfunction"}}} - -function! neosnippet#source_file(filename) "{{{ - call neosnippet#init#check() - - let neosnippet = neosnippet#get_current_neosnippet() - call s:parse_snippets_file(neosnippet.snippets, a:filename) -endfunction"}}} - -function! s:parse_snippets_file(snippets, snippets_file) "{{{ +function! neosnippet#_parse_snippets_file(snippets, snippets_file) "{{{ let dup_check = {} let snippet_dict = {} @@ -292,7 +197,7 @@ function! s:parse_snippets_file(snippets, snippets_file) "{{{ for snippets_file in split(globpath(join( \ neosnippet#get_snippets_directory(), ','), \ filename), '\n') - call s:parse_snippets_file(a:snippets, snippets_file) + call neosnippet#_parse_snippets_file(a:snippets, snippets_file) endfor elseif line =~ '^delete\s' let name = matchstr(line, '^delete\s\+\zs.*$') @@ -968,7 +873,7 @@ function! neosnippet#get_snippets() "{{{ 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 neosnippet#commands#_make_cache(filetype) call extend(snippets, \ neosnippet#variables#get_snippets()[filetype], 'keep') endfor @@ -1032,10 +937,6 @@ function! s:get_sources_filetypes(filetype) "{{{ return filetypes + ['_'] endfunction"}}} -function! neosnippet#edit_complete(arglead, cmdline, cursorpos) "{{{ - return filter(s:neosnippet_options + neosnippet#filetype_complete( - \ a:arglead, a:cmdline, a:cursorpos), 'stridx(v:val, a:arglead) == 0') -endfunction"}}} " Complete filetype helper. function! neosnippet#filetype_complete(arglead, cmdline, cursorpos) "{{{ " Dup check. diff --git a/autoload/neosnippet/commands.vim b/autoload/neosnippet/commands.vim new file mode 100644 index 0000000..3e1cd14 --- /dev/null +++ b/autoload/neosnippet/commands.vim @@ -0,0 +1,135 @@ +"============================================================================= +" FILE: commands.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 + +" 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 == '' + let filetype = neosnippet#get_filetype() + 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 + + let snippets = neosnippet#variables#get_snippets() + if has_key(snippets, filetype) + return + endif + + let snippets_dir = neosnippet#get_snippets_directory() + let snippet = {} + let snippets_files = + \ split(globpath(join(snippets_dir, ','), + \ filetype . '.snip*'), '\n') + \ + split(globpath(join(snippets_dir, ','), + \ filetype . '_*.snip*'), '\n') + \ + split(globpath(join(snippets_dir, ','), + \ filetype . '/**/*.snip*'), '\n') + for snippets_file in reverse(snippets_files) + call neosnippet#_parse_snippets_file(snippet, snippets_file) + endfor + + let snippets = neosnippet#variables#get_snippets() + let snippets[filetype] = snippet +endfunction"}}} + +function! neosnippet#commands#_source(filename) "{{{ + call neosnippet#init#check() + + let neosnippet = neosnippet#get_current_neosnippet() + call neosnippet#_parse_snippets_file(neosnippet.snippets, a:filename) +endfunction"}}} + +function! neosnippet#commands#_edit_complete(arglead, cmdline, cursorpos) "{{{ + return filter(s:edit_options + + \ neosnippet#filetype_complete(a:arglead, a:cmdline, a:cursorpos), + \ 'stridx(v:val, a:arglead) == 0') +endfunction"}}} + +let &cpo = s:save_cpo +unlet s:save_cpo + +" vim: foldmethod=marker diff --git a/autoload/neosnippet/init.vim b/autoload/neosnippet/init.vim index 3e30f0b..80a0f84 100644 --- a/autoload/neosnippet/init.vim +++ b/autoload/neosnippet/init.vim @@ -68,8 +68,8 @@ function! s:initialize_script_variables() "{{{ call map(snippets_dir, 'substitute(v:val, "[\\\\/]$", "", "")') endfunction"}}} function! s:initialize_cache() "{{{ - " Caching _ snippets. - call neosnippet#make_cache('_') + " Make cache for _ snippets. + call neosnippet#commands#_make_cache('_') " Initialize check. call neosnippet#caching() diff --git a/autoload/unite/sources/neosnippet_file.vim b/autoload/unite/sources/neosnippet_file.vim index 3a0904f..18d5720 100644 --- a/autoload/unite/sources/neosnippet_file.vim +++ b/autoload/unite/sources/neosnippet_file.vim @@ -1,7 +1,7 @@ "============================================================================= " FILE: neosnippet_file.vim " AUTHOR: Shougo Matsushita -" Last Modified: 17 Feb 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 @@ -42,7 +42,7 @@ function! s:action_table.neosnippet_source.func(candidates) "{{{ for candidate in a:candidates let snippet_name = candidate.action__path if snippet_name != '' - call neosnippet#source_file(snippet_name) + call neosnippet#commands#_source(snippet_name) endif endfor endfunction"}}} @@ -67,7 +67,7 @@ function! s:source_user.action_table.unite__new_candidate.func(candidate) "{{{ let filename = input( \ 'New snippet file name: ', neosnippet#get_filetype()) if filename != '' - call neosnippet#edit_snippets(filename) + call neosnippet#commands#_edit(filename) endif endfunction"}}} @@ -92,7 +92,7 @@ function! s:source_runtime.action_table.unite__new_candidate.func(candidate) "{{ let filename = input( \ 'New snippet file name: ', neosnippet#get_filetype()) if filename != '' - call neosnippet#edit_snippets('-runtime ' . filename) + call neosnippet#commands#_edit('-runtime ' . filename) endif endfunction"}}} diff --git a/plugin/neosnippet.vim b/plugin/neosnippet.vim index 77cc284..3efa1d3 100644 --- a/plugin/neosnippet.vim +++ b/plugin/neosnippet.vim @@ -74,17 +74,17 @@ augroup neosnippet "{{{ augroup END"}}} " Commands. "{{{ -command! -nargs=? -complete=customlist,neosnippet#edit_complete +command! -nargs=? -complete=customlist,neosnippet#commands#_edit_complete \ NeoSnippetEdit - \ call neosnippet#edit_snippets() + \ call neosnippet#commands#_edit() command! -nargs=? -complete=customlist,neosnippet#filetype_complete \ NeoSnippetMakeCache - \ call neosnippet#make_cache() + \ call neosnippet#commands#_make_cache() command! -nargs=1 -complete=file \ NeoSnippetSource - \ call neosnippet#source_file() + \ call neosnippet#commands#_source() "}}} let g:loaded_neosnippet = 1