summaryrefslogtreecommitdiff
path: root/runtime/lua/core/app.lua
blob: a596f59c27e5ecfe7bb1ab067e3d009b9f984560 (plain)
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
local theme = require("core.theme")
local themes = require("core.themes")
local state = require("core.state")
local ui = require("core.ui")
local command = require("core.commands")
local cmdline = require("core.cmdline")
local hooks = require("core.hooks")
local motion = require("core.motion")
local modes = require("core.modes")

local M = {}

state.hooks = hooks.new_store()

local function current_palette()
  return themes.all[state.current_theme]
end

local function set_theme(name)
  local palette = themes.all[name]
  if not palette then
    ui.set_message(state, "Unknown theme: " .. tostring(name))
    return false
  end

  local prev = state.current_theme
  state.current_theme = name
  theme.apply_base16(palette)
  ui.setup_layout(state, palette)
  ui.set_message(state, "Theme: " .. name)
  ui.update_modeline(state)
  hooks.run(state.hooks, "theme_changed", name, prev)
  return true
end

local function update_cursor_for_current_mode()
  if state.command_mode then
    editor.set_cursor_shape("block")
    editor.set_cursor_thickness(2)
    return
  end

  if modes.is_insert(state) then
    editor.set_cursor_shape("bar")
    editor.set_cursor_thickness(2)
    return
  end

  editor.set_cursor_shape("block")
  editor.set_cursor_thickness(2)
end

local function on_commandline_key(key)
  if key == "escape" then
    cmdline.exit(state, ui, current_palette())
    ui.set_message(state, "lua cmdline cancelled")
    update_cursor_for_current_mode()
    hooks.run(state.hooks, "commandline_exit", "cancel")
    return true
  end

  if key == "backspace" then
    state.command_text = state.command_text:sub(1, #state.command_text - 1)
    ui.update_commandline_buffer(state)
    hooks.run(state.hooks, "commandline_change", state.command_text)
    return true
  end

  if key == "enter" then
    local line = state.command_text
    local kind = state.command_kind
    cmdline.exit(state, ui, current_palette())
    update_cursor_for_current_mode()
    if line ~= "" then
      hooks.run(state.hooks, "commandline_execute", line)
      local ok, message
      if kind == "command" then
        ok, message = cmdline.eval_command(state, line)
      else
        ok, message = cmdline.eval_line(line)
      end
      if message then
        ui.set_message(state, message)
      end
      hooks.run(state.hooks, "commandline_result", line, ok, message)
      return ok
    end
    ui.set_message(state, "lua cmdline empty")
    hooks.run(state.hooks, "commandline_exit", "empty")
    return true
  end

  return true
end

local function define_editor_commands()
  command.define(state, "quit", function()
    hooks.run(state.hooks, "before_quit")
    editor.request_quit()
  end)

  command.define(state, "save", function()
    local path = ui.current_buffer_label()
    local ok = editor.save_file(nil)
    if ok then
      ui.set_message(state, "Saved " .. path)
    else
      ui.set_message(state, "Save failed (no file path yet)")
    end
    ui.update_modeline(state)
    hooks.run(state.hooks, "after_save", ok, path)
  end)

  command.define(state, "new-buffer", function()
    local index = editor.new_buffer()
    if not index then
      ui.set_message(state, "Failed to create new buffer")
      return
    end
    ui.set_message(state, string.format("New buffer #%d", index))
    ui.update_modeline(state)
  end)

  command.define(state, "next-buffer", function()
    local count = editor.buffer_count()
    if count <= 0 then
      return
    end

    local current = editor.current_buffer_index()
    local next_index = (current + 1) % count
    editor.switch_buffer(next_index)
    local path = editor.buffer_path()
    if path then
      ui.set_message(state, "Buffer: " .. path)
    else
      ui.set_message(state, string.format("Buffer #%d", next_index))
    end
    ui.update_modeline(state)
  end)

  command.define(state, "prev-buffer", function()
    local count = editor.buffer_count()
    if count <= 0 then
      return
    end

    local current = editor.current_buffer_index()
    local prev_index = (current - 1 + count) % count
    editor.switch_buffer(prev_index)
    local path = editor.buffer_path()
    if path then
      ui.set_message(state, "Buffer: " .. path)
    else
      ui.set_message(state, string.format("Buffer #%d", prev_index))
    end
    ui.update_modeline(state)
  end)

  command.define(state, "list-buffers", function()
    local count = editor.buffer_count()
    local current = editor.current_buffer_index()
    local names = {}
    for i = 0, count - 1 do
      local path = editor.buffer_path_at(i)
      local label = path or string.format("*buffer-%d*", i)
      local dirty = editor.buffer_modified_at(i) and "*" or ""
      local mark = (i == current) and ">" or " "
      names[#names + 1] = string.format("%s%d:%s%s", mark, i, label, dirty)
    end
    ui.set_message(state, table.concat(names, " | "))
  end)

  command.define(state, "buffer", function()
    ui.set_message(state, "Usage: :buffer <index>")
  end)

  command.define(state, "buffer-switch", function(index)
    if type(index) ~= "number" then
      ui.set_message(state, "buffer-switch expects number")
      return
    end
    if editor.switch_buffer(index) then
      local path = editor.buffer_path()
      if path then
        ui.set_message(state, "Buffer: " .. path)
      else
        ui.set_message(state, string.format("Buffer #%d", index))
      end
      ui.update_modeline(state)
    else
      ui.set_message(state, string.format("Invalid buffer index: %s", tostring(index)))
    end
  end)

  command.define(state, "write", function()
    command.call(state, "save")
  end)

  command.define(state, "w", function()
    command.call(state, "write")
  end)

  command.define(state, "write-quit", function()
    local ok = editor.save_file(nil)
    local path = ui.current_buffer_label()
    if ok then
      ui.set_message(state, "Saved " .. path)
      ui.update_modeline(state)
      hooks.run(state.hooks, "after_save", ok, path)
      hooks.run(state.hooks, "before_quit")
      editor.request_quit()
    else
      ui.set_message(state, "Save failed (no file path yet)")
      ui.update_modeline(state)
      hooks.run(state.hooks, "after_save", ok, path)
    end
  end)

  command.define(state, "wq", function()
    command.call(state, "write-quit")
  end)

  command.define(state, "help", function()
    ui.set_message(state, "Modes: i/a/o insert, esc normal. Normal: hjkl w b d ^ $ : | <leader>l lua")
  end)

  command.define(state, "toggle-theme", function()
    if state.current_theme == "base16-default-light" then
      set_theme("base16-gruvbox-dark-hard")
    else
      set_theme("base16-default-light")
    end
  end)

  command.define(state, "lua-commandline", function()
    if state.command_mode then
      cmdline.exit(state, ui, current_palette())
      hooks.run(state.hooks, "commandline_exit", "toggle")
    else
      cmdline.enter(state, ui, current_palette(), "lua")
      hooks.run(state.hooks, "commandline_enter")
    end
    update_cursor_for_current_mode()
  end)

  command.define(state, "named-command-prompt", function()
    if state.command_mode then
      cmdline.exit(state, ui, current_palette())
      hooks.run(state.hooks, "commandline_exit", "toggle")
    else
      cmdline.enter(state, ui, current_palette(), "command")
      hooks.run(state.hooks, "commandline_enter")
    end
    update_cursor_for_current_mode()
  end)

  command.define(state, "enter-insert-mode", function()
    modes.set_mode(state, "insert")
    ui.update_modeline(state)
    update_cursor_for_current_mode()
  end)

  command.define(state, "append-mode", function()
    editor.move_right()
    modes.set_mode(state, "insert")
    ui.update_modeline(state)
    update_cursor_for_current_mode()
  end)

  command.define(state, "open-below", function()
    editor.move_end_of_line()
    editor.insert("\n")
    modes.set_mode(state, "insert")
    ui.update_modeline(state)
    update_cursor_for_current_mode()
  end)

  command.define(state, "enter-normal-mode", function()
    modes.set_mode(state, "normal")
    state.leader_pending = false
    ui.update_modeline(state)
    update_cursor_for_current_mode()
  end)

  command.define(state, "move-left", function() editor.move_left() end)
  command.define(state, "move-right", function() editor.move_right() end)
  command.define(state, "move-up", function() editor.move_up() end)
  command.define(state, "move-down", function() editor.move_down() end)
  command.define(state, "delete-char", function() editor.delete_forward() end)
  command.define(state, "beginning-of-line", function() editor.move_beginning_of_line() end)
  command.define(state, "end-of-line", function() editor.move_end_of_line() end)
  command.define(state, "forward-word", function() motion.forward_word() end)
  command.define(state, "backward-word", function() motion.backward_word() end)
  command.define(state, "backspace", function() editor.backspace() end)
  command.define(state, "delete-forward", function() editor.delete_forward() end)
  command.define(state, "newline", function() editor.insert("\n") end)
  command.define(state, "tab", function() editor.insert("    ") end)

  command.bind(state, "C-q", "quit")
  command.bind(state, "C-s", "save")
  command.bind(state, "C-h", "help")
  command.bind(state, "C-t", "toggle-theme")
  command.bind(state, "C-a", "beginning-of-line")
  command.bind(state, "C-e", "end-of-line")
  command.bind(state, "C-f", "forward-word")
  command.bind(state, "C-b", "backward-word")

  state.leader_map["l"] = "lua-commandline"

  command.bind(state, "left", "move-left")
  command.bind(state, "right", "move-right")
  command.bind(state, "up", "move-up")
  command.bind(state, "down", "move-down")
  command.bind(state, "backspace", "backspace")
  command.bind(state, "delete", "delete-forward")
  command.bind(state, "enter", "newline")
  command.bind(state, "tab", "tab")
end

function M.add_hook(event_name, fn)
  return hooks.add(state.hooks, event_name, fn)
end

function M.bind_key(key, target)
  if type(target) == "string" then
    command.bind(state, key, target)
    return true
  end

  if type(target) == "function" then
    return command.bind_fn(state, key, target)
  end

  return false, "bind target must be command name or function"
end

function M.bind_leader(key, target)
  if type(key) ~= "string" or key == "" then
    return false, "leader key must be non-empty"
  end

  if type(target) == "string" then
    state.leader_map[key] = target
    return true
  end

  if type(target) == "function" then
    local name = string.format("leader:%s", key)
    command.define(state, name, target)
    state.leader_map[key] = name
    return true
  end

  return false, "leader bind target must be command name or function"
end

local function try_extended_named_command(input)
  local name, arg = input:match("^(%S+)%s+(.+)$")
  if not name then
    return false
  end

  if name ~= "buffer" and name ~= "b" then
    return false
  end

  local index = tonumber(arg)
  if not index then
    return false
  end

  command.call(state, "buffer-switch", index)
  return true
end

function M.bootstrap()
  state.mode_line_buffer = editor.aux_buffer_new()
  state.message_buffer = editor.aux_buffer_new()
  state.command_buffer = editor.aux_buffer_new()
  if not state.mode_line_buffer or not state.message_buffer or not state.command_buffer then
    error("failed to allocate UI buffers")
  end

  editor.set_font("monospace:size=14")
  set_theme(state.current_theme)
  update_cursor_for_current_mode()
  ui.set_message(state, state.message_text)
  ui.update_commandline_buffer(state)
  define_editor_commands()
  ui.update_modeline(state)
  ui.set_message(state, "ready")
  hooks.run(state.hooks, "bootstrap")
end

function M.on_open_file(path)
  if not path or path == "" then
    return false
  end

  local ok = editor.open_file(path)
  if ok then
    ui.set_message(state, "Opened " .. path)
  else
    ui.set_message(state, "Could not open: " .. path)
  end
  ui.update_modeline(state)
  hooks.run(state.hooks, "after_open_file", ok, path)
  return ok
end

function M.on_key(key)
  hooks.run(state.hooks, "before_key", key)

  if hooks.run_first_truthy(state.hooks, "intercept_key", key) then
    hooks.run(state.hooks, "after_key", key, true)
    return true
  end

  if state.command_mode then
    local handled = on_commandline_key(key)
    hooks.run(state.hooks, "after_key", key, handled)
    return handled
  end

  if modes.is_insert(state) and key == "escape" then
    state.suppress_next_text = true
    command.call(state, "enter-normal-mode")
    hooks.run(state.hooks, "after_key", key, true)
    return true
  end

  if modes.is_normal(state) then
    if state.leader_pending then
      state.suppress_next_text = true
      state.leader_pending = false
      local command_name = state.leader_map[key]
      if command_name then
        command.call(state, command_name)
      else
        ui.set_message(state, string.format("No <leader> mapping for '%s'", key))
      end
      ui.update_modeline(state)
      hooks.run(state.hooks, "after_key", key, true)
      return true
    end

    if key == ":" then
      state.suppress_next_text = true
      command.call(state, "named-command-prompt")
      hooks.run(state.hooks, "after_key", key, true)
      return true
    end

    if key == state.leader_key then
      state.suppress_next_text = true
      state.leader_pending = true
      ui.update_modeline(state)
      hooks.run(state.hooks, "after_key", key, true)
      return true
    end

    if key == "i" then
      state.suppress_next_text = true
      command.call(state, "enter-insert-mode")
      hooks.run(state.hooks, "after_key", key, true)
      return true
    end
    if key == "a" then
      state.suppress_next_text = true
      command.call(state, "append-mode")
      hooks.run(state.hooks, "after_key", key, true)
      return true
    end
    if key == "o" then
      state.suppress_next_text = true
      command.call(state, "open-below")
      hooks.run(state.hooks, "after_key", key, true)
      return true
    end
    if key == "h" then
      state.suppress_next_text = true
      command.call(state, "move-left")
      hooks.run(state.hooks, "after_key", key, true)
      return true
    end
    if key == "j" then
      state.suppress_next_text = true
      command.call(state, "move-down")
      hooks.run(state.hooks, "after_key", key, true)
      return true
    end
    if key == "k" then
      state.suppress_next_text = true
      command.call(state, "move-up")
      hooks.run(state.hooks, "after_key", key, true)
      return true
    end
    if key == "l" then
      state.suppress_next_text = true
      command.call(state, "move-right")
      hooks.run(state.hooks, "after_key", key, true)
      return true
    end
    if key == "w" then
      state.suppress_next_text = true
      command.call(state, "forward-word")
      hooks.run(state.hooks, "after_key", key, true)
      return true
    end
    if key == "b" then
      state.suppress_next_text = true
      command.call(state, "backward-word")
      hooks.run(state.hooks, "after_key", key, true)
      return true
    end
    if key == "d" then
      state.suppress_next_text = true
      command.call(state, "delete-char")
      hooks.run(state.hooks, "after_key", key, true)
      return true
    end
    if key == "^" then
      state.suppress_next_text = true
      command.call(state, "beginning-of-line")
      hooks.run(state.hooks, "after_key", key, true)
      return true
    end
    if key == "$" then
      state.suppress_next_text = true
      command.call(state, "end-of-line")
      hooks.run(state.hooks, "after_key", key, true)
      return true
    end
  end

  local command_name = state.keymap[key]
  if not command_name then
    hooks.run(state.hooks, "after_key", key, false)
    return false
  end

  local ok, err = command.call(state, command_name)
  if not ok and err then
    ui.set_message(state, err)
  end
  hooks.run(state.hooks, "after_key", key, ok)
  return ok
end

function M.try_named_command_extension(input)
  return try_extended_named_command(input)
end

function M.on_text(text)
  hooks.run(state.hooks, "before_text", text)

  if state.suppress_next_text then
    state.suppress_next_text = false
    hooks.run(state.hooks, "after_text", text, true)
    return true
  end

  if hooks.run_first_truthy(state.hooks, "intercept_text", text) then
    hooks.run(state.hooks, "after_text", text, true)
    return true
  end

  if state.command_mode then
    state.command_text = state.command_text .. text
    ui.update_commandline_buffer(state)
    hooks.run(state.hooks, "after_text", text, true)
    return true
  end

  if not modes.is_insert(state) then
    hooks.run(state.hooks, "after_text", text, true)
    return true
  end

  local ok = editor.insert(text)
  ui.update_modeline(state)
  hooks.run(state.hooks, "after_text", text, ok)
  return ok
end

function M.on_tick()
  hooks.run(state.hooks, "before_tick")
  ui.setup_layout(state, current_palette())
  ui.update_modeline(state)
  hooks.run(state.hooks, "after_layout")
  hooks.run(state.hooks, "after_tick")
end

return M