Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support both <A-*> and unicode mapping on mac #194

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,8 @@ type allCommands = {
interlaceInsertRepeat?: boolean,
exitVisualBlock?: boolean,
isEdit?: boolean,
repeatOverride?: number
repeatOverride?: number,
noremap?: boolean,
}
export type motionCommand = allCommands & {
type: 'motion',
Expand Down Expand Up @@ -223,7 +224,7 @@ export type operatorMotionCommand = allCommands & {
operator: string,
motionArgs?: MotionArgsPartial,
operatorArgs?: OperatorArgs,
operatorMotionArgs?: { [arg: string]: boolean | string }
operatorMotionArgs?: { [arg: string]: boolean | string },
}
export type idleCommand = allCommands & { type: 'idle' }
export type exCommand = allCommands & { type: 'ex' }
Expand Down
49 changes: 38 additions & 11 deletions src/vim.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,9 @@ export function initVim(CodeMirror) {
{ keys: '<C-u>', type: 'motion', motion: 'moveByScroll', motionArgs: { forward: false, explicitRepeat: true }},
{ keys: 'gg', type: 'motion', motion: 'moveToLineOrEdgeOfDocument', motionArgs: { forward: false, explicitRepeat: true, linewise: true, toJumplist: true }},
{ keys: 'G', type: 'motion', motion: 'moveToLineOrEdgeOfDocument', motionArgs: { forward: true, explicitRepeat: true, linewise: true, toJumplist: true }},
{keys: "g$", type: "motion", motion: "moveToEndOfDisplayLine"},
{keys: "g^", type: "motion", motion: "moveToStartOfDisplayLine"},
{keys: "g0", type: "motion", motion: "moveToStartOfDisplayLine"},
{ keys: "g$", type: "motion", motion: "moveToEndOfDisplayLine" },
{ keys: "g^", type: "motion", motion: "moveToStartOfDisplayLine" },
{ keys: "g0", type: "motion", motion: "moveToStartOfDisplayLine" },
{ keys: '0', type: 'motion', motion: 'moveToStartOfLine' },
{ keys: '^', type: 'motion', motion: 'moveToFirstNonWhiteSpaceCharacter' },
{ keys: '+', type: 'motion', motion: 'moveByLines', motionArgs: { forward: true, toFirstChar:true }},
Expand Down Expand Up @@ -256,6 +256,7 @@ export function initVim(CodeMirror) {
// Ex command
{ keys: ':', type: 'ex' }
];
var usedKeys = Object.create(null);
var defaultKeymapLength = defaultKeymap.length;

/**
Expand Down Expand Up @@ -971,7 +972,7 @@ export function initVim(CodeMirror) {
if (vim.insertMode) { command = handleKeyInsertMode(); }
else { command = handleKeyNonInsertMode(); }
if (command === false) {
return !vim.insertMode && key.length === 1 ? function() { return true; } : undefined;
return !vim.insertMode && (key.length === 1 || (CodeMirror.isMac && /<A-.>/.test(key)))? function() { return true; } : undefined;
} else if (command === true) {
// TODO: Look into using CodeMirror's multi-key handling.
// Return no-op since we are caching the key. Counts as handled, but
Expand Down Expand Up @@ -1149,7 +1150,7 @@ export function initVim(CodeMirror) {
// on mac many characters are entered as option- combos
// (e.g. on swiss keyboard { is option-8)
// so we ignore lonely A- modifier for keypress event on mac
if (CodeMirror.isMac && e.altKey && !e.metaKey && !e.ctrlKey) {
if (CodeMirror.isMac && name == "A-" && key.length == 1) {
name = name.slice(2);
}
if ((name || key.length > 1) && e.shiftKey) { name += 'S-'; }
Expand All @@ -1158,10 +1159,16 @@ export function initVim(CodeMirror) {
if (langmap.keymap && key in langmap.keymap) {
if (langmap.remapCtrl != false || !name)
key = langmap.keymap[key];
} else if (key.charCodeAt(0) > 255) {
var code = e.code?.slice(-1) || "";
if (!e.shiftKey) code = code.toLowerCase();
if (code) key = code;
} else if (key.charCodeAt(0) > 128) {
if (!usedKeys[key]) {
var code = e.code?.slice(-1) || "";
if (!e.shiftKey) code = code.toLowerCase();
if (code) {
key = code;
// also restore A- for mac
if (!name && e.altKey) name = 'A-'
}
}
}
}

Expand Down Expand Up @@ -5597,15 +5604,15 @@ export function initVim(CodeMirror) {
}
} else {
// Key to key or ex mapping
/**@type {vimKey} */
var mapping = {
keys: lhs,
type: 'keyToKey',
toKeys: rhs,
noremap: !!noremap
};
if (ctx) { mapping.context = ctx; }
// @ts-ignore
defaultKeymap.unshift(mapping);
_mapCommand(mapping);
}
}
/**@type {(lhs: string, ctx: string) => boolean|void} */
Expand All @@ -5625,6 +5632,7 @@ export function initVim(CodeMirror) {
if (keys == defaultKeymap[i].keys
&& defaultKeymap[i].context === ctx) {
defaultKeymap.splice(i, 1);
removeUsedKeys(keys);
return true;
}
}
Expand Down Expand Up @@ -6378,6 +6386,25 @@ export function initVim(CodeMirror) {
/** @arg {vimKey} command*/
function _mapCommand(command) {
defaultKeymap.unshift(command);
if (command.keys) addUsedKeys(command.keys);
}

/** @arg {string} keys */
function addUsedKeys(keys) {
keys.split(/(<(?:[CSMA]-)*\w+>|.)/i).forEach(function(part) {
if (part) {
if (!usedKeys[part]) usedKeys[part] = 0;
usedKeys[part]++;
}
});
}

/** @arg {string} keys */
function removeUsedKeys(keys) {
keys.split(/(<(?:[CSMA]-)*\w+>|.)/i).forEach(function(part) {
if (usedKeys[part])
usedKeys[part]--;
});
}

/**
Expand Down
47 changes: 43 additions & 4 deletions test/vim_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5541,6 +5541,41 @@ testVim('option_key_on_mac', function(cm, vim, helpers) {
helpers.assertCursorAt(3, 0);
typeKey.optionTextInput('8', '{');
helpers.assertCursorAt(0, 0);

typeKey.utfTextInput('d', 'д');
typeKey.utfTextInput('l', 'л');
typeKey.utfTextInput('d', 'д');
typeKey.utfTextInput('G', 'Г');
eq('', cm.getValue());
helpers.doKeys('"-p');
eq('0', cm.getValue());
//TODO bug in paste
helpers.doKeys('l');
helpers.assertCursorAt(0, 0);

// map A-v
helpers.doEx(':map <A-v> i<lt>A-v><Esc>');

// verify that replace still works
typeKey.utfTextInput('r', 'Ռ');
typeKey.optionTextInput('v', '√');
eq('√', cm.getValue());

typeKey.optionTextInput('v', '√');
eq('<A-v>√', cm.getValue());

helpers.doEx(':map √ i√G<Esc>');
typeKey.optionTextInput('v', '√');
eq('<A-v√G>√', cm.getValue());

helpers.doEx(':unmap √');
typeKey.optionTextInput('v', '√');
eq('<A-v√<A-v>G>√', cm.getValue());

helpers.doEx(':unmap <A-v>');
typeKey.optionTextInput('v', '√');
eq('<A-v√<A-v>G>√', cm.getValue());

CodeMirror.isMac = false;
}, { value: '0\n1\n2\n\n\n3\n4\n' });

Expand Down Expand Up @@ -5810,9 +5845,9 @@ var typeKey = function() {
}
var key = keyCodeToKey[(shift ? "s-" : "") + keyCode];

if (options && options.macAltText) {
alt = true;
text = key = options.macAltText;
if (options && options.text) {
alt = options.altKey;
text = key = options.text;
isTextInput = true;
}

Expand Down Expand Up @@ -5926,7 +5961,11 @@ var typeKey = function() {
// emulates option-9 inputting } on mac swiss keyboard
type.optionTextInput = function(letter, altText) {
reset();
sendKey(letter, {macAltText: altText});
sendKey(letter, {text: altText, altKey: true});
};

type.utfTextInput = function(letter, altText) {
sendKey(letter, {text: altText});
};

type.clipboard = {};
Expand Down
Loading