diff options
-rw-r--r-- | README.md | 1 | ||||
-rw-r--r-- | plugins/autoinsert.lua | 75 | ||||
-rw-r--r-- | plugins/lfautoinsert.lua | 7 |
3 files changed, 81 insertions, 2 deletions
@@ -10,6 +10,7 @@ to a something other than a raw file it should be marked with an asterisk.* Plugin | Description -------|----------------------------------------- +[`autoinsert`](plugins/autoinsert.lua?raw=1) | Automatically inserts closing brackets and quotes [`autowrap`](plugins/autowrap.lua?raw=1) | Automatically hardwraps lines when typing [`bigclock`](plugins/bigclock.lua?raw=1) | Shows the current time and date in a view with large text *([screenshot](https://user-images.githubusercontent.com/3920290/82752891-3318df00-9db9-11ea-803f-261d80d5cf53.png))* [`bracketmatch`](plugins/bracketmatch.lua?raw=1) | Underlines matching pair for bracket under the caret *([screenshot](https://user-images.githubusercontent.com/3920290/80132745-0c863f00-8594-11ea-8875-c455c6fd7eae.png))* diff --git a/plugins/autoinsert.lua b/plugins/autoinsert.lua new file mode 100644 index 0000000..a3c8f98 --- /dev/null +++ b/plugins/autoinsert.lua @@ -0,0 +1,75 @@ +local core = require "core" +local config = require "core.config" +local DocView = require "core.docview" +local command = require "core.command" +local keymap = require "core.keymap" + + +config.autoinsert_map = { + ["["] = "]", + ["{"] = "}", + ["("] = ")", + ['"'] = '"', +} + + +local function is_closer(chr) + for _, v in pairs(config.autoinsert_map) do + if v == chr then + return true + end + end +end + + +local on_text_input = DocView.on_text_input + +function DocView:on_text_input(text) + local mapping = config.autoinsert_map[text] + + -- prevents plugin from operating on `CommandView` + if getmetatable(self) ~= DocView then + return on_text_input(self, text) + end + + -- wrap selection if we have a selection + if mapping and self.doc:has_selection() then + local l1, c1, l2, c2, swap = self.doc:get_selection(true) + self.doc:insert(l2, c2, mapping) + self.doc:insert(l1, c1, text) + self.doc:set_selection(l1, c1, l2, c2 + 2, swap) + return + end + + -- skip inserting closing text + local chr = self.doc:get_char(self.doc:get_selection()) + if text == chr and is_closer(chr) then + self.doc:move_to(1) + return + end + + -- auto insert closing bracket + if mapping and (chr:find("%s") or is_closer(chr) and chr ~= '"') then + on_text_input(self, text) + on_text_input(self, mapping) + self.doc:move_to(-1) + return + end + + on_text_input(self, text) +end + + +command.add("core.docview", { + ["autoinsert:backspace"] = function() + local doc = core.active_view.doc + local l, c = doc:get_selection() + local chr = doc:get_char(l, c) + if config.autoinsert_map[doc:get_char(l, c - 1)] and is_closer(chr) then + doc:delete_to(1) + end + command.perform "doc:backspace" + end, +}) + +keymap.add { ["backspace"] = "autoinsert:backspace" } diff --git a/plugins/lfautoinsert.lua b/plugins/lfautoinsert.lua index 2c59e41..0ea7b9d 100644 --- a/plugins/lfautoinsert.lua +++ b/plugins/lfautoinsert.lua @@ -3,7 +3,7 @@ local command = require "core.command" local config = require "core.config" local keymap = require "core.keymap" -config.autoinsert_map = { +config.lfautoinsert_map = { ["{%s*\n"] = "}", ["%(%s*\n"] = ")", ["%f[[]%[%s*\n"] = "]", @@ -35,7 +35,7 @@ command.add("core.docview", { local line, col = doc:get_selection() local text = doc.lines[line - 1] - for ptn, close in pairs(config.autoinsert_map) do + for ptn, close in pairs(config.lfautoinsert_map) do local s, _, str = text:find(ptn) if s then if close @@ -49,6 +49,9 @@ command.add("core.docview", { if doc.lines[line+1] == doc.lines[line+2] then doc:remove(line+1, 1, line+2, 1) end + elseif col < #doc.lines[line] then + command.perform("doc:newline") + command.perform("doc:move-to-previous-line") end command.perform("doc:indent") end |