mirror of
https://github.com/zyedidia/micro.git
synced 2026-03-15 21:37:09 +09:00
Add autoclose plugin to handle autoclose for brackets, quotes etc...
The plugin adds an option `autoclose` to configure whether or not you would like quotes, brackets etc... to be automatically closed. The option is enabled by default. Closes #176
This commit is contained in:
@@ -248,6 +248,7 @@ func main() {
|
||||
L.SetGlobal("BindKey", luar.New(L, BindKey))
|
||||
L.SetGlobal("MakeCommand", luar.New(L, MakeCommand))
|
||||
L.SetGlobal("CurView", luar.New(L, CurView))
|
||||
L.SetGlobal("IsWordChar", luar.New(L, IsWordChar))
|
||||
|
||||
L.SetGlobal("JobStart", luar.New(L, JobStart))
|
||||
L.SetGlobal("JobSend", luar.New(L, JobSend))
|
||||
|
||||
@@ -14,6 +14,7 @@ var loadedPlugins []string
|
||||
var preInstalledPlugins = []string{
|
||||
"go",
|
||||
"linter",
|
||||
"autoclose",
|
||||
}
|
||||
|
||||
// Call calls the lua function 'function'
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -227,6 +227,10 @@ Default plugin options:
|
||||
|
||||
default value: `on`
|
||||
|
||||
* `autoclose`: Automatically close `{}` `()` `[]` `""` `''`. Provided by the autoclose plugin
|
||||
|
||||
default value: `on`
|
||||
|
||||
* `goimports`: Run goimports on save. Provided by the `go` plugin.
|
||||
|
||||
default value: `off`
|
||||
|
||||
78
runtime/plugins/autoclose/autoclose.lua
Normal file
78
runtime/plugins/autoclose/autoclose.lua
Normal file
@@ -0,0 +1,78 @@
|
||||
getmetatable('').__index = function(str,i) return string.sub(str,i,i) end
|
||||
|
||||
if GetOption("autoclose") == nil then
|
||||
AddOption("autoclose", true)
|
||||
end
|
||||
|
||||
local autoclosePairs = {"\"\"", "''", "()", "{}", "[]"}
|
||||
local autoNewlinePairs = {"()", "{}", "[]"}
|
||||
|
||||
function onRune(r)
|
||||
if not GetOption("autoclose") then
|
||||
return
|
||||
end
|
||||
|
||||
local v = CurView()
|
||||
for i = 1, #autoclosePairs do
|
||||
if r == autoclosePairs[i][2] then
|
||||
local curLine = v.Buf:Line(v.Cursor.Y)
|
||||
|
||||
if curLine[v.Cursor.X+1] == autoclosePairs[i][2] then
|
||||
v:Backspace()
|
||||
v:CursorRight()
|
||||
break
|
||||
end
|
||||
|
||||
if v.Cursor.X > 1 and (IsWordChar(curLine[v.Cursor.X-1]) or curLine[v.Cursor.X-1] == autoclosePairs[i][1]) then
|
||||
break
|
||||
end
|
||||
end
|
||||
if r == autoclosePairs[i][1] then
|
||||
local curLine = v.Buf:Line(v.Cursor.Y)
|
||||
|
||||
if v.Cursor.X == #curLine or not IsWordChar(curLine[v.Cursor.X+1]) then
|
||||
-- the '-' here is to derefence the pointer to v.Cursor.Loc which is automatically made
|
||||
-- when converting go structs to lua
|
||||
-- It needs to be dereferenced because the function expects a non pointer struct
|
||||
v.Buf:Insert(-v.Cursor.Loc, autoclosePairs[i][2])
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function onInsertEnter()
|
||||
if not GetOption("autoclose") then
|
||||
return
|
||||
end
|
||||
|
||||
local v = CurView()
|
||||
local curLine = v.Buf:Line(v.Cursor.Y)
|
||||
local lastLine = v.Buf:Line(v.Cursor.Y-1)
|
||||
local curRune = lastLine[#lastLine]
|
||||
local nextRune = curLine[1]
|
||||
|
||||
for i = 1, #autoNewlinePairs do
|
||||
if curRune == autoNewlinePairs[i][1] then
|
||||
if nextRune == autoNewlinePairs[i][2] then
|
||||
v:InsertTab()
|
||||
v.Buf:Insert(-v.Cursor.Loc, "\n")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function onBackspace()
|
||||
if not GetOption("autoclose") then
|
||||
return
|
||||
end
|
||||
|
||||
local v = CurView()
|
||||
|
||||
for i = 1, #autoclosePairs do
|
||||
local curLine = v.Buf:Line(v.Cursor.Y)
|
||||
if curLine[v.Cursor.X+1] == autoclosePairs[i][2] then
|
||||
v:Delete()
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user