aboutsummaryrefslogtreecommitdiff
path: root/data/plugins/lineguide.lua
blob: 61989816e5b44eb586361ebe5e69166c8c55de18 (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
-- mod-version:4
local common = require "core.common"
local command = require "core.command"
local config = require "core.config"
local style = require "core.style"
local DocView = require "core.docview"
local CommandView = require "core.commandview"

config.plugins.lineguide = common.merge({
  enabled = false,
  width = 2,
  rulers = {
    -- 80,
    -- 100,
    -- 120,
    config.line_limit
  },
  use_custom_color = false,
  custom_color = style.selection,
  -- The config specification used by gui generators
  config_spec = {
    name = "Line Guide",
    {
      label = "Enabled",
      description = "Disable or enable drawing of the line guide.",
      path = "enabled",
      type = "toggle",
      default = true
    },
    {
      label = "Width",
      description = "Width in pixels of the line guide.",
      path = "width",
      type = "number",
      default = 2,
      min = 1
    },
    {
      label = "Ruler Positions",
      description = "The different column numbers for the line guides to draw.",
      path = "rulers",
      type = "list_strings",
      default = { tostring(config.line_limit) or "80" },
      get_value = function(rulers)
        if type(rulers) == "table" then
          local new_rulers = {}
          for _, ruler in ipairs(rulers) do
            table.insert(new_rulers, tostring(ruler))
          end
          return new_rulers
        else
          return { tostring(config.line_limit) }
        end
      end,
      set_value = function(rulers)
        local new_rulers = {}
        for _, ruler in ipairs(rulers) do
          local number = tonumber(ruler)
          if number then
            table.insert(new_rulers, number)
          end
        end
        if #new_rulers == 0 then
          table.insert(new_rulers, config.line_limit)
        end
        return new_rulers
      end
    },
    {
      label = "Use Custom Color",
      description = "Enable the utilization of a custom line color.",
      path = "use_custom_color",
      type = "toggle",
      default = false
    },
    {
      label = "Custom Color",
      description = "Applied when the above toggle is enabled.",
      path = "custom_color",
      type = "color",
      default = style.selection
    },
  }
}, config.plugins.lineguide)

local function get_ruler(v)
  local result = nil
  if type(v) == 'number' then
    result = { columns = v }
  elseif type(v) == 'table' then
    result = v
  end
  return result
end

local draw_overlay = DocView.draw_overlay
function DocView:draw_overlay(...)
  if
    type(config.plugins.lineguide) == "table"
    and
    config.plugins.lineguide.enabled
    and
    self:is(DocView)
  then
    local conf = config.plugins.lineguide
    local line_x = self:get_line_screen_position(1)
    local character_width = self:get_font():get_width("n")
    local ruler_width = config.plugins.lineguide.width
    local ruler_color = conf.use_custom_color and conf.custom_color
      or (style.guide or style.selection)

    for k,v in ipairs(config.plugins.lineguide.rulers) do
      local ruler = get_ruler(v)

      if ruler then
        local x = line_x + (character_width * ruler.columns)
        local y = self.position.y
        local w = ruler_width
        local h = self.size.y

        renderer.draw_rect(x, y, w, h, ruler.color or ruler_color)
      end
    end
  end
  -- everything else like the cursor above the line guides
  draw_overlay(self, ...)
end

command.add(nil, {
  ["lineguide:toggle"] = function()
    config.plugins.lineguide.enabled = not config.plugins.lineguide.enabled
  end
})