-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.lua
More file actions
129 lines (113 loc) · 3.03 KB
/
init.lua
File metadata and controls
129 lines (113 loc) · 3.03 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
VERSION = "0.0.15"
local micro = import("micro")
local config = import("micro/config")
local buffer = import("micro/buffer")
local utf8 = import("unicode/utf8")
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 utils = require("vi/utils")
local bell = require("vi/bell")
local mode = require("vi/mode")
local combuf = require("vi/combuf")
local prompt = require("vi/prompt")
local context = require("vi/context")
local move = require("vi/move")
local insert = require("vi/insert")
local memorized = false
function Vi(_)
-- reset states
combuf.clear()
-- ensure command mode
if mode.is_command() then
bell.ring("already in vi command mode")
return true
elseif mode.is_search() then
mode.command()
return true
elseif mode.is_prompt() then
prompt.escape()
return true
end
mode.command()
--
local pane = micro.CurPane()
local buf = pane.Buf
local cursor = buf:GetActiveCursor()
local orig_loc = buffer.Loc(cursor.X, cursor.Y)
-- ensure cursor y in text
local last_line_index = utils.last_line_index(buf)
cursor.Y = math.min(cursor.Y, last_line_index)
-- ensure cursor x in text
local line = buf:Line(cursor.Y)
local length = utf8.RuneCount(line)
cursor.X = math.min(math.max(cursor.X - 1, 0), math.max(length - 1, 0))
pane:Relocate()
--
move.update_virtual_cursor()
mode.show()
--
if not memorized then
context.memorize()
memorized = true
end
--
insert.resume(orig_loc)
return true
end
function ViEnter(_)
if mode.is_command() then
local buf = micro.CurPane().Buf
local cursor = buf:GetActiveCursor()
local loc = buffer.Loc(cursor.X, cursor.Y)
buf:Insert(loc, "\n")
return true
elseif mode.is_insert() then
return false
elseif mode.is_search() then
mode.command()
return true
elseif mode.is_prompt() then
prompt.enter()
return true
else
bell.program_error("invalid mode == " .. mode.code())
return false
end
end
function ViDefault(_, args)
local USAGE = "usage: videfault [true|false]"
local default
if #args < 1 then
default = not config.GetGlobalOption("vi.default")
elseif #args < 2 then
if utils.toboolean(args[1]) == nil then
bell.info(USAGE)
return
end
default = args[1]
else
bell.info(USAGE)
return
end
config.SetGlobalOption("vi.default", tostring(default))
bell.info("vi.default is now " .. tostring(default))
end
function preinit()
config.RegisterCommonOption("vi", "default", false)
end
function init()
config.MakeCommand("vi", Vi, config.NoComplete)
config.MakeCommand("vienter", ViEnter, config.NoComplete)
config.MakeCommand("videfault", ViDefault, config.NoComplete)
config.TryBindKey("Escape", "Escape,Deselect,ClearInfo,RemoveAllMultiCursors,UnhighlightSearch,lua:vi.Vi", false)
config.TryBindKey("Enter", "lua:vi.ViEnter|InsertNewline", false)
config.AddRuntimeFile("vi", config.RTHelp, "help/vi.md")
config.AddRuntimeFile("vi", config.RTHelp, "help/vicommands.md")
end
function postinit()
if config.GetGlobalOption("vi.default") then
Vi()
end
end