Skip to content

Commit

Permalink
WIP on kb selection
Browse files Browse the repository at this point in the history
  • Loading branch information
yury committed Jan 27, 2018
1 parent e1e9efc commit a96cdac
Show file tree
Hide file tree
Showing 6 changed files with 237 additions and 143 deletions.
84 changes: 78 additions & 6 deletions Blink/TermInput.m
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ @implementation TermInput {
BOOL _dismissInput;

NSMutableArray<UIKeyCommand *> *_kbdCommands;
NSArray<UIKeyCommand *> *_selectionCommands;
SmartKeysController *_smartKeys;

BOOL _inputEnabled;
Expand Down Expand Up @@ -496,10 +497,9 @@ - (BOOL)canPerformAction:(SEL)action withSender:(id)sender
if ([sender isKindOfClass:[UIMenuController class]]) {
// The menu can only perform paste methods
if (action == @selector(paste:) ||
action == @selector(copy:) ||
action == @selector(copyLink:) ||
action == @selector(openLink:) ||
action == @selector(unselect:)
(action == @selector(copy:) && _termDelegate.termView.selectedText.length > 0) ||
(action == @selector(copyLink:) && _termDelegate.termView.detectedLink) ||
(action == @selector(openLink:) && _termDelegate.termView.detectedLink)
) {
return YES;
}
Expand All @@ -514,8 +514,6 @@ - (BOOL)canPerformAction:(SEL)action withSender:(id)sender
action == @selector(select:) ||
action == @selector(selectAll:) ||
action == @selector(delete:) ||
action == @selector(makeTextWritingDirectionLeftToRight:) ||
action == @selector(makeTextWritingDirectionRightToLeft:) ||
action == @selector(toggleBoldface:) ||
action == @selector(toggleItalics:) ||
action == @selector(toggleUnderline:)
Expand Down Expand Up @@ -671,6 +669,9 @@ - (NSArray *)_functionModifierKeys

- (NSArray<UIKeyCommand *> *)keyCommands
{
if (_termDelegate.termView.selectedText.length > 0) {
return _selectionCommands;
}
return _kbdCommands;
}

Expand All @@ -688,6 +689,40 @@ - (void)yank:(id)sender
if (str) {
[_termDelegate write:str];
}
[_termDelegate.termView cleanSelection];
}

- (void)_changeSelection:(UIKeyCommand *) cmd
{
NSString *input = cmd.input;

if ([input isEqualToString:UIKeyInputLeftArrow] || [input isEqualToString:@"h"]) {
[_termDelegate.termView modifySelectionInDirection:@"left" granularity:
cmd.modifierFlags == UIKeyModifierShift ? @"word" : @"character"];
} else if ([input isEqualToString:UIKeyInputRightArrow] || [input isEqualToString:@"l"]) {
[_termDelegate.termView modifySelectionInDirection:@"right" granularity:
cmd.modifierFlags == UIKeyModifierShift ? @"word" : @"character"];
} else if ([input isEqualToString:UIKeyInputUpArrow] || [input isEqualToString:@"k"]) {
[_termDelegate.termView modifySelectionInDirection:@"left" granularity:@"line"];
} else if ([input isEqualToString:UIKeyInputDownArrow] || [input isEqualToString:@"j"]) {
[_termDelegate.termView modifySelectionInDirection:@"right" granularity:@"line"];
} else if ([input isEqualToString:@"o"] || [input isEqualToString:@"x"]) {
[_termDelegate.termView modifySideOfSelection];
} else if ([input isEqualToString:@"b"]) {
if (cmd.modifierFlags == UIKeyModifierControl) {
[_termDelegate.termView modifySelectionInDirection:@"left" granularity:@"character"];
} else {
[_termDelegate.termView modifySelectionInDirection:@"left" granularity:@"word"];
}
} else if ([input isEqualToString:@"w"]) {
[_termDelegate.termView modifySelectionInDirection:@"right" granularity:@"word"];
} else if ([input isEqualToString:@"f"]) {
if (cmd.modifierFlags == UIKeyModifierControl) {
[_termDelegate.termView modifySelectionInDirection:@"right" granularity:@"character"];
}
} else if ([input isEqualToString:@"y"]) {
[self copy: self];
}
}

- (void)_resetDefaultControlKeys
Expand All @@ -699,9 +734,46 @@ - (void)_resetDefaultControlKeys
[self _setKbdCommands];
}

