From 301bc915bc3c2bc2a78a5126ac97c1abe7cfef59 Mon Sep 17 00:00:00 2001 From: groverlynn Date: Thu, 8 Feb 2024 02:50:55 +0100 Subject: [PATCH] prevent commit keystroke being hijacked & inline placeholder &fix terminal non-inline --- SquirrelInputController.m | 32 +++++++++++++++++++++++++++++--- data/squirrel.yaml | 7 +++++++ 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/SquirrelInputController.m b/SquirrelInputController.m index 02f0ef8a4..7aa4f567c 100644 --- a/SquirrelInputController.m +++ b/SquirrelInputController.m @@ -29,6 +29,9 @@ @implementation SquirrelInputController { NSString *_schemaId; BOOL _inlinePreedit; BOOL _inlineCandidate; + // app-specific bug fix + BOOL _inlinePlaceholder; + BOOL _panellessCommitFix; // for chord-typing int _chordKeyCodes[N_KEY_ROLL_OVER]; int _chordModifiers[N_KEY_ROLL_OVER]; @@ -196,6 +199,13 @@ -(BOOL)processKey:(int)rime_keycode modifiers:(int)rime_modifiers rime_get_api()->set_option(_session, "ascii_mode", True); // NSLog(@"turned Chinese mode off in vim-like editor's command mode"); } + + if (_panellessCommitFix && (rime_keycode == XK_Delete || + (rime_keycode >= XK_Home && rime_keycode <= XK_KP_Delete) || + (rime_keycode >= XK_BackSpace && rime_keycode <= XK_Escape))) { + [self showPlaceholder:@""]; + return NO; + } } // Simulate key-ups for every interesting key-down for chord-typing. @@ -418,6 +428,14 @@ -(void)commitString:(NSString*)string [NSApp.squirrelAppDelegate.panel hide]; } +-(void)showPlaceholder:(NSString*)placeholder { + NSDictionary* attrs = [self markForStyle:kTSMHiliteSelectedRawText atRange:NSMakeRange(0, MAX(placeholder.length, 1UL))]; + NSMutableAttributedString* attrString = [[NSMutableAttributedString alloc] initWithString:placeholder ? : @"█" attributes:attrs]; + [_currentClient setMarkedText:attrString + selectionRange:NSMakeRange(attrString.length, 0) + replacementRange:NSMakeRange(NSNotFound, NSNotFound)]; +} + -(void)showPreeditString:(NSString*)preedit selRange:(NSRange)range caretPos:(NSUInteger)pos @@ -508,6 +526,8 @@ -(void)updateAppOptions NSLog(@"set app option: %@ = %d", key, value); rime_get_api()->set_option(_session, key.UTF8String, value); } + _panellessCommitFix = appOptions[@"panelless_commit_fix"].boolValue; + _inlinePlaceholder = appOptions[@"inline_placeholder"].boolValue; } } @@ -526,7 +546,10 @@ -(void)rimeConsumeCommittedText RIME_STRUCT(RimeCommit, commit); if (rime_get_api()->get_commit(_session, &commit)) { NSString *commitText = @(commit.text); - [self commitString: commitText]; + if (_panellessCommitFix && [_currentClient markedRange].length == 0) { + [self showPlaceholder:nil]; + } + [self commitString:commitText]; rime_get_api()->free_commit(&commit); } } @@ -571,7 +594,9 @@ -(void)rimeUpdate NSUInteger end = substr(preedit, ctx.composition.sel_end).length; NSUInteger caretPos = substr(preedit, ctx.composition.cursor_pos).length; NSRange selRange = NSMakeRange(start, end - start); - if (_inlineCandidate) { + if (_panellessCommitFix && !preedit) { + [self showPlaceholder:nil]; + } else if (_inlineCandidate) { const char *candidatePreview = ctx.commit_text_preview; NSString *candidatePreviewText = candidatePreview ? @(candidatePreview) : @""; if (_inlinePreedit) { @@ -595,7 +620,8 @@ -(void)rimeUpdate // TRICKY: display a non-empty string to prevent iTerm2 from echoing each character in preedit. // note this is a full-shape space U+3000; using half shape characters like "..." will result in // an unstable baseline when composing Chinese characters. - [self showPreeditString:(preedit ? @" " : @"") selRange:empty caretPos:0]; + preedit && _inlinePlaceholder ? [self showPlaceholder: @" "] + : [self showPreeditString:@"" selRange:empty caretPos:0]; } } // update candidates diff --git a/data/squirrel.yaml b/data/squirrel.yaml index a231691b4..71c4fb8df 100644 --- a/data/squirrel.yaml +++ b/data/squirrel.yaml @@ -341,6 +341,7 @@ app_options: com.apple.Terminal: ascii_mode: true no_inline: true + inline_placeholder: true com.googlecode.iterm2: ascii_mode: true no_inline: true @@ -350,6 +351,7 @@ app_options: vim_mode: true # 退出VIM插入模式自動切換輸入法狀態 com.apple.dt.Xcode: ascii_mode: true + no_inline: true com.barebones.textwrangler: ascii_mode: true com.macromates.TextMate.preview: @@ -367,9 +369,14 @@ app_options: no_inline: true co.zeit.hyper: ascii_mode: true + org.alacritty: + ascii_mode: true + vim_mode: true + panelless_commit_fix: true com.google.Chrome: # 規避 https://github.com/rime/squirrel/issues/435 inline: true + inline_placeholder: true ru.keepcoder.Telegram: # 規避 https://github.com/rime/squirrel/issues/475 inline: true