diff options
| author | Raymaekers Luca <raymaekers.luca@gmail.com> | 2024-06-20 22:30:24 +0200 | 
|---|---|---|
| committer | Raymaekers Luca <raymaekers.luca@gmail.com> | 2024-06-20 22:30:24 +0200 | 
| commit | 3d099d5157ce479b6a15e30ff2efbe1ee0d377c3 (patch) | |
| tree | c015e0fc9322fef2c5f2d826d9b4e2a4761a8ee3 | |
| parent | 41d3ecdd5245c8c3f8adaa76bbdca24eacd9e007 (diff) | |
checkpoint
| -rw-r--r-- | config/essentials/vis/Makefile | 16 | ||||
| -rw-r--r-- | config/essentials/vis/cursors.lua | 122 | ||||
| -rw-r--r-- | config/essentials/vis/plugins/init.lua | 2 | ||||
| m--------- | config/essentials/vis/plugins/vis-cursors | 0 | ||||
| -rw-r--r-- | config/essentials/vis/plugins/vis-snippets/init.lua | 0 | ||||
| m--------- | config/essentials/vis/plugins/vis-title | 0 | ||||
| -rw-r--r-- | config/essentials/vis/title.lua | 22 | ||||
| -rw-r--r-- | config/essentials/vis/visrc.lua | 69 | 
8 files changed, 202 insertions, 29 deletions
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 -Subproject f86c584fc2d4a2bab47df0cd5d187dd81fb7185 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 --- a/config/essentials/vis/plugins/vis-snippets/init.lua +++ /dev/null diff --git a/config/essentials/vis/plugins/vis-title b/config/essentials/vis/plugins/vis-title deleted file mode 160000 -Subproject 9c808f7e71b43aca31dee8553dcfce2214d7fc4 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("<Escape>")  		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(":<seq -f '%0.0f. ' 1 ") end, "Insert numbered list") +map_keys(m.NORMAL, " nl", ":<seq -f '%0.0f. ' 1 ", "Insert numbered list") +map_keys(m.NORMAL, " ws", ":,x/[ \t]+$|^[ \t]+$/d<Enter>", "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\"/<Enter><Escape>", "Print variable") +	end + +end) +  | 
