diff --git a/autoload/minisnip.vim b/autoload/minisnip.vim index 36dae0d..2b05158 100644 --- a/autoload/minisnip.vim +++ b/autoload/minisnip.vim @@ -122,10 +122,11 @@ function! s:SelectPlaceholder() abort let @s=substitute(@s, '\V' . g:minisnip_finalenddelim, '', '') catch /E486:/ " There's no placeholder at all, enter insert mode - call feedkeys('i', 'n') + call feedkeys('A', 'n') return finally let &wrapscan = l:ws + unlet! s:minisnip_selected_text endtry finally let &wrapscan = l:ws @@ -143,6 +144,20 @@ function! s:SelectPlaceholder() abort let l:skip = 0 endif + " is this placeholder marked as 'visual'? + if @s =~ '\V\^' . g:minisnip_visualmarker + " remove the marker + let @s=substitute(@s, '\V\^' . g:minisnip_visualmarker, '', '') + if exists('s:minisnip_selected_text') + " get the text + let @s=s:minisnip_selected_text + endif + let l:visualmarker = 1 + let l:skip = 1 + else + let l:visualmarker = 0 + endif + " is this placeholder marked as 'evaluate'? if @s =~ '\V\^' . g:minisnip_evalmarker " remove the marker @@ -163,6 +178,10 @@ function! s:SelectPlaceholder() abort call feedkeys(col("'>") - l:slen >= col('$') - 1 ? 'a' : 'i', 'n') elseif l:skip == 1 normal! gv"sp + if l:visualmarker == 1 + " if the text came by replacing a visual marker, indent the same + normal! gv=`] + endif let @s = l:old_s call s:SelectPlaceholder() else @@ -174,6 +193,14 @@ function! s:SelectPlaceholder() abort let @s = l:old_s endfunction +" function to get the selected text in a variable +function! minisnip#CopyVisualSelection() abort + execute "'<,'>d" + let s:minisnip_selected_text = substitute(@", '\n\+$', '', '') + execute "normal! O\\\" + startinsert! +endfunction + function! minisnip#complete() abort " Locate the start of the word let l:line = getline('.') diff --git a/doc/minisnip.txt b/doc/minisnip.txt index f16937e..ee4e18f 100644 --- a/doc/minisnip.txt +++ b/doc/minisnip.txt @@ -135,6 +135,33 @@ of how it can be used: > This will automatically fill the `#define` line with the value entered on the `#ifndef` line upon jumping to it. +------------------------------------------------------------------------------- + *'g:minisnip_visualmarker'* +Default: '~#' + +Marks a "visual" placeholder that gets replaced with the last visually +selected text; after selecting the text, |'g:minisnip_trigger'| needs to be +pressed before starting out with triggering a snippet. +Here is an example of how it can be used: > + if (cond) { + {{+~#+}} + } + +Say the following text has to be surrounded by the above `if` snippet: + x = 10; + y = 20; + +First select the text and press `Tab` (or whatever is the value of +|'g:minisnip_trigger'|), then trigger the `if` snippet as usual. Once the +control reaches the visual placeholder, the output will be the following: + if (cond) { + x = 10; + y = 20; + } + +In case there is no text that was copied from the visual mode previously, +the placeholder will simply land you to the insert mode. + ------------------------------------------------------------------------------- *'g:minisnip_finalstartdelim'* *'g:minisnip_finalenddelim'* diff --git a/plugin/minisnip.vim b/plugin/minisnip.vim index ea42291..b6a61a3 100644 --- a/plugin/minisnip.vim +++ b/plugin/minisnip.vim @@ -12,6 +12,7 @@ let g:minisnip_enddelim = get(g:, 'minisnip_enddelim', '+}}') let g:minisnip_finalstartdelim = get(g:, 'minisnip_finalstartdelim', '{{-') let g:minisnip_finalenddelim = get(g:, 'minisnip_finalenddelim', '-}}') let g:minisnip_evalmarker = get(g:, 'minisnip_evalmarker', '~') +let g:minisnip_visualmarker = get(g:, 'minisnip_visualmarker', '~#') let g:minisnip_donotskipmarker = get(g:, 'minisnip_donotskipmarker', '`') let g:minisnip_backrefmarker = get(g:, 'minisnip_backrefmarker', '\\~') @@ -33,12 +34,17 @@ snoremap