-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheditor_process_key.go
More file actions
417 lines (384 loc) · 11.5 KB
/
editor_process_key.go
File metadata and controls
417 lines (384 loc) · 11.5 KB
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
package main
import (
"io/ioutil"
"os/user"
"path/filepath"
"strings"
"unicode/utf8"
"github.com/slzatz/vimango/vim"
)
// note that bool returned is whether to redraw
func (e *Editor) editorProcessKey(c int) (redraw bool) {
//No matter what mode you are in an escape puts you in NORMAL mode
if c == '\x1b' {
vim.SendKey("<esc>")
e.ss = e.vbuf.Lines() /// for commands like 4i
//e.mode = NORMAL_BUSY
prevMode := e.mode
mode := vim.GetCurrentMode() ////////
e.mode = modeMap[mode]
e.ShowMessage(BL, "vim mode: %d | e.mode: %s", mode, e.mode) //////Debug
e.command = ""
e.command_line = ""
pos := vim.GetCursorPosition() //set screen cx and cy from pos
e.fr = pos[0] - 1
e.fc = utf8.RuneCountInString(e.ss[e.fr][:pos[1]])
e.ShowMessage(BR, "")
e.ShowMessage(BR, "%s", prevMode)
//return false
// INSERT is below because escaping from INSERT needs a redraw if previously in VISUAL BLOCK mode and an s, c or I was typed
if prevMode == VISUAL || prevMode == PREVIEW || prevMode == HELP || prevMode == INSERT { //need to redraw to remove highlight or if leaving preview
//app.Organizer.refreshScreen()
return true
} else {
return false
}
}
// if exit is false, doesn't matter if redraw is false since redraw will be determined
// by the mode processing below
// skip := false
var exit bool
// the switch is basically pre-processing before sending key to vim
// Below don't handle NORMAL_BUSY because just want that to drop through
switch e.mode {
case NORMAL, NORMAL_BUSY: //NORMAL only occurs after INSERT is escaped
redraw, exit = e.NormalModeKeyHandler(c)
case VISUAL:
redraw, exit = e.VisualModeKeyHandler(c)
case EX_COMMAND:
redraw, exit = e.ExModeKeyHandler(c)
case SEARCH:
redraw, exit = e.SearchModeKeyHandler(c)
case PREVIEW, HELP: // RUN:
redraw, exit = e.PreviewModeKeyHandler(c)
case VIEW_LOG:
redraw, exit = e.ViewLogModeKeyHandler(c)
}
// if exit true, don't process key any further
if exit {
return
}
// Process the key
if z, found := termcodes[c]; found {
vim.SendKey(z)
} else {
vim.SendInput(string(c))
}
tick := e.vbuf.GetLastChangedTick()
if tick > e.bufferTick {
e.bufferTick = tick
redraw = true
} else {
redraw = false
}
mode := vim.GetCurrentMode()
//submode := vim.GetSubMode() added this 9-22-25 but really only for obscure submodes
switch mode {
//case 1: //NORMAL
// e.mode = NORMAL
case 4: //OP_PENDING delete, change, yank, etc
e.mode = PENDING
//e.ShowMessage(BL, "vim mode: %d | e.mode: %s | char: %q", mode, e.mode, rune(c)) //////Debug
return false
case 8: //SEARCH and EX_COMMAND
// Note: will not hit this case if we are in e.mode == Ex_COMMAND because we
// park vim in NORMAL mode and don't feed it keys
// note that if e.mode has been set to SEARCH, this code does nothing
if e.mode != SEARCH {
e.command_line = ""
e.command = ""
if c == ':' {
e.mode = EX_COMMAND
vim.SendKey("<esc>") // park in NORMAL mode
e.ShowMessage(BR, ":")
} else {
e.mode = SEARCH
e.searchPrefix = string(c)
e.ShowMessage(BR, e.searchPrefix)
}
//e.ShowMessage(BL, "vim mode: %d | e.mode: %s | char: %q", mode, e.mode, rune(c)) //////Debug
return false
}
case 16: //INSERT
if e.mode != INSERT {
e.mode = INSERT
e.ShowMessage(BR, "\x1b[1m-- INSERT --\x1b[0m")
}
//redraw = true
case 2: //VISUAL_MODE
vmode := vim.GetVisualType()
e.vmode = visualModeMap[vmode]
e.mode = VISUAL
//e.ShowMessage(BR, "Current visualType from vim: %d, visual test: %v", vmode, mode == 118)
e.highlightInfo()
//e.ShowMessage(BL, "vim mode: %d | vmode: %s | e.mode: %s", mode, e.vmode, e.mode) //////Debug
redraw = true
//case 257: //NORMAL_BUSY
// e.mode = NORMAL_BUSY
}
//default:
if m, ok := modeMap[mode]; ok { //note that 8 => SEARCH (8 is also COMMAND)
e.mode = m
} else {
e.mode = OTHER // not sure this ever happens
}
//} // was end of switch mode
//e.ShowMessage(BL, "vim mode: %d | e.mode: %s | char: %q", mode, e.mode, rune(c)) //////Debug
//below is done for everything except SEARCH, EX_COMMAND and OP_PENDING
e.ss = e.vbuf.Lines()
// Add safety checks to prevent panic with empty buffers
if len(e.ss) == 0 {
e.ss = []string{""}
}
pos := vim.GetCursorPosition() //set screen cx and cy from pos
e.fr = pos[0] - 1
// Ensure fr is in bounds
if e.fr < 0 {
e.fr = 0
}
if e.fr >= len(e.ss) {
e.fr = len(e.ss) - 1
}
// Ensure pos[1] (column) is valid
if pos[1] > len(e.ss[e.fr]) {
pos[1] = len(e.ss[e.fr])
}
e.fc = utf8.RuneCountInString(e.ss[e.fr][:pos[1]])
return
}
// case PREVIEW:
func (e *Editor) PreviewModeKeyHandler(c int) (redraw, exit bool) {
switch c {
case PAGE_DOWN, ctrlKey('j'):
app.Organizer.scrollNoticeDown()
case PAGE_UP, ctrlKey('k'):
app.Organizer.scrollNoticeUp()
case HOME_KEY:
app.Organizer.scrollNoticeHome()
case ':': // COMMAND or SEARCH
e.ShowMessage(BR, ":")
vim.SendKey("<esc>") // park in NORMAL mode
e.command_line = ""
e.mode = EX_COMMAND
e.tabCompletion.index = 0
e.tabCompletion.list = nil
}
return false, true
}
// case VIEW_LOG:
func (e *Editor) ViewLogModeKeyHandler(c int) (redraw, skip bool) {
switch c {
case PAGE_DOWN, ARROW_DOWN, 'j':
e.previewLineOffset++
e.drawOverlay()
return false, true
case PAGE_UP, ARROW_UP, 'k':
if e.previewLineOffset > 0 {
e.previewLineOffset--
e.drawOverlay()
redraw = false
skip = true
return false, true
}
}
if c == DEL_KEY || c == BACKSPACE {
if len(e.command_line) > 0 {
e.command_line = e.command_line[:len(e.command_line)-1]
}
} else {
e.command_line += string(c)
}
return false, true
}
// case NORMAL, OTHER (257):
// note that Crtl-A and Ctrl-X are passed through and perform their usual weird function of incrementing and decrementing the number under the cursor and Ctrl-R also works to undo the last undone change. All other vim built-in Ctrl commands appear to do nothing.
func (e *Editor) NormalModeKeyHandler(c int) (redraw, skip bool) {
//leader := ' ' //vim.GetLeaderKey()
if c == ' ' { //should become if c == leader
e.command = string(c)
return false, true
}
e.command += string(c)
if len(e.command) > 0 {
if e.command[0] != ' ' { //leader
e.command = string(c)
}
}
//e.ShowMessage(BR, "e.command = %q", e.command) //Debug
if cmd, found := e.normalCmds[e.command]; found {
cmd(e, c)
vim.SendKey("<esc>")
// below is kludge becuse moveLeft and moveRight move you out of current editor
if e.command == "\x08" || e.command == "\x0c" { //moveLeft or moveRight
e.command = ""
return true, true
}
e.command = ""
e.ss = e.vbuf.Lines()
pos := vim.GetCursorPosition() //set screen cx and cy from pos
e.fr = pos[0] - 1
e.fc = utf8.RuneCountInString(e.ss[e.fr][:pos[1]])
redraw = true
if e.mode == PREVIEW {
redraw = false
}
return redraw, true
} else {
if e.command[0] == ' ' {
return false, true // don't process key
} else {
return false, false // have vim process key
}
}
}
// case VISUAL:
func (e *Editor) VisualModeKeyHandler(c int) (redraw, skip bool) {
// Special commands in visual mode to do markdown decoration: ctrl-b, e, i
if strings.IndexAny(string(c), "\x02\x05\x09") != -1 { // this should define commmands like normalCmds, ie visualCmds
e.decorateWordVisual(c)
vim.SendKey("<esc>")
e.mode = NORMAL
e.command = ""
e.ss = e.vbuf.Lines()
pos := vim.GetCursorPosition() //set screen cx and cy from pos
e.fr = pos[0] - 1
e.fc = utf8.RuneCountInString(e.ss[e.fr][:pos[1]])
return true, true
}
return false, false
}
// case EX_COMMAND:
func (e *Editor) ExModeKeyHandler(c int) (redraw, skip bool) {
if c == '\r' {
// Index doesn't work for vert resize
// and LastIndex doesn't work for run
// so total kluge below
//if e.command_line[0] == '%'
//if strings.Index(e.command_line, "s/") != -1
// we want libvim to handle the following Ex-Commands:
use_vim := []string{"s/", "%s/", "g/", "g!/", "v/"}
for _, p := range use_vim {
if strings.HasPrefix(e.command_line, p) {
if strings.HasSuffix(e.command_line, "/c") {
e.ShowMessage(BR, "We don't support [c]onfirm")
e.mode = NORMAL
return false, true
}
vim.SendInput(":" + e.command_line + "\r")
e.mode = NORMAL
e.command = ""
e.ss = e.vbuf.Lines()
pos := vim.GetCursorPosition() //set screen cx and cy from pos
e.fr = pos[0] - 1
e.fc = utf8.RuneCountInString(e.ss[e.fr][:pos[1]])
e.ShowMessage(BL, "search and replace: %s", e.command_line)
return true, true
}
}
var pos int
var cmd string
if strings.HasPrefix(e.command_line, "vert") {
pos = strings.LastIndex(e.command_line, " ")
} else {
pos = strings.Index(e.command_line, " ")
}
if pos != -1 {
cmd = e.command_line[:pos]
} else {
cmd = e.command_line
}
//if cmd0, found := e_lookup_C[cmd]; found
if cmd0, found := e.exCmds[cmd]; found {
cmd0(e)
e.command_line = ""
if e.mode != HELP {
e.mode = NORMAL
}
e.tabCompletion.index = 0
e.tabCompletion.list = nil
if cmd == "read" || cmd == "r" {
return true, false
}
return false, true
}
// Try to provide helpful suggestions using command registry
if e.commandRegistry != nil {
suggestions := e.commandRegistry.SuggestCommand(cmd)
if len(suggestions) > 0 {
e.ShowMessage(BR, "\x1b[41mCommand '%s' not found. Did you mean: %s?\x1b[0m", cmd, strings.Join(suggestions, ", "))
} else {
e.ShowMessage(BR, "\x1b[41mCommand '%s' not found. Use ':help' to see available commands.\x1b[0m", cmd)
}
} else {
// Fallback if registry not available
e.ShowMessage(BR, "\x1b[41mNot an editor command: %s\x1b[0m", cmd)
}
e.mode = NORMAL
e.command_line = ""
return false, true
} //end 'r'
if c == '\t' {
pos := strings.Index(e.command_line, " ")
if e.tabCompletion.list == nil {
e.ShowMessage(BL, "tab")
var s string
if pos != -1 {
s = e.command_line[pos+1:]
//cl := p.command_line
dir := filepath.Dir(s)
if dir == "~" {
usr, _ := user.Current()
dir = usr.HomeDir
} else if strings.HasPrefix(dir, "~/") {
usr, _ := user.Current()
dir = filepath.Join(usr.HomeDir, dir[2:])
}
partial := filepath.Base(s)
paths, _ := ioutil.ReadDir(dir)
e.ShowMessage(BL, "dir: %s base: %s", dir, partial)
for _, path := range paths {
if strings.HasPrefix(path.Name(), partial) {
e.tabCompletion.list = append(e.tabCompletion.list, filepath.Join(dir, path.Name()))
}
}
}
if len(e.tabCompletion.list) == 0 {
return false, true
}
} else {
e.tabCompletion.index++
if e.tabCompletion.index > len(e.tabCompletion.list)-1 {
e.tabCompletion.index = 0
}
}
e.command_line = e.command_line[:pos+1] + e.tabCompletion.list[e.tabCompletion.index]
e.ShowMessage(BR, ":%s", e.command_line)
return false, true
}
// process the key typed in COMMAND_LINE mode
if c == DEL_KEY || c == BACKSPACE {
if len(e.command_line) > 0 {
e.command_line = e.command_line[:len(e.command_line)-1]
}
} else {
e.command_line += string(c)
}
e.tabCompletion.index = 0
e.tabCompletion.list = nil
e.ShowMessage(BR, ":%s", e.command_line)
return false, true
}
// case SEARCH:
func (e *Editor) SearchModeKeyHandler(c int) (bool, bool) {
// Below is purely for displaying command line correctly
// vim is actually handling the keystrokes whether we do this or not
if c == DEL_KEY || c == BACKSPACE {
if len(e.command_line) > 0 {
e.command_line = e.command_line[:len(e.command_line)-1]
}
} else {
e.command_line += string(c)
}
e.ShowMessage(BR, "%s%s", e.searchPrefix, e.command_line)
return false, false
}