" Define mappings
nnoremap <silent><buffer><expr> <cr>
\ defx#is_directory() ?
\ defx#do_action('open_tree', ['toggle']) :
\ defx#do_action('open')
nnoremap <silent><buffer><expr> o
\ defx#do_action('open')
nnoremap <silent><buffer><expr> c
\ defx#do_action('copy')
nnoremap <silent><buffer><expr> m
\ defx#do_action('move')
nnoremap <silent><buffer><expr> p
\ defx#do_action('paste')
nnoremap <silent><buffer><expr> P
\ defx#do_action('preview')
nnoremap <silent><buffer><expr> D
\ defx#do_action('new_directory')
nnoremap <silent><buffer><expr> i
\ defx#do_action('new_file')
nnoremap <silent><buffer><expr> I
\ defx#do_action('new_multiple_files')
nnoremap <silent><buffer><expr> C
\ defx#do_action('toggle_columns',
\ 'mark:indent:icon:filename:type')
nnoremap <silent><buffer><expr> S
\ defx#do_action('toggle_sort', 'time')
nnoremap <silent><buffer><expr> d
\ defx#do_action('remove')
nnoremap <silent><buffer><expr> r
\ defx#do_action('rename')
nnoremap <silent><buffer><expr> !
\ defx#do_action('execute_command')
nnoremap <silent><buffer><expr> x
\ defx#do_action('execute_system')
nnoremap <silent><buffer><expr> yy
\ defx#do_action('yank_path')
nnoremap <silent><buffer><expr> .
\ defx#do_action('toggle_ignored_files')
nnoremap <silent><buffer><expr> ;
\ defx#do_action('repeat')
nnoremap <silent><buffer><expr> u
\ defx#do_action('cd', ['..'])
nnoremap <silent><buffer><expr> ~
\ defx#do_action('cd')
nnoremap <silent><buffer><expr> q
\ defx#do_action('quit')
nnoremap <silent><buffer><expr> s
\ defx#do_action('toggle_select') . 'j'
nnoremap <silent><buffer><expr> *
\ defx#do_action('toggle_select_all')
nnoremap <silent><buffer><expr> j
\ line('.') == line('$') ? 'gg' : 'j'
nnoremap <silent><buffer><expr> k
\ line('.') == 1 ? 'G' : 'k'
" nnoremap <silent><buffer><expr> cd
" \ defx#do_action('change_vim_cwd')
function! s:init_fern() abort
" Use 'select' instead of 'edit' for default 'open' action
" nmap <buffer> <cr> <plug>(fern-action-open-or-expand)
nmap <buffer><expr>
\ <plug>(fern-my-open-or-expand-or-collapse)
\ fern#smart#leaf(
\ "\<Plug>(fern-action-open)",
\ "\<Plug>(fern-action-expand:stay)",
\ "\<Plug>(fern-action-collapse)",
\ )
nmap <buffer> <cr> <plug>(fern-my-open-or-expand-or-collapse)
nmap <buffer> o <plug>(fern-action-open)
nmap <buffer> u <plug>(fern-action-leave)
nmap <buffer> i <plug>(fern-action-new-file)
nmap <buffer> d <plug>(fern-action-new-dir)
nmap <buffer> D <plug>(fern-action-remove)
nmap <buffer> y <plug>(fern-action-yank)
nmap <buffer> x <plug>(fern-action-move)
nmap <buffer> p <plug>(fern-action-paste)
nmap <buffer> r <plug>(fern-action-rename:below)
nmap <buffer> m <plug>(fern-action-mark)
nmap <buffer> cm <plug>(fern-action-mark:clear)