From 3d099d5157ce479b6a15e30ff2efbe1ee0d377c3 Mon Sep 17 00:00:00 2001 From: Raymaekers Luca Date: Thu, 20 Jun 2024 22:30:24 +0200 Subject: checkpoint --- config/essentials/vis/Makefile | 16 +++ config/essentials/vis/cursors.lua | 122 +++++++++++++++++++++ config/essentials/vis/plugins/init.lua | 2 - config/essentials/vis/plugins/vis-cursors | 1 - .../essentials/vis/plugins/vis-snippets/init.lua | 0 config/essentials/vis/plugins/vis-title | 1 - config/essentials/vis/title.lua | 22 ++++ config/essentials/vis/visrc.lua | 69 +++++++----- 8 files changed, 202 insertions(+), 31 deletions(-) create mode 100644 config/essentials/vis/Makefile create mode 100644 config/essentials/vis/cursors.lua delete mode 100644 config/essentials/vis/plugins/init.lua delete mode 160000 config/essentials/vis/plugins/vis-cursors delete mode 100644 config/essentials/vis/plugins/vis-snippets/init.lua delete mode 160000 config/essentials/vis/plugins/vis-title create mode 100644 config/essentials/vis/title.lua (limited to 'config') diff --git a/config/essentials/vis/Makefile b/config/essentials/vis/Makefile new file mode 100644 index 0000000..5f41043 --- /dev/null +++ b/config/essentials/vis/Makefile @@ -0,0 +1,16 @@ +.PHONY: check format check-luacheck check-format + +LUA_FILES := $(shell find -name "*.lua") + +check: check-luacheck + +all: check check-format + +check-luacheck: + luacheck --globals=vis -- $(LUA_FILES) + +check-format: + set -e; for lf in $(LUA_FILES); do tools/check-format "$${lf}"; done + +format: + lua-format -i $(LUA_FILES) diff --git a/config/essentials/vis/cursors.lua b/config/essentials/vis/cursors.lua new file mode 100644 index 0000000..a4e7fa3 --- /dev/null +++ b/config/essentials/vis/cursors.lua @@ -0,0 +1,122 @@ +local M = {} +local cursors = {} +local files = {} + +-- default maxsize +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') + local BASE = XDG_CACHE_HOME or HOME + return BASE .. '/.vis-cursors' +end + +-- default save path +M.path = get_default_cache_path() + +local read_files = function() + + -- read file + local file = io.open(M.path) + if file == nil then + return + end + + files = {} + + -- read positions per file path + for line in file:lines() do + for path, pos in string.gmatch(line, '(.+)[,%s](%d+)') do + cursors[path] = pos + table.insert(files, path) + end + end + + file:close() +end + +-- read cursors from file on init +local on_init = function() + read_files() +end + +-- apply cursor pos on win open +local on_win_open = function(win) + + if win.file == nil or win.file.path == nil then + return + end + + -- init cursor path if nil + local pos = cursors[win.file.path] + if pos == nil then + cursors[win.file.path] = win.selection.pos + return + end + + -- set current cursor + win.selection.pos = tonumber(pos) + + -- center view around cursor + vis:feedkeys("zz") +end + +-- set cursor pos on close +local on_win_close = function(win) + + if win.file == nil or win.file.path == nil then + return + end + + -- re-read files in case they've changed + read_files() + + -- remove old occurences of current path + for i, path in ipairs(files) do + if path == win.file.path then + table.remove(files, i) + end + end + + -- ignore files with cursor at the beginning + if win.selection.pos == 0 then + return + end + + + -- insert current path to top of files + table.insert(files, 1, win.file.path) + + -- set cursor pos for current file path + cursors[win.file.path] = win.selection.pos +end + +-- write cursors to file on quit +local on_quit = function() + + local file = io.open(M.path, 'w+') + if file == nil then + return + end + + -- buffer cursors string + local buffer = {} + for _, path in ipairs(files) do + table.insert(buffer, string.format('%s,%d', path, cursors[path])) + if M.maxsize and #buffer >= M.maxsize then + break + end + end + local output = table.concat(buffer, '\n') + file:write(output) + file:close() +end + +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 diff --git a/config/essentials/vis/plugins/init.lua b/config/essentials/vis/plugins/init.lua deleted file mode 100644 index 80c9280..0000000 --- a/config/essentials/vis/plugins/init.lua +++ /dev/null @@ -1,2 +0,0 @@ -require("plugins.vis-cursors") -require("plugins.vis-title") diff --git a/config/essentials/vis/plugins/vis-cursors b/config/essentials/vis/plugins/vis-cursors deleted file mode 160000 index f86c584..0000000 --- a/config/essentials/vis/plugins/vis-cursors +++ /dev/null @@ -1 +0,0 @@ -Subproject commit f86c584fc2d4a2bab47df0cd5d187dd81fb71856 diff --git a/config/essentials/vis/plugins/vis-snippets/init.lua b/config/essentials/vis/plugins/vis-snippets/init.lua deleted file mode 100644 index e69de29..0000000 diff --git a/config/essentials/vis/plugins/vis-title b/config/essentials/vis/plugins/vis-title deleted file mode 160000 index 9c808f7..0000000 --- a/config/essentials/vis/plugins/vis-title +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 9c808f7e71b43aca31dee8553dcfce2214d7fc40 diff --git a/config/essentials/vis/title.lua b/config/essentials/vis/title.lua new file mode 100644 index 0000000..c87088b --- /dev/null +++ b/config/essentials/vis/title.lua @@ -0,0 +1,22 @@ +require('vis') + +local function set_title(title) + -- print() cannot be used here as it will mess up vis + vis:command(string.format(":!printf '\\033]2;vis %s\\007'", title)) +end + +vis.events.subscribe(vis.events.INIT, function() + print('\27[22;2t') +end) + +vis.events.subscribe(vis.events.WIN_OPEN, function(win) + set_title(win.file.name or '[No Name]') +end) + +vis.events.subscribe(vis.events.FILE_SAVE_POST, function(file) + set_title(file.name) +end) + +vis.events.subscribe(vis.events.QUIT, function() + print('\27[23;2t') +end) diff --git a/config/essentials/vis/visrc.lua b/config/essentials/vis/visrc.lua index 81e09eb..05df7ee 100644 --- a/config/essentials/vis/visrc.lua +++ b/config/essentials/vis/visrc.lua @@ -3,50 +3,41 @@ ------------------------------------ require('vis') -require('plugins') ------------------------------------- ---- EVENTS ------------------------------------- +-- plugins +require("backup") +require("cursors") +require("title") -vis.events.subscribe(vis.events.INIT, function() - vis.options.ignorecase = true - vis.options.autoindent = true - vis.options.shell = "/bin/sh" - theme = "nord" - vis:command("set theme " .. theme) -end) - -vis.events.subscribe(vis.events.WIN_OPEN, function(win) -- luacheck: no unused args - win.options.relativenumbers = true -end) ------------------------------------ --- FUNCTIONS ------------------------------------ -function map_cmd(mode, map, command, help) - vis:map(mode, map, function() - vis:command(command) - end, help) +local map_cmd = function(mode, map, command, help) + vis:map(mode, map, function() vis:command(command) end, help) end -function map_cmd_restore(mode, map, command, help) +local map_cmd_restore = function(mode, map, command, help) vis:map(mode, map, function() if (mode == vis.modes.INSERT) then vis:feedkeys("") end - + vis:feedkeys("m") vis:command(command) vis:feedkeys("M") if (mode == vis.modes.INSERT) then vis:feedkeys("i") - end + end end, help) end +local map_keys = function(mode, map, keys, help) + vis:map(mode, map, function() vis:feedkeys(keys) end, help) +end + ------------------------------------ --- VARIABLES ------------------------------------ @@ -55,11 +46,9 @@ local m = vis.modes ------------------------------------ --- COMMANDS ------------------------------------- +----------------------------------- -vis:command_register("Q", function(argv, force, win, selection, range) - vis:command("qa!") -end, "Quit all") +vis:command_register("Q", function() vis:command("qa!") end, "Quit all") ------------------------------------- --- MAPPINGS @@ -77,7 +66,33 @@ vis:map(m.NORMAL, " eh", function() vis:command("!lowdown $vis_filepath > ${vis_filepath%.md}.html") vis:info("exported.") end, "Export markdown to html") -vis:map(m.NORMAL, " nl", function() vis:feedkeys(":", "Remove trailing whitespace") -- select markdown list element: ,x/^(\d+\.|[-*])\s+.+\n(^ .+\n)*/ + + + +------------------------------------ +--- EVENTS +------------------------------------ + +vis.events.subscribe(vis.events.INIT, function() + vis.options.ignorecase = true + vis.options.autoindent = true + vis.options.shell = "/bin/sh" + local theme = "nord" + vis:command("set theme " .. theme) +end) + +vis.events.subscribe(vis.events.WIN_OPEN, function(win) -- luacheck: no unused args + win.options.relativenumbers = true + + if win.syntax == "bash" then + map_keys(m.NORMAL, " v", + "V:x/^(\\s*)(.+)$/ c/\\1>\\&2 printf '\\2: %s\\\\n' \"$\\2\"/", "Print variable") + end + +end) + -- cgit v1.2.3