aboutsummaryrefslogtreecommitdiff
path: root/plugins/autowrap.lua
blob: c9dde6d708652a04dd8ef73675f1c6fa706e2e10 (plain)
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
-- mod-version:2 -- lite-xl 2.0
require "plugins.reflow"
local config = require "core.config"
local command = require "core.command"
local DocView = require "core.docview"

config.plugins.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.plugins.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")
    command.perform("doc:move-to-end-of-line")
  end
end