1
1
mirror of https://github.com/qvacua/vimr.git synced 2024-12-29 16:56:40 +03:00

Merge pull request #509 from qvacua/fix-ctrl

Make key handling more similar to Vim
This commit is contained in:
Tae Won Ha 2017-08-16 17:41:19 +02:00 committed by GitHub
commit a62797778b
2 changed files with 51 additions and 18 deletions

View File

@ -7,6 +7,18 @@ import Cocoa
class KeyUtils { class KeyUtils {
static func isControlCode(key: String) -> Bool {
guard key.characters.count == 1 else {
return false
}
guard let firstChar = key.utf16.first else {
return false
}
return firstChar < 32 && firstChar > 0
}
static func isSpecial(key: String) -> Bool { static func isSpecial(key: String) -> Bool {
guard key.characters.count == 1 else { guard key.characters.count == 1 else {
return false return false

View File

@ -27,22 +27,18 @@ extension NeoVimView {
? event.charactersIgnoringModifiers!.lowercased() ? event.charactersIgnoringModifiers!.lowercased()
: event.charactersIgnoringModifiers! : event.charactersIgnoringModifiers!
if KeyUtils.isSpecial(key: charsIgnoringModifiers) { let flags = self.vimModifierFlags(modifierFlags) ?? ""
if let vimModifiers = self.vimModifierFlags(modifierFlags) { let isNamedKey = KeyUtils.isSpecial(key: charsIgnoringModifiers)
self.agent.vimInput( let isControlCode = KeyUtils.isControlCode(key: chars) && !isNamedKey
self.wrapNamedKeys(vimModifiers + KeyUtils.namedKeyFrom(key: charsIgnoringModifiers)) let isPlain = flags.isEmpty && !isNamedKey
) let isWrapNeeded = !isControlCode && !isPlain
} else {
self.agent.vimInput(self.wrapNamedKeys(KeyUtils.namedKeyFrom(key: charsIgnoringModifiers)))
}
} else {
if let vimModifiers = self.vimModifierFlags(modifierFlags) {
self.agent.vimInput(self.wrapNamedKeys(vimModifiers + charsIgnoringModifiers))
} else {
self.agent.vimInput(self.vimPlainString(chars))
}
}
let namedChars = KeyUtils.namedKeyFrom(key: charsIgnoringModifiers)
let finalInput = isWrapNeeded
? self.wrapNamedKeys(flags + namedChars)
: self.vimPlainString(chars)
self.agent.vimInput(finalInput)
self.keyDownDone = true self.keyDownDone = true
} }
@ -82,19 +78,44 @@ extension NeoVimView {
} }
override public func performKeyEquivalent(with event: NSEvent) -> Bool { override public func performKeyEquivalent(with event: NSEvent) -> Bool {
let type = event.type if .keyDown != event.type { return false }
let flags = event.modifierFlags let flags = event.modifierFlags.intersection(.deviceIndependentFlagsMask)
/* <C-Tab> & <C-S-Tab> do not trigger keyDown events. /* <C-Tab> & <C-S-Tab> do not trigger keyDown events.
Catch the key event here and pass it to keyDown. Catch the key event here and pass it to keyDown.
(By rogual in NeoVim dot app (By rogual in NeoVim dot app
https://github.com/rogual/neovim-dot-app/pull/248/files ) https://github.com/rogual/neovim-dot-app/pull/248/files )
*/ */
if .keyDown == type && flags.contains(.control) && 48 == event.keyCode { if flags.contains(.control) && 48 == event.keyCode {
self.keyDown(with: event) self.keyDown(with: event)
return true return true
} }
guard let chars = event.characters else {
return false;
}
// Control code \0 causes rpc parsing problems.
// So we escape as early as possible
if chars == "\0" {
self.agent.vimInput(self.wrapNamedKeys("Nul"))
return true
}
// For the following two conditions:
// See special cases in vim/os_win32.c from vim sources
// Also mentioned in MacVim's KeyBindings.plist
if .control == flags && chars == "6" {
self.agent.vimInput("\u{1e}") // AKA ^^
return true
}
if .control == flags && chars == "2" {
// <C-2> should generate \0, escaping as above
self.agent.vimInput(self.wrapNamedKeys("Nul"))
return true
}
// NsEvent already sets \u{1f} for <C--> && <C-_>
return false return false
} }