Permalink
Browse files

Add global option g:expand_region_use_select_mode to allow expansion …

…to default in Vim's select mode
  • Loading branch information...
1 parent 0d45209 commit 31db83cd9dfc12fad0ab6a32f3ecdf106e8756c4 @terryma committed Apr 5, 2013
Showing with 98 additions and 26 deletions.
  1. +8 −0 README.md
  2. +62 −20 autoload/expand_region.vim
  3. +12 −2 doc/expand_region.txt
  4. +16 −4 plugin/expand_region.vim
View
@@ -26,6 +26,7 @@ map J <Plug>(expand_region_shrink)
```
## Setting
+### Customize selected regions
The plugin uses __your own__ text objects to determine the expansion. You can customize the text objects the plugin knows about with ```g:expand_region_text_objects```.
```vim
@@ -78,6 +79,13 @@ call expand_region#custom_text_objects('ruby', {
\ })
```
+### Customize selection mode
+By default, after an expansion, the plugin leaves you in visual mode. If your ```selectmode```(h:selectmode)) contains ```cmd```, then the plugin will respect that setting and leave you in select mode. If you don't have ```selectmode``` set, but would like to default the expansion in select mode, you can use the global setting below:
+
+```vim
+let g:expand_region_use_select_mode = 1
+```
+
[vim-expand-region]:http://github.com/terryma/vim-expand-region
[Pathogen]:http://github.com/tpope/vim-pathogen
[Vundle]:http://github.com/gmarik/vundle
@@ -10,23 +10,34 @@ set cpo&vim
" ==============================================================================
" Settings
" ==============================================================================
-if !exists('g:expand_region_text_objects')
+
+" Init global vars
+function! expand_region#init()
+ if exists('g:expand_region_init') && g:expand_region_init
+ return
+ endif
+ let g:expand_region_init = 1
+
" Dictionary of text objects that are supported by default. Note that some of
" the text objects are not available in vanilla vim. '1' indicates that the
" text object is recursive (think of nested parens or brackets)
- let g:expand_region_text_objects = {
- \ 'iw' :0,
- \ 'iW' :0,
- \ 'i"' :0,
- \ 'i''' :0,
- \ 'i]' :1,
- \ 'ib' :1,
- \ 'iB' :1,
- \ 'il' :0,
- \ 'ip' :0,
- \ 'ie' :0,
- \}
-endif
+ let g:expand_region_text_objects = get(g:, 'expand_region_text_objects', {
+ \ 'iw' :0,
+ \ 'iW' :0,
+ \ 'i"' :0,
+ \ 'i''' :0,
+ \ 'i]' :1,
+ \ 'ib' :1,
+ \ 'iB' :1,
+ \ 'il' :0,
+ \ 'ip' :0,
+ \ 'ie' :0,
+ \})
+
+ " Option to default to the select mode when selecting a new region
+ let g:expand_region_use_select_mode = get(g:, 'expand_region_use_select_mode', 0)
+endfunction
+call expand_region#init()
" ==============================================================================
" Global Functions
@@ -45,6 +56,12 @@ function! expand_region#custom_text_objects(...)
endif
endfunction
+" Returns whether we should perform the region highlighting use visual mode or
+" select mode
+function! expand_region#use_select_mode()
+ return g:expand_region_use_select_mode || index(split(s:saved_selectmode, ','), 'cmd') != -1
+endfunction
+
" Main function
function! expand_region#next(mode, direction)
call s:expand_region(a:mode, a:direction)
@@ -72,6 +89,11 @@ let s:cur_index = -1
" length: The number of characters for the text object
let s:candidates = []
+" This is used to save the user's selectmode setting. If the user's selectmode
+" contains 'cmd', then our expansion should result in the region selected under
+" select mode.
+let s:saved_selectmode = &selectmode
+
" ==============================================================================
" Functions
" ==============================================================================
@@ -270,9 +292,24 @@ function! s:compute_candidates(cursor_pos)
call s:remove_duplicate(s:candidates)
endfunction
+" Perform the visual selection at the end. If the user wants to be left in
+" select mode, do so
+function! s:select_region()
+ exec 'normal! v'
+ exec 'normal '.s:candidates[s:cur_index].text_object
+ if expand_region#use_select_mode()
+ exec "normal! \<C-g>"
+ endif
+endfunction
+
" Expand or shrink the visual selection to the next candidate in the text object
" list.
function! s:expand_region(mode, direction)
+ " Save the selectmode setting, and remove the setting so our 'v' command do
+ " not get interfered
+ let s:saved_selectmode = &selectmode
+ let &selectmode=""
+
if s:should_compute_candidates(a:mode)
call s:compute_candidates(getpos('.'))
else
@@ -287,21 +324,26 @@ function! s:expand_region(mode, direction)
let s:cur_index+=1
" Associate the window view with the text object
let s:candidates[s:cur_index].prev_winview = winsaveview()
- exec 'normal! v'
- exec 'normal '.s:candidates[s:cur_index].text_object
+ call s:select_region()
endif
else
"Shrinking
if s:cur_index <=# 0
- " Do nothing, this will also return us to normal mode
+ " In visual mode, doing nothing here will return us to normal mode. For
+ " select mode, the following is needed.
+ if expand_region#use_select_mode()
+ exec "normal! gV"
+ endif
else
- let s:cur_index-=1
" Restore the window view
call winrestview(s:candidates[s:cur_index].prev_winview)
- exec 'normal! v'
- exec 'normal '.s:candidates[s:cur_index].text_object
+ let s:cur_index-=1
+ call s:select_region()
endif
endif
+
+ " Restore the selectmode setting
+ let &selectmode = s:saved_selectmode
endfunction
let &cpo = s:save_cpo
View
@@ -22,8 +22,8 @@ CONTENTS *expand-region-contents*
==============================================================================
1. Intro *expand-region-intro*
-vim-expand-regions brings the incremental visual selection feature from other
-text editors into Vim.
+*vim-expand-regions* brings the incremental visual selection feature from
+other text editors into Vim.
Emac's 'expand-region': https://github.com/magnars/expand-region.el
IntellJ's 'syntax aware selection':
@@ -105,6 +105,16 @@ argument: >
\ 'am' :0,
\ })
<
+ *expand_region_use_select_mode*
+Default: 0
+By default, after an expansion, the plugin leaves you in visual mode. If your
+'selectmode' contains "cmd", then the plugin will respect that setting and
+leave you in select mode. If you don't have 'selectmode' set, but would
+like to default the expansion in select mode, you can use the global setting
+below: >
+
+ let g:expand_region_use_select_mode = 1
+<
==============================================================================
5. About *expand-region-about*
@@ -9,6 +9,9 @@
let s:save_cpo = &cpo
set cpo&vim
+" Init global vars
+call expand_region#init()
+
" ==============================================================================
" Mappings
" ==============================================================================
@@ -20,12 +23,21 @@ if !hasmapto('<Plug>(expand_region_shrink)')
vmap _ <Plug>(expand_region_shrink)
nmap _ <Plug>(expand_region_shrink)
endif
+
nnoremap <silent> <Plug>(expand_region_expand)
\ :<C-U>call expand_region#next('n', '+')<CR>
-vnoremap <silent> <Plug>(expand_region_expand)
- \ :<C-U>call expand_region#next('v', '+')<CR>
-vnoremap <silent> <Plug>(expand_region_shrink)
- \ :<C-U>call expand_region#next('v', '-')<CR>
+" Map keys differently depending on which mode is desired
+if expand_region#use_select_mode()
+ snoremap <silent> <Plug>(expand_region_expand)
+ \ :<C-U>call expand_region#next('v', '+')<CR>
+ snoremap <silent> <Plug>(expand_region_shrink)
+ \ :<C-U>call expand_region#next('v', '-')<CR>
+else
+ xnoremap <silent> <Plug>(expand_region_expand)
+ \ :<C-U>call expand_region#next('v', '+')<CR>
+ xnoremap <silent> <Plug>(expand_region_shrink)
+ \ :<C-U>call expand_region#next('v', '-')<CR>
+endif
let &cpo = s:save_cpo
unlet s:save_cpo

0 comments on commit 31db83c

Please sign in to comment.