Skip to content

User Configurations

TheBlob42 edited this page Oct 30, 2022 · 1 revision

User Configurations

Custom DREX configurations by other users of the plugin

If you a nice configuration that you want to share with the community please open an issue so we can chat about it

  • per buffer clipboard
  • automatically clear the clipboard after pasting
  • "flexible" action depending on the current state of the clipboard
-- file path: ~/.config/nvim/lua/user/drex.lua

local clearClipboard = '<cmd>lua require("drex.clipboard").clear_clipboard()<cr>'

require('drex.config').configure {
    keybindings = {
        ['n'] = {
            -- open single or multiple files (if drex clipboard is populated), or expand the line if drex clipboard is empty and it's a directory
            ['<cr>'] = '<cmd>lua require("user.drex").open()<cr>',
            -- just clear drex clipboard
            ['<c-c>'] = clearClipboard,
            -- use the user defined function "flexible_action" to delete single or multiple files (if drex clipboard is populated)
            ['d'] = '<cmd>lua require("user.drex").flexible_action(require("drex.actions.files").delete)<cr>',
            -- same concept of "delete" action, but for buffer renaming (i love using my vim keys for editing file name)
            --- since i use bulk renaming usually for changing file name, i appended "3j$T/" after the function call, because i want my cursor to skip drex comments on the first three lines and to be right at the beginning of the file name
            ---- example: /home/rodolfo/test.dir/<CURSOR>test_file.lua
            ['r'] = '<cmd>lua require("user.drex").flexible_action(require("drex.actions.files").multi_rename)<cr>3j$T/',
            -- i like to clear drex clipboard after copying or moving files
            ['p'] = '<cmd>lua require("drex.actions.files").copy_and_paste()<cr>' .. clearClipboard,
            ['P'] = '<cmd>lua require("drex.actions.files").cut_and_move()<cr>' .. clearClipboard
        }
    },
    -- also clear drex clipboard when closing the plugin
    on_leave = function ()
        require('drex.clipboard').clear_clipboard()
    end,
}


-- keymaps
vim.keymap.set('n', '<leader>p', '<cmd>lua require("user.drex").find_element("%")<cr>', { noremap = true, silent = true })

-- customized functions
local D = {}

local api = vim.api
local utils = require('drex.utils')
local elements = require('drex.elements')
local open_directory_buffer = require('drex').open_directory_buffer
local get_clipboard_entries = require('drex.clipboard').get_clipboard_entries

-- "find_element" opens drex "full screen" and find the buffer file, if the buffer is not empty, otherwise opens the root directory of the project
function D.find_element(path)
    path = utils.expand_path(path)
    open_directory_buffer('.')
    -- here i use pcall because drex always throws me an error if i try to call "focus_element" with an empty buffer
    pcall(elements.focus_element, api.nvim_get_current_win(), path)
end

-- "flexible_action" basically checks if drex clipboard is populated so "delete" and "multi_rename" actions can be called for a single file or for multiple files
--- perhaps this is a nice improvement for the plugin in general and doesn't cost that much.. just one extra if statement and a function call
function D.flexible_action(action)
    local clipboard = get_clipboard_entries()

    -- if there are entries on drex clipboard, use "clipboard" as a parameter of the callback function
    local target = 'clipboard'
    -- if drex clipboard is empty, use "line" as a parameter of the callback function
    if vim.tbl_isempty(clipboard) then
        target = 'line'
    end

    action(target)
end

-- "open" is the function i was missing the most on drex and it basically opens multiple files, or a single file, if the clipboard is empty
function D.open()
    local clipboard = get_clipboard_entries()

    -- if drex clipboard is empty, open the file or expand the directory on the line
    if vim.tbl_isempty(clipboard) then
        elements.expand_element()
    -- if drex clipboard is populated, open multiple files
    else
        -- map over the clipboard table
        vim.tbl_map(
            function(file)
                -- just open the file if it is not a directory, otherwise the directory would open as a drex buffer with root on that directory (this behavior is not desired)
                if vim.fn.isdirectory(file) == 0 then vim.cmd('e ' .. file)
                end
            end,
            clipboard
        )
    end
end

return D
Clone this wiki locally