diff options
author | max397574 <[email protected]> | 2021-12-09 08:35:00 +0100 |
---|---|---|
committer | max397574 <[email protected]> | 2021-12-09 08:35:00 +0100 |
commit | 9d289800efe181306eeeebe9682b4c9f648ac70d (patch) | |
tree | 053247dc00e67c42535fd91596f81060c4e4fdcf /lua | |
parent | 7ee7c54a4068b632752d7c55a4a110b4484b6e15 (diff) |
feat(oldfiles): added mapping 0-9 for last files
Diffstat (limited to 'lua')
-rw-r--r-- | lua/startup/themes/startify.lua | 4 | ||||
-rw-r--r-- | lua/startup/utils.lua | 36 |
2 files changed, 35 insertions, 5 deletions
diff --git a/lua/startup/themes/startify.lua b/lua/startup/themes/startify.lua index 915a0f5..311b30a 100644 --- a/lua/startup/themes/startify.lua +++ b/lua/startup/themes/startify.lua @@ -47,6 +47,7 @@ for _, line in ipairs(cow) do end -- NOTE: lua dump(vim.fn.expand("#<1")) to get newest oldfile + local settings = { header = { type = "text", @@ -93,12 +94,13 @@ local settings = { options = { after = function() require("startup").create_mappings(user_bookmark_mappings) + require("startup.utils").oldfiles_mappings() end, mapping_keys = false, cursor_column = 0.25, empty_line_between_mappings = false, disable_statuslines = true, - paddings = { 1, 2, 2, 3 }, + paddings = { 1, 1, 1, 1 }, }, mappings = { execute_command = "<CR>", diff --git a/lua/startup/utils.lua b/lua/startup/utils.lua index f6db9ee..bfe74fc 100644 --- a/lua/startup/utils.lua +++ b/lua/startup/utils.lua @@ -4,6 +4,9 @@ local flag = false local new_cursor_pos local help_window +local oldfiles_total = 0 +local all_oldfiles = {} + local set_buf_opt = vim.api.nvim_buf_set_option local line_count = function() @@ -174,7 +177,7 @@ end ---@return table oldfiles table with all the oldfiles in it function U.get_oldfiles(amount) local home = vim.fn.expand("~") - local oldfiles = { "Last files", "" } + local oldfiles = {} local oldfiles_raw = vim.fn.execute("oldfiles") local oldfiles_amount = 0 for file in oldfiles_raw:gmatch("[^\n]+") do @@ -182,13 +185,21 @@ function U.get_oldfiles(amount) break end table.insert(oldfiles, (string.sub(file, 4, -1))) + table.insert(all_oldfiles, (string.sub(file, 4, -1))) oldfiles_amount = oldfiles_amount + 1 end local oldfiles_shortened = {} for _, file in ipairs(oldfiles) do - oldfiles_shortened[#oldfiles_shortened+1]=string.gsub(file, home, "~") + if oldfiles_total < 10 then + oldfiles_shortened[#oldfiles_shortened+1]="["..oldfiles_total.."] "..string.gsub(file, home, "~") + else + oldfiles_shortened[#oldfiles_shortened+1]=string.gsub(file, home, "~") + end + oldfiles_total = oldfiles_total+1 end oldfiles = oldfiles_shortened + table.insert(oldfiles,1,"Last Files:") + table.insert(oldfiles,2,"") local length = U.longest_line(oldfiles) + 2 local oldfiles_aligned = {} @@ -206,19 +217,27 @@ function U.get_oldfiles_directory(amount) local oldfiles_raw = vim.fn.execute("oldfiles") local oldfiles_amount = 0 local directory = vim.api.nvim_exec([[pwd]], true) - local oldfiles = { "Last files in " .. directory, " " } + local oldfiles = {} for file in oldfiles_raw:gmatch(directory .. "[^\n]+") do if oldfiles_amount >= amount then break end table.insert(oldfiles, (string.sub(file, #directory + 1, -1))) + table.insert(all_oldfiles, (string.sub(file, 4, -1))) oldfiles_amount = oldfiles_amount + 1 end local oldfiles_shortened = {} for _, file in ipairs(oldfiles) do - oldfiles_shortened[#oldfiles_shortened+1]=string.gsub(file, home, "~") + if oldfiles_total < 10 then + oldfiles_shortened[#oldfiles_shortened+1]="["..oldfiles_total.."] "..string.gsub(file, home, "~") + else + oldfiles_shortened[#oldfiles_shortened+1]=string.gsub(file, home, "~") + end + oldfiles_total = oldfiles_total+1 end oldfiles = oldfiles_shortened + table.insert(oldfiles,1,"Last Files in "..directory..":") + table.insert(oldfiles,2,"") local length = U.longest_line(oldfiles) + 2 local oldfiles_aligned = {} @@ -228,6 +247,15 @@ function U.get_oldfiles_directory(amount) return oldfiles_aligned end +function U.oldfiles_mappings() + if not all_oldfiles then + return + end + for i = 0, 9, 1 do + vim.api.nvim_buf_set_keymap(0, "n", tostring(i), "<cmd>e "..all_oldfiles[i+1].."<CR>", {noremap = true,silent = true}) + end +end + ---return column on which cursor should be positioned local column = function() local settings = require("startup").settings |