aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrxi <rxi@users.noreply.github.com>2020-05-18 20:39:59 +0100
committerrxi <rxi@users.noreply.github.com>2020-05-18 20:39:59 +0100
commitfeb990fb2eaa5ce8e2f473db9a581722ce26735a (patch)
tree913bc18a43cd0a355d893049ac9e4b44ed63bcf9
parentf2e5821e22b5f813fae1b6d3087b3d75c82f438b (diff)
downloadlite-xl-plugins-feb990fb2eaa5ce8e2f473db9a581722ce26735a.tar.gz
lite-xl-plugins-feb990fb2eaa5ce8e2f473db9a581722ce26735a.zip
Added plugins/markers.lua
Resolves #27
-rw-r--r--README.md1
-rw-r--r--plugins/markers.lua103
2 files changed, 104 insertions, 0 deletions
diff --git a/README.md b/README.md
index f5f095f..141864d 100644
--- a/README.md
+++ b/README.md
@@ -43,6 +43,7 @@ Plugin | Description
[`lineguide`](plugins/lineguide.lua?raw=1) | Displays a line-guide at the line limit offset *([screenshot](https://user-images.githubusercontent.com/3920290/81476159-2cf70000-9208-11ea-928b-9dae3884c477.png))*
[`linter`](https://github.com/drmargarido/linters)* | Linters for multiple languages
[`macmodkeys`](plugins/macmodkeys.lua?raw=1) | Remaps mac modkeys `command/option` to `ctrl/alt`
+[`markers`](plugins/markers.lua?raw=1) | Add markers to docs and jump between them quickly *([screenshot](https://user-images.githubusercontent.com/3920290/82252149-5faaa200-9946-11ea-9199-bea2efb7ee23.png))*
[`openfilelocation`](plugins/openfilelocation.lua?raw=1) | Opens the parent directory of the current file in the file manager
[`openselected`](plugins/openselected.lua?raw=1) | Opens the selected filename or url
[`selectionhighlight`](plugins/selectionhighlight.lua?raw=1) | Highlights regions of code that match the current selection *([screenshot](https://user-images.githubusercontent.com/3920290/80710883-5f597c80-8ae7-11ea-97f0-76dfacc08439.png))*
diff --git a/plugins/markers.lua b/plugins/markers.lua
new file mode 100644
index 0000000..6e397d7
--- /dev/null
+++ b/plugins/markers.lua
@@ -0,0 +1,103 @@
+-- Markers plugin for lite text editor
+-- original implementation by Petri Häkkinen
+
+local core = require "core"
+local command = require "core.command"
+local keymap = require "core.keymap"
+local style = require "core.style"
+local DocView = require "core.docview"
+local Doc = require "core.doc"
+
+local cache = {} -- this table contains subtables for each document, each subtable is a set of line numbers
+setmetatable(cache, {
+ __mode = "k",
+ __index = function(t, k)
+ t[k] = {}
+ return t[k]
+ end,
+})
+
+
+local function shift_lines(doc, at, diff)
+ if diff == 0 then return end
+ local t = {}
+ for line in pairs(cache[doc]) do
+ line = line >= at and line + diff or line
+ t[line] = true
+ end
+ cache[doc] = t
+end
+
+
+local raw_insert = Doc.raw_insert
+
+function Doc:raw_insert(line, col, text, ...)
+ raw_insert(self, line, col, text, ...)
+ local line_count = 0
+ for _ in text:gmatch("\n") do
+ line_count = line_count + 1
+ end
+ shift_lines(self, line, line_count)
+end
+
+
+local raw_remove = Doc.raw_remove
+
+function Doc:raw_remove(line1, col1, line2, col2, ...)
+ raw_remove(self, line1, col1, line2, col2, ...)
+ shift_lines(self, line2, line1 - line2)
+end
+
+
+local draw_line_gutter = DocView.draw_line_gutter
+
+function DocView:draw_line_gutter(idx, x, y)
+ if cache[self.doc] and cache[self.doc][idx] then
+ local h = self:get_line_height()
+ renderer.draw_rect(x, y, style.padding.x * 0.4, h, style.selection)
+ end
+ draw_line_gutter(self, idx, x, y)
+end
+
+
+command.add("core.docview", {
+ ["markers:toggle-marker"] = function()
+ local doc = core.active_view.doc
+ local line = doc:get_selection()
+ local markers = cache[doc]
+
+ if markers[line] then
+ markers[line] = nil
+ else
+ markers[line] = true
+ end
+ end,
+
+ ["markers:go-to-next-marker"] = function()
+ local doc = core.active_view.doc
+ local line = doc:get_selection()
+ local markers = cache[doc]
+
+ local first_marker = math.huge
+ local next_marker = math.huge
+ for l, _ in pairs(markers) do
+ if l > line and l < next_marker then
+ next_marker = l
+ end
+ first_marker = math.min(first_marker, l)
+ end
+ if next_marker == math.huge then
+ next_marker = first_marker
+ end
+ if next_marker ~= math.huge then
+ doc:set_selection(next_marker, 1)
+ core.active_view:scroll_to_line(next_marker, true)
+ end
+ end,
+})
+
+
+keymap.add {
+ ["ctrl+f2"] = "markers:toggle-marker",
+ ["f2"] = "markers:go-to-next-marker",
+}