diff options
author | Erlend Lind Madsen <erlendf80@gmail.com> | 2022-01-23 22:57:27 +0100 |
---|---|---|
committer | Erlend Lind Madsen <erlendf80@gmail.com> | 2022-01-23 22:57:27 +0100 |
commit | 2bb11524d3863be7d6ea796cf2ba0142e59be9d7 (patch) | |
tree | 327b719b0333a4c89b0d7dedd97a2ec370b42b58 | |
parent | 92db312669fa6ca3d70e30467f22ffa2b18a6987 (diff) | |
download | vis-cursors-2bb11524d3863be7d6ea796cf2ba0142e59be9d7.tar.gz |
Limit max number of files to maxsize (1000)
The paths in .vis-cursors are now sorted in a least used order and limited
to a maxsize, currently set to 1000, but can however be changed using M.maxsize.
Based on PR by @leorosa :
https://github.com/erf/vis-cursors/pull/8
-rw-r--r-- | README.md | 7 | ||||
-rw-r--r-- | init.lua | 60 |
2 files changed, 50 insertions, 17 deletions
@@ -2,6 +2,11 @@ A [vis](https://github.com/martanne/vis) [plugin](https://github.com/martanne/vis/wiki/Plugins) for saving cursor position per file. +The file positions are ordered by latest used first. + +You can set a `maxsize` which defaults to 1000 files. + Default save path is `{XDG_CACHE_HOME|HOME}/.vis-cursors` -Set a custom path with `M.path`
\ No newline at end of file +Set a custom path with `M.path` + @@ -1,6 +1,9 @@ local M = {} local cursors = {} +local files = {} +M.maxsize = 1000 +-- get the default system cache directory local get_default_cache_path = function() local HOME = os.getenv('HOME') local XDG_CACHE_HOME = os.getenv('XDG_CACHE_HOME') @@ -10,33 +13,59 @@ end M.path = get_default_cache_path() -local read_cursors = function() +-- read cursors from file on init +local on_init = function() + + -- read file local f = io.open(M.path) if f == nil then return end + -- read positions per file path for line in f:lines() do for path, pos in string.gmatch(line, '(.+)[,%s](%d+)') do cursors[path] = pos + table.insert(files, path) end end + f:close() end -local apply_cursor_pos = function(win) +-- apply cursor pos on win open +local on_win_open = function(win) + if win.file == nil or win.file.path == nil then return end + + -- remove old occurences of path + for i, path in ipairs(files) do + if path == win.file.path then + table.remove(files, i) + break + end + end + + -- insert current path to top of files + table.insert(files, 1, win.file.path) + + -- local pos = cursors[win.file.path] if pos == nil then + cursors[win.file.path] = win.selection.pos return end + win.selection.pos = tonumber(pos) + vis:feedkeys("zz") end -local set_cursor_pos = function(win) +-- set cursor pos on close +local on_win_close = function(win) + if win.file == nil or win.file.path == nil then return end @@ -44,33 +73,32 @@ local set_cursor_pos = function(win) cursors[win.file.path] = win.selection.pos end -local write_cursors = function() +-- write cursors to file on quit +local on_quit = function() local f = io.open(M.path, 'w+') if f == nil then return end - -- sort paths - local paths = {} - for path in pairs(cursors) do - table.insert(paths, path) - end - table.sort(paths) - -- buffer cursors string local t = {} - for i, path in ipairs(paths) do + local n = 0 + for i, path in ipairs(files) do table.insert(t, string.format('%s,%d', path, cursors[path])) + n = n + 1 + if M.maxsize and n >= M.maxsize then + break + end end local s = table.concat(t, '\n') f:write(s) f:close() end -vis.events.subscribe(vis.events.INIT, read_cursors) -vis.events.subscribe(vis.events.WIN_OPEN, apply_cursor_pos) -vis.events.subscribe(vis.events.WIN_CLOSE, set_cursor_pos) -vis.events.subscribe(vis.events.QUIT, write_cursors) +vis.events.subscribe(vis.events.INIT, on_init) +vis.events.subscribe(vis.events.WIN_OPEN, on_win_open) +vis.events.subscribe(vis.events.WIN_CLOSE, on_win_close) +vis.events.subscribe(vis.events.QUIT, on_quit) return M |