aboutsummaryrefslogtreecommitdiff
path: root/plugins/custom_caret.lua
blob: ac45f55abd7c2dbfc43ec3b449e84d2d0ba95439 (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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
-- mod-version:3

--[[
  Author: techie-guy
  Plugin to customize the caret in the editor
  Thanks to @Guldoman for the initial example on Discord

  Features
    Change the Color and Opacity of the caret
    Change the Shape of the caret, available shapes are Line, Block, Underline

  Customizing the Caret: (this can be changed from the .config/lite-xl/init.lua
  file or from the settings menu plugin)
    config.plugins.custom_caret.shape - Change the shape of the caret [string]
    style.caret - Change the rgba color of the caret [table]

  Example Config(in the .config/lite-xl/init.lua)
    style.caret = {0, 255, 255, 150}
    config.plugins.custom_caret.shape = "block"
]]

local core = require "core"
local style = require "core.style"
local common = require "core.common"
local config = require "core.config"
local DocView = require "core.docview"


config.plugins.custom_caret = common.merge({
    shape = "line",
    custom_color = true,
    caret_color = table.pack(table.unpack(style.caret)),
    surrounding_chars = false,
}, config.plugins.custom_caret)

-- Reference to plugin config
local conf = config.plugins.custom_caret

-- Get real default caret color after everything is loaded up
core.add_thread(function()
  if
    conf.caret_color[1] == 147 and conf.caret_color[2] == 221
    and
    conf.caret_color[3] == 250 and conf.caret_color[4] == 255
    and
    (
      style.caret[1] ~= conf.caret_color[1] or style.caret[2] ~= conf.caret_color[2]
      or
      style.caret[3] ~= conf.caret_color[3] or style.caret[4] ~= conf.caret_color[4]
    )
  then
    conf.caret_color = table.pack(table.unpack(style.caret))
  end

  local settings_loaded, settings = pcall(require, "plugins.settings")
  if settings_loaded then
    conf.config_spec = {
      name = "Custom Caret",
      {
        label = "Shape",
        description = "The Shape of the cursor.",
        path = "shape",
        type = "selection",
        default = "line",
        values = {
          {"Line", "line"},
          {"Block", "block"},
          {"Underline", "underline"}
        }
      },
      {
        label = "Custom Color",
        description = "Use a custom color for the caret as specified below.",
        path = "custom_color",
        type = "toggle",
        default = true
      },
      {
        label = "Caret Color",
        description = "Custom color of the caret.",
        path = "caret_color",
        type = "color",
        default = table.pack(table.unpack(style.caret)),
      },
      {
        label = "Surrounding Characters",
        description = "When using block caret, whether you want to show the characters around for a better character switch.",
        path = "surrounding_chars",
        type ="toggle",
        default = false,
      },
    }

    ---@cast settings plugins.settings
    settings.ui:enable_plugin("custom_caret")
  end
end)

local caret_idx = 1

local docview_update = DocView.update
function DocView:update()
  docview_update(self)
  caret_idx = 1
end

function DocView:draw_caret(x, y)
  local caret_width = style.caret_width
  local caret_height = self:get_line_height()
  local current_caret_shape = conf.shape
  local caret_color = conf.custom_color and conf.caret_color or style.caret

  local font = self:get_font()
  local line, col = self.doc:get_selection_idx(caret_idx)
  local charw = math.ceil(font:get_width(self.doc:get_char(line, col)))

  if (current_caret_shape == "block") then
    caret_width = charw
  elseif (current_caret_shape == "underline") then
    caret_width = charw
    caret_height = style.caret_width*2
    y = y+self:get_line_height()
  else
    caret_width = style.caret_width
    caret_height = self:get_line_height()
  end

  renderer.draw_rect(x, y, caret_width, caret_height, caret_color)
  if current_caret_shape == "block" then
    core.push_clip_rect(x, y, caret_width, caret_height)

    local function draw_char(l, c)
      l = common.clamp(l, 1, #self.doc.lines   )
      c = common.clamp(c, 1, #self.doc.lines[l])
      local cx,cy = self:get_line_screen_position(l, c)
      renderer.draw_text(
        font, self.doc:get_char(l, c),
        cx, cy+self:get_line_text_y_offset(),
        style.background
      )
    end

    if conf.surrounding_chars then
      for yo=-1, 1 do
        for xo=-1, 1 do
          draw_char(line+xo, col+yo)
        end
      end
    else
      draw_char(line, col)
    end

    core.pop_clip_rect()
  end

  caret_idx = caret_idx + 1
end