diff options
| author | srdusr <trevorgray@srdusr.com> | 2023-10-22 22:48:45 +0200 |
|---|---|---|
| committer | srdusr <trevorgray@srdusr.com> | 2023-10-22 22:48:45 +0200 |
| commit | bf45ef759815ad7ac3f6423114a96f4d56d1a2a2 (patch) | |
| tree | 5bb05a59fd6c5e5e525974fc537ee10a2da58883 /.config/nvim | |
| parent | 45ce82be9445460f1ee77251d7c7ceef80a924a2 (diff) | |
| parent | c287afaf5e62445efa39a052e9241c05700714e9 (diff) | |
| download | dotfiles-bf45ef759815ad7ac3f6423114a96f4d56d1a2a2.tar.gz dotfiles-bf45ef759815ad7ac3f6423114a96f4d56d1a2a2.zip | |
Merge commit 'c46609533bab420a02dc289adb5f9e2101e8ee31'
Diffstat (limited to '.config/nvim')
| -rw-r--r-- | .config/nvim/lua/plugins/telescope.lua | 30 | ||||
| -rw-r--r-- | .config/nvim/lua/user/mods.lua | 97 |
2 files changed, 81 insertions, 46 deletions
diff --git a/.config/nvim/lua/plugins/telescope.lua b/.config/nvim/lua/plugins/telescope.lua index cf0864a..daf50bd 100644 --- a/.config/nvim/lua/plugins/telescope.lua +++ b/.config/nvim/lua/plugins/telescope.lua @@ -367,7 +367,7 @@ function M.find_scripts() prompt_title = ' Find Scripts', path_display = { 'smart' }, search_dirs = { - '~/.local/bin/scripts', + '~/.scripts', }, layout_strategy = 'horizontal', layout_config = { preview_width = 0.65, width = 0.75 }, @@ -579,18 +579,17 @@ function M.find_dirs() -- Use vim.fn.expand() to get an absolute path local root_path = vim.fn.expand(root_dir) - local subdirs = vim.fn.readdir(root_path) - if subdirs then - for _, subdir in ipairs(subdirs) do - if vim.fn.isdirectory(root_path .. '/' .. subdir) == 1 then - table.insert(entries, subdir) - end + local subentries = vim.fn.readdir(root_path) + if subentries then + for _, subentry in ipairs(subentries) do + local absolute_path = root_path .. '/' .. subentry + table.insert(entries, subentry) end end pickers .new({}, { - prompt_title = 'Change Directory', + prompt_title = 'Change Directory or Open File', finder = finders.new_table({ results = entries, }), @@ -600,13 +599,16 @@ function M.find_dirs() actions_set.select:replace(function() local entry = actions_state.get_selected_entry() if entry ~= nil then - local selected_subdir = entry.value + local selected_entry = entry.value actions.close(prompt_bufnr, false) - local selected_path = root_path .. '/' .. selected_subdir - vim.fn.chdir(selected_path) - vim.cmd('e .') - vim.cmd("echon ''") - print('cwd: ' .. vim.fn.getcwd()) + local selected_path = root_path .. '/' .. selected_entry + if vim.fn.isdirectory(selected_path) == 1 then + vim.fn.chdir(selected_path) + vim.cmd('e .') + print('cwd: ' .. vim.fn.getcwd()) + else + vim.cmd('e ' .. selected_path) + end end end) return true diff --git a/.config/nvim/lua/user/mods.lua b/.config/nvim/lua/user/mods.lua index b6431a6..84498bb 100644 --- a/.config/nvim/lua/user/mods.lua +++ b/.config/nvim/lua/user/mods.lua @@ -609,12 +609,22 @@ end -------------------------------------------------- ---- Function to create or toggle a scratch buffer -local scratch_buf = nil -- Store the scratch buffer globally -local scratch_win = nil -- Store the scratch window globally +-- Define global variables to store the scratch buffer and window +local scratch_buf = nil +local scratch_win = nil + +-- Other global variables local scratch_date = os.date('%Y-%m-%d') local scratch_dir = vim.fn.expand('~/notes/private') local scratch_file = 'scratch-' .. scratch_date .. '.md' +-- Function to close and delete a buffer +function CloseAndDeleteBuffer(bufnr) + if bufnr and vim.api.nvim_buf_is_valid(bufnr) then + vim.api.nvim_command('silent! bwipe ' .. bufnr) + end +end + function M.Scratch(Split_direction) -- Check if the directory exists, and create it if it doesn't if vim.fn.isdirectory(scratch_dir) == 0 then @@ -638,10 +648,12 @@ function M.Scratch(Split_direction) WriteScratchBufferToFile(scratch_buf, file_path) vim.cmd(':w!') vim.api.nvim_win_close(scratch_win, true) + CloseAndDeleteBuffer(scratch_buf) scratch_win = nil + scratch_buf = nil else if scratch_buf and vim.api.nvim_buf_is_valid(scratch_buf) then - -- Buffer exists, reuse it + -- Buffer exists, reuse it and open a new window OpenScratchWindow(scratch_buf, current_window_type) else -- Buffer doesn't exist, create it and load the file if it exists @@ -651,19 +663,23 @@ function M.Scratch(Split_direction) end end +-- Function to write buffer contents to a file function WriteScratchBufferToFile(buf, file_path) - local lines = vim.api.nvim_buf_get_lines(buf, 0, -1, false) - local content = table.concat(lines, '\n') - local escaped_file_path = vim.fn.fnameescape(file_path) - - -- Write the buffer content to the file - local file = io.open(escaped_file_path, 'w') - if file then - file:write(content) - file:close() + if buf and vim.api.nvim_buf_is_valid(buf) then + local lines = vim.api.nvim_buf_get_lines(buf, 0, -1, false) + local content = table.concat(lines, '\n') + local escaped_file_path = vim.fn.fnameescape(file_path) + + -- Write the buffer content to the file + local file = io.open(escaped_file_path, 'w') + if file then + file:write(content) + file:close() + end end end +-- Function to create or open the scratch buffer function OpenScratchBuffer(file_path) local buf = vim.api.nvim_create_buf(true, false) @@ -690,27 +706,30 @@ function OpenScratchBuffer(file_path) return buf end +-- Function to open the scratch buffer in a window function OpenScratchWindow(buf, current_window_type) - if current_window_type == 'float' then - local opts = { - relative = 'win', - width = 120, - height = 10, - border = 'single', - row = 20, - col = 20, - } - scratch_win = vim.api.nvim_open_win(buf, true, opts) - -- Go to the last line of the buffer - vim.api.nvim_win_set_cursor(0, { vim.api.nvim_buf_line_count(buf), 1 }) - elseif current_window_type == 'vertical' then - vim.cmd('vsplit') - vim.api.nvim_win_set_buf(0, buf) - scratch_win = 0 - elseif current_window_type == 'horizontal' then - vim.cmd('split') - vim.api.nvim_win_set_buf(0, buf) - scratch_win = 0 + if buf and vim.api.nvim_buf_is_valid(buf) then + if current_window_type == 'float' then + local opts = { + relative = 'win', + width = 120, + height = 10, + border = 'single', + row = 20, + col = 20, + } + scratch_win = vim.api.nvim_open_win(buf, true, opts) + -- Go to the last line of the buffer + vim.api.nvim_win_set_cursor(scratch_win, { vim.api.nvim_buf_line_count(buf), 1 }) + elseif current_window_type == 'vertical' then + vim.cmd('vsplit') + vim.api.nvim_win_set_buf(0, buf) + scratch_win = 0 + elseif current_window_type == 'horizontal' then + vim.cmd('split') + vim.api.nvim_win_set_buf(0, buf) + scratch_win = 0 + end end end @@ -814,5 +833,19 @@ vim.api.nvim_create_autocmd({ 'BufNew' }, { -------------------------------------------------- +-- Delete [No Name] buffers +vim.api.nvim_create_autocmd('BufHidden', { + desc = 'Delete [No Name] buffers', + callback = function(event) + if event.file == '' and vim.bo[event.buf].buftype == '' and not vim.bo[event.buf].modified then + vim.schedule(function() + pcall(vim.api.nvim_buf_delete, event.buf, {}) + end) + end + end, +}) + +-------------------------------------------------- + -- ... return M |
