aboutsummaryrefslogtreecommitdiff
path: root/plugins/autowrap.lua
blob: fee001bb00bbb0265851192c83b95a16556dfd80 (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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
-- mod-version:3 --lite-xl 2.1
local core = require "core"
local config = require "core.config"
local command = require "core.command"
local common = require "core.common"
local DocView = require "core.docview"

config.plugins.autowrap = common.merge({
  enable = false,
  files = { "%.md$", "%.txt$" }
}, config.plugins.autowrap)


local on_text_input = DocView.on_text_input

DocView.on_text_input = function(self, ...)
  on_text_input(self, ...)

  if not config.plugins.autowrap.enable then return end

  -- 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-end-of-line")
  end
end

command.add(nil, {
  ["auto-wrap:toggle"] = function()
    config.plugins.autowrap.enable = not config.plugins.autowrap.enable
    if config.plugins.autowrap.enable then
      core.log("Auto wrap: on")
    else
      core.log("Auto wrap: off")
    end
  end
})