Skip to content

Commit

Permalink
prevent commit keystroke being hijacked & inline placeholder &fix ter…
Browse files Browse the repository at this point in the history
…minal non-inline
  • Loading branch information
groverlynn committed Feb 8, 2024
1 parent 7de3919 commit 301bc91
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
32 changes: 29 additions & 3 deletions SquirrelInputController.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
}

Expand All @@ -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);
}
}
Expand Down Expand Up @@ -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) {
Expand All @@ -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
Expand Down
7 changes: 7 additions & 0 deletions data/squirrel.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -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

0 comments on commit 301bc91

Please sign in to comment.