diff options
Diffstat (limited to 'configs/nvim/lua')
-rw-r--r-- | configs/nvim/lua/crupest/nvim.lua | 120 | ||||
-rw-r--r-- | configs/nvim/lua/crupest/system/fs.lua | 58 | ||||
-rw-r--r-- | configs/nvim/lua/crupest/table.lua | 2 |
3 files changed, 178 insertions, 2 deletions
diff --git a/configs/nvim/lua/crupest/nvim.lua b/configs/nvim/lua/crupest/nvim.lua index 67bb3b0..44c9b5f 100644 --- a/configs/nvim/lua/crupest/nvim.lua +++ b/configs/nvim/lua/crupest/nvim.lua @@ -1,3 +1,5 @@ +local fs = require("crupest.system.fs") + local function list_listed_bufs() local bufs = vim.api.nvim_list_bufs() local result = {} @@ -49,13 +51,17 @@ local function list_wins_editing_buf(buf) return result end +local function buf_is_normal(buf) + return vim.fn.bufexists(buf) ~= 0 and vim.fn.buflisted(buf) ~= 0 +end + -- Delete current buffer and jump back. -- If no previous jump, switch to previous buffer. -- If no previous buffer (no other buffers), create a unnamed one. (So the window does not quit.) local function win_close_buf() local buf = vim.api.nvim_get_current_buf() - if vim.fn.bufexists(buf) == 0 or vim.fn.buflisted(buf) == 0 then + if not buf_is_normal(buf) then return end @@ -99,11 +105,123 @@ local function close_float() end end +local function full_path(name) + return vim.fn.fnamemodify(name, ":p:gs?\\?/?") +end + +local function coerce_path_for_dir(old, new) + if fs.isdir(new) then + return new .. "/" .. vim.fn.fnamemodify(old, ":t") + end + return new +end + +local function do_mv_file(old, new, overwrite) + new = coerce_path_for_dir(old, new) + + if full_path(old) == full_path(new) then + vim.notify("Paths are identical. Do nothing.", vim.log.levels.WARN) + return false + end + + if not fs.isfile(old) then + vim.notify("Not exists or not a file. Can't move.", vim.log.levels.ERROR) + return false + end + + if not overwrite and fs.exist(new) then + vim.notify("Target path exists.", vim.log.levels.ERROR) + return false + end + + fs.move(old, new) + vim.notify("File moved.") + + return new +end + +local function mv_file(old, new, overwrite) + new = do_mv_file(old, new, overwrite) + if not new then return end + + local bufs = list_listed_bufs() + for _, b in ipairs(bufs) do + if full_path(vim.api.nvim_buf_get_name(b)) == full_path(old) then + vim.api.nvim_buf_set_name(b, new) + end + end +end + +local function mv_buf_file(buf, new, overwrite) + if not buf_is_normal(buf) then + vim.notify("Buf is not a normal buffer, can't move it.", vim.log.levels.ERROR) + return + end + + local name = vim.api.nvim_buf_get_name(buf) + + new = do_mv_file(name, new, overwrite) + if not new then return end + + vim.api.nvim_buf_set_name(buf, new) +end + +local function mv_dir(old_dir, new_dir, overwrite) + new_dir = coerce_path_for_dir(old_dir, new_dir) + + if full_path(old_dir) == full_path(new_dir) then + vim.notify("Paths are identical. Do nothing.", vim.log.levels.WARN) + return + end + + if not fs.isdir(old_dir) then + vim.notify("Not exist or not a dir. Can't move.", vim.log.levels.ERROR) + end + + if not overwrite and fs.exist(new_dir) then + vim.notify("Target path exists.", vim.log.levels.ERROR) + return + end + + if fs.isdir(old_dir) then + fs.move(old_dir, new_dir) + vim.notify("Dir moved.") + end + + local bufs = list_listed_bufs() + + for _, buf in ipairs(bufs) do + local name = vim.api.nvim_buf_get_name(buf) + local full_name = full_path(name) + local old_dir_full = full_path(old_dir) + if string.find(full_name, old_dir_full, 1, true) == 1 then + local new_name = new_dir .. string.sub(full_name, #old_dir_full + 1) + vim.api.nvim_buf_set_name(buf, new_name) + end + end +end + +local function rename_file(old, new, overwrite) + local dir = vim.fn.fnamemodify(old, ":h") + mv_file(old, dir .. "/" .. new, overwrite) +end + +local function rename_buf_file(buf, new_name, overwrite) + local old_path = vim.api.nvim_buf_get_name(buf) + local dir = vim.fn.fnamemodify(old_path, ":h") + mv_buf_file(buf, dir .. "/" .. new_name, overwrite) +end + return { list_listed_bufs = list_listed_bufs, get_previous_buffer = get_previous_buffer, list_wins_editing_buf = list_wins_editing_buf, win_close_buf = win_close_buf, close_float = close_float, + mv_file = mv_file, + mv_buf_file = mv_buf_file, + mv_dir = mv_dir, + rename_file = rename_file, + rename_buf_file = rename_buf_file } diff --git a/configs/nvim/lua/crupest/system/fs.lua b/configs/nvim/lua/crupest/system/fs.lua new file mode 100644 index 0000000..25ce02c --- /dev/null +++ b/configs/nvim/lua/crupest/system/fs.lua @@ -0,0 +1,58 @@ +local function exist(path) + return vim.uv.fs_stat(path) +end + +local function isfile(path) + local s = vim.uv.fs_stat(path) + if not s then return false end + return s.type == "file" +end + +local function isdir(path) + local s = vim.uv.fs_stat(path) + if not s then return false end + return s.type == "directory" +end + +local function mkdir(dir) + local parents = {} + require("crupest/system").walk_up(dir, function(p) + table.insert(parents, 1, p) + end) + + for _, v in ipairs(parents) do + if exist(v) and not isdir(v) then + vim.notify(v.." is not a dir. Can't make dir "..dir, vim.log.levels.ERROR) + return + end + if not exist(v) then + vim.notify("Creating dir "..v) + assert(vim.uv.fs_mkdir(v, 504)) -- mode = 0770 + end + end +end + +local function copy(old, new) + mkdir(vim.fn.fnamemodify(new, ":p:h")) + assert(vim.uv.fs_copyfile(old, new)) +end + +local function remove(path) + assert(vim.uv.fs_unlink(path)) +end + +local function move(old, new) + mkdir(vim.fn.fnamemodify(new, ":p:h")) + assert(vim.uv.fs_rename(old, new)) +end + +return { + exist = exist, + isfile = isfile, + isdir = isdir, + mkdir = mkdir, + copy = copy, + remove = remove, + move = move +} + diff --git a/configs/nvim/lua/crupest/table.lua b/configs/nvim/lua/crupest/table.lua index 6807151..ab069ce 100644 --- a/configs/nvim/lua/crupest/table.lua +++ b/configs/nvim/lua/crupest/table.lua @@ -25,6 +25,6 @@ end return { remove_element = remove_element, - element_at = element_at + element_at = element_at, } |