From d9c3c62eaf923d6bae7db72772e008d39b38f4df Mon Sep 17 00:00:00 2001 From: ichizok Date: Wed, 13 Jun 2018 19:49:39 +0900 Subject: [PATCH] Add the condition for calling interpretKeyEvents --- src/MacVim/MMTextViewHelper.m | 39 +++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/src/MacVim/MMTextViewHelper.m b/src/MacVim/MMTextViewHelper.m index ea3ee42615..284462ce74 100644 --- a/src/MacVim/MMTextViewHelper.m +++ b/src/MacVim/MMTextViewHelper.m @@ -151,6 +151,10 @@ - (void)keyDown:(NSEvent *)event id mmta = [[[self vimController] vimState] objectForKey:@"p_mmta"]; NSString *string = [event characters]; NSString *unmod = [event charactersIgnoringModifiers]; + BOOL modCommand = (flags & NSEventModifierFlagCommand) ? YES : NO; + BOOL modControl = (flags & NSEventModifierFlagControl) ? YES : NO; + BOOL modOption = (flags & NSEventModifierFlagOption) ? YES : NO; + BOOL modShift = (flags & NSEventModifierFlagShift) ? YES : NO; // Alt key presses should not be interpreted if the 'macmeta' option is // set. We still have to call interpretKeyEvents: for keys @@ -158,13 +162,11 @@ - (void)keyDown:(NSEvent *)event // ASCII chars in the range after space (0x20) and before backspace (0x7f). // Note that this implies that 'mmta' (if enabled) breaks input methods // when the Alt key is held. - if ((flags & NSEventModifierFlagOption) - && [mmta boolValue] && [unmod length] == 1 + if (modOption && [mmta boolValue] && [unmod length] == 1 && [unmod characterAtIndex:0] > 0x20) { ASLogDebug(@"MACMETA key, don't interpret it"); string = unmod; - } else if (imState && (flags & NSEventModifierFlagControl) - && !(flags & (NSEventModifierFlagOption|NSEventModifierFlagCommand)) + } else if (imState && (modControl && !modCommand && !modOption) && [unmod length] == 1 && ([unmod characterAtIndex:0] == '6' || [unmod characterAtIndex:0] == '^')) { @@ -173,23 +175,28 @@ - (void)keyDown:(NSEvent *)event [self doKeyDown:@"\x1e"]; string = nil; } else { - // HACK! interpretKeyEvents: may call insertText: or - // doCommandBySelector:, or it may swallow the key (most likely the - // current input method used it). In the first two cases we have to - // manually set the below flag to NO if the key wasn't handled. - interpretKeyEventsSwallowedKey = YES; - [textView interpretKeyEvents:[NSArray arrayWithObject:event]]; - if (interpretKeyEventsSwallowedKey) - string = nil; - else if (flags & NSEventModifierFlagCommand) { + // When using JapaneseIM with "Windows-like shortcuts" turned on, + // interpretKeyEvents: does not call doCommandBySelector: with Ctrl-O + // and Ctrl-U (why?), so we cannot handle them at all. + // As a workaround, we do not call interpretKeyEvents: when no marked + // text and with only control-modifier. + if ([self hasMarkedText] || (!modControl || modCommand || modOption)) { + // HACK! interpretKeyEvents: may call insertText: or + // doCommandBySelector:, or it may swallow the key (most likely the + // current input method used it). In the first two cases we have to + // manually set the below flag to NO if the key wasn't handled. + interpretKeyEventsSwallowedKey = YES; + [textView interpretKeyEvents:[NSArray arrayWithObject:event]]; + if (interpretKeyEventsSwallowedKey) + string = nil; + } + if (string && modCommand) { // HACK! When Command is held we have to more or less guess whether // we should use characters or charactersIgnoringModifiers. The // following heuristic seems to work but it may have to change. // Note that the Shift and Alt flags may also need to be cleared // (see doKeyDown:keyCode:modifiers: in MMBackend). - if ((flags & NSEventModifierFlagShift - && !(flags & NSEventModifierFlagOption)) - || flags & NSEventModifierFlagControl) + if ((modShift && !modOption) || modControl) string = unmod; } }