From 9f8c26be23e2335e20f7198a4c9d29bd24ed918f Mon Sep 17 00:00:00 2001 From: Kevin Le Date: Mon, 23 Nov 2015 13:12:47 -0800 Subject: don't take over keys by default, require configuration in vimrc fixes #26 --- README.rst | 80 +++++++++++++++++++------------------------- autoload/camelcasemotion.vim | 45 ++++++++++++++++--------- doc/camelcasemotion.txt | 51 ++++++++++++++-------------- plugin/camelcasemotion.vim | 54 +++++++----------------------- 4 files changed, 102 insertions(+), 128 deletions(-) diff --git a/README.rst b/README.rst index 6b59931..bb86391 100644 --- a/README.rst +++ b/README.rst @@ -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 'w', 'b' and '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 'iw', 'ib' and 'ie', 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('') -The new motions are 'w', 'b' and 'e', all of which can +If you want to use different mappings, map your keys to the +CamelCaseMotion_? mapping targets your vimrc). + +EXAMPLE: Map to w, b and e mappings:: + map w CamelCaseMotion_w + map b CamelCaseMotion_b + map e CamelCaseMotion_e + map ge CamelCaseMotion_ge + sunmap w + sunmap b + sunmap e + sunmap ge + +EXAMPLE: Map iw, ib and ie motions:: + omap iw CamelCaseMotion_iw + xmap iw CamelCaseMotion_iw + omap ib CamelCaseMotion_ib + xmap ib CamelCaseMotion_ib + omap ie CamelCaseMotion_ie + xmap ie CamelCaseMotion_ie + +Most commonly motions are 'w', 'b' and 'e', all of which can be used in normal mode, operator-pending mode (cp. :help operator), and visual mode. For example, type 'bcw' 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 -``CamelCaseMotion_?`` mapping targets _before_ sourcing this script (e.g. in -your .vimrc). - -**Example**: Use 'W', 'B' and 'E':: - - map CamelCaseMotion_w - map CamelCaseMotion_b - map CamelCaseMotion_e - -**Example**: Replace the default 'w', 'b' and 'e' mappings instead of defining -additional mappings 'w', 'b' and 'e':: - - map w CamelCaseMotion_w - map b CamelCaseMotion_b - map e CamelCaseMotion_e - map ge CamelCaseMotion_e - sunmap w - sunmap b - sunmap e - sunmap ge - -**Example**: Replace default 'iw' text-object and define 'ib' and 'ie' -motions:: - - omap iw CamelCaseMotion_iw - xmap iw CamelCaseMotion_iw - omap ib CamelCaseMotion_ib - xmap ib CamelCaseMotion_ib - omap ie CamelCaseMotion_ie - xmap ie 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\@CamelCaseMotion_' . l:motion + execute (l:mode ==# 'v' ? 'x' : l:mode) . + \ 'map ' . 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 = 'CamelCaseMotion_i' . l:motion + execute (l:mode ==# 'v' ? 'x' : l:mode) . + \ 'map 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 |w|, |b| and |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 |iw|, |ib| and |ie|, 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* - *w* - *b* - *e* -Use the new motions |w|, |b| and |e| in normal mode, -operator-pending mode (cf. |operator|), and visual mode. For example, if the -cursor is on the 'm', type 'bcw' to change 'Camel' in 'CamelCase' to -something else. + +Call |camelcasemotion#CreateMotionMappings('')| to bind the new +motions |w|, |b| and |e| in normal mode, operator-pending +mode (cf. |operator|), and visual mode. For example, if the cursor is on the +'m', type 'bcw' 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('')| also binds the new +motions |iw|, |ib|, |ie|. + 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('') + If you want to use different mappings, map your keys to the -CamelCaseMotion_? mapping targets _before_ sourcing this script (e.g. in -your |vimrc|). +CamelCaseMotion_? mapping targets your |vimrc|). -EXAMPLE: Replace the default |w|, |b| and |e| mappings instead of defining -additional mappings |w|, |b| and |e|: > +EXAMPLE: Map to |w|, |b| and |e| mappings: > map w CamelCaseMotion_w map b CamelCaseMotion_b map e CamelCaseMotion_e @@ -105,7 +106,7 @@ additional mappings |w|, |b| and |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 iw CamelCaseMotion_iw xmap iw CamelCaseMotion_iw omap ib 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 CamelCaseMotion_? :call camelcasemotion#Motion('?',v:count1,'*') - " if ! hasmapto('CamelCaseMotion_?', '*') - " *map ,? CamelCaseMotion_? - " endif - - for l:mode in ['n', 'o', 'v'] - for l:motion in ['w', 'b', 'e', 'ge'] - let l:targetMapping = 'CamelCaseMotion_' . l:motion - execute l:mode . 'noremap ' . l:targetMapping . ' :call camelcasemotion#Motion(''' . l:motion . ''',v:count1,''' . l:mode . ''')' - - if ! hasmapto(l:targetMapping, l:mode) - execute (l:mode ==# 'v' ? 'x' : l:mode) . 'map ' . l:motion . ' ' . l:targetMapping - endif - endfor +for s:mode in ['n', 'o', 'v'] + for s:motion in ['w', 'b', 'e', 'ge'] + let s:targetMapping = 'CamelCaseMotion_' . s:motion + execute s:mode . 'noremap ' . s:targetMapping . + \ ' :call camelcasemotion#Motion(''' . s:motion . ''',v:count1,''' . s:mode . ''')' 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 CamelCaseMotion_i? :call camelcasemotion#InnerMotion('?',v:count1) - " if ! hasmapto('CamelCaseInnerMotion_i?', '*') - " *map i,? CamelCaseInnerMotion_i? - " endif - for l:mode in ['o', 'v'] - for l:motion in ['w', 'b', 'e', 'ge'] - let l:targetMapping = 'CamelCaseMotion_i' . l:motion - execute l:mode . 'noremap ' . l:targetMapping . ' :call camelcasemotion#InnerMotion(''' . l:motion . ''',v:count1)' - if ! hasmapto(l:targetMapping, l:mode) - execute (l:mode ==# 'v' ? 'x' : l:mode) . 'map i' . l:motion . ' ' . l:targetMapping - endif - endfor +for s:mode in ['o', 'v'] + for s:motion in ['w', 'b', 'e', 'ge'] + let s:targetMapping = 'CamelCaseMotion_i' . s:motion + execute s:mode . 'noremap ' . s:targetMapping . + \ ' :call camelcasemotion#InnerMotion(''' . s:motion . ''',v:count1)' 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 : -- cgit v1.1