-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprompt.lua
More file actions
150 lines (127 loc) · 2.56 KB
/
prompt.lua
File metadata and controls
150 lines (127 loc) · 2.56 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
local micro = import("micro")
local shell = import("micro/shell")
local config = import("micro/config")
local plug_path = config.ConfigDir .. "/plug/?.lua"
if not package.path:find(plug_path, 1, true) then
package.path = package.path .. ";" .. plug_path
end
local bell = require("vi/bell")
local mode = require("vi/mode")
local move = require("vi/move")
local PROMPT = ":"
local prompt_buffer = ""
local function show()
micro.InfoBar():Message(PROMPT .. prompt_buffer)
end
local function insert_chars(chars)
prompt_buffer = prompt_buffer .. chars
show()
end
local function clear()
prompt_buffer = ""
end
local function enter()
local pb = prompt_buffer
local pane = micro.CurPane()
local matched = false
--
-- Move
--
if pb:match("^%d+$") then
-- Move cursor to line <num>
local num = tonumber(pb:match("^(%d+)$"))
if num < 1 then
num = 1
end
move.to_line(num)
matched = true
end
--
-- File
--
if pb == "wq" then
-- Save current file and quit.
pane:Save()
pane:Quit()
matched = true
elseif pb == "w" then
-- Save current file.
pane:Save()
matched = true
elseif pb == "w!" then
-- Force save current file.
bell.not_planned()
matched = true
elseif pb == "q" then
-- Quit editor.
pane:Quit()
matched = true
elseif pb == "q!" then
-- Force quit editor.
pane:ForceQuit()
matched = true
elseif pb == "e" then
-- Open file.
pane:OpenFile()
matched = true
elseif pb == "e!" then
-- Force open file.
bell.planned()
matched = true
elseif pb == "r" then
-- Read file and insert to current buffer.
bell.not_planned()
matched = true
elseif pb == "n" then
-- Switch to next buffer (tab).
pane:NextTab()
matched = true
elseif pb == "prev" then
-- Switch to previous buffer (tab).
pane:PreviousTab()
matched = true
end
--
-- Utility
--
if pb == "sh" then
-- Execute shell.
shell.RunInteractiveShell("sh -c $SHELL", false, false)
matched = true
end
--
-- From Vim
--
if pb == "wa" then -- vim
-- Save all files.
pane:SaveAll()
matched = true
elseif pb == "qa" then -- vim
-- Close all files and quit editor.
pane:QuitAll()
matched = true
elseif pb == "qa!" then -- vim
-- Force close all files and quit editor.
pane:QuitAll()
matched = true
end
if not matched then
bell.vi_error("not (yet) a ex command [" .. pb .. "]")
end
clear()
mode.command()
end
local function escape()
clear()
micro.InfoBar():Message("")
mode.command()
end
-------------
-- Exports --
-------------
local M = {}
M.show = show
M.insert_chars = insert_chars
M.enter = enter
M.escape = escape
return M