- (void)_configureSelectionShortcuts
{
_selectionCommands =
@[
// Default
[UIKeyCommand keyCommandWithInput:UIKeyInputUpArrow modifierFlags:kNilOptions action:@selector(_changeSelection:)],
[UIKeyCommand keyCommandWithInput:UIKeyInputDownArrow modifierFlags:kNilOptions action:@selector(_changeSelection:)],
[UIKeyCommand keyCommandWithInput:UIKeyInputLeftArrow modifierFlags:kNilOptions action:@selector(_changeSelection:)],
[UIKeyCommand keyCommandWithInput:UIKeyInputRightArrow modifierFlags:kNilOptions action:@selector(_changeSelection:)],

[UIKeyCommand keyCommandWithInput:UIKeyInputUpArrow modifierFlags:UIKeyModifierShift action:@selector(_changeSelection:)],
[UIKeyCommand keyCommandWithInput:UIKeyInputDownArrow modifierFlags:UIKeyModifierShift action:@selector(_changeSelection:)],
[UIKeyCommand keyCommandWithInput:UIKeyInputLeftArrow modifierFlags:UIKeyModifierShift action:@selector(_changeSelection:)],
[UIKeyCommand keyCommandWithInput:UIKeyInputRightArrow modifierFlags:UIKeyModifierShift action:@selector(_changeSelection:)],

// VIM
[UIKeyCommand keyCommandWithInput:@"j" modifierFlags:kNilOptions action:@selector(_changeSelection:)],
[UIKeyCommand keyCommandWithInput:@"k" modifierFlags:kNilOptions action:@selector(_changeSelection:)],
[UIKeyCommand keyCommandWithInput:@"h" modifierFlags:kNilOptions action:@selector(_changeSelection:)],
[UIKeyCommand keyCommandWithInput:@"l" modifierFlags:kNilOptions action:@selector(_changeSelection:)],
[UIKeyCommand keyCommandWithInput:@"b" modifierFlags:kNilOptions action:@selector(_changeSelection:)],
[UIKeyCommand keyCommandWithInput:@"w" modifierFlags:kNilOptions action:@selector(_changeSelection:)],
[UIKeyCommand keyCommandWithInput:@"y" modifierFlags:kNilOptions action:@selector(_changeSelection:)],

[UIKeyCommand keyCommandWithInput:@"o" modifierFlags:kNilOptions action:@selector(_changeSelection:)],

// EMACS
[UIKeyCommand keyCommandWithInput:@"b" modifierFlags:UIKeyModifierControl action:@selector(_changeSelection:)],
[UIKeyCommand keyCommandWithInput:@"f" modifierFlags:UIKeyModifierControl action:@selector(_changeSelection:)],


[UIKeyCommand keyCommandWithInput:@"x" modifierFlags:UIKeyModifierControl action:@selector(_changeSelection:)],
];
}

- (void)_configureShotcuts
{
[self _resetDefaultControlKeys];

[self _configureSelectionShortcuts];

if ([BKDefaults autoRepeatKeys]) {
[self _assignSequence:TermViewAutoRepeateSeq toModifier:0];
Expand Down
12 changes: 11 additions & 1 deletion Blink/TermJS.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,23 @@ NSString *term_setFontFamily(NSString *family)

NSString *term_appendUserCss(NSString *css)
{
return [NSString stringWithFormat:@"\nterm_appendUserCss(%@[0])", _encodeString(css)];
return [NSString stringWithFormat:@"term_appendUserCss(%@[0])", _encodeString(css)];
}

NSString *term_cleanSelection()
{
return @"term_cleanSelection();";
}

NSString *term_modifySelection(NSString *direction, NSString *granularity)
{
return [NSString stringWithFormat:@"term_modifySelection(%@[0], %@[0])", _encodeString(direction), _encodeString(granularity)];
}

NSString *term_modifySideSelection()
{
return @"term_modifySideSelection();";
}


#endif /* TermJS_h */
2 changes: 2 additions & 0 deletions Blink/TermView.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,6 @@
- (void)setFreezed:(BOOL)enabled;
- (BOOL)isDragging;

- (void)modifySideOfSelection;
- (void)modifySelectionInDirection:(NSString *)direction granularity:(NSString *)granularity;
@end
Loading

0 comments on commit a96cdac

Please sign in to comment.