summaryrefslogtreecommitdiff
path: root/helix-view/src/keymap.rs
blob: 69e6cabbf15e88124c1032554fcc7de2b1513823 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
use crate::commands::{self, Command};
use helix_core::{hashmap, state};
use std::collections::HashMap;

// Kakoune-inspired:
// mode = {
//      normal = {
//          q = record_macro
//          w = (next) word
//          W = next WORD
//          e = end of word
//          E = end of WORD
//          r =
//          t = 'till char
//          y = yank
//          u = undo
//          U = redo
//          i = insert
//          I = INSERT (start of line)
//          o = open below (insert on new line below)
//          O = open above (insert on new line above)
//          p = paste (before cursor)
//          P = PASTE (after cursor)
//          ` =
//          [ = select to text object start (alt = select whole object)
//          ] = select to text object end
//          { = extend to inner object start
//          } = extend to inner object end
//          a = append
//          A = APPEND (end of line)
//          s = split
//          S = select
//          d = delete()
//          f = find_char()
//          g = goto (gg, G, gc, gd, etc)
//
//          h = move_char_left(n)
//          j = move_line_down(n)
//          k = move_line_up(n)
//          l = move_char_right(n)
//          : = command line
//          ; = collapse selection to cursor
//          " = use register
//          ` = convert case? (to lower) (alt = swap case)
//          ~ = convert to upper case
//          . = repeat last command
//          \ = disable hook?
//          / = search
//          > = indent
//          < = deindent
//          % = select whole buffer (in vim = jump to matching bracket)
//          * = search pattern in selection
//          ( = rotate main selection backward
//          ) = rotate main selection forward
//          - = trim selections? (alt = merge contiguous sel together)
//          @ = convert tabs to spaces
//          & = align cursor
//          ? = extend to next given regex match (alt = to prev)
//
//          in kakoune these are alt-h alt-l / gh gl
//                              select from curs to begin end / move curs to begin end
//          0 = start of line
//          ^ = start of line (first non blank char)
//          $ = end of line
//
//          z = save selections
//          Z = restore selections
//          x = select line
//          X = extend line
//          c = change selected text
//          C = copy selection?
//          v = view menu (viewport manipulation)
//          b = select to previous word start
//          B = select to previous WORD start
//
//
//
//
//
//
//          = = align?
//          + =
//      }
// }

#[cfg(feature = "term")]
pub use crossterm::event::{KeyCode, KeyEvent as Key, KeyModifiers as Modifiers};

// TODO: could be trie based
type Keymap = HashMap<Vec<Key>, Command>;
type Keymaps = HashMap<state::Mode, Keymap>;

macro_rules! key {
    ($ch:expr) => {
        Key {
            code: KeyCode::Char($ch),
            modifiers: Modifiers::NONE,
        }
    };
}

macro_rules! shift {
    ($ch:expr) => {
        Key {
            code: KeyCode::Char($ch),
            modifiers: Modifiers::SHIFT,
        }
    };
}

macro_rules! ctrl {
    ($ch:expr) => {
        Key {
            code: KeyCode::Char($ch),
            modifiers: Modifiers::CONTROL,
        }
    };
}

// macro_rules! alt {
//     ($ch:expr) => {
//         Key {
//             code: KeyCode::Char($ch),
//             modifiers: Modifiers::ALT,
//         }
//     };
// }

pub fn default() -> Keymaps {
    hashmap!(
        state::Mode::Normal =>
            // as long as you cast the first item, rust is able to infer the other cases
            hashmap!(
                vec![key!('h')] => commands::move_char_left as Command,
                vec![key!('j')] => commands::move_line_down,
                vec![key!('k')] => commands::move_line_up,
                vec![key!('0')] => commands::move_line_start,
                vec![key!('$')] => commands::move_line_end,
                vec![key!('l')] => commands::move_char_right,
                vec![shift!('H')] => commands::extend_char_left,
                vec![shift!('J')] => commands::extend_line_down,
                vec![shift!('K')] => commands::extend_line_up,
                vec![shift!('L')] => commands::extend_char_right,
                vec![key!('w')] => commands::move_next_word_start,
                vec![key!('b')] => commands::move_prev_word_start,
                vec![key!('e')] => commands::move_next_word_end,
                vec![key!('g')] => commands::goto_mode,
                vec![key!('i')] => commands::insert_mode,
                vec![shift!('I')] => commands::prepend_to_line,
                vec![key!('a')] => commands::append_mode,
                vec![shift!('A')] => commands::append_to_line,
                vec![key!('o')] => commands::open_below,
                vec![key!('d')] => commands::delete_selection,
                vec![key!('c')] => commands::change_selection,
                vec![key!('s')] => commands::split_selection_on_newline,
                vec![key!(';')] => commands::collapse_selection,
                // TODO should be alt(;)
                vec![key!('%')] => commands::flip_selections,
                vec![key!('x')] => commands::select_line,
                vec![key!('u')] => commands::undo,
                vec![shift!('U')] => commands::redo,
                vec![key!('y')] => commands::yank,
                vec![key!('p')] => commands::paste,
                vec![key!('>')] => commands::indent,
                vec![key!('<')] => commands::unindent,
                vec![key!(':')] => commands::command_mode,
                vec![Key {
                    code: KeyCode::Esc,
                    modifiers: Modifiers::NONE
                }] => commands::normal_mode,
                vec![Key {
                    code: KeyCode::PageUp,
                    modifiers: Modifiers::NONE
                }] => commands::page_up,
                vec![Key {
                    code: KeyCode::PageDown,
                    modifiers: Modifiers::NONE
                }] => commands::page_down,
                vec![ctrl!('u')] => commands::half_page_up,
                vec![ctrl!('d')] => commands::half_page_down,
            ),
            state::Mode::Insert => hashmap!(
                vec![Key {
                    code: KeyCode::Esc,
                    modifiers: Modifiers::NONE
                }] => commands::normal_mode as Command,
                vec![Key {
                    code: KeyCode::Backspace,
                    modifiers: Modifiers::NONE
                }] => commands::insert::delete_char_backward,
                vec![Key {
                    code: KeyCode::Delete,
                    modifiers: Modifiers::NONE
                }] => commands::insert::delete_char_forward,
                vec![Key {
                    code: KeyCode::Enter,
                    modifiers: Modifiers::NONE
                }] => commands::insert::insert_newline,
                vec![Key {
                    code: KeyCode::Tab,
                    modifiers: Modifiers::NONE
                }] => commands::insert::insert_tab,
            ),
            state::Mode::Goto => hashmap!(
                vec![Key {
                    code: KeyCode::Esc,
                    modifiers: Modifiers::NONE
                }] => commands::normal_mode as Command,
                vec![key!('g')] => commands::move_file_start as Command,
                vec![key!('e')] => commands::move_file_end as Command,
            ),
    )
}