From 676f04945010aeb2ae71325bf2d1d6f336a5e162 Mon Sep 17 00:00:00 2001 From: rxi Date: Thu, 23 Apr 2020 20:49:37 +0100 Subject: Moved all plugins to `plugins` dir --- plugins/autowrap.lua | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 plugins/autowrap.lua (limited to 'plugins/autowrap.lua') diff --git a/plugins/autowrap.lua b/plugins/autowrap.lua new file mode 100644 index 0000000..85d4c24 --- /dev/null +++ b/plugins/autowrap.lua @@ -0,0 +1,35 @@ +require "plugins.reflow" +local config = require "core.config" +local command = require "core.command" +local DocView = require "core.docview" + +config.autowrap_files = { "%.md$", "%.txt$" } + + +local on_text_input = DocView.on_text_input + +DocView.on_text_input = function(self, ...) + on_text_input(self, ...) + + -- early-exit if the filename does not match a file type pattern + local filename = self.doc.filename or "" + local matched = false + for _, ptn in ipairs(config.autowrap_files) do + if filename:match(ptn) then + matched = true + break + end + end + if not matched then return end + + -- do automatic reflow on line if we're typing at the end of the line and have + -- reached the line limit + local line, col = self.doc:get_selection() + local text = self.doc:get_text(line, 1, line, math.huge) + if #text >= config.line_limit and col > #text then + command.perform("doc:select-lines") + command.perform("reflow:reflow") + command.perform("doc:move-to-next-char") + command.perform("doc:move-to-previous-char") + end +end -- cgit v1.2.3