-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheditor_normal.go
More file actions
549 lines (482 loc) · 15.2 KB
/
editor_normal.go
File metadata and controls
549 lines (482 loc) · 15.2 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
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
package main
import (
"fmt"
"strings"
"github.com/charmbracelet/glamour"
"github.com/charmbracelet/glamour/ansi"
"github.com/slzatz/vimango/vim"
)
func (a *App) setEditorNormalCmds(editor *Editor) map[string]func(*Editor, int) {
registry := NewCommandRegistry[func(*Editor, int)]()
// Movement commands
registry.Register("\x08", (*Editor).moveLeft, CommandInfo{
Name: keyToDisplayName("\x08"),
Description: "Move to previous editor or return to organizer",
Usage: "Ctrl-H",
Category: "Editor Selection",
Examples: []string{"Ctrl-H - Switch to previous editor"},
})
registry.Register("\x0c", (*Editor).moveRight, CommandInfo{
Name: keyToDisplayName("\x0c"),
Description: "Move to next editor",
Usage: "Ctrl-L",
Category: "Editor Selection",
Examples: []string{"Ctrl-L - Switch to next editor"},
})
// Text Editing commands
registry.Register(string(ctrlKey('b')), (*Editor).decorateWord, CommandInfo{
Name: keyToDisplayName(string(ctrlKey('b'))),
Aliases: []string{leader + "b"},
Description: "Make word bold (toggle **word**)",
Usage: "Ctrl-B",
Category: "Markup Shortcuts",
Examples: []string{"Ctrl-B - Toggle bold formatting on current word"},
})
registry.Register(string(ctrlKey('e')), (*Editor).decorateWord, CommandInfo{
Name: keyToDisplayName(string(ctrlKey('e'))),
Aliases: []string{leader + "e"},
Description: "Make word code (toggle `word`)",
Usage: "Ctrl-E",
Category: "Markup Shortcuts",
Examples: []string{"Ctrl-E - Toggle code formatting on current word"},
})
registry.Register(string(ctrlKey('i')), (*Editor).decorateWord, CommandInfo{
Name: keyToDisplayName(string(ctrlKey('i'))),
Aliases: []string{leader + "i"},
Description: "Make word italic (toggle *word*)",
Usage: "Ctrl-I",
Category: "Markup Shortcuts",
Examples: []string{"Ctrl-I - Toggle italic formatting on current word"},
})
// Preview commands
registry.Register(leader+"m", (*Editor).showMarkdownPreview, CommandInfo{
Name: keyToDisplayName(leader + "m"),
Description: "Show markdown preview of current note",
Usage: "<leader>m",
Category: "Preview",
Examples: []string{"<leader>m - Display formatted markdown preview"},
})
registry.Register(leader+"w", (*Editor).showWebView, CommandInfo{
Name: keyToDisplayName(leader + "w"),
Description: "Show current note in web browser",
Usage: "<leader>w",
Category: "Preview",
Examples: []string{"<leader>w - Open note in web browser"},
})
/*
// Window Management commands
registry.Register("\x17L", (*Editor).moveOutputWindowRight, CommandInfo{
Name: keyToDisplayName("\x17L"),
Description: "Move output window to the right",
Usage: "<C-w>L",
Category: "Window Management",
Examples: []string{"<C-w>L - Position output window to the right"},
})
registry.Register("\x17J", (*Editor).moveOutputWindowBelow, CommandInfo{
Name: keyToDisplayName("\x17J"),
Description: "Move output window below editor",
Usage: "<C-w>J",
Category: "Window Management",
Examples: []string{"<C-w>J - Position output window below editor"},
})
registry.Register("\x17=", (*Editor).changeSplit, CommandInfo{
Name: keyToDisplayName("\x17="),
Description: "Equalize split sizes",
Usage: "<C-w>=",
Category: "Window Management",
Examples: []string{"<C-w>= - Make editor and output windows equal size"},
})
registry.Register("\x17_", (*Editor).changeSplit, CommandInfo{
Name: keyToDisplayName("\x17_"),
Description: "Minimize output window",
Usage: "<C-w>_",
Category: "Window Management",
Examples: []string{"<C-w>_ - Minimize output window to 2 lines"},
})
registry.Register("\x17-", (*Editor).changeSplit, CommandInfo{
Name: keyToDisplayName("\x17-"),
Description: "Decrease output window height",
Usage: "<C-w>-",
Category: "Window Management",
Examples: []string{"<C-w>- - Decrease output window height by 1 line"},
})
registry.Register("\x17+", (*Editor).changeSplit, CommandInfo{
Name: keyToDisplayName("\x17+"),
Description: "Increase output window height",
Usage: "<C-w>+",
Category: "Window Management",
Examples: []string{"<C-w>+ - Increase output window height by 1 line"},
})
*/
registry.Register("\x17>", (*Editor).changeHSplit, CommandInfo{
Name: keyToDisplayName("\x17>"),
Description: "Increase editor width",
Usage: "<C-w>>",
Category: "Window Management",
Examples: []string{"<C-w>> - Increase editor width by 1 column"},
})
registry.Register("\x17<", (*Editor).changeHSplit, CommandInfo{
Name: keyToDisplayName("\x17<"),
Description: "Decrease editor width",
Usage: "<C-w><",
Category: "Window Management",
Examples: []string{"<C-w>< - Decrease editor width by 1 column"},
})
/*
// Output Control commands
registry.Register("\x0a", (*Editor).scrollOutputDown, CommandInfo{
Name: keyToDisplayName("\x0a"),
Description: "Scroll output window down",
Usage: "Ctrl-J",
Category: "Output Control",
Examples: []string{"Ctrl-J - Scroll down in output window"},
})
registry.Register("\x0b", (*Editor).scrollOutputUp, CommandInfo{
Name: keyToDisplayName("\x0b"),
Description: "Scroll output window up",
Usage: "Ctrl-K",
Category: "Output Control",
Examples: []string{"Ctrl-K - Scroll up in output window"},
})
*/
// Utility commands
registry.Register(leader+"y", (*Editor).nextStyle, CommandInfo{
Name: keyToDisplayName(leader + "y"),
Description: "Cycle through available styles",
Usage: "<leader>y",
Category: "Utility",
Examples: []string{"<leader>y - Switch to next available style"},
})
registry.Register(leader+"t", (*Editor).readGoTemplate, CommandInfo{
Name: keyToDisplayName(leader + "t"),
Description: "Read Go template into current note",
Usage: "<leader>t",
Category: "Utility",
Examples: []string{"<leader>t - Insert Go template content"},
})
registry.Register(leader+"sp", (*Editor).spellingCheck, CommandInfo{
Name: keyToDisplayName(leader + "sp"),
Description: "Highlight misspelled words",
Usage: "<leader>sp",
Category: "Utility",
Examples: []string{"<leader>sp - Check spelling and highlight errors"},
})
registry.Register(leader+"su", (*Editor).spellSuggest, CommandInfo{
Name: keyToDisplayName(leader + "su"),
Description: "Show spelling suggestions for current word",
Usage: "<leader>su",
Category: "Utility",
Examples: []string{"<leader>su - Get spelling suggestions"},
})
// System commands
registry.Register(string(ctrlKey('z')), (*Editor).switchImplementation, CommandInfo{
Name: keyToDisplayName(string(ctrlKey('z'))),
Description: "Switch between Go and C vim implementations",
Usage: "Ctrl-Z",
Category: "System",
Examples: []string{"Ctrl-Z - Toggle vim implementation"},
})
// Store registry in editor for help command access
editor.normalCommandRegistry = registry
return registry.GetFunctionMap()
}
func (e *Editor) changeHSplit(flag int) {
var width int
if flag == '>' {
width = e.Screen.screenCols - e.Screen.divider + 1
app.moveDividerAbs(width)
} else if flag == '<' {
width = e.Screen.screenCols - e.Screen.divider - 1
app.moveDividerAbs(width)
} else {
e.ShowMessage(BL, "flag = %v", flag)
return
}
}
func (e *Editor) moveLeft(_ int) {
// below "if" really for testing
if e.isModified() {
e.ShowMessage(BR, "Note you left has been modified")
}
if len(e.Session.Editors) == 1 {
if e.Screen.divider < 10 {
e.Screen.edPct = 80
app.moveDividerPct(80)
}
e.Session.editorMode = false
vim.SetCurrentBuffer(app.Organizer.vbuf)
app.Organizer.displayNote()
app.Organizer.mode = NORMAL
app.returnCursor()
return
}
index := 0
for i, ed := range e.Session.Editors {
if ed == e {
index = i
break
}
}
e.ShowMessage(BL, "index: %d; length: %d", index, len(e.Session.Editors))
if index > 0 {
ae := e.Session.Editors[index-1]
app.Session.activeEditor = ae
vim.SetCurrentBuffer(ae.vbuf)
// There is a bug in libvim C implementation where the cursor column position is set to zero when switching buffers
// so we set the cursor position from the stored values
vim.SetCursorPosition(ae.fr+1, ae.fc)
ae.ShowMessage(BR, "Cursor position: %+v", vim.GetCursorPosition())
return
} else {
if e.Screen.divider < 10 {
e.Screen.edPct = 80
app.moveDividerPct(80)
}
e.Session.editorMode = false
vim.SetCurrentBuffer(app.Organizer.vbuf)
app.Organizer.displayNote()
app.Organizer.mode = NORMAL
app.returnCursor()
return
}
}
func (e *Editor) moveRight(_ int) {
// below "if" for testing but may have use
if e.isModified() {
e.ShowMessage(BR, "Note you left has been modified")
}
index := 0
for i, z := range e.Session.Editors {
if z == e {
index = i
break
}
}
pos := vim.GetCursorPosition()
e.ShowMessage(BR, "Before move: index: %d; length: %d e.fr: %d; e.fc %d", index, len(e.Session.Editors), pos[0]-1, pos[1])
if index < len(e.Session.Editors)-1 {
ae := e.Session.Editors[index+1]
vim.SetCurrentBuffer(ae.vbuf)
app.Session.activeEditor = ae
// There is a bug in libvim C implementation where the cursor column position is set to zero when switching buffers
// so we set the cursor position from the stored values
vim.SetCursorPosition(ae.fr+1, ae.fc)
ae.ShowMessage(BR, "Cursor position: %+v", vim.GetCursorPosition())
}
return
}
// for VISUAL mode
func (e *Editor) decorateWordVisual(c int) {
if len(e.ss) == 0 {
return
}
if e.highlight[0][0] != e.highlight[1][0] {
e.ShowMessage(BR, "The text must all be in the same row")
return
}
row := e.ss[e.highlight[0][0]-1]
beg, end := e.highlight[0][1], e.highlight[1][1]
var undo bool
var s string
// in VISUAL mode like INSERT mode, the cursor can go beyond end of row
if len(row) == end {
s = row[beg:end]
} else {
s = row[beg : end+1]
}
e.ShowMessage(BR, "end = %d", end)
if strings.HasPrefix(s, "**") {
if c == ctrlKey('b') {
undo = true
}
} else if s[0] == '*' {
if c == ctrlKey('i') {
undo = true
}
} else if s[0] == '`' {
if c == ctrlKey('e') {
undo = true
}
}
s = strings.Trim(s, "*`")
if undo {
/*
v.SetBufferText(e.vbuf, e.fr, beg, e.fr, end, [][]byte{word})
v.SetWindowCursor(w, [2]int{e.fr + 1, beg}) //set screen cx and cy from pos
*/
vim.SendMultiInput("xi" + s + "\x1b")
return
}
// Definitely weird and needs to be looked at again but lose space at end of row
var space string
if len(row) >= end-1 {
space = " "
}
switch c {
case ctrlKey('b'):
s = fmt.Sprintf("%s**%s**", space, s)
case ctrlKey('i'):
s = fmt.Sprintf("%s*%s*", space, s)
case ctrlKey('e'):
s = fmt.Sprintf("%s`%s`", space, s)
}
vim.SendMultiInput("xi" + s + "\x1b")
/*
v.SetBufferText(e.vbuf, e.fr, beg, e.fr, end, [][]byte{[]byte(newText)})
v.SetWindowCursor(w, [2]int{e.fr + 1, beg}) //set screen cx and cy from pos
*/
}
func (e *Editor) decorateWord(c int) {
if len(e.ss) == 0 {
return
}
if e.ss[e.fr][e.fc] == ' ' {
return
}
vim.ExecuteCommand("let cword = expand('<cword>')")
w := vim.EvaluateExpression("cword")
if w == "" {
return
}
var undo bool
if strings.HasPrefix(w, "**") {
if c == ctrlKey('b') || c == 'b' {
undo = true
}
} else if w[0] == '*' {
if c == ctrlKey('i') || c == 'i' {
undo = true
}
} else if w[0] == '`' {
if c == ctrlKey('e') || c == 'e' {
undo = true
}
}
w = strings.Trim(w, "*`")
if undo {
vim.SendMultiInput("ciw" + w + "\x1b")
return
}
switch c {
case ctrlKey('b'), 'b':
w = fmt.Sprintf("**%s**", w)
case ctrlKey('i'), 'i':
w = fmt.Sprintf("*%s*", w)
case ctrlKey('e'), 'e':
w = fmt.Sprintf("`%s`", w)
}
vim.SendInput("ciw" + w + "\x1b")
}
func (e *Editor) showMarkdownPreview(_ int) {
if len(e.ss) == 0 {
return
}
//note := e.generateWWStringFromBuffer2()
note := strings.Join(e.vbuf.Lines(), "\n")
r, _ := glamour.NewTermRenderer(
glamour.WithStylePath(getGlamourStylePath()),
glamour.WithWordWrap(0),
)
note, _ = r.Render(note)
// Decode any Kitty text sizing markers (OSC 66)
note = ansi.DecodeKittyTextSizeMarkers(note)
//note = WordWrap(note, e.Screen.totaleditorcols)
note = WordWrap(note, e.screencols, 0)
note = strings.TrimSpace(note)
e.renderedNote = note
e.mode = PREVIEW
e.previewLineOffset = 0
e.drawPreview()
}
func (e *Editor) showWebView(_ int) {
if len(e.ss) == 0 {
return
}
// Get current note content
note := strings.Join(e.vbuf.Lines(), "\n")
// Get note title from the editor
title := e.title
if title == "" {
title = "Untitled Note"
}
// Convert to HTML
htmlContent, err := RenderNoteAsHTML(title, note)
if err != nil {
e.ShowMessage(BR, "Error rendering HTML: %v", err)
return
}
// Check if webview is available
if !IsWebviewAvailable() {
e.ShowMessage(BR, ShowWebviewNotAvailableMessage())
// Fall back to opening in browser
err = OpenNoteInWebview(title, htmlContent)
if err != nil {
e.ShowMessage(BR, "Error opening note: %v", err)
}
return
}
// Open in webview in a goroutine since it blocks
// This will either create a new webview or update the existing one
go func() {
err := OpenNoteInWebview(title, htmlContent)
if err != nil {
// Note: Can't directly show message from goroutine
// Could implement a channel-based message system if needed
}
}()
if IsWebviewRunning() {
e.ShowMessage(BR, "Updating webview content...")
} else {
e.ShowMessage(BR, "Opening note in webview...")
}
}
func (e *Editor) nextStyle(_ int) {
e.Session.styleIndex++
if e.Session.styleIndex > len(e.Session.style)-1 {
e.Session.styleIndex = 0
}
e.ShowMessage(BR, "New style is %q", e.Session.style[e.Session.styleIndex])
}
func (e *Editor) readGoTemplate(_ int) {
e.readFileIntoNote("go.template")
}
func (e *Editor) spellingCheck(_ int) {
/* Really need to look at this and decide if there will be a spellcheck flag in NORMAL mode */
if !IsSpellCheckAvailable() {
e.ShowMessage(BR, ShowSpellCheckNotAvailableMessage())
return
}
if e.isModified() {
e.ShowMessage(BR, "%sYou need to write the note before highlighting text%s", RED_BG, RESET)
return
}
e.highlightMispelledWords()
}
func (e *Editor) spellSuggest(_ int) {
if !IsSpellCheckAvailable() {
e.ShowMessage(BR, ShowSpellCheckNotAvailableMessage())
return
}
curPos := vim.GetCursorPosition()
w, _, _ := GetWordAtIndex(e.ss[curPos[0]-1], curPos[1])
//w := vim.EvaluateExpression("expand('<cword>')")
if CheckSpelling(w) {
e.ShowMessage(BR, "%q is spelled correctly", w)
return
}
suggestions := GetSpellingSuggestions(w)
e.ShowMessage(BR, "%q -> %s", w, strings.Join(suggestions, "|"))
}
func (e *Editor) switchImplementation(_ int) {
// Toggle between C and Go implementations
currentImpl := vim.GetActiveImplementation()
if currentImpl == vim.ImplC {
// Switch to Go implementation
e.ShowMessage(BL, "Switching to Go implementation")
vim.SwitchToGoImplementation()
} else {
// Switch to C implementation
e.ShowMessage(BL, "Switching to C implementation")
vim.SwitchToCImplementation()
}
}