summaryrefslogtreecommitdiff
path: root/init.lua
blob: 9930ba77ef23aaa1e5bc6a58d70ea020800a933f (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
local open_prompt = {
  active = false,
  text = "",
  buffer = editor.aux_buffer_new(),
}

local function hide_open_prompt()
  open_prompt.active = false
  open_prompt.text = ""
  editor.aux_buffer_set_text(open_prompt.buffer, "")
end

local function show_open_prompt()
  open_prompt.active = true
  open_prompt.text = ""
  editor.aux_buffer_set_text(open_prompt.buffer, "open file: ")
end

local function refresh_open_prompt()
  editor.aux_buffer_set_text(open_prompt.buffer, "open file: " .. open_prompt.text)
end

lemacs.keys.bind_leader("f", function()
  if not open_prompt.active then
    show_open_prompt()
  end
end)

lemacs.hooks.add("intercept_key", function(key)
  if not open_prompt.active then
    return false
  end

  if key == "escape" then
    hide_open_prompt()
    return true
  end

  if key == "backspace" then
    open_prompt.text = open_prompt.text:sub(1, #open_prompt.text - 1)
    refresh_open_prompt()
    return true
  end

  if key == "enter" then
    local path = open_prompt.text
    hide_open_prompt()
    if path ~= "" then
      editor.open_file(path)
    end
    return true
  end

  return open_prompt.active
end)

lemacs.hooks.add("intercept_text", function(text)
  if not open_prompt.active then
    return false
  end

  open_prompt.text = open_prompt.text .. text
  refresh_open_prompt()
  return true
end)

lemacs.hooks.add("after_layout", function()
  if not open_prompt.active then
    return
  end

  local width = editor.screen_width()
  local y = editor.screen_height() - 24
  lemacs.ui.add_view(1, open_prompt.buffer, 0, y, width, 24, false, 42, 44, 54, 245, 232, 232, 236, 255)
end)