Skip to content

Commit 301bc91

Browse files
committed
prevent commit keystroke being hijacked & inline placeholder &fix terminal non-inline
1 parent 7de3919 commit 301bc91

2 files changed

Lines changed: 36 additions & 3 deletions

File tree

SquirrelInputController.m

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ @implementation SquirrelInputController {
2929
NSString *_schemaId;
3030
BOOL _inlinePreedit;
3131
BOOL _inlineCandidate;
32+
// app-specific bug fix
33+
BOOL _inlinePlaceholder;
34+
BOOL _panellessCommitFix;
3235
// for chord-typing
3336
int _chordKeyCodes[N_KEY_ROLL_OVER];
3437
int _chordModifiers[N_KEY_ROLL_OVER];
@@ -196,6 +199,13 @@ -(BOOL)processKey:(int)rime_keycode modifiers:(int)rime_modifiers
196199
rime_get_api()->set_option(_session, "ascii_mode", True);
197200
// NSLog(@"turned Chinese mode off in vim-like editor's command mode");
198201
}
202+
203+
if (_panellessCommitFix && (rime_keycode == XK_Delete ||
204+
(rime_keycode >= XK_Home && rime_keycode <= XK_KP_Delete) ||
205+
(rime_keycode >= XK_BackSpace && rime_keycode <= XK_Escape))) {
206+
[self showPlaceholder:@""];
207+
return NO;
208+
}
199209
}
200210

201211
// Simulate key-ups for every interesting key-down for chord-typing.
@@ -418,6 +428,14 @@ -(void)commitString:(NSString*)string
418428
[NSApp.squirrelAppDelegate.panel hide];
419429
}
420430

431+
-(void)showPlaceholder:(NSString*)placeholder {
432+
NSDictionary* attrs = [self markForStyle:kTSMHiliteSelectedRawText atRange:NSMakeRange(0, MAX(placeholder.length, 1UL))];
433+
NSMutableAttributedString* attrString = [[NSMutableAttributedString alloc] initWithString:placeholder ? : @"" attributes:attrs];
434+
[_currentClient setMarkedText:attrString
435+
selectionRange:NSMakeRange(attrString.length, 0)
436+
replacementRange:NSMakeRange(NSNotFound, NSNotFound)];
437+
}
438+
421439
-(void)showPreeditString:(NSString*)preedit
422440
selRange:(NSRange)range
423441
caretPos:(NSUInteger)pos
@@ -508,6 +526,8 @@ -(void)updateAppOptions
508526
NSLog(@"set app option: %@ = %d", key, value);
509527
rime_get_api()->set_option(_session, key.UTF8String, value);
510528
}
529+
_panellessCommitFix = appOptions[@"panelless_commit_fix"].boolValue;
530+
_inlinePlaceholder = appOptions[@"inline_placeholder"].boolValue;
511531
}
512532
}
513533

@@ -526,7 +546,10 @@ -(void)rimeConsumeCommittedText
526546
RIME_STRUCT(RimeCommit, commit);
527547
if (rime_get_api()->get_commit(_session, &commit)) {
528548
NSString *commitText = @(commit.text);
529-
[self commitString: commitText];
549+
if (_panellessCommitFix && [_currentClient markedRange].length == 0) {
550+
[self showPlaceholder:nil];
551+
}
552+
[self commitString:commitText];
530553
rime_get_api()->free_commit(&commit);
531554
}
532555
}
@@ -571,7 +594,9 @@ -(void)rimeUpdate
571594
NSUInteger end = substr(preedit, ctx.composition.sel_end).length;
572595
NSUInteger caretPos = substr(preedit, ctx.composition.cursor_pos).length;
573596
NSRange selRange = NSMakeRange(start, end - start);
574-
if (_inlineCandidate) {
597+
if (_panellessCommitFix && !preedit) {
598+
[self showPlaceholder:nil];
599+
} else if (_inlineCandidate) {
575600
const char *candidatePreview = ctx.commit_text_preview;
576601
NSString *candidatePreviewText = candidatePreview ? @(candidatePreview) : @"";
577602
if (_inlinePreedit) {
@@ -595,7 +620,8 @@ -(void)rimeUpdate
595620
// TRICKY: display a non-empty string to prevent iTerm2 from echoing each character in preedit.
596621
// note this is a full-shape space U+3000; using half shape characters like "..." will result in
597622
// an unstable baseline when composing Chinese characters.
598-
[self showPreeditString:(preedit ? @" " : @"") selRange:empty caretPos:0];
623+
preedit && _inlinePlaceholder ? [self showPlaceholder: @" "]
624+
: [self showPreeditString:@"" selRange:empty caretPos:0];
599625
}
600626
}
601627
// update candidates

data/squirrel.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,7 @@ app_options:
341341
com.apple.Terminal:
342342
ascii_mode: true
343343
no_inline: true
344+
inline_placeholder: true
344345
com.googlecode.iterm2:
345346
ascii_mode: true
346347
no_inline: true
@@ -350,6 +351,7 @@ app_options:
350351
vim_mode: true # 退出VIM插入模式自動切換輸入法狀態
351352
com.apple.dt.Xcode:
352353
ascii_mode: true
354+
no_inline: true
353355
com.barebones.textwrangler:
354356
ascii_mode: true
355357
com.macromates.TextMate.preview:
@@ -367,9 +369,14 @@ app_options:
367369
no_inline: true
368370
co.zeit.hyper:
369371
ascii_mode: true
372+
org.alacritty:
373+
ascii_mode: true
374+
vim_mode: true
375+
panelless_commit_fix: true
370376
com.google.Chrome:
371377
# 規避 https://github.com/rime/squirrel/issues/435
372378
inline: true
379+
inline_placeholder: true
373380
ru.keepcoder.Telegram:
374381
# 規避 https://github.com/rime/squirrel/issues/475
375382
inline: true

0 commit comments

Comments
 (0)