aboutsummaryrefslogtreecommitdiff
path: root/plugins/selectionhighlight.lua
diff options
context:
space:
mode:
authorrxi <rxi@users.noreply.github.com>2020-04-29 08:33:41 +0100
committerrxi <rxi@users.noreply.github.com>2020-04-29 08:34:01 +0100
commit83b5b6d5dac4c6250de1ce0c68018d26bffd634d (patch)
tree6611141bbc381759651c088345150596f244a845 /plugins/selectionhighlight.lua
parent4e8373c9d60607b7dbdc5c860be4eae6ba84ac15 (diff)
downloadlite-xl-plugins-83b5b6d5dac4c6250de1ce0c68018d26bffd634d.tar.gz
lite-xl-plugins-83b5b6d5dac4c6250de1ce0c68018d26bffd634d.zip
Added `selectionhighlight` plugin
Diffstat (limited to 'plugins/selectionhighlight.lua')
-rw-r--r--plugins/selectionhighlight.lua26
1 files changed, 26 insertions, 0 deletions
diff --git a/plugins/selectionhighlight.lua b/plugins/selectionhighlight.lua
new file mode 100644
index 0000000..31a5042
--- /dev/null
+++ b/plugins/selectionhighlight.lua
@@ -0,0 +1,26 @@
+local style = require "core.style"
+local DocView = require "core.docview"
+
+-- originally written by luveti
+
+local draw_line_body = DocView.draw_line_body
+
+function DocView:draw_line_body(idx, x, y)
+ local line1, col1, line2, col2 = self.doc:get_selection(true)
+ if line1 == line2 and col1 ~= col2 then
+ local lh = self:get_line_height()
+ local selected_text = self:get_cached_line(line1).text:sub(col1, col2 - 1)
+ local current_line_text = self:get_cached_line(idx).text
+ local last_col = 1
+ while true do
+ local start_col, end_col = current_line_text:find(selected_text, last_col, true)
+ if start_col == nil then break end
+ local x1 = x + self:get_col_x_offset(idx, start_col)
+ local x2 = x + self:get_col_x_offset(idx, end_col + 1)
+ renderer.draw_rect(x1, y, x2 - x1, lh, style.selection)
+ last_col = end_col + 1
+ end
+ end
+ draw_line_body(self, idx, x, y)
+end
+