You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

248 lines
7.7 KiB

  1. "=============================================================================
  2. " FILE: mappings.vim
  3. " AUTHOR: Shougo Matsushita <Shougo.Matsu at gmail.com>
  4. " License: MIT license
  5. "=============================================================================
  6. function! neosnippet#mappings#expandable_or_jumpable() abort "{{{
  7. return neosnippet#mappings#expandable() || neosnippet#mappings#jumpable()
  8. endfunction"}}}
  9. function! neosnippet#mappings#expandable() abort "{{{
  10. " Check snippet trigger.
  11. return neosnippet#mappings#completed_expandable()
  12. \ || neosnippet#helpers#get_cursor_snippet(
  13. \ neosnippet#helpers#get_snippets('i'),
  14. \ neosnippet#util#get_cur_text()) != ''
  15. endfunction"}}}
  16. function! neosnippet#mappings#jumpable() abort "{{{
  17. " Found snippet placeholder.
  18. return search(neosnippet#get_placeholder_marker_pattern(). '\|'
  19. \ .neosnippet#get_sync_placeholder_marker_pattern(), 'nw') > 0
  20. endfunction"}}}
  21. function! neosnippet#mappings#completed_expandable() abort "{{{
  22. if !s:enabled_completed_snippet()
  23. return 0
  24. endif
  25. let snippet = neosnippet#parser#_get_completed_snippet(
  26. \ v:completed_item, neosnippet#util#get_cur_text(),
  27. \ neosnippet#util#get_next_text())
  28. return snippet != ''
  29. endfunction"}}}
  30. function! s:enabled_completed_snippet() abort "{{{
  31. return exists('v:completed_item')
  32. \ && !empty(v:completed_item)
  33. \ && g:neosnippet#enable_completed_snippet
  34. endfunction"}}}
  35. function! neosnippet#mappings#_clear_select_mode_mappings() abort "{{{
  36. if !g:neosnippet#disable_select_mode_mappings
  37. return
  38. endif
  39. redir => mappings
  40. silent! smap
  41. redir END
  42. for map in map(filter(split(mappings, '\n'),
  43. \ "v:val !~# '^s' && v:val !~ '^\\a*\\s*<\\S\\+>'"),
  44. \ "matchstr(v:val, '^\\a*\\s*\\zs\\S\\+')")
  45. silent! execute 'sunmap' map
  46. silent! execute 'sunmap <buffer>' map
  47. endfor
  48. " Define default select mode mappings.
  49. snoremap <CR> a<BS>
  50. snoremap <BS> a<BS>
  51. snoremap <Del> a<BS>
  52. snoremap <C-h> a<BS>
  53. endfunction"}}}
  54. function! neosnippet#mappings#_register_oneshot_snippet() abort "{{{
  55. let trigger = input('Please input snippet trigger: ', 'oneshot')
  56. if trigger == ''
  57. return
  58. endif
  59. let selected_text = substitute(
  60. \ neosnippet#helpers#get_selected_text(visualmode(), 1), '\n$', '', '')
  61. call neosnippet#helpers#delete_selected_text(visualmode(), 1)
  62. let base_indent = matchstr(selected_text, '^\s*')
  63. " Delete base_indent.
  64. let selected_text = substitute(selected_text,
  65. \'^' . base_indent, '', 'g')
  66. let neosnippet = neosnippet#variables#current_neosnippet()
  67. let options = neosnippet#parser#_initialize_snippet_options()
  68. let options.word = 1
  69. let options.oneshot = 1
  70. let neosnippet.snippets[trigger] = neosnippet#parser#_initialize_snippet(
  71. \ { 'name' : trigger, 'word' : selected_text, 'options' : options },
  72. \ '', 0, '', trigger)
  73. echo 'Registered trigger : ' . trigger
  74. endfunction"}}}
  75. function! neosnippet#mappings#_expand_target() abort "{{{
  76. let trigger = input('Please input snippet trigger: ',
  77. \ '', 'customlist,neosnippet#commands#_complete_target_snippets')
  78. let neosnippet = neosnippet#variables#current_neosnippet()
  79. if !has_key(neosnippet#helpers#get_snippets('i'), trigger) ||
  80. \ neosnippet#helpers#get_snippets('i')[trigger].snip !~#
  81. \ neosnippet#get_placeholder_target_marker_pattern()
  82. if trigger != ''
  83. echo 'The trigger is invalid.'
  84. endif
  85. let neosnippet.target = ''
  86. return
  87. endif
  88. call neosnippet#mappings#_expand_target_trigger(trigger)
  89. endfunction"}}}
  90. function! neosnippet#mappings#_expand_target_trigger(trigger) abort "{{{
  91. let neosnippet = neosnippet#variables#current_neosnippet()
  92. let neosnippet.target = substitute(
  93. \ neosnippet#helpers#get_selected_text(visualmode(), 1), '\n$', '', '')
  94. let line = getpos("'<")[1]
  95. let col = getpos("'<")[2]
  96. call neosnippet#helpers#delete_selected_text(visualmode())
  97. call cursor(line, col)
  98. if col == 1
  99. let cur_text = a:trigger
  100. else
  101. let cur_text = neosnippet#util#get_cur_text()
  102. let cur_text = cur_text[: col-2] . a:trigger . cur_text[col :]
  103. endif
  104. call neosnippet#view#_expand(cur_text, col, a:trigger)
  105. if !neosnippet#mappings#jumpable()
  106. call cursor(0, col('.') - 1)
  107. stopinsert
  108. endif
  109. endfunction"}}}
  110. function! neosnippet#mappings#_anonymous(snippet) abort "{{{
  111. let [cur_text, col, expr] = neosnippet#mappings#_pre_trigger()
  112. let expr .= printf("\<ESC>:call neosnippet#view#_insert(%s, {}, %s, %d)\<CR>",
  113. \ string(a:snippet), string(cur_text), col)
  114. return expr
  115. endfunction"}}}
  116. function! neosnippet#mappings#_expand(trigger) abort "{{{
  117. let [cur_text, col, expr] = neosnippet#mappings#_pre_trigger()
  118. let expr .= printf("\<ESC>:call neosnippet#view#_expand(%s, %d, %s)\<CR>",
  119. \ string(cur_text), col, string(a:trigger))
  120. return expr
  121. endfunction"}}}
  122. function! s:snippets_expand(cur_text, col) abort "{{{
  123. if s:enabled_completed_snippet()
  124. let snippet = neosnippet#parser#_get_completed_snippet(
  125. \ v:completed_item, a:cur_text, neosnippet#util#get_next_text())
  126. if snippet != ''
  127. call neosnippet#view#_insert(snippet, {}, a:cur_text, a:col)
  128. return 0
  129. endif
  130. endif
  131. let cur_word = neosnippet#helpers#get_cursor_snippet(
  132. \ neosnippet#helpers#get_snippets('i'),
  133. \ a:cur_text)
  134. if cur_word != ''
  135. " Found snippet trigger.
  136. call neosnippet#view#_expand(
  137. \ neosnippet#util#get_cur_text(), a:col, cur_word)
  138. return 0
  139. endif
  140. return 1
  141. endfunction"}}}
  142. function! s:snippets_expand_or_jump(cur_text, col) abort "{{{
  143. if s:snippets_expand(a:cur_text, a:col)
  144. call neosnippet#view#_jump('', a:col)
  145. endif
  146. endfunction"}}}
  147. function! s:snippets_jump_or_expand(cur_text, col) abort "{{{
  148. if search(neosnippet#get_placeholder_marker_pattern(). '\|'
  149. \ .neosnippet#get_sync_placeholder_marker_pattern(), 'nw') > 0
  150. " Found snippet placeholder.
  151. call neosnippet#view#_jump('', a:col)
  152. else
  153. return s:snippets_expand(a:cur_text, a:col)
  154. endif
  155. endfunction"}}}
  156. function! s:SID_PREFIX() abort "{{{
  157. return matchstr(expand('<sfile>'), '<SNR>\d\+_\ze\w\+$')
  158. endfunction"}}}
  159. function! neosnippet#mappings#_trigger(function) abort "{{{
  160. let [cur_text, col, expr] = neosnippet#mappings#_pre_trigger()
  161. if !neosnippet#mappings#expandable_or_jumpable()
  162. return ''
  163. endif
  164. let expr .= printf("\<ESC>:call %s(%s,%d)\<CR>",
  165. \ a:function, string(cur_text), col)
  166. return expr
  167. endfunction"}}}
  168. function! neosnippet#mappings#_pre_trigger() abort "{{{
  169. call neosnippet#init#check()
  170. let cur_text = neosnippet#util#get_cur_text()
  171. let col = col('.')
  172. let expr = ''
  173. if mode() !=# 'i'
  174. " Fix column.
  175. let col += 2
  176. endif
  177. " Get selected text.
  178. let neosnippet = neosnippet#variables#current_neosnippet()
  179. let neosnippet.trigger = 1
  180. if mode() ==# 's' && neosnippet.optional_tabstop
  181. let expr .= "\<C-o>\"_d"
  182. endif
  183. return [cur_text, col, expr]
  184. endfunction"}}}
  185. " Plugin key-mappings.
  186. function! neosnippet#mappings#expand_or_jump_impl() abort
  187. return mode() ==# 's' ?
  188. \ neosnippet#mappings#_trigger('neosnippet#view#_jump') :
  189. \ neosnippet#mappings#_trigger(
  190. \ s:SID_PREFIX().'snippets_expand_or_jump')
  191. endfunction
  192. function! neosnippet#mappings#jump_or_expand_impl() abort
  193. return mode() ==# 's' ?
  194. \ neosnippet#mappings#_trigger('neosnippet#view#_jump') :
  195. \ neosnippet#mappings#_trigger(
  196. \ s:SID_PREFIX().'snippets_jump_or_expand')
  197. endfunction
  198. function! neosnippet#mappings#expand_impl() abort
  199. return neosnippet#mappings#_trigger(s:SID_PREFIX().'snippets_expand')
  200. endfunction
  201. function! neosnippet#mappings#jump_impl() abort
  202. return neosnippet#mappings#_trigger('neosnippet#view#_jump')
  203. endfunction
  204. " vim: foldmethod=marker