diff --git a/runtime/help/plugins.md b/runtime/help/plugins.md index fbab646943..ae200d5b29 100644 --- a/runtime/help/plugins.md +++ b/runtime/help/plugins.md @@ -486,8 +486,8 @@ The following plugins come pre-installed with micro: directory, the diff gutter will show changes with respect to the most recent Git commit rather than the diff since opening the file. -See `> help linter`, `> help comment`, and `> help status` for additional -documentation specific to those plugins. +See `> help autoclose`, `> help linter`, `> help comment`, and `> help status` +for additional documentation specific to those plugins. These are good examples for many use-cases if you are looking to write your own plugins. diff --git a/runtime/plugins/autoclose/autoclose.lua b/runtime/plugins/autoclose/autoclose.lua index f1fc2fad9f..4df879e1c0 100644 --- a/runtime/plugins/autoclose/autoclose.lua +++ b/runtime/plugins/autoclose/autoclose.lua @@ -1,9 +1,12 @@ VERSION = "1.0.0" +local config = import("micro/config") local uutil = import("micro/util") local utf8 = import("utf8") -local autoclosePairs = {"\"\"", "''", "``", "()", "{}", "[]"} -local autoNewlinePairs = {"()", "{}", "[]"} + +config.RegisterCommonOption("autoclose", "pairs", {"\"\"", "''", "``", "()", "{}", "[]"}) +config.RegisterCommonOption("autoclose", "newlinePairs", {"()", "{}", "[]"}) +config.AddRuntimeFile("autoclose", config.RTHelp, "help/autoclose.md") function charAt(str, i) -- lua indexing is one off from go @@ -11,6 +14,7 @@ function charAt(str, i) end function onRune(bp, r) + local autoclosePairs = bp.Buf.Settings["autoclose.pairs"] for i = 1, #autoclosePairs do if r == charAt(autoclosePairs[i], 2) then local curLine = bp.Buf:Line(bp.Cursor.Y) @@ -42,6 +46,7 @@ function onRune(bp, r) end function preInsertNewline(bp) + local autoNewlinePairs = bp.Buf.Settings["autoclose.newlinePairs"] local curLine = bp.Buf:Line(bp.Cursor.Y) local curRune = charAt(curLine, bp.Cursor.X) local nextRune = charAt(curLine, bp.Cursor.X+1) @@ -64,6 +69,7 @@ function preInsertNewline(bp) end function preBackspace(bp) + local autoclosePairs = bp.Buf.Settings["autoclose.pairs"] for i = 1, #autoclosePairs do local curLine = bp.Buf:Line(bp.Cursor.Y) if charAt(curLine, bp.Cursor.X+1) == charAt(autoclosePairs[i], 2) and charAt(curLine, bp.Cursor.X) == charAt(autoclosePairs[i], 1) then diff --git a/runtime/plugins/autoclose/help/autoclose.md b/runtime/plugins/autoclose/help/autoclose.md new file mode 100644 index 0000000000..25d56c44ed --- /dev/null +++ b/runtime/plugins/autoclose/help/autoclose.md @@ -0,0 +1,13 @@ +# Autoclose + +The autoclose plugin automatically closes brackets, quotes and the like, +and it can add indentation between such symbols. The plugin can be configured +on a per-buffer basis via the following options: + +- `autoclose.pairs`: When the first rune in a pair is entered, autoclose will + add the second automatically. Moreover, when the first rune is deleted while + the cursor is on the second, then this second rune is also deleted. + The default value is ```{"\"\"", "''", "``", "()", "{}", "[]"}```. +- `autoclose.newlinePairs`: When `Enter` is pressed between such a pair, + autoclose will put the closing rune on a separate line and add indentation. + The default value is `{"()", "{}", "[]"}`.