on master help sign in
Add home-end keymaps, (as kakoune/vim do) (#83)

* add home-end keymaps * implement extend methods for extend_line_start, extend_line_end * add home-end mappings to keymaps.md * add ^-$ extend mappings for extend mode * pass cargo linter

commita1f4b8f92b50fbb446400b167b344cdf189978f9 authorAntoni Stevenet <itzmjauz@gmail.com> dateSat 5 Jun 2021, 02:25 (5 years ago) parent72eaaaa
3 files changed+55−4
book/src/keymap.md+2 helix-term/src/commands.rs+31 helix-term/src/keymap.rs+22−4
book/src/keymap.md+2
@@ -17,6 +17,8 @@
1717| f | find next char |
1818| T | find 'till previous char |
1919| F | find previous char |
20+| Home | move to the start of the line |
21+| End | move to the end of the line |
2022| m | Jump to matching bracket |
2123| PageUp | Move page up |
2224| PageDown | Move page down |
helix-term/src/commands.rs+31
@@ -573,6 +573,37 @@ pub fn extend_line_down(cx: &mut Context) {
573573 doc.set_selection(view.id, selection);
574574}
575575
576+pub fn extend_line_end(cx: &mut Context) {
577+ let (view, doc) = cx.current();
578+
579+ let selection = doc.selection(view.id).transform(|range| {
580+ let text = doc.text();
581+ let line = text.char_to_line(range.head);
582+
583+ // Line end is pos at the start of next line - 1
584+ // subtract another 1 because the line ends with \n
585+ let pos = text.line_to_char(line + 1).saturating_sub(2);
586+ Range::new(range.anchor, pos)
587+ });
588+
589+ doc.set_selection(view.id, selection);
590+}
591+
592+pub fn extend_line_start(cx: &mut Context) {
593+ let (view, doc) = cx.current();
594+
595+ let selection = doc.selection(view.id).transform(|range| {
596+ let text = doc.text();
597+ let line = text.char_to_line(range.head);
598+
599+ // adjust to start of the line
600+ let pos = text.line_to_char(line);
601+ Range::new(range.anchor, pos)
602+ });
603+
604+ doc.set_selection(view.id, selection);
605+}
606+
576607pub fn select_all(cx: &mut Context) {
577608 let (view, doc) = cx.current();
578609
helix-term/src/keymap.rs+22−4
@@ -61,8 +61,8 @@ use std::collections::HashMap;
6161// in kakoune these are alt-h alt-l / gh gl
6262// select from curs to begin end / move curs to begin end
6363// 0 = start of line
64−// ^ = start of line (first non blank char)
65−// $ = end of line
64+// ^ = start of line(first non blank char) || Home = start of line(first non blank char)
65+// $ = end of line || End = end of line
6666//
6767// z = save selections
6868// Z = restore selections
@@ -153,6 +153,16 @@ pub fn default() -> Keymaps {
153153 //
154154 key!('r') => commands::replace,
155155
156+ KeyEvent {
157+ code: KeyCode::Home,
158+ modifiers: KeyModifiers::NONE
159+ } => commands::move_line_start,
160+
161+ KeyEvent {
162+ code: KeyCode::End,
163+ modifiers: KeyModifiers::NONE
164+ } => commands::move_line_end,
165+
156166 key!('w') => commands::move_next_word_start,
157167 key!('b') => commands::move_prev_word_start,
158168 key!('e') => commands::move_next_word_end,
@@ -306,13 +316,21 @@ pub fn default() -> Keymaps {
306316
307317 key!('t') => commands::extend_till_char,
308318 key!('f') => commands::extend_next_char,
319+
309320 key!('T') => commands::extend_till_prev_char,
310321 key!('F') => commands::extend_prev_char,
311−
322+ KeyEvent {
323+ code: KeyCode::Home,
324+ modifiers: KeyModifiers::NONE
325+ } => commands::extend_line_start,
326+ KeyEvent {
327+ code: KeyCode::End,
328+ modifiers: KeyModifiers::NONE
329+ } => commands::extend_line_end,
312330 KeyEvent {
313331 code: KeyCode::Esc,
314332 modifiers: KeyModifiers::NONE
315− } => commands::exit_select_mode as Command,
333+ } => commands::exit_select_mode,
316334 )
317335 .into_iter(),
318336 );