-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathorganizer_process_key.go
More file actions
448 lines (414 loc) · 11.5 KB
/
organizer_process_key.go
File metadata and controls
448 lines (414 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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
package main
import (
"strconv"
"strings"
"github.com/slzatz/vimango/vim"
)
func (o *Organizer) organizerProcessKey(c int) (redraw RedrawScope) {
//o.ShowMessage(BR, "organizerProcessKey: %d -- mode %s", c, o.mode) //debug
redraw = RedrawFull
// Handle global escape key
if c == '\x1b' {
o.showMessage("")
o.command = ""
vim.SendKey("<esc>")
//o.last_mode = o.mode // not sure this is necessary
o.mode = NORMAL
o.altRowoff = 0
o.tabCompletion.index = 0
o.tabCompletion.list = nil
o.Session.imagePreview = false
if o.view == TASK {
o.displayNote()
} else {
o.Screen.eraseRightScreen()
o.displayContainerInfo()
}
return
}
switch o.mode {
case INSERT:
redraw = o.InsertModeKeyHandler(c)
case NORMAL, NORMAL_BUSY:
redraw = o.NormalModeKeyHandler(c)
case VISUAL:
o.VisualModeKeyHandler(c)
redraw = RedrawPartial
case COMMAND_LINE:
redraw = o.ExModeKeyHandler(c)
case NAVIGATE_NOTICE, HELP:
redraw = o.NavigateNoticeModeKeyHandler(c)
case CONTAINER:
redraw = o.NavigateContainerModeKeyHandler(c)
default:
return
}
return
}
func sendToVim(c int) {
if z, found := termcodes[c]; found {
vim.SendKey(z)
} else {
vim.SendInput(string(c))
}
}
func (o *Organizer) updateRowStatus() {
row := &o.rows[o.fr]
tick := o.vbuf.GetLastChangedTick()
if tick > o.bufferTick {
row.dirty = true
o.bufferTick = tick
}
}
func (o *Organizer) updateRowStatus2() bool {
row := &o.rows[o.fr]
tick := o.vbuf.GetLastChangedTick()
if tick > o.bufferTick {
row.dirty = true
o.bufferTick = tick
return true
}
return false
}
func (o *Organizer) InsertModeKeyHandler(c int) (redraw RedrawScope) {
redraw = RedrawPartial
if c == '\r' {
o.writeTitle() // now updates ftsTitle if taskview == BY_FIND
vim.SendKey("<esc>")
o.mode = NORMAL
row := &o.rows[o.fr]
row.dirty = false
o.bufferTick = o.vbuf.GetLastChangedTick()
o.command = ""
return RedrawPartial // was RedrawFull
}
sendToVim(c)
s := o.vbuf.Lines()[o.fr]
o.rows[o.fr].title = s
pos := vim.GetCursorPosition()
o.fc = pos[1]
// need to prevent row from changing in INSERT mode; for instance, an when an up or down arrow is pressed
if o.fr != pos[0]-1 {
vim.SetCursorPosition(o.fr+1, o.fc)
}
o.updateRowStatus()
return
}
func (o *Organizer) NormalModeKeyHandler(c int) (redraw RedrawScope) {
//o.ShowMessage(BR, "key pressed: %d", c) ///debug
redraw = RedrawNone
if c == '\r' {
o.command = ""
row := &o.rows[o.fr]
if row.dirty {
o.writeTitle() // now updates ftsTitle if taskview == BY_FIND
vim.SendKey("<esc>")
row.dirty = false
o.bufferTick = o.vbuf.GetLastChangedTick()
redraw = RedrawPartial
return
}
}
// commands are letters or control characters
if _, err := strconv.Atoi(string(c)); err != nil {
o.command += string(c) //note currently all are single characters although future could use leader+chars
if cmd, found := o.normalCmds[o.command]; found {
//o.ShowMessage(BR, "key pressed: %T", cmd) ///debug
cmd(o)
switch o.command {
case string(ctrlKey('a')), string(ctrlKey('d')), string(ctrlKey('x')), "m":
redraw = RedrawPartial
default:
redraw = RedrawNone
}
o.command = ""
vim.SendKey("<esc>")
return
}
}
// in NORMAL mode don't want ? leader, O, o, V, ctrl-V, J being passed to vim
switch c {
case 'J', 'V', ctrlKey('v'), 'o', 'O':
o.showMessage("Ascii %d has no effect in Organizer NORMAL mode", c)
return
}
// anything sent to vim should only require the active screen line to be redrawn
// however if the row changes we need to erase the > from the previous row
sendToVim(c)
if o.updateRowStatus2() {
redraw = RedrawPartial
} else {
redraw = RedrawNone
}
mode := vim.GetCurrentMode()
if mode == 4 { // OP_PENDING like 4da
return
}
if mode == 8 { // COMMAND or SEARCH
o.ShowMessage(BL, ":")
vim.SendKey("<esc>") // park in NORMAL mode
o.command_line = ""
//o.last_mode = o.mode //Should probably be NORMAL
o.mode = COMMAND_LINE
o.tabCompletion.index = 0
o.tabCompletion.list = nil
return
}
prevRow := o.fr
pos := vim.GetCursorPosition()
o.fc = pos[1]
newRow := pos[0] - 1
// if move to a new row then draw task note preview or container info
// and set cursor back to beginning of line and erase > from previous row
if newRow != prevRow {
o.fr = newRow
o.fc = 0
vim.SetCursorPosition(o.fr+1, 0)
o.altRowoff = 0 // reset scroll in right window
// need to erase > from previous row
o.erasePreviousRowMarker(prevRow)
if o.view == TASK {
o.displayNote()
} else {
o.displayContainerInfo()
}
}
s := o.vbuf.Lines()[o.fr]
o.rows[o.fr].title = s
//firstLine := vim.WindowGetTopLine() // doesn't seem to work
//o.updateRowStatus()
o.command = ""
if mode == 16 && o.mode != INSERT {
o.showMessage("\x1b[1m-- INSERT --\x1b[0m")
}
o.mode = modeMap[mode] //note that 8 => SEARCH (8 is also COMMAND)
if o.mode == VISUAL {
pos := vim.GetVisualRange()
o.highlight[1] = pos[1][1] + 1
o.highlight[0] = pos[0][1]
redraw = RedrawPartial
}
o.ShowMessage(BL, "%s", s)
//could put this redraw inside if o.mode == VISUAL and then check GetLastChangedTick
//redraw = RedrawPartial
return
}
func (o *Organizer) VisualModeKeyHandler(c int) {
/*
if c == 'J' || c == 'V' || c == ctrlKey('v') || c == ':' {
o.showMessage("Ascii %d has no effect in Organizer VISUAL mode", c)
return
}
*/
// in VISUAL mode don't want J, V, ctrl-V, : being passed to vim
// in vim : triggers visual selection range :'<,'>
switch c {
case 'J', 'V', ctrlKey('v'), ':':
o.showMessage("Ascii %d has no effect in Organizer VISUAL mode", c)
return
}
sendToVim(c)
o.bufferTick = o.vbuf.GetLastChangedTick()
s := o.vbuf.Lines()[o.fr]
o.rows[o.fr].title = s
pos := vim.GetCursorPosition()
o.fc = pos[1]
// need to prevent row from changing in VISUAL mode; this catches j,k,g,G and arrow keys
if o.fr != pos[0]-1 {
vim.SetCursorPosition(o.fr+1, o.fc)
}
o.updateRowStatus()
mode := vim.GetCurrentMode() // I think just a few possibilities - stay in VISUAL or something like 'x' switches to NORMAL and : to command
o.mode = modeMap[mode] //note that 8 => SEARCH (8 is also COMMAND)
o.command = ""
visPos := vim.GetVisualRange()
o.highlight[1] = visPos[1][1] + 1
o.highlight[0] = visPos[0][1]
o.showMessage("visual %s; %d %d", s, o.highlight[0], o.highlight[1])
}
func (o *Organizer) ExModeKeyHandler(c int) (redraw RedrawScope) {
redraw = RedrawNone
switch c {
case '\r':
var cmd func(*Organizer, int)
var found bool
var s string
o.command = "" ////////there must be a better place!
pos := strings.LastIndexByte(o.command_line, ' ')
if pos == -1 {
//s = o.command_line
// allow for commands with no arguments to have a variant ending with !
s = strings.TrimSuffix(o.command_line, "!")
} else {
s = o.command_line[:pos]
}
if cmd, found = o.exCmds[s]; found {
cmd(o, pos)
redraw = RedrawFull
//o.mode = NORMAl - could be NAVIGATE_RENDER
return
}
// to catch find with more than one find term
if !found && pos != -1 && strings.Count(o.command_line, " ") > 1 {
pos := strings.IndexByte(o.command_line, ' ')
if cmd, found = o.exCmds[o.command_line[:pos]]; found {
// pass the position of the first space
cmd(o, pos)
redraw = RedrawFull
o.mode = NORMAL
return
}
}
o.tabCompletion.index = 0
o.tabCompletion.list = nil
// Try to provide helpful suggestions using command registry
if o.commandRegistry != nil {
suggestions := o.commandRegistry.SuggestCommand(s)
if len(suggestions) > 0 {
o.showMessage("%sCommand '%s' not found. Did you mean: %s?%s", RED_BG, s, strings.Join(suggestions, ", "), RESET)
} else {
o.showMessage("%sCommand '%s' not found. Use ':help' to see available commands.%s", RED_BG, s, RESET)
}
} else {
// Fallback if registry not available
o.showMessage("%sNot a recognized command: %s%s", RED_BG, s, RESET)
}
//o.mode = o.last_mode
o.mode = NORMAL
return
case '\t':
pos := strings.LastIndex(o.command_line, " ")
if pos == -1 {
return
}
if o.tabCompletion.list != nil {
o.tabCompletion.index++
if o.tabCompletion.index > len(o.tabCompletion.list)-1 {
o.tabCompletion.index = 0
}
} else {
o.ShowMessage(BR, "tab")
o.tabCompletion.index = 0
option := o.command_line[pos+1:]
/* NOTE: there may be some commands we do want to exclude from tab completion
cmd := o.command_line[:pos]
if !(cmd == "o" || cmd == "cd" ) {
return
}
*/
for _, k := range o.filterList {
if strings.HasPrefix(k.Text, option) {
o.tabCompletion.list = append(o.tabCompletion.list, FilterNames{Text: k.Text, Char: k.Char})
}
}
if len(o.tabCompletion.list) == 0 {
return
}
//sort.Strings(tabCompletion.list)
}
o.command_line = o.command_line[:pos+1] + o.tabCompletion.list[o.tabCompletion.index].Text
//o.showMessage(":%s (%c)", o.command_line, o.tabCompletion.list[o.tabCompletion.index].Char)
o.showMessage(":%s", o.command_line)
return
case DEL_KEY, BACKSPACE:
length := len(o.command_line)
if length > 0 {
o.command_line = o.command_line[:length-1]
}
default:
o.command_line += string(c)
} // end switch c in COMMAND_LINE
o.tabCompletion.index = 0
o.tabCompletion.list = nil
o.showMessage(":%s", o.command_line)
return
}
// Used for viewing sync log and help
func (o *Organizer) NavigateNoticeModeKeyHandler(c int) RedrawScope {
o.ShowMessage(BL, "NavigateRender mode")
switch c {
case ctrlKey('j'), PAGE_DOWN:
o.scrollNoticeDown()
case ctrlKey('k'), PAGE_UP:
o.scrollNoticeUp()
case HOME_KEY:
o.scrollNoticeHome()
case ':': // COMMAND or SEARCH
o.ShowMessage(BL, ":")
vim.SendKey("<esc>") // park in NORMAL mode
o.command_line = ""
//o.last_mode = o.mode //Should probably be NORMAL
o.mode = COMMAND_LINE
o.tabCompletion.index = 0
o.tabCompletion.list = nil
}
return RedrawNone
}
func (o *Organizer) NavigateContainerModeKeyHandler(c int) RedrawScope {
if _, err := strconv.Atoi(string(c)); err == nil {
o.command += string(c)
}
o.ShowMessage(BR, "%s", o.command) //debug
if c == '\r' {
index, _ := strconv.Atoi(o.command)
index-- // to convert to 0 based
if o.altView == CONTEXT {
o.setContextNew(o.containerList[index])
}
if o.altView == FOLDER {
o.setFolderNew(o.containerList[index])
}
o.command = ""
vim.SendKey("<esc>")
o.mode = NORMAL
o.altRowoff = 0
o.displayNote()
return RedrawFull // needed to redraw status line
}
switch c {
case ctrlKey('j'), PAGE_DOWN:
o.scrollNoticeDown()
case ctrlKey('k'), PAGE_UP:
o.scrollNoticeUp()
case HOME_KEY:
o.scrollNoticeHome()
case ':': // COMMAND or SEARCH
o.ShowMessage(BL, ":")
vim.SendKey("<esc>") // park in NORMAL mode
o.command_line = ""
//o.last_mode = o.mode //Should probably be NORMAL
o.mode = COMMAND_LINE
o.tabCompletion.index = 0
o.tabCompletion.list = nil
}
return RedrawNone
}
func (o *Organizer) NavigateContainerModeKeyHandler_(c int) RedrawScope {
if c >= '0' && c <= '9' {
index := int(c-'0') - 1
if o.altView == CONTEXT {
o.setContextNew(o.containerList[index])
}
o.mode = NORMAL
o.view = TASK
o.ShowMessage(BL, "NavigateContainer mode: %s %s", string(c), o.containerList[index])
return RedrawNone
}
switch c {
case ctrlKey('j'), PAGE_DOWN:
o.scrollNoticeDown()
case ctrlKey('k'), PAGE_UP:
o.scrollNoticeUp()
case HOME_KEY:
o.scrollNoticeHome()
case ':': // COMMAND or SEARCH
o.ShowMessage(BL, ":")
vim.SendKey("<esc>") // park in NORMAL mode
o.command_line = ""
//o.last_mode = o.mode //Should probably be NORMAL
o.mode = COMMAND_LINE
o.tabCompletion.index = 0
o.tabCompletion.list = nil
}
return RedrawNone
}