-
Notifications
You must be signed in to change notification settings - Fork 3
/
modes.go
339 lines (313 loc) · 8.88 KB
/
modes.go
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
package main
import (
"fmt"
"path/filepath"
"strings"
"github.com/gdamore/tcell"
)
type CommandFn func(*ViewTree, *Buffer, *KeyList)
type ModeBinding struct {
k *KeyList
f CommandFn
}
type Mode struct {
name string
bindings []*ModeBinding
}
var modes = map[string]*Mode{}
func modeHandle(m *Mode, kl *KeyList) *KeyList {
var match *KeyList = nil
var matchBinding *ModeBinding = nil
for _, binding := range m.bindings {
if matched := kl.HasSuffix(binding.k); matched != nil {
if match == nil || len(matched.keys) > len(match.keys) {
matchBinding = binding
match = matched
}
}
}
if match != nil {
matchBinding.f(currentViewTree, currentViewTree.Leaf.Buf, match)
return match
}
return nil
}
func findMode(name string) *Mode {
if m, ok := modes[name]; ok {
return m
} else {
return nil
}
}
func mustFindMode(name string) *Mode {
mode := findMode(name)
if mode == nil {
panic(fmt.Sprintf("no mode named '%s'", name))
}
return mode
}
// Adds a new empty mode to the mode list, if not already present
func addMode(name string) {
if _, ok := modes[name]; !ok {
modes[name] = &Mode{name: name, bindings: []*ModeBinding{}}
}
}
func bind(mode_name string, k *KeyList, f CommandFn) {
mode := mustFindMode(mode_name)
// If this key is bound, update bound function
for _, binding := range mode.bindings {
if k.String() == binding.k.String() {
binding.f = f
}
}
// Else, it's a new binding, add it
mode.bindings = append(mode.bindings, &ModeBinding{k: k, f: f})
}
func initModes() {
addMode("normal")
bind("normal", k("m $alpha"), commandMark)
bind("normal", k("' $alpha"), commandMoveToMark)
bind("normal", k(":"), promptCommand)
bind("normal", k("h"), moveLeft)
bind("normal", k("j"), moveDown)
bind("normal", k("k"), moveUp)
bind("normal", k("l"), moveRight)
bind("normal", k("0"), moveLineBeg)
bind("normal", k("$"), moveLineEnd)
bind("normal", k("g g"), moveTop)
bind("normal", k("G"), moveBottom)
bind("normal", k("C-u"), moveJumpUp)
bind("normal", k("C-d"), moveJumpDown)
bind("normal", k("z z"), moveCenterLine)
bind("normal", k("w"), moveWordForward)
bind("normal", k("e"), moveWordEndForward)
bind("normal", k("b"), moveWordBackward)
bind("normal", k("C-c"), cancelKeysEntered)
bind("normal", k("C-g"), cancelKeysEntered)
bind("normal", k("ESC ESC"), cancelKeysEntered)
bind("normal", k("i"), enterInsertMode)
bind("normal", k("a"), enterInsertModeAppend)
bind("normal", k("A"), enterInsertModeEol)
bind("normal", k("o"), enterInsertModeNl)
bind("normal", k("O"), enterInsertModeNlUp)
bind("normal", k("x"), removeChar)
bind("normal", k("d d"), removeLine)
bind("normal", k("y y"), commandCopyLine)
bind("normal", k("p"), commandPaste)
bind("normal", k("u"), commandUndo)
bind("normal", k("C-r"), commandRedo)
bind("normal", k("v"), enterVisualMode)
bind("normal", k("V"), enterVisualBlockMode)
addMode("insert")
bind("insert", k("ESC"), enterNormalMode)
bind("insert", k("C-c"), enterNormalMode)
bind("insert", k("RET"), insertEnter)
bind("insert", k("BAK"), insertBackspace)
bind("insert", k("$any"), insert)
addMode("prompt")
bind("prompt", k("C-c"), promptCancel)
bind("prompt", k("C-g"), promptCancel)
bind("prompt", k("ESC"), promptCancel)
bind("prompt", k("RET"), promptFinish)
bind("prompt", k("BAK"), promptBackspace)
bind("prompt", k("$any"), promptInsert)
addMode("buffers")
bind("buffers", k("q"), func(vt *ViewTree, b *Buffer, kl *KeyList) {
closeCurrentBuffer(true)
})
bind("buffers", k("RET"), func(vt *ViewTree, b *Buffer, kl *KeyList) {
closeCurrentBuffer(true)
showBuffer(string(b.Data[b.Cursor.Line]))
})
addMode("directory")
bind("directory", k("q"), func(vt *ViewTree, b *Buffer, kl *KeyList) {
closeCurrentBuffer(true)
})
bind("directory", k("RET"), func(vt *ViewTree, b *Buffer, kl *KeyList) {
closeBuffer(currentViewTree.Leaf.Buf)
selectAvailableBuffer(false)
file_path := filepath.Join(b.Path, string(b.Data[b.Cursor.Line]))
runCommand([]string{"edit", file_path})
})
// TODO Remove once we have user configurable bindings
bind("normal", k("SPC b"), func(vt *ViewTree, b *Buffer, kl *KeyList) {
runCommand([]string{"buffers"})
})
editCurrentFolder := func(vt *ViewTree, b *Buffer, kl *KeyList) {
if b.Path == "" {
runCommand([]string{"edit", "."})
} else {
runCommand([]string{"edit", filepath.Dir(b.Path)})
}
}
bind("normal", k("SPC f"), editCurrentFolder)
bind("normal", k("-"), editCurrentFolder)
bind("normal", k("SPC n"), func(vt *ViewTree, b *Buffer, kl *KeyList) {
runCommand([]string{"clearsearch"})
})
}
func moveLeft(vt *ViewTree, b *Buffer, kl *KeyList) {
b.Move(-1, 0)
}
func moveRight(vt *ViewTree, b *Buffer, kl *KeyList) {
b.Move(1, 0)
}
func moveUp(vt *ViewTree, b *Buffer, kl *KeyList) {
b.Move(0, -1)
}
func moveDown(vt *ViewTree, b *Buffer, kl *KeyList) {
b.Move(0, 1)
}
func moveLineBeg(vt *ViewTree, b *Buffer, kl *KeyList) {
b.MoveTo(0, b.Cursor.Line)
}
func moveLineEnd(vt *ViewTree, b *Buffer, kl *KeyList) {
b.MoveTo(len(b.Data[b.Cursor.Line]), b.Cursor.Line)
}
func moveTop(vt *ViewTree, b *Buffer, kl *KeyList) {
b.MoveTo(0, 0)
}
func moveBottom(vt *ViewTree, b *Buffer, kl *KeyList) {
b.MoveTo(0, len(b.Data)-1)
}
func moveJumpUp(vt *ViewTree, b *Buffer, kl *KeyList) {
b.Move(0, -15)
}
func moveJumpDown(vt *ViewTree, b *Buffer, kl *KeyList) {
b.Move(0, 15)
}
func moveCenterLine(vt *ViewTree, b *Buffer, kl *KeyList) {
vt.Leaf.CenterPending = true
}
func moveWordBackward(vt *ViewTree, b *Buffer, kl *KeyList) {
b.MoveWordBackward()
}
func moveWordForward(vt *ViewTree, b *Buffer, kl *KeyList) {
b.MoveWordForward()
}
func moveWordEndForward(vt *ViewTree, b *Buffer, kl *KeyList) {
b.MoveWordEndForward()
}
func cancelKeysEntered(vt *ViewTree, b *Buffer, kl *KeyList) {
keysEntered = k("")
}
// Enter in a new mode
func enterMode(mode string) {
editorMode = mode
// TODO maybe not the best place to clear this
message("")
}
func enterNormalMode(vt *ViewTree, b *Buffer, kl *KeyList) {
moveLeft(vt, b, kl)
enterMode("normal")
}
func enterInsertMode(vt *ViewTree, b *Buffer, kl *KeyList) {
enterMode("insert")
}
func enterInsertModeAppend(vt *ViewTree, b *Buffer, kl *KeyList) {
moveRight(vt, b, kl)
enterMode("insert")
}
func enterInsertModeEol(vt *ViewTree, b *Buffer, kl *KeyList) {
moveLineEnd(vt, b, kl)
enterMode("insert")
}
func enterInsertModeNl(vt *ViewTree, b *Buffer, kl *KeyList) {
moveLineEnd(vt, b, kl)
b.Insert([]rune("\n"))
b.Move(0, 1) // ensure a valid position
enterMode("insert")
}
func enterInsertModeNlUp(vt *ViewTree, b *Buffer, kl *KeyList) {
moveLineBeg(vt, b, kl)
b.Insert([]rune("\n"))
b.Move(0, 0) // ensure a valid position
enterMode("insert")
}
func insertEnter(vt *ViewTree, b *Buffer, kl *KeyList) {
i := 0
for ; i < len(b.Data[b.Cursor.Line]) && isSpace(b.Data[b.Cursor.Line][i]); i++ {
}
b.Insert([]rune("\n" + strings.Repeat(" ", i)))
b.MoveTo(i, b.Cursor.Line+1)
}
func insertBackspace(vt *ViewTree, b *Buffer, kl *KeyList) {
if b.Cursor.Char == 0 {
if b.Cursor.Line != 0 {
moveUp(vt, b, kl)
moveLineEnd(vt, b, kl)
b.Remove(1)
}
} else {
if b.CharAtLeft() != ' ' {
b.Move(-1, 0)
b.Remove(1)
return
}
// handle spaces
delete_n := 1
if configGetBool("tab_to_spaces", b) {
delete_n = int(configGetNumber("tab_width", b))
}
for i := 0; i < delete_n && b.CharAtLeft() == ' '; i++ {
b.Move(-1, 0)
b.Remove(1)
}
}
}
func insert(vt *ViewTree, b *Buffer, kl *KeyList) {
k := kl.keys[len(kl.keys)-1]
if k.Key == tcell.KeyTab {
if configGetBool("tab_to_spaces", b) {
tabWidth := int(configGetNumber("tab_width", b))
b.Insert([]rune(strings.Repeat(" ", tabWidth)))
b.Move(tabWidth, 0)
} else {
b.Insert([]rune{'\t'})
b.Move(1, 0)
}
} else if k.Key == tcell.KeyRune && k.Mod == 0 {
b.Insert([]rune{k.Chr})
moveRight(vt, b, kl)
} else {
message("Can't insert '" + kl.String() + "'")
}
}
func removeChar(vt *ViewTree, b *Buffer, kl *KeyList) {
removed := b.Remove(1)
clipboardSet(defaultClipboard, removed)
}
func removeLine(vt *ViewTree, b *Buffer, kl *KeyList) {
moveLineBeg(vt, b, kl)
removed := b.Remove(len(b.Data[b.Cursor.Line]) + 1)
clipboardSet(defaultClipboard, removed)
}
func commandUndo(vt *ViewTree, b *Buffer, kl *KeyList) {
b.Undo()
}
func commandRedo(vt *ViewTree, b *Buffer, kl *KeyList) {
b.Redo()
}
func commandCopyLine(vt *ViewTree, b *Buffer, kl *KeyList) {
value := make([]rune, len(b.Data[b.Cursor.Line])+1)
copy(value, b.Data[b.Cursor.Line])
value[len(value)-1] = '\n'
clipboardSet(defaultClipboard, value)
}
func commandPaste(vt *ViewTree, b *Buffer, kl *KeyList) {
value := clipboardGet(defaultClipboard)
if len(value) == 0 {
message("Nothing to paste!")
return
}
b.Move(1, 0)
b.Insert(value)
}
func commandMark(vt *ViewTree, b *Buffer, kl *KeyList) {
mark_letter := kl.keys[len(kl.keys)-1].Chr
markCreate(mark_letter, b)
}
func commandMoveToMark(vt *ViewTree, b *Buffer, kl *KeyList) {
mark_letter := kl.keys[len(kl.keys)-1].Chr
markJump(mark_letter)
}