diff options
author | Kevin Le <solnovus@gmail.com> | 2015-11-23 13:12:47 -0800 |
---|---|---|
committer | Kevin Le <solnovus@gmail.com> | 2015-11-23 13:24:20 -0800 |
commit | 9f8c26be23e2335e20f7198a4c9d29bd24ed918f (patch) | |
tree | 67e900ae33882467b79482ab8d56cbfd039c21b9 | |
parent | c27b437ca0f9a270fa6a9aeb6b882e47628c053b (diff) |
don't take over keys by default, require configuration in vimrc
fixes #26
-rw-r--r-- | README.rst | 80 | ||||
-rw-r--r-- | autoload/camelcasemotion.vim | 45 | ||||
-rw-r--r-- | doc/camelcasemotion.txt | 51 | ||||
-rw-r--r-- | plugin/camelcasemotion.vim | 54 |
4 files changed, 102 insertions, 128 deletions
@@ -13,25 +13,47 @@ for identifiers. The best way to navigate inside those identifiers using Vim built-in motions is the [count]f{char} motion, i.e. f{uppercase-char} or f\_, respectively. But we can make this easier: -This script defines motions '<leader>w', '<leader>b' and '<leader>e' (similar -to 'w', 'b', 'e'), which do not move word-wise (forward/backward), but -Camel-wise; i.e. to word boundaries and uppercase letters. The motions also -work on underscore notation, where words are delimited by underscore ('_') -characters. From here on, both CamelCase and underscore_notation entities are -referred to as "words" (in double quotes). Just like with the regular motions, -a [count] can be prepended to move over multiple "words" at once. Outside of -"words" (e.g. in non-keyword characters like // or ;), the new motions move -just like the regular motions. +This script defines motions similar to 'w', 'b', 'e' which do not move +word-wise (forward/backward), but Camel-wise; i.e. to word boundaries and +uppercase letters. The motions also work on underscore notation, where words +are delimited by underscore ('_') characters. From here on, both CamelCase +and underscore_notation entities are referred to as "words" (in double quotes). +Just like with the regular motions, a [count] can be prepended to move over +multiple "words" at once. Outside of "words" (e.g. in non-keyword characters +like // or ;), the new motions move just like the regular motions. Vim provides a built-in 'iw' text object called 'inner word', which works in operator-pending and visual mode. Analog to that, this script defines inner -"word" motions 'i<leader>w', 'i<leader>b' and 'i<leader>e', which select the -"word" (or multiple "words" if a [count] is given) where the cursor is located. +"word" motions which select the "word" (or multiple "words" if a [count] is +given) where the cursor is located. Usage ====== +To use the default mappings, add the following to your vimrc:: + camelcasemotion#CreateMotionMappings('<leader>') -The new motions are '<leader>w', '<leader>b' and '<leader>e', all of which can +If you want to use different mappings, map your keys to the +<Plug>CamelCaseMotion_? mapping targets your vimrc). + +EXAMPLE: Map to w, b and e mappings:: + map <silent> w <Plug>CamelCaseMotion_w + map <silent> b <Plug>CamelCaseMotion_b + map <silent> e <Plug>CamelCaseMotion_e + map <silent> ge <Plug>CamelCaseMotion_ge + sunmap w + sunmap b + sunmap e + sunmap ge + +EXAMPLE: Map iw, ib and ie motions:: + omap <silent> iw <Plug>CamelCaseMotion_iw + xmap <silent> iw <Plug>CamelCaseMotion_iw + omap <silent> ib <Plug>CamelCaseMotion_ib + xmap <silent> ib <Plug>CamelCaseMotion_ib + omap <silent> ie <Plug>CamelCaseMotion_ie + xmap <silent> ie <Plug>CamelCaseMotion_ie + +Most commonly motions are '<leader>w', '<leader>b' and '<leader>e', all of which can be used in normal mode, operator-pending mode (cp. :help operator), and visual mode. For example, type 'bc<leader>w' to change 'Camel' in 'CamelCase' to something else. @@ -109,37 +131,3 @@ To uninstall, use the ``:RmVimball`` command. **Dependencies** Requires Vim 7.0 or higher. - -**Configuration** - -If you want to use different mappings, map your keys to the -``<Plug>CamelCaseMotion_?`` mapping targets _before_ sourcing this script (e.g. in -your .vimrc). - -**Example**: Use 'W', 'B' and 'E':: - - map <S-W> <Plug>CamelCaseMotion_w - map <S-B> <Plug>CamelCaseMotion_b - map <S-E> <Plug>CamelCaseMotion_e - -**Example**: Replace the default 'w', 'b' and 'e' mappings instead of defining -additional mappings '<leader>w', '<leader>b' and '<leader>e':: - - map <silent> w <Plug>CamelCaseMotion_w - map <silent> b <Plug>CamelCaseMotion_b - map <silent> e <Plug>CamelCaseMotion_e - map <silent> ge <Plug>CamelCaseMotion_e - sunmap w - sunmap b - sunmap e - sunmap ge - -**Example**: Replace default 'iw' text-object and define 'ib' and 'ie' -motions:: - - omap <silent> iw <Plug>CamelCaseMotion_iw - xmap <silent> iw <Plug>CamelCaseMotion_iw - omap <silent> ib <Plug>CamelCaseMotion_ib - xmap <silent> ib <Plug>CamelCaseMotion_ib - omap <silent> ie <Plug>CamelCaseMotion_ie - xmap <silent> ie <Plug>CamelCaseMotion_ie diff --git a/autoload/camelcasemotion.vim b/autoload/camelcasemotion.vim index 248ba39..505786d 100644 --- a/autoload/camelcasemotion.vim +++ b/autoload/camelcasemotion.vim @@ -17,7 +17,7 @@ " file creation "- functions ------------------------------------------------------------------" -function! s:Move( direction, count, mode ) +function! s:Move(direction, count, mode) " Note: There is no inversion of the regular expression character class " 'keyword character' (\k). We need an inversion "non-keyword" defined as " "any non-whitespace character that is not a keyword character" (e.g. @@ -64,14 +64,6 @@ function! s:Move( direction, count, mode ) let l:direction = (a:direction == 'w' ? '' : a:direction) - " CamelCase: Jump to beginning of either (start of word, Word, WORD, - " 123). - " Underscore_notation: Jump to the beginning of an underscore-separated - " word or number. - "call search( '\<\|\u', 'W' . l:direction ) - "call search( '\<\|\u\(\l\+\|\u\+\ze\u\)\|\d\+', 'W' . l:direction ) - "call search( '\<\|\u\(\l\+\|\u\+\ze\u\)\|\d\+\|_\zs\(\a\|\d\)\+', 'W' . l:direction ) - " beginning of ... " word | empty line | non-keyword after whitespaces | non-whitespace after word | number | lowercase folowed by capital letter or number | ACRONYM followed by CamelCase or number | CamelCase | ACRONYM | underscore followed by ACRONYM, Camel, lowercase or number call search( '\m\<\D\|^$\|\%(^\|\s\)\+\zs\k\@!\S\|\>\<\|\d\+\|\l\+\zs\%(\u\|\d\)\|\u\+\zs\%(\u\l\|\d\)\|\u\l\+\|\u\@<!\u\+\|[-_]\zs\%(\u\+\|\u\l\+\|\l\+\|\d\+\)', 'W' . l:direction) " Note: word must be defined as '\<\D' to avoid that a word like @@ -87,7 +79,7 @@ function! s:Move( direction, count, mode ) endwhile endfunction -function! camelcasemotion#Motion( direction, count, mode ) +function! camelcasemotion#Motion(direction, count, mode) "******************************************************************************* "* PURPOSE: " Perform the motion over CamelCaseWords or underscore_notation. @@ -119,7 +111,7 @@ function! camelcasemotion#Motion( direction, count, mode ) endif endif - call s:Move( a:direction, a:count, a:mode ) + call s:Move(a:direction, a:count, a:mode) if a:mode == 'v' || a:mode == 'iv' " Note: 'selection' setting. @@ -142,7 +134,7 @@ function! camelcasemotion#Motion( direction, count, mode ) endif endfunction -function! camelcasemotion#InnerMotion( direction, count ) +function! camelcasemotion#InnerMotion(direction, count) " If the cursor is positioned on the first character of a CamelWord, the " backward motion would move to the previous word, which would result in a " wrong selection. To fix this, first move the cursor to the right, so that @@ -156,16 +148,39 @@ function! camelcasemotion#InnerMotion( direction, count ) if a:direction == 'b' " Do not do the selection backwards, because the backwards "word" motion " in visual mode + selection=inclusive has an off-by-one error. - call camelcasemotion#Motion( 'b', a:count, 'n' ) + call camelcasemotion#Motion('b', a:count, 'n') normal! v " We decree that 'b' is the opposite of 'e', not 'w'. This makes more " sense at the end of a line and for underscore_notation. - call camelcasemotion#Motion( 'e', a:count, 'iv' ) + call camelcasemotion#Motion('e', a:count, 'iv') else - call camelcasemotion#Motion( 'b', 1, 'n' ) + call camelcasemotion#Motion('b', 1, 'n') normal! v call camelcasemotion#Motion(a:direction, a:count, 'iv') endif endfunction + +function! camelcasemotion#CreateMotionMappings(leader) + " Create mappings according to this template: + " (* stands for the mode [nov], ? for the underlying motion [wbe].) + for l:mode in ['n', 'o', 'v'] + for l:motion in ['w', 'b', 'e', 'ge'] + let l:targetMapping = '<Plug>CamelCaseMotion_' . l:motion + execute (l:mode ==# 'v' ? 'x' : l:mode) . + \ 'map <silent> ' . a:leader . l:motion . ' ' . l:targetMapping + endfor + endfor + + " Create mappings according to this template: + " (* stands for the mode [ov], ? for the underlying motion [wbe].) + for l:mode in ['o', 'v'] + for l:motion in ['w', 'b', 'e', 'ge'] + let l:targetMapping = '<Plug>CamelCaseMotion_i' . l:motion + execute (l:mode ==# 'v' ? 'x' : l:mode) . + \ 'map <silent> i' . a:leader . l:motion . ' ' . l:targetMapping + endfor + endfor +endfunction + " vim: set sts=2 sw=2 expandtab ff=unix fdm=syntax : diff --git a/doc/camelcasemotion.txt b/doc/camelcasemotion.txt index 8e48b3d..bdad484 100644 --- a/doc/camelcasemotion.txt +++ b/doc/camelcasemotion.txt @@ -20,31 +20,27 @@ for identifiers. The best way to navigate inside those identifiers using Vim built-in motions is the [count]f{char} motion, i.e. f{uppercase-char} or f_, respectively. But we can make this easier: -This script defines motions |<leader>w|, |<leader>b| and |<leader>e| (similar -to |w|, |b|, |e|), which do not move word-wise (forward/backward), but -Camel-wise; i.e. to word boundaries and uppercase letters. The motions also -work on underscore notation, where words are delimited by underscore ('_') -characters. From here on, both CamelCase and underscore_notation entities are -referred to as "words" (in double quotes). Just like with the regular motions, -a [count] can be prepended to move over multiple "words" at once. Outside of -"words" (e.g. in non-keyword characters like // or ;), the new motions move -just like the regular motions. +This script defines motions similar to |w|, |b|, |e|, which do not move word-wise +(forward/backward), but Camel-wise; i.e. to word boundaries and uppercase +letters. The motions also work on underscore notation, where words are +delimited by underscore ('_') characters. From here on, both CamelCase and +underscore_notation entities are referred to as "words" (in double quotes). +Just like with the regular motions, a [count] can be prepended to move over +multiple "words" at once. Outside of "words" (e.g. in non-keyword characters +like // or ;), the new motions move just like the regular motions. Vim provides a built-in |iw| text object called 'inner word', which works in operator-pending and visual mode. Analoguous to that, this script defines -inner "word" motions |i<leader>w|, |i<leader>b| and |i<leader>e|, which select -the "word" (or multiple "words" if a [count] is given) where the cursor is -located. +inner "word" motions which select the "word" (or multiple "words" if a +[count] is given) where the cursor is located. ============================================================================== USAGE *camelcasemotion-usage* - *<leader>w* - *<leader>b* - *<leader>e* -Use the new motions |<leader>w|, |<leader>b| and |<leader>e| in normal mode, -operator-pending mode (cf. |operator|), and visual mode. For example, if the -cursor is on the 'm', type 'bc<leader>w' to change 'Camel' in 'CamelCase' to -something else. + +Call |camelcasemotion#CreateMotionMappings('<leader>')| to bind the new +motions |<leader>w|, |<leader>b| and |<leader>e| in normal mode, operator-pending +mode (cf. |operator|), and visual mode. For example, if the cursor is on the +'m', type 'bc<leader>w' to change 'Camel' in 'CamelCase' to something else. EXAMPLE: motions @@ -63,6 +59,9 @@ and the corresponding identifiers in underscore_notation: EXAMPLE: inner motions +|camelcasemotion#CreateMotionMappings('<leader>')| also binds the new +motions |i<leader>w|, |i<leader>b|, |i<leader>e|. + Given the following identifier, with the cursor positioned at [x]: script_31337_path_and_na[m]e_without_extension_11 ~ @@ -81,7 +80,8 @@ the archive first, e.g. using WinZip. Inside Vim, install by sourcing the vimball or via the |:UseVimball| command. > vim camelcasemotion.vba.gz :so % -To uninstall, use the |:RmVimball| command. +To uninstall, use the |:RmVimball| command: + asdfsdf DEPENDENCIES *camelcasemotion-dependencies* @@ -90,12 +90,13 @@ DEPENDENCIES *camelcasemotion-dependencies* ============================================================================== CONFIGURATION *camelcasemotion-configuration* +To use the default mappings, add the following to your |vimrc|: > + camelcasemotion#CreateMotionMappings('<leader>') + If you want to use different mappings, map your keys to the -<Plug>CamelCaseMotion_? mapping targets _before_ sourcing this script (e.g. in -your |vimrc|). +<Plug>CamelCaseMotion_? mapping targets your |vimrc|). -EXAMPLE: Replace the default |w|, |b| and |e| mappings instead of defining -additional mappings |<leader>w|, |<leader>b| and |<leader>e|: > +EXAMPLE: Map to |w|, |b| and |e| mappings: > map <silent> w <Plug>CamelCaseMotion_w map <silent> b <Plug>CamelCaseMotion_b map <silent> e <Plug>CamelCaseMotion_e @@ -105,7 +106,7 @@ additional mappings |<leader>w|, |<leader>b| and |<leader>e|: > sunmap e sunmap ge -EXAMPLE: Replace default |iw| text-object and define |ib| and |ie| motions: > +EXAMPLE: Map |iw|, |ib| and |ie| motions: > omap <silent> iw <Plug>CamelCaseMotion_iw xmap <silent> iw <Plug>CamelCaseMotion_iw omap <silent> ib <Plug>CamelCaseMotion_ib diff --git a/plugin/camelcasemotion.vim b/plugin/camelcasemotion.vim index 9cae4bd..879972b 100644 --- a/plugin/camelcasemotion.vim +++ b/plugin/camelcasemotion.vim @@ -146,26 +146,13 @@ let g:loaded_camelcasemotion = 1 " We do not provide the fourth "backward to end" motion (,E), because it is " seldomly used. -function! s:CreateMotionMappings() - " Create mappings according to this template: - " (* stands for the mode [nov], ? for the underlying motion [wbe].) - " - " *noremap <Plug>CamelCaseMotion_? :<C-U>call camelcasemotion#Motion('?',v:count1,'*')<CR> - " if ! hasmapto('<Plug>CamelCaseMotion_?', '*') - " *map <silent> ,? <Plug>CamelCaseMotion_? - " endif - - for l:mode in ['n', 'o', 'v'] - for l:motion in ['w', 'b', 'e', 'ge'] - let l:targetMapping = '<Plug>CamelCaseMotion_' . l:motion - execute l:mode . 'noremap <silent> ' . l:targetMapping . ' :<C-U>call camelcasemotion#Motion(''' . l:motion . ''',v:count1,''' . l:mode . ''')<CR>' - - if ! hasmapto(l:targetMapping, l:mode) - execute (l:mode ==# 'v' ? 'x' : l:mode) . 'map <silent> <leader>' . l:motion . ' ' . l:targetMapping - endif - endfor +for s:mode in ['n', 'o', 'v'] + for s:motion in ['w', 'b', 'e', 'ge'] + let s:targetMapping = '<Plug>CamelCaseMotion_' . s:motion + execute s:mode . 'noremap <silent> ' . s:targetMapping . + \ ' :<C-U>call camelcasemotion#Motion(''' . s:motion . ''',v:count1,''' . s:mode . ''')<CR>' endfor -endfunction +endfor " To create a text motion, a mapping for operator-pending mode needs to be " defined. This mapping should move the cursor according to the implemented @@ -177,30 +164,13 @@ endfunction " different behavior depending on whether visual mode has just been entered or " whether text has already been selected. " We deviate from that and always override the existing selection. -function! s:CreateInnerMotionMappings() - " Create mappings according to this template: - " (* stands for the mode [ov], ? for the underlying motion [wbe].) - " - " *noremap <Plug>CamelCaseMotion_i? :<C-U>call camelcasemotion#InnerMotion('?',v:count1)<CR> - " if ! hasmapto('<Plug>CamelCaseInnerMotion_i?', '*') - " *map <silent> i,? <Plug>CamelCaseInnerMotion_i? - " endif - for l:mode in ['o', 'v'] - for l:motion in ['w', 'b', 'e', 'ge'] - let l:targetMapping = '<Plug>CamelCaseMotion_i' . l:motion - execute l:mode . 'noremap <silent> ' . l:targetMapping . ' :<C-U>call camelcasemotion#InnerMotion(''' . l:motion . ''',v:count1)<CR>' - if ! hasmapto(l:targetMapping, l:mode) - execute (l:mode ==# 'v' ? 'x' : l:mode) . 'map <silent> i<leader>' . l:motion . ' ' . l:targetMapping - endif - endfor +for s:mode in ['o', 'v'] + for s:motion in ['w', 'b', 'e', 'ge'] + let s:targetMapping = '<Plug>CamelCaseMotion_i' . s:motion + execute s:mode . 'noremap <silent> ' . s:targetMapping . + \ ' :<C-U>call camelcasemotion#InnerMotion(''' . s:motion . ''',v:count1)<CR>' endfor -endfunction - -call s:CreateMotionMappings() -call s:CreateInnerMotionMappings() - -delfunction s:CreateMotionMappings -delfunction s:CreateInnerMotionMappings +endfor " vim: set sts=2 sw=2 expandtab ff=unix fdm=syntax : |