@@ -133,9 +133,9 @@ export function initVim(CodeMirror) {
133133 { keys : '<C-u>' , type : 'motion' , motion : 'moveByScroll' , motionArgs : { forward : false , explicitRepeat : true } } ,
134134 { keys : 'gg' , type : 'motion' , motion : 'moveToLineOrEdgeOfDocument' , motionArgs : { forward : false , explicitRepeat : true , linewise : true , toJumplist : true } } ,
135135 { keys : 'G' , type : 'motion' , motion : 'moveToLineOrEdgeOfDocument' , motionArgs : { forward : true , explicitRepeat : true , linewise : true , toJumplist : true } } ,
136- { keys : "g$" , type : "motion" , motion : "moveToEndOfDisplayLine" } ,
137- { keys : "g^" , type : "motion" , motion : "moveToStartOfDisplayLine" } ,
138- { keys : "g0" , type : "motion" , motion : "moveToStartOfDisplayLine" } ,
136+ { keys : "g$" , type : "motion" , motion : "moveToEndOfDisplayLine" } ,
137+ { keys : "g^" , type : "motion" , motion : "moveToStartOfDisplayLine" } ,
138+ { keys : "g0" , type : "motion" , motion : "moveToStartOfDisplayLine" } ,
139139 { keys : '0' , type : 'motion' , motion : 'moveToStartOfLine' } ,
140140 { keys : '^' , type : 'motion' , motion : 'moveToFirstNonWhiteSpaceCharacter' } ,
141141 { keys : '+' , type : 'motion' , motion : 'moveByLines' , motionArgs : { forward : true , toFirstChar :true } } ,
@@ -257,6 +257,7 @@ export function initVim(CodeMirror) {
257257 // Ex command
258258 { keys : ':' , type : 'ex' }
259259 ] ;
260+ var usedKeys = Object . create ( null ) ;
260261 var defaultKeymapLength = defaultKeymap . length ;
261262
262263 /**
@@ -972,7 +973,7 @@ export function initVim(CodeMirror) {
972973 if ( vim . insertMode ) { command = handleKeyInsertMode ( ) ; }
973974 else { command = handleKeyNonInsertMode ( ) ; }
974975 if ( command === false ) {
975- return ! vim . insertMode && key . length === 1 ? function ( ) { return true ; } : undefined ;
976+ return ! vim . insertMode && ( key . length === 1 || ( CodeMirror . isMac && / < A - . > / . test ( key ) ) ) ? function ( ) { return true ; } : undefined ;
976977 } else if ( command === true ) {
977978 // TODO: Look into using CodeMirror's multi-key handling.
978979 // Return no-op since we are caching the key. Counts as handled, but
@@ -1150,7 +1151,7 @@ export function initVim(CodeMirror) {
11501151 // on mac many characters are entered as option- combos
11511152 // (e.g. on swiss keyboard { is option-8)
11521153 // so we ignore lonely A- modifier for keypress event on mac
1153- if ( CodeMirror . isMac && e . altKey && ! e . metaKey && ! e . ctrlKey ) {
1154+ if ( CodeMirror . isMac && name == "A-" && key . length == 1 ) {
11541155 name = name . slice ( 2 ) ;
11551156 }
11561157 if ( ( name || key . length > 1 ) && e . shiftKey ) { name += 'S-' ; }
@@ -1159,10 +1160,16 @@ export function initVim(CodeMirror) {
11591160 if ( langmap . keymap && key in langmap . keymap ) {
11601161 if ( langmap . remapCtrl != false || ! name )
11611162 key = langmap . keymap [ key ] ;
1162- } else if ( key . charCodeAt ( 0 ) > 255 ) {
1163- var code = e . code ?. slice ( - 1 ) || "" ;
1164- if ( ! e . shiftKey ) code = code . toLowerCase ( ) ;
1165- if ( code ) key = code ;
1163+ } else if ( key . charCodeAt ( 0 ) > 128 ) {
1164+ if ( ! usedKeys [ key ] ) {
1165+ var code = e . code ?. slice ( - 1 ) || "" ;
1166+ if ( ! e . shiftKey ) code = code . toLowerCase ( ) ;
1167+ if ( code ) {
1168+ key = code ;
1169+ // also restore A- for mac
1170+ if ( ! name && e . altKey ) name = 'A-'
1171+ }
1172+ }
11661173 }
11671174 }
11681175
@@ -5663,15 +5670,15 @@ export function initVim(CodeMirror) {
56635670 }
56645671 } else {
56655672 // Key to key or ex mapping
5673+ /**@type {vimKey } */
56665674 var mapping = {
56675675 keys : lhs ,
56685676 type : 'keyToKey' ,
56695677 toKeys : rhs ,
56705678 noremap : ! ! noremap
56715679 } ;
56725680 if ( ctx ) { mapping . context = ctx ; }
5673- // @ts -ignore
5674- defaultKeymap . unshift ( mapping ) ;
5681+ _mapCommand ( mapping ) ;
56755682 }
56765683 }
56775684 /**@type {(lhs: string, ctx: string) => boolean|void } */
@@ -5691,6 +5698,7 @@ export function initVim(CodeMirror) {
56915698 if ( keys == defaultKeymap [ i ] . keys
56925699 && defaultKeymap [ i ] . context === ctx ) {
56935700 defaultKeymap . splice ( i , 1 ) ;
5701+ removeUsedKeys ( keys ) ;
56945702 return true ;
56955703 }
56965704 }
@@ -6472,6 +6480,25 @@ export function initVim(CodeMirror) {
64726480 /** @arg {vimKey} command*/
64736481 function _mapCommand ( command ) {
64746482 defaultKeymap . unshift ( command ) ;
6483+ if ( command . keys ) addUsedKeys ( command . keys ) ;
6484+ }
6485+
6486+ /** @arg {string} keys */
6487+ function addUsedKeys ( keys ) {
6488+ keys . split ( / ( < (?: [ C S M A ] - ) * \w + > | .) / i) . forEach ( function ( part ) {
6489+ if ( part ) {
6490+ if ( ! usedKeys [ part ] ) usedKeys [ part ] = 0 ;
6491+ usedKeys [ part ] ++ ;
6492+ }
6493+ } ) ;
6494+ }
6495+
6496+ /** @arg {string} keys */
6497+ function removeUsedKeys ( keys ) {
6498+ keys . split ( / ( < (?: [ C S M A ] - ) * \w + > | .) / i) . forEach ( function ( part ) {
6499+ if ( usedKeys [ part ] )
6500+ usedKeys [ part ] -- ;
6501+ } ) ;
64756502 }
64766503
64776504 /**
0 commit comments