-
Notifications
You must be signed in to change notification settings - Fork 0
/
organizer_process_key.go
599 lines (540 loc) · 14.2 KB
/
organizer_process_key.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
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
package main
import (
"fmt"
"os/exec"
"sort"
"strconv"
"strings"
"github.com/slzatz/vimango/vim"
)
var tabCompletion struct {
idx int
list []string
}
func organizerProcessKey(c int) {
if c == '\x1b' {
sess.showOrgMessage("")
org.command = ""
vim.Key("<esc>")
org.last_mode = org.mode // not sure this is necessary
org.mode = NORMAL
pos := vim.CursorGetPosition()
org.fc = pos[1]
org.fr = pos[0] - 1
//org.fc = utf8.RuneCount(p.ss[org.fr][:pos[1]])
tabCompletion.idx = 0
tabCompletion.list = nil
sess.imagePreview = false
if org.view == TASK {
org.drawPreview()
}
return
}
/* not sure this is necessary
if c < 32 && !(c == 13 || c == ctrlKey('j') || c == ctrlKey('k') || c == ctrlKey('x') || c == ctrlKey('d')) {
return
}
*/
switch org.mode {
case INSERT:
if c == '\r' {
org.writeTitle()
vim.Key("<esc>")
org.mode = NORMAL
row := &org.rows[org.fr]
row.dirty = false
org.bufferTick = vim.BufferGetLastChangedTick(org.vbuf)
org.command = ""
sess.showOrgMessage("")
return
}
if z, found := termcodes[c]; found {
vim.Key(z)
} else {
vim.Input(string(c))
}
s := vim.BufferLines(org.vbuf)[org.fr]
org.rows[org.fr].title = s
pos := vim.CursorGetPosition()
org.fc = pos[1]
// need to prevent row from changing in INSERT mode; for instance, an when an up or down arrow is pressed
if org.fr != pos[0]-1 {
vim.CursorSetPosition(org.fr+1, org.fc)
}
row := &org.rows[org.fr]
tick := vim.BufferGetLastChangedTick(org.vbuf)
if tick > org.bufferTick {
row.dirty = true
org.bufferTick = tick
}
case NORMAL:
if c == ctrlKey('l') && org.last_mode == ADD_CHANGE_FILTER {
org.mode = ADD_CHANGE_FILTER
sess.eraseRightScreen()
}
if c == '\r' {
org.command = ""
row := &org.rows[org.fr]
if row.dirty {
org.writeTitle()
vim.Key("<esc>")
row.dirty = false
org.bufferTick = vim.BufferGetLastChangedTick(org.vbuf)
return
}
// if not row.dirty nothing happens in TASK but if in a CONTAINER view open the entries with that container
var tid int
switch org.view {
case TASK:
return
case CONTEXT:
org.taskview = BY_CONTEXT
tid, _ = contextExists(row.title)
case FOLDER:
org.taskview = BY_FOLDER
tid, _ = folderExists(row.title)
case KEYWORD:
org.taskview = BY_KEYWORD
// this guard to see if synced may not be necessary for keyword
tid, _ = keywordExists(row.title)
}
// if it's a new context|folder|keyword we can't filter tasks by it
if tid < 1 {
sess.showOrgMessage("You need to sync before you can use %q", row.title)
return
}
org.filter = row.title
sess.showOrgMessage("'%s' will be opened", org.filter)
org.clearMarkedEntries()
org.view = TASK
org.fc, org.fr, org.rowoff = 0, 0, 0
org.rows = filterEntries(org.taskview, org.filter, org.show_deleted, org.sort, org.sortPriority, MAX)
if len(org.rows) == 0 {
org.insertRow(0, "", true, false, false, BASE_DATE)
org.rows[0].dirty = false
sess.showOrgMessage("No results were returned")
}
sess.imagePreview = false
org.readRowsIntoBuffer()
vim.CursorSetPosition(1, 0)
org.bufferTick = vim.BufferGetLastChangedTick(org.vbuf)
org.drawPreview()
return
}
if _, err := strconv.Atoi(string(c)); err != nil {
org.command += string(c)
//sess.showEdMessage(org.command)
}
if cmd, found := n_lookup[org.command]; found {
cmd()
org.command = ""
vim.Key("<esc>")
return
}
// in NORMAL mode don't want ' ' (leader), 'O', 'V', 'o' ctrl-V (22)
// being passed to vim
if c == int([]byte(leader)[0]) || c == 'O' || c == 'V' || c == ctrlKey('v') || c == 'o' || c == 'J' {
if c != int([]byte(leader)[0]) {
sess.showOrgMessage("Ascii %d has no effect in Organizer NORMAL mode", c)
}
return
}
// Send the keystroke to vim
if z, found := termcodes[c]; found {
vim.Key(z)
sess.showEdMessage("%s", z)
} else {
vim.Input(string(c))
}
pos := vim.CursorGetPosition()
org.fc = pos[1]
// if move to a new row then draw task note preview or container info
// and set cursor back to beginning of line
if org.fr != pos[0]-1 {
org.fr = pos[0] - 1
org.fc = 0
vim.CursorSetPosition(org.fr+1, 0)
org.altRowoff = 0
if org.view == TASK {
org.drawPreview()
} else {
sess.displayContainerInfo()
}
}
s := vim.BufferLines(org.vbuf)[org.fr]
org.rows[org.fr].title = s
//firstLine := vim.WindowGetTopLine() // doesn't seem to work
row := &org.rows[org.fr]
tick := vim.BufferGetLastChangedTick(org.vbuf)
if tick > org.bufferTick {
row.dirty = true
org.bufferTick = tick
}
mode := vim.GetMode()
// OP_PENDING like 4da
if mode == 4 {
return
}
org.command = ""
// the only way to get into EX_COMMAND or SEARCH
//if mode.Mode == "c" && p.mode != SEARCH //note that "c" => SEARCH
//if mode == 8 && org.mode != SEARCH //note that 8 => SEARCH
if mode == 16 && org.mode != INSERT {
sess.showOrgMessage("\x1b[1m-- INSERT --\x1b[0m")
}
org.mode = modeMap[mode] //note that 8 => SEARCH (8 is also COMMAND)
if org.mode == VISUAL {
pos := vim.VisualGetRange()
org.highlight[1] = pos[1][1] + 1
org.highlight[0] = pos[0][1]
}
sess.showOrgMessage("%s", s)
// end case NORMAL
case VISUAL:
if c == 'j' || c == 'k' || c == 'J' || c == 'V' || c == ctrlKey('v') || c == 'g' || c == 'G' {
sess.showOrgMessage("Ascii %d has no effect in Organizer VISUAL mode", c)
return
}
if z, found := termcodes[c]; found {
vim.Key(z)
} else {
vim.Input(string(c))
}
s := vim.BufferLines(org.vbuf)[org.fr]
org.rows[org.fr].title = s
pos := vim.CursorGetPosition()
org.fc = pos[1]
// need to prevent row from changing in INSERT mode; for instance, an when an up or down arrow is pressed
// note can probably remove j,k,g,G from the above
if org.fr != pos[0]-1 {
vim.CursorSetPosition(org.fr+1, org.fc)
}
row := &org.rows[org.fr]
tick := vim.BufferGetLastChangedTick(org.vbuf)
if tick > org.bufferTick {
row.dirty = true
org.bufferTick = tick
}
mode := vim.GetMode() // I think just a few possibilities - stay in VISUAL or something like 'x' switches to NORMAL and : to command
org.mode = modeMap[mode] //note that 8 => SEARCH (8 is also COMMAND)
org.command = ""
visPos := vim.VisualGetRange()
org.highlight[1] = visPos[1][1] + 1
org.highlight[0] = visPos[0][1]
sess.showOrgMessage("visual %s; %d %d", s, org.highlight[0], org.highlight[1])
// end case VISUAL
case COMMAND_LINE:
switch c {
case '\r':
var cmd func(*Organizer, int)
var found bool
var s string
pos := strings.LastIndex(org.command_line, " ")
if pos == -1 {
s = org.command_line
if cmd, found = cmd_lookup[s]; found {
cmd(&org, pos)
}
} else {
s = org.command_line[:pos]
if cmd, found = cmd_lookup[s]; found {
cmd(&org, pos)
} else {
pos := strings.Index(org.command_line, " ")
s = org.command_line[:pos]
if cmd, found = cmd_lookup[s]; found {
cmd(&org, pos)
}
}
}
tabCompletion.idx = 0
tabCompletion.list = nil
if !found {
sess.showOrgMessage("\x1b[41mNot a recognized command: %s\x1b[0m", s)
sess.showOrgMessage("%sNot a recognized command: %s%s", RED_BG, s, RESET)
org.mode = org.last_mode
}
return
case '\t':
pos := strings.Index(org.command_line, " ")
if pos == -1 {
return
}
if tabCompletion.list != nil {
tabCompletion.idx++
if tabCompletion.idx > len(tabCompletion.list)-1 {
tabCompletion.idx = 0
}
} else {
sess.showEdMessage("tab")
cmd := org.command_line[:pos]
option := org.command_line[pos+1:]
var filterMap = make(map[string]struct{})
switch cmd {
case "o", "oc", "c":
filterMap = contextList()
case "of", "f":
filterMap = folderList()
case "ok", "k":
filterMap = keywordList()
case "sort":
filterMap = sortColumns
default:
return
}
for k, _ := range filterMap {
if strings.HasPrefix(k, option) {
tabCompletion.list = append(tabCompletion.list, k)
}
}
if len(tabCompletion.list) == 0 {
return
}
sort.Strings(tabCompletion.list)
}
org.command_line = org.command_line[:pos+1] + tabCompletion.list[tabCompletion.idx]
sess.showOrgMessage(":%s", org.command_line)
return
case DEL_KEY, BACKSPACE:
length := len(org.command_line)
if length > 0 {
org.command_line = org.command_line[:length-1]
}
default:
org.command_line += string(c)
} // end switch c in COMMAND_LINE
tabCompletion.idx = 0
tabCompletion.list = nil
sess.showOrgMessage(":%s", org.command_line)
//end case COMMAND_LINE
case ADD_CHANGE_FILTER:
switch c {
case ARROW_UP, ARROW_DOWN, 'j', 'k':
org.moveAltCursor(c)
case '\r':
altRow := &org.altRows[org.altFr] //currently highlighted container row
var tid int
row := &org.rows[org.fr] //currently highlighted entry row
switch org.altView {
case KEYWORD:
tid, _ = keywordExists(altRow.title)
case FOLDER:
tid, _ = folderExists(altRow.title)
case CONTEXT:
tid, _ = contextExists(altRow.title)
}
if tid < 1 {
sess.showOrgMessage("%q has not been synched yet - must do that before adding tasks", altRow.title)
return
}
if len(org.marked_entries) == 0 {
switch org.altView {
case KEYWORD:
addTaskKeywordByTid(tid, row.id, true)
sess.showOrgMessage("Added keyword %s to current entry", altRow.title)
case FOLDER:
updateTaskFolderByTid(tid, row.id)
sess.showOrgMessage("Current entry folder changed to %s", altRow.title)
case CONTEXT:
updateTaskContextByTid(tid, row.id)
sess.showOrgMessage("Current entry had context changed to %s", altRow.title)
}
} else {
for id := range org.marked_entries {
switch org.altView {
case KEYWORD:
addTaskKeywordByTid(tid, id, true)
case FOLDER:
updateTaskFolderByTid(tid, id)
case CONTEXT:
updateTaskContextByTid(tid, id)
}
sess.showOrgMessage("Marked entries' %d changed/added to %s", org.altView, altRow.title)
}
}
}
case FIND:
if c == ':' {
exCmd()
return
}
if c >= '0' && c <= '9' {
vim.Input(string(c))
return
}
if _, found := navKeys[c]; found {
if z, found := termcodes[c]; found {
vim.Key(z)
} else {
vim.Input(string(c))
}
pos := vim.CursorGetPosition()
org.fc = pos[1]
if org.fr != pos[0]-1 {
org.fr = pos[0] - 1
org.fc = 0
vim.CursorSetPosition(org.fr+1, 0)
org.drawPreview()
}
} else {
org.mode = NORMAL
org.command = ""
organizerProcessKey(c)
}
//sess.showOrgMessage("Find: fr %d fc %d", org.fr, org.fc)
//probably should be a org.view not org.mode but
// for the moment this kluge works
case SYNC_LOG:
switch c {
case ARROW_UP, 'k':
if len(org.rows) == 0 {
return
}
if org.fr == 0 {
return
}
org.fr--
sess.eraseRightScreen()
org.altRowoff = 0
note := readSyncLog(org.rows[org.fr].id)
org.note = strings.Split(note, "\n")
//note = generateWWString(note, org.totaleditorcols)
org.drawPreviewWithoutImages()
case ARROW_DOWN, 'j':
if len(org.rows) == 0 {
return
}
if org.fr == len(org.rows)-1 {
return
}
org.fr++
sess.eraseRightScreen()
org.altRowoff = 0
note := readSyncLog(org.rows[org.fr].id)
//note = generateWWString(note, org.totaleditorcols)
org.note = strings.Split(note, "\n")
org.drawPreviewWithoutImages()
case ':':
sess.showOrgMessage(":")
org.command_line = ""
org.last_mode = org.mode
org.mode = COMMAND_LINE
// the two below only handle logs < 2x textLines
case ctrlKey('j'):
if len(org.rows) == 0 {
return
}
if len(org.note) > org.altRowoff+org.textLines {
if len(org.note) < org.altRowoff+2*org.textLines {
org.altRowoff = len(org.note) - org.textLines
} else {
org.altRowoff += org.textLines
}
}
sess.eraseRightScreen()
org.drawPreviewWithoutImages()
//org.altRowoff++
//sess.eraseRightScreen()
//org.drawPreviewWithoutImages()
//case PAGE_UP:
case ctrlKey('k'):
if len(org.rows) == 0 {
return
}
if org.altRowoff > org.textLines {
org.altRowoff -= org.textLines
} else {
org.altRowoff = 0
}
sess.eraseRightScreen()
org.drawPreviewWithoutImages()
//if org.altRowoff > 0 {
// org.altRowoff--
//}
//sess.eraseRightScreen()
//org.drawPreviewWithoutImages()
case ctrlKey('d'):
if len(org.rows) == 0 {
return
}
if len(org.marked_entries) == 0 {
deleteSyncItem(org.rows[org.fr].id)
} else {
for id := range org.marked_entries {
deleteSyncItem(id)
}
}
org.log(0)
case 'm':
if len(org.rows) == 0 {
return
}
mark()
}
case PREVIEW_SYNC_LOG:
switch c {
case ':':
exCmd()
case ctrlKey('j'):
org.altRowoff++
sess.eraseRightScreen()
org.drawPreviewWithoutImages()
//case ARROW_UP, 'k':
case ctrlKey('k'):
if org.altRowoff > 0 {
org.altRowoff--
}
sess.eraseRightScreen()
org.drawPreviewWithoutImages()
case PAGE_DOWN:
if len(org.note) > org.altRowoff+org.textLines {
if len(org.note) < org.altRowoff+2*org.textLines {
org.altRowoff = len(org.note) - org.textLines
} else {
org.altRowoff += org.textLines
}
}
sess.eraseRightScreen()
org.drawPreviewWithoutImages()
case PAGE_UP:
if org.altRowoff > org.textLines {
org.altRowoff -= org.textLines
} else {
org.altRowoff = 0
}
sess.eraseRightScreen()
org.drawPreviewWithoutImages()
}
case LINKS:
if c < 49 || c > 57 {
sess.showOrgMessage("That's not a number between 1 and 9")
org.mode = NORMAL
return
}
linkNum := c - 48
var found string
pre := fmt.Sprintf("[%d]", linkNum)
for _, line := range org.note {
idx := strings.Index(line, pre)
if idx != -1 {
found = line
break
}
}
if found == "" {
sess.showOrgMessage("There is no [%d]", linkNum)
org.mode = NORMAL
return
}
beg := strings.Index(found, "http")
end := strings.Index(found, "\x1b\\")
url := found[beg:end]
sess.showOrgMessage("Opening %q", url)
cmd := exec.Command("qutebrowser", url)
err := cmd.Start()
if err != nil {
sess.showOrgMessage("Problem opening url: %v", err)
}
org.mode = NORMAL
} // end switch o.mode
} // end func organizerProcessKey(c int)