Skip to content

Commit

Permalink
Add "first-segment" and "last-segment" commands
Browse files Browse the repository at this point in the history
Bind them to Home/End respectively.
Fixes the problem reported by Jens Petersen:
https://bugzilla.redhat.com/show_bug.cgi?id=1056912
  • Loading branch information
ueno committed Feb 5, 2014
1 parent ed40055 commit e1617f6
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 0 deletions.
2 changes: 2 additions & 0 deletions data/rules/default/keymap/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
"Right": "next-segment",
"Left": "previous-segment",
"C-f": "next-segment",
"Home": "first-segment",
"End": "last-segment",
"C-Right": "expand-segment",
"C-Left": "shrink-segment",
"S-Right": "expand-segment",
Expand Down
4 changes: 4 additions & 0 deletions libkkc/convert-segment-state-handler.vala
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ namespace Kkc {
do_select_unhandled);
register_command_callback ("previous-segment",
do_select_unhandled);
register_command_callback ("first-segment",
do_select_unhandled);
register_command_callback ("last-segment",
do_select_unhandled);
register_command_callback ("delete",
do_clear_unhandled);
register_command_callback ("original-candidate",
Expand Down
14 changes: 14 additions & 0 deletions libkkc/convert-sentence-state-handler.vala
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ namespace Kkc {
do_next_segment);
register_command_callback ("previous-segment",
do_previous_segment);
register_command_callback ("first-segment",
do_first_segment);
register_command_callback ("last-segment",
do_last_segment);

register_command_callback ("abort", do_clear_unhandled);
register_command_callback ("delete", do_clear_unhandled);
Expand Down Expand Up @@ -95,6 +99,16 @@ namespace Kkc {
return true;
}

bool do_first_segment (string? command, State state, KeyEvent key) {
state.segments.first_segment ();
return true;
}

bool do_last_segment (string? command, State state, KeyEvent key) {
state.segments.last_segment ();
return true;
}

bool do_start_segment_conversion (string? command, State state, KeyEvent key) {
state.lookup (state.segments[state.segments.cursor_pos]);
state.candidates.first ();
Expand Down
20 changes: 20 additions & 0 deletions libkkc/initial-state-handler.vala
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ namespace Kkc {
register_command_callback ("next-candidate", do_next_candidate);
register_command_callback ("next-segment", do_next_character);
register_command_callback ("previous-segment", do_previous_character);
register_command_callback ("first-segment", do_first_character);
register_command_callback ("last-segment", do_last_character);
register_command_callback ("quote", do_quote);
register_command_callback ("register", do_register);

Expand Down Expand Up @@ -208,6 +210,24 @@ namespace Kkc {
return true;
}

bool do_first_character (string? command, State state, KeyEvent key) {
state.finish_input_editing ();
if (state.input_characters.size == 0)
return false;

state.input_characters_cursor_pos = 0;
return true;
}

bool do_last_character (string? command, State state, KeyEvent key) {
state.finish_input_editing ();
if (state.input_characters.size == 0)
return false;

state.input_characters_cursor_pos = state.input_characters.size - 1;
return true;
}

bool do_ (string? command, State state, KeyEvent key) {
bool retval = false;

Expand Down
2 changes: 2 additions & 0 deletions libkkc/keymap.vala
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ namespace Kkc {
public class Keymap : Object {
static const KeymapCommandEntry Commands[] = {
{ "abort", N_("Abort") },
{ "first-segment", N_("First Segment") },
{ "last-segment", N_("Last Segment") },
{ "commit", N_("Commit") },
{ "complete", N_("Complete") },
{ "delete", N_("Delete") },
Expand Down
8 changes: 8 additions & 0 deletions libkkc/segment-list.vala
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@ namespace Kkc {
return false;
}

public bool last_segment () {
if (segments.size > 0) {
cursor_pos = segments.size - 1;
return true;
}
return false;
}

public void next_segment () {
if (cursor_pos == -1)
return;
Expand Down
18 changes: 18 additions & 0 deletions tests/context.vala
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,28 @@ class ContextTests : Kkc.TestCase {
context.reset ();
context.clear_output ();

try {
context.process_key_events ("a a a");
assert (context.process_key_events ("Left"));
assert (context.input_cursor_pos == 2);
assert (context.process_key_events ("Right"));
assert (context.input_cursor_pos == -1);
assert (context.process_key_events ("Home"));
assert (context.input_cursor_pos == 0);
assert (context.process_key_events ("End"));
assert (context.input_cursor_pos == 2);
} catch (Kkc.KeyEventFormatError e) {
assert_not_reached ();
}
context.reset ();
context.clear_output ();

try {
context.process_key_events ("a a a RET");
assert (!context.process_key_events ("Left"));
assert (!context.process_key_events ("Right"));
assert (!context.process_key_events ("Home"));
assert (!context.process_key_events ("End"));
} catch (Kkc.KeyEventFormatError e) {
assert_not_reached ();
}
Expand Down

0 comments on commit e1617f6

Please sign in to comment